toolbox_rs/
level_directory.rs

1use crate::partition::PartitionID;
2
3/// Note: LevelDirectory is a poor naming choice.
4/// This struct encapsulates the logic to decide whether
5///  - two nodes are within the same cell at a given level
6///  - TBD.
7pub struct LevelDirectory<'a> {
8    partition_ids: &'a [PartitionID],
9    levels: &'a [u32],
10}
11
12impl<'a> LevelDirectory<'a> {
13    pub fn new(partition_ids: &'a [PartitionID], levels: &'a [u32]) -> Self {
14        Self {
15            partition_ids,
16            levels,
17        }
18    }
19    pub fn crosses_at_level(&self, u: usize, v: usize, level: u32) -> bool {
20        let u_id = self.partition_ids[u];
21        let v_id = self.partition_ids[v];
22        let u_id = u_id.parent_at_level(level);
23        let v_id = v_id.parent_at_level(level);
24        u_id != v_id
25    }
26
27    // return a slice of all the levels where two nodes are in different cells
28    pub fn get_crossing_levels(&self, u: usize, v: usize) -> &[u32] {
29        let mut i = 0;
30        for level in self.levels {
31            if self.crosses_at_level(u, v, *level) {
32                i += 1;
33            } else {
34                break;
35            }
36        }
37        &self.levels[..i]
38    }
39}
40
41// TODO: Add tests
42// let id = PartitionID::new(0xffff_ffff);
43// for l in &levels {
44//     // TODO: remove
45//     println!("[{l:#02}] {:#032b}", id.parent_at_level(*l));
46// }