[−][src]Struct immutable_chunkmap::map::Map
This Map uses a similar strategy to BTreeMap to ensure cache efficient performance on modern hardware while still providing log(N) get, insert, and remove operations.
For good performance, it is very important to understand that clone is a fundamental operation, it needs to be fast for your key and data types, because it's going to be called a lot whenever you change the map.
Why
-
Multiple threads can read this structure even while one thread is updating it. Using a library like arc_swap you can avoid ever blocking readers.
-
Snapshotting this structure is free.
Examples
use std::string::String; use self::immutable_chunkmap::map::Map; let m = Map::new() .insert(String::from("1"), 1).0 .insert(String::from("2"), 2).0 .insert(String::from("3"), 3).0; assert_eq!(m.get("1"), Option::Some(&1)); assert_eq!(m.get("2"), Option::Some(&2)); assert_eq!(m.get("3"), Option::Some(&3)); assert_eq!(m.get("4"), Option::None); for (k, v) in &m { println!("key {}, val: {}", k, v) }
Methods
impl<K, V> Map<K, V> where
K: Ord + Clone,
V: Clone, [src]
impl<K, V> Map<K, V> where
K: Ord + Clone,
V: Clone, pub fn new() -> Self[src]
pub fn new() -> SelfCreate a new empty map
pub fn insert_many<E: IntoIterator<Item = (K, V)>>(&self, elts: E) -> Self[src]
pub fn insert_many<E: IntoIterator<Item = (K, V)>>(&self, elts: E) -> SelfThis will insert many elements at once, and is potentially a lot faster than inserting one by one, especially if the data is sorted. It is just a wrapper around the more general update_many method.
#Examples
use self::immutable_chunkmap::map::Map; let mut v = vec![(1, 3), (10, 1), (-12, 2), (44, 0), (50, -1)]; v.sort_unstable_by_key(|&(k, _)| k); let m = Map::new().insert_many(v.iter().map(|(k, v)| (*k, *v))); for (k, v) in &v { assert_eq!(m.get(k), Option::Some(v)) }
pub fn update_many<Q, D, E, F>(&self, elts: E, f: F) -> Self where
E: IntoIterator<Item = (Q, D)>,
Q: Ord,
K: Borrow<Q>,
F: FnMut(Q, D, Option<(&K, &V)>) -> Option<(K, V)>, [src]
pub fn update_many<Q, D, E, F>(&self, elts: E, f: F) -> Self where
E: IntoIterator<Item = (Q, D)>,
Q: Ord,
K: Borrow<Q>,
F: FnMut(Q, D, Option<(&K, &V)>) -> Option<(K, V)>, This method updates multiple bindings in one call. Given an iterator of an arbitrary type (Q, D), where Q is any borrowed form of K, an update function taking Q, D, the current binding in the map, if any, and producing the new binding, if any, this method will produce a new map with updated bindings of many elements at once. It will skip intermediate node allocations where possible. If the data in elts is sorted, it will be able to skip many more intermediate allocations, and can produce a speedup of about 10x compared to inserting/updating one by one. In any case it should always be faster than inserting elements one by one, even with random unsorted keys.
#Examples
use std::iter::FromIterator; use self::immutable_chunkmap::map::Map; let m = Map::from_iter((0..4).map(|k| (k, k))); let m = m.update_many( (0..4).map(|x| (x, ())), |k, (), cur| cur.map(|(_, c)| (k, c + 1)) ); assert_eq!( m.into_iter().map(|(k, v)| (*k, *v)).collect::<Vec<_>>(), vec![(0, 1), (1, 2), (2, 3), (3, 4)] );
pub fn insert(&self, k: K, v: V) -> (Self, Option<V>)[src]
pub fn insert(&self, k: K, v: V) -> (Self, Option<V>)return a new map with (k, v) inserted into it. If k already exists in the old map, the old binding will be returned, and the new map will contain the new binding. In fact this method is just a wrapper around update.
pub fn update<Q, D, F>(&self, q: Q, d: D, f: F) -> (Self, Option<V>) where
Q: Ord,
K: Borrow<Q>,
F: FnMut(Q, D, Option<(&K, &V)>) -> Option<(K, V)>, [src]
pub fn update<Q, D, F>(&self, q: Q, d: D, f: F) -> (Self, Option<V>) where
Q: Ord,
K: Borrow<Q>,
F: FnMut(Q, D, Option<(&K, &V)>) -> Option<(K, V)>, return a new map with the binding for q, which can be any
borrowed form of k, updated to the result of f. If f returns
None, the binding will be removed from the new map, otherwise
it will be inserted. This function is more efficient than
calling get and then insert, since it makes only one tree
traversal instead of two. This method runs in log(N) time and
space where N is the size of the map.
Examples
use self::immutable_chunkmap::map::Map; let (m, _) = Map::new().update(0, 0, |k, d, _| Some((k, d))); let (m, _) = m.update(1, 1, |k, d, _| Some((k, d))); let (m, _) = m.update(2, 2, |k, d, _| Some((k, d))); assert_eq!(m.get(&0), Some(&0)); assert_eq!(m.get(&1), Some(&1)); assert_eq!(m.get(&2), Some(&2)); let (m, _) = m.update(0, (), |k, (), v| v.map(move |(_, v)| (k, v + 1))); assert_eq!(m.get(&0), Some(&1)); assert_eq!(m.get(&1), Some(&1)); assert_eq!(m.get(&2), Some(&2)); let (m, _) = m.update(1, (), |_, (), _| None); assert_eq!(m.get(&0), Some(&1)); assert_eq!(m.get(&1), None); assert_eq!(m.get(&2), Some(&2));
pub fn merge<F>(&self, other: &Map<K, V>, f: F) -> Self where
F: FnMut(&K, &V, &V) -> Option<V>, [src]
pub fn merge<F>(&self, other: &Map<K, V>, f: F) -> Self where
F: FnMut(&K, &V, &V) -> Option<V>, Merge two maps together. Bindings that exist in both maps will be passed to f, which may elect to remove the binding by returning None. This function runs in O(log(n) + m) time and space, where n is the size of the largest map, and m is the number of intersecting chunks. It will never be slower than calling update_many on the first map with an iterator over the second, and will be significantly faster if the intersection is minimal or empty.
Examples
use std::iter::FromIterator; use self::immutable_chunkmap::map::Map; let m0 = Map::from_iter((0..10).map(|k| (k, 1))); let m1 = Map::from_iter((10..20).map(|k| (k, 1))); let m2 = m0.merge(&m1, |_k, _v0, _v1| panic!("no intersection expected")); for i in 0..20 { assert!(m2.get(&i).is_some()) } let m3 = Map::from_iter((5..9).map(|k| (k, 1))); let m4 = m3.merge(&m2, |_k, v0, v1| Some(v0 + v1)); for i in 0..20 { assert!( *m4.get(&i).unwrap() == *m3.get(&i).unwrap_or(&0) + *m2.get(&i).unwrap_or(&0) ) }
pub fn get<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<&'a V> where
K: Borrow<Q>, [src]
pub fn get<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<&'a V> where
K: Borrow<Q>, lookup the mapping for k. If it doesn't exist return None. Runs in log(N) time and constant space. where N is the size of the map.
pub fn get_key<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<&'a K> where
K: Borrow<Q>, [src]
pub fn get_key<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<&'a K> where
K: Borrow<Q>, lookup the mapping for k. Return the key. If it doesn't exist return None. Runs in log(N) time and constant space. where N is the size of the map.
pub fn get_full<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<(&'a K, &'a V)> where
K: Borrow<Q>, [src]
pub fn get_full<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<(&'a K, &'a V)> where
K: Borrow<Q>, lookup the mapping for k. Return both the key and the value. If it doesn't exist return None. Runs in log(N) time and constant space. where N is the size of the map.
pub fn remove<Q: Sized + Ord>(&self, k: &Q) -> (Self, Option<V>) where
K: Borrow<Q>, [src]
pub fn remove<Q: Sized + Ord>(&self, k: &Q) -> (Self, Option<V>) where
K: Borrow<Q>, return a new map with the mapping under k removed. If the binding existed in the old map return it. Runs in log(N) time and log(N) space, where N is the size of the map.
pub fn len(&self) -> usize[src]
pub fn len(&self) -> usizeget the number of elements in the map O(1) time and space
pub fn range<'a, Q>(
&'a self,
lbound: Bound<Q>,
ubound: Bound<Q>
) -> Iter<'a, Q, K, V> where
Q: Ord,
K: Borrow<Q>, [src]
pub fn range<'a, Q>(
&'a self,
lbound: Bound<Q>,
ubound: Bound<Q>
) -> Iter<'a, Q, K, V> where
Q: Ord,
K: Borrow<Q>, return an iterator over the subset of elements in the map that are within the specified range.
The returned iterator runs in O(log(N) + M) time, and constant space. N is the number of elements in the tree, and M is the number of elements you examine.
if lbound >= ubound the returned iterator will be empty
Trait Implementations
impl<K: Clone + Ord + Clone, V: Clone + Clone> Clone for Map<K, V>[src]
impl<K: Clone + Ord + Clone, V: Clone + Clone> Clone for Map<K, V>fn clone(&self) -> Map<K, V>[src]
fn clone(&self) -> Map<K, V>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> Hash for Map<K, V> where
K: Hash + Ord + Clone,
V: Hash + Clone, [src]
impl<K, V> Hash for Map<K, V> where
K: Hash + Ord + Clone,
V: Hash + Clone, fn hash<H: Hasher>(&self, state: &mut H)[src]
fn hash<H: Hasher>(&self, state: &mut H)Feeds this value into the given [Hasher]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, 1.3.0[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher, Feeds a slice of this type into the given [Hasher]. Read more
impl<K, V> Default for Map<K, V> where
K: Ord + Clone,
V: Clone, [src]
impl<K, V> Default for Map<K, V> where
K: Ord + Clone,
V: Clone, impl<K, V> PartialEq for Map<K, V> where
K: PartialEq + Ord + Clone,
V: PartialEq + Clone, [src]
impl<K, V> PartialEq for Map<K, V> where
K: PartialEq + Ord + Clone,
V: PartialEq + Clone, fn eq(&self, other: &Map<K, V>) -> bool[src]
fn eq(&self, other: &Map<K, V>) -> boolThis method tests for self and other values to be equal, and is used by ==. Read more
#[must_use]
fn ne(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ne(&self, other: &Rhs) -> boolThis method tests for !=.
impl<K, V> Eq for Map<K, V> where
K: Eq + Ord + Clone,
V: Eq + Clone, [src]
impl<K, V> Eq for Map<K, V> where
K: Eq + Ord + Clone,
V: Eq + Clone, impl<K, V> PartialOrd for Map<K, V> where
K: Ord + Clone,
V: PartialOrd + Clone, [src]
impl<K, V> PartialOrd for Map<K, V> where
K: Ord + Clone,
V: PartialOrd + Clone, fn partial_cmp(&self, other: &Map<K, V>) -> Option<Ordering>[src]
fn partial_cmp(&self, other: &Map<K, V>) -> Option<Ordering>This method returns an ordering between self and other values if one exists. Read more
#[must_use]
fn lt(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn lt(&self, other: &Rhs) -> boolThis method tests less than (for self and other) and is used by the < operator. Read more
#[must_use]
fn le(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn le(&self, other: &Rhs) -> boolThis method tests less than or equal to (for self and other) and is used by the <= operator. Read more
#[must_use]
fn gt(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn gt(&self, other: &Rhs) -> boolThis method tests greater than (for self and other) and is used by the > operator. Read more
#[must_use]
fn ge(&self, other: &Rhs) -> bool1.0.0[src]
#[must_use]
fn ge(&self, other: &Rhs) -> boolThis method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl<K, V> Ord for Map<K, V> where
K: Ord + Clone,
V: Ord + Clone, [src]
impl<K, V> Ord for Map<K, V> where
K: Ord + Clone,
V: Ord + Clone, fn cmp(&self, other: &Map<K, V>) -> Ordering[src]
fn cmp(&self, other: &Map<K, V>) -> OrderingThis method returns an Ordering between self and other. Read more
fn max(self, other: Self) -> Self1.21.0[src]
fn max(self, other: Self) -> SelfCompares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self1.21.0[src]
fn min(self, other: Self) -> SelfCompares and returns the minimum of two values. Read more
impl<K, V> Debug for Map<K, V> where
K: Debug + Ord + Clone,
V: Debug + Clone, [src]
impl<K, V> Debug for Map<K, V> where
K: Debug + Ord + Clone,
V: Debug + Clone, fn fmt(&self, f: &mut Formatter) -> Result[src]
fn fmt(&self, f: &mut Formatter) -> ResultFormats the value using the given formatter. Read more
impl<'a, Q, K, V> Index<&'a Q> for Map<K, V> where
Q: Ord,
K: Ord + Clone + Borrow<Q>,
V: Clone, [src]
impl<'a, Q, K, V> Index<&'a Q> for Map<K, V> where
Q: Ord,
K: Ord + Clone + Borrow<Q>,
V: Clone, type Output = V
The returned type after indexing.
fn index(&self, k: &Q) -> &V[src]
fn index(&self, k: &Q) -> &VPerforms the indexing (container[index]) operation.
impl<K, V> FromIterator<(K, V)> for Map<K, V> where
K: Ord + Clone,
V: Clone, [src]
impl<K, V> FromIterator<(K, V)> for Map<K, V> where
K: Ord + Clone,
V: Clone, fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self[src]
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> SelfCreates a value from an iterator. Read more
impl<'a, K, V> IntoIterator for &'a Map<K, V> where
K: 'a + Borrow<K> + Ord + Clone,
V: 'a + Clone, [src]
impl<'a, K, V> IntoIterator for &'a Map<K, V> where
K: 'a + Borrow<K> + Ord + Clone,
V: 'a + Clone, Auto Trait Implementations
impl<K, V> Send for Map<K, V> where
K: Send + Sync,
V: Send + Sync,
impl<K, V> Send for Map<K, V> where
K: Send + Sync,
V: Send + Sync, impl<K, V> Sync for Map<K, V> where
K: Send + Sync,
V: Send + Sync,
impl<K, V> Sync for Map<K, V> where
K: Send + Sync,
V: Send + Sync, Blanket Implementations
impl<T> ToOwned for T where
T: Clone, [src]
impl<T> ToOwned for T where
T: Clone, type Owned = T
fn to_owned(&self) -> T[src]
fn to_owned(&self) -> TCreates owned data from borrowed data, usually by cloning. Read more
fn clone_into(&self, target: &mut T)[src]
fn clone_into(&self, target: &mut T)🔬 This is a nightly-only experimental API. (toowned_clone_into)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T> From for T[src]
impl<T> From for Timpl<T, U> Into for T where
U: From<T>, [src]
impl<T, U> Into for T where
U: From<T>, impl<T, U> TryFrom for T where
T: From<U>, [src]
impl<T, U> TryFrom for T where
T: From<U>, type Error = !
try_from)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>try_from)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized, [src]
impl<T> Borrow for T where
T: ?Sized, impl<T, U> TryInto for T where
U: TryFrom<T>, [src]
impl<T, U> TryInto for T where
U: TryFrom<T>, type Error = <U as TryFrom<T>>::Error
try_from)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>try_from)Performs the conversion.
impl<T> BorrowMut for T where
T: ?Sized, [src]
impl<T> BorrowMut for T where
T: ?Sized, fn borrow_mut(&mut self) -> &mut T[src]
fn borrow_mut(&mut self) -> &mut TMutably borrows from an owned value. Read more
impl<T> Any for T where
T: 'static + ?Sized, [src]
impl<T> Any for T where
T: 'static + ?Sized, fn get_type_id(&self) -> TypeId[src]
fn get_type_id(&self) -> TypeId🔬 This is a nightly-only experimental API. (get_type_id)
this method will likely be replaced by an associated static
Gets the TypeId of self. Read more