Skip to main content

BTreeIndexSet

Struct BTreeIndexSet 

Source
pub struct BTreeIndexSet<T> { /* private fields */ }
Expand description

BTree-based index set.

This is a thin wrapper around BTreeIndexMap<T, ()> providing a set-like API: every value is stored at most once, values keep a stable index until they (or an earlier value) are removed, and values can be iterated either in insertion order or in sorted order.

§Example

use btree_indexmap::BTreeIndexSet;

let mut set = BTreeIndexSet::new();

// Values are stored in insertion order and assigned a stable index.
assert!(set.insert("banana"));
assert!(set.insert("apple"));
assert!(!set.insert("banana")); // already present

// O(1) access by index (insertion order).
assert_eq!(set.as_slice(), ["banana", "apple"]);

// O(log n) lookup, returning the stable index alongside the value.
assert_eq!(set.get_full("apple"), Some((1, &"apple")));

// Iterate in sorted order.
let sorted: Vec<_> = set.iter_sorted().copied().collect();
assert_eq!(sorted, ["apple", "banana"]);

Implementations§

Source§

impl<T> BTreeIndexSet<T>

Source

pub fn new() -> Self

Creates a new, empty set.

Source

pub fn singleton(value: T) -> Self
where T: Ord,

Creates a new set containing the single value.

Source

pub fn clear(&mut self)

Removes all values from the set.

This keeps the capacity allocated for the values.

Source

pub fn is_empty(&self) -> bool

Returns true if the set contains no values.

Source

pub fn len(&self) -> usize

Returns the number of values in the set.

Source

pub fn as_slice(&self) -> &[T]

Returns the values of the set as a slice, in insertion (index) order.

Source

pub fn get_full<Q>(&self, value: &Q) -> Option<(usize, &T)>
where Q: ?Sized + Comparable<T>,

Returns the index and a reference to the value equal to value, if any.

Source

pub fn get_index_of<Q>(&self, value: &Q) -> Option<usize>
where Q: ?Sized + Comparable<T>,

Returns the index of value in the set, if any.

Source

pub fn get<Q>(&self, value: &Q) -> Option<&T>
where Q: ?Sized + Comparable<T>,

Returns a reference to the value equal to value, if any.

Source

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

Returns true if the set contains value.

Source

pub fn insert_full(&mut self, value: T) -> (usize, bool)
where T: Ord,

Inserts value in the set, returning its index and whether it was newly inserted (true) or already present (false).

Source

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

Inserts value in the set, returning whether it was newly inserted.

Source

pub fn shift_remove_full<Q>(&mut self, value: &Q) -> Option<(usize, T)>
where Q: ?Sized + Comparable<T>,

Removes value from the set, returning its former index and the value itself, if it was present.

Shifts every following value one index down.

Source

pub fn shift_take<Q>(&mut self, value: &Q) -> Option<T>
where Q: ?Sized + Comparable<T>,

Removes value from the set, returning the removed value, if any.

Shifts every following value one index down.

Source

pub fn shift_remove<Q>(&mut self, value: &Q) -> bool
where Q: ?Sized + Comparable<T>,

Removes value from the set, returning whether the value was present.

Shifts every following value one index down.

Source

pub fn swap_remove_full<Q>(&mut self, value: &Q) -> Option<(usize, T)>
where Q: ?Sized + Comparable<T>, T: Ord,

Removes value from the set, returning its former index and the value itself, if it was present.

Moves the last value into the removed slot.

Source

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

Removes value from the set, returning the removed value, if any.

Moves the last value into its place.

Source

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

Removes value from the set, returning whether the value was present.

Moves the last value into its place.

Source

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

Returns an iterator over the values of the set, in insertion (index) order.

Source

pub fn iter_sorted(&self) -> IterSorted<'_, T> ⓘ

Returns an iterator over the values of the set, sorted according to T’s implementation of Ord.

Source

pub fn into_iter_sorted(self) -> IntoIterSorted<T> ⓘ

Turns this set into an iterator over its values, sorted according to T’s implementation of Ord.

Trait Implementations§

Source§

impl<T: Clone> Clone for BTreeIndexSet<T>

Source§

fn clone(&self) -> BTreeIndexSet<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for BTreeIndexSet<T>

Source§

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

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

impl<T> Default for BTreeIndexSet<T>

Source§

fn default() -> Self

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

impl<T: Eq> Eq for BTreeIndexSet<T>

Source§

impl<T: Ord> Extend<T> for BTreeIndexSet<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: T)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T: Ord> FromIterator<T> for BTreeIndexSet<T>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for BTreeIndexSet<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<usize> for BTreeIndexSet<T>

Source§

fn index(&self, index: usize) -> &Self::Output

§Panics

Panics if index is out of bounds.

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> IntoIterator for BTreeIndexSet<T>

Source§

type IntoIter = IntoIter<T>

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

type Item = T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a BTreeIndexSet<T>

Source§

type IntoIter = Iter<'a, T>

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

type Item = &'a T

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Ord> Ord for BTreeIndexSet<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<T: PartialEq> PartialEq for BTreeIndexSet<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialOrd> PartialOrd for BTreeIndexSet<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.