Skip to main content

SkipSet

Struct SkipSet 

Source
pub struct SkipSet<T, C = BasicComparator, A: SkiplistAllocator = TursoAllocator> { /* private fields */ }
Expand description

A set based on a lock-free skip list.

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

A custom comparator may be provided, causing all elements to be ordered by the comparison function used instead of the standard Ord impl. See Comparator.

Implementations§

Source§

impl<T> SkipSet<T>

Source

pub fn new() -> Self

Returns a new, empty set with the default comparator.

§Example
use turso_core::skiplist::SkipSet;

let set: SkipSet<i32> = SkipSet::new();
Source§

impl<T, A: SkiplistAllocator> SkipSet<T, BasicComparator, A>

Source

pub fn new_in(alloc: A) -> Self

Returns a new, empty set with the default comparator that allocates its nodes in alloc.

§Example
use turso_core::alloc::TursoAllocator;
use turso_core::skiplist::SkipSet;

let set: SkipSet<i32, _, TursoAllocator> = SkipSet::new_in(TursoAllocator);
Source§

impl<T, C> SkipSet<T, C>

Source

pub fn with_comparator(comparator: C) -> Self

Returns a new, empty set with the given comparator.

§Example
use turso_core::skiplist::{SkipSet, comparator::BasicComparator};

let set: SkipSet<i32> = SkipSet::with_comparator(BasicComparator);
Source§

impl<T, C, A: SkiplistAllocator> SkipSet<T, C, A>

Source

pub fn with_comparator_in(comparator: C, alloc: A) -> Self

Returns a new, empty set with the given comparator that allocates its nodes in alloc.

§Example
use turso_core::alloc::TursoAllocator;
use turso_core::skiplist::{SkipSet, comparator::BasicComparator};

let set: SkipSet<i32, _, TursoAllocator> =
    SkipSet::with_comparator_in(BasicComparator, TursoAllocator);
Source

pub fn is_empty(&self) -> bool

Returns true if the set is empty.

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
assert!(set.is_empty());

set.insert(1);
assert!(!set.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of entries in the set.

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

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
assert_eq!(set.len(), 0);

set.insert(1);
assert_eq!(set.len(), 1);
Source§

impl<T, C, A: SkiplistAllocator> SkipSet<T, C, A>
where C: Comparator<T>,

Source

pub fn front(&self) -> Option<Entry<'_, T, C, A>>

Returns the entry with the smallest key.

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
set.insert(1);
assert_eq!(*set.front().unwrap(), 1);
set.insert(2);
assert_eq!(*set.front().unwrap(), 1);
Source

pub fn back(&self) -> Option<Entry<'_, T, C, A>>

Returns the entry with the largest key.

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
set.insert(1);
assert_eq!(*set.back().unwrap(), 1);
set.insert(2);
assert_eq!(*set.back().unwrap(), 2);
Source

pub fn contains<Q>(&self, key: &Q) -> bool
where C: Comparator<T, Q>, Q: ?Sized,

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

§Example
use turso_core::skiplist::SkipSet;

let set: SkipSet<_> = (1..=3).collect();
assert!(set.contains(&1));
assert!(!set.contains(&4));
Source

pub fn get<Q>(&self, key: &Q) -> Option<Entry<'_, T, C, A>>
where C: Comparator<T, Q>, Q: ?Sized,

Returns an entry with the specified key.

§Example
use turso_core::skiplist::SkipSet;

let set: SkipSet<_> = (1..=3).collect();
assert_eq!(*set.get(&3).unwrap(), 3);
assert!(set.get(&4).is_none());
Source

pub fn lower_bound<'a, Q>( &'a self, bound: Bound<&Q>, ) -> Option<Entry<'a, T, C, A>>
where C: Comparator<T, 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.

§Example
use turso_core::skiplist::SkipSet;
use std::ops::Bound::*;

let set = SkipSet::new();
set.insert(6);
set.insert(7);
set.insert(12);

