Skip to main content

PagedDirectMap

Struct PagedDirectMap 

Source
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.

  • PAGE is the number of slots per page; it must be non-zero. Each slot takes size_of::<Option<(K::Tag, V)>>() bytes whether or not it is occupied; see what a slot stores.
  • D is the page directory: Flat (default), Radix for wide keys, or Tree. See crate::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>

Source

pub const PAGE_SIZE: usize = PAGE

Number of slots per page.

Source

pub fn new() -> Self

Creates an empty map. No memory is allocated until the first insert.

Source

pub fn len(&self) -> usize

Number of entries in the map.

Source

pub fn is_empty(&self) -> bool

Whether the map has no entries.

Source

pub fn allocated_pages(&self) -> usize

Number of allocated pages.

Source

pub fn contains_key(&self, key: K) -> bool

Whether key is in the map.

Source

pub fn get(&self, key: K) -> Option<&V>

The value for key, if it is in the map.

Source

pub fn get_mut(&mut self, key: K) -> Option<&mut V>

Mutable access to the value for key, if it is in the map.

Source

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.

Source

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.

Source

pub fn occupant_mut(&mut self, index: usize) -> Option<(K, &mut V)>

The entry occupying slot index, with mutable access to its value.

Source

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.

Source

pub fn replace(&mut self, key: K, value: V) -> Option<(K, V)>

Stores value for key and returns the entry previously in key’s slot, whether it was key itself or another generation.

Unlike insert, this stores key even when the old key is equal to it.

§Panics

As insert.

Source

pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, V>

Adds an entry if key’s slot is vacant and returns its value, or returns value back and leaves the map unchanged if the slot is held (by key or by another key with the same index).

§Panics

As insert.

Source

pub fn get_or_insert_with(&mut self, key: K, make: impl FnOnce() -> V) -> &mut V

The value for key, inserting make() first if key is absent.

If the slot is held by a different key, that entry is evicted, as by insert. make runs before the map changes, so if it panics the entries are unchanged.

§Panics

As insert.

Source

pub fn remove(&mut self, key: K) -> Option<V>

Removes key and returns its value, if it was present.

Source

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.

Source

pub fn remove_occupant(&mut self, index: usize) -> Option<(K, V)>

Removes whichever entry occupies slot index and returns it.

Source

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.

Source

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.

Source

pub fn reset(&mut self)

Removes every entry and frees every page.

Source

pub fn shrink_to_fit(&mut self)

Frees every page that holds no entry and shrinks the page directory.

Source

pub fn iter(&self) -> Iter<'_, K, V, PAGE, D> ⓘ

Iterates over the entries in slot-index order.

Source

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.

Source

pub fn keys(&self) -> Keys<'_, K, V, PAGE, D> ⓘ

Iterates over the keys in slot-index order.

Source

pub fn values(&self) -> Values<'_, K, V, PAGE, D> ⓘ

Iterates over the values in slot-index order.

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, PAGE, D> ⓘ

Iterates mutably over the values in slot-index order.

Source

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.

Source

pub fn range_mut( &mut self, keys: impl RangeBounds<K>, ) -> RangeMut<'_, K, V, PAGE, D> ⓘ

Like range, with mutable access to the values.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn first_key_value(&self) -> Option<(K, &V)>

The entry with the smallest slot index.

Source

pub fn last_key_value(&self) -> Option<(K, &V)>

The entry with the largest slot index.

Source

pub fn pop_first(&mut self) -> Option<(K, V)>

Removes and returns the entry with the smallest slot index.

Source

pub fn pop_last(&mut self) -> Option<(K, V)>

Removes and returns the entry with the largest slot index.

Source

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());
Source

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);
Source

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.

Source

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));
Source

pub fn into_keys(self) -> IntoKeys<K, V, PAGE, D> ⓘ

Consumes the map and returns its keys in slot-index order.

Source

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>
where <D as Directory>::Store<Slot<K, V>, PAGE>: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: DirectKey + Debug, V: Debug, const PAGE: usize, D: Directory> Debug for PagedDirectMap<K, V, PAGE, D>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: DirectKey, V, const PAGE: usize, D: Directory> Default for PagedDirectMap<K, V, PAGE, D>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, K, V, const PAGE: usize, D> Deserialize<'de> for PagedDirectMap<K, V, PAGE, D>
where K: DirectKey + Deserialize<'de>, V: Deserialize<'de>, D: Directory,

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.

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>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K: DirectKey, V: Eq, const PAGE: usize, D: Directory> Eq for PagedDirectMap<K, V, PAGE, D>

Map PartialEq is an equivalence relation when the values implement Eq.

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>

Source§

fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
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.

Source§

fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
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.
Source§

fn from(map: PagedDirectMap<K, V, PAGE, D>) -> Self

Converts to this type from the input type.
Source§

impl<const PAGE: usize, D: SharedDirectory> From<PagedDirectMap<usize, u64, PAGE, D>> for AtomicDirectMap<PAGE, D>

