4#ifndef GREM_CORE_DATA_RANGE_ALLOCATOR_HPP
5#define GREM_CORE_DATA_RANGE_ALLOCATOR_HPP
19template <
typename Index>
21 using size_type =
decltype(std::declval<Index>() - std::declval<Index>());
23 Index
index = std::numeric_limits<Index>::max();
29 [[nodiscard]]
constexpr bool empty() const noexcept {
30 return index == std::numeric_limits<Index>::max();
34template <
typename Index>
36 using size_type =
decltype(std::declval<Index>() - std::declval<Index>());
45 [[nodiscard]]
constexpr bool empty() const noexcept {
50template <
typename Index>
53 using size_type =
decltype(std::declval<Index>() - std::declval<Index>());
64 : fullRange{.begin = begin, .end = end} {}
69 .end = std::numeric_limits<Index>::max(),
72 freeRanges.push_back(fullRange);
73 allocatedRangeCount = 0;
88 size_t smallestFittingFreeRangeIndex = freeRanges.size();
89 for (
size_t freeRangeIndex = 0; freeRangeIndex < freeRanges.size(); ++freeRangeIndex) {
90 const size_type freeRangeSize = freeRanges[freeRangeIndex].size();
91 if (freeRangeSize >= size) {
92 if (freeRangeSize == size) {
93 smallestFittingFreeRangeIndex = freeRangeIndex;
96 if (smallestFittingFreeRangeIndex == freeRanges.size() || freeRangeSize < freeRanges[smallestFittingFreeRangeIndex].size()) {
97 smallestFittingFreeRangeIndex = freeRangeIndex;
101 if (smallestFittingFreeRangeIndex == freeRanges.size()) {
104 const Index freeRangeBegin = freeRanges[smallestFittingFreeRangeIndex].begin;
105 const Index freeRangeEnd = freeRanges[smallestFittingFreeRangeIndex].end;
106 ++allocatedRangeCount;
107 if (allocatedRangeCount + 1 > freeRanges.capacity()) {
108 freeRanges.reserve(allocatedRangeCount + 1);
110 if (freeRangeEnd - freeRangeBegin == size) {
111 freeRanges.erase(freeRanges.begin() +
static_cast<typename decltype(freeRanges)::difference_type
>(smallestFittingFreeRangeIndex));
113 freeRanges[smallestFittingFreeRangeIndex].begin += size;
119 if (allocation.empty()) {
132 if (allocation.empty()) {
138 const auto it = std::lower_bound(freeRanges.begin(), freeRanges.end(), allocation,
141 if (it != freeRanges.begin() && allocation.begin == (it - 1)->end) {
142 if (it != freeRanges.end() && allocation.end == it->begin) {
143 (it - 1)->end = it->end;
144 freeRanges.erase(it);
146 (it - 1)->end = allocation.end;
148 --allocatedRangeCount;
152 if (it != freeRanges.end() && allocation.end == it->begin) {
153 if (it != freeRanges.begin() && allocation.begin == (it - 1)->end) {
154 it->begin = (it - 1)->begin;
155 freeRanges.erase(it - 1);
157 it->begin = allocation.begin;
159 --allocatedRangeCount;
163 GREM_ASSERT((it == freeRanges.begin() || allocation.begin > (it - 1)->end) &&
164 (it == freeRanges.end() || allocation.end < it->begin));
165 freeRanges.insert(it, allocation);
166 --allocatedRangeCount;
171 if (newBegin < fullRange.begin) {
172 if (freeRanges.empty()) {
175 freeRanges.front().begin = newBegin;
177 fullRange.begin = newBegin;
183 if (newEnd > fullRange.end) {
184 if (freeRanges.empty()) {
187 freeRanges.back().end = newEnd;
189 fullRange.end = newEnd;
194 return fullRange.begin;
198 return fullRange.end;
202 return fullRange.size();
206 if (freeRanges.empty() || freeRanges.front().begin != fullRange.begin) {
207 return fullRange.begin;
209 return freeRanges.front().end;
213 if (freeRanges.empty() || freeRanges.back().end != fullRange.end) {
214 return fullRange.end;
216 return freeRanges.back().begin;
226 .end = std::numeric_limits<Index>::max(),
228 SmallBuffer<RangeAllocation<Index>, 2> freeRanges{fullRange};
229 size_t allocatedRangeCount = 0;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
void clear() noexcept
Definition RangeAllocator.hpp:66
size_type getUsedRangeSize() const noexcept
Definition RangeAllocator.hpp:219
size_type getFullRangeSize() const noexcept
Definition RangeAllocator.hpp:201
decltype(std::declval< Index >() - std::declval< Index >()) size_type
Definition RangeAllocator.hpp:53
Optional< IndexAllocation< Index > > allocateIndex()
Definition RangeAllocator.hpp:76
Index getFullRangeBegin() const noexcept
Definition RangeAllocator.hpp:193
Index getUsedRangeEnd() const noexcept
Definition RangeAllocator.hpp:212
Index getUsedRangeBegin() const noexcept
Definition RangeAllocator.hpp:205
Index getFullRangeEnd() const noexcept
Definition RangeAllocator.hpp:197
void expandFrontTo(Index newBegin)
Definition RangeAllocator.hpp:169
Optional< RangeAllocation< Index > > allocateRange(size_type size)
Definition RangeAllocator.hpp:83
void expandBackTo(Index newEnd)
Definition RangeAllocator.hpp:181
void deallocateRange(RangeAllocation< Index > allocation) noexcept
Definition RangeAllocator.hpp:126
RangeAllocator() noexcept=default
void deallocateIndex(IndexAllocation< Index > allocation) noexcept
Definition RangeAllocator.hpp:118
Definition RangeAllocator.hpp:20
constexpr size_type size() const noexcept
Definition RangeAllocator.hpp:25
constexpr bool empty() const noexcept
Definition RangeAllocator.hpp:29
decltype(std::declval< Index >() - std::declval< Index >()) size_type
Definition RangeAllocator.hpp:21
Index index
Definition RangeAllocator.hpp:23
Definition RangeAllocator.hpp:35
constexpr size_type size() const noexcept
Definition RangeAllocator.hpp:41
Index begin
Definition RangeAllocator.hpp:38
Index end
Definition RangeAllocator.hpp:39
decltype(std::declval< Index >() - std::declval< Index >()) size_type
Definition RangeAllocator.hpp:36
constexpr bool empty() const noexcept
Definition RangeAllocator.hpp:45