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]
impl<K, V> OrdMap<K, V>
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
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]
impl<K, V> OrdMap<K, V> where
K: Ord,
pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self where
K: Borrow<Q>,
[src]
pub fn split_off<Q: ?Sized + Ord>(&mut self, key: &Q) -> Self where
K: Borrow<Q>,
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>pub fn range<T: ?Sized, R>(&self, range: R) -> Range<K, V> where
T: Ord,
K: Borrow<T>,
R: RangeBounds<T>,
[src]
pub fn range<T: ?Sized, R>(&self, range: R) -> Range<K, V> where
T: Ord,
K: Borrow<T>,
R: RangeBounds<T>,
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>pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<K, V> where
T: Ord,
K: Borrow<T>,
R: RangeBounds<T>,
[src]
pub fn range_mut<T: ?Sized, R>(&mut self, range: R) -> RangeMut<K, V> where
T: Ord,
K: Borrow<T>,
R: RangeBounds<T>,
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); }
pub fn append(&mut self, other: &mut Self)
[src]
pub fn append(&mut self, other: &mut Self)
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");
pub fn entry(&mut self, key: K) -> Entry<K, V>
[src]
pub fn entry(&mut self, key: K) -> Entry<K, V>
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);
pub fn find_cursors<Q>(&mut self, q: &Q) -> Cursors<K, V> where
K: Borrow<Q>,
Q: Ord,
[src]
pub fn find_cursors<Q>(&mut self, q: &Q) -> Cursors<K, V> where
K: Borrow<Q>,
Q: Ord,
Returns the cursors of a found pos.
pub fn max_height(&self) -> i32
[src]
pub fn max_height(&self) -> i32
Returns the max height of the tree.
pub fn is_empty(&self) -> bool
[src]
pub fn is_empty(&self) -> bool
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());
pub fn len(&self) -> usize
[src]
pub fn len(&self) -> usize
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);
pub fn new() -> Self
[src]
pub fn new() -> Self
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();
pub fn isomorphic(&self, other: &OrdMap<K, V>) -> bool
[src]
pub fn isomorphic(&self, other: &OrdMap<K, V>) -> bool
Return true if two tree are isomorphic.
pub fn check_balanced(&self) -> bool
[src]
pub fn check_balanced(&self) -> bool
Return true if tree is balanced.
pub fn check_ord_valid(&self) -> bool
[src]
pub fn check_ord_valid(&self) -> bool
Return true if tree is a BST.
pub fn remove<Q: ?Sized>(&mut self, q: &Q) -> Option<(K, V)> where
K: Borrow<Q>,
Q: Ord,
[src]
pub fn remove<Q: ?Sized>(&mut self, q: &Q) -> Option<(K, V)> where
K: Borrow<Q>,
Q: Ord,
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);
pub fn contains_key<Q: ?Sized>(&self, q: &Q) -> bool where
K: Borrow<Q>,
Q: Ord,
[src]
pub fn contains_key<Q: ?Sized>(&self, q: &Q) -> bool where
K: Borrow<Q>,
Q: Ord,
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);
pub fn get<Q: ?Sized>(&self, q: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Ord,
[src]
pub fn get<Q: ?Sized>(&self, q: &Q) -> Option<&V> where
K: Borrow<Q>,
Q: Ord,
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);
pub fn get_mut<Q: ?Sized>(&mut self, q: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Ord,
[src]
pub fn get_mut<Q: ?Sized>(&mut self, q: &Q) -> Option<&mut V> where
K: Borrow<Q>,
Q: Ord,
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");
pub fn insert(&mut self, key: K, value: V) -> Option<(K, V)>
[src]
pub fn insert(&mut self, key: K, value: V) -> Option<(K, V)>
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>pub fn keys(&self) -> Keys<K, V>
[src]
pub fn keys(&self) -> Keys<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>pub fn values(&self) -> Values<K, V>
[src]
pub fn values(&self) -> Values<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>pub fn values_mut(&mut self) -> ValuesMut<K, V>
[src]
pub fn values_mut(&mut self) -> ValuesMut<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>pub fn iter(&self) -> Iter<K, V>
[src]
pub fn iter(&self) -> Iter<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>pub fn iter_mut(&mut self) -> IterMut<K, V>
[src]
pub fn iter_mut(&mut self) -> IterMut<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]
impl<K, V> Drop for OrdMap<K, V>
impl<K, V> Clone for OrdMap<K, V> where
K: Ord + Clone,
V: Clone,
[src]
impl<K, V> Clone for OrdMap<K, V> where
K: Ord + Clone,
V: Clone,
fn clone(&self) -> Self
[src]
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<K, V> PartialEq for OrdMap<K, V> where
K: Eq + Ord,
V: PartialEq,
[src]
impl<K, V> PartialEq for OrdMap<K, V> where
K: Eq + Ord,
V: PartialEq,
fn eq(&self, other: &OrdMap<K, V>) -> bool
[src]
fn eq(&self, other: &OrdMap<K, V>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
fn ne(&self, other: &Rhs) -> bool
This method tests for !=
.
impl<K, V> Eq for OrdMap<K, V> where
K: Eq + Ord,
V: Eq,
[src]
impl<K, V> Eq for OrdMap<K, V> where
K: Eq + Ord,
V: Eq,
impl<'a, K, V> Index<&'a K> for OrdMap<K, V> where
K: Ord,
[src]
impl<'a, K, V> Index<&'a K> for OrdMap<K, V> where
K: Ord,
type Output = V
The returned type after indexing.
fn index(&self, key: &K) -> &V
[src]
fn index(&self, key: &K) -> &V
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]
impl<K, V> FromIterator<(K, V)> for OrdMap<K, V> where
K: Ord,
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> OrdMap<K, V>
[src]
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> OrdMap<K, V>
Creates a value from an iterator. Read more
impl<K, V> Extend<(K, V)> for OrdMap<K, V> where
K: Ord,
[src]
impl<K, V> Extend<(K, V)> for OrdMap<K, V> where
K: Ord,
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
[src]
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
impl<K, V> IntoIterator for OrdMap<K, V> where
K: Ord,
[src]
impl<K, V> IntoIterator for OrdMap<K, V> where
K: Ord,
type Item = (K, V)
The type of the elements being iterated over.
type IntoIter = IntoIter<K, V>
Which kind of iterator are we turning this into?
ⓘImportant traits for IntoIter<K, V>fn into_iter(self) -> IntoIter<K, V>
[src]
fn into_iter(self) -> 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]
impl<'a, K, V> IntoIterator for &'a OrdMap<K, V> where
K: Ord,