pub struct OrdSet<A> { /* private fields */ }
Expand description
An ordered set.
An immutable ordered set implemented as a [B-tree] 1.
Most operations on this type of set are O(log n). A
HashSet
is usually a better choice for
performance, but the OrdSet
has the advantage of only requiring
an Ord
constraint on its values, and of being
ordered, so values always come out from lowest to highest, where a
HashSet
has no guaranteed ordering.
Implementations§
source§impl<A> OrdSet<A>
impl<A> OrdSet<A>
sourcepub fn unit(a: A) -> Self
pub fn unit(a: A) -> Self
Construct a set with a single value.
§Examples
let set = OrdSet::unit(123);
assert!(set.contains(&123));
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Test whether a set is empty.
Time: O(1)
§Examples
assert!(
!ordset![1, 2, 3].is_empty()
);
assert!(
OrdSet::<i32>::new().is_empty()
);
sourcepub fn ptr_eq(&self, other: &Self) -> bool
pub fn ptr_eq(&self, other: &Self) -> bool
Test whether two sets refer to the same content in memory.
This is true if the two sides are references to the same set, or if the two sets refer to the same root node.
This would return true if you’re comparing a set to itself, or if you’re comparing a set to a fresh clone of itself.
Time: O(1)
source§impl<A> OrdSet<A>where
A: Ord,
impl<A> OrdSet<A>where
A: Ord,
sourcepub fn get_min(&self) -> Option<&A>
pub fn get_min(&self) -> Option<&A>
Get the smallest value in a set.
If the set is empty, returns None
.
Time: O(log n)
sourcepub fn get_max(&self) -> Option<&A>
pub fn get_max(&self) -> Option<&A>
Get the largest value in a set.
If the set is empty, returns None
.
Time: O(log n)
sourcepub fn range<R, BA>(&self, range: R) -> RangedIter<'_, A> ⓘ
pub fn range<R, BA>(&self, range: R) -> RangedIter<'_, A> ⓘ
Create an iterator over a range inside the set.
sourcepub fn diff<'a>(&'a self, other: &'a Self) -> DiffIter<'_, A> ⓘ
pub fn diff<'a>(&'a self, other: &'a Self) -> DiffIter<'_, A> ⓘ
Get an iterator over the differences between this set and another, i.e. the set of entries to add or remove to this set in order to make it equal to the other set.
This function will avoid visiting nodes which are shared between the two sets, meaning that even very large sets can be compared quickly if most of their structure is shared.
Time: O(n) (where n is the number of unique elements across the two sets, minus the number of elements belonging to nodes shared between them)
sourcepub fn contains<BA>(&self, a: &BA) -> bool
pub fn contains<BA>(&self, a: &BA) -> bool
Test if a value is part of a set.
Time: O(log n)
§Examples
let mut set = ordset!{1, 2, 3};
assert!(set.contains(&1));
assert!(!set.contains(&4));
sourcepub fn get_prev(&self, key: &A) -> Option<&A>
pub fn get_prev(&self, key: &A) -> Option<&A>
Get the closest smaller value in a set to a given value.
If the set contains the given value, this is returned.
Otherwise, the closest value in the set smaller than the
given value is returned. If the smallest value in the set
is larger than the given value, None
is returned.
§Examples
let set = ordset![1, 3, 5, 7, 9];
assert_eq!(Some(&5), set.get_prev(&6));
sourcepub fn get_next(&self, key: &A) -> Option<&A>
pub fn get_next(&self, key: &A) -> Option<&A>
Get the closest larger value in a set to a given value.
If the set contains the given value, this is returned.
Otherwise, the closest value in the set larger than the
given value is returned. If the largest value in the set
is smaller than the given value, None
is returned.
§Examples
let set = ordset![1, 3, 5, 7, 9];
assert_eq!(Some(&5), set.get_next(&4));
sourcepub fn is_subset<RS>(&self, other: RS) -> boolwhere
RS: Borrow<Self>,
pub fn is_subset<RS>(&self, other: RS) -> boolwhere
RS: Borrow<Self>,
Test whether a set is a subset of another set, meaning that all values in our set must also be in the other set.
Time: O(n log m) where m is the size of the other set
sourcepub fn is_proper_subset<RS>(&self, other: RS) -> boolwhere
RS: Borrow<Self>,
pub fn is_proper_subset<RS>(&self, other: RS) -> boolwhere
RS: Borrow<Self>,
Test whether a set is a proper subset of another set, meaning that all values in our set must also be in the other set. A proper subset must also be smaller than the other set.
Time: O(n log m) where m is the size of the other set
source§impl<A> OrdSet<A>
impl<A> OrdSet<A>
sourcepub fn insert(&mut self, a: A) -> Option<A>
pub fn insert(&mut self, a: A) -> Option<A>
Insert a value into a set.
Time: O(log n)
§Examples
let mut set = ordset!{};
set.insert(123);
set.insert(456);
assert_eq!(
set,
ordset![123, 456]
);
sourcepub fn remove_min(&mut self) -> Option<A>
pub fn remove_min(&mut self) -> Option<A>
Remove the smallest value from a set.
Time: O(log n)
sourcepub fn remove_max(&mut self) -> Option<A>
pub fn remove_max(&mut self) -> Option<A>
Remove the largest value from a set.
Time: O(log n)
sourcepub fn update(&self, a: A) -> Self
pub fn update(&self, a: A) -> Self
Construct a new set from the current set with the given value added.
Time: O(log n)
§Examples
let set = ordset![456];
assert_eq!(
set.update(123),
ordset![123, 456]
);
sourcepub fn without<BA>(&self, a: &BA) -> Self
pub fn without<BA>(&self, a: &BA) -> Self
Construct a new set with the given value removed if it’s in the set.
Time: O(log n)
sourcepub fn without_min(&self) -> (Option<A>, Self)
pub fn without_min(&self) -> (Option<A>, Self)
Remove the smallest value from a set, and return that value as well as the updated set.
Time: O(log n)
sourcepub fn without_max(&self) -> (Option<A>, Self)
pub fn without_max(&self) -> (Option<A>, Self)
Remove the largest value from a set, and return that value as well as the updated set.
Time: O(log n)
sourcepub fn union(self, other: Self) -> Self
pub fn union(self, other: Self) -> Self
Construct the union of two sets.
Time: O(n log n)
§Examples
let set1 = ordset!{1, 2};
let set2 = ordset!{2, 3};
let expected = ordset!{1, 2, 3};
assert_eq!(expected, set1.union(set2));
sourcepub fn unions<I>(i: I) -> Selfwhere
I: IntoIterator<Item = Self>,
pub fn unions<I>(i: I) -> Selfwhere
I: IntoIterator<Item = Self>,
Construct the union of multiple sets.
Time: O(n log n)
sourcepub fn difference(self, other: Self) -> Self
pub fn difference(self, other: Self) -> Self
Construct the symmetric difference between two sets.
This is an alias for the
symmetric_difference
method.
Time: O(n log n)
§Examples
let set1 = ordset!{1, 2};
let set2 = ordset!{2, 3};
let expected = ordset!{1, 3};
assert_eq!(expected, set1.difference(set2));
sourcepub fn symmetric_difference(self, other: Self) -> Self
pub fn symmetric_difference(self, other: Self) -> Self
Construct the symmetric difference between two sets.
Time: O(n log n)
§Examples
let set1 = ordset!{1, 2};
let set2 = ordset!{2, 3};
let expected = ordset!{1, 3};
assert_eq!(expected, set1.symmetric_difference(set2));
sourcepub fn relative_complement(self, other: Self) -> Self
pub fn relative_complement(self, other: Self) -> Self
Construct the relative complement between two sets, that is the set
of values in self
that do not occur in other
.
Time: O(m log n) where m is the size of the other set
§Examples
let set1 = ordset!{1, 2};
let set2 = ordset!{2, 3};
let expected = ordset!{1};
assert_eq!(expected, set1.relative_complement(set2));
sourcepub fn intersection(self, other: Self) -> Self
pub fn intersection(self, other: Self) -> Self
Construct the intersection of two sets.
Time: O(n log n)
§Examples
let set1 = ordset!{1, 2};
let set2 = ordset!{2, 3};
let expected = ordset!{2};
assert_eq!(expected, set1.intersection(set2));
sourcepub fn split<BA>(self, split: &BA) -> (Self, Self)
pub fn split<BA>(self, split: &BA) -> (Self, Self)
Split a set into two, with the left hand set containing values
which are smaller than split
, and the right hand set
containing values which are larger than split
.
The split
value itself is discarded.
Time: O(n)
sourcepub fn split_member<BA>(self, split: &BA) -> (Self, bool, Self)
pub fn split_member<BA>(self, split: &BA) -> (Self, bool, Self)
Split a set into two, with the left hand set containing values
which are smaller than split
, and the right hand set
containing values which are larger than split
.
Returns a tuple of the two sets and a boolean which is true if
the split
value existed in the original set, and false
otherwise.
Time: O(n)
Trait Implementations§
source§impl<A, R> Extend<R> for OrdSet<A>
impl<A, R> Extend<R> for OrdSet<A>
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = R>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = R>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)source§impl<A, R> FromIterator<R> for OrdSet<A>
impl<A, R> FromIterator<R> for OrdSet<A>
source§fn from_iter<T>(i: T) -> Selfwhere
T: IntoIterator<Item = R>,
fn from_iter<T>(i: T) -> Selfwhere
T: IntoIterator<Item = R>,
source§impl<'a, A> IntoIterator for &'a OrdSet<A>where
A: 'a + Ord,
impl<'a, A> IntoIterator for &'a OrdSet<A>where
A: 'a + Ord,
source§impl<A> IntoIterator for OrdSet<A>
impl<A> IntoIterator for OrdSet<A>
source§impl<A: Ord> Ord for OrdSet<A>
impl<A: Ord> Ord for OrdSet<A>
source§impl<A: Ord> PartialOrd for OrdSet<A>
impl<A: Ord> PartialOrd for OrdSet<A>
impl<A: Ord + Eq> Eq for OrdSet<A>
Auto Trait Implementations§
impl<A> Freeze for OrdSet<A>
impl<A> RefUnwindSafe for OrdSet<A>where
A: RefUnwindSafe,
impl<A> Send for OrdSet<A>
impl<A> Sync for OrdSet<A>
impl<A> Unpin for OrdSet<A>where
A: Unpin,
impl<A> UnwindSafe for OrdSet<A>where
A: RefUnwindSafe + UnwindSafe,
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)