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

An ordered map based on a lock-free skip list.

This is an alternative to BTreeMap which supports concurrent access across multiple threads.

Implementations§

source§

impl<K, V> SkipMap<K, V>

source

pub fn new() -> Self

Returns a new, empty map.

Example
use crossbeam_skiplist::SkipMap;

let map: SkipMap<i32, &str> = SkipMap::new();
source

pub fn is_empty(&self) -> bool

Returns true if the map is empty.

Example
use crossbeam_skiplist::SkipMap;

let map: SkipMap<&str, &str> = SkipMap::new();
assert!(map.is_empty());

map.insert("key", "value");
assert!(!map.is_empty());
source

pub fn len(&self) -> usize

Returns the number of entries in the map.

If the map is being concurrently modified, consider the returned number just an approximation without any guarantees.

Example
use crossbeam_skiplist::SkipMap;

let map = SkipMap::new();
map.insert(0, 1);
assert_eq!(map.len(), 1);

for x in 1..=5 {
    map.insert(x, x + 1);
}

assert_eq!(map.len(), 6);
source§

impl<K, V> SkipMap<K, V>
where K: Ord,

source

pub fn front(&self) -> Option<Entry<'_, K, V>>

Returns the entry with the smallest key.

This function returns an Entry which can be used to access the key’s associated value.

Example
use crossbeam_skiplist::SkipMap;

let numbers = SkipMap::new();
numbers.insert(5, "five");
assert_eq!(*numbers.front().unwrap().value(), "five");
numbers.insert(6, "six");
assert_eq!(*numbers.front().unwrap().value(), "five");
source

pub fn back(&self) -> Option<Entry<'_, K, V>>

Returns the entry with the largest key.

This function returns an Entry which can be used to access the key’s associated value.

Example
use crossbeam_skiplist::SkipMap;

let numbers = SkipMap::new();
numbers.insert(5, "five");
assert_eq!(*numbers.back().unwrap().value(), "five");
numbers.insert(6, "six");
assert_eq!(*numbers.back().unwrap().value(), "six");
source

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

Returns true if the map contains a value for the specified key.

Example
use crossbeam_skiplist::SkipMap;

let ages = SkipMap::new();
ages.insert("Bill Gates", 64);

assert!(ages.contains_key(&"Bill Gates"));
assert!(!ages.contains_key(&"Steve Jobs"));
source

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

Returns an entry with the specified key.

This function returns an Entry which can be used to access the key’s associated value.

Example
use crossbeam_skiplist::SkipMap;

let numbers: SkipMap<&str, i32> = SkipMap::new();
assert!(numbers.get("six").is_none());

numbers.insert("six", 6);
assert_eq!(*numbers.get("six").unwrap().value(), 6);
source

pub fn lower_bound<'a, Q>(&'a self, bound: Bound<&Q>) -> Option<Entry<'a, K, V>>
where K: Borrow<Q>, Q: Ord + ?Sized,

Returns an Entry pointing to the lowest element whose key is above the given bound. If no such element is found then None is returned.

This function returns an Entry which can be used to access the key’s associated value.

Example
use crossbeam_skiplist::SkipMap;
use std::ops::Bound::*;

let numbers = SkipMap::new();
numbers.insert(6, "six");
numbers.insert(7, "seven");
numbers.insert(12, "twelve");

let greater_than_five = numbers.lower_bound(Excluded(&5)).unwrap();
assert_eq!(*greater_than_five.value(), "six");

let greater_than_six = numbers.lower_bound(Excluded(&6)).unwrap();
assert_eq!(*greater_than_six.value(), "seven");

let greater_than_thirteen = numbers.lower_bound(Excluded(&13));
assert!(greater_than_thirteen.is_none());
source

pub fn upper_bound<'a, Q>(&'a self, bound: Bound<&Q>) -> Option<Entry<'a, K, V>>
where K: Borrow<Q>, Q: Ord + ?Sized,

Returns an Entry pointing to the highest element whose key is below the given bound. If no such element is found then None is returned.

This function returns an Entry which can be used to access the key’s associated value.

Example
use crossbeam_skiplist::SkipMap;
use std::ops::Bound::*;

let numbers = SkipMap::new();
numbers.insert(6, "six");
numbers.insert(7, "seven");
numbers.insert(12, "twelve");

let less_than_eight = numbers.upper_bound(Excluded(&8)).unwrap();
assert_eq!(*less_than_eight.value(), "seven");

let less_than_six = numbers.upper_bound(Excluded(&6));
assert!(less_than_six.is_none());
source

pub fn get_or_insert(&self, key: K, value: V) -> Entry<'_, K, V>

Finds an entry with the specified key, or inserts a new key-value pair if none exist. This function returns an Entry which can be used to access the key’s associated value.

Example
use crossbeam_skiplist::SkipMap;

let ages = SkipMap::new();
let gates_age = ages.get_or_insert("Bill Gates", 64);
assert_eq!(*gates_age.value(), 64);

ages.insert("Steve Jobs", 65);
let jobs_age = ages.get_or_insert("Steve Jobs", -1);
assert_eq!(*jobs_age.value(), 65);
source

pub fn get_or_insert_with<F>(&self, key: K, value_fn: F) -> Entry<'_, K, V>
where F: FnOnce() -> V,

Finds an entry with the specified key, or inserts a new key-value pair if none exist, where value is calculated with a function.

Note: Another thread may write key value first, leading to the result of this closure discarded. If closure is modifying some other state (such as shared counters or shared objects), it may lead to undesired behaviour such as counters being changed without result of closure inserted This function returns an Entry which can be used to access the key’s associated value.

Example
use crossbeam_skiplist::SkipMap;

