Struct scapegoat::SGMap[][src]

pub struct SGMap<K: Ord, V> { /* fields omitted */ }
Expand description

Ordered map. A wrapper interface for SGTree. API examples and descriptions are all adapted or directly copied from the standard library’s BTreeMap.

Implementations

Makes a new, empty SGMap.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();

map.insert(1, "a");

#![no_std]: total capacity, e.g. maximum number of map pairs. Attempting to insert pairs beyond capacity will panic, unless the high_assurance feature is enabled.

If using std: fast capacity, e.g. number of map pairs stored on the stack. Pairs inserted beyond capacity will be stored on the heap.

Examples
use scapegoat::SGMap;

let mut map = SGMap::<usize, &str>::new();

assert!(map.capacity() > 0)

Moves all elements from other into self, leaving other empty.

Examples
use scapegoat::SGMap;

let mut a = SGMap::new();
a.insert(1, "a");
a.insert(2, "b");
a.insert(3, "c");

let mut b = SGMap::new();
b.insert(3, "d");
b.insert(4, "e");
b.insert(5, "f");

a.append(&mut b);

assert_eq!(a.len(), 5);
assert_eq!(b.len(), 0);

assert_eq!(a[&1], "a");
assert_eq!(a[&2], "b");
assert_eq!(a[&3], "d");
assert_eq!(a[&4], "e");
assert_eq!(a[&5], "f");

Insert a key-value pair into the map. If the map did not have this key present, None is returned. If the map did have this key present, the value is updated, the old value is returned, and the key is updated. This accommodates types that can be == without being identical.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);

map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");

Gets an iterator over the entries of the map, sorted by key.

Examples

Basic usage:

use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(3, "c");
map.insert(2, "b");
map.insert(1, "a");

for (key, value) in map.iter() {
    println!("{}: {}", key, value);
}

let (first_key, first_value) = map.iter().next().unwrap();
assert_eq!((*first_key, *first_value), (1, "a"));

Gets a mutable iterator over the entries of the map, sorted by key.

Examples

Basic usage:

use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

// Add 10 to the value if the key isn't "a"
for (key, value) in map.iter_mut() {
    if key != &"a" {
        *value += 10;
    }
}

let (second_key, second_value) = map.iter().skip(1).next().unwrap();
assert_eq!((*second_key, *second_value), ("b", 12));

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)> where
    K: Borrow<Q> + Ord,
    Q: Ord + ?Sized

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

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "a");
assert_eq!(map.remove_entry(&1), Some((1, "a")));
assert_eq!(map.remove_entry(&1), None);

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) such that f(&k, &mut v) returns false. The elements are visited in ascending key order.

Examples
use scapegoat::SGMap;

let mut map: SGMap<i32, i32> = (0..8).map(|x| (x, x*10)).collect();
// Keep only the elements with even-numbered keys.
map.retain(|&k, _| k % 2 == 0);
assert!(map.into_iter().eq(vec![(0, 0), (2, 20), (4, 40), (6, 60)]));

Splits the collection into two at the given key. Returns everything after the given key, including the key.

Examples

Basic usage:

use scapegoat::SGMap;

let mut a = SGMap::new();
a.insert(1, "a");
a.insert(2, "b");
a.insert(3, "c");
a.insert(17, "d");
a.insert(41, "e");

let b = a.split_off(&3);

assert_eq!(a.len(), 2);
assert_eq!(b.len(), 3);

assert_eq!(a[&1], "a");
assert_eq!(a[&2], "b");

assert_eq!(b[&3], "c");
assert_eq!(b[&17], "d");
assert_eq!(b[&41], "e");

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

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "a");
assert_eq!(map.remove(&1), Some("a"));
assert_eq!(map.remove(&1), None);

Returns the key-value pair corresponding to the supplied key.

The supplied key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_value(&1), Some((&1, &"a")));
assert_eq!(map.get_key_value(&2), None);

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "a");
assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), None);

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "a");
if let Some(x) = map.get_mut(&1) {
    *x = "b";
}
assert_eq!(map[&1], "b");

Clears the map, removing all elements.

Examples
use scapegoat::SGMap;

let mut a = SGMap::new();
a.insert(1, "a");
a.clear();
assert!(a.is_empty());

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

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "a");
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);

Returns true if the map contains no elements.

Examples
use scapegoat::SGMap;

let mut a = SGMap::new();
assert!(a.is_empty());
a.insert(1, "a");
assert!(!a.is_empty());

Returns a reference to the first key-value pair in the map. The key in this pair is the minimum key in the map.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
assert_eq!(map.first_key_value(), None);
map.insert(1, "b");
map.insert(2, "a");
assert_eq!(map.first_key_value(), Some((&1, &"b")));

Returns a reference to the first/minium key in the map, if any.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
assert_eq!(map.first_key_value(), None);
map.insert(1, "b");
map.insert(2, "a");
assert_eq!(map.first_key(), Some(&1));

Removes and returns the first element in the map. The key of this element is the minimum key that was in the map.

Examples

Draining elements in ascending order, while keeping a usable map each iteration.

use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "a");
map.insert(2, "b");
while let Some((key, _val)) = map.pop_first() {
    assert!((&map).into_iter().all(|(k, _v)| *k > key));
}
assert!(map.is_empty());

Returns a reference to the last key-value pair in the map. The key in this pair is the maximum key in the map.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "b");
map.insert(2, "a");
assert_eq!(map.last_key_value(), Some((&2, &"a")));

Returns a reference to the last/maximum key in the map, if any.

Examples
use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "b");
map.insert(2, "a");
assert_eq!(map.last_key(), Some(&2));

Removes and returns the last element in the map. The key of this element is the maximum key that was in the map.

Examples

Draining elements in descending order, while keeping a usable map each iteration.

use scapegoat::SGMap;

let mut map = SGMap::new();
map.insert(1, "a");
map.insert(2, "b");
while let Some((key, _val)) = map.pop_last() {
    assert!((&map).into_iter().all(|(k, _v)| *k < key));
}
assert!(map.is_empty());

Returns the number of elements in the map.

Examples
use scapegoat::SGMap;

let mut a = SGMap::new();
assert_eq!(a.len(), 0);
a.insert(1, "a");
assert_eq!(a.len(), 1);

Trait Implementations

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

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

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

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Creates a value from an iterator. Read more

The returned type after indexing.

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.