35#include <gtest/gtest.h>
52 Rectangle rect(
const int x1,
const int y1,
const int x2,
const int y2)
58 Point pt(
const int x,
const int y)
63 template <
typename Tree>
71 Array<int> brute_intersects(
const Array<std::pair<Rectangle, int>> &ref,
75 for (
const auto &[b,
id] : ref)
86 for (
const auto &[b,
id] : ref)
93 template <
typename Tree>
102 template <
size_t Max,
size_t Min>
103 void randomized_parity(
const unsigned seed,
const int iters,
const int space,
107 std::uniform_int_distribution<int> op_dist(0, 2);
108 std::uniform_int_distribution<int> coord(0, space);
109 std::uniform_int_distribution<int> extent(0, ext);
115 auto random_rect = [&]()
117 const int x1 = coord(
rng);
118 const int y1 = coord(
rng);
119 return rect(x1,
y1, x1 + extent(
rng),
y1 + extent(
rng));
122 for (
int iter = 0; iter < iters; ++iter)
128 const int id = next_id++;
130 ref.
append(std::make_pair(b,
id));
134 std::uniform_int_distribution<size_t> pick(0, ref.
size() - 1);
135 const size_t k = pick(
rng);
136 ASSERT_TRUE(tree.
erase(ref(
k).first, ref(
k).second));
137 std::swap(ref(
k), ref(ref.
size() - 1));
138 [[maybe_unused]]
const auto popped = ref.
remove_last();
143 EXPECT_EQ(sorted_intersects(tree, q), brute_intersects(ref, q));
145 EXPECT_EQ(sorted_contains(tree, p), brute_contains(ref, p));
148 ASSERT_TRUE(tree.
verify()) <<
"invariant broken at iter " << iter;
152 for (
int x = 0; x <= space; x += 9)
153 for (
int y = 0;
y <= space;
y += 9)
156 ASSERT_EQ(sorted_intersects(tree, q), brute_intersects(ref, q));
157 ASSERT_EQ(sorted_contains(tree, pt(x,
y)), brute_contains(ref, pt(x,
y)));
166 EXPECT_EQ(tree.
size(), 0u);
167 EXPECT_EQ(tree.
height(), 0u);
168 EXPECT_TRUE(tree.
verify());
169 EXPECT_FALSE(tree.
erase(rect(0, 0, 1, 1), 5));
175 tree.
insert(rect(0, 0, 2, 2), 42);
176 EXPECT_EQ(tree.
size(), 1u);
177 EXPECT_EQ(sorted_intersects(tree, rect(1, 1, 5, 5)), build_array<int>(42));
178 EXPECT_TRUE(tree.
erase(rect(0, 0, 2, 2), 42));
180 EXPECT_EQ(tree.
height(), 0u);
181 EXPECT_TRUE(tree.
verify());
189 for (
int i = 0; i < 500; ++i)
191 tree.
insert(rect(i % 50, i / 50, i % 50 + 3, i / 50 + 3), i);
192 ASSERT_TRUE(tree.
verify());
194 EXPECT_EQ(tree.
size(), 500u);
195 EXPECT_GT(tree.
height(), 1u);
198 for (
int i = 0; i < 500; ++i)
200 const Rectangle b = rect(i % 50, i / 50, i % 50 + 3, i / 50 + 3);
201 const Array<int> hits = sorted_intersects(tree, b);
202 EXPECT_TRUE(std::find(hits.
begin(), hits.
end(), i) != hits.
end());
209 for (
int i = 0; i < 500; ++i)
210 tree.
insert(rect(i % 50, i / 50, i % 50 + 3, i / 50 + 3), i);
213 ASSERT_LT(snap.root, snap.nodes.size());
215 EXPECT_EQ(total_entries, tree.
size());
222 for (
int i = 0; i < 25; ++i)
224 ASSERT_TRUE(tree.
verify());
225 EXPECT_EQ(sorted_intersects(tree, rect(4, 4, 5, 5)).
size(), 25u);
228 ASSERT_TRUE(tree.
verify());
229 EXPECT_EQ(sorted_intersects(tree,
box).
size(), 24u);
235 for (
int i = 0; i < 40; ++i)
236 tree.
insert(rect(i, 5, i, 5), i);
237 for (
int i = 0; i < 20; ++i)
238 tree.
insert(rect(0, i, 40, i), 100 + i);
239 ASSERT_TRUE(tree.
verify());
240 EXPECT_EQ(tree.
size(), 60u);
242 const Array<int> hits = sorted_contains(tree, pt(10, 5));
243 EXPECT_TRUE(std::find(hits.
begin(), hits.
end(), 10) != hits.
end());
244 EXPECT_TRUE(std::find(hits.
begin(), hits.
end(), 105) != hits.
end());
250 for (
int i = 0; i < 60; ++i)
251 original.
insert(rect(i, i, i + 2, i + 2), i);
254 ASSERT_TRUE(
copy.verify());
255 EXPECT_EQ(
copy.size(), original.
size());
256 for (
int i = 0; i < 30; ++i)
257 ASSERT_TRUE(original.
erase(rect(i, i, i + 2, i + 2), i));
258 EXPECT_EQ(original.
size(), 30u);
259 EXPECT_EQ(
copy.size(), 60u);
260 ASSERT_TRUE(original.
verify());
261 ASSERT_TRUE(
copy.verify());
264 EXPECT_EQ(moved.
size(), 60u);
265 EXPECT_TRUE(moved.
verify());
266 EXPECT_TRUE(
copy.is_empty());
267 EXPECT_TRUE(
copy.verify());
269 copy.insert(rect(-5, -5, -4, -4), -5);
270 EXPECT_EQ(
copy.size(), 1u);
271 EXPECT_TRUE(
copy.verify());
277 for (
int i = 0; i < 40; ++i)
278 tree.
insert(rect(i, i, i + 1, i + 1), std::make_unique<int>(i));
279 ASSERT_TRUE(tree.
verify());
280 EXPECT_EQ(tree.
size(), 40u);
284 [&](
const Rectangle &,
const std::unique_ptr<int> &) { ++seen; });
292 for (
int i = 0; i < 100; ++i)
294 const Rectangle b = rect(i % 20, i / 20, i % 20 + 3, i / 20 + 2);
296 ref.
append(std::make_pair(b, i));
298 ASSERT_TRUE(source.verify());
302 EXPECT_EQ(assigned.
size(), source.size());
303 EXPECT_TRUE(assigned.
verify());
306 moved = std::move(assigned);
307 EXPECT_EQ(moved.
size(), 100u);
308 EXPECT_TRUE(moved.
verify());
310 EXPECT_TRUE(assigned.
verify());
312 assigned.
insert(rect(-10, -10, -9, -9), -10);
313 EXPECT_EQ(assigned.
size(), 1u);
314 EXPECT_TRUE(assigned.
verify());
317 for (
const auto &[b,
id] : ref)
319 ASSERT_TRUE(moved.
erase(b,
id));
320 ASSERT_TRUE(moved.
verify());
323 EXPECT_EQ(moved.
height(), 0u);
326 EXPECT_TRUE(source.is_empty());
327 EXPECT_TRUE(source.verify());
328 source.insert(rect(-20, -20, -19, -19), -20);
329 EXPECT_EQ(source.size(), 1u);
330 EXPECT_TRUE(source.verify());
335 randomized_parity<16, 8>(20260714u, 4000, 90, 12);
340 randomized_parity<4, 2>(913u, 3000, 60, 8);
High-level sorting functions for Aleph containers.
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & append(const T &data)
Append a copy of data
Represents a point with rectangular coordinates in a 2D plane.
Dynamic R-tree indexing axis-aligned rectangles by payload.
DebugSnapshot debug_snapshot() const
Capture the full tree structure for visualization/debugging.
void insert(const Rectangle &bbox, const Payload &value)
Insert a (bbox, value) entry, copying value.
bool erase(const Rectangle &bbox, const Payload &value)
Remove one entry equal to (bbox, value).
size_t height() const noexcept
Return the number of node levels (0 when empty, 1 for a lone leaf).
bool is_empty() const noexcept
Return true when the tree has no entries.
size_t size() const noexcept
Return the number of stored entries.
void for_each_intersecting(const Rectangle &rect, F &&f) const
Invoke f for every entry whose bbox intersects rect.
bool verify() const
Verify the R-tree structural invariants.
void clear() noexcept
Remove all entries.
An axis-aligned rectangle.
QuadTree - Hierarchical spatial index for 2D points.
iterator end() noexcept
Return an STL-compatible end iterator.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_y1_function > > y1(const __gmp_expr< T, U > &expr)
size_t check_snapshot_node(const Snapshot &snap, const size_t idx, const size_t expected_depth)
Recursively validates the structural invariants of a DebugSnapshot produced by RTree::debug_snapshot(...
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
bool contains(const std::string_view &str, const std::string_view &substr)
Check if substr appears inside str.
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
mpq_class Geom_Number
Numeric type used by the geometry module.
Shared DebugSnapshot structural-invariant checker for RTree and RStarTree unit tests.
static Rectangle box(const int x1, const int y1, const int x2, const int y2)
R*-tree: an R-tree tuned with the Beckmann-Kriegel-Schneider-Seeger heuristics.