Struct hash_ord::ord_map::OrdMap[][src]

pub struct OrdMap<K, V> { /* fields omitted */ }

Optimized AVL.

To improve performance, raw pointer is used frequently. Because Rust uses a similar memory model to C/C++, two classic macros offset_of and container_of are used to dereference member variables into main struct. Fastbin is implemented to reduce the cost of memory allocation.

Examples

use hash_ord::ord_map::OrdMap;

// type inference lets us omit an explicit type signature (which
// would be `OrdMap<&str, &str>` in this example).
let mut book_reviews = OrdMap::new();

// review some books.
book_reviews.insert("Adventures of Huckleberry Finn",    "My favorite book.");
book_reviews.insert("Grimms' Fairy Tales",               "Masterpiece.");
book_reviews.insert("Pride and Prejudice",               "Very enjoyable.");
book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");

// check for a specific one.
if !book_reviews.contains_key("Les Misérables") {
    println!("We've got {} reviews, but Les Misérables ain't one.",
             book_reviews.len());
}

// oops, this review has a lot of spelling mistakes, let's delete it.
book_reviews.remove("The Adventures of Sherlock Holmes");

// look up the values associated with some keys.
let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
for book in &to_find {
    match book_reviews.get(book) {
        Some(review) => println!("{}: {}", book, review),
        None => println!("{} is unreviewed.", book)
    }
}

// iterate over everything.
for (book, review) in &book_reviews {
    println!("{}: \"{}\"", book, review);
}

