Compact and return both the removed count and the old→new id map. `id_map[old_local]` = new_local, or `u32::MAX` if the node was tombstoned (removed).
(&mut self)
| 436 | /// `id_map[old_local]` = new_local, or `u32::MAX` if the node was |
| 437 | /// tombstoned (removed). |
| 438 | pub fn compact_with_map(&mut self) -> (usize, Vec<u32>) { |
| 439 | let tombstone_count = self.tombstone_count(); |
| 440 | if tombstone_count == 0 { |
| 441 | let identity: Vec<u32> = (0..self.nodes.len() as u32).collect(); |
| 442 | return (0, identity); |
| 443 | } |
| 444 | self.ensure_mutable_neighbors(); |
| 445 | |
| 446 | let mut id_map: Vec<u32> = Vec::with_capacity(self.nodes.len()); |
| 447 | let mut new_id = 0u32; |
| 448 | for node in &self.nodes { |
| 449 | if node.deleted { |
| 450 | id_map.push(u32::MAX); |
| 451 | } else { |
| 452 | id_map.push(new_id); |
| 453 | new_id += 1; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | let mut new_nodes: Vec<Node> = Vec::with_capacity(new_id as usize); |
| 458 | for node in self.nodes.drain(..) { |
| 459 | if node.deleted { |
| 460 | continue; |
| 461 | } |
| 462 | let remapped_neighbors: Vec<Vec<u32>> = node |
| 463 | .neighbors |
| 464 | .into_iter() |
| 465 | .map(|layer_neighbors| { |
| 466 | layer_neighbors |
| 467 | .into_iter() |
| 468 | .filter_map(|old_nid| { |
| 469 | let new_nid = id_map[old_nid as usize]; |
| 470 | if new_nid == u32::MAX { |
| 471 | None |
| 472 | } else { |
| 473 | Some(new_nid) |
| 474 | } |
| 475 | }) |
| 476 | .collect() |
| 477 | }) |
| 478 | .collect(); |
| 479 | new_nodes.push(Node { |
| 480 | storage: node.storage, |
| 481 | neighbors: remapped_neighbors, |
| 482 | deleted: false, |
| 483 | }); |
| 484 | } |
| 485 | |
| 486 | self.entry_point = if let Some(old_ep) = self.entry_point { |
| 487 | let new_ep = id_map[old_ep as usize]; |
| 488 | if new_ep == u32::MAX { |
| 489 | new_nodes |
| 490 | .iter() |
| 491 | .enumerate() |
| 492 | .max_by_key(|(_, n)| n.neighbors.len()) |
| 493 | .map(|(i, _)| i as u32) |
| 494 | } else { |
| 495 | Some(new_ep) |
no test coverage detected