pub struct SkipMap<K, V, C = BasicComparator, A: SkiplistAllocator = TursoAllocator> { /* 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.
A custom comparator may be provided, causing all keys
to be ordered by the comparison function used instead
of the standard Ord impl. See Comparator.
Implementations§
Source§impl<K, V, A: SkiplistAllocator> SkipMap<K, V, BasicComparator, A>
impl<K, V, A: SkiplistAllocator> SkipMap<K, V, BasicComparator, A>
Source§impl<K, V, C> SkipMap<K, V, C>
impl<K, V, C> SkipMap<K, V, C>
Sourcepub fn with_comparator(comparator: C) -> Self
pub fn with_comparator(comparator: C) -> Self
Returns a new, empty map with the given comparator.
§Example
use turso_core::skiplist::{SkipMap, comparator::BasicComparator};
let map: SkipMap<i32, &str> = SkipMap::with_comparator(BasicComparator);Source§impl<K, V, C, A: SkiplistAllocator> SkipMap<K, V, C, A>
impl<K, V, C, A: SkiplistAllocator> SkipMap<K, V, C, A>
Sourcepub fn with_comparator_in(comparator: C, alloc: A) -> Self
pub fn with_comparator_in(comparator: C, alloc: A) -> Self
Returns a new, empty map with the given comparator that allocates its
nodes in alloc.
§Example
use turso_core::alloc::TursoAllocator;
use turso_core::skiplist::{SkipMap, comparator::BasicComparator};
let map: SkipMap<i32, &str, _, TursoAllocator> =
SkipMap::with_comparator_in(BasicComparator, TursoAllocator);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map is empty.
§Example
use turso_core::skiplist::SkipMap;
let map: SkipMap<&str, &str> = SkipMap::new();
assert!(map.is_empty());
map.insert("key", "value");
assert!(!map.is_empty());Sourcepub fn len(&self) -> usize
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 turso_core::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, C, A: SkiplistAllocator> SkipMap<K, V, C, A>where
C: Comparator<K>,
impl<K, V, C, A: SkiplistAllocator> SkipMap<K, V, C, A>where
C: Comparator<K>,
Sourcepub fn front(&self) -> Option<Entry<'_, K, V, C, A>>
pub fn front(&self) -> Option<Entry<'_, K, V, C, A>>
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 turso_core::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");Sourcepub fn back(&self) -> Option<Entry<'_, K, V, C, A>>
pub fn back(&self) -> Option<Entry<'_, K, V, C, A>>
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 turso_core::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");Sourcepub fn get_or_insert(&self, key: K, value: V) -> Entry<'_, K, V, C, A>
pub fn get_or_insert(&self, key: K, value: V) -> Entry<'_, K, V, C, A>
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 turso_core::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);Sourcepub fn try_get_or_insert(
&self,
key: K,
value: V,
) -> Result<Entry<'_, K, V, C, A>, TryReserveError>
pub fn try_get_or_insert( &self, key: K, value: V, ) -> Result<Entry<'_, K, V, C, A>, TryReserveError>
Fallible version of get_or_insert: returns an error instead of
aborting the process when node allocation fails.
On error the map is unchanged and both key and value are dropped.
§Example
use turso_core::skiplist::SkipMap;
let ages = SkipMap::new();
let gates_age = ages.try_get_or_insert("Bill Gates", 64).unwrap();
assert_eq!(*gates_age.value(), 64);Sourcepub fn get_or_insert_with<F>(
&self,
key: K,
value_fn: F,
) -> Entry<'_, K, V, C, A>where
F: FnOnce() -> V,
pub fn get_or_insert_with<F>(
&self,
key: K,
value_fn: F,
) -> Entry<'_, K, V, C, A>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 turso_core::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);Sourcepub fn try_get_or_insert_with<F>(
&self,
key: K,
value_fn: F,
) -> Result<Entry<'_, K, V, C, A>, TryReserveError>where
F: FnOnce() -> V,
pub fn try_get_or_insert_with<F>(
&self,
key: K,
value_fn: F,
) -> Result<Entry<'_, K, V, C, A>, TryReserveError>where
F: FnOnce() -> V,
Fallible version of get_or_insert_with: returns an error
instead of aborting the process when node allocation fails.
On error the map is unchanged and both key and the value built by value_fn are
dropped.
§Example
use turso_core::skiplist::SkipMap;
let ages = SkipMap::new();
let gates_age = ages.try_get_or_insert_with("Bill Gates", || 64).unwrap();
assert_eq!(*gates_age.value(), 64);Sourcepub fn iter(&self) -> Iter<'_, K, V, C, A> ⓘ
pub fn iter(&self) -> Iter<'_, K, V, C, A> ⓘ
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 turso_core::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§impl<K, V, C, A: SkiplistAllocator> SkipMap<K, V, C, A>where
C: Comparator<K>,
impl<K, V, C, A: SkiplistAllocator> SkipMap<K, V, C, A>where
C: Comparator<K>,
Sourcepub fn contains_key<Q>(&self, key: &Q) -> boolwhere
C: Comparator<K, Q>,
Q: ?Sized,
pub fn contains_key<Q>(&self, key: &Q) -> boolwhere
C: Comparator<K, Q>,
Q: ?Sized,
Returns true if the map contains a value for the specified key.
§Example
use turso_core::skiplist::SkipMap;
let ages = SkipMap::new();
ages.insert("Bill Gates", 64);
assert!(ages.contains_key(&"Bill Gates"));
assert!(!ages.contains_key(&"Steve Jobs"));Sourcepub fn get<Q>(&self, key: &Q) -> Option<Entry<'_, K, V, C, A>>where
C: Comparator<K, Q>,
Q: ?Sized,
pub fn get<Q>(&self, key: &Q) -> Option<Entry<'_, K, V, C, A>>where
C: Comparator<K, Q>,
Q: ?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 turso_core::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);Sourcepub fn lower_bound<'a, Q>(
&'a self,
bound: Bound<&Q>,
) -> Option<Entry<'a, K, V, C, A>>where
C: Comparator<K, Q>,
Q: ?Sized,
pub fn lower_bound<'a, Q>(
&'a self,
bound: Bound<&Q>,
) -> Option<Entry<'a, K, V, C, A>>where
C: Comparator<K, Q>,
Q: ?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 turso_core::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());Sourcepub fn upper_bound<'a, Q>(
&'a self,
bound: Bound<&Q>,
) -> Option<Entry<'a, K, V, C, A>>where
C: Comparator<K, Q>,
Q: ?Sized,
pub fn upper_bound<'a, Q>(
&'a self,
bound: Bound<&Q>,
) -> Option<Entry<'a, K, V, C, A>>where
C: Comparator<K, Q>,
Q: ?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 turso_core::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());Sourcepub fn range<Q, R>(&self, range: R) -> Range<'_, Q, R, K, V, C, A> ⓘ
pub fn range<Q, R>(&self, range: R) -> Range<'_, Q, R, K, V, C, A> ⓘ
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 turso_core::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, C, A: SkiplistAllocator> SkipMap<K, V, C, A>
impl<K, V, C, A: SkiplistAllocator> SkipMap<K, V, C, A>
Sourcepub fn insert(&self, key: K, value: V) -> Entry<'_, K, V, C, A>
pub fn insert(&self, key: K, value: V) -> Entry<'_, K, V, C, A>
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 turso_core::skiplist::SkipMap;
let map = SkipMap::new();
map.insert("key", "value");
assert_eq!(*map.get("key").unwrap().value(), "value");Sourcepub fn try_insert(
&self,
key: K,
value: V,
) -> Result<Entry<'_, K, V, C, A>, TryReserveError>
pub fn try_insert( &self, key: K, value: V, ) -> Result<Entry<'_, K, V, C, A>, TryReserveError>
Fallible version of insert: returns an error instead of aborting the
process when node allocation fails.
On error the map is unchanged and both key and value are dropped.
§Example
use turso_core::skiplist::SkipMap;
let map = SkipMap::new();
map.try_insert("key", "value").unwrap();
assert_eq!(*map.get("key").unwrap().value(), "value");Sourcepub fn compare_insert<F>(
&self,
key: K,
value: V,
compare_fn: F,
) -> Entry<'_, K, V, C, A>
pub fn compare_insert<F>( &self, key: K, value: V, compare_fn: F, ) -> Entry<'_, K, V, C, A>
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 turso_core::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);Sourcepub fn try_compare_insert<F>(
&self,
key: K,
value: V,
compare_fn: F,
) -> Result<Entry<'_, K, V, C, A>, TryReserveError>
pub fn try_compare_insert<F>( &self, key: K, value: V, compare_fn: F, ) -> Result<Entry<'_, K, V, C, A>, TryReserveError>
Fallible version of compare_insert: returns an error instead of
aborting the process when node allocation fails.
On error the map is unchanged and both key and value are dropped.
§Example
use turso_core::skiplist::SkipMap;
let map = SkipMap::new();
map.try_insert("key", 1).unwrap();
map.try_compare_insert("key", 2, |x| x < &2).unwrap();
assert_eq!(*map.get("key").unwrap().value(), 2);Sourcepub fn remove<Q>(&self, key: &Q) -> Option<Entry<'_, K, V, C, A>>where
C: Comparator<K, Q>,
Q: ?Sized,
pub fn remove<Q>(&self, key: &Q) -> Option<Entry<'_, K, V, C, A>>where
C: Comparator<K, Q>,
Q: ?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 turso_core::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");Sourcepub fn pop_front(&self) -> Option<Entry<'_, K, V, C, A>>
pub fn pop_front(&self) -> Option<Entry<'_, K, V, C, A>>
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 turso_core::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());Sourcepub fn pop_back(&self) -> Option<Entry<'_, K, V, C, A>>
pub fn pop_back(&self) -> Option<Entry<'_, K, V, C, A>>
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 turso_core::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());Trait Implementations§
Source§impl<K, V, C, A: SkiplistAllocator> Debug for SkipMap<K, V, C, A>
impl<K, V, C, A: SkiplistAllocator> Debug for SkipMap<K, V, C, A>
Source§impl<K, V, C> FromIterator<(K, V)> for SkipMap<K, V, C>where
C: Comparator<K> + Default,
impl<K, V, C> FromIterator<(K, V)> for SkipMap<K, V, C>where
C: Comparator<K> + Default,
Source§impl<K, V, C, A: SkiplistAllocator> IntoIterator for SkipMap<K, V, C, A>
impl<K, V, C, A: SkiplistAllocator> IntoIterator for SkipMap<K, V, C, A>
Source§impl<'a, K, V, C, A: SkiplistAllocator> IntoIterator for &'a SkipMap<K, V, C, A>where
C: Comparator<K>,
impl<'a, K, V, C, A: SkiplistAllocator> IntoIterator for &'a SkipMap<K, V, C, A>where
C: Comparator<K>,
Auto Trait Implementations§
impl<K, V, C = BasicComparator, A = TursoAllocator> !Freeze for SkipMap<K, V, C, A>
impl<K, V, C = BasicComparator, A = TursoAllocator> !RefUnwindSafe for SkipMap<K, V, C, A>
impl<K, V, C = BasicComparator, A = TursoAllocator> !UnwindSafe for SkipMap<K, V, C, A>
impl<K, V, C, A> Send for SkipMap<K, V, C, A>
impl<K, V, C, A> Sync for SkipMap<K, V, C, A>
impl<K, V, C, A> Unpin for SkipMap<K, V, C, A>
impl<K, V, C, A> UnsafeUnpin for SkipMap<K, V, C, A>where
SkipList<K, V, C, A>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more