Skip to main content

BTreeIndexMultiSet

Struct BTreeIndexMultiSet 

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

BTree-based index multi-set.

This is a thin wrapper around BTreeIndexMultiMap<T, ()> providing a multi-set API: a value can occur any number of times, every occurrence keeps a stable index until it (or an earlier entry) is removed, and occurrences can be iterated either in insertion order or in sorted order.

§Example

use btree_indexmap::BTreeIndexMultiSet;

let mut set = BTreeIndexMultiSet::new();

// Multiple occurrences of the same value, each with its own stable
// index.
set.push_back("apple");
set.push_back("carrot");
set.push_back("apple");

assert_eq!(set.count("apple"), 2);

let apples: Vec<_> = set.get("apple").copied().collect();
assert_eq!(apples, ["apple", "apple"]);

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

Implementations§

Source§

impl<T> BTreeIndexMultiSet<T>

Source

pub fn new() -> Self

Creates a new, empty multi-set.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new, empty multi-set with space reserved for at least capacity occurrences.

Source

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

Creates a new multi-set containing the single value.

Source

pub fn clear(&mut self)

Removes all occurrences from the set.

This keeps the capacity allocated for the occurrences.

Source

pub fn capacity(&self) -> usize

Returns the number of occurrences the set can hold without reallocating.

Source

pub fn is_empty(&self) -> bool

Returns true if the set contains no occurrences.

Source

pub fn len(&self) -> usize

Returns the total number of occurrences 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 first(&self) -> Option<&T>

Returns a reference to the first occurrence in the set (in insertion order), if any.

Source

pub fn last(&self) -> Option<&T>

Returns a reference to the last occurrence in the set (in insertion order), if any.

Source

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

Returns true if the set contains at least one occurrence of value.

Source

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

Returns the number of occurrences of value in the set.

Source

pub fn indexes_of<Q>(&self, value: &Q) -> IndexesIter<'_> ⓘ
where Q: ?Sized + Comparable<T>,

Returns an iterator over the indexes of every occurrence of value.

Source

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

Returns the index of the first occurrence of value.

Source

pub fn get_indexed<Q>(&self, value: &Q) -> GetIndexed<'_, T> ⓘ
where Q: ?Sized + Comparable<T>,

Returns an iterator over (index, &value) for every occurrence of value.

Source

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

Returns an iterator over a reference to value for every occurrence found in the set.

Source

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

Inserts a new occurrence of value at the end of the set.

Returns the index of the new occurrence, and whether value was not already present in the set.

Source

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

Inserts a new occurrence of value at the front of the set.

Returns whether value was not already present in the set.

Source

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

Inserts a new occurrence of value at the given index.

Returns whether value was not already present in the set.

§Panics

Panics if index > len.

Source

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

Inserts a new occurrence of value in the set, without removing any existing occurrence.

This is an alias for Self::push_back.

Source

pub fn shift_remove_at(&mut self, index: usize) -> Option<T>
where T: Ord,

Removes the occurrence at the given index, shifting every following occurrence one index down.

Source

pub fn swap_remove_at(&mut self, index: usize) -> Option<T>
where T: Ord,

Removes the occurrence at the given index, moving the last occurrence into its place.

Source

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

Removes every occurrence of value, returning an iterator over the removed occurrences.

Shifts the remaining occurrences down to fill the gaps.

Source

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

Removes every occurrence of value, returning an iterator over the removed occurrences.

Moves occurrences from the end of the set into the gaps.

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.

Source

pub fn sort(&mut self)
where T: Ord,

Sorts the entries of the set by value.

The relative order of equal values is unchanged.

Trait Implementations§

Source§

impl<T: Clone> Clone for BTreeIndexMultiSet<T>

Source§

fn clone(&self) -> BTreeIndexMultiSet<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 BTreeIndexMultiSet<T>

Source§

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

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

impl<T> Default for BTreeIndexMultiSet<T>

Source§

fn default() -> Self

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

impl<T: Eq> Eq for BTreeIndexMultiSet<T>

Source§

impl<T: Ord> Extend<T> for BTreeIndexMultiSet<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 BTreeIndexMultiSet<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 BTreeIndexMultiSet<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 BTreeIndexMultiSet<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 BTreeIndexMultiSet<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 BTreeIndexMultiSet<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 BTreeIndexMultiSet<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 BTreeIndexMultiSet<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 BTreeIndexMultiSet<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.