@@ -342,6 +342,22 @@ where
342
342
self . get_full ( key) . map ( third)
343
343
}
344
344
345
+ /// Return references to the key-value pair stored for `key`,
346
+ /// if it is present, else `None`.
347
+ ///
348
+ /// Computes in **O(1)** time (average).
349
+ pub fn get_key_value < Q : ?Sized > ( & self , key : & Q ) -> Option < ( & K , & V ) >
350
+ where
351
+ Q : Hash + Equivalent < K > ,
352
+ {
353
+ if let Some ( i) = self . get_index_of ( key) {
354
+ let entry = & self . as_entries ( ) [ i] ;
355
+ Some ( ( & entry. key , & entry. value ) )
356
+ } else {
357
+ None
358
+ }
359
+ }
360
+
345
361
/// Return item index, key and value
346
362
pub fn get_full < Q : ?Sized > ( & self , key : & Q ) -> Option < ( usize , & K , & V ) >
347
363
where
@@ -424,6 +440,20 @@ where
424
440
self . swap_remove ( key)
425
441
}
426
442
443
+ /// Remove and return the key-value pair equivalent to `key`.
444
+ ///
445
+ /// **NOTE:** This is equivalent to `.swap_remove_entry(key)`, if you need to
446
+ /// preserve the order of the keys in the map, use `.shift_remove_entry(key)`
447
+ /// instead.
448
+ ///
449
+ /// Computes in **O(1)** time (average).
450
+ pub fn remove_entry < Q : ?Sized > ( & mut self , key : & Q ) -> Option < ( K , V ) >
451
+ where
452
+ Q : Hash + Equivalent < K > ,
453
+ {
454
+ self . swap_remove_entry ( key)
455
+ }
456
+
427
457
/// Remove the key-value pair equivalent to `key` and return
428
458
/// its value.
429
459
///
@@ -441,6 +471,25 @@ where
441
471
self . swap_remove_full ( key) . map ( third)
442
472
}
443
473
474
+ /// Remove and return the key-value pair equivalent to `key`.
475
+ ///
476
+ /// Like `Vec::swap_remove`, the pair is removed by swapping it with the
477
+ /// last element of the map and popping it off. **This perturbs
478
+ /// the postion of what used to be the last element!**
479
+ ///
480
+ /// Return `None` if `key` is not in map.
481
+ ///
482
+ /// Computes in **O(1)** time (average).
483
+ pub fn swap_remove_entry < Q : ?Sized > ( & mut self , key : & Q ) -> Option < ( K , V ) >
484
+ where
485
+ Q : Hash + Equivalent < K > ,
486
+ {
487
+ match self . swap_remove_full ( key) {
488
+ Some ( ( _, key, value) ) => Some ( ( key, value) ) ,
489
+ None => None ,
490
+ }
491
+ }
492
+
444
493
/// Remove the key-value pair equivalent to `key` and return it and
445
494
/// the index it had.
446
495
///
@@ -480,6 +529,25 @@ where
480
529
self . shift_remove_full ( key) . map ( third)
481
530
}
482
531
532
+ /// Remove and return the key-value pair equivalent to `key`.
533
+ ///
534
+ /// Like `Vec::remove`, the pair is removed by shifting all of the
535
+ /// elements that follow it, preserving their relative order.
536
+ /// **This perturbs the index of all of those elements!**
537
+ ///
538
+ /// Return `None` if `key` is not in map.
539
+ ///
540
+ /// Computes in **O(n)** time (average).
541
+ pub fn shift_remove_entry < Q : ?Sized > ( & mut self , key : & Q ) -> Option < ( K , V ) >
542
+ where
543
+ Q : Hash + Equivalent < K > ,
544
+ {
545
+ match self . shift_remove_full ( key) {
546
+ Some ( ( _, key, value) ) => Some ( ( key, value) ) ,
547
+ None => None ,
548
+ }
549
+ }
550
+
483
551
/// Remove the key-value pair equivalent to `key` and return it and
484
552
/// the index it had.
485
553
///
0 commit comments