@@ -198,11 +198,23 @@ impl<Sz> ShortHashProxy<Sz>
198
198
199
199
/// A hash map with consistent order of the key-value pairs.
200
200
///
201
+ /// # Order
202
+ ///
201
203
/// The key-value pairs have a consistent order that is determined by
202
204
/// the sequence of insertion and removal calls on the map. The order does
203
205
/// not depend on the keys or the hash function at all.
204
206
///
205
- /// All iterators traverse the map in the same order.
207
+ /// All iterators traverse the map in *the order*.
208
+ ///
209
+ /// # Mutable Keys
210
+ ///
211
+ /// Some methods expose `&mut K`, mutable references to the key as it is stored
212
+ /// in the map. The key is allowed to be modified, but *only in a way that
213
+ /// preserves its hash and equality* (it is only useful for composite key
214
+ /// structs).
215
+ ///
216
+ /// This is sound (memory safe) but a logical error hazard (just like
217
+ /// implementing PartialEq, Eq, or Hash incorrectly would be).
206
218
///
207
219
/// # Examples
208
220
///
@@ -314,17 +326,26 @@ macro_rules! probe_loop {
314
326
}
315
327
316
328
impl < K , V > OrderMap < K , V > {
329
+ /// Create a new map. (Does not allocate.)
317
330
pub fn new ( ) -> Self {
318
331
Self :: with_capacity ( 0 )
319
332
}
320
333
334
+ /// Create a new map with capacity for `n` key-value pairs. (Does not
335
+ /// allocate if `n` is zero.)
336
+ ///
337
+ /// Computes in **O(n)** time.
321
338
pub fn with_capacity ( n : usize ) -> Self {
322
339
Self :: with_capacity_and_hasher ( n, <_ >:: default ( ) )
323
340
}
324
341
}
325
342
326
343
impl < K , V , S > OrderMap < K , V , S >
327
344
{
345
+ /// Create a new map with capacity for `n` key-value pairs. (Does not
346
+ /// allocate if `n` is zero.)
347
+ ///
348
+ /// Computes in **O(n)** time.
328
349
pub fn with_capacity_and_hasher ( n : usize , hash_builder : S ) -> Self
329
350
where S : BuildHasher
330
351
{
@@ -347,6 +368,9 @@ impl<K, V, S> OrderMap<K, V, S>
347
368
}
348
369
}
349
370
371
+ /// Return the number of key-value pairs in the map.
372
+ ///
373
+ /// Computes in **O(1)** time.
350
374
pub fn len ( & self ) -> usize { self . entries . len ( ) }
351
375
352
376
// Return whether we need 32 or 64 bits to specify a bucket or entry index
@@ -367,6 +391,7 @@ impl<K, V, S> OrderMap<K, V, S>
367
391
self . indices . len ( )
368
392
}
369
393
394
+ /// Computes in **O(1)** time.
370
395
pub fn capacity ( & self ) -> usize {
371
396
usable_capacity ( self . raw_capacity ( ) )
372
397
}
@@ -420,13 +445,15 @@ pub enum Entry<'a, K: 'a, V: 'a, S: 'a = RandomState> {
420
445
}
421
446
422
447
impl < ' a , K , V , S > Entry < ' a , K , V , S > {
448
+ /// Computes in **O(1)** time (amortized average).
423
449
pub fn or_insert ( self , default : V ) -> & ' a mut V {
424
450
match self {
425
451
Entry :: Occupied ( entry) => entry. into_mut ( ) ,
426
452
Entry :: Vacant ( entry) => entry. insert ( default) ,
427
453
}
428
454
}
429
455
456
+ /// Computes in **O(1)** time (amortized average).
430
457
pub fn or_insert_with < F > ( self , call : F ) -> & ' a mut V
431
458
where F : FnOnce ( ) -> V ,
432
459
{
@@ -512,6 +539,8 @@ impl<K, V, S> OrderMap<K, V, S>
512
539
S : BuildHasher ,
513
540
{
514
541
/// Get the given key’s corresponding entry in the map for in-place manipulation.
542
+ ///
543
+ /// Computes in **O(1)** time (amortized average).
515
544
pub fn entry ( & mut self , key : K ) -> Entry < K , V , S > {
516
545
self . reserve_one ( ) ;
517
546
dispatch_32_vs_64 ! ( self . entry_phase_1( key) )
@@ -709,7 +738,7 @@ impl<K, V, S> OrderMap<K, V, S>
709
738
/// If a value already existed for `key`, that old value is returned
710
739
/// in `Some`; otherwise, return `None`.
711
740
///
712
- /// Computes in **O(1)** time (amortized).
741
+ /// Computes in **O(1)** time (amortized average ).
713
742
pub fn insert ( & mut self , key : K , value : V ) -> Option < V > {
714
743
self . reserve_one ( ) ;
715
744
if self . size_class_is_64bit ( ) {
@@ -754,6 +783,9 @@ impl<K, V, S> OrderMap<K, V, S>
754
783
}
755
784
}
756
785
786
+ /// Return `true` if and equivalent to `key` exists in the map.
787
+ ///
788
+ /// Computes in **O(1)** time (average).
757
789
pub fn contains_key < Q : ?Sized > ( & self , key : & Q ) -> bool
758
790
where K : Borrow < Q > ,
759
791
Q : Eq + Hash ,
@@ -848,6 +880,8 @@ impl<K, V, S> OrderMap<K, V, S>
848
880
/// the postion of what used to be the last element!**
849
881
///
850
882
/// Return `None` if `key` is not in map.
883
+ ///
884
+ /// Computes in **O(1)** time (average).
851
885
pub fn swap_remove < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
852
886
where K : Borrow < Q > ,
853
887
Q : Eq + Hash ,
@@ -889,19 +923,48 @@ impl<K, V, S> OrderMap<K, V, S>
889
923
pub fn pop ( & mut self ) -> Option < ( K , V ) > {
890
924
self . pop_impl ( )
891
925
}
926
+
927
+ /// Scan through each key-value pair in the map and keep those where the
928
+ /// closure `keep` returns `true`.
929
+ ///
930
+ /// The order the elements are visited is not specified.
931
+ ///
932
+ /// Computes in **O(n)** time (average).
933
+ pub fn retain < F > ( & mut self , mut keep : F )
934
+ where F : FnMut ( & mut K , & mut V ) -> bool ,
935
+ {
936
+ // We can use either forward or reverse scan, but forward was
937
+ // faster in a microbenchmark
938
+ let mut i = 0 ;
939
+ while i < self . len ( ) {
940
+ {
941
+ let entry = & mut self . entries [ i] ;
942
+ if keep ( & mut entry. key , & mut entry. value ) {
943
+ i += 1 ;
944
+ continue ;
945
+ }
946
+ }
947
+ self . swap_remove_index ( i) ;
948
+ // skip increment on remove
949
+ }
950
+ }
892
951
}
893
952
894
953
impl < K , V , S > OrderMap < K , V , S > {
895
954
/// Get a key-value pair by index
896
955
///
897
956
/// Valid indices are *0 <= index < self.len()*
957
+ ///
958
+ /// Computes in **O(1)** time.
898
959
pub fn get_index ( & self , index : usize ) -> Option < ( & K , & V ) > {
899
960
self . entries . get ( index) . map ( |ent| ( & ent. key , & ent. value ) )
900
961
}
901
962
902
963
/// Get a key-value pair by index
903
964
///
904
965
/// Valid indices are *0 <= index < self.len()*
966
+ ///
967
+ /// Computes in **O(1)** time.
905
968
pub fn get_index_mut ( & mut self , index : usize ) -> Option < ( & mut K , & mut V ) > {
906
969
self . entries . get_mut ( index) . map ( |ent| ( & mut ent. key , & mut ent. value ) )
907
970
}
0 commit comments