Remove a value from the map and return it.
(&mut self, key: K)
| 165 | |
| 166 | /// Remove a value from the map and return it. |
| 167 | pub fn remove(&mut self, key: K) -> Option<V> { |
| 168 | if let Some(idx) = self.index(key) { |
| 169 | let back = self.dense.pop().unwrap(); |
| 170 | |
| 171 | // Are we popping the back of `dense`? |
| 172 | if idx == self.dense.len() { |
| 173 | return Some(back); |
| 174 | } |
| 175 | |
| 176 | // We're removing an element from the middle of `dense`. |
| 177 | // Replace the element at `idx` with the back of `dense`. |
| 178 | // Repair `sparse` first. |
| 179 | self.sparse[back.key()] = idx as u32; |
| 180 | return Some(mem::replace(&mut self.dense[idx], back)); |
| 181 | } |
| 182 | |
| 183 | // Nothing to remove. |
| 184 | None |
| 185 | } |
| 186 | |
| 187 | /// Remove the last value from the map. |
| 188 | pub fn pop(&mut self) -> Option<V> { |