Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add into_dense() and get_dense_index() #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
100 changes: 95 additions & 5 deletions src/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ impl<K: Key, V> DenseSlotMap<K, V> {
/// ```
#[inline(always)]
pub fn insert(&mut self, value: V) -> K {
unsafe { self.try_insert_with_key::<_, Never>(move |_| Ok(value)).unwrap_unchecked_() }
unsafe {
self.try_insert_with_key::<_, Never>(move |_| Ok(value))
.unwrap_unchecked_()
}
}

/// Inserts a value given by `f` into the slot map. The key where the
Expand All @@ -277,7 +280,10 @@ impl<K: Key, V> DenseSlotMap<K, V> {
where
F: FnOnce(K) -> V,
{
unsafe { self.try_insert_with_key::<_, Never>(move |k| Ok(f(k))).unwrap_unchecked_() }
unsafe {
self.try_insert_with_key::<_, Never>(move |k| Ok(f(k)))
.unwrap_unchecked_()
}
}

/// Inserts a value given by `f` into the slot map. The key where the
Expand Down Expand Up @@ -515,7 +521,10 @@ impl<K: Key, V> DenseSlotMap<K, V> {
/// ```
pub unsafe fn get_unchecked(&self, key: K) -> &V {
debug_assert!(self.contains_key(key));
let idx = self.slots.get_unchecked(key.data().idx as usize).idx_or_free;
let idx = self
.slots
.get_unchecked(key.data().idx as usize)
.idx_or_free;
&self.values.get_unchecked(idx as usize)
}

Expand Down Expand Up @@ -565,7 +574,10 @@ impl<K: Key, V> DenseSlotMap<K, V> {
/// ```
pub unsafe fn get_unchecked_mut(&mut self, key: K) -> &mut V {
debug_assert!(self.contains_key(key));
let idx = self.slots.get_unchecked(key.data().idx as usize).idx_or_free;
let idx = self
.slots
.get_unchecked(key.data().idx as usize)
.idx_or_free;
self.values.get_unchecked_mut(idx as usize)
}

Expand Down Expand Up @@ -785,6 +797,81 @@ impl<K: Key, V> DenseSlotMap<K, V> {
inner: self.iter_mut(),
}
}

/// Returns the index that can be used to access the value associated to
/// this key in the [`Vec`] returned by [`into_dense()`]. This index is
/// only valid if no mutations occur to the slotmap between the index
/// being retrieved and the slotmap being converted to its dense form.
///
/// # Examples
///
/// ```
/// # use slotmap::*;
/// let mut sm = DenseSlotMap::new();
/// let keys = [
/// sm.insert(1),
/// sm.insert(2),
/// sm.insert(3),
/// ];
/// sm.values_mut().for_each(|n| { *n *= 3 });
/// let indices: Vec<_> =
/// keys
/// .into_iter()
/// .map(|key| sm.get_dense_index(*key).unwrap())
/// .collect();
/// let dense = sm.into_dense();
/// assert_eq!(
/// [3, 6, 9],
/// [
/// dense[indices[0]],
/// dense[indices[1]],
/// dense[indices[2]],
/// ]
/// );
/// ```
pub fn get_dense_index(&self, key: K) -> Option<usize> {
let kd = key.data();
self.slots
.get(kd.idx as usize)
.filter(|slot| slot.version == kd.version.get())
.map(|slot| slot.idx_or_free as usize)
}

/// Returns a dense [`Vec`] of the values in this [`DenseSlotMap`],
/// in an arbitrary order. Indices into this [`Vec`] can be
/// retrieved by calling [`get_dense_index()`] *without mutating
/// the slotmap between the index retrieval and the conversion to
/// dense [`Vec`]*. Mutating the slotmap before conversion
/// invalidates all retrieved indices.
///
/// # Example
///
/// ```
/// # use slotmap::*;
/// let mut sm = DenseSlotMap::new();
/// let keys = [
/// sm.insert(4),
/// sm.insert(2),
/// sm.insert(5),
/// ];
/// let indices: Vec<_> =
/// keys
/// .into_iter()
/// .map(|key| sm.get_dense_index(*key).unwrap())
/// .collect();
/// let dense = sm.into_dense();
/// assert_eq!(
/// [4, 2, 5],
/// [
/// dense[indices[0]],
/// dense[indices[1]],
/// dense[indices[2]],
/// ]
/// );
/// ```
pub fn into_dense(self) -> Vec<V> {
self.values
}
}

impl<K: Key, V> Clone for DenseSlotMap<K, V>
Expand Down Expand Up @@ -1132,7 +1219,10 @@ mod serialize {
}

// Ensure the first slot exists and is empty for the sentinel.
if serde_slots.get(0).map_or(true, |slot| slot.version % 2 == 1) {
if serde_slots
.get(0)
.map_or(true, |slot| slot.version % 2 == 1)
{
return Err(de::Error::custom(&"first slot not empty"));
}

Expand Down