@@ -316,6 +316,17 @@ impl<K, V> IndexMapCore<K, V> {
316316 self . indices . find ( hash. get ( ) , eq) . copied ( )
317317 }
318318
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+
319330 pub ( crate ) fn insert_full ( & mut self , hash : HashValue , key : K , value : V ) -> ( usize , Option < V > )
320331 where
321332 K : Eq ,
@@ -330,7 +341,7 @@ impl<K, V> IndexMapCore<K, V> {
330341 hash_table:: Entry :: Vacant ( entry) => {
331342 let i = self . entries . len ( ) ;
332343 entry. insert ( i) ;
333- self . borrow_mut ( ) . push_entry ( hash, key, value) ;
344+ self . push_entry ( hash, key, value) ;
334345 debug_assert_eq ! ( self . indices. len( ) , self . entries. len( ) ) ;
335346 ( i, None )
336347 }
@@ -362,7 +373,7 @@ impl<K, V> IndexMapCore<K, V> {
362373 hash_table:: Entry :: Vacant ( entry) => {
363374 let i = self . entries . len ( ) ;
364375 entry. insert ( i) ;
365- self . borrow_mut ( ) . push_entry ( hash, key, value) ;
376+ self . push_entry ( hash, key, value) ;
366377 debug_assert_eq ! ( self . indices. len( ) , self . entries. len( ) ) ;
367378 ( i, None )
368379 }
@@ -522,37 +533,25 @@ impl<'a, K, V> RefMut<'a, K, V> {
522533 self . entries . reserve_exact ( additional) ;
523534 }
524535
525- /// Append a key-value pair to `entries`,
536+ /// Insert a key-value pair in `entries`,
526537 /// *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 > {
528539 if self . entries . len ( ) == self . entries . capacity ( ) {
529540 // Reserve our own capacity synced to the indices,
530541 // rather than letting `Vec::push` just double it.
531542 self . reserve_entries ( 1 ) ;
532543 }
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 {
548544 let i = self . indices . len ( ) ;
549- self . indices
545+ let entry = self
546+ . indices
550547 . insert_unique ( hash. get ( ) , i, get_hash ( self . entries ) ) ;
551548 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 )
554551 }
555552
553+ /// Insert a key-value pair in `entries` at a particular index,
554+ /// *without* checking whether it already exists.
556555 fn shift_insert_unique ( & mut self , index : usize , hash : HashValue , key : K , value : V ) {
557556 let end = self . indices . len ( ) ;
558557 assert ! ( index <= end) ;
@@ -565,7 +564,12 @@ impl<'a, K, V> RefMut<'a, K, V> {
565564 let i = if i < index { i } else { i - 1 } ;
566565 entries[ i] . hash . get ( )
567566 } ) ;
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 } ) ;
569573 }
570574
571575 /// Remove an entry by shifting all entries that follow it
0 commit comments