1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::quadtree::free_list;
use std::fmt::{Debug, Formatter};
pub type NodeElementCountType = u32;
const NODE_IS_BRANCH: u32 = NodeElementCountType::MAX;
#[derive(Copy, Clone)]
pub struct Node {
pub first_child_or_element: free_list::IndexType,
pub element_count: NodeElementCountType,
}
impl Debug for Node {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match (self.element_count, self.first_child_or_element) {
(NODE_IS_BRANCH, free_list::SENTINEL) => {
write!(f, "Branch, no child nodes")
}
(NODE_IS_BRANCH, child_index) => write!(
f,
"Branch, child nodes at {}..={}",
child_index,
child_index + 3
),
(0, free_list::SENTINEL) => write!(f, "Leaf, no elements"),
(count, free_list::SENTINEL) => {
panic!("Got {} child elements but no data pointer", count)
}
(count, element_index) => write!(
f,
"Leaf, elements {}..={}",
element_index,
element_index + count
),
}
}
}
impl Default for Node {
#[inline]
fn default() -> Self {
let node = Self {
first_child_or_element: free_list::SENTINEL,
element_count: 0,
};
debug_assert!(node.is_leaf() && node.is_empty());
node
}
}
impl Node {
#[inline]
pub fn is_empty(&self) -> bool {
self.element_count == 0
}
#[inline]
pub fn is_branch(&self) -> bool {
self.element_count == NODE_IS_BRANCH
}
#[inline]
pub fn is_leaf(&self) -> bool {
debug_assert!(self.element_count > 0 || self.first_child_or_element == free_list::SENTINEL);
!self.is_branch()
}
#[inline]
pub fn get_first_child_node_index(&self) -> free_list::IndexType {
debug_assert!(self.is_branch());
self.first_child_or_element
}
#[inline]
pub fn get_first_element_node_index(&self) -> free_list::IndexType {
debug_assert!(self.is_leaf());
self.first_child_or_element
}
#[inline]
pub fn make_empty_leaf(&mut self) {
self.first_child_or_element = free_list::SENTINEL;
self.element_count = 0;
}
#[inline]
pub fn make_branch(&mut self, first_child: free_list::IndexType) {
self.first_child_or_element = first_child;
self.element_count = NODE_IS_BRANCH;
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn quad_node_is_eight_bytes() {
assert_eq!(std::mem::size_of::<Node>(), 8);
}
}