Skip to main content

hashmap/
hashmap.rs

1// SPDX-FileCopyrightText: 2025 rstared contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use std::collections::HashMap;
6
7use rstared::RTreed;
8use rstared::{AABB, primitives::Rectangle};
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::new(rect_hashmap);
16
17    // Insert two rectangles, recording them in the R-tree.
18    rtreed.insert(1, Rectangle::from_corners((0, 0), (1, 1)));
19    rtreed.insert(2, Rectangle::from_corners((1, 1), (2, 2)));
20
21    // Locate the two rectangles in the R-tree.
22    assert_eq!(
23        rtreed
24            .rtree()
25            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
26            .count(),
27        2
28    );
29
30    // Now remove one of the rectangles, recording this in the R-tree.
31    rtreed.remove(&1);
32
33    // Make the same query to the R-tree as before.
34    // Only one rectangle is now present.
35    assert_eq!(
36        rtreed
37            .rtree()
38            .locate_in_envelope(AABB::from_corners((0, 0), (2, 2)))
39            .count(),
40        1
41    );
42}
43
44#[test]
45fn test() {
46    main();
47}