basic_usage/
basic_usage.rs

1// SPDX-FileCopyrightText: 2025 rstared contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use std::collections::HashMap;
6
7use rstar::{AABB, primitives::Rectangle};
8use rstared::{Insert, RTreed, Remove};
9
10fn main() {
11    // A hashmap of 2D rectangles will be the underlying collection.
12    let rect_hashmap: HashMap<i32, Rectangle<(i32, i32)>> = HashMap::new();
13
14    // Wrap `RTreed` around the hashmap.
15    let mut rtreed = RTreed::<i32, Rectangle<(i32, i32)>, HashMap<i32, Rectangle<(i32, i32)>>>::new(
16        rect_hashmap,
17    );
18
19    // Insert two rectangles, recording them in the R-tree.
20    rtreed.insert(1, Rectangle::from_corners((0, 0), (1, 1)));
21    rtreed.insert(2, Rectangle::from_corners((1, 1), (2, 2)));
22
23    // Locate the two rectangles in the R-tree.
24    assert_eq!(
25        rtreed
26            .rtree()
27            .locate_in_envelope(&AABB::from_corners((0, 0), (2, 2)))
28            .count(),
29        2
30    );
31
32    // Now remove one of the rectangles, recording this in the R-tree.
33    rtreed.remove(&1);
34
35    // Make the same query to the R-tree as before.
36    // Only one rectangle is now present.
37    assert_eq!(
38        rtreed
39            .rtree()
40            .locate_in_envelope(&AABB::from_corners((0, 0), (2, 2)))
41            .count(),
42        1
43    );
44}
45
46#[test]
47fn test() {
48    main();
49}