OMap

Struct OMap 

Source
pub struct OMap<K, V> { /* private fields */ }
Expand description

Ephemeral ordered-map type using left-leaning-red-black tree.

Refer package level documentation for brief description.

Implementations§

Source§

impl<K, V> OMap<K, V>

Source

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

Source§

impl<K, V> OMap<K, V>

Source

pub fn len(&self) -> usize

Return number of entries in index.

Source

pub fn is_empty(&self) -> bool

Check whether this index is empty.

Source§

impl<K, V> OMap<K, V>

Source

pub fn set(&mut self, key: K, value: V) -> Option<V>
where K: Ord, V: Clone,

Set value for key. If there is an existing entry for key, overwrite the old value with new value and return the old value.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Clone + Borrow<Q>, V: Clone, Q: Ord + ?Sized,

Remove key from this instance and return its value. If key is not present, then delete is effectively a no-op.

Source

pub fn validate(&self) -> Result<()>
where K: Ord + Debug,

Validate OMap tree with following rules:

  • From root to any leaf, no consecutive reds allowed in its path.
  • Number of blacks should be same under left child and right child.
  • Make sure keys are in sorted order.
Source§

impl<K, V> OMap<K, V>

Source

pub fn get<Q>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, V: Clone, Q: Ord + ?Sized,

Get value for key.

Source

pub fn iter(&self) -> Iter<'_, K, V>

Return an iterator over all entries in this instance.

use ppom::OMap;

let mut index: OMap<String,String> = OMap::new();
index.set("key1".to_string(), "value1".to_string());
index.set("key2".to_string(), "value2".to_string());

for (i, (key, value)) in index.iter().enumerate() {
    let refkey = format!("key{}", i+1);
    let refval = format!("value{}", i+1);
    assert_eq!(refkey, key);
    assert_eq!(refval, value);
}
Source

pub fn range<Q, R>(&self, range: R) -> Range<'_, K, V, R, Q>
where K: Borrow<Q>, R: RangeBounds<Q>, Q: Ord + ?Sized,

Range over all entries from low to high, specified by range.

use std::ops::Bound;
use ppom::OMap;

let mut index: OMap<String,String> = OMap::new();

index.set("key1".to_string(), "value1".to_string());
index.set("key2".to_string(), "value2".to_string());
index.set("key3".to_string(), "value3".to_string());

let low = Bound::Excluded("key1");
let high = Bound::Excluded("key2");
let item = index.range::<str, _>((low, high)).next();
assert_eq!(item, None);

let low = Bound::Excluded("key1");
let high = Bound::Excluded("key3");
let item = index.range::<str, _>((low, high)).next();
assert_eq!(item, Some(("key2".to_string(), "value2".to_string())));

let low = Bound::Included("key1");
let high = Bound::Included("key3");
let mut ranger = index.range::<str, _>((low, high));
let item = ranger.next();
assert_eq!(item, Some(("key1".to_string(), "value1".to_string())));
let item = ranger.last();
assert_eq!(item, Some(("key3".to_string(), "value3".to_string())));
Source

pub fn reverse<R, Q>(&self, range: R) -> Reverse<'_, K, V, R, Q>
where K: Borrow<Q>, R: RangeBounds<Q>, Q: Ord + ?Sized,

Reverse range over all entries from high to low, specified by range.

use std::ops::Bound;
use ppom::OMap;

let mut index: OMap<String,String> = OMap::new();

index.set("key1".to_string(), "value1".to_string());
index.set("key2".to_string(), "value2".to_string());
index.set("key3".to_string(), "value3".to_string());

let low = Bound::Included("key1");
let high = Bound::Included("key3");
let mut iter = index.reverse::<_, str>((low, high));

let item = iter.next();
assert_eq!(item, Some(("key3".to_string(), "value3".to_string())));
let item = iter.last();
assert_eq!(item, Some(("key1".to_string(), "value1".to_string())));
Source

pub fn random<R>(&self, rng: &mut R) -> Option<(K, V)>
where K: Clone, V: Clone, R: Rng,

Return a random entry from this index.

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for OMap<K, V>

Source§

fn clone(&self) -> OMap<K, V>

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<K: Default, V: Default> Default for OMap<K, V>

Source§

fn default() -> OMap<K, V>

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

impl<K, V> Extend<(K, V)> for OMap<K, V>
where K: Ord, V: Clone,

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = (K, V)>,

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations§

§

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

§

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

§

impl<K, V> Send for OMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for OMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for OMap<K, V>

§

impl<K, V> UnwindSafe for OMap<K, V>
where K: UnwindSafe, V: UnwindSafe,

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V