The easiest way to use OrdMap with a custom type as key is to derive `Ord``.

use hash_ord::ord_map::OrdMap;
use std::cmp::Ordering;

#[derive(Eq, Debug)]
struct Viking {
   name: String,
   country: String,
}

impl Ord for Viking {
    fn cmp(&self, other: &Self) -> Ordering {
        let tmp = self.name.cmp(&other.name);
        return if tmp != Ordering::Equal { tmp } else {
            self.country.cmp(&other.country)
        };
    }
}

impl PartialOrd for Viking {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for Viking {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name && self.country == other.country
    }
}

impl Viking {
    /// / Create a new Viking.
    fn new(name: &str, country: &str) -> Viking {
        Viking { name: name.to_string(), country: country.to_string() }
    }
}

//  Use a OrdMap to store the vikings' health points.
let mut vikings = OrdMap::new();

vikings.insert(Viking::new("Einar", "Norway"), 25);
vikings.insert(Viking::new("Olaf", "Denmark"), 24);
vikings.insert(Viking::new("Harald", "Iceland"), 12);

// Use derived implementation to print the status of the vikings.
for (viking, health) in &vikings {
    println!("{:?} has {} hp", viking, health);
}

A OrdMap with fixed list of elements can be initialized from an array:

use hash_ord::ord_map::OrdMap;

let timber_resources: OrdMap<&str, i32> =
    [("Norway", 100),
     ("Denmark", 50),
     ("Iceland", 10)]
     .iter().cloned().collect();

Methods

impl<K, V> OrdMap<K, V>
[src]

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

Examples

use hash_ord::ord_map::OrdMap;

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

impl<K, V> OrdMap<K, V> where
    K: Ord
[src]

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

Examples

Basic usage:

use hash_ord::ord_map::OrdMap;

let mut a = OrdMap::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");

Important traits for Range<'a, K, V>

Constructs a double-ended iterator over a sub-range of elements in the map. The simplest way is to use the range syntax min..max, thus range(min..max) will yield elements from min (inclusive) to max (exclusive). The range may also be entered as (Bound<T>, Bound<T>), so for example range((Excluded(4), Included(10))) will yield a left-exclusive, right-inclusive range from 4 to 10.

Panics

Panics if range start > end. Panics if range start == end and both bounds are Excluded.

Examples

Basic usage:

use hash_ord::ord_map::OrdMap;
use std::collections::Bound::Included;

let mut map = OrdMap::new();
map.insert(3, "a");
map.insert(5, "b");
map.insert(8, "c");
for (&key, &value) in map.range((Included(&4), Included(&8))) {
    println!("{}: {}", key, value);
}
assert_eq!(Some((&5, &"b")), map.range(4..).next());

Important traits for RangeMut<'a, K, V>

Constructs a mutable double-ended iterator over a sub-range of elements in the map. The simplest way is to use the range syntax min..max, thus range(min..max) will yield elements from min (inclusive) to max (exclusive). The range may also be entered as (Bound<T>, Bound<T>), so for example range((Excluded(4), Included(10))) will yield a left-exclusive, right-inclusive range from 4 to 10.

Panics

Panics if range start > end. Panics if range start == end and both bounds are Excluded.

Examples

Basic usage:

use hash_ord::ord_map::OrdMap;

let mut map: OrdMap<&str, i32> = ["Alice", "Bob", "Carol", "Cheryl"].iter()
                                                                      .map(|&s| (s, 0))
                                                                      .collect();
for (_, balance) in map.range_mut("B".."Cheryl") {
    *balance += 100;
}
for (name, balance) in &map {
    println!("{} => {}", name, balance);
}

Moves all elements from other into Self, leaving other empty. O(n) time complexity

Examples

use hash_ord::ord_map::OrdMap;

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

let mut b = OrdMap::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");

Gets the given key's corresponding entry in the map for in-place manipulation.

Examples

use hash_ord::ord_map::OrdMap;

let mut letters = OrdMap::new();

for ch in "a short treatise on fungi".chars() {
    let counter = letters.entry(ch).or_insert(0);
    *counter += 1;
}

assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);

Returns the cursors of a found pos.

Returns the max height of the tree.

Returns true if the map contains no element.

Examples

use hash_ord::ord_map::OrdMap;

let mut map = OrdMap::new();
assert!(map.is_empty());
map.insert(1, 1);
assert!(!map.is_empty());

Returns the number of elements in the map.

Examples

use hash_ord::ord_map::OrdMap;

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

Creates an empty OrdMap.

The hash map is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

Examples

use hash_ord::ord_map::OrdMap;
let mut map: OrdMap<&str, isize> = OrdMap::new();

Return true if two tree are isomorphic.

Return true if tree is balanced.

Return true if tree is a BST.

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 Ord on the borrowed form must match those for the key type.

Examples

use hash_ord::ord_map::OrdMap;

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

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 Ord on the borrowed form must match those for the key type.

Examples

use hash_ord::ord_map::OrdMap;

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

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but Ord on the borrowed form must match those for the key type.

Examples

use hash_ord::ord_map::OrdMap;

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

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

The key may be any borrowed form of the map's key type, but Ord on the borrowed form must match those for the key type.

Examples

use hash_ord::ord_map::OrdMap;

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

Inserts 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, update map with new (key, value) and return the old one.

Examples

use hash_ord::ord_map::OrdMap;

let mut map = OrdMap::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((37, "b")));
assert_eq!(map[&37], "c");

Important traits for Keys<'a, K, V>

An iterator visiting all keys in incremental order. The iterator element type is &'a K.

Examples

use hash_ord::ord_map::OrdMap;

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

for key in map.keys() {
    println!("{}", key);
}

Important traits for Values<'a, K, V>

An iterator visiting all values in incremental order. The iterator element type is &'a V.

Examples

use hash_ord::ord_map::OrdMap;

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

for val in map.values() {
    println!("{}", val);
}

Important traits for ValuesMut<'a, K, V>

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

Examples

use hash_ord::ord_map::OrdMap;

let mut map = OrdMap::new();

map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);

for val in map.values_mut() {
    *val = *val + 10;
}

for val in map.values() {
    println!("{}", val);
}

Important traits for Iter<'a, K, V>

An iterator visiting all key-value pairs in incremental order. The iterator element type is (&'a K, &'a V).

Examples

use hash_ord::ord_map::OrdMap;

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

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

Important traits for IterMut<'a, K, V>

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

Examples

use hash_ord::ord_map::OrdMap;

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

// Update all values
for (_, val) in map.iter_mut() {
    *val *= 2;
}

for (key, val) in &map {
    println!("key: {} val: {}", key, val);
}

Trait Implementations

impl<K, V> Drop for OrdMap<K, V>
[src]

Executes the destructor for this type. Read more

impl<K, V> Clone for OrdMap<K, V> where
    K: Ord + Clone,
    V: Clone
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<K, V> PartialEq for OrdMap<K, V> where
    K: Eq + Ord,
    V: PartialEq
[src]

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

This method tests for !=.

impl<K, V> Eq for OrdMap<K, V> where
    K: Eq + Ord,
    V: Eq
[src]

impl<'a, K, V> Index<&'a K> for OrdMap<K, V> where
    K: Ord
[src]

The returned type after indexing.

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

Panics

Panics if the key is not present in the OrdMap.

impl<K, V> FromIterator<(K, V)> for OrdMap<K, V> where
    K: Ord
[src]

Creates a value from an iterator. Read more

impl<K, V> Extend<(K, V)> for OrdMap<K, V> where
    K: Ord
[src]

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

impl<K, V> IntoIterator for OrdMap<K, V> where
    K: Ord
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Important traits for IntoIter<K, V>

Creates an iterator from a value. Read more

impl<'a, K, V> IntoIterator for &'a OrdMap<K, V> where
    K: Ord
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Important traits for Iter<'a, K, V>

Creates an iterator from a value. Read more

Auto Trait Implementations

impl<K, V> !Send for OrdMap<K, V>

impl<K, V> !Sync for OrdMap<K, V>