LeveledHashMap

Struct LeveledHashMap 

Source
pub struct LeveledHashMap<K: Eq + Hash, V> { /* private fields */ }
Expand description

A structure to separate values into different levels with keys. Every key-value entry which is not at the top level has a parent key at the superior level. Keys at the same level are unique, no matter what parent keys they have.

Implementations§

Source§

impl<K: Eq + Hash, V> LeveledHashMap<K, V>

Source

pub fn new() -> LeveledHashMap<K, V>

Create a new LeveledHashMap instance. The key needs to be implemented Eq and Hash traits.

use leveled_hash_map::LeveledHashMap;

let _map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();
Source

pub fn get(&self, key_chain: &[Arc<K>]) -> Option<&V>

Get a value by a key chain. The key chain starts at Level 0.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

let _result = map.get(&[Arc::new("first_key")]);
Source

pub fn get_mut(&mut self, key_chain: &[Arc<K>]) -> Option<&mut V>

Get a value by a key chain. The key chain starts at Level 0.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

let _result = map.get_mut(&[Arc::new("first_key")]);
Source

pub fn get_advanced( &self, key_chain: &[Arc<K>], start_level: usize, ) -> Option<&V>

Get a value by a key chain and a level which the key chain starts with.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

let _result = map.get_advanced(&[Arc::new("second_key")], 1);
Source

pub fn get_advanced_mut( &mut self, key_chain: &[Arc<K>], start_level: usize, ) -> Option<&mut V>

Get a value by a key chain and a level which the key chain starts with.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

let _result = map.get_advanced_mut(&[Arc::new("second_key")], 1);
Source

pub fn get_professional( &self, key_chain: &[Arc<K>], start_level: usize, ) -> Result<(Option<Arc<K>>, &V), LeveledHashMapError<K>>

Get a value and its parent key by a key chain and a level which the key chain starts with. It returns a Err(LeveledHashMapError) instance to describe the reason of the getting failure.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

map.insert(&[Arc::new("food")], "食物".to_string()).unwrap();

map.insert(&[Arc::new("food"), Arc::new("dessert")], "甜點".to_string()).unwrap();

let result_1 = map.get_professional(&[Arc::new("food")], 0).unwrap();

let result_2 = map.get_professional(&[Arc::new("food"), Arc::new("dessert")], 0).unwrap();

assert_eq!(None, result_1.0);
assert_eq!("食物", result_1.1);

assert_eq!(Some(Arc::new("food")), result_2.0);
assert_eq!("甜點", result_2.1);
Source

pub fn get_professional_mut( &mut self, key_chain: &[Arc<K>], start_level: usize, ) -> Result<(Option<Arc<K>>, &mut V), LeveledHashMapError<K>>

Get a value and its parent key by a key chain and a level which the key chain starts with. It returns a Err(LeveledHashMapError) instance to describe the reason of the getting failure.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

map.insert(&[Arc::new("food")], "食物".to_string()).unwrap();

map.insert(&[Arc::new("food"), Arc::new("dessert")], "甜點".to_string()).unwrap();

let result = map.get_professional_mut(&[Arc::new("food")], 0).unwrap();

result.1.push_str("/食品");

assert_eq!(None, result.0);
assert_eq!("食物/食品", result.1);
Source

pub fn remove( &mut self, key_chain: &[Arc<K>], ) -> Option<(V, Vec<HashMap<Arc<K>, (Option<Arc<K>>, V)>>)>

Remove a value by a key chain. The key chain starts at Level 0.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

map.insert(&[Arc::new("food")], "食物".to_string()).unwrap();

map.insert(&[Arc::new("food"), Arc::new("dessert")], "甜點".to_string()).unwrap();

map.insert(&[Arc::new("food"), Arc::new("meat")], "肉類".to_string()).unwrap();

let result = map.remove(&[Arc::new("food"), Arc::new("dessert")]).unwrap();

assert_eq!("甜點", result.0);
assert_eq!(0, result.1.len());

let result = map.remove(&[Arc::new("food")]).unwrap();

assert_eq!("食物", result.0);
assert_eq!(1, result.1.len());
assert_eq!(
    &(Some(Arc::new("food")), "肉類".to_string()),
    result.1[0].get(&Arc::new("meat")).unwrap()
);
Source

pub fn remove_advanced( &mut self, key_chain: &[Arc<K>], start_level: usize, ) -> Option<(V, Vec<HashMap<Arc<K>, (Option<Arc<K>>, V)>>)>

Remove a value by a key chain and a level which the key chain starts with.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

map.insert(&[Arc::new("food")], "食物".to_string()).unwrap();

map.insert(&[Arc::new("food"), Arc::new("dessert")], "甜點".to_string()).unwrap();

map.insert(&[Arc::new("food"), Arc::new("meat")], "肉類".to_string()).unwrap();

let result = map.remove_advanced(&[Arc::new("dessert")], 1).unwrap();

