Struct densemap::DenseMap

source ·
pub struct DenseMap<T> { /* private fields */ }
Expand description

A contiguous array with sparse index, written as DenseMap<T>, short for ‘dense map’.

Examples

use densemap::DenseMap;

let mut densemap = DenseMap::new();
let key = densemap.insert(0);
assert_eq!(densemap.get(key), Some(&0));

For more information see Crate documentation.

Implementations§

source§

impl<T> DenseMap<T>

source

pub const fn new() -> DenseMap<T>

Constructs a new, empty DenseMap<T>.

The dense map will not allocate until elements are inserted onto it.

Examples
use densemap::DenseMap;

let densemap: DenseMap<i32> = DenseMap::new();
assert_eq!(densemap.len(), 0);
source

pub fn with_capacity( sparse_capacity: usize, dense_capacity: usize ) -> DenseMap<T>

Constructs a new, empty DenseMap<T> with at least the specified capacity.

Panics

Panics if the new capacity exceeds isize::MAX bytes.

Examples
use densemap::DenseMap;

let densemap: DenseMap<i32> = DenseMap::with_capacity(10, 2);
assert_eq!(densemap.len(), 0);
let (sparse, dense) = densemap.capacity();
assert!(10 <= sparse && 2 <= dense);
source

pub fn capacity(&self) -> (usize, usize)

Returns the total number of elements the dense map can hold without reallocating.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::with_capacity(10, 2);
densemap.insert(42);
let (sparse, dense) = densemap.capacity();
assert!(10 <= sparse && 2 <= dense);
source

pub fn reserve(&mut self, sparse_additional: usize, dense_additional: usize)

Reserves capacity for at least additional more elements to be inserted in the DenseMap.

Panics

Panics if the new capacity exceeds isize::MAX.

Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.reserve(10, 2);
densemap.insert(1);
source

pub fn try_reserve( &mut self, sparse_additional: usize, dense_additional: usize ) -> Result<(), TryReserveError>

Tries to reserves capacity for at least additional more elements to be inserted in the DenseMap.

Panics

Panics if the new capacity exceeds isize::MAX.

Examples
use densemap::DenseMap;
let mut densemap = DenseMap::new();
densemap.try_reserve(10, 2).expect("can't reserve capacity");
densemap.insert(1);
source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the dense map as much as possible.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::with_capacity(100, 100);
densemap.insert(3);
densemap.insert(4);
let (sparse, dense) = densemap.capacity();
assert!(100 <= sparse && 100 <= dense);
densemap.shrink_to_fit();
let (sparse, dense) = densemap.capacity();
assert!(2 <= sparse && 2 <= dense);
source

pub fn shrink_to(&mut self, sparse_capacity: usize, dense_capacity: usize)

Shrinks the capacity of the map with a lower limit.

If the current capacity is less than the lower limit, this is a no-op.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::with_capacity(100, 100);
densemap.insert(3);
densemap.insert(4);
let (sparse, dense) = densemap.capacity();
assert!(100 <= sparse && 100 <= dense);
densemap.shrink_to(10, 10);
let (sparse, dense) = densemap.capacity();
assert!(10 <= sparse && 10 <= dense);
source

pub fn keys(&self) -> Keys<'_>

An iterator visiting all keys in arbitrary order. The iterator element type is &'a Key.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(1);
let keys = densemap.keys();
source

pub fn into_keys(self) -> IntoKeys

Creates a consuming iterator visiting all the keys in arbitrary order. The dense map cannot be used after calling this. The iterator element type is Key.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(1);
let keys = densemap.keys();
source

pub fn values(&self) -> Values<'_, T>

An iterator visiting all values in arbitrary order. The iterator element type is &'a T.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(1);
let values = densemap.values();
source

pub fn values_mut(&mut self) -> ValuesMut<'_, T>

An iterator visiting all values mutably in arbitrary order. The iterator element type is &'a mut T.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(1);
let values = densemap.values_mut();
source

pub fn into_values(self) -> IntoValues<T>

Creates a consuming iterator visiting all the values in arbitrary order. The dense map cannot be used after calling this. The iterator element type is T.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(1);
let values = densemap.into_values();
source

pub fn iter(&self) -> Iter<'_, T>

An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (&'a Key, &'a T).

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(12);
densemap.insert(34);
let mut iter = densemap.iter();
source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values. The iterator element type is (&'a Key, &'a mut T).

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(12);
densemap.insert(34);
let mut iter = densemap.iter();
source

pub fn len(&self) -> usize

Returns the number of elements in the dense map.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(3);
assert_eq!(densemap.len(), 1);
source

pub fn is_empty(&self) -> bool

Returns true if the dense map contains no elements.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
assert!(densemap.is_empty());

