Skip to content

Commit 74eed4a

Browse files
committed
Make get_index_mut return &K, and add get_index_mut2
1 parent 7dae8eb commit 74eed4a

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

src/map.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -479,21 +479,6 @@ where
479479
}
480480
}
481481

482-
pub(crate) fn get_full_mut2_impl<Q: ?Sized>(
483-
&mut self,
484-
key: &Q,
485-
) -> Option<(usize, &mut K, &mut V)>
486-
where
487-
Q: Hash + Equivalent<K>,
488-
{
489-
if let Some(i) = self.get_index_of(key) {
490-
let entry = &mut self.as_entries_mut()[i];
491-
Some((i, &mut entry.key, &mut entry.value))
492-
} else {
493-
None
494-
}
495-
}
496-
497482
/// Remove the key-value pair equivalent to `key` and return
498483
/// its value.
499484
///
@@ -780,8 +765,8 @@ impl<K, V, S> IndexMap<K, V, S> {
780765
/// Valid indices are *0 <= index < self.len()*
781766
///
782767
/// Computes in **O(1)** time.
783-
pub fn get_index_mut(&mut self, index: usize) -> Option<(&mut K, &mut V)> {
784-
self.as_entries_mut().get_mut(index).map(Bucket::muts)
768+
pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> {
769+
self.as_entries_mut().get_mut(index).map(Bucket::ref_mut)
785770
}
786771

787772
/// Get the first key-value pair

src/mutable_keys.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::hash::{BuildHasher, Hash};
22

3-
use super::{Equivalent, IndexMap};
3+
use super::{Bucket, Entries, Equivalent, IndexMap};
44

55
pub struct PrivateMarker {}
66

@@ -21,13 +21,22 @@ pub trait MutableKeys {
2121
type Value;
2222

2323
/// Return item index, mutable reference to key and value
24+
///
25+
/// Computes in **O(1)** time (average).
2426
fn get_full_mut2<Q: ?Sized>(
2527
&mut self,
2628
key: &Q,
2729
) -> Option<(usize, &mut Self::Key, &mut Self::Value)>
2830
where
2931
Q: Hash + Equivalent<Self::Key>;
3032

33+
/// Return mutable reference to key and value at an index.
34+
///
35+
/// Valid indices are *0 <= index < self.len()*
36+
///
37+
/// Computes in **O(1)** time.
38+
fn get_index_mut2(&mut self, index: usize) -> Option<(&mut Self::Key, &mut Self::Value)>;
39+
3140
/// Scan through each key-value pair in the map and keep those where the
3241
/// closure `keep` returns `true`.
3342
///
@@ -39,6 +48,7 @@ pub trait MutableKeys {
3948
where
4049
F: FnMut(&mut Self::Key, &mut Self::Value) -> bool;
4150

51+
#[doc(hidden)]
4252
/// This method is not useful in itself – it is there to “seal” the trait
4353
/// for external implementation, so that we can add methods without
4454
/// causing breaking changes.
@@ -55,11 +65,21 @@ where
5565
{
5666
type Key = K;
5767
type Value = V;
68+
5869
fn get_full_mut2<Q: ?Sized>(&mut self, key: &Q) -> Option<(usize, &mut K, &mut V)>
5970
where
6071
Q: Hash + Equivalent<K>,
6172
{
62-
self.get_full_mut2_impl(key)
73+
if let Some(i) = self.get_index_of(key) {
74+
let entry = &mut self.as_entries_mut()[i];
75+
Some((i, &mut entry.key, &mut entry.value))
76+
} else {
77+
None
78+
}
79+
}
80+
81+
fn get_index_mut2(&mut self, index: usize) -> Option<(&mut K, &mut V)> {
82+
self.as_entries_mut().get_mut(index).map(Bucket::muts)
6383
}
6484

6585
fn retain2<F>(&mut self, keep: F)

0 commit comments

Comments
 (0)