Recursively check invariants for a node and its children.
(
&self,
node: &NodeRef<K, V>,
min_key: Option<&K>,
max_key: Option<&K>,
_is_root: bool,
)
| 148 | |
| 149 | /// Recursively check invariants for a node and its children. |
| 150 | fn check_node_invariants( |
| 151 | &self, |
| 152 | node: &NodeRef<K, V>, |
| 153 | min_key: Option<&K>, |
| 154 | max_key: Option<&K>, |
| 155 | _is_root: bool, |
| 156 | ) -> bool { |
| 157 | match node { |
| 158 | NodeRef::Leaf(id, _) => { |
| 159 | if let Some(leaf) = self.get_leaf(*id) { |
| 160 | // Check leaf invariants |
| 161 | if leaf.keys_len() != leaf.values_len() { |
| 162 | return false; // Keys and values must have same length |
| 163 | } |
| 164 | |
| 165 | // Check that keys are sorted |
| 166 | for i in 1..leaf.keys_len() { |
| 167 | if let (Some(prev_key), Some(curr_key)) = |
| 168 | (leaf.get_key(i - 1), leaf.get_key(i)) |
| 169 | { |
| 170 | if prev_key >= curr_key { |
| 171 | return false; // Keys must be in ascending order |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Check capacity constraints |
| 177 | if leaf.keys_len() > self.capacity { |
| 178 | return false; // Node exceeds capacity |
| 179 | } |
| 180 | |
| 181 | // Check minimum occupancy |
| 182 | if !leaf.keys_is_empty() && leaf.is_underfull() { |
| 183 | // For root nodes, allow fewer keys only if it's the only node |
| 184 | if _is_root { |
| 185 | // Root leaf can have any number of keys >= 1 |
| 186 | // (This is fine for leaf roots) |
| 187 | } else { |
| 188 | return false; // Non-root leaf is underfull |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Check key bounds |
| 193 | if let Some(min) = min_key { |
| 194 | if !leaf.keys_is_empty() { |
| 195 | if let Some(first_key) = leaf.first_key() { |
| 196 | if first_key < min { |
| 197 | return false; // First key must be >= min_key |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | if let Some(max) = max_key { |
| 203 | if !leaf.keys_is_empty() { |
| 204 | if let Some(last_key) = leaf.last_key() { |
| 205 | if last_key >= max { |
| 206 | return false; // Last key must be < max_key |
| 207 | } |
no test coverage detected