GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
SquareAllocator.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_SQUARE_ALLOCATOR_HPP
5#define GREM_CORE_DATA_SQUARE_ALLOCATOR_HPP
6
8
14
15#include <new> // std::bad_array_new_length
16#include <utility> // std::move
17
18namespace grem {
19
20template <typename Coordinate>
22 Coordinate x{};
23 Coordinate y{};
24 Coordinate allocatedWidth{};
25};
26
27template <typename Coordinate>
29public:
30 SquareAllocator() noexcept = default;
31 ~SquareAllocator() = default;
32
34 SquareAllocator(SquareAllocator&&) noexcept = default;
35 SquareAllocator& operator=(const SquareAllocator&) = delete;
36 SquareAllocator& operator=(SquareAllocator&&) noexcept = default;
37
38 explicit SquareAllocator(Coordinate initialWidth)
39 : fullWidth(roundUpToPowerOf2(initialWidth)) {}
40
41 void clear() noexcept {
42 fullSquare.clear(fullWidth);
43 }
44
45 [[nodiscard]] Optional<SquareAllocation<Coordinate>> allocateSquare(Coordinate width) {
46 if (width == 0) {
48 }
49
50 const Coordinate allocatedWidth = roundUpToPowerOf2(width);
51 if (allocatedWidth == 0) {
52 throw std::bad_array_new_length{};
53 }
54
55 return fullSquare.allocate(0, 0, fullWidth, allocatedWidth);
56 }
57
59 GREM_ASSERT(allocation.allocatedWidth <= fullWidth);
60 GREM_ASSERT(allocation.x <= fullWidth - allocation.allocatedWidth);
61 GREM_ASSERT(allocation.y <= fullWidth - allocation.allocatedWidth);
62
63 if (allocation.allocatedWidth == 0) {
64 return;
65 }
66
67 GREM_ASSERT(isPowerOf2(allocation.allocatedWidth));
68 fullSquare.deallocate(allocation.x, allocation.y, fullWidth, allocation.allocatedWidth);
69 }
70
71 void expandTo(Coordinate newWidth) {
72 GREM_ASSERT(newWidth >= fullWidth);
73
74 if (fullWidth == 0) {
75 const Coordinate newFullWidth = roundUpToPowerOf2(newWidth);
76 if ((newFullWidth == 0 && newWidth > 0) || newFullWidth > Limits<Coordinate>::MAX / newFullWidth) {
77 throw std::bad_array_new_length{};
78 }
79 fullWidth = newFullWidth;
80 fullSquare.largestAvailableWidth = newFullWidth;
81 return;
82 }
83
84 while (newWidth > fullWidth) {
85 if (fullWidth > Limits<Coordinate>::MAX / 2) {
86 throw std::bad_array_new_length{};
87 }
88 const Coordinate newFullWidth = fullWidth * 2;
89 if (newFullWidth > Limits<Coordinate>::MAX / newFullWidth) {
90 throw std::bad_array_new_length{};
91 }
92 Square newFullSquare{(fullSquare.largestAvailableWidth == fullWidth) ? newFullWidth : fullWidth};
93 newFullSquare.quadrants = UniquePointer<Array<Square, 4>>::create(Array{std::move(fullSquare), Square{fullWidth}, Square{fullWidth}, Square{fullWidth}});
94 fullSquare = std::move(newFullSquare);
95 fullWidth = newFullWidth;
96 }
97 }
98
99 [[nodiscard]] Coordinate getFullWidth() const noexcept {
100 return fullWidth;
101 }
102
103private:
104 struct Square {
105 Coordinate largestAvailableWidth;
107
108 explicit Square(Coordinate largestAvailableWidth)
109 : largestAvailableWidth(largestAvailableWidth) {}
110
111 void clear(Coordinate width) noexcept {
112 largestAvailableWidth = width;
113 if (quadrants) {
114 const Coordinate quadrantWidth = width / 2;
115 for (Square& quadrant : *quadrants) {
116 quadrant.clear(quadrantWidth);
117 }
118 }
119 }
120
121 [[nodiscard]] Optional<SquareAllocation<Coordinate>> allocate(Coordinate x, Coordinate y, Coordinate width, Coordinate allocatedWidth) {
122 if (largestAvailableWidth < allocatedWidth) {
123 return {};
124 }
125 if (width == allocatedWidth) {
126 largestAvailableWidth = 0;
127 return SquareAllocation<Coordinate>{.x = x, .y = y, .allocatedWidth = allocatedWidth};
128 }
129 const Coordinate quadrantWidth = width / 2;
130 if (!quadrants) {
131 quadrants = UniquePointer<Array<Square, 4>>::create(Array{Square{quadrantWidth}, Square{quadrantWidth}, Square{quadrantWidth}, Square{quadrantWidth}});
132 }
133 Optional<SquareAllocation<Coordinate>> result = (*quadrants)[0].allocate(x, y, quadrantWidth, allocatedWidth);
134 if (!result) {
135 result = (*quadrants)[1].allocate(x + quadrantWidth, y, quadrantWidth, allocatedWidth);
136 if (!result) {
137 result = (*quadrants)[2].allocate(x, y + quadrantWidth, quadrantWidth, allocatedWidth);
138 if (!result) {
139 result = (*quadrants)[3].allocate(x + quadrantWidth, y + quadrantWidth, quadrantWidth, allocatedWidth);
140 }
141 }
142 }
143 const Array quadrantLargestAvailableWidths{
144 (*quadrants)[0].largestAvailableWidth,
145 (*quadrants)[1].largestAvailableWidth,
146 (*quadrants)[2].largestAvailableWidth,
147 (*quadrants)[3].largestAvailableWidth,
148 };
149 largestAvailableWidth =
150 max(max(max(quadrantLargestAvailableWidths[0], quadrantLargestAvailableWidths[1]), quadrantLargestAvailableWidths[2]), quadrantLargestAvailableWidths[3]);
151 return result;
152 }
153
154 void deallocate(Coordinate localX, Coordinate localY, Coordinate width, Coordinate allocatedWidth) noexcept {
155 GREM_ASSERT(width >= allocatedWidth);
156 if (width == allocatedWidth) {
157 GREM_ASSERT(localX == 0 && localY == 0);
158 largestAvailableWidth = width;
159 return;
160 }
161 const Coordinate quadrantWidth = width / 2;
162 GREM_ASSERT(quadrants);
163 if (localY < quadrantWidth) {
164 if (localX < quadrantWidth) {
165 (*quadrants)[0].deallocate(localX, localY, quadrantWidth, allocatedWidth);
166 } else {
167 (*quadrants)[1].deallocate(localX - quadrantWidth, localY, quadrantWidth, allocatedWidth);
168 }
169 } else {
170 if (localX < quadrantWidth) {
171 (*quadrants)[2].deallocate(localX, localY - quadrantWidth, quadrantWidth, allocatedWidth);
172 } else {
173 (*quadrants)[3].deallocate(localX - quadrantWidth, localY - quadrantWidth, quadrantWidth, allocatedWidth);
174 }
175 }
176 const Array quadrantLargestAvailableWidths{
177 (*quadrants)[0].largestAvailableWidth,
178 (*quadrants)[1].largestAvailableWidth,
179 (*quadrants)[2].largestAvailableWidth,
180 (*quadrants)[3].largestAvailableWidth,
181 };
182 if (quadrantLargestAvailableWidths[0] == quadrantWidth && quadrantLargestAvailableWidths[1] == quadrantWidth && quadrantLargestAvailableWidths[2] == quadrantWidth &&
183 quadrantLargestAvailableWidths[3] == quadrantWidth) {
184 largestAvailableWidth = width;
185 } else {
186 largestAvailableWidth =
187 max(max(max(quadrantLargestAvailableWidths[0], quadrantLargestAvailableWidths[1]), quadrantLargestAvailableWidths[2]), quadrantLargestAvailableWidths[3]);
188 }
189 }
190 };
191
192 Coordinate fullWidth{};
193 Square fullSquare{fullWidth};
194};
195
196} // namespace grem
197
198#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Coordinate getFullWidth() const noexcept
Definition SquareAllocator.hpp:99
void expandTo(Coordinate newWidth)
Definition SquareAllocator.hpp:71
void clear() noexcept
Definition SquareAllocator.hpp:41
SquareAllocator() noexcept=default
void deallocateSquare(SquareAllocation< Coordinate > allocation) noexcept
Definition SquareAllocator.hpp:58
Optional< SquareAllocation< Coordinate > > allocateSquare(Coordinate width)
Definition SquareAllocator.hpp:45
Nullable RAII data pointer with exclusive ownership of an object that is automatically destroyed on p...
Definition UniquePointer.hpp:77
Definition aliases.hpp:9
Array(T, Us...) -> Array< T, 1+sizeof...(Us)>
GREM_ALWAYS_INLINE constexpr bool isPowerOf2(UnsignedInteger value) noexcept
Check if a given integer is a power of 2, i.e. if the number appears in the infinite sequence 2^0,...
Definition fundamentals.hpp:781
GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToPowerOf2(UnsignedInteger value) noexcept
Get the smallest integral power of 2 that is not less than a given value.
Definition fundamentals.hpp:794
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
Definition Array.hpp:20
static constexpr T MAX
Definition fundamentals.hpp:29
Definition SquareAllocator.hpp:21
Coordinate allocatedWidth
Definition SquareAllocator.hpp:24
Coordinate x
Definition SquareAllocator.hpp:22
Coordinate y
Definition SquareAllocator.hpp:23
Flat 2D axis-aligned square shape with a position and width.
Definition geometry.hpp:390