assert_eq!("甜點", result.0);
assert_eq!(0, result.1.len());

let result = map.remove_advanced(&[Arc::new("food")], 0).unwrap();

assert_eq!("食物", result.0);
assert_eq!(1, result.1.len());
assert_eq!(
    &(Some(Arc::new("food")), "肉類".to_string()),
    result.1[0].get(&Arc::new("meat")).unwrap()
);
Source

pub fn remove_professional( &mut self, key_chain: &[Arc<K>], start_level: usize, ) -> Result<(Option<Arc<K>>, V, Vec<HashMap<Arc<K>, (Option<Arc<K>>, V)>>), LeveledHashMapError<K>>

Remove a value by a key chain and a level which the key chain starts with. It returns a Err(LeveledHashMapError) instance to describe the reason of the getting failure.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

map.insert(&[Arc::new("food")], "食物".to_string()).unwrap();

map.insert(&[Arc::new("food"), Arc::new("dessert")], "甜點".to_string()).unwrap();

map.insert(&[Arc::new("food"), Arc::new("meat")], "肉類".to_string()).unwrap();

let result = map.remove_professional(&[Arc::new("dessert")], 1).unwrap();

assert_eq!(Some(Arc::new("food")), result.0);
assert_eq!("甜點", result.1);
assert_eq!(0, result.2.len());

let result = map.remove_professional(&[Arc::new("food")], 0).unwrap();

assert_eq!(None, result.0);
assert_eq!("食物", result.1);
assert_eq!(1, result.2.len());
assert_eq!(
    &(Some(Arc::new("food")), "肉類".to_string()),
    result.2[0].get(&Arc::new("meat")).unwrap()
);
Source

pub fn insert( &mut self, key_chain: &[Arc<K>], value: V, ) -> Result<Option<V>, LeveledHashMapError<K>>

Insert a value by a key chain. It returns a Err(LeveledHashMapError) instance to describe the reason of the getting failure.

use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

map.insert(&[Arc::new("food")], "食物".to_string()).unwrap();

{
    let result = map.get(&[Arc::new("food")]).unwrap();

    assert_eq!("食物", result);
}

let result = map.insert(&[Arc::new("food")], "食品".to_string()).unwrap();

assert_eq!(Some("食物".to_string()), result);

let result = map.get(&[Arc::new("food")]).unwrap();

assert_eq!("食品", result);
Source

pub fn insert_many( &mut self, key_chain: &[Arc<K>], value: HashMap<K, V>, start_level: usize, ) -> Result<HashMap<Arc<K>, V>, LeveledHashMapError<K>>

Insert values by a key chain and a HashMap instance and a level which the key chain starts with. It returns a Err(LeveledHashMapError) instance to describe the reason of the getting failure.

use std::collections::HashMap;
use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

map.insert(&[Arc::new("food")], "食物".to_string()).unwrap();

let mut insert_map = HashMap::new();

insert_map.insert("dessert", "甜點".to_string());
insert_map.insert("meat", "肉類".to_string());

map.insert_many(&[Arc::new("food")], insert_map, 0).unwrap();

let result = map.get(&[Arc::new("food"), Arc::new("dessert")]).unwrap();

assert_eq!("甜點", result);

let result = map.get(&[Arc::new("food"), Arc::new("meat")]).unwrap();

assert_eq!("肉類", result);
Source

pub fn keys(&self, level: usize) -> Option<&HashMap<Arc<K>, HashSet<Arc<K>>>>

Get the keys at a specific level.

use std::collections::HashMap;
use std::sync::Arc;

use leveled_hash_map::LeveledHashMap;

let mut map: LeveledHashMap<&'static str, String> = LeveledHashMap::new();

map.insert(&[Arc::new("food")], "食物".to_string()).unwrap();

let mut insert_map = HashMap::new();

insert_map.insert("dessert", "甜點".to_string());
insert_map.insert("meat", "肉類".to_string());

map.insert_many(&[Arc::new("food")], insert_map, 0).unwrap();

let result = map.keys(0).unwrap();

assert_eq!(1, result.len());

let result = map.keys(1).unwrap();

assert_eq!(2, result.len());

Trait Implementations§

Source§

impl<K: Debug + Eq + Hash, V: Debug> Debug for LeveledHashMap<K, V>

Source§

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

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

impl<K: Eq + Hash, V> Default for LeveledHashMap<K, V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<K, V> Freeze for LeveledHashMap<K, V>

§

impl<K, V> RefUnwindSafe for LeveledHashMap<K, V>

§

impl<K, V> Send for LeveledHashMap<K, V>
where K: Sync + Send, V: Send,

§

impl<K, V> Sync for LeveledHashMap<K, V>
where K: Sync + Send, V: Sync,

§

impl<K, V> Unpin for LeveledHashMap<K, V>
where V: Unpin,

§

impl<K, V> UnwindSafe for LeveledHashMap<K, V>

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> 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, 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.