pub struct PagedDirectMap<K: DirectKey, V, const PAGE: usize = DEFAULT_PAGE, D: Directory = Flat> { /* private fields */ }Expand description
A map from DirectKeys to values stored in place at each key’s slot
index.
PAGEis the number of slots per page; it must be non-zero. Each slot takessize_of::<Option<(K::Tag, V)>>()bytes whether or not it is occupied; see what a slot stores.Dis the page directory:Flat(default),Radixfor wide keys, orTree. Seecrate::page.
Entries are ordered by slot index. See the module docs for costs and for how methods treat a slot held by another generation of a key.
Implementations§
Source§impl<K: DirectKey, V, const PAGE: usize, D: Directory> PagedDirectMap<K, V, PAGE, D>
impl<K: DirectKey, V, const PAGE: usize, D: Directory> PagedDirectMap<K, V, PAGE, D>
Sourcepub fn allocated_pages(&self) -> usize
pub fn allocated_pages(&self) -> usize
Number of allocated pages.
Sourcepub fn contains_key(&self, key: K) -> bool
pub fn contains_key(&self, key: K) -> bool
Whether key is in the map.
Sourcepub fn get_mut(&mut self, key: K) -> Option<&mut V>
pub fn get_mut(&mut self, key: K) -> Option<&mut V>
Mutable access to the value for key, if it is in the map.
Sourcepub fn get_key_value(&self, key: K) -> Option<(K, &V)>
pub fn get_key_value(&self, key: K) -> Option<(K, &V)>
The stored key equal to key and its value, if it is in the map.
Sourcepub fn occupant(&self, index: usize) -> Option<(K, &V)>
pub fn occupant(&self, index: usize) -> Option<(K, &V)>
The entry occupying slot index, if any.
With generational keys this finds whichever generation is present.
Sourcepub fn occupant_mut(&mut self, index: usize) -> Option<(K, &mut V)>
pub fn occupant_mut(&mut self, index: usize) -> Option<(K, &mut V)>
The entry occupying slot index, with mutable access to its value.
Sourcepub fn insert(&mut self, key: K, value: V) -> Option<V>
pub fn insert(&mut self, key: K, value: V) -> Option<V>
Stores value for key and returns the value previously in key’s
slot.
If key is present its value is replaced and the stored key is kept.
If the slot is held by a different key (another generation), that
entry is evicted and its value returned; use
replace to get the evicted key too, or
try_insert to refuse.
§Panics
Panics if the page or the directory cannot be allocated. The entries are then unchanged, though the directory may have grown.
Sourcepub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, V>
pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, V>
Sourcepub fn get_or_insert_with(&mut self, key: K, make: impl FnOnce() -> V) -> &mut V
pub fn get_or_insert_with(&mut self, key: K, make: impl FnOnce() -> V) -> &mut V
Sourcepub fn remove(&mut self, key: K) -> Option<V>
pub fn remove(&mut self, key: K) -> Option<V>
Removes key and returns its value, if it was present.
Sourcepub fn remove_entry(&mut self, key: K) -> Option<(K, V)>
pub fn remove_entry(&mut self, key: K) -> Option<(K, V)>
Removes key and returns the stored key and its value, if it was
present.
Sourcepub fn remove_occupant(&mut self, index: usize) -> Option<(K, V)>
pub fn remove_occupant(&mut self, index: usize) -> Option<(K, V)>
Removes whichever entry occupies slot index and returns it.
Sourcepub fn retain(&mut self, keep: impl FnMut(&K, &mut V) -> bool)
pub fn retain(&mut self, keep: impl FnMut(&K, &mut V) -> bool)
Keeps only the entries for which keep returns true, visiting them
in slot-index order.
If keep or a value’s drop panics, the entries not yet visited are
kept and the map stays consistent.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes every entry, dropping the values in slot-index order. Pages
stay allocated for reuse; use reset to free them.
If a value’s drop panics, the entries not yet dropped stay in the
map.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Frees every page that holds no entry and shrinks the page directory.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V, PAGE, D> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, K, V, PAGE, D> ⓘ
Iterates over the entries in slot-index order, with mutable access to the values.
Sourcepub fn values(&self) -> Values<'_, K, V, PAGE, D> ⓘ
pub fn values(&self) -> Values<'_, K, V, PAGE, D> ⓘ
Iterates over the values in slot-index order.
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V, PAGE, D> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, PAGE, D> ⓘ
Iterates mutably over the values in slot-index order.
Sourcepub fn range(&self, keys: impl RangeBounds<K>) -> Range<'_, K, V, PAGE, D> ⓘ
pub fn range(&self, keys: impl RangeBounds<K>) -> Range<'_, K, V, PAGE, D> ⓘ
Iterates over the entries whose slot index lies between the slot indices of the bounds, in slot-index order.
The bounds are keys, compared by Key::index: for plain integer
keys this is the usual key range. A generational key bounds by its
index only, so range(id..=id) yields whichever generation holds
id’s slot. An empty or inverted range yields nothing.
See the module docs for the cost.
Sourcepub fn range_mut(
&mut self,
keys: impl RangeBounds<K>,
) -> RangeMut<'_, K, V, PAGE, D> ⓘ
pub fn range_mut( &mut self, keys: impl RangeBounds<K>, ) -> RangeMut<'_, K, V, PAGE, D> ⓘ
Like range, with mutable access to the values.
Sourcepub fn slot_range(
&self,
indices: impl RangeBounds<usize>,
) -> Range<'_, K, V, PAGE, D> ⓘ
pub fn slot_range( &self, indices: impl RangeBounds<usize>, ) -> Range<'_, K, V, PAGE, D> ⓘ
Iterates over the entries whose slot index is in indices, in
slot-index order. An empty or inverted range yields nothing.
Sourcepub fn slot_range_mut(
&mut self,
indices: impl RangeBounds<usize>,
) -> RangeMut<'_, K, V, PAGE, D> ⓘ
pub fn slot_range_mut( &mut self, indices: impl RangeBounds<usize>, ) -> RangeMut<'_, K, V, PAGE, D> ⓘ
Like slot_range, with mutable access to the
values.
Sourcepub fn next_occupant(&self, index: usize) -> Option<(K, &V)>
pub fn next_occupant(&self, index: usize) -> Option<(K, &V)>
The first entry at slot index or after it, in slot-index order.
For plain integer keys, the entry with the smallest key >= index.
Sourcepub fn prev_occupant(&self, index: usize) -> Option<(K, &V)>
pub fn prev_occupant(&self, index: usize) -> Option<(K, &V)>
The last entry at slot index or before it, in slot-index order.
For plain integer keys, the entry with the largest key <= index.
Sourcepub fn first_key_value(&self) -> Option<(K, &V)>
pub fn first_key_value(&self) -> Option<(K, &V)>
The entry with the smallest slot index.
Sourcepub fn last_key_value(&self) -> Option<(K, &V)>
pub fn last_key_value(&self) -> Option<(K, &V)>
The entry with the largest slot index.
Sourcepub fn pop_first(&mut self) -> Option<(K, V)>
pub fn pop_first(&mut self) -> Option<(K, V)>
Removes and returns the entry with the smallest slot index.
Sourcepub fn pop_last(&mut self) -> Option<(K, V)>
pub fn pop_last(&mut self) -> Option<(K, V)>
Removes and returns the entry with the largest slot index.
Sourcepub fn drain(&mut self) -> Drain<'_, K, V, PAGE, D> ⓘ
pub fn drain(&mut self) -> Drain<'_, K, V, PAGE, D> ⓘ
Removes every entry and returns them in slot-index order. Pages stay
allocated, as with clear.
Entries are removed as the iterator yields them; dropping it removes
the rest. If it is leaked instead (mem::forget), or a value’s drop
panics while the rest are removed, the entries not yet reached stay in
the map, which stays consistent.
use paged_collections::direct_map::PagedDirectMap;
let mut map: PagedDirectMap<u32, char> = [(5, 'b'), (1, 'a')].into_iter().collect();
assert_eq!(map.drain().collect::<Vec<_>>(), [(1, 'a'), (5, 'b')]);
assert!(map.is_empty());Sourcepub fn extract_if<F: FnMut(&K, &mut V) -> bool>(
&mut self,
pred: F,
) -> ExtractIf<'_, K, V, F, PAGE, D> ⓘ
pub fn extract_if<F: FnMut(&K, &mut V) -> bool>( &mut self, pred: F, ) -> ExtractIf<'_, K, V, F, PAGE, D> ⓘ
Removes the entries for which pred returns true and yields them,
visiting the entries in slot-index order.
Entries are tested and removed as the iterator advances: those it has
not reached when it is dropped stay in the map. Use
retain if the removed entries are not needed.
use paged_collections::direct_map::PagedDirectMap;
let mut map: PagedDirectMap<u32, u32> = (0..6).map(|k| (k, k * 10)).collect();
let odd: Vec<(u32, u32)> = map.extract_if(|k, _| k % 2 == 1).collect();
assert_eq!(odd, [(1, 10), (3, 30), (5, 50)]);
assert_eq!(map.len(), 3);Sourcepub fn get_disjoint_mut<const N: usize>(
&mut self,
keys: [K; N],
) -> [Option<&mut V>; N]
pub fn get_disjoint_mut<const N: usize>( &mut self, keys: [K; N], ) -> [Option<&mut V>; N]
The values of several keys at once, mutably: None for each key that
is not in the map. O(N log N) for N keys, plus stepping over the pages
between the smallest and largest key’s.
use paged_collections::direct_map::PagedDirectMap;
let mut map: PagedDirectMap<u32, u32> = [(1, 10), (9000, 20)].into_iter().collect();
if let [Some(a), None, Some(b)] = map.get_disjoint_mut([1, 2, 9000]) {
core::mem::swap(a, b);
}
assert_eq!((map[1], map[9000]), (20, 10));§Panics
Panics if a key in the map is given twice.
Sourcepub fn entry(&mut self, key: K) -> Entry<'_, K, V, PAGE, D>
pub fn entry(&mut self, key: K) -> Entry<'_, K, V, PAGE, D>
The entry for key, for in-place access or insertion.
The entry is Occupied only if key itself is in
the map. A slot held by a different key (another generation) gives a
Vacant entry, whose inserts evict that key as
insert does; VacantEntry::occupant shows it.
No page is allocated until a vacant entry is inserted into.
use paged_collections::direct_map::PagedDirectMap;
let mut counts: PagedDirectMap<u32, u32> = PagedDirectMap::new();
for word in [3, 1, 3, 3] {
*counts.entry(word).or_insert(0) += 1;
}
assert_eq!((counts[3], counts[1]), (3, 1));Sourcepub fn into_keys(self) -> IntoKeys<K, V, PAGE, D> ⓘ
pub fn into_keys(self) -> IntoKeys<K, V, PAGE, D> ⓘ
Consumes the map and returns its keys in slot-index order.
Sourcepub fn into_values(self) -> IntoValues<K, V, PAGE, D> ⓘ
pub fn into_values(self) -> IntoValues<K, V, PAGE, D> ⓘ
Consumes the map and returns its values in slot-index order.
Trait Implementations§
Source§impl<K: DirectKey, V: Clone, const PAGE: usize, D: Directory> Clone for PagedDirectMap<K, V, PAGE, D>
impl<K: DirectKey, V: Clone, const PAGE: usize, D: Directory> Clone for PagedDirectMap<K, V, PAGE, D>
Source§impl<K: DirectKey + Debug, V: Debug, const PAGE: usize, D: Directory> Debug for PagedDirectMap<K, V, PAGE, D>
impl<K: DirectKey + Debug, V: Debug, const PAGE: usize, D: Directory> Debug for PagedDirectMap<K, V, PAGE, D>
Source§impl<K: DirectKey, V, const PAGE: usize, D: Directory> Default for PagedDirectMap<K, V, PAGE, D>
impl<K: DirectKey, V, const PAGE: usize, D: Directory> Default for PagedDirectMap<K, V, PAGE, D>
Source§impl<'de, K, V, const PAGE: usize, D> Deserialize<'de> for PagedDirectMap<K, V, PAGE, D>
Available on crate feature serde only.Deserializes from a map. Entries are inserted in input order with
insert, so a later entry overwrites an
earlier one in the same slot.
impl<'de, K, V, const PAGE: usize, D> Deserialize<'de> for PagedDirectMap<K, V, PAGE, D>
serde only.Deserializes from a map. Entries are inserted in input order with
insert, so a later entry overwrites an
earlier one in the same slot.
Memory follows the key indices, not the input length: one key can
allocate a page, and with Flat a directory up to that page. For
untrusted input, use Radix or
Tree, or validate the keys first.
Source§fn deserialize<De: Deserializer<'de>>(
deserializer: De,
) -> Result<Self, De::Error>
fn deserialize<De: Deserializer<'de>>( deserializer: De, ) -> Result<Self, De::Error>
impl<K: DirectKey, V: Eq, const PAGE: usize, D: Directory> Eq for PagedDirectMap<K, V, PAGE, D>
Source§impl<'a, K: DirectKey + 'a, V: Copy + 'a, const PAGE: usize, D: Directory> Extend<(&'a K, &'a V)> for PagedDirectMap<K, V, PAGE, D>
impl<'a, K: DirectKey + 'a, V: Copy + 'a, const PAGE: usize, D: Directory> Extend<(&'a K, &'a V)> for PagedDirectMap<K, V, PAGE, D>
Source§fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K: DirectKey, V, const PAGE: usize, D: Directory> Extend<(K, V)> for PagedDirectMap<K, V, PAGE, D>
Inserts each entry with insert, so later
entries overwrite earlier ones in the same slot.
impl<K: DirectKey, V, const PAGE: usize, D: Directory> Extend<(K, V)> for PagedDirectMap<K, V, PAGE, D>
Inserts each entry with insert, so later
entries overwrite earlier ones in the same slot.
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: T)
fn extend_one(&mut self, item: T)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<K, V, PAGE, D>> for ConcurrentDirectMap<K, V, PAGE, D>
Available on crate feature concurrent only.
impl<K: DirectKey, V, const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<K, V, PAGE, D>> for ConcurrentDirectMap<K, V, PAGE, D>
concurrent only.Source§fn from(map: PagedDirectMap<K, V, PAGE, D>) -> Self
fn from(map: PagedDirectMap<K, V, PAGE, D>) -> Self
Source§impl<const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<usize, u64, PAGE, D>> for AtomicDirectMap<PAGE, D>
Available on crate feature concurrent only.
impl<const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<usize, u64, PAGE, D>> for AtomicDirectMap<PAGE, D>
concurrent only.Source§impl<K: DirectKey, V, const PAGE: usize, D: Directory> FromIterator<(K, V)> for PagedDirectMap<K, V, PAGE, D>
impl<K: DirectKey, V, const PAGE: usize, D: Directory> FromIterator<(K, V)> for PagedDirectMap<K, V, PAGE, D>
Source§impl<K: DirectKey + Hash, V: Hash, const PAGE: usize, D: Directory> Hash for PagedDirectMap<K, V, PAGE, D>
Hashes the length, then the entries in slot-index order, consistent with
PartialEq.
impl<K: DirectKey + Hash, V: Hash, const PAGE: usize, D: Directory> Hash for PagedDirectMap<K, V, PAGE, D>
Hashes the length, then the entries in slot-index order, consistent with
PartialEq.
Source§impl<K: DirectKey, V, const PAGE: usize, D: Directory> Index<K> for PagedDirectMap<K, V, PAGE, D>
map[key] returns the value for key.
impl<K: DirectKey, V, const PAGE: usize, D: Directory> Index<K> for PagedDirectMap<K, V, PAGE, D>
map[key] returns the value for key.
§Panics
Panics if key is not in the map.
Source§impl<'a, K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for &'a PagedDirectMap<K, V, PAGE, D>
impl<'a, K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for &'a PagedDirectMap<K, V, PAGE, D>
Source§impl<'a, K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for &'a mut PagedDirectMap<K, V, PAGE, D>
impl<'a, K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for &'a mut PagedDirectMap<K, V, PAGE, D>
Source§impl<K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for PagedDirectMap<K, V, PAGE, D>
impl<K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for PagedDirectMap<K, V, PAGE, D>
Source§impl<'a, K: DirectKey + Send, V: Sync, const PAGE: usize, D: Directory> IntoParallelIterator for &'a PagedDirectMap<K, V, PAGE, D>
Available on crate feature rayon only.
impl<'a, K: DirectKey + Send, V: Sync, const PAGE: usize, D: Directory> IntoParallelIterator for &'a PagedDirectMap<K, V, PAGE, D>
rayon only.Source§impl<'a, K: DirectKey + Send, V: Send, const PAGE: usize, D: Directory> IntoParallelIterator for &'a mut PagedDirectMap<K, V, PAGE, D>
Available on crate feature rayon only.
impl<'a, K: DirectKey + Send, V: Send, const PAGE: usize, D: Directory> IntoParallelIterator for &'a mut PagedDirectMap<K, V, PAGE, D>
rayon only.Source§impl<K: DirectKey, V: PartialEq<W>, W, const PAGE: usize, D: Directory, const P2: usize, D2: Directory> PartialEq<PagedDirectMap<K, W, P2, D2>> for PagedDirectMap<K, V, PAGE, D>
Compares entries in iter’s slot-index order,
across page sizes and directories.
impl<K: DirectKey, V: PartialEq<W>, W, const PAGE: usize, D: Directory, const P2: usize, D2: Directory> PartialEq<PagedDirectMap<K, W, P2, D2>> for PagedDirectMap<K, V, PAGE, D>
Compares entries in iter’s slot-index order,
across page sizes and directories.
Auto Trait Implementations§
impl<K, V, const PAGE: usize, D> Forget for PagedDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Freeze for PagedDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Move for PagedDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> RefUnwindSafe for PagedDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Send for PagedDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Sync for PagedDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> Unpin for PagedDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> UnsafeUnpin for PagedDirectMap<K, V, PAGE, D>
impl<K, V, const PAGE: usize, D> UnwindSafe for PagedDirectMap<K, V, PAGE, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<'data, I> IntoParallelRefIterator<'data> for I
impl<'data, I> IntoParallelRefIterator<'data> for I
Source§impl<'data, I> IntoParallelRefMutIterator<'data> for I
impl<'data, I> IntoParallelRefMutIterator<'data> for I
Source§type Iter = <&'data mut I as IntoParallelIterator>::Iter
type Iter = <&'data mut I as IntoParallelIterator>::Iter
Source§type Item = <&'data mut I as IntoParallelIterator>::Item
type Item = <&'data mut I as IntoParallelIterator>::Item
&'data mut T reference.Source§fn par_iter_mut(
&'data mut self,
) -> <I as IntoParallelRefMutIterator<'data>>::Iter
fn par_iter_mut( &'data mut self, ) -> <I as IntoParallelRefMutIterator<'data>>::Iter
self. Read more