Struct BPlusTreeMap

Source
pub struct BPlusTreeMap<K: Key, V, A: Argument<K> = ()> { /* private fields */ }
Expand description

A B+ tree map implemented with BPlusTree

Implementations§

Source§

impl<K: Key, V, A: Argument<K>> BPlusTreeMap<K, V, A>

Source

pub fn new() -> Self

Create a new BPlusTreeMap

§Examples
use sweep_bptree::BPlusTreeMap;

let map = BPlusTreeMap::<i32, i32>::new();

assert!(map.is_empty());
Source

pub fn len(&self) -> usize

Returns item count in the map

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no item

§Examples
use sweep_bptree::BPlusTreeMap;

let map = BPlusTreeMap::<i32, i32>::new();

assert!(map.is_empty());
Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Insert a key-value pair into the map

§Examples
use sweep_bptree::BPlusTreeMap;

let mut map = BPlusTreeMap::<i32, i32>::new();
map.insert(1, 2);
map.insert(2, 4);

assert!(!map.is_empty());
assert_eq!(map.len(), 2);
Source

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

Returns a reference to the value corresponding to the key.

§Examples
use sweep_bptree::BPlusTreeMap;

let mut map = BPlusTreeMap::<i32, i32>::new();
map.insert(1, 2);

assert_eq!(map.get(&1).unwrap(), &2);
assert!(map.get(&2).is_none());
Source

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

Returns a mut reference to the value corresponding to the key.

§Examples
use sweep_bptree::BPlusTreeMap;

let mut map = BPlusTreeMap::<i32, i32>::new();
map.insert(1, 2);
*map.get_mut(&1).unwrap() += 1;
assert_eq!(map.get(&1).unwrap(), &3);
Source

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

Removes a key from the map, returning the value at the key if the key was previously in the map.

§Examples
use sweep_bptree::BPlusTreeMap;

let mut map = BPlusTreeMap::<i32, i32>::new();
map.insert(1, 2);

assert!(map.remove(&1).is_some());
assert!(map.remove(&2).is_none());
Source

pub fn iter(&self) -> impl Iterator<Item = (&K, &V)>

Returns an iterator over the map.

§Examples

use sweep_bptree::BPlusTreeMap;

let mut map = BPlusTreeMap::<i32, i32>::new();
map.insert(1, 2);
map.insert(2, 3);

let kvs = map.iter().map(|(k, v)| (*k, *v)).collect::<Vec<_>>();
assert_eq!(kvs.len(), 2);
assert_eq!(kvs, vec![(1, 2), (2, 3)]);
Source

pub fn root_argument(&self) -> &A

Get root argument for the map

Source

pub fn get_by_argument<Q>(&self, query: Q) -> Option<(&K, &V)>
where A: SearchArgument<K, Query = Q>,

Get value by argument’s query

§Example

use sweep_bptree::BPlusTreeMap;
use sweep_bptree::argument::count::Count;


let mut map = BPlusTreeMap::<i32, i32, Count>::new();
map.insert(1, 2);
map.insert(2, 3);
map.insert(3, 4);

assert_eq!(map.get_by_argument(0), Some((&1, &2)));
assert_eq!(map.get_by_argument(1), Some((&2, &3)));
assert_eq!(map.get_by_argument(2), Some((&3, &4)));
Source

pub fn get_mut_by_argument<Q>(&mut self, query: Q) -> Option<&mut V>
where A: SearchArgument<K, Query = Q>,

Get mut referece to value by argument’s query

§Example

use sweep_bptree::BPlusTreeMap;
use sweep_bptree::argument::count::Count;


let mut map = BPlusTreeMap::<i32, i32, Count>::new();
map.insert(1, 2);
map.insert(2, 3);
map.insert(3, 4);

*map.get_mut_by_argument(0).unwrap() = 20;
assert_eq!(map.get_by_argument(0), Some((&1, &20)));
Source

pub fn remove_by_argument<Q>(&mut self, query: Q) -> Option<(K, V)>
where A: SearchArgument<K, Query = Q>,

Remove by argument’s query, returns deleted Key Value if exist

§Example

use sweep_bptree::BPlusTreeMap;
use sweep_bptree::argument::count::Count;


let mut map = BPlusTreeMap::<i32, i32, Count>::new();
map.insert(1, 2);
map.insert(2, 3);
map.insert(3, 4);

assert_eq!(map.remove_by_argument(0), Some((1, 2)));
assert_eq!(map.remove_by_argument(0), Some((2, 3)));
assert_eq!(map.remove_by_argument(0), Some((3, 4)));
Source

pub fn rank_by_argument<R>(&self, k: &K) -> Result<R, R>
where A: RankArgument<K, Rank = R>,

Get the rank for key

§Example

use sweep_bptree::BPlusTreeMap;
use sweep_bptree::argument::count::Count;

let mut map = BPlusTreeMap::<i32, i32, Count>::new();
map.insert(1, 2);
map.insert(2, 3);
map.insert(3, 4);

// 0 does not exists
assert_eq!(map.rank_by_argument(&0), Err(0));

// 1's rank is 0
assert_eq!(map.rank_by_argument(&1), Ok(0));
assert_eq!(map.rank_by_argument(&2), Ok(1));
assert_eq!(map.rank_by_argument(&3), Ok(2));

// 4 does not exists
assert_eq!(map.rank_by_argument(&4), Err(3));
Source

pub fn descend_visit<VI, R>(&self, v: VI) -> Option<R>
where VI: DescendVisit<K, V, A, Result = R>,

Visit the tree’s node with a visitor Returns None if visitor cancelled Otherwise, returns visitor’s result

Trait Implementations§

Source§

impl<K: Key, V> Default for BPlusTreeMap<K, V>

Source§

fn default() -> Self

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

impl<K: Key, V, A: Argument<K>> FromIterator<(K, V)> for BPlusTreeMap<K, V, A>

Source§

fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self

Create a BPlusTreeMap from an iterator

§Example
use sweep_bptree::BPlusTreeMap;

let mut items = (0..1000).map(|i| (i, i));

let map = BPlusTreeMap::<i32, i32>::from_iter(items);
assert_eq!(map.len(), 1000);
assert_eq!(map.get(&1), Some(&1));

Auto Trait Implementations§

§

impl<K, V, A = ()> !Freeze for BPlusTreeMap<K, V, A>

§

impl<K, V, A> RefUnwindSafe for BPlusTreeMap<K, V, A>

§

impl<K, V, A> Send for BPlusTreeMap<K, V, A>
where A: Send, K: Send, V: Send,

§

impl<K, V, A> Sync for BPlusTreeMap<K, V, A>
where A: Sync, K: Sync, V: Sync,

§

impl<K, V, A> Unpin for BPlusTreeMap<K, V, A>
where A: Unpin,

§

impl<K, V, A> UnwindSafe for BPlusTreeMap<K, V, A>
where A: UnwindSafe, 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> 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.