@@ -316,6 +316,17 @@ impl<K, V> IndexMapCore<K, V> {
316
316
self . indices . find ( hash. get ( ) , eq) . copied ( )
317
317
}
318
318
319
+ /// Append a key-value pair to `entries`,
320
+ /// *without* checking whether it already exists.
321
+ fn push_entry ( & mut self , hash : HashValue , key : K , value : V ) {
322
+ if self . entries . len ( ) == self . entries . capacity ( ) {
323
+ // Reserve our own capacity synced to the indices,
324
+ // rather than letting `Vec::push` just double it.
325
+ self . borrow_mut ( ) . reserve_entries ( 1 ) ;
326
+ }
327
+ self . entries . push ( Bucket { hash, key, value } ) ;
328
+ }
329
+
319
330
pub ( crate ) fn insert_full ( & mut self , hash : HashValue , key : K , value : V ) -> ( usize , Option < V > )
320
331
where
321
332
K : Eq ,
@@ -330,7 +341,7 @@ impl<K, V> IndexMapCore<K, V> {
330
341
hash_table:: Entry :: Vacant ( entry) => {
331
342
let i = self . entries . len ( ) ;
332
343
entry. insert ( i) ;
333
- self . borrow_mut ( ) . push_entry ( hash, key, value) ;
344
+ self . push_entry ( hash, key, value) ;
334
345
debug_assert_eq ! ( self . indices. len( ) , self . entries. len( ) ) ;
335
346
( i, None )
336
347
}
@@ -362,7 +373,7 @@ impl<K, V> IndexMapCore<K, V> {
362
373
hash_table:: Entry :: Vacant ( entry) => {
363
374
let i = self . entries . len ( ) ;
364
375
entry. insert ( i) ;
365
- self . borrow_mut ( ) . push_entry ( hash, key, value) ;
376
+ self . push_entry ( hash, key, value) ;
366
377
debug_assert_eq ! ( self . indices. len( ) , self . entries. len( ) ) ;
367
378
( i, None )
368
379
}
@@ -522,37 +533,25 @@ impl<'a, K, V> RefMut<'a, K, V> {
522
533
self . entries . reserve_exact ( additional) ;
523
534
}
524
535
525
- /// Append a key-value pair to `entries`,
536
+ /// Insert a key-value pair in `entries`,
526
537
/// *without* checking whether it already exists.
527
- fn push_entry ( & mut self , hash : HashValue , key : K , value : V ) {
538
+ fn insert_unique ( mut self , hash : HashValue , key : K , value : V ) -> OccupiedEntry < ' a , K , V > {
528
539
if self . entries . len ( ) == self . entries . capacity ( ) {
529
540
// Reserve our own capacity synced to the indices,
530
541
// rather than letting `Vec::push` just double it.
531
542
self . reserve_entries ( 1 ) ;
532
543
}
533
- self . entries . push ( Bucket { hash, key, value } ) ;
534
- }
535
-
536
- /// Insert a key-value pair in `entries` at a particular index,
537
- /// *without* checking whether it already exists.
538
- fn insert_entry ( & mut self , index : usize , hash : HashValue , key : K , value : V ) {
539
- if self . entries . len ( ) == self . entries . capacity ( ) {
540
- // Reserve our own capacity synced to the indices,
541
- // rather than letting `Vec::insert` just double it.
542
- self . reserve_entries ( 1 ) ;
543
- }
544
- self . entries . insert ( index, Bucket { hash, key, value } ) ;
545
- }
546
-
547
- fn insert_unique ( & mut self , hash : HashValue , key : K , value : V ) -> usize {
548
544
let i = self . indices . len ( ) ;
549
- self . indices
545
+ let entry = self
546
+ . indices
550
547
. insert_unique ( hash. get ( ) , i, get_hash ( self . entries ) ) ;
551
548
debug_assert_eq ! ( i, self . entries. len( ) ) ;
552
- self . push_entry ( hash, key, value) ;
553
- i
549
+ self . entries . push ( Bucket { hash, key, value } ) ;
550
+ OccupiedEntry :: new ( self . entries , entry )
554
551
}
555
552
553
+ /// Insert a key-value pair in `entries` at a particular index,
554
+ /// *without* checking whether it already exists.
556
555
fn shift_insert_unique ( & mut self , index : usize , hash : HashValue , key : K , value : V ) {
557
556
let end = self . indices . len ( ) ;
558
557
assert ! ( index <= end) ;
@@ -565,7 +564,12 @@ impl<'a, K, V> RefMut<'a, K, V> {
565
564
let i = if i < index { i } else { i - 1 } ;
566
565
entries[ i] . hash . get ( )
567
566
} ) ;
568
- self . insert_entry ( index, hash, key, value) ;
567
+ if self . entries . len ( ) == self . entries . capacity ( ) {
568
+ // Reserve our own capacity synced to the indices,
569
+ // rather than letting `Vec::insert` just double it.
570
+ self . reserve_entries ( 1 ) ;
571
+ }
572
+ self . entries . insert ( index, Bucket { hash, key, value } ) ;
569
573
}
570
574
571
575
/// Remove an entry by shifting all entries that follow it
0 commit comments