Skip to content

Commit

Permalink
Opt-in mutable access on IndexSet
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Mar 20, 2024
1 parent 184fe4b commit 210b027
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! - The [`Equivalent`] trait, which offers more flexible equality definitions
//! between borrowed and owned versions of keys.
//! - The [`MutableKeys`][map::MutableKeys] trait, which gives opt-in mutable
//! access to hash map keys.
//! access to map keys, and [`MutableValues`][set::MutableValues] for sets.
//!
//! ### Feature Flags
//!
Expand Down Expand Up @@ -116,7 +116,6 @@ mod arbitrary;
mod macros;
#[cfg(feature = "borsh")]
mod borsh;
mod mutable_keys;
#[cfg(feature = "serde")]
mod serde;
mod util;
Expand Down
10 changes: 2 additions & 8 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

mod core;
mod iter;
mod mutable;
mod slice;

#[cfg(feature = "serde")]
Expand All @@ -17,8 +18,8 @@ pub use self::core::{Entry, IndexedEntry, OccupiedEntry, VacantEntry};
pub use self::iter::{
Drain, IntoIter, IntoKeys, IntoValues, Iter, IterMut, Keys, Splice, Values, ValuesMut,
};
pub use self::mutable::MutableKeys;
pub use self::slice::Slice;
pub use crate::mutable_keys::MutableKeys;

#[cfg(feature = "rayon")]
pub use crate::rayon::map as rayon;
Expand Down Expand Up @@ -808,13 +809,6 @@ impl<K, V, S> IndexMap<K, V, S> {
self.core.retain_in_order(move |k, v| keep(k, v));
}

pub(crate) fn retain_mut<F>(&mut self, keep: F)
where
F: FnMut(&mut K, &mut V) -> bool,
{
self.core.retain_in_order(keep);
}

/// Sort the map’s key-value pairs by the default ordering of the keys.
///
/// This is a stable sort -- but equivalent keys should not normally coexist in
Expand Down
6 changes: 3 additions & 3 deletions src/mutable_keys.rs → src/map/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{Bucket, Entries, Equivalent, IndexMap};
///
/// These methods expose `&mut K`, mutable references to the key as it is stored
/// in the map.
/// You are allowed to modify the keys in the hashmap **if the modification
/// You are allowed to modify the keys in the map **if the modification
/// does not change the key’s hash and equality**.
///
/// If keys are modified erroneously, you can no longer look them up.
Expand Down Expand Up @@ -49,7 +49,7 @@ pub trait MutableKeys: private::Sealed {
F: FnMut(&mut Self::Key, &mut Self::Value) -> bool;
}

/// Opt-in mutable access to keys.
/// Opt-in mutable access to [`IndexMap`] keys.
///
/// See [`MutableKeys`] for more information.
impl<K, V, S> MutableKeys for IndexMap<K, V, S>
Expand Down Expand Up @@ -79,7 +79,7 @@ where
where
F: FnMut(&mut K, &mut V) -> bool,
{
self.retain_mut(keep)
self.core.retain_in_order(keep);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A hash set implemented using [`IndexMap`]

mod iter;
mod mutable;
mod slice;

#[cfg(test)]
Expand All @@ -9,6 +10,7 @@ mod tests;
pub use self::iter::{
Difference, Drain, Intersection, IntoIter, Iter, Splice, SymmetricDifference, Union,
};
pub use self::mutable::MutableValues;
pub use self::slice::Slice;

#[cfg(feature = "rayon")]
Expand Down
86 changes: 86 additions & 0 deletions src/set/mutable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use core::hash::{BuildHasher, Hash};

use super::{Equivalent, IndexSet};
use crate::map::MutableKeys;

/// Opt-in mutable access to [`IndexSet`] values.
///
/// These methods expose `&mut T`, mutable references to the value as it is stored
/// in the set.
/// You are allowed to modify the values in the set **if the modification
/// does not change the value’s hash and equality**.
///
/// If values are modified erroneously, you can no longer look them up.
/// This is sound (memory safe) but a logical error hazard (just like
/// implementing `PartialEq`, `Eq`, or `Hash` incorrectly would be).
///
/// `use` this trait to enable its methods for `IndexSet`.
///
/// This trait is sealed and cannot be implemented for types outside this crate.
pub trait MutableValues: private::Sealed {
type Value;

/// Return item index and mutable reference to the value
///
/// Computes in **O(1)** time (average).
fn get_full_mut2<Q: ?Sized>(&mut self, value: &Q) -> Option<(usize, &mut Self::Value)>
where
Q: Hash + Equivalent<Self::Value>;

/// Return mutable reference to the value at an index.
///
/// Valid indices are *0 <= index < self.len()*
///
/// Computes in **O(1)** time.
fn get_index_mut2(&mut self, index: usize) -> Option<&mut Self::Value>;

/// Scan through each value in the set and keep those where the
/// closure `keep` returns `true`.
///
/// The values are visited in order, and remaining values keep their order.
///
/// Computes in **O(n)** time (average).
fn retain2<F>(&mut self, keep: F)
where
F: FnMut(&mut Self::Value) -> bool;
}

/// Opt-in mutable access to [`IndexSet`] values.
///
/// See [`MutableValues`] for more information.
impl<T, S> MutableValues for IndexSet<T, S>
where
S: BuildHasher,
{
type Value = T;

fn get_full_mut2<Q: ?Sized>(&mut self, value: &Q) -> Option<(usize, &mut T)>
where
Q: Hash + Equivalent<T>,
{
match self.map.get_full_mut2(value) {
Some((index, value, ())) => Some((index, value)),
None => None,
}
}

fn get_index_mut2(&mut self, index: usize) -> Option<&mut T> {
match self.map.get_index_mut2(index) {
Some((value, ())) => Some(value),
None => None,
}
}

fn retain2<F>(&mut self, mut keep: F)
where
F: FnMut(&mut T) -> bool,
{
self.map.retain2(move |value, ()| keep(value));
}
}

mod private {
pub trait Sealed {}

impl<T, S> Sealed for super::IndexSet<T, S> {}
}

0 comments on commit 210b027

Please sign in to comment.