1mod entry;
11
12pub mod raw_entry_v1;
13
14use hashbrown::hash_table;
15
16use crate::vec::{self, Vec};
17use crate::TryReserveError;
18use core::mem;
19use core::ops::RangeBounds;
20
21use crate::util::simplify_range;
22use crate::{Bucket, Equivalent, HashValue};
23
24type Indices = hash_table::HashTable<usize>;
25type Entries<K, V> = Vec<Bucket<K, V>>;
26
27pub use entry::{Entry, IndexedEntry, OccupiedEntry, VacantEntry};
28
29#[derive(Debug)]
31pub(crate) struct IndexMapCore<K, V> {
32 indices: Indices,
34 entries: Entries<K, V>,
36}
37
38struct RefMut<'a, K, V> {
45 indices: &'a mut Indices,
46 entries: &'a mut Entries<K, V>,
47}
48
49#[inline(always)]
50fn get_hash<K, V>(entries: &[Bucket<K, V>]) -> impl Fn(&usize) -> u64 + '_ {
51 move |&i| entries[i].hash.get()
52}
53
54#[inline]
55fn equivalent<'a, K, V, Q: ?Sized + Equivalent<K>>(
56 key: &'a Q,
57 entries: &'a [Bucket<K, V>],
58) -> impl Fn(&usize) -> bool + 'a {
59 move |&i| Q::equivalent(key, &entries[i].key)
60}
61
62#[inline]
63fn erase_index(table: &mut Indices, hash: HashValue, index: usize) {
64 if let Ok(entry) = table.find_entry(hash.get(), move |&i| i == index) {
65 entry.remove();
66 } else if cfg!(debug_assertions) {
67 panic!("index not found");
68 }
69}
70
71#[inline]
72fn update_index(table: &mut Indices, hash: HashValue, old: usize, new: usize) {
73 let index = table
74 .find_mut(hash.get(), move |&i| i == old)
75 .expect("index not found");
76 *index = new;
77}
78
79fn insert_bulk_no_grow<K, V>(indices: &mut Indices, entries: &[Bucket<K, V>]) {
84 assert!(indices.capacity() - indices.len() >= entries.len());
85 for entry in entries {
86 indices.insert_unique(entry.hash.get(), indices.len(), |_| unreachable!());
87 }
88}
89
90impl<K, V> Clone for IndexMapCore<K, V>
91where
92 K: Clone,
93 V: Clone,
94{
95 fn clone(&self) -> Self {
96 let mut new = Self::new();
97 new.clone_from(self);
98 new
99 }
100
101 fn clone_from(&mut self, other: &Self) {
102 self.indices.clone_from(&other.indices);
103 if self.entries.capacity() < other.entries.len() {
104 let additional = other.entries.len() - self.entries.len();
106 self.borrow_mut().reserve_entries(additional);
107 }
108 self.entries.clone_from(&other.entries);
109 }
110}
111
112impl<K, V> crate::Entries for IndexMapCore<K, V> {
113 type Entry = Bucket<K, V>;
114
115 #[inline]
116 fn into_entries(self) -> Vec<Self::Entry> {
117 self.entries
118 }
119
120 #[inline]
121 fn as_entries(&self) -> &[Self::Entry] {
122 &self.entries
123 }
124
125 #[inline]
126 fn as_entries_mut(&mut self) -> &mut [Self::Entry] {
127 &mut self.entries
128 }
129
130 fn with_entries<F>(&mut self, f: F)
131 where
132 F: FnOnce(&mut [Self::Entry]),
133 {
134 f(&mut self.entries);
135 self.rebuild_hash_table();
136 }
137}
138
139impl<K, V> IndexMapCore<K, V> {
140 const MAX_ENTRIES_CAPACITY: usize = (isize::MAX as usize) / mem::size_of::<Bucket<K, V>>();
142
143 #[inline]
144 pub(crate) const fn new() -> Self {
145 IndexMapCore {
146 indices: Indices::new(),
147 entries: Vec::new(),
148 }
149 }
150
151 #[inline]
152 fn borrow_mut(&mut self) -> RefMut<'_, K, V> {
153 RefMut::new(&mut self.indices, &mut self.entries)
154 }
155
156 #[inline]
157 pub(crate) fn with_capacity(n: usize) -> Self {
158 IndexMapCore {
159 indices: Indices::with_capacity(n),
160 entries: Vec::with_capacity(n),
161 }
162 }
163
164 #[inline]
165 pub(crate) fn len(&self) -> usize {
166 self.indices.len()
167 }
168
169 #[inline]
170 pub(crate) fn capacity(&self) -> usize {
171 Ord::min(self.indices.capacity(), self.entries.capacity())
172 }
173
174 pub(crate) fn clear(&mut self) {
175 self.indices.clear();
176 self.entries.clear();
177 }
178
179 pub(crate) fn truncate(&mut self, len: usize) {
180 if len < self.len() {
181 self.erase_indices(len, self.entries.len());
182 self.entries.truncate(len);
183 }
184 }
185
186 #[track_caller]
187 pub(crate) fn drain<R>(&mut self, range: R) -> vec::Drain<'_, Bucket<K, V>>
188 where
189 R: RangeBounds<usize>,
190 {
191 let range = simplify_range(range, self.entries.len());
192 self.erase_indices(range.start, range.end);
193 self.entries.drain(range)
194 }
195
196 #[cfg(feature = "rayon")]
197 pub(crate) fn par_drain<R>(&mut self, range: R) -> rayon::vec::Drain<'_, Bucket<K, V>>
198 where
199 K: Send,
200 V: Send,
201 R: RangeBounds<usize>,
202 {
203 use rayon::iter::ParallelDrainRange;
204 let range = simplify_range(range, self.entries.len());
205 self.erase_indices(range.start, range.end);
206 self.entries.par_drain(range)
207 }
208
209 #[track_caller]
210 pub(crate) fn split_off(&mut self, at: usize) -> Self {
211 let len = self.entries.len();
212 assert!(
213 at <= len,
214 "index out of bounds: the len is {len} but the index is {at}. Expected index <= len"
215 );
216
217 self.erase_indices(at, self.entries.len());
218 let entries = self.entries.split_off(at);
219
220 let mut indices = Indices::with_capacity(entries.len());
221 insert_bulk_no_grow(&mut indices, &entries);
222 Self { indices, entries }
223 }
224
225 #[track_caller]
226 pub(crate) fn split_splice<R>(&mut self, range: R) -> (Self, vec::IntoIter<Bucket<K, V>>)
227 where
228 R: RangeBounds<usize>,
229 {
230 let range = simplify_range(range, self.len());
231 self.erase_indices(range.start, self.entries.len());
232 let entries = self.entries.split_off(range.end);
233 let drained = self.entries.split_off(range.start);
234
235 let mut indices = Indices::with_capacity(entries.len());
236 insert_bulk_no_grow(&mut indices, &entries);
237 (Self { indices, entries }, drained.into_iter())
238 }
239
240 pub(crate) fn append_unchecked(&mut self, other: &mut Self) {
242 self.reserve(other.len());
243 insert_bulk_no_grow(&mut self.indices, &other.entries);
244 self.entries.append(&mut other.entries);
245 other.indices.clear();
246 }
247
248 pub(crate) fn reserve(&mut self, additional: usize) {
250 self.indices.reserve(additional, get_hash(&self.entries));
251 if additional > self.entries.capacity() - self.entries.len() {
253 self.borrow_mut().reserve_entries(additional);
254 }
255 }
256
257 pub(crate) fn reserve_exact(&mut self, additional: usize) {
259 self.indices.reserve(additional, get_hash(&self.entries));
260 self.entries.reserve_exact(additional);
261 }
262
263 pub(crate) fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
265 self.indices
266 .try_reserve(additional, get_hash(&self.entries))
267 .map_err(TryReserveError::from_hashbrown)?;
268 if additional > self.entries.capacity() - self.entries.len() {
270 self.try_reserve_entries(additional)
271 } else {
272 Ok(())
273 }
274 }
275
276 fn try_reserve_entries(&mut self, additional: usize) -> Result<(), TryReserveError> {
278 let new_capacity = Ord::min(self.indices.capacity(), Self::MAX_ENTRIES_CAPACITY);
281 let try_add = new_capacity - self.entries.len();
282 if try_add > additional && self.entries.try_reserve_exact(try_add).is_ok() {
283 return Ok(());
284 }
285 self.entries
286 .try_reserve_exact(additional)
287 .map_err(TryReserveError::from_alloc)
288 }
289
290 pub(crate) fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
292 self.indices
293 .try_reserve(additional, get_hash(&self.entries))
294 .map_err(TryReserveError::from_hashbrown)?;
295 self.entries
296 .try_reserve_exact(additional)
297 .map_err(TryReserveError::from_alloc)
298 }
299
300 pub(crate) fn shrink_to(&mut self, min_capacity: usize) {
302 self.indices
303 .shrink_to(min_capacity, get_hash(&self.entries));
304 self.entries.shrink_to(min_capacity);
305 }
306
307 pub(crate) fn pop(&mut self) -> Option<(K, V)> {
309 if let Some(entry) = self.entries.pop() {
310 let last = self.entries.len();
311 erase_index(&mut self.indices, entry.hash, last);
312 Some((entry.key, entry.value))
313 } else {
314 None
315 }
316 }
317
318 pub(crate) fn get_index_of<Q>(&self, hash: HashValue, key: &Q) -> Option<usize>
320 where
321 Q: ?Sized + Equivalent<K>,
322 {
323 let eq = equivalent(key, &self.entries);
324 self.indices.find(hash.get(), eq).copied()
325 }
326
327 fn push_entry(&mut self, hash: HashValue, key: K, value: V) {
330 if self.entries.len() == self.entries.capacity() {
331 self.borrow_mut().reserve_entries(1);
334 }
335 self.entries.push(Bucket { hash, key, value });
336 }
337
338 pub(crate) fn insert_full(&mut self, hash: HashValue, key: K, value: V) -> (usize, Option<V>)
339 where
340 K: Eq,
341 {
342 let eq = equivalent(&key, &self.entries);
343 let hasher = get_hash(&self.entries);
344 match self.indices.entry(hash.get(), eq, hasher) {
345 hash_table::Entry::Occupied(entry) => {
346 let i = *entry.get();
347 (i, Some(mem::replace(&mut self.entries[i].value, value)))
348 }
349 hash_table::Entry::Vacant(entry) => {
350 let i = self.entries.len();
351 entry.insert(i);
352 self.push_entry(hash, key, value);
353 debug_assert_eq!(self.indices.len(), self.entries.len());
354 (i, None)
355 }
356 }
357 }
358
359 pub(crate) fn replace_full(
361 &mut self,
362 hash: HashValue,
363 key: K,
364 value: V,
365 ) -> (usize, Option<(K, V)>)
366 where
367 K: Eq,
368 {
369 let eq = equivalent(&key, &self.entries);
370 let hasher = get_hash(&self.entries);
371 match self.indices.entry(hash.get(), eq, hasher) {
372 hash_table::Entry::Occupied(entry) => {
373 let i = *entry.get();
374 let entry = &mut self.entries[i];
375 let kv = (
376 mem::replace(&mut entry.key, key),
377 mem::replace(&mut entry.value, value),
378 );
379 (i, Some(kv))
380 }
381 hash_table::Entry::Vacant(entry) => {
382 let i = self.entries.len();
383 entry.insert(i);
384 self.push_entry(hash, key, value);
385 debug_assert_eq!(self.indices.len(), self.entries.len());
386 (i, None)
387 }
388 }
389 }
390
391 pub(crate) fn shift_remove_full<Q>(&mut self, hash: HashValue, key: &Q) -> Option<(usize, K, V)>
393 where
394 Q: ?Sized + Equivalent<K>,
395 {
396 let eq = equivalent(key, &self.entries);
397 match self.indices.find_entry(hash.get(), eq) {
398 Ok(entry) => {
399 let (index, _) = entry.remove();
400 let (key, value) = self.borrow_mut().shift_remove_finish(index);
401 Some((index, key, value))
402 }
403 Err(_) => None,
404 }
405 }
406
407 #[inline]
409 pub(crate) fn shift_remove_index(&mut self, index: usize) -> Option<(K, V)> {
410 self.borrow_mut().shift_remove_index(index)
411 }
412
413 #[inline]
414 #[track_caller]
415 pub(super) fn move_index(&mut self, from: usize, to: usize) {
416 self.borrow_mut().move_index(from, to);
417 }
418
419 #[inline]
420 #[track_caller]
421 pub(crate) fn swap_indices(&mut self, a: usize, b: usize) {
422 self.borrow_mut().swap_indices(a, b);
423 }
424
425 pub(crate) fn swap_remove_full<Q>(&mut self, hash: HashValue, key: &Q) -> Option<(usize, K, V)>
427 where
428 Q: ?Sized + Equivalent<K>,
429 {
430 let eq = equivalent(key, &self.entries);
431 match self.indices.find_entry(hash.get(), eq) {
432 Ok(entry) => {
433 let (index, _) = entry.remove();
434 let (key, value) = self.borrow_mut().swap_remove_finish(index);
435 Some((index, key, value))
436 }
437 Err(_) => None,
438 }
439 }
440
441 #[inline]
443 pub(crate) fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> {
444 self.borrow_mut().swap_remove_index(index)
445 }
446
447 fn erase_indices(&mut self, start: usize, end: usize) {
452 let (init, shifted_entries) = self.entries.split_at(end);
453 let (start_entries, erased_entries) = init.split_at(start);
454
455 let erased = erased_entries.len();
456 let shifted = shifted_entries.len();
457 let half_capacity = self.indices.capacity() / 2;
458
459 if erased == 0 {
461 } else if start + shifted < half_capacity && start < erased {
463 self.indices.clear();
465
466 insert_bulk_no_grow(&mut self.indices, start_entries);
468 insert_bulk_no_grow(&mut self.indices, shifted_entries);
469 } else if erased + shifted < half_capacity {
470 for (i, entry) in (start..).zip(erased_entries) {
474 erase_index(&mut self.indices, entry.hash, i);
475 }
476
477 for ((new, old), entry) in (start..).zip(end..).zip(shifted_entries) {
479 update_index(&mut self.indices, entry.hash, old, new);
480 }
481 } else {
482 let offset = end - start;
484 self.indices.retain(move |i| {
485 if *i >= end {
486 *i -= offset;
487 true
488 } else {
489 *i < start
490 }
491 });
492 }
493
494 debug_assert_eq!(self.indices.len(), start + shifted);
495 }
496
497 pub(crate) fn retain_in_order<F>(&mut self, mut keep: F)
498 where
499 F: FnMut(&mut K, &mut V) -> bool,
500 {
501 self.entries
502 .retain_mut(|entry| keep(&mut entry.key, &mut entry.value));
503 if self.entries.len() < self.indices.len() {
504 self.rebuild_hash_table();
505 }
506 }
507
508 fn rebuild_hash_table(&mut self) {
509 self.indices.clear();
510 insert_bulk_no_grow(&mut self.indices, &self.entries);
511 }
512
513 pub(crate) fn reverse(&mut self) {
514 self.entries.reverse();
515
516 let len = self.entries.len();
519 for i in &mut self.indices {
520 *i = len - *i - 1;
521 }
522 }
523}
524
525fn reserve_entries<K, V>(entries: &mut Entries<K, V>, additional: usize, try_capacity: usize) {
527 let try_capacity = try_capacity.min(IndexMapCore::<K, V>::MAX_ENTRIES_CAPACITY);
530 let try_add = try_capacity - entries.len();
531 if try_add > additional && entries.try_reserve_exact(try_add).is_ok() {
532 return;
533 }
534 entries.reserve_exact(additional);
535}
536
537impl<'a, K, V> RefMut<'a, K, V> {
538 #[inline]
539 fn new(indices: &'a mut Indices, entries: &'a mut Entries<K, V>) -> Self {
540 Self { indices, entries }
541 }
542
543 #[inline]
545 fn reserve_entries(&mut self, additional: usize) {
546 reserve_entries(self.entries, additional, self.indices.capacity());
547 }
548
549 fn insert_unique(self, hash: HashValue, key: K, value: V) -> OccupiedEntry<'a, K, V> {
552 let i = self.indices.len();
553 debug_assert_eq!(i, self.entries.len());
554 let entry = self
555 .indices
556 .insert_unique(hash.get(), i, get_hash(self.entries));
557 if self.entries.len() == self.entries.capacity() {
558 reserve_entries(self.entries, 1, 2 * self.entries.capacity());
562 }
563 self.entries.push(Bucket { hash, key, value });
564 OccupiedEntry::new(self.entries, entry)
565 }
566
567 fn shift_insert_unique(&mut self, index: usize, hash: HashValue, key: K, value: V) {
570 let end = self.indices.len();
571 assert!(index <= end);
572 self.increment_indices(index, end);
574 let entries = &*self.entries;
575 self.indices.insert_unique(hash.get(), index, move |&i| {
576 debug_assert_ne!(i, index);
578 let i = if i < index { i } else { i - 1 };
579 entries[i].hash.get()
580 });
581 if self.entries.len() == self.entries.capacity() {
582 self.reserve_entries(1);
585 }
586 self.entries.insert(index, Bucket { hash, key, value });
587 }
588
589 fn shift_remove_index(&mut self, index: usize) -> Option<(K, V)> {
591 match self.entries.get(index) {
592 Some(entry) => {
593 erase_index(self.indices, entry.hash, index);
594 Some(self.shift_remove_finish(index))
595 }
596 None => None,
597 }
598 }
599
600 fn shift_remove_finish(&mut self, index: usize) -> (K, V) {
604 self.decrement_indices(index + 1, self.entries.len());
606
607 let entry = self.entries.remove(index);
609 (entry.key, entry.value)
610 }
611
612 fn swap_remove_index(&mut self, index: usize) -> Option<(K, V)> {
614 match self.entries.get(index) {
615 Some(entry) => {
616 erase_index(self.indices, entry.hash, index);
617 Some(self.swap_remove_finish(index))
618 }
619 None => None,
620 }
621 }
622
623 fn swap_remove_finish(&mut self, index: usize) -> (K, V) {
627 let entry = self.entries.swap_remove(index);
630
631 if let Some(entry) = self.entries.get(index) {
633 let last = self.entries.len();
636 update_index(self.indices, entry.hash, last, index);
637 }
638
639 (entry.key, entry.value)
640 }
641
642 fn decrement_indices(&mut self, start: usize, end: usize) {
647 let shifted_entries = &self.entries[start..end];
649 if shifted_entries.len() > self.indices.capacity() / 2 {
650 for i in &mut *self.indices {
652 if start <= *i && *i < end {
653 *i -= 1;
654 }
655 }
656 } else {
657 for (i, entry) in (start..end).zip(shifted_entries) {
659 update_index(self.indices, entry.hash, i, i - 1);
660 }
661 }
662 }
663
664 fn increment_indices(&mut self, start: usize, end: usize) {
669 let shifted_entries = &self.entries[start..end];
671 if shifted_entries.len() > self.indices.capacity() / 2 {
672 for i in &mut *self.indices {
674 if start <= *i && *i < end {
675 *i += 1;
676 }
677 }
678 } else {
679 for (i, entry) in (start..end).zip(shifted_entries).rev() {
682 update_index(self.indices, entry.hash, i, i + 1);
683 }
684 }
685 }
686
687 #[track_caller]
688 fn move_index(&mut self, from: usize, to: usize) {
689 let from_hash = self.entries[from].hash;
690 let _ = self.entries[to]; if from != to {
692 update_index(self.indices, from_hash, from, usize::MAX);
694
695 if from < to {
697 self.decrement_indices(from + 1, to + 1);
698 self.entries[from..=to].rotate_left(1);
699 } else if to < from {
700 self.increment_indices(to, from);
701 self.entries[to..=from].rotate_right(1);
702 }
703
704 update_index(self.indices, from_hash, usize::MAX, to);
706 }
707 }
708
709 #[track_caller]
710 fn swap_indices(&mut self, a: usize, b: usize) {
711 if a == b && a < self.entries.len() {
713 return;
714 }
715
716 match self.indices.get_many_mut(
719 [self.entries[a].hash.get(), self.entries[b].hash.get()],
720 move |i, &x| if i == 0 { x == a } else { x == b },
721 ) {
722 [Some(ref_a), Some(ref_b)] => {
723 mem::swap(ref_a, ref_b);
724 self.entries.swap(a, b);
725 }
726 _ => panic!("indices not found"),
727 }
728 }
729}
730
731#[test]
732fn assert_send_sync() {
733 fn assert_send_sync<T: Send + Sync>() {}
734 assert_send_sync::<IndexMapCore<i32, i32>>();
735 assert_send_sync::<Entry<'_, i32, i32>>();
736 assert_send_sync::<IndexedEntry<'_, i32, i32>>();
737 assert_send_sync::<raw_entry_v1::RawEntryMut<'_, i32, i32, ()>>();
738}