densemap.insert(1);
assert!(!densemap.is_empty());
source

pub fn drain(&mut self) -> Drain<'_, T>

Clears the dense map, removing all key-value pairs as an iterator. Keeps the allocated memory for reuse.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(3);
densemap.insert(4);
let iter = densemap.drain();
source

pub fn clear(&mut self)

Clears the dense map, removing all values and index.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
densemap.insert(3);
densemap.clear();
assert!(densemap.is_empty())
source

pub fn contain_key(&self, key: Key) -> bool

Returns true if the dense map contains a value for the specified key.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
assert!(densemap.is_empty());

let key = densemap.insert(1);
assert!(densemap.contain_key(key));
source

pub fn get(&self, key: Key) -> Option<&T>

Returns a reference to the value corresponding to the key.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
let key = densemap.insert(3);
assert_eq!(densemap.get(key), Some(&3));
source

pub fn get_key_value(&self, key: Key) -> Option<(&Key, &T)>

Returns a reference to the value corresponding to the key.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
let key = densemap.insert(3);
let (key, value) = densemap.get_key_value(key).unwrap();
source

pub fn get_mut(&mut self, key: Key) -> Option<&mut T>

Returns a mutable reference to the value corresponding to the key.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
let key = densemap.insert(3);

if let Some(value) = densemap.get_mut(key) {
    *value = 24;
}

assert_eq!(densemap.get(key), Some(&24));
source

pub fn insert(&mut self, value: T) -> Key

Inserts an element to the back of collection and returns key as stable identity.

Panics

Panics if the new capacity exceeds isize::MAX. Panics if a index or generation of element in the sparse layer exceeds u32::MAX.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
let key = densemap.insert(3);
assert_eq!(densemap.get(key), Some(&3));
source

pub fn insert_with_key<F>(&mut self, f: F) -> Keywhere F: FnOnce(Key) -> T,

Inserts a value given by f into the map. The key where the value will be stored is passed into f. This is useful to store value that contain their own key.

Panics

Panics if the new capacity exceeds isize::MAX. Panics if a index or generation of element in the sparse layer exceeds u32::MAX.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
let key = densemap.insert_with_key(|key| (key, 3));
assert_eq!(densemap.get(key), Some(&(key, 3)));
source

pub fn remove(&mut self, key: Key) -> Option<T>

Removes a key from the map, returning the value at the key if the key was previously in the map.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
let key = densemap.insert(3);
assert_eq!(densemap.remove(key), Some(3));
source

pub fn remove_entry(&mut self, key: Key) -> Option<(Key, T)>

Removes a key from the dense map, returning the stored key and value if the key was previously in the dense map.

Examples
use densemap::DenseMap;

let mut densemap = DenseMap::new();
let key = densemap.insert(3);
let (key, value) = densemap.remove_entry(key).unwrap();

Trait Implementations§

source§

impl<T: Clone> Clone for DenseMap<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for DenseMap<T>

source§

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

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

impl Default for DenseMap<()>

source§

fn default() -> DenseMap<()>

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

impl<T> Extend<T> for DenseMap<T>

source§

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

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

fn extend_one(&mut self, item: A)

🔬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<T, const N: usize> From<[T; N]> for DenseMap<T>

source§

fn from(value: [T; N]) -> DenseMap<T>

Converts to this type from the input type.
source§

impl<T> FromIterator<T> for DenseMap<T>

source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> DenseMap<T>

Creates a value from an iterator. Read more
source§

impl<T> Index<Key> for DenseMap<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, key: Key) -> &T

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

impl<T> IndexMut<Key> for DenseMap<T>

source§

fn index_mut(&mut self, key: Key) -> &mut T

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

impl<'a, T> IntoIterator for &'a DenseMap<T>

§

type Item = (&'a Key, &'a T)

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, T>

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

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
source§

impl<'a, T> IntoIterator for &'a mut DenseMap<T>

§

type Item = (&'a Key, &'a mut T)

The type of the elements being iterated over.
§

type IntoIter = IterMut<'a, T>

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

fn into_iter(self) -> IterMut<'a, T>

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for DenseMap<T>

§

type Item = (Key, T)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T>

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

fn into_iter(self) -> IntoIter<T>

Creates an iterator from a value. Read more
source§

impl<T: PartialEq> PartialEq<DenseMap<T>> for DenseMap<T>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Eq> Eq for DenseMap<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for DenseMap<T>where T: RefUnwindSafe,

§

impl<T> Send for DenseMap<T>where T: Send,

§

impl<T> Sync for DenseMap<T>where T: Sync,

§

impl<T> Unpin for DenseMap<T>where T: Unpin,

§

impl<T> UnwindSafe for DenseMap<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.