Struct valord_map::ValordMap
source · pub struct ValordMap<T, K, V: OrdBy<Target = T>> { /* private fields */ }Implementations§
source§impl<T, K, V> ValordMap<T, K, V>
impl<T, K, V> ValordMap<T, K, V>
pub fn new() -> Self
sourcepub fn watcher(&self) -> Watcher<V>
pub fn watcher(&self) -> Watcher<V>
Watch a key, trigger a notification when the maximum value changes.
Example
use valord_map::ValordMap;
use std::time::Duration;
#[tokio::main]
async fn main() {
let mut sorted_map = ValordMap::new();
let mut watcher = sorted_map.watcher();
let handle = tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(1)).await;
sorted_map.insert("qians", 1);
tokio::time::sleep(Duration::from_secs(1)).await;
sorted_map.insert("tedious", 2);
tokio::time::sleep(Duration::from_secs(1)).await;
sorted_map.insert("sheng", 3);
tokio::time::sleep(Duration::from_secs(1)).await;
sorted_map.insert("xuandu", 4);
tokio::time::sleep(Duration::from_secs(1)).await;
sorted_map.insert("xuandu2", 5);
tokio::time::sleep(Duration::from_secs(1)).await;
sorted_map.insert("xuandu3", 6);
});
println!("watching...");
for v in 1..=6 {
let header = watcher.head_changed().await.unwrap().unwrap();
assert_eq!(&v, header.as_ref());
}
let _ = handle.await;
}Runsourcepub fn insert(&mut self, key: K, value: V)
pub fn insert(&mut self, key: K, value: V)
insert into ValordMap
Example
use valord_map::ValordMap;
let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);
let sorted_pairs: Vec<_> = sorted_map.iter().collect();
assert_eq!(sorted_pairs.len(), 3);
assert_eq!(sorted_pairs[0].1, &1);
assert_eq!(sorted_pairs[1].1, &1);
assert_eq!(sorted_pairs[2], (&"tedious", &2));Runsourcepub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>
Returns an iterator over the ValordMap. The iterator yields all items from start to end order by value.ord_by().
Example
use valord_map::ValordMap;
let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);
let mut iter = sorted_map.iter();
assert_eq!(iter.next().unwrap().1, &1);
assert_eq!(iter.next().unwrap().1, &1);
assert_eq!(iter.next().unwrap(), (&"tedious", &2));Runsourcepub fn first(&self) -> Vec<(&K, &V)>
pub fn first(&self) -> Vec<(&K, &V)>
Returns the first vector of key-value pairs in the map. The value in this pair is the minimum values in the map.
Example
use valord_map::ValordMap;
let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);
let min_list = sorted_map.first();
assert_eq!(min_list.len(), 2);
assert!(min_list.iter().all(|(_, v)| **v == 1));Runsourcepub fn last(&self) -> Vec<(&K, &V)>
pub fn last(&self) -> Vec<(&K, &V)>
Returns the last vector of key-value pairs in the map. The value in this pair is the maximum values in the map.
Example
use valord_map::ValordMap;
let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("xuandu", 3);
sorted_map.insert("xuandu", 1);
let max_list = sorted_map.last();
assert_eq!(max_list.len(), 1);
assert_eq!(max_list, vec![(&"tedious", &2)]);Runsourcepub fn range<R>(&self, range: R) -> impl Iterator<Item = (&K, &V)>where
R: RangeBounds<V::Target>,
pub fn range<R>(&self, range: R) -> impl Iterator<Item = (&K, &V)>where
R: RangeBounds<V::Target>,
remove from ValordMap
Example
use valord_map::ValordMap;
let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
sorted_map.insert("tedious", 2);
sorted_map.insert("sheng", 3);
sorted_map.insert("xuandu", 4);
sorted_map.insert("xuandu2", 5);
sorted_map.insert("xuandu3", 6);
assert_eq!(sorted_map.range(4..).last().unwrap(), (&"xuandu3", &6));
assert_eq!(
sorted_map
.range(4..)
.filter(|(_, v)| **v != 6)
.last()
.unwrap(),
(&"xuandu2", &5)
);Runpub fn get(&self, key: &K) -> Option<&V>
sourcepub fn modify<F>(&mut self, key: &K, op: F) -> bool
pub fn modify<F>(&mut self, key: &K, op: F) -> bool
Modify value in map, if exist return true, else return false
Example
use valord_map::ValordMap;
let mut sorted_map = ValordMap::new();
sorted_map.insert("qians", 1);
assert!(sorted_map.modify(&"qians", |k, v| *v = 2));
assert_eq!(sorted_map.iter().next().unwrap(), (&"qians", &2));Runsourcepub fn remove(&mut self, key: &K) -> Option<V>
pub fn remove(&mut self, key: &K) -> Option<V>
remove from ValordMap
Example
use valord_map::ValordMap;
let mut sorted_map = ValordMap::new();
sorted_map.insert(1, "a");
sorted_map.insert(2, "b");
let removed_value = sorted_map.remove(&1);
assert_eq!(removed_value, Some("a"));
assert_eq!(sorted_map.get(&1), None);Runsourcepub fn remove_entry(&mut self, key: &K) -> Option<(Arc<K>, V)>
pub fn remove_entry(&mut self, key: &K) -> Option<(Arc<K>, V)>
Removes a key from the map, returning the stored key and value if the key was previously in the map.
Example
use valord_map::ValordMap;
let mut sorted_map = ValordMap::new();
sorted_map.insert(1, "a");
sorted_map.insert(2, "b");
let removed_entry = sorted_map.remove_entry(&1);
assert_eq!(removed_entry, Some((std::sync::Arc::new(1), "a")));
assert_eq!(sorted_map.get(&1), None);RunTrait Implementations§
Auto Trait Implementations§
impl<T, K, V> Freeze for ValordMap<T, K, V>
impl<T, K, V> !RefUnwindSafe for ValordMap<T, K, V>
impl<T, K, V> Send for ValordMap<T, K, V>
impl<T, K, V> Sync for ValordMap<T, K, V>
impl<T, K, V> Unpin for ValordMap<T, K, V>
impl<T, K, V> !UnwindSafe for ValordMap<T, K, V>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more