Struct leveled_hash_map::LeveledHashMap
source · [−]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
sourceimpl<K: Eq + Hash, V> LeveledHashMap<K, V>
impl<K: Eq + Hash, V> LeveledHashMap<K, V>
sourcepub fn new() -> LeveledHashMap<K, V>
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();
sourcepub fn get(&self, key_chain: &[Arc<K>]) -> Option<&V>
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")]);
sourcepub fn get_mut(&mut self, key_chain: &[Arc<K>]) -> Option<&mut V>
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")]);
sourcepub fn get_advanced(
&self,
key_chain: &[Arc<K>],
start_level: usize
) -> Option<&V>
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);
sourcepub fn get_advanced_mut(
&mut self,
key_chain: &[Arc<K>],
start_level: usize
) -> Option<&mut V>
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);
sourcepub fn get_professional(
&self,
key_chain: &[Arc<K>],
start_level: usize
) -> Result<(Option<Arc<K>>, &V), LeveledHashMapError<K>>
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);
sourcepub fn get_professional_mut(
&mut self,
key_chain: &[Arc<K>],
start_level: usize
) -> Result<(Option<Arc<K>>, &mut V), LeveledHashMapError<K>>
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);
sourcepub fn remove(
&mut self,
key_chain: &[Arc<K>]
) -> Option<(V, Vec<HashMap<Arc<K>, (Option<Arc<K>>, V)>>)>
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()
);
sourcepub fn remove_advanced(
&mut self,
key_chain: &[Arc<K>],
start_level: usize
) -> Option<(V, Vec<HashMap<Arc<K>, (Option<Arc<K>>, V)>>)>
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()
);
sourcepub 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>>
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()
);
sourcepub fn insert(
&mut self,
key_chain: &[Arc<K>],
value: V
) -> Result<Option<V>, LeveledHashMapError<K>>
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);
sourcepub fn insert_many(
&mut self,
key_chain: &[Arc<K>],
value: HashMap<K, V>,
start_level: usize
) -> Result<HashMap<Arc<K>, V>, LeveledHashMapError<K>>
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);
sourcepub fn keys(&self, level: usize) -> Option<&HashMap<Arc<K>, HashSet<Arc<K>>>>
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
Auto Trait Implementations
impl<K, V> RefUnwindSafe for LeveledHashMap<K, V> where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for LeveledHashMap<K, V> where
K: Send + Sync,
V: Send,
impl<K, V> Sync for LeveledHashMap<K, V> where
K: Send + Sync,
V: Sync,
impl<K, V> Unpin for LeveledHashMap<K, V> where
V: Unpin,
impl<K, V> UnwindSafe for LeveledHashMap<K, V> where
K: RefUnwindSafe,
V: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more