Use a SortedMap instead of a VecMap.
This commit is contained in:
parent
13608715d8
commit
9f2ab5b9ad
3 changed files with 19 additions and 16 deletions
|
@ -96,6 +96,23 @@ impl<K: Ord, V> SortedMap<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Gets a mutable reference to the value in the entry, or insert a new one.
|
||||
#[inline]
|
||||
pub fn get_mut_or_insert_default(&mut self, key: K) -> &mut V
|
||||
where
|
||||
K: Eq,
|
||||
V: Default,
|
||||
{
|
||||
let index = match self.lookup_index_for(&key) {
|
||||
Ok(index) => index,
|
||||
Err(index) => {
|
||||
self.data.insert(index, (key, V::default()));
|
||||
index
|
||||
}
|
||||
};
|
||||
unsafe { &mut self.data.get_unchecked_mut(index).1 }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn clear(&mut self) {
|
||||
self.data.clear();
|
||||
|
|
|
@ -53,20 +53,6 @@ where
|
|||
self.0.iter_mut().find(|(key, _)| k == key.borrow()).map(|elem| &mut elem.1)
|
||||
}
|
||||
|
||||
/// Gets a mutable reference to the value in the entry, or insert a new one.
|
||||
pub fn get_mut_or_insert_default(&mut self, k: K) -> &mut V
|
||||
where
|
||||
K: Eq,
|
||||
V: Default,
|
||||
{
|
||||
let pos = self.0.iter().position(|(key, _)| &k == key).unwrap_or_else(|| {
|
||||
let pos = self.0.len();
|
||||
self.0.push((k, V::default()));
|
||||
pos
|
||||
});
|
||||
&mut self.0[pos].1
|
||||
}
|
||||
|
||||
/// Returns the any value corresponding to the supplied predicate filter.
|
||||
///
|
||||
/// The supplied predicate will be applied to each (key, value) pair and it will return a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue