Grow the tree to accommodate at least `min_leaves` leaves. Copies existing leaves to the new tree and does a full bottom-up rehash. No-op if the current capacity is sufficient.
(&mut self, min_leaves: usize)
| 224 | /// Copies existing leaves to the new tree and does a full bottom-up rehash. |
| 225 | /// No-op if the current capacity is sufficient. |
| 226 | pub fn grow(&mut self, min_leaves: usize) { |
| 227 | if min_leaves <= self.capacity { |
| 228 | return; |
| 229 | } |
| 230 | let new_capacity = min_leaves.next_power_of_two(); |
| 231 | let new_depth = new_capacity.ilog2() as usize; |
| 232 | let mut new_nodes = vec![[0u8; 32]; 2 * new_capacity]; |
| 233 | |
| 234 | // Copy existing leaves |
| 235 | let old_leaf_start = self.capacity; |
| 236 | let old_leaf_end = 2 * self.capacity; |
| 237 | let new_leaf_start = new_capacity; |
| 238 | new_nodes[new_leaf_start..new_leaf_start + self.capacity] |
| 239 | .copy_from_slice(&self.nodes[old_leaf_start..old_leaf_end]); |
| 240 | |
| 241 | self.nodes = new_nodes; |
| 242 | self.capacity = new_capacity; |
| 243 | self.depth = new_depth; |
| 244 | |
| 245 | // Full bottom-up rehash from leaf parents |
| 246 | self.rehash_from(self.capacity / 2); |
| 247 | } |
| 248 | |
| 249 | /// Shrink the tree so that its capacity matches `min_leaves.next_power_of_two()`. |
| 250 | /// |
no test coverage detected