let greater_than_five = set.lower_bound(Excluded(&5)).unwrap();
assert_eq!(*greater_than_five, 6);

let greater_than_six = set.lower_bound(Excluded(&6)).unwrap();
assert_eq!(*greater_than_six, 7);

let greater_than_thirteen = set.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, T, C, A>>
where C: Comparator<T, 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.

§Example
use turso_core::skiplist::SkipSet;
use std::ops::Bound::*;

let set = SkipSet::new();
set.insert(6);
set.insert(7);
set.insert(12);

let less_than_eight = set.upper_bound(Excluded(&8)).unwrap();
assert_eq!(*less_than_eight, 7);

let less_than_six = set.upper_bound(Excluded(&6));
assert!(less_than_six.is_none());
Source

pub fn get_or_insert(&self, key: T) -> Entry<'_, T, C, A>

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

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
let entry = set.get_or_insert(2);
assert_eq!(*entry, 2);
Source

pub fn try_get_or_insert( &self, key: T, ) -> Result<Entry<'_, T, C, A>, TryReserveError>

Fallible version of get_or_insert: returns an error instead of aborting the process when node allocation fails.

On error the set is unchanged and key is dropped.

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
let entry = set.try_get_or_insert(2).unwrap();
assert_eq!(*entry, 2);
Source

pub fn iter(&self) -> Iter<'_, T, C, A>

Returns an iterator over all entries in the set.

§Examples
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
set.insert(6);
set.insert(7);
set.insert(12);

let mut set_iter = set.iter();
assert_eq!(*set_iter.next().unwrap(), 6);
assert_eq!(*set_iter.next().unwrap(), 7);
assert_eq!(*set_iter.next().unwrap(), 12);
assert!(set_iter.next().is_none());
Source

pub fn range<Q, R>(&self, range: R) -> Range<'_, Q, R, T, C, A>
where R: RangeBounds<Q>, C: Comparator<T, Q>, Q: ?Sized,

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

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
set.insert(6);
set.insert(7);
set.insert(12);

let mut set_range = set.range(5..=8);
assert_eq!(*set_range.next().unwrap(), 6);
assert_eq!(*set_range.next().unwrap(), 7);
assert!(set_range.next().is_none());
Source§

impl<T, C, A: SkiplistAllocator> SkipSet<T, C, A>
where C: Comparator<T>, T: Send + 'static,

Source

pub fn insert(&self, key: T) -> Entry<'_, T, C, A>

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

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

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
set.insert(2);
assert_eq!(*set.get(&2).unwrap(), 2);
Source

pub fn try_insert(&self, key: T) -> Result<Entry<'_, T, C, A>, TryReserveError>

Fallible version of insert: returns an error instead of aborting the process when node allocation fails.

On error the set is unchanged and key is dropped.

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
set.try_insert(2).unwrap();
assert_eq!(*set.get(&2).unwrap(), 2);
Source

pub fn remove<Q>(&self, key: &Q) -> Option<Entry<'_, T, C, A>>
where C: Comparator<T, Q>, Q: ?Sized,

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

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

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
set.insert(2);
assert_eq!(*set.remove(&2).unwrap(), 2);
assert!(set.remove(&2).is_none());
Source

pub fn pop_front(&self) -> Option<Entry<'_, T, C, A>>

Removes an entry from the front of the set. 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::SkipSet;

let set = SkipSet::new();
set.insert(1);
set.insert(2);

assert_eq!(*set.pop_front().unwrap(), 1);
assert_eq!(*set.pop_front().unwrap(), 2);

// All entries have been removed now.
assert!(set.is_empty());
Source

pub fn pop_back(&self) -> Option<Entry<'_, T, C, A>>

Removes an entry from the back of the set. 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::SkipSet;

let set = SkipSet::new();
set.insert(1);
set.insert(2);

assert_eq!(*set.pop_back().unwrap(), 2);
assert_eq!(*set.pop_back().unwrap(), 1);

