Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Version 1.1.0

- Bumped MSRV to 1.58.
- Fixed memory leak in `clone_from`.
- Added `keys_as_slice`, `values_as_slice`, and `values_as_mut_slice` to
`DenseSlotMap`.

Version 1.0.7
=============
Expand Down
20 changes: 10 additions & 10 deletions src/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ impl<K: Key, V> SlotMap<K, V> {
self.drain();
}

/// Clears the slot map, returning all key-value pairs in arbitrary order as
/// an iterator. Keeps the allocated memory for reuse.
/// Clears the slot map, returning all key-value pairs in an arbitrary order
/// as an iterator. Keeps the allocated memory for reuse.
///
/// When the iterator is dropped all elements in the slot map are removed,
/// even if the iterator was not fully consumed. If the iterator is not
Expand Down Expand Up @@ -767,7 +767,7 @@ impl<K: Key, V> SlotMap<K, V> {
core::mem::transmute_copy::<_, [&mut V; N]>(&ptrs)
}

/// An iterator visiting all key-value pairs in arbitrary order. The
/// An iterator visiting all key-value pairs in an arbitrary order. The
/// iterator element type is `(K, &'a V)`.
///
/// This function must iterate over all slots, empty or not. In the face of
Expand Down Expand Up @@ -796,7 +796,7 @@ impl<K: Key, V> SlotMap<K, V> {
}
}

/// An iterator visiting all key-value pairs in arbitrary order, with
/// An iterator visiting all key-value pairs in an arbitrary order, with
/// mutable references to the values. The iterator element type is
/// `(K, &'a mut V)`.
///
Expand Down Expand Up @@ -833,8 +833,8 @@ impl<K: Key, V> SlotMap<K, V> {
}
}

/// An iterator visiting all keys in arbitrary order. The iterator element
/// type is `K`.
/// An iterator visiting all keys in an arbitrary order. The iterator
/// element type is `K`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand All @@ -856,8 +856,8 @@ impl<K: Key, V> SlotMap<K, V> {
Keys { inner: self.iter() }
}

/// An iterator visiting all values in arbitrary order. The iterator element
/// type is `&'a V`.
/// An iterator visiting all values in an arbitrary order. The iterator
/// element type is `&'a V`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand All @@ -879,8 +879,8 @@ impl<K: Key, V> SlotMap<K, V> {
Values { inner: self.iter() }
}

/// An iterator visiting all values mutably in arbitrary order. The iterator
/// element type is `&'a mut V`.
/// An iterator visiting all values mutably in an arbitrary order. The
/// iterator element type is `&'a mut V`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand Down
84 changes: 75 additions & 9 deletions src/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl<K: Key, V> DenseSlotMap<K, V> {
self.drain();
}

/// Clears the slot map, returning all key-value pairs in arbitrary order
/// Clears the slot map, returning all key-value pairs in an arbitrary order
/// as an iterator. Keeps the allocated memory for reuse.
///
/// When the iterator is dropped all elements in the slot map are removed,
Expand Down Expand Up @@ -679,7 +679,7 @@ impl<K: Key, V> DenseSlotMap<K, V> {
core::mem::transmute_copy::<_, [&mut V; N]>(&ptrs)
}

/// An iterator visiting all key-value pairs in arbitrary order. The
/// An iterator visiting all key-value pairs in an arbitrary order. The
/// iterator element type is `(K, &'a V)`.
///
/// # Examples
Expand All @@ -703,7 +703,7 @@ impl<K: Key, V> DenseSlotMap<K, V> {
}
}

/// An iterator visiting all key-value pairs in arbitrary order, with
/// An iterator visiting all key-value pairs in an arbitrary order, with
/// mutable references to the values. The iterator element type is
/// `(K, &'a mut V)`.
///
Expand Down Expand Up @@ -733,8 +733,8 @@ impl<K: Key, V> DenseSlotMap<K, V> {
}
}

/// An iterator visiting all keys in arbitrary order. The iterator element
/// type is K.
/// An iterator visiting all keys in an arbitrary order. The iterator
/// element type is K.
///
/// # Examples
///
Expand All @@ -753,8 +753,8 @@ impl<K: Key, V> DenseSlotMap<K, V> {
Keys { inner: self.iter() }
}

/// An iterator visiting all values in arbitrary order. The iterator element
/// type is `&'a V`.
/// An iterator visiting all values in an arbitrary order. The iterator
/// element type is `&'a V`.
///
/// # Examples
///
Expand All @@ -773,8 +773,8 @@ impl<K: Key, V> DenseSlotMap<K, V> {
Values { inner: self.iter() }
}

/// An iterator visiting all values mutably in arbitrary order. The iterator
/// element type is `&'a mut V`.
/// An iterator visiting all values mutably in an arbitrary order. The
/// iterator element type is `&'a mut V`.
///
/// # Examples
///
Expand All @@ -795,6 +795,72 @@ impl<K: Key, V> DenseSlotMap<K, V> {
inner: self.iter_mut(),
}
}

