GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
RangeAllocator.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_RANGE_ALLOCATOR_HPP
5#define GREM_CORE_DATA_RANGE_ALLOCATOR_HPP
6
8
12
13#include <algorithm> // std::lower_bound
14#include <limits> // std::numeric_limits
15#include <utility> // std::declval
16
17namespace grem {
18
19template <typename Index>
21 using size_type = decltype(std::declval<Index>() - std::declval<Index>());
22
23 Index index = std::numeric_limits<Index>::max();
24
25 [[nodiscard]] constexpr size_type size() const noexcept {
26 return (index == std::numeric_limits<Index>::max()) ? size_type{0} : size_type{1};
27 }
28
29 [[nodiscard]] constexpr bool empty() const noexcept {
30 return index == std::numeric_limits<Index>::max();
31 }
32};
33
34template <typename Index>
36 using size_type = decltype(std::declval<Index>() - std::declval<Index>());
37
38 Index begin{};
39 Index end{};
40
41 [[nodiscard]] constexpr size_type size() const noexcept {
42 return end - begin;
43 }
44
45 [[nodiscard]] constexpr bool empty() const noexcept {
46 return begin == end;
47 }
48};
49
50template <typename Index>
52public:
53 using size_type = decltype(std::declval<Index>() - std::declval<Index>());
54
55 RangeAllocator() noexcept = default;
56 ~RangeAllocator() = default;
57
59 RangeAllocator(RangeAllocator&&) noexcept = default;
60 RangeAllocator& operator=(const RangeAllocator&) = delete;
61 RangeAllocator& operator=(RangeAllocator&&) noexcept = default;
62
63 explicit RangeAllocator(Index begin, Index end)
64 : fullRange{.begin = begin, .end = end} {}
65
66 void clear() noexcept {
67 fullRange = {
68 .begin = 0,
69 .end = std::numeric_limits<Index>::max(),
70 };
71 freeRanges.clear();
72 freeRanges.push_back(fullRange);
73 allocatedRangeCount = 0;
74 }
75
77 if (const Optional<RangeAllocation<Index>> result = allocateRange(1)) {
78 return IndexAllocation<Index>{.index = result->begin};
79 }
80 return {};
81 }
82
84 if (size == 0) {
86 }
87
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;
94 break;
95 }
96 if (smallestFittingFreeRangeIndex == freeRanges.size() || freeRangeSize < freeRanges[smallestFittingFreeRangeIndex].size()) {
97 smallestFittingFreeRangeIndex = freeRangeIndex;
98 }
99 }
100 }
101 if (smallestFittingFreeRangeIndex == freeRanges.size()) {
102 return {};
103 }
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);
109 }
110 if (freeRangeEnd - freeRangeBegin == size) {
111 freeRanges.erase(freeRanges.begin() + static_cast<typename decltype(freeRanges)::difference_type>(smallestFittingFreeRangeIndex));
112 } else {
113 freeRanges[smallestFittingFreeRangeIndex].begin += size;
114 }
115 return RangeAllocation<Index>{.begin = freeRangeBegin, .end = freeRangeBegin + size};
116 }
117
118 void deallocateIndex(IndexAllocation<Index> allocation) noexcept {
119 if (allocation.empty()) {
120 return;
121 }
122
123 deallocateRange(RangeAllocation<Index>{.begin = allocation.index, .end = allocation.index + 1});
124 }
125
126 void deallocateRange(RangeAllocation<Index> allocation) noexcept {
127 GREM_ASSERT(allocation.begin <= allocation.end);
128 GREM_ASSERT(fullRange.begin < fullRange.end);
129 GREM_ASSERT(allocation.begin >= fullRange.begin);
130 GREM_ASSERT(allocation.end <= fullRange.end);
131
132 if (allocation.empty()) {
133 return;
134 }
135
136 GREM_ASSERT(allocatedRangeCount > 0);
137
138 const auto it = std::lower_bound(freeRanges.begin(), freeRanges.end(), allocation,
139 [](const RangeAllocation<Index>& a, const RangeAllocation<Index>& b) -> bool { return a.begin < b.begin; });
140
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);
145 } else {
146 (it - 1)->end = allocation.end;
147 }
148 --allocatedRangeCount;
149 return;
150 }
151
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);
156 } else {
157 it->begin = allocation.begin;
158 }
159 --allocatedRangeCount;
160 return;
161 }
162
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;
167 }
168
169 void expandFrontTo(Index newBegin) {
170 GREM_ASSERT(newBegin <= fullRange.begin);
171 if (newBegin < fullRange.begin) {
172 if (freeRanges.empty()) {
173 freeRanges.push_back(RangeAllocation<Index>{.begin = newBegin, .end = fullRange.begin});
174 } else {
175 freeRanges.front().begin = newBegin;
176 }
177 fullRange.begin = newBegin;
178 }
179 }
180
181 void expandBackTo(Index newEnd) {
182 GREM_ASSERT(newEnd >= fullRange.end);
183 if (newEnd > fullRange.end) {
184 if (freeRanges.empty()) {
185 freeRanges.push_back(RangeAllocation<Index>{.begin = fullRange.end, .end = newEnd});
186 } else {
187 freeRanges.back().end = newEnd;
188 }
189 fullRange.end = newEnd;
190 }
191 }
192
193 [[nodiscard]] Index getFullRangeBegin() const noexcept {
194 return fullRange.begin;
195 }
196
197 [[nodiscard]] Index getFullRangeEnd() const noexcept {
198 return fullRange.end;
199 }
200
201 [[nodiscard]] size_type getFullRangeSize() const noexcept {
202 return fullRange.size();
203 }
204
205 [[nodiscard]] Index getUsedRangeBegin() const noexcept {
206 if (freeRanges.empty() || freeRanges.front().begin != fullRange.begin) {
207 return fullRange.begin;
208 }
209 return freeRanges.front().end;
210 }
211
212 [[nodiscard]] Index getUsedRangeEnd() const noexcept {
213 if (freeRanges.empty() || freeRanges.back().end != fullRange.end) {
214 return fullRange.end;
215 }
216 return freeRanges.back().begin;
217 }
218
219 [[nodiscard]] size_type getUsedRangeSize() const noexcept {
221 }
222
223private:
224 RangeAllocation<Index> fullRange{
225 .begin = 0,
226 .end = std::numeric_limits<Index>::max(),
227 };
228 SmallBuffer<RangeAllocation<Index>, 2> freeRanges{fullRange};
229 size_t allocatedRangeCount = 0;
230};
231
232} // namespace grem
233
234#endif
#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 aliases.hpp:9
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