@@ -41,7 +41,7 @@ use std::collections::hash_map::RandomState;
41
41
42
42
use self :: core:: RingMapCore ;
43
43
use crate :: util:: third;
44
- use crate :: { Bucket , Entries , Equivalent , HashValue , TryReserveError } ;
44
+ use crate :: { Bucket , Entries , Equivalent , GetDisjointMutError , HashValue , TryReserveError } ;
45
45
46
46
/// A hash table where the iteration order of the key-value pairs is independent
47
47
/// of the hash values of the keys.
@@ -825,6 +825,33 @@ where
825
825
}
826
826
}
827
827
828
+ /// Return the values for `N` keys. If any key is duplicated, this function will panic.
829
+ ///
830
+ /// # Examples
831
+ ///
832
+ /// ```
833
+ /// let mut map = ringmap::RingMap::from([(1, 'a'), (3, 'b'), (2, 'c')]);
834
+ /// assert_eq!(map.get_disjoint_mut([&2, &1]), [Some(&mut 'c'), Some(&mut 'a')]);
835
+ /// ```
836
+ pub fn get_disjoint_mut < Q , const N : usize > ( & mut self , keys : [ & Q ; N ] ) -> [ Option < & mut V > ; N ]
837
+ where
838
+ Q : ?Sized + Hash + Equivalent < K > ,
839
+ {
840
+ let indices = keys. map ( |key| self . get_index_of ( key) ) ;
841
+ let ( head, tail) = self . as_mut_slices ( ) ;
842
+ match Slice :: get_disjoint_opt_mut ( head, tail, indices) {
843
+ Err ( GetDisjointMutError :: IndexOutOfBounds ) => {
844
+ unreachable ! (
845
+ "Internal error: indices should never be OOB as we got them from get_index_of"
846
+ ) ;
847
+ }
848
+ Err ( GetDisjointMutError :: OverlappingIndices ) => {
849
+ panic ! ( "duplicate keys found" ) ;
850
+ }
851
+ Ok ( key_values) => key_values. map ( |kv_opt| kv_opt. map ( |kv| kv. 1 ) ) ,
852
+ }
853
+ }
854
+
828
855
/// Remove the key-value pair equivalent to `key` and return its value.
829
856
///
830
857
/// Like [`VecDeque::remove`], the pair is removed by shifting all of the
@@ -1286,6 +1313,26 @@ impl<K, V, S> RingMap<K, V, S> {
1286
1313
Some ( IndexedEntry :: new ( & mut self . core , index) )
1287
1314
}
1288
1315
1316
+ /// Get an array of `N` key-value pairs by `N` indices
1317
+ ///
1318
+ /// Valid indices are *0 <= index < self.len()* and each index needs to be unique.
1319
+ ///
1320
+ /// # Examples
1321
+ ///
1322
+ /// ```
1323
+ /// let mut map = ringmap::RingMap::from([(1, 'a'), (3, 'b'), (2, 'c')]);
1324
+ /// assert_eq!(map.get_disjoint_indices_mut([2, 0]), Ok([(&2, &mut 'c'), (&1, &mut 'a')]));
1325
+ /// ```
1326
+ pub fn get_disjoint_indices_mut < const N : usize > (
1327
+ & mut self ,
1328
+ indices : [ usize ; N ] ,
1329
+ ) -> Result < [ ( & K , & mut V ) ; N ] , GetDisjointMutError > {
1330
+ let indices = indices. map ( Some ) ;
1331
+ let ( head, tail) = self . as_mut_slices ( ) ;
1332
+ let key_values = Slice :: get_disjoint_opt_mut ( head, tail, indices) ?;
1333
+ Ok ( key_values. map ( Option :: unwrap) )
1334
+ }
1335
+
1289
1336
/// Get the first key-value pair
1290
1337
///
1291
1338
/// Computes in **O(1)** time.
0 commit comments