pub struct Map<KeyType: Ord, ContentType> { /* private fields */ }Expand description
§Map optimized for search
Map of <KeyType> to <ContentType> optimized for search
worst case log(n)
the implementation of a AVl self balancing tree
should add a little bit more info on the implementation
Implementations§
Source§impl<KeyType: Ord, ContentType> Map<KeyType, ContentType>
impl<KeyType: Ord, ContentType> Map<KeyType, ContentType>
Sourcepub fn new() -> Self
pub fn new() -> Self
This function returns a Map<KeyType, ContentType>
The map will not allocate until elements are inserted/added.
KeyType should implement Ord
§Example
let map:gavl::Map<String, i32> = gavl::Map::new();Sourcepub fn insert(
&mut self,
key: KeyType,
content: ContentType,
) -> Option<ContentType>
pub fn insert( &mut self, key: KeyType, content: ContentType, ) -> Option<ContentType>
Replaces or adds node to Map
-
If the key is not present in the map it works just like
add -
If it already present it will remplace the
contentwith new one return the old value
§Examples
let mut map = gavl::Map::<&str, i32>::new();
const key:&str = "key";
let old_content = map.insert(key, 12);
let holder = map.get(&key);
assert_eq!(old_content, None);
assert_eq!(holder, Ok(&12));
let old_content = map.insert(key, 13);
let holder = map.get(&key);
assert_eq!(old_content, Some(12));
assert_eq!(holder, Ok(&13));§Returns
§Success
None: key didn’t existed inMapSome(oldContent): old key’s content
Sourcepub fn empty(&mut self)
pub fn empty(&mut self)
Deletes all the nodes in the Map and sets the len to 0
§Examples
let mut map = gavl::Map::<String, i32>::new();
for elem in 0..10 {
map.add(elem.to_string(), elem);
}
assert_eq!(map.len(), 10);
map.empty();
assert_eq!(map.len(), 0);Sourcepub fn get(&self, key: &KeyType) -> Result<&ContentType, Error>
pub fn get(&self, key: &KeyType) -> Result<&ContentType, Error>
Gets a reference to the content associated to the key in Map
§Examples
let mut map = gavl::Map::<String, i32>::new();
map.add(12.to_string(), 12);
let holder = map.get(&12.to_string()); // holder = Ok(&12)
assert_eq!(holder, Ok(&12));§Returns
§Success
Ok(&ContentType): A reference to the content associated to that key
§Errors
Err(Error::NotFound): Is returned if the key is not present
Sourcepub fn get_mut(&mut self, key: &KeyType) -> Result<&mut ContentType, Error>
pub fn get_mut(&mut self, key: &KeyType) -> Result<&mut ContentType, Error>
Gets a mutable reference to the content associated to the key in Map
§Examples
let mut map = gavl::Map::<i32, i32>::new();
map.add(10, 1);
let holder = map.get_mut(&10); // holder = Ok(&1)
*holder.unwrap() += 9; // holder = Ok(&12+8)
let holder = map.get(&10);
assert_eq!(holder, Ok(&10));
§Returns
§Success
Ok(&mut ContentType): A mutable reference to the content associated to that key
§Errors
Err(Error::NotFound): Is returned if the key is not present
Sourcepub fn remove(&mut self, key: &KeyType) -> Result<ContentType, Error>
pub fn remove(&mut self, key: &KeyType) -> Result<ContentType, Error>
Deletes one node the Map drops the key and the content if the key is found
§Examples
let mut map = gavl::Map::<String, i32>::new();
map.add(12.to_string(), 12);
assert_eq!(map.len(), 1);
let holder = map.remove(&12.to_string());
assert_eq!(map.len(), 0);
assert_eq!(holder, Ok(12));§Returns
§Success
Ok(())
§Errors
Err(Error::NotFound): Is returned if the key is not present
Sourcepub fn delete(&mut self, key: &KeyType) -> Result<(), Error>
pub fn delete(&mut self, key: &KeyType) -> Result<(), Error>
Deletes one node the Map drops the key and the content if the key is found
§Examples
let mut map = gavl::Map::<String, i32>::new();
map.add(12.to_string(), 12);
assert_eq!(map.len(), 1);
map.delete(&12.to_string());
assert_eq!(map.len(), 0);§Returns
§Success
Ok(())
§Errors
Err(Error::NotFound): Is returned if the key is not present
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elemnts in the Map
§Examples
let mut map = gavl::Map::<String, i32>::new();
for elem in 0..10 {
map.add(elem.to_string(), elem);
}
assert_eq!(map.len(), 10);pub fn into_iter(self) -> IntoIter<KeyType, ContentType> ⓘ
Sourcepub fn iter(&self) -> Iter<'_, KeyType, ContentType> ⓘ
pub fn iter(&self) -> Iter<'_, KeyType, ContentType> ⓘ
Returns a iterator for Map
The iterator yields a pair of inmutable references to key and content for each element
Returned values are In-Order
item = (&KeyType, &ContentType)
§Examples
let mut map:gavl::Map<usize, usize> = gavl::Map::new();
for elem in (0..4).rev() {
map.add(elem, 0).unwrap();
}
let mut iterator = map.iter();
assert_eq!(Some((&0, &0)), iterator.next());
assert_eq!(Some((&1, &0)), iterator.next());
assert_eq!(Some((&2, &0)), iterator.next());
assert_eq!(Some((&3, &0)), iterator.next());
assert_eq!(None, iterator.next());
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, KeyType, ContentType> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, KeyType, ContentType> ⓘ
Returns a iterator for Map with mutable content
The iterator yields a pair of references for each elemnt inmutable for key and mutable for content
Returned values are In-Order
item = (&KeyType, &mut ContentType)
§Examples
let mut map:gavl::Map<usize, usize> = gavl::Map::new();
for elem in (0..4).rev() {
map.add(elem, 0).unwrap();
}
for (key, content) in map.iter_mut() {
*content = 1;
}
let mut iterator = map.iter_mut();
assert_eq!(Some((&0, &mut 1)), iterator.next());
assert_eq!(Some((&1, &mut 1)), iterator.next());
assert_eq!(Some((&2, &mut 1)), iterator.next());
assert_eq!(Some((&3, &mut 1)), iterator.next());
assert_eq!(None, iterator.next());
pub fn iter_ref_mut_unchecked( &mut self, ) -> IterMutUnchecked<'_, KeyType, ContentType> ⓘ
Sourcepub fn into_iter_precomputed(self) -> IntoIterPrecomp<KeyType, ContentType> ⓘ
pub fn into_iter_precomputed(self) -> IntoIterPrecomp<KeyType, ContentType> ⓘ
§Dependant on feature into_precomputed
Return an iterator check IntoIterPrecomp for extra info
- This method consumes the Map