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>
where T: Ord + Clone, K: Hash + Eq, V: OrdBy<Target = T>,

source

pub fn new() -> Self

source

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;
}
Run
source

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));
Run
source

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));
Run
source

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));
Run
source

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)]);
Run
source

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)
);
Run
source

pub fn get(&self, key: &K) -> Option<&V>

source

pub fn modify<F>(&mut self, key: &K, op: F) -> bool
where F: Fn(&K, &mut V),

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));
Run
source

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);
Run
source

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

Trait Implementations§

source§

impl<T, K, V> Default for ValordMap<T, K, V>
where T: Ord + Clone, K: Hash + Eq, V: OrdBy<Target = T>,

source§

fn default() -> Self

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

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>
where T: Send, K: Sync + Send, V: Sync + Send,

§

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

§

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> 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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>,

§

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

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more