Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
aiju committed Oct 29, 2024
1 parent 1bce70d commit ca7795a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
16 changes: 8 additions & 8 deletions stable_set/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
mod test_set;
10 changes: 7 additions & 3 deletions stable_set/src/stable_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V, S, W=u32> {
pub struct StableMap<K, V, S, W = u32> {
index_table: IndexTable<W>,
items: Vec<(K, V)>,
build_hasher: S,
Expand Down Expand Up @@ -64,7 +64,9 @@ impl<K, V, S, W: SmallIndex> StableMap<K, V, S, W> {
}
}

impl<K: std::fmt::Debug, V: std::fmt::Debug, S, W: SmallIndex> std::fmt::Debug for StableMap<K, V, S, W> {
impl<K: std::fmt::Debug, V: std::fmt::Debug, S, W: SmallIndex> std::fmt::Debug
for StableMap<K, V, S, W>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_map().entries(self.iter()).finish()
}
Expand Down Expand Up @@ -416,7 +418,9 @@ impl<'a, K, V, S, W: SmallIndex> IntoIterator for &'a mut StableMap<K, V, S, W>
}
}

impl<K: Hash + Eq, V, S: BuildHasher + Default, W: SmallIndex> FromIterator<(K, V)> for StableMap<K, V, S, W> {
impl<K: Hash + Eq, V, S: BuildHasher + Default, W: SmallIndex> FromIterator<(K, V)>
for StableMap<K, V, S, W>
{
fn from_iter<Iter: IntoIterator<Item = (K, V)>>(iter: Iter) -> Self {
let iter = iter.into_iter();
let (lower_bound, _) = iter.size_hint();
Expand Down
28 changes: 14 additions & 14 deletions stable_set/src/stable_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, S, W>`,
/// `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<T, S, W=u32> {
pub struct StableSet<T, S, W = u32> {
index_table: IndexTable<W>,
items: Vec<T>,
build_hasher: S,
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<T: Hash + Eq, S: BuildHasher, W: SmallIndex> StableSet<T, S, W> {
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<Q>(&mut self, value: &Q) -> bool
where
Expand All @@ -216,7 +216,7 @@ impl<T: Hash + Eq, S: BuildHasher, W: SmallIndex> StableSet<T, S, W> {
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<Q>(&mut self, value: &Q) -> Option<T>
where
Expand All @@ -226,7 +226,7 @@ impl<T: Hash + Eq, S: BuildHasher, W: SmallIndex> StableSet<T, S, W> {
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<Q>(&mut self, value: &Q) -> Option<(usize, T)>
where
Expand All @@ -247,7 +247,7 @@ impl<T: Hash + Eq, S: BuildHasher, W: SmallIndex> StableSet<T, S, W> {
}
}
/// 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<T> {
let hash = self.build_hasher.hash_one(self.items.get(index)?);
Expand All @@ -258,15 +258,15 @@ impl<T: Hash + Eq, S: BuildHasher, W: SmallIndex> StableSet<T, S, W> {
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<S2: BuildHasher>(&self, other: &StableSet<T, S2, W>) -> 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;
Expand Down Expand Up @@ -304,7 +304,7 @@ impl<T: Hash + Eq, S1: BuildHasher, S2: BuildHasher, W: SmallIndex> PartialEq<St
impl<T: Hash + Eq, S: BuildHasher, W: SmallIndex> Eq for StableSet<T, S, W> {}

/// An iterator that moves out of a set.
///
///
/// This struct is created by the `into_iter` method on [`StableSet`].
pub struct IntoIter<T> {
inner: std::vec::IntoIter<T>,
Expand Down Expand Up @@ -332,7 +332,7 @@ impl<'a, T, S, W> IntoIterator for &'a StableSet<T, S, W> {
}

/// 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>,
Expand All @@ -344,7 +344,7 @@ impl<'a, T> Iterator for Iter<'a, T> {

impl<T, S, W> StableSet<T, S, W> {
/// Returns an iterator over the set.
///
///
/// The iterator yields all items in order.
pub fn iter(&self) -> Iter<'_, T> {
Iter {
Expand Down Expand Up @@ -398,9 +398,9 @@ impl<T> Iterator for Drain<'_, T> {

impl<T, S, W: SmallIndex> StableSet<T, S, W> {
/// 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<usize>) -> Drain<'_, T> {
Expand Down
10 changes: 8 additions & 2 deletions stable_set/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ pub fn simplify_range(range: impl RangeBounds<usize>, len: usize) -> Range<usize
std::ops::Bound::Included(&n) => 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
}

Expand Down

0 comments on commit ca7795a

Please sign in to comment.