Retains only the elements specified by the predicate. Remove all key-value pairs where the predicate returns false. The predicate is allowed to update the values stored in the map.
(&mut self, forest: &mut MapForest<K, V>, mut predicate: F)
| 179 | /// |
| 180 | /// The predicate is allowed to update the values stored in the map. |
| 181 | pub fn retain<F>(&mut self, forest: &mut MapForest<K, V>, mut predicate: F) |
| 182 | where |
| 183 | F: FnMut(K, &mut V) -> bool, |
| 184 | { |
| 185 | let mut path = Path::default(); |
| 186 | if let Some(root) = self.root.expand() { |
| 187 | path.first(root, &forest.nodes); |
| 188 | } |
| 189 | while let Some((node, entry)) = path.leaf_pos() { |
| 190 | let keep = { |
| 191 | let (ks, vs) = forest.nodes[node].unwrap_leaf_mut(); |
| 192 | predicate(ks[entry], &mut vs[entry]) |
| 193 | }; |
| 194 | if keep { |
| 195 | path.next(&forest.nodes); |
| 196 | } else { |
| 197 | self.root = path.remove(&mut forest.nodes).into(); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /// Create an immutable cursor for navigating this map. |
| 203 | /// |