diff --git a/stable_set/src/lib.rs b/stable_set/src/lib.rs index afee5af..eae9366 100644 --- a/stable_set/src/lib.rs +++ b/stable_set/src/lib.rs @@ -1,14 +1,14 @@ //! [StableMap] and [StableSet] are a hash map and hash set that preserve the order of their entries, //! i.e. the order in which entries are inserted is remembered and iterators yield elements in that order. -//! +//! //! Strictly speaking, this only holds if elements are not removed, as removal via the `swap_remove_*` series of methods perturbs the order //! (use the more expensive `retain` operation to remove elements while preserving their order). -//! +//! //! Both structs are implemented as a `Vec` storing the actual entries, supplemented by a hashbrown `HashTable` to allow for fast lookups. //! This table only stores indices into the `Vec`. -//! +//! //! This crate is very similar to the `index_map` crate, however there are two key differences: -//! +//! //! 1. Hashes are not stored with the entries, but instead recalculated when needed. //! This saves memory and potentially improves performance when hashes are cheap to calculate. //! 2. For small tables, the index table stores `u32` values, again saving memory. @@ -19,13 +19,13 @@ mod index_table; mod util; -pub use stable_set::StableSet; pub use stable_map::StableMap; +pub use stable_set::StableSet; -pub mod stable_set; pub mod stable_map; +pub mod stable_set; #[cfg(test)] -mod test_set; +mod test_map; #[cfg(test)] -mod test_map; \ No newline at end of file +mod test_set; diff --git a/stable_set/src/stable_map.rs b/stable_set/src/stable_map.rs index 7d6c946..fdc31b3 100644 --- a/stable_set/src/stable_map.rs +++ b/stable_set/src/stable_map.rs @@ -15,7 +15,7 @@ use std::{borrow::Borrow, hash::BuildHasher, ops::RangeBounds}; /// `S: BuildHasher` is used for hashing elements /// and `W: SmallIndex` is the type used for small indices internally (`W` should usually be omitted, it then defaults to `u32`). #[derive(Clone)] -pub struct StableMap { +pub struct StableMap { index_table: IndexTable, items: Vec<(K, V)>, build_hasher: S, @@ -64,7 +64,9 @@ impl StableMap { } } -impl std::fmt::Debug for StableMap { +impl std::fmt::Debug + for StableMap +{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_map().entries(self.iter()).finish() } @@ -416,7 +418,9 @@ impl<'a, K, V, S, W: SmallIndex> IntoIterator for &'a mut StableMap } } -impl FromIterator<(K, V)> for StableMap { +impl FromIterator<(K, V)> + for StableMap +{ fn from_iter>(iter: Iter) -> Self { let iter = iter.into_iter(); let (lower_bound, _) = iter.size_hint(); diff --git a/stable_set/src/stable_set.rs b/stable_set/src/stable_set.rs index ba67cb5..09db103 100644 --- a/stable_set/src/stable_set.rs +++ b/stable_set/src/stable_set.rs @@ -8,13 +8,13 @@ use index_table::{IndexTable, SmallIndex}; use std::{borrow::Borrow, hash::BuildHasher, ops::RangeBounds}; /// A hash set that maintains the order of its elements. -/// +/// /// In `StableSet`, /// `T: Hash + Eq` is the type of elements of the set, /// `S: BuildHasher` is used for hashing elements /// and `W: SmallIndex` is the type used for small indices internally (`W` should usually be omitted, it then defaults to `u32`). #[derive(Clone)] -pub struct StableSet { +pub struct StableSet { index_table: IndexTable, items: Vec, build_hasher: S, @@ -206,7 +206,7 @@ impl StableSet { item } /// Removes the specified value from the set, if it exists. Returns `true` if an item was removed. - /// + /// /// The last item is put in its place. pub fn swap_remove(&mut self, value: &Q) -> bool where @@ -216,7 +216,7 @@ impl StableSet { self.swap_remove_full(value).is_some() } /// Removes the specified value from the set and returns it, if it exists. - /// + /// /// The last item is put in its place. pub fn swap_take(&mut self, value: &Q) -> Option where @@ -226,7 +226,7 @@ impl StableSet { self.swap_remove_full(value).map(|x| x.1) } /// Removes the specified value from the set and returns its index and it, if it exists. - /// + /// /// The last item is put in its place. pub fn swap_remove_full(&mut self, value: &Q) -> Option<(usize, T)> where @@ -247,7 +247,7 @@ impl StableSet { } } /// Removes the item at the given index and returns it, if it exists. - /// + /// /// The last item is put in its place. pub fn swap_remove_index(&mut self, index: usize) -> Option { let hash = self.build_hasher.hash_one(self.items.get(index)?); @@ -258,15 +258,15 @@ impl StableSet { Some(self.swap_remove_finish(index)) } /// Returns `true` if the set is a subset of `other`. - /// + /// /// The order of elements is ignored. pub fn is_subset(&self, other: &StableSet) -> bool { self.len() <= other.len() && self.iter().all(|t| other.contains(t)) } /// Removes all items from the set for which `f` evaluates to `false`. - /// + /// /// `f` is guaranteed to be called exactly once for each item and in order. - /// + /// /// The order of elements is preserved. pub fn retain(&mut self, mut f: impl FnMut(&T) -> bool) { let mut in_index = 0; @@ -304,7 +304,7 @@ impl PartialEq Eq for StableSet {} /// An iterator that moves out of a set. -/// +/// /// This struct is created by the `into_iter` method on [`StableSet`]. pub struct IntoIter { inner: std::vec::IntoIter, @@ -332,7 +332,7 @@ impl<'a, T, S, W> IntoIterator for &'a StableSet { } /// An iterator that returns references into a set. -/// +/// /// This struct is created by the [`iter`](StableSet::iter) method on [`StableSet`]. pub struct Iter<'a, T> { inner: std::slice::Iter<'a, T>, @@ -344,7 +344,7 @@ impl<'a, T> Iterator for Iter<'a, T> { impl StableSet { /// Returns an iterator over the set. - /// + /// /// The iterator yields all items in order. pub fn iter(&self) -> Iter<'_, T> { Iter { @@ -398,9 +398,9 @@ impl Iterator for Drain<'_, T> { impl StableSet { /// Returns an iterator yielding all elements with indices in the specified range and removes those elements from the set. - /// + /// /// Panics if the range is invalid or out of bounds. - /// + /// /// If the returned iterator is leaked, the set is left in an invalid state and can only be dropped. /// Any other operations may panic or return incorrect results. pub fn drain(&mut self, range: impl RangeBounds) -> Drain<'_, T> { diff --git a/stable_set/src/util.rs b/stable_set/src/util.rs index d3110a6..8774c18 100644 --- a/stable_set/src/util.rs +++ b/stable_set/src/util.rs @@ -11,9 +11,15 @@ pub fn simplify_range(range: impl RangeBounds, len: usize) -> Range n.checked_add(1).expect("end point of range too large"), std::ops::Bound::Excluded(&n) => n, }; - assert!(lower < len, "start point {lower} of range is >= length {len}"); + assert!( + lower < len, + "start point {lower} of range is >= length {len}" + ); assert!(upper <= len, "end point {upper} of range is > length {len}"); - assert!(lower <= upper, "start point {lower} is larger than end point {upper}"); + assert!( + lower <= upper, + "start point {lower} is larger than end point {upper}" + ); lower..upper }