35#include <gtest/gtest.h>
55 Rectangle rect(
const int x1,
const int y1,
const int x2,
const int y2)
61 Point pt(
const int x,
const int y)
66 template <
typename Tree>
74 template <
typename Tree>
82 Array<int> brute_intersects(
const Array<std::pair<Rectangle, int>> &ref,
86 for (
const auto &[b,
id] : ref)
97 for (
const auto &[b,
id] : ref)
104 struct CopyConstructibleNonCopyAssignablePayload
108 CopyConstructibleNonCopyAssignablePayload() =
default;
109 explicit CopyConstructibleNonCopyAssignablePayload(
const int v) :
value(v) {}
111 CopyConstructibleNonCopyAssignablePayload(
112 const CopyConstructibleNonCopyAssignablePayload &) =
default;
113 CopyConstructibleNonCopyAssignablePayload(
114 CopyConstructibleNonCopyAssignablePayload &&) noexcept = default;
116 CopyConstructibleNonCopyAssignablePayload &operator = (
117 const CopyConstructibleNonCopyAssignablePayload &) = delete;
118 CopyConstructibleNonCopyAssignablePayload &operator = (
119 CopyConstructibleNonCopyAssignablePayload &&) noexcept = default;
122 static_assert(
std::default_initializable<CopyConstructibleNonCopyAssignablePayload>);
123 static_assert(
std::movable<CopyConstructibleNonCopyAssignablePayload>);
124 static_assert(
std::is_copy_constructible_v<CopyConstructibleNonCopyAssignablePayload>);
125 static_assert(not
std::is_copy_assignable_v<CopyConstructibleNonCopyAssignablePayload>);
132 EXPECT_EQ(tree.
size(), 0u);
133 EXPECT_EQ(tree.
height(), 0u);
134 EXPECT_TRUE(tree.
verify());
137 EXPECT_FALSE(tree.
erase(rect(0, 0, 1, 1), 5));
143 tree.
insert(rect(0, 0, 2, 2), 42);
146 EXPECT_EQ(tree.
size(), 1u);
147 EXPECT_EQ(tree.
height(), 1u);
148 EXPECT_TRUE(tree.
verify());
150 EXPECT_EQ(sorted_intersects(tree, rect(1, 1, 5, 5)), build_array<int>(42));
151 EXPECT_EQ(sorted_intersects(tree, rect(10, 10, 20, 20)),
Array<int>());
152 EXPECT_EQ(sorted_contains(tree, pt(1, 1)), build_array<int>(42));
153 EXPECT_EQ(sorted_contains(tree, pt(9, 9)),
Array<int>());
159 tree.
insert(rect(0, 0, 2, 2), 1);
160 EXPECT_TRUE(tree.
erase(rect(0, 0, 2, 2), 1));
162 EXPECT_EQ(tree.
size(), 0u);
163 EXPECT_EQ(tree.
height(), 0u);
164 EXPECT_TRUE(tree.
verify());
170 tree.
insert(rect(0, 0, 2, 2), 1);
171 EXPECT_FALSE(tree.
erase(rect(0, 0, 2, 2), 999));
172 EXPECT_FALSE(tree.
erase(rect(5, 5, 6, 6), 1));
173 EXPECT_EQ(tree.
size(), 1u);
174 EXPECT_TRUE(tree.
verify());
180 for (
int i = 0; i < 200; ++i)
182 tree.
insert(rect(i, i, i + 1, i + 1), i);
183 ASSERT_TRUE(tree.
verify());
185 EXPECT_EQ(tree.
size(), 200u);
186 EXPECT_GT(tree.
height(), 1u);
189 for (
int i = 0; i < 200; ++i)
191 const Array<int> hits = sorted_contains(tree, pt(i, i));
192 EXPECT_TRUE(std::find(hits.
begin(), hits.
end(), i) != hits.
end());
200 EXPECT_TRUE(snap.nodes.is_empty());
201 EXPECT_EQ(snap.root, std::numeric_limits<size_t>::max());
207 for (
int i = 0; i < 200; ++i)
208 tree.
insert(rect(i, i, i + 1, i + 1), i);
211 ASSERT_LT(snap.root, snap.nodes.size());
213 EXPECT_EQ(total_entries, tree.
size());
219 tree.
insert(rect(0, 0, 1, 1), 42);
221 ASSERT_EQ(snap.nodes.size(), 1u);
222 EXPECT_EQ(snap.root, 0u);
223 EXPECT_TRUE(snap.nodes(0).is_leaf);
224 EXPECT_EQ(snap.nodes(0).depth, 0u);
225 ASSERT_EQ(snap.nodes(0).entry_boxes.size(), 1u);
226 EXPECT_EQ(snap.nodes(0).bbox, rect(0, 0, 1, 1));
233 for (
int i = 0; i < 20; ++i)
236 ASSERT_TRUE(tree.
verify());
237 EXPECT_EQ(tree.
size(), 20u);
238 EXPECT_EQ(sorted_intersects(tree, rect(4, 4, 5, 5)).
size(), 20u);
242 ASSERT_TRUE(tree.
verify());
244 EXPECT_EQ(hits.
size(), 19u);
245 EXPECT_TRUE(std::find(hits.
begin(), hits.
end(), 7) == hits.
end());
252 for (
int i = 0; i < 30; ++i)
253 tree.
insert(rect(i, 5, i, 5), i);
254 for (
int i = 0; i < 30; ++i)
255 tree.
insert(rect(0, i, 40, i), 100 + i);
257 ASSERT_TRUE(tree.
verify());
258 EXPECT_EQ(tree.
size(), 60u);
261 const Array<int> hits = sorted_contains(tree, pt(10, 5));
262 EXPECT_TRUE(std::find(hits.
begin(), hits.
end(), 10) != hits.
end());
263 EXPECT_TRUE(std::find(hits.
begin(), hits.
end(), 105) != hits.
end());
269 for (
int i = 0; i < 50; ++i)
270 original.
insert(rect(i, i, i + 2, i + 2), i);
273 ASSERT_TRUE(
copy.verify());
274 EXPECT_EQ(
copy.size(), original.
size());
277 for (
int i = 0; i < 25; ++i)
278 ASSERT_TRUE(original.
erase(rect(i, i, i + 2, i + 2), i));
280 ASSERT_TRUE(original.
verify());
281 ASSERT_TRUE(
copy.verify());
282 EXPECT_EQ(original.
size(), 25u);
283 EXPECT_EQ(
copy.size(), 50u);
284 EXPECT_EQ(sorted_contains(
copy, pt(1, 1)).is_empty(),
false);
285 EXPECT_TRUE(sorted_contains(original, pt(1, 1)).is_empty());
291 for (
int i = 0; i < 30; ++i)
292 a.
insert(rect(i, 0, i + 1, 1), i);
295 EXPECT_EQ(b.
size(), 30u);
304 for (
int i = 0; i < 20; ++i)
305 tree.
insert(rect(i, i, i + 1, i + 1), std::make_unique<int>(i));
307 ASSERT_TRUE(tree.
verify());
308 EXPECT_EQ(tree.
size(), 20u);
313 [&](
const Rectangle &,
const std::unique_ptr<int> &v)
322TEST(
RTree, CopyConstructiblePayloadNeedNotBeCopyAssignable)
324 using Payload = CopyConstructibleNonCopyAssignablePayload;
327 const Payload first(7);
328 tree.
insert(rect(0, 0, 2, 2), first);
329 tree.
insert(rect(3, 3, 5, 5), Payload(11));
332 ASSERT_TRUE(
copy.verify());
335 ASSERT_EQ(intersecting.
size(), 2u);
336 EXPECT_EQ(intersecting(0).
value + intersecting(1).
value, 18);
339 ASSERT_EQ(containing.
size(), 1u);
340 EXPECT_EQ(containing(0).
value, 7);
347 for (
int i = 0; i < 90; ++i)
349 const Rectangle b = rect(i % 15, i / 15, i % 15 + 2, i / 15 + 2);
351 ref.
append(std::make_pair(b, i));
353 ASSERT_TRUE(source.verify());
357 EXPECT_EQ(assigned.
size(), source.size());
358 EXPECT_TRUE(assigned.
verify());
360 for (
int i = 0; i < 30; ++i)
361 ASSERT_TRUE(source.erase(ref(i).first, ref(i).second));
362 EXPECT_EQ(source.size(), 60u);
363 EXPECT_EQ(assigned.
size(), 90u);
364 EXPECT_TRUE(source.verify());
365 EXPECT_TRUE(assigned.
verify());
368 moved = std::move(assigned);
369 EXPECT_EQ(moved.
size(), 90u);
370 EXPECT_TRUE(moved.
verify());
372 EXPECT_TRUE(assigned.
verify());
376 EXPECT_EQ(moved.
height(), 0u);
377 EXPECT_TRUE(moved.
verify());
379 for (
const auto &[b,
id] : ref)
381 ASSERT_TRUE(source.erase(b,
id) or
id < 30);
382 ASSERT_TRUE(source.verify());
384 EXPECT_TRUE(source.is_empty());
385 EXPECT_EQ(source.height(), 0u);
390 std::mt19937
rng(20260714u);
391 std::uniform_int_distribution<int> op_dist(0, 2);
392 std::uniform_int_distribution<int> coord(0, 80);
393 std::uniform_int_distribution<int> extent(0, 12);
399 auto random_rect = [&]()
401 const int x1 = coord(
rng);
402 const int y1 = coord(
rng);
403 return rect(x1,
y1, x1 + extent(
rng),
y1 + extent(
rng));
406 for (
int iter = 0; iter < 4000; ++iter)
412 const int id = next_id++;
414 ref.
append(std::make_pair(b,
id));
418 std::uniform_int_distribution<size_t> pick(0, ref.
size() - 1);
419 const size_t k = pick(
rng);
420 ASSERT_TRUE(tree.
erase(ref(
k).first, ref(
k).second));
421 std::swap(ref(
k), ref(ref.
size() - 1));
422 [[maybe_unused]]
const auto popped = ref.
remove_last();
427 EXPECT_EQ(sorted_intersects(tree, q), brute_intersects(ref, q));
429 EXPECT_EQ(sorted_contains(tree, p), brute_contains(ref, p));
432 ASSERT_TRUE(tree.
verify()) <<
"invariant broken at iter " << iter;
437 for (
int x = 0; x < 90; x += 7)
438 for (
int y = 0;
y < 90;
y += 7)
441 ASSERT_EQ(sorted_intersects(tree, q), brute_intersects(ref, q));
High-level sorting functions for Aleph containers.
size_t size_t int32_t value
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.
Array< Payload > search_intersects(const Rectangle &rect) const
Return the payloads of every entry whose bbox intersects rect.
Array< Payload > search_contains(const Point &p) const
Return the payloads of every entry whose bbox contains p.
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.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
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)
Dynamic R-tree spatial index over axis-aligned rectangles.