Struct compactmap::CompactMap [] [src]

pub struct CompactMap<V> { /* fields omitted */ }

A map that chooses small integer keys for you. You store something into this map and then access it by ID returned by it. For small V entries are expected to take 16 bytes.

Example:

use compactmap::CompactMap;

let mut mymap : CompactMap<String> = CompactMap::new();
let id_qwerty = mymap.insert("qwerty".to_string());
let id_qwertz = mymap.insert("qwertz".to_string());
assert_eq!(mymap[id_qwerty], "qwerty");
for (id, val) in mymap {
    println!("{}:{}", id, val);
}

Methods

impl<V> CompactMap<V>
[src]

[src]

Creates an empty CompactMap.

Examples

use compactmap::CompactMap;
let mut map: CompactMap<String> = CompactMap::new();

[src]

Creates an empty CompactMap with space for at least capacity elements before resizing.

Examples

use compactmap::CompactMap;
let mut map: CompactMap<String> = CompactMap::with_capacity(10);

[src]

Returns capacity of the underlying vector.

[src]

Reserves capacity for CompactMap's underlying vector. If you just cleared M elements from the map and want to insert N more elements, you'll probably need to reserve N-M elements.

[src]

Reserves capacity for CompactMap's underlying vector. If you just cleared M elements from the map and want to insert N more elements, you'll probably need to reserve N-M elements.

[src]

Clears the map, removing all key-value pairs.

Examples

use compactmap::CompactMap;

let mut a = CompactMap::new();
a.insert("a");
a.clear();
assert!(a.is_empty_slow());

[src]

Iterating the map to check if it is empty. O(n) where n is historical maximum element count.

[src]

Inserts a value into the map. The map generates and returns ID of the inserted element.

Examples

use compactmap::CompactMap;

let mut map = CompactMap::new();
assert_eq!(map.is_empty_slow(), true);
assert_eq!(map.insert(37), 0);
assert_eq!(map.is_empty_slow(), false);

assert_eq!(map.insert(37), 1);
assert_eq!(map.insert(37), 2);
assert_eq!(map.insert(44), 3);
assert_eq!(map.len_slow(), 4);

[src]

Removes a key from the map, returning the value at the key if the key was previously in the map. ``` use compactmap::CompactMap;

let mut map = CompactMap::new(); let id = map.insert("a"); assert_eq!(map.remove(id), Some("a")); assert_eq!(map.remove(123), None); ```

[src]

Returns a reference to the value corresponding to the key.

[src]

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

[src]

Returns an iterator visiting all key-value pairs in unspecified order. The iterator's element type is (usize, &'r V).

Examples

use compactmap::CompactMap;

let mut map = CompactMap::new();
map.insert("a");
map.insert("c");
map.insert("b");

// Print `1: a`, `2: b` and `3: c`.
for (key, value) in map.iter() {
    println!("{}: {}", key, value);
}

[src]

Returns an iterator visiting all key-value pairs in unspecified order, with mutable references to the values. The iterator's element type is (usize, &'r mut V)

[src]

Returns an iterator visiting all key-value pairs in unspecified order, the keys, consuming the original CompactMap. The iterator's element type is (usize, V).

[src]

Returns an iterator visiting all keys in some order. The iterator's element type is usize.

[src]

Returns an iterator visiting all values in ascending order of the keys. The iterator's element type is &'r V.

[src]

Returns an iterator visiting all values in ascending order of the keys. The iterator's element type is &'r mut V.

[src]

Iterates the map to get number of elements. O(n) where n is historical maximum element count.

[src]

Trims the CompactMap of any excess capacity.

Rescans the whole map to reindex empty slots. O(n).

The collection may reserve more space to avoid frequent reallocations.

Examples

use compactmap::CompactMap;
let mut map: CompactMap<&str> = CompactMap::with_capacity(10);
assert_eq!(map.capacity(), 10);
map.shrink_to_fit();
assert_eq!(map.capacity(), 0);
map.insert("qwe");
map.insert("345");
map.insert("555");
map.remove(1);
map.shrink_to_fit();
assert_eq!(map.capacity(), 2);

[src]

Returns an iterator visiting all key-value pairs in ascending order of the keys, emptying (but not consuming) the original CompactMap. The iterator's element type is (usize, V). Keeps the allocated memory for reuse.

Examples

use compactmap::CompactMap;

let mut map = CompactMap::new();
map.insert("a");
map.insert("b");
map.insert("c");
map.remove(1);

let vec: Vec<(usize, &str)> = map.drain().collect();

assert_eq!(vec, [(0, "a"), (2, "c")]);
assert!(map.is_empty_slow());
assert!(map.capacity() > 0);

Trait Implementations

impl<V: Clone> Clone for CompactMap<V>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<V> Default for CompactMap<V>
[src]

[src]

Returns the "default value" for a type. Read more

impl<V> Hash for CompactMap<V> where
    V: Hash
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl<V: PartialEq> PartialEq for CompactMap<V>
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

1.0.0
[src]

This method tests for !=.

impl<V: Eq> Eq for CompactMap<V>
[src]

impl<V> PartialOrd<CompactMap<V>> for CompactMap<V> where
    V: PartialOrd<V>, 
[src]

[src]

This method returns an ordering between self and other values if one exists. Read more

1.0.0
[src]

This method tests less than (for self and other) and is used by the < operator. Read more

1.0.0
[src]

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

1.0.0
[src]

This method tests greater than (for self and other) and is used by the > operator. Read more

1.0.0
[src]

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

impl<V> Ord for CompactMap<V> where
    V: Ord
[src]

[src]

This method returns an Ordering between self and other. Read more

1.22.0
[src]

Compares and returns the maximum of two values. Read more

1.22.0
[src]

Compares and returns the minimum of two values. Read more

impl<V> FromIterator<V> for CompactMap<V>
[src]

[src]

Creates a value from an iterator. Read more

impl<'a, V> FromIterator<&'a V> for CompactMap<V> where
    V: Copy
[src]

[src]

Creates a value from an iterator. Read more

impl<V> Extend<V> for CompactMap<V>
[src]

[src]

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

impl<'a, V> Extend<&'a V> for CompactMap<V> where
    V: Copy
[src]

[src]

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

impl<V> Index<usize> for CompactMap<V>
[src]

The returned type after indexing.

[src]

Performs the indexing (container[index]) operation.

impl<'a, V> Index<&'a usize> for CompactMap<V>
[src]

The returned type after indexing.

[src]

Performs the indexing (container[index]) operation.

impl<V> IndexMut<usize> for CompactMap<V>
[src]

[src]

Performs the mutable indexing (container[index]) operation.

impl<'a, V> IndexMut<&'a usize> for CompactMap<V>
[src]

[src]

Performs the mutable indexing (container[index]) operation.

impl<V: Debug> Debug for CompactMap<V>
[src]

[src]

Formats the value using the given formatter.

impl<'a, V> IntoIterator for &'a CompactMap<V>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<'a, V: 'a> IntoIterator for &'a mut CompactMap<V>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<V> IntoIterator for CompactMap<V>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more