Skip to main content

BTreeSet

Struct BTreeSet 

Source
pub struct BTreeSet<T, Node = Vec<T>>
where T: Ord + Clone + 'static, Node: NodeLike<T>,
{ /* private fields */ }
Expand description

A persistent concurrent ordered set based on a B-Tree.

See [BTreeMap]’s documentation for a detailed discussion of this collection’s performance benefits and drawbacks.

It is a logic error for an item to be modified in such a way that the item’s ordering relative to any other item, as determined by the Ord trait, changes while it is in the set. This is normally only possible through Cell, RefCell, global state, I/O, or unsafe code. The behavior resulting from such a logic error is not specified, but will be encapsulated to the BTreeSet that observed the logic error and not result in undefined behavior. This could include panics, incorrect results, aborts, memory leaks, and non-termination.

Iterators returned by crate::BTreeSet::iter produce their items in order, and take worst-case logarithmic and amortized constant time per item returned.

§Examples

use wt_indexset::concurrent::set::BTreeSet;

// Type inference lets us omit an explicit type signature (which
// would be `BTreeSet<&str>` in this example).
let mut books = BTreeSet::<&str>::new();

// Add some books.
books.insert("A Dance With Dragons");
books.insert("To Kill a Mockingbird");
books.insert("The Odyssey");
books.insert("The Great Gatsby");

// Check for a specific one.
if !books.contains("The Winds of Winter") {
    println!("We have {} books, but The Winds of Winter ain't one.",
             books.len());
}

// Remove a book.
books.remove("The Odyssey");

// Iterate over everything.
for book in &books {
    println!("{book}");
}

A BTreeSet with a known list of items can be initialized from an array:

use wt_indexset::concurrent::set::BTreeSet;

let set = BTreeSet::from_iter([1, 2, 3]);

Implementations§

Source§

impl<T, Node> BTreeSet<T, Node>
where T: Debug + Ord + Clone + Send, Node: NodeLike<T> + Send + 'static,

Source

pub fn new() -> Self

Source

pub fn with_maximum_node_size(node_capacity: usize) -> Self

Makes a new, empty BTreeSet with the given maximum node size. Allocates one vec with the capacity set to be the specified node size.

§Examples
use wt_indexset::concurrent::set::BTreeSet;

let set: BTreeSet<i32> = BTreeSet::with_maximum_node_size(128);
Source

pub fn attach_node(&self, node: Node)

Source

pub fn insert(&self, value: T) -> bool

Adds a value to the set.

Returns whether the value was newly inserted. That is:

  • If the set did not previously contain an equal value, true is returned.
  • If the set already contained an equal value, false is returned, and the entry is not updated.
§Examples
use wt_indexset::concurrent::set::BTreeSet;

let mut set = BTreeSet::<usize>::new();

assert_eq!(set.insert(2), true);
assert_eq!(set.insert(2), false);
assert_eq!(set.len(), 1);
Source

pub fn remove_cdc<Q>(&self, value: &Q) -> (Option<T>, Vec<ChangeEvent<T>>)
where T: Borrow<Q>, Q: Ord + ?Sized,

Source

pub fn remove<Q>(&self, value: &Q) -> Option<T>
where T: Borrow<Q>, Q: Ord + ?Sized,

If the set contains an element equal to the value, removes it from the set and drops it. Returns whether such an element was present.

The value may be any borrowed form of the set’s element type, but the ordering on the borrowed form must match the ordering on the element type.

§Examples
use wt_indexset::concurrent::set::BTreeSet;

let mut set = BTreeSet::<usize>::new();

set.insert(2);
assert_eq!(set.remove(&2).is_some(), true);
assert_eq!(set.remove(&2).is_some(), false);
Source

pub fn contains<Q>(&self, value: &Q) -> bool
where T: Borrow<Q>, Q: Ord + ?Sized,

Returns true if the set contains an element equal to the value.

The value may be any borrowed form of the set’s element type, but the ordering on the borrowed form must match the ordering on the element type.

§Examples
use wt_indexset::concurrent::set::BTreeSet;

let set = BTreeSet::from_iter([1, 2, 3]);
assert_eq!(set.contains(&1), true);
assert_eq!(set.contains(&4), false);
Source

pub fn get<'a, Q>(&'a self, value: &'a Q) -> Option<Ref<T, Node>>
where T: Borrow<Q>, Q: Ord + ?Sized,