// All entries have been removed now.
assert!(set.is_empty());
Source

pub fn clear(&self)

Iterates over the set and removes every entry.

§Example
use turso_core::skiplist::SkipSet;

let set = SkipSet::new();
set.insert(1);
set.insert(2);

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

Trait Implementations§

Source§

impl<T, C, A: SkiplistAllocator> Debug for SkipSet<T, C, A>
where C: Comparator<T>, T: Debug,

Source§

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

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

impl<T, C> Default for SkipSet<T, C>
where C: Default,

Source§

fn default() -> Self

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

impl<T, C> FromIterator<T> for SkipSet<T, C>
where C: Comparator<T> + Default,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<T, C, A: SkiplistAllocator> IntoIterator for SkipSet<T, C, A>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T, A>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T, C, A: SkiplistAllocator> IntoIterator for &'a SkipSet<T, C, A>
where C: Comparator<T>,

Source§

type Item = Entry<'a, T, C, A>

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, C, A>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T, C = BasicComparator, A = TursoAllocator> !Freeze for SkipSet<T, C, A>

§

impl<T, C = BasicComparator, A = TursoAllocator> !RefUnwindSafe for SkipSet<T, C, A>

§

impl<T, C = BasicComparator, A = TursoAllocator> !UnwindSafe for SkipSet<T, C, A>

§

impl<T, C, A> Send for SkipSet<T, C, A>
where SkipMap<T, (), C, A>: Send,

§

impl<T, C, A> Sync for SkipSet<T, C, A>
where SkipMap<T, (), C, A>: Sync,

§

impl<T, C, A> Unpin for SkipSet<T, C, A>
where SkipMap<T, (), C, A>: Unpin,

§

impl<T, C, A> UnsafeUnpin for SkipSet<T, C, A>
where SkipMap<T, (), C, A>: UnsafeUnpin,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<'a, I> MultiOps<&'a RoaringBitmap> for I
where I: IntoIterator<Item = &'a RoaringBitmap>,

Source§

type Output = RoaringBitmap

The type of output from operations.
Source§

fn union(self) -> <I as MultiOps<&'a RoaringBitmap>>::Output

The union between all elements.
Source§

fn intersection(self) -> <I as MultiOps<&'a RoaringBitmap>>::Output

The intersection between all elements.
Source§

fn difference(self) -> <I as MultiOps<&'a RoaringBitmap>>::Output

The difference between all elements.
Source§

fn symmetric_difference(self) -> <I as MultiOps<&'a RoaringBitmap>>::Output

The symmetric difference between all elements.
Source§

impl<'a, I> MultiOps<&'a RoaringTreemap> for I
where I: IntoIterator<Item = &'a RoaringTreemap>,

Source§

type Output = RoaringTreemap

The type of output from operations.
Source§

fn union(self) -> <I as MultiOps<&'a RoaringTreemap>>::Output

The union between all elements.
Source§

fn intersection(self) -> <I as MultiOps<&'a RoaringTreemap>>::Output

The intersection between all elements.
Source§

fn difference(self) -> <I as MultiOps<&'a RoaringTreemap>>::Output

The difference between all elements.
Source§

fn symmetric_difference(self) -> <I as MultiOps<&'a RoaringTreemap>>::Output

The symmetric difference between all elements.
Source§

impl<'a, I, E> MultiOps<Result<&'a RoaringBitmap, E>> for I
where E: 'a, I: IntoIterator<Item = Result<&'a RoaringBitmap, E>>,

Source§

type Output = Result<RoaringBitmap, E>

The type of output from operations.
Source§

fn union(self) -> <I as MultiOps<Result<&'a RoaringBitmap, E>>>::Output

The union between all elements.
Source§

fn intersection(self) -> <I as MultiOps<Result<&'a RoaringBitmap, E>>>::Output

The intersection between all elements.
Source§

fn difference(self) -> <I as MultiOps<Result<&'a RoaringBitmap, E>>>::Output

The difference between all elements.
Source§

fn symmetric_difference( self, ) -> <I as MultiOps<Result<&'a RoaringBitmap, E>>>::Output

The symmetric difference between all elements.
Source§

impl<'a, I, E> MultiOps<Result<&'a RoaringTreemap, E>> for I
where E: 'a, I: IntoIterator<Item = Result<&'a RoaringTreemap, E>>,

Source§

type Output = Result<RoaringTreemap, E>

The type of output from operations.
Source§

fn union(self) -> <I as MultiOps<Result<&'a RoaringTreemap, E>>>::Output

The union between all elements.
Source§

fn intersection(self) -> <I as MultiOps<Result<&'a RoaringTreemap, E>>>::Output

The intersection between all elements.
Source§

fn difference(self) -> <I as MultiOps<Result<&'a RoaringTreemap, E>>>::Output

The difference between all elements.
Source§

fn symmetric_difference( self, ) -> <I as MultiOps<Result<&'a RoaringTreemap, E>>>::Output

The symmetric difference between all elements.
Source§

impl<I, E> MultiOps<Result<RoaringBitmap, E>> for I
where I: IntoIterator<Item = Result<RoaringBitmap, E>>,

Source§

type Output = Result<RoaringBitmap, E>

The type of output from operations.
Source§

fn union(self) -> <I as MultiOps<Result<RoaringBitmap, E>>>::Output

The union between all elements.
Source§

fn intersection(self) -> <I as MultiOps<Result<RoaringBitmap, E>>>::Output

The intersection between all elements.
Source§

fn difference(self) -> <I as MultiOps<Result<RoaringBitmap, E>>>::Output

The difference between all elements.
Source§

fn symmetric_difference( self, ) -> <I as MultiOps<Result<RoaringBitmap, E>>>::Output

The symmetric difference between all elements.
Source§

impl<I, E> MultiOps<Result<RoaringTreemap, E>> for I
where I: IntoIterator<Item = Result<RoaringTreemap, E>>,

Source§

type Output = Result<RoaringTreemap, E>

The type of output from operations.
Source§

fn union(self) -> <I as MultiOps<Result<RoaringTreemap, E>>>::Output

The union between all elements.
Source§

fn intersection(self) -> <I as MultiOps<Result<RoaringTreemap, E>>>::Output

The intersection between all elements.
Source§

fn difference(self) -> <I as MultiOps<Result<RoaringTreemap, E>>>::Output

The difference between all elements.
Source§

fn symmetric_difference( self, ) -> <I as MultiOps<Result<RoaringTreemap, E>>>::Output

The symmetric difference between all elements.
Source§

impl<I> MultiOps<RoaringBitmap> for I
where I: IntoIterator<Item = RoaringBitmap>,

Source§

type Output = RoaringBitmap

The type of output from operations.
Source§

fn union(self) -> <I as MultiOps<RoaringBitmap>>::Output

The union between all elements.
Source§

fn intersection(self) -> <I as MultiOps<RoaringBitmap>>::Output

The intersection between all elements.
Source§

fn difference(self) -> <I as MultiOps<RoaringBitmap>>::Output

The difference between all elements.
Source§

fn symmetric_difference(self) -> <I as MultiOps<RoaringBitmap>>::Output

The symmetric difference between all elements.
Source§

impl<I> MultiOps<RoaringTreemap> for I
where I: IntoIterator<Item = RoaringTreemap>,

Source§

type Output = RoaringTreemap

The type of output from operations.
Source§

fn union(self) -> <I as MultiOps<RoaringTreemap>>::Output

The union between all elements.
Source§

fn intersection(self) -> <I as MultiOps<RoaringTreemap>>::Output

The intersection between all elements.
Source§

fn difference(self) -> <I as MultiOps<RoaringTreemap>>::Output

The difference between all elements.
Source§

fn symmetric_difference(self) -> <I as MultiOps<RoaringTreemap>>::Output

The symmetric difference between all elements.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more