|
3 | 3 | //! mostly in dealing with its bucket "pointers". |
4 | 4 |
|
5 | 5 | use super::{Entry, Equivalent, HashValue, IndexMapCore, VacantEntry}; |
| 6 | +use crate::util::enumerate; |
6 | 7 | use core::fmt; |
7 | 8 | use core::mem::replace; |
8 | 9 | use hashbrown::raw::RawTable; |
@@ -44,11 +45,75 @@ impl<K, V> IndexMapCore<K, V> { |
44 | 45 | } |
45 | 46 | } |
46 | 47 |
|
| 48 | + /// Erase the given index from `indices`. |
| 49 | + /// |
| 50 | + /// The index doesn't need to be valid in `entries` while calling this. No other index |
| 51 | + /// adjustments are made -- this is only used by `pop` for the greatest index. |
47 | 52 | pub(super) fn erase_index(&mut self, hash: HashValue, index: usize) { |
| 53 | + debug_assert_eq!(index, self.indices.len() - 1); |
48 | 54 | let raw_bucket = self.find_index(hash, index).unwrap(); |
49 | 55 | unsafe { self.indices.erase(raw_bucket) }; |
50 | 56 | } |
51 | 57 |
|
| 58 | + /// Erase `start..end` from `indices`, and shift `end..` indices down to `start..` |
| 59 | + /// |
| 60 | + /// All of these items should still be at their original location in `entries`. |
| 61 | + /// This is used by `drain`, which will let `Vec::drain` do the work on `entries`. |
| 62 | + pub(super) fn erase_indices(&mut self, start: usize, end: usize) { |
| 63 | + let (init, shifted_entries) = self.entries.split_at(end); |
| 64 | + let (start_entries, erased_entries) = init.split_at(start); |
| 65 | + |
| 66 | + let erased = erased_entries.len(); |
| 67 | + let shifted = shifted_entries.len(); |
| 68 | + let half_capacity = self.indices.buckets() / 2; |
| 69 | + |
| 70 | + // Use a heuristic between different strategies |
| 71 | + if erased == 0 { |
| 72 | + // Degenerate case, nothing to do |
| 73 | + } else if start + shifted < half_capacity && start < erased { |
| 74 | + // Reinsert everything, as there are few kept indices |
| 75 | + self.indices.clear(); |
| 76 | + |
| 77 | + // Reinsert stable indices |
| 78 | + for (i, entry) in enumerate(start_entries) { |
| 79 | + self.indices.insert_no_grow(entry.hash.get(), i); |
| 80 | + } |
| 81 | + |
| 82 | + // Reinsert shifted indices |
| 83 | + for (i, entry) in (start..).zip(shifted_entries) { |
| 84 | + self.indices.insert_no_grow(entry.hash.get(), i); |
| 85 | + } |
| 86 | + } else if erased + shifted < half_capacity { |
| 87 | + // Find each affected index, as there are few to adjust |
| 88 | + |
| 89 | + // Find erased indices |
| 90 | + for (i, entry) in (start..).zip(erased_entries) { |
| 91 | + let bucket = self.find_index(entry.hash, i).unwrap(); |
| 92 | + unsafe { self.indices.erase(bucket) }; |
| 93 | + } |
| 94 | + |
| 95 | + // Find shifted indices |
| 96 | + for ((new, old), entry) in (start..).zip(end..).zip(shifted_entries) { |
| 97 | + let bucket = self.find_index(entry.hash, old).unwrap(); |
| 98 | + unsafe { bucket.write(new) }; |
| 99 | + } |
| 100 | + } else { |
| 101 | + // Sweep the whole table for adjustments |
| 102 | + unsafe { |
| 103 | + for bucket in self.indices.iter() { |
| 104 | + let i = bucket.read(); |
| 105 | + if i >= end { |
| 106 | + bucket.write(i - erased); |
| 107 | + } else if i >= start { |
| 108 | + self.indices.erase(bucket); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + debug_assert_eq!(self.indices.len(), start + shifted); |
| 115 | + } |
| 116 | + |
52 | 117 | pub(crate) fn entry(&mut self, hash: HashValue, key: K) -> Entry<'_, K, V> |
53 | 118 | where |
54 | 119 | K: Eq, |
|
0 commit comments