Source

pub fn len(&self) -> usize

Source

pub fn capacity(&self) -> usize

Source

pub fn node_count(&self) -> usize

Source§

impl<'a, T, Node> BTreeSet<T, Node>
where T: Debug + Ord + Clone + Send + 'static, Node: NodeLike<T> + Send + 'static,

Source

pub fn iter(&'a self) -> Iter<'a, T, Node> ⓘ

Gets an iterator that visits the elements in the BTreeSet in ascending order.

§Examples
use wt_indexset::concurrent::set::BTreeSet;

let set = BTreeSet::from_iter([1, 2, 3]);
let mut set_iter = set.iter();
assert_eq!(set_iter.next(), Some(&1));
assert_eq!(set_iter.next(), Some(&2));
assert_eq!(set_iter.next(), Some(&3));
assert_eq!(set_iter.next(), None);

Values returned by the iterator are returned in ascending order:

use wt_indexset::concurrent::set::BTreeSet;

let set = BTreeSet::from_iter([3, 1, 2]);
let mut set_iter = set.iter();
assert_eq!(set_iter.next(), Some(&1));
assert_eq!(set_iter.next(), Some(&2));
assert_eq!(set_iter.next(), Some(&3));
assert_eq!(set_iter.next(), None);
Source

pub fn range<Q, R>(&'a self, range: R) -> Range<'a, T, Node> ⓘ
where T: Borrow<Q>, Q: Ord + ?Sized, R: RangeBounds<Q>,

Constructs a double-ended iterator over a sub-range of elements in the set. The simplest way is to use the range syntax min..max, thus range(min..max) will yield elements from min (inclusive) to max (exclusive). The range may also be entered as (Bound<T>, Bound<T>), so for example range((Excluded(4), Included(10))) will yield a left-exclusive, right-inclusive range from 4 to 10.

§Panics

Panics if range start > end. Panics if range start == end and both bounds are Excluded.

§Examples
use wt_indexset::concurrent::set::BTreeSet;
use std::ops::Bound::Included;

let mut set = BTreeSet::<usize>::new();
set.insert(3);
set.insert(5);
set.insert(8);
for &elem in set.range((Included(&4), Included(&8))) {
    println!("{elem}");
}
assert_eq!(Some(&5), set.range(4..).next());
Source§

impl<T> BTreeSet<T>
where T: Debug + Ord + Clone + Send + 'static,

Source

pub fn remove_range<R, Q>(&self, range: R)
where Q: Ord + ?Sized, T: Borrow<Q>, R: RangeBounds<Q>,

Trait Implementations§

Source§

impl<T, Node> Debug for BTreeSet<T, Node>
where T: Ord + Clone + 'static + Debug, Node: NodeLike<T> + Debug,

Source§

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

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

impl<T: Ord + Clone + 'static, Node: NodeLike<T>> Default for BTreeSet<T, Node>

Source§

fn default() -> Self

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

impl<T, const N: usize> From<[T; N]> for BTreeSet<T>
where T: Debug + Ord + Clone + Send,

Source§

fn from(value: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for BTreeSet<T>
where T: Debug + Ord + Clone + Send,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, T, Node> IntoIterator for &'a BTreeSet<T, Node>
where T: Debug + Ord + Send + Clone, Node: NodeLike<T> + Send + 'static,

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T, Node>

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, Node = Vec<T>> !Freeze for BTreeSet<T, Node>

§

impl<T, Node = Vec<T>> !RefUnwindSafe for BTreeSet<T, Node>

§

impl<T, Node = Vec<T>> !UnwindSafe for BTreeSet<T, Node>

§

impl<T, Node> Send for BTreeSet<T, Node>
where SkipMap<T, Arc<Mutex<RawMutex, Node>>>: Send,

§

impl<T, Node> Sync for BTreeSet<T, Node>
where SkipMap<T, Arc<Mutex<RawMutex, Node>>>: Sync,

§

impl<T, Node> Unpin for BTreeSet<T, Node>
where SkipMap<T, Arc<Mutex<RawMutex, Node>>>: Unpin,

§

impl<T, Node> UnsafeUnpin for BTreeSet<T, Node>
where SkipMap<T, Arc<Mutex<RawMutex, Node>>>: 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<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.
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, 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.