Skip to content

Commit e295a57

Browse files
committed
Move insert fully into the core
1 parent 0be1f26 commit e295a57

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/map.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::cmp::Ordering;
2424
use std::fmt;
2525

2626
use equivalent::Equivalent;
27-
use map_core::{IndexMapCore, InsertValue, MakeEntry, ProbeAction, Size};
27+
use map_core::{IndexMapCore, MakeEntry, ProbeAction, Size};
2828
use util::third;
2929
use {Bucket, Entries, HashValue};
3030

@@ -236,23 +236,10 @@ where
236236
/// FIXME Not implemented fully yet.
237237
pub fn reserve(&mut self, additional: usize) {
238238
if additional > 0 {
239-
self.reserve_one();
239+
self.core.reserve_one();
240240
}
241241
}
242242

243-
fn reserve_one(&mut self) {
244-
if self.len() == self.capacity() {
245-
dispatch_32_vs_64!(self.double_capacity());
246-
}
247-
}
248-
249-
fn double_capacity<Sz>(&mut self)
250-
where
251-
Sz: Size,
252-
{
253-
self.core.double_capacity::<Sz>();
254-
}
255-
256243
/// Insert a key-value pair in the map.
257244
///
258245
/// If an equivalent key already exists in the map: the key remains and
@@ -284,16 +271,16 @@ where
284271
/// See also [`entry`](#method.entry) if you you want to insert *or* modify
285272
/// or if you need to get the index of the corresponding key-value pair.
286273
pub fn insert_full(&mut self, key: K, value: V) -> (usize, Option<V>) {
287-
self.reserve_one();
288-
dispatch_32_vs_64!(self.insert_phase_1::<_>(key, InsertValue(value)))
274+
let hash = hash_elem_using(&self.hash_builder, &key);
275+
self.core.insert_full(hash, key, value)
289276
}
290277

291278
/// Get the given key’s corresponding entry in the map for insertion and/or
292279
/// in-place manipulation.
293280
///
294281
/// Computes in **O(1)** time (amortized average).
295282
pub fn entry(&mut self, key: K) -> Entry<K, V> {
296-
self.reserve_one();
283+
self.core.reserve_one();
297284
dispatch_32_vs_64!(self.insert_phase_1::<_>(key, MakeEntry))
298285
}
299286

src/map_core.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ impl<K, V> IndexMapCore<K, V> {
436436
self.entries = Vec::with_capacity(usable_capacity(raw_cap));
437437
}
438438

439+
pub(crate) fn reserve_one(&mut self) {
440+
if self.len() == self.capacity() {
441+
dispatch_32_vs_64!(self.double_capacity());
442+
}
443+
}
444+
439445
#[inline(never)]
440446
// `Sz` is *current* Size class, before grow
441447
pub(crate) fn double_capacity<Sz>(&mut self)
@@ -561,7 +567,7 @@ impl<K, V> IndexMapCore<K, V> {
561567
}
562568

563569
/// phase 2 is post-insert where we forward-shift `Pos` in the indices.
564-
pub(crate) fn insert_phase_2<Sz>(&mut self, mut probe: usize, mut old_pos: Pos)
570+
fn insert_phase_2<Sz>(&mut self, mut probe: usize, mut old_pos: Pos)
565571
where
566572
Sz: Size,
567573
{
@@ -576,6 +582,14 @@ impl<K, V> IndexMapCore<K, V> {
576582
});
577583
}
578584

585+
pub(crate) fn insert_full(&mut self, hash: HashValue, key: K, value: V) -> (usize, Option<V>)
586+
where
587+
K: Eq,
588+
{
589+
self.reserve_one();
590+
dispatch_32_vs_64!(self.insert_phase_1::<_>(hash, key, InsertValue(value)))
591+
}
592+
579593
/// Return probe (indices) and position (entries)
580594
pub(crate) fn find_using<F>(&self, hash: HashValue, key_eq: F) -> Option<(usize, usize)>
581595
where
@@ -864,7 +878,7 @@ pub(crate) trait ProbeAction<'a, Sz: Size, K, V>: Sized {
864878
fn steal(self, entry: VacantEntry<'a, K, V>) -> Self::Output;
865879
}
866880

867-
pub(crate) struct InsertValue<V>(pub(crate) V);
881+
struct InsertValue<V>(V);
868882

869883
impl<'a, Sz: Size, K, V> ProbeAction<'a, Sz, K, V> for InsertValue<V> {
870884
type Output = (usize, Option<V>);

0 commit comments

Comments
 (0)