SkipList

Struct SkipList 

Source
pub struct SkipList<K, V> { /* private fields */ }

Implementations§

Source§

impl<K: Ord, V> SkipList<K, V>

Source

pub fn new(max_level: usize) -> Self

Create a skip list with max level

§Example
use skip_list::SkipList;
let mut skiplist: SkipList<i32, i32> = SkipList::new(12);
Source

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

Returns a reference to the value of the key in skip list or None if not exist.

§Example
use skip_list::SkipList;
 
let mut skip_list = SkipList::default();
 
skip_list.insert(1, "a");
 
assert_eq!(skip_list.get(&1), Some(&"a"));
Source

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

Insert a key-value pair into skip list. If the key already exists, updates key’s value and return old value. Otherwise, None is returned.

§Example
use skip_list::SkipList;
 
let mut skip_list = SkipList::default();
 
assert_eq!(skip_list.insert(1, "a"), None);
assert_eq!(skip_list.get(&1), Some(&"a"));
assert_eq!(skip_list.insert(1, "aa"), Some("a"));
assert_eq!(skip_list.get(&1), Some(&"aa"));
 
Source

pub fn delete(&mut self, k: &K) -> Option<V>

Deletes and returns the key’s value from skip list or None if not exist.

§Example
use skip_list::SkipList;
 
let mut skip_list = SkipList::default();
 
assert_eq!(skip_list.insert(1, "a"), None);
assert_eq!(skip_list.get(&1), Some(&"a"));
assert_eq!(skip_list.delete(&1), Some("a"));
assert_eq!(skip_list.get(&1), None);
Source

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

Visit all key-value pairs in the order of keys The Iterator element type is (&K, &V).

§Example
use skip_list::SkipList;
 
let mut skip_list = SkipList::default();
assert_eq!(skip_list.insert(3, "c"), None);
assert_eq!(skip_list.insert(4, "d"), None);
assert_eq!(skip_list.insert(2, "b"), None);
assert_eq!(skip_list.insert(5, "e"), None);
assert_eq!(skip_list.insert(1, "a"), None);
 
// visit all key-value paris in the order of keys
let mut keys = vec![];
let mut values = vec![];
for (k, v) in skip_list.iter() {
    keys.push(k);
    values.push(v);
}
 
// check key sorted
assert_eq!(keys, vec![&1, &2, &3, &4, &5]);
assert_eq!(values, vec![&"a", &"b", &"c", &"d", &"e"]);
Source

pub fn iter_mut(&self) -> IterMut<'_, K, V>

Visit all key-value pairs in the order of keys The Iterator element type is (&K, &mut V). The value is mut, can be update;

§Example
use skip_list::SkipList;
 
let mut skip_list = SkipList::default();
assert_eq!(skip_list.insert(3, 3), None);
assert_eq!(skip_list.insert(4, 4), None);
assert_eq!(skip_list.insert(2, 2), None);
assert_eq!(skip_list.insert(5, 5), None);
assert_eq!(skip_list.insert(1, 1), None);
 
for (k, v) in skip_list.iter_mut() {
    *v = k * 10;
}
// visit all key-value paris in the order of keys
let mut keys = vec![];
let mut values = vec![];
for (k, v) in skip_list.iter() {
    keys.push(k);
    values.push(v);
}
 
// check key sorted
assert_eq!(keys, vec![&1, &2, &3, &4, &5]);
assert_eq!(values, vec![&10, &20, &30, &40, &50]);

Trait Implementations§

Source§

impl<K, V> Default for SkipList<K, V>

Source§

fn default() -> Self

Create a skip list with max level(12)

§Example
use skip_list::SkipList;
let mut skiplist: SkipList<i32, i32> = SkipList::default();
Source§

impl<K, V> Drop for SkipList<K, V>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<K, V> IntoIterator for SkipList<K, V>

Source§

fn into_iter(self) -> Self::IntoIter

Visit all key-value pairs in the order of keys The Iterator element type is (K, V).

§Example
use skip_list::SkipList;
 
let mut skip_list = SkipList::default();
assert_eq!(skip_list.insert(3, "c"), None);
assert_eq!(skip_list.insert(4, "d"), None);
assert_eq!(skip_list.insert(2, "b"), None);
assert_eq!(skip_list.insert(5, "e"), None);
assert_eq!(skip_list.insert(1, "a"), None);
 
// visit all key-value paris in the order of keys
let mut keys = vec![];
let mut values = vec![];
for (k, v) in skip_list.into_iter() {
    keys.push(k);
    values.push(v);
}
 
// check key sorted
assert_eq!(keys, vec![1, 2, 3, 4, 5]);
assert_eq!(values, vec!["a", "b", "c", "d", "e"]);
Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?

Auto Trait Implementations§

§

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

§

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

§

impl<K, V> !Send for SkipList<K, V>

§

impl<K, V> !Sync for SkipList<K, V>

§

impl<K, V> Unpin for SkipList<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for SkipList<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, 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.
Source§

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

Source§

fn vzip(self) -> V