Skip to content

Commit 8973b0f

Browse files
authored
Merge pull request #136 from cuviper/std-parity
Add more methods matching the standard HashMap
2 parents 299d950 + 6e8614d commit 8973b0f

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

src/map.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,22 @@ where
342342
self.get_full(key).map(third)
343343
}
344344

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+
345361
/// Return item index, key and value
346362
pub fn get_full<Q: ?Sized>(&self, key: &Q) -> Option<(usize, &K, &V)>
347363
where
@@ -424,6 +440,20 @@ where
424440
self.swap_remove(key)
425441
}
426442

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+
427457
/// Remove the key-value pair equivalent to `key` and return
428458
/// its value.
429459
///
@@ -441,6 +471,25 @@ where
441471
self.swap_remove_full(key).map(third)
442472
}
443473

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+
444493
/// Remove the key-value pair equivalent to `key` and return it and
445494
/// the index it had.
446495
///
@@ -480,6 +529,25 @@ where
480529
self.shift_remove_full(key).map(third)
481530
}
482531

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+
483551
/// Remove the key-value pair equivalent to `key` and return it and
484552
/// the index it had.
485553
///

src/set.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ where
330330
where
331331
Q: Hash + Equivalent<T>,
332332
{
333-
self.map.get_full(value).map(|(_, x, &())| x)
333+
self.map.get_key_value(value).map(|(x, &())| x)
334334
}
335335

336336
/// Return item index and value
@@ -439,7 +439,7 @@ where
439439
where
440440
Q: Hash + Equivalent<T>,
441441
{
442-
self.map.swap_remove_full(value).map(|(_, x, ())| x)
442+
self.map.swap_remove_entry(value).map(|(x, ())| x)
443443
}
444444

445445
/// Removes and returns the value in the set, if any, that is equal to the
@@ -456,7 +456,7 @@ where
456456
where
457457
Q: Hash + Equivalent<T>,
458458
{
459-
self.map.shift_remove_full(value).map(|(_, x, ())| x)
459+
self.map.shift_remove_entry(value).map(|(x, ())| x)
460460
}
461461

462462
/// Remove the value from the set return it and the index it had.

0 commit comments

Comments
 (0)