/// Returns a slice containing all keys in the slot map in an arbitrary
/// order (but in the same order as [`values_as_slice`]).
///
/// # Examples
///
/// ```
/// # use slotmap::*;
/// let mut sm = DenseSlotMap::new();
/// let k1 = sm.insert(1);
/// let k2 = sm.insert(2);
/// sm.remove(k1);
/// sm.insert(3);
/// let key_slice = sm.keys_as_slice();
/// // The order is arbitrary, but we can check if the keys are present.
/// assert!(key_slice.contains(&k2));
/// assert_eq!(key_slice.len(), 2);
/// // The keys correspond to the values.
/// assert_eq!(sm.values_as_slice()[0], sm[key_slice[0]]);
/// ```
pub fn keys_as_slice(&self) -> &[K] {
self.keys.as_slice()
}

/// Returns a slice containing all values in the slot map in an arbitrary
/// order (but in the same order as [`keys_as_slice`]).
///
/// # Examples
///
/// ```
/// # use slotmap::*;
/// # use std::collections::HashSet;
/// let mut sm = DenseSlotMap::new();
/// sm.insert(10);
/// sm.insert(20);
/// sm.insert(30);
/// let value_slice = sm.values_as_slice();
/// let values: HashSet<_> = value_slice.iter().collect();
/// let check: HashSet<_> = vec![&10, &20, &30].into_iter().collect();
/// assert_eq!(values, check);
/// // The keys correspond to the values.
/// assert_eq!(value_slice[0], sm[sm.keys_as_slice()[0]]);
/// ```
pub fn values_as_slice(&self) -> &[V] {
self.values.as_slice()
}

/// Returns a mutable slice containing all values in the slot map in
/// an arbitrary order (but in the same order as [`keys_as_slice`]).
///
/// # Examples
///
/// ```
/// # use slotmap::*;
/// # use std::collections::HashSet;
/// let mut sm = DenseSlotMap::new();
/// sm.insert(1);
/// sm.insert(2);
/// sm.insert(3);
/// let slice = sm.values_as_mut_slice();
/// slice.iter_mut().for_each(|n| { *n *= 3 });
/// assert_eq!(slice, &[3, 6, 9]);
/// ```
pub fn values_as_mut_slice(&mut self) -> &mut [V] {
self.values.as_mut_slice()
}
}

impl<K: Key, V> Clone for DenseSlotMap<K, V>
Expand Down
20 changes: 10 additions & 10 deletions src/hop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ impl<K: Key, V> HopSlotMap<K, V> {
self.drain();
}

/// Clears the slot map, returning all key-value pairs in arbitrary order as
/// an iterator. Keeps the allocated memory for reuse.
/// Clears the slot map, returning all key-value pairs in an arbitrary order
/// as an iterator. Keeps the allocated memory for reuse.
///
/// When the iterator is dropped all elements in the slot map are removed,
/// even if the iterator was not fully consumed. If the iterator is not
Expand Down Expand Up @@ -872,7 +872,7 @@ impl<K: Key, V> HopSlotMap<K, V> {
core::mem::transmute_copy::<_, [&mut V; N]>(&ptrs)
}

/// An iterator visiting all key-value pairs in arbitrary order. The
/// An iterator visiting all key-value pairs in an arbitrary order. The
/// iterator element type is `(K, &'a V)`.
///
/// # Examples
Expand All @@ -897,7 +897,7 @@ impl<K: Key, V> HopSlotMap<K, V> {
}
}

/// An iterator visiting all key-value pairs in arbitrary order, with
/// An iterator visiting all key-value pairs in an arbitrary order, with
/// mutable references to the values. The iterator element type is
/// `(K, &'a mut V)`.
///
Expand Down Expand Up @@ -929,8 +929,8 @@ impl<K: Key, V> HopSlotMap<K, V> {
}
}

/// An iterator visiting all keys in arbitrary order. The iterator element
/// type is `K`.
/// An iterator visiting all keys in an arbitrary order. The iterator
/// element type is `K`.
///
/// # Examples
///
Expand All @@ -949,8 +949,8 @@ impl<K: Key, V> HopSlotMap<K, V> {
Keys { inner: self.iter() }
}

/// An iterator visiting all values in arbitrary order. The iterator element
/// type is `&'a V`.
/// An iterator visiting all values in an arbitrary order. The iterator
/// element type is `&'a V`.
///
/// # Examples
///
Expand All @@ -969,8 +969,8 @@ impl<K: Key, V> HopSlotMap<K, V> {
Values { inner: self.iter() }
}

/// An iterator visiting all values mutably in arbitrary order. The iterator
/// element type is `&'a mut V`.
/// An iterator visiting all values mutably in an arbitrary order. The
/// iterator element type is `&'a mut V`.
///
/// # Examples
///
Expand Down
20 changes: 10 additions & 10 deletions src/secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ impl<K: Key, V> SecondaryMap<K, V> {
self.drain();
}

/// Clears the slot map, returning all key-value pairs in arbitrary order as
/// an iterator. Keeps the allocated memory for reuse.
/// Clears the slot map, returning all key-value pairs in an arbitrary order
/// as an iterator. Keeps the allocated memory for reuse.
///
/// When the iterator is dropped all elements in the slot map are removed,
/// even if the iterator was not fully consumed. If the iterator is not
Expand Down Expand Up @@ -666,7 +666,7 @@ impl<K: Key, V> SecondaryMap<K, V> {
core::mem::transmute_copy::<_, [&mut V; N]>(&ptrs)
}

/// An iterator visiting all key-value pairs in arbitrary order. The
/// An iterator visiting all key-value pairs in an arbitrary order. The
/// iterator element type is `(K, &'a V)`.
///
/// This function must iterate over all slots, empty or not. In the face of
Expand Down Expand Up @@ -694,7 +694,7 @@ impl<K: Key, V> SecondaryMap<K, V> {
}
}

/// An iterator visiting all key-value pairs in arbitrary order, with
/// An iterator visiting all key-value pairs in an arbitrary order, with
/// mutable references to the values. The iterator element type is
/// `(K, &'a mut V)`.
///
Expand Down Expand Up @@ -729,8 +729,8 @@ impl<K: Key, V> SecondaryMap<K, V> {
}
}

/// An iterator visiting all keys in arbitrary order. The iterator element
/// type is `K`.
/// An iterator visiting all keys in an arbitrary order. The iterator
/// element type is `K`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand All @@ -753,8 +753,8 @@ impl<K: Key, V> SecondaryMap<K, V> {
Keys { inner: self.iter() }
}

/// An iterator visiting all values in arbitrary order. The iterator element
/// type is `&'a V`.
/// An iterator visiting all values in an arbitrary order. The iterator
/// element type is `&'a V`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand All @@ -777,8 +777,8 @@ impl<K: Key, V> SecondaryMap<K, V> {
Values { inner: self.iter() }
}

/// An iterator visiting all values mutably in arbitrary order. The iterator
/// element type is `&'a mut V`.
/// An iterator visiting all values mutably in an arbitrary order. The
/// iterator element type is `&'a mut V`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand Down
20 changes: 10 additions & 10 deletions src/sparse_secondary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ impl<K: Key, V, S: hash::BuildHasher> SparseSecondaryMap<K, V, S> {
self.slots.clear();
}

/// Clears the slot map, returning all key-value pairs in arbitrary order as
/// an iterator. Keeps the allocated memory for reuse.
/// Clears the slot map, returning all key-value pairs in an arbitrary order
/// as an iterator. Keeps the allocated memory for reuse.
///
/// When the iterator is dropped all elements in the slot map are removed,
/// even if the iterator was not fully consumed. If the iterator is not
Expand Down Expand Up @@ -625,7 +625,7 @@ impl<K: Key, V, S: hash::BuildHasher> SparseSecondaryMap<K, V, S> {
core::mem::transmute_copy::<_, [&mut V; N]>(&ptrs)
}

/// An iterator visiting all key-value pairs in arbitrary order. The
/// An iterator visiting all key-value pairs in an arbitrary order. The
/// iterator element type is `(K, &'a V)`.
///
/// This function must iterate over all slots, empty or not. In the face of
Expand All @@ -652,7 +652,7 @@ impl<K: Key, V, S: hash::BuildHasher> SparseSecondaryMap<K, V, S> {
}
}

/// An iterator visiting all key-value pairs in arbitrary order, with
/// An iterator visiting all key-value pairs in an arbitrary order, with
/// mutable references to the values. The iterator element type is
/// `(K, &'a mut V)`.
///
Expand Down Expand Up @@ -686,8 +686,8 @@ impl<K: Key, V, S: hash::BuildHasher> SparseSecondaryMap<K, V, S> {
}
}

/// An iterator visiting all keys in arbitrary order. The iterator element
/// type is `K`.
/// An iterator visiting all keys in an arbitrary order. The iterator
/// element type is `K`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand All @@ -710,8 +710,8 @@ impl<K: Key, V, S: hash::BuildHasher> SparseSecondaryMap<K, V, S> {
Keys { inner: self.iter() }
}

/// An iterator visiting all values in arbitrary order. The iterator element
/// type is `&'a V`.
/// An iterator visiting all values in an arbitrary order. The iterator
/// element type is `&'a V`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand All @@ -734,8 +734,8 @@ impl<K: Key, V, S: hash::BuildHasher> SparseSecondaryMap<K, V, S> {
Values { inner: self.iter() }
}

/// An iterator visiting all values mutably in arbitrary order. The iterator
/// element type is `&'a mut V`.
/// An iterator visiting all values mutably in an arbitrary order. The
/// iterator element type is `&'a mut V`.
///
/// This function must iterate over all slots, empty or not. In the face of
/// many deleted elements it can be inefficient.
Expand Down