Available on crate feature concurrent only.
Source§

fn from(map: PagedDirectMap<usize, u64, PAGE, D>) -> Self

Panics if a value is u64::MAX or a Flat index exceeds the spine range.

Source§

impl<K: DirectKey, V, const PAGE: usize, D: Directory> FromIterator<(K, V)> for PagedDirectMap<K, V, PAGE, D>

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
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.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
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.

§Panics

Panics if key is not in the map.

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, key: K) -> &V

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for &'a PagedDirectMap<K, V, PAGE, D>

Source§

type Item = (K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V, PAGE, D>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for &'a mut PagedDirectMap<K, V, PAGE, D>

Source§

type Item = (K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V, PAGE, D>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: DirectKey, V, const PAGE: usize, D: Directory> IntoIterator for PagedDirectMap<K, V, PAGE, D>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V, PAGE, D>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K: DirectKey + Send, V: Sync, const PAGE: usize, D: Directory> IntoParallelIterator for &'a PagedDirectMap<K, V, PAGE, D>
where K::Tag: Sync, <D as Directory>::Store<Slot<K, V>, PAGE>: Sync,

Available on crate feature rayon only.
Source§

type Item = (K, &'a V)

The type of item that the parallel iterator will produce.
Source§

type Iter = ParIter<'a, K, V, PAGE>

The parallel iterator type that will be created.
Source§

fn into_par_iter(self) -> Self::Iter

Converts self into a parallel iterator. Read more
Source§

impl<'a, K: DirectKey + Send, V: Send, const PAGE: usize, D: Directory> IntoParallelIterator for &'a mut PagedDirectMap<K, V, PAGE, D>
where K::Tag: Send,

Available on crate feature rayon only.
Source§

type Item = (K, &'a mut V)

The type of item that the parallel iterator will produce.
Source§

type Iter = ParIterMut<'a, K, V, PAGE>

The parallel iterator type that will be created.
Source§

fn into_par_iter(self) -> Self::Iter

Converts self into a parallel iterator. Read more
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.

Source§

fn eq(&self, other: &PagedDirectMap<K, W, P2, D2>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<K: DirectKey + Serialize, V: Serialize, const PAGE: usize, D: Directory> Serialize for PagedDirectMap<K, V, PAGE, D>

Available on crate feature serde only.

Serializes as a map from keys to values, in slot-index order.

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

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>
where <D as Directory>::Store<Option<(<K as DirectKey>::Tag, V)>, PAGE>: Freeze,

§

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>
where <D as Directory>::Store<Option<(<K as DirectKey>::Tag, V)>, PAGE>: RefUnwindSafe,

§

impl<K, V, const PAGE: usize, D> Send for PagedDirectMap<K, V, PAGE, D>
where <D as Directory>::Store<Option<(<K as DirectKey>::Tag, V)>, PAGE>: Send,

§

impl<K, V, const PAGE: usize, D> Sync for PagedDirectMap<K, V, PAGE, D>
where <D as Directory>::Store<Option<(<K as DirectKey>::Tag, V)>, PAGE>: Sync,

§

impl<K, V, const PAGE: usize, D> Unpin for PagedDirectMap<K, V, PAGE, D>
where <D as Directory>::Store<Option<(<K as DirectKey>::Tag, V)>, PAGE>: Unpin,

§

impl<K, V, const PAGE: usize, D> UnsafeUnpin for PagedDirectMap<K, V, PAGE, D>
where <D as Directory>::Store<Option<(<K as DirectKey>::Tag, V)>, PAGE>: UnsafeUnpin,

§

impl<K, V, const PAGE: usize, D> UnwindSafe for PagedDirectMap<K, V, PAGE, D>
where <D as Directory>::Store<Option<(<K as DirectKey>::Tag, V)>, PAGE>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<'data, I> IntoParallelRefIterator<'data> for I
where I: 'data + ?Sized, &'data I: IntoParallelIterator,

Source§

type Iter = <&'data I as IntoParallelIterator>::Iter

The type of the parallel iterator that will be returned.
Source§

type Item = <&'data I as IntoParallelIterator>::Item

The type of item that the parallel iterator will produce. This will typically be an &'data T reference type.
Source§

fn par_iter(&'data self) -> <I as IntoParallelRefIterator<'data>>::Iter

Converts self into a parallel iterator. Read more
Source§

impl<'data, I> IntoParallelRefMutIterator<'data> for I

Source§

type Iter = <&'data mut I as IntoParallelIterator>::Iter

The type of iterator that will be created.
Source§

type Item = <&'data mut I as IntoParallelIterator>::Item

The type of item that will be produced; this is typically an &'data mut T reference.
Source§

fn par_iter_mut( &'data mut self, ) -> <I as IntoParallelRefMutIterator<'data>>::Iter

Creates the parallel iterator from self. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.