Map

Struct Map 

Source
pub struct Map<KeyType: Ord, ContentType> { /* private fields */ }
Expand description

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>

Source

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();
Source

pub fn add(&mut self, key: KeyType, content: ContentType) -> Result<(), Error>

Inserts a node into 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);
§Returns
§Success
  • Ok(())
§Errors
  • Err(Error::KeyOcupied): Is returned if the key is already present
Source

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 content with 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 in Map
  • Some(oldContent): old key’s content
Source

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);
Source

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
Source

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
Source

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
Source

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
Source

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);
Source

pub fn into_iter(self) -> IntoIter<KeyType, ContentType>

Source

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());
 
Source

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());
 
Source

pub fn iter_ref_mut_unchecked( &mut self, ) -> IterMutUnchecked<'_, KeyType, ContentType>

Source

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

Trait Implementations§

Source§

impl<KeyType: Ord + Clone, ContentType: Clone> Clone for Map<KeyType, ContentType>

Source§

fn clone(&self) -> Map<KeyType, ContentType>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<KeyType: Ord + Debug, ContentType: Debug> Debug for Map<KeyType, ContentType>

Source§

fn fmt(&self, formater: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<KeyType: Ord, ContentType> Default for Map<KeyType, ContentType>

Source§

fn default() -> Map<KeyType, ContentType>

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

impl<KeyType: Ord, ContentType> Drop for Map<KeyType, ContentType>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<KeyType: Ord, ContentType> FromIterator<(KeyType, ContentType)> for Map<KeyType, ContentType>

Source§

fn from_iter<IterType: IntoIterator<Item = (KeyType, ContentType)>>( iter: IterType, ) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<KeyType, ContentType> Freeze for Map<KeyType, ContentType>

§

impl<KeyType, ContentType> RefUnwindSafe for Map<KeyType, ContentType>
where KeyType: RefUnwindSafe, ContentType: RefUnwindSafe,

§

impl<KeyType, ContentType> !Send for Map<KeyType, ContentType>

§

impl<KeyType, ContentType> !Sync for Map<KeyType, ContentType>

§

impl<KeyType, ContentType> Unpin for Map<KeyType, ContentType>

§

impl<KeyType, ContentType> UnwindSafe for Map<KeyType, ContentType>
where KeyType: RefUnwindSafe, ContentType: RefUnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.