let ages = SkipMap::new();
let gates_age = ages.get_or_insert_with("Bill Gates", || 64);
assert_eq!(*gates_age.value(), 64);

ages.insert("Steve Jobs", 65);
let jobs_age = ages.get_or_insert_with("Steve Jobs", || -1);
assert_eq!(*jobs_age.value(), 65);
source

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

Returns an iterator over all entries in the map, sorted by key.

This iterator returns Entrys which can be used to access keys and their associated values.

Examples
use crossbeam_skiplist::SkipMap;

let numbers = SkipMap::new();
numbers.insert(6, "six");
numbers.insert(7, "seven");
numbers.insert(12, "twelve");

// Print then numbers from least to greatest
for entry in numbers.iter() {
    let number = entry.key();
    let number_str = entry.value();
    println!("{} is {}", number, number_str);
}
source

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

Returns an iterator over a subset of entries in the map.

This iterator returns Entrys which can be used to access keys and their associated values.

Example
use crossbeam_skiplist::SkipMap;

let numbers = SkipMap::new();
numbers.insert(6, "six");
numbers.insert(7, "seven");
numbers.insert(12, "twelve");

// Print all numbers in the map between 5 and 8.
for entry in numbers.range(5..=8) {
    let number = entry.key();
    let number_str = entry.value();
    println!("{} is {}", number, number_str);
}
source§

impl<K, V> SkipMap<K, V>
where K: Ord + Send + 'static, V: Send + 'static,

source

pub fn insert(&self, key: K, value: V) -> Entry<'_, K, V>

Inserts a key-value pair into the map and returns the new entry.

If there is an existing entry with this key, it will be removed before inserting the new one.

This function returns an Entry which can be used to access the inserted key’s associated value.

Example
use crossbeam_skiplist::SkipMap;

let map = SkipMap::new();
map.insert("key", "value");

assert_eq!(*map.get("key").unwrap().value(), "value");
source

pub fn compare_insert<F>( &self, key: K, value: V, compare_fn: F ) -> Entry<'_, K, V>
where F: Fn(&V) -> bool,

Inserts a key-value pair into the skip list and returns the new entry.

If there is an existing entry with this key and compare(entry.value) returns true, it will be removed before inserting the new one. The closure will not be called if the key is not present.

This function returns an Entry which can be used to access the inserted key’s associated value.

Example
use crossbeam_skiplist::SkipMap;

let map = SkipMap::new();
map.insert("key", 1);
map.compare_insert("key", 0, |x| x < &0);
assert_eq!(*map.get("key").unwrap().value(), 1);
map.compare_insert("key", 2, |x| x < &2);
assert_eq!(*map.get("key").unwrap().value(), 2);
map.compare_insert("absent_key", 0, |_| false);
assert_eq!(*map.get("absent_key").unwrap().value(), 0);
source

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

Removes an entry with the specified key from the map and returns it.

The value will not actually be dropped until all references to it have gone out of scope.

This function returns an Entry which can be used to access the removed key’s associated value.

Example
use crossbeam_skiplist::SkipMap;

let map: SkipMap<&str, &str> = SkipMap::new();
assert!(map.remove("invalid key").is_none());

map.insert("key", "value");
assert_eq!(*map.remove("key").unwrap().value(), "value");
source

pub fn pop_front(&self) -> Option<Entry<'_, K, V>>

Removes the entry with the lowest key from the map. Returns the removed entry.

The value will not actually be dropped until all references to it have gone out of scope.

Example
use crossbeam_skiplist::SkipMap;

let numbers = SkipMap::new();
numbers.insert(6, "six");
numbers.insert(7, "seven");
numbers.insert(12, "twelve");

assert_eq!(*numbers.pop_front().unwrap().value(), "six");
assert_eq!(*numbers.pop_front().unwrap().value(), "seven");
assert_eq!(*numbers.pop_front().unwrap().value(), "twelve");

// All entries have been removed now.
assert!(numbers.is_empty());
source

pub fn pop_back(&self) -> Option<Entry<'_, K, V>>

Removes the entry with the greatest key from the map. Returns the removed entry.

The value will not actually be dropped until all references to it have gone out of scope.

Example
use crossbeam_skiplist::SkipMap;

let numbers = SkipMap::new();
numbers.insert(6, "six");
numbers.insert(7, "seven");
numbers.insert(12, "twelve");

assert_eq!(*numbers.pop_back().unwrap().value(), "twelve");
assert_eq!(*numbers.pop_back().unwrap().value(), "seven");
assert_eq!(*numbers.pop_back().unwrap().value(), "six");

// All entries have been removed now.
assert!(numbers.is_empty());
source

pub fn clear(&self)

Removes all entries from the map.

Example
use crossbeam_skiplist::SkipMap;

let people = SkipMap::new();
people.insert("Bill", "Gates");
people.insert("Steve", "Jobs");

people.clear();
assert!(people.is_empty());

Trait Implementations§

source§

impl<K, V> Debug for SkipMap<K, V>
where K: Ord + Debug, V: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

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

source§

fn default() -> Self

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

impl<K, V> FromIterator<(K, V)> for SkipMap<K, V>
where K: Ord,

source§

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

Creates a value from an iterator. Read more
source§

impl<'a, K, V> IntoIterator for &'a SkipMap<K, V>
where K: Ord,

§

type Item = Entry<'a, K, V>

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Iter<'a, K, V>

Creates an iterator from a value. Read more
source§

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

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> IntoIter<K, V>

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<K, V> !RefUnwindSafe for SkipMap<K, V>

§

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

§

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

§

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

§

impl<K, V> !UnwindSafe for SkipMap<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> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.