Skip to content

Commit 3b6d04b

Browse files
authored
Merge pull request #425 from cuviper/inner-core
Move `crate::map::core` to `crate::inner`
2 parents cfad758 + eb30eb1 commit 3b6d04b

File tree

10 files changed

+403
-347
lines changed

10 files changed

+403
-347
lines changed

src/map/core.rs renamed to src/inner.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! This is the core implementation that doesn't depend on the hasher at all.
22
//!
3-
//! The methods of `IndexMapCore` don't use any Hash properties of K.
3+
//! The methods of `Core` don't use any Hash properties of K.
44
//!
55
//! It's cleaner to separate them out, then the compiler checks that we are not
66
//! using Hash at all in these methods.
@@ -10,8 +10,6 @@
1010
mod entry;
1111
mod extract;
1212

13-
pub mod raw_entry_v1;
14-
1513
use alloc::vec::{self, Vec};
1614
use core::mem;
1715
use core::ops::RangeBounds;
@@ -23,12 +21,12 @@ use crate::{Bucket, Equivalent, HashValue, TryReserveError};
2321
type Indices = hash_table::HashTable<usize>;
2422
type Entries<K, V> = Vec<Bucket<K, V>>;
2523

26-
pub use entry::{Entry, IndexedEntry, OccupiedEntry, VacantEntry};
24+
pub use entry::{OccupiedEntry, VacantEntry};
2725
pub(crate) use extract::ExtractCore;
2826

2927
/// Core of the map that does not depend on S
30-
#[derive(Debug)]
31-
pub(crate) struct IndexMapCore<K, V> {
28+
#[cfg_attr(feature = "test_debug", derive(Debug))]
29+
pub(crate) struct Core<K, V> {
3230
/// indices mapping from the entry hash to its index.
3331
indices: Indices,
3432
/// entries is a dense vec maintaining entry order.
@@ -76,7 +74,7 @@ fn insert_bulk_no_grow<K, V>(indices: &mut Indices, entries: &[Bucket<K, V>]) {
7674
}
7775
}
7876

79-
impl<K, V> Clone for IndexMapCore<K, V>
77+
impl<K, V> Clone for Core<K, V>
8078
where
8179
K: Clone,
8280
V: Clone,
@@ -98,21 +96,21 @@ where
9896
}
9997
}
10098

101-
impl<K, V> IndexMapCore<K, V> {
99+
impl<K, V> Core<K, V> {
102100
/// The maximum capacity before the `entries` allocation would exceed `isize::MAX`.
103101
const MAX_ENTRIES_CAPACITY: usize = (isize::MAX as usize) / size_of::<Bucket<K, V>>();
104102

105103
#[inline]
106104
pub(crate) const fn new() -> Self {
107-
IndexMapCore {
105+
Core {
108106
indices: Indices::new(),
109107
entries: Vec::new(),
110108
}
111109
}
112110

113111
#[inline]
114112
pub(crate) fn with_capacity(n: usize) -> Self {
115-
IndexMapCore {
113+
Core {
116114
indices: Indices::with_capacity(n),
117115
entries: Vec::with_capacity(n),
118116
}
@@ -305,6 +303,15 @@ impl<K, V> IndexMapCore<K, V> {
305303
self.indices.find(hash.get(), eq).copied()
306304
}
307305

306+
/// Return the index in `entries` where an equivalent key can be found
307+
pub(crate) fn get_index_of_raw<F>(&self, hash: HashValue, mut is_match: F) -> Option<usize>
308+
where
309+
F: FnMut(&K) -> bool,
310+
{
311+
let eq = move |&i: &usize| is_match(&self.entries[i].key);
312+
self.indices.find(hash.get(), eq).copied()
313+
}
314+
308315
/// Append a key-value pair to `entries`,
309316
/// *without* checking whether it already exists.
310317
fn push_entry(&mut self, hash: HashValue, key: K, value: V) {
@@ -509,7 +516,13 @@ impl<K, V> IndexMapCore<K, V> {
509516

510517
/// Insert a key-value pair in `entries` at a particular index,
511518
/// *without* checking whether it already exists.
512-
fn shift_insert_unique(&mut self, index: usize, hash: HashValue, key: K, value: V) {
519+
pub(crate) fn shift_insert_unique(
520+
&mut self,
521+
index: usize,
522+
hash: HashValue,
523+
key: K,
524+
value: V,
525+
) -> &mut Bucket<K, V> {
513526
let end = self.indices.len();
514527
assert!(index <= end);
515528
// Increment others first so we don't have duplicate indices.
@@ -527,6 +540,7 @@ impl<K, V> IndexMapCore<K, V> {
527540
self.reserve_entries(1);
528541
}
529542
self.entries.insert(index, Bucket { hash, key, value });
543+
&mut self.entries[index]
530544
}
531545

532546
/// Remove an entry by shifting all entries that follow it
@@ -680,8 +694,5 @@ impl<K, V> IndexMapCore<K, V> {
680694
#[test]
681695
fn assert_send_sync() {
682696
fn assert_send_sync<T: Send + Sync>() {}
683-
assert_send_sync::<IndexMapCore<i32, i32>>();
684-
assert_send_sync::<Entry<'_, i32, i32>>();
685-
assert_send_sync::<IndexedEntry<'_, i32, i32>>();
686-
assert_send_sync::<raw_entry_v1::RawEntryMut<'_, i32, i32, ()>>();
697+
assert_send_sync::<Core<i32, i32>>();
687698
}

0 commit comments

Comments
 (0)