Skip to main content

vec/
vec.rs

1// SPDX-FileCopyrightText: 2025 rstared contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use rstared::RTreed;
6use rstared::{AABB, primitives::Rectangle};
7
8fn main() {
9    // A vec of 2D rectangles will be the underlying collection.
10    let rect_vec: Vec<Rectangle<(i32, i32)>> = Vec::new();
11
12    // Wrap `RTreed` around the vec.
13    let mut rtreed = RTreed::new(rect_vec);
14
15    // Push two rectangles, recording them in the R-tree.
16    rtreed.push(Rectangle::from_corners((0, 0), (1, 1)));
17    rtreed.push(Rectangle::from_corners((1, 1), (2, 2)));
18
19    // Locate the two rectangles in the R-tree.
20    assert_eq!(
21        rtreed
22            .rtree()
23            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
24            .count(),
25        2
26    );
27
28    // Access a rectangle by its index in the vec.
29    assert_eq!(
30        rtreed.get(&0),
31        Some(&Rectangle::from_corners((0, 0), (1, 1)))
32    );
33
34    // Push one more rectangle.
35    rtreed.push(Rectangle::from_corners((2, 2), (3, 3)));
36
37    // Swap-remove the first rectangle.
38    assert_eq!(
39        rtreed.swap_remove(&0),
40        Rectangle::from_corners((0, 0), (1, 1))
41    );
42
43    // Now the latest-pushed rectangle occupies the place of the first
44    // rectangle. The second rectangle is as it was previously.
45    assert_eq!(
46        rtreed.get(&0),
47        Some(&Rectangle::from_corners((2, 2), (3, 3)))
48    );
49    assert_eq!(
50        rtreed.get(&1),
51        Some(&Rectangle::from_corners((1, 1), (2, 2)))
52    );
53    assert_eq!(
54        rtreed
55            .rtree()
56            .locate_in_envelope(AABB::from_corners((0, 0), (3, 3)))
57            .count(),
58        2
59    );
60}
61
62#[test]
63fn test() {
64    main();
65}