Struct druid::im::OrdSet

source ·
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>

source

pub fn new() -> OrdSet<A>

Construct an empty set.

source

pub fn unit(a: A) -> OrdSet<A>

Construct a set with a single value.

Examples
let set = OrdSet::unit(123);
assert!(set.contains(&123));
source

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()
);
source

pub fn len(&self) -> usize

Get the size of a set.

Time: O(1)

Examples
assert_eq!(3, ordset![1, 2, 3].len());
source

pub fn ptr_eq(&self, other: &OrdSet<A>) -> 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

pub fn clear(&mut self)

Discard all elements from the set.

This leaves you with an empty set, and all elements that were previously inside it are dropped.

Time: O(n)

Examples
let mut set = ordset![1, 2, 3];
set.clear();
assert!(set.is_empty());
source§

impl<A> OrdSet<A>where A: Ord,

source

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)

source

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)

source

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

Create an iterator over the contents of the set.

source

pub fn range<R, BA>(&self, range: R) -> RangedIter<'_, A> where R: RangeBounds<BA>, A: Borrow<BA>, BA: Ord + ?Sized,

Create an iterator over a range inside the set.

source

pub fn diff<'a>(&'a self, other: &'a OrdSet<A>) -> DiffIter<'a, 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)

source

pub fn contains<BA>(&self, a: &BA) -> boolwhere BA: Ord + ?Sized, A: Borrow<BA>,

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));
source

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));
source

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));
source

pub fn is_subset<RS>(&self, other: RS) -> boolwhere RS: Borrow<OrdSet<A>>,

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

source

pub fn is_proper_subset<RS>(&self, other: RS) -> boolwhere RS: Borrow<OrdSet<A>>,

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>where A: Ord + Clone,

source

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]
);
source

pub fn remove<BA>(&mut self, a: &BA) -> Option<A>where BA: Ord + ?Sized, A: Borrow<BA>,

Remove a value from a set.

Time: O(log n)

source

pub fn remove_min(&mut self) -> Option<A>

Remove the smallest value from a set.

Time: O(log n)

source

pub fn remove_max(&mut self) -> Option<A>

Remove the largest value from a set.

Time: O(log n)

source

pub fn update(&self, a: A) -> OrdSet<A>

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]
);
source

pub fn without<BA>(&self, a: &BA) -> OrdSet<A>where BA: Ord + ?Sized, A: Borrow<BA>,

Construct a new set with the given value removed if it’s in the set.

Time: O(log n)

source

pub fn without_min(&self) -> (Option<A>, OrdSet<A>)

Remove the smallest value from a set, and return that value as well as the updated set.

Time: O(log n)

source

pub fn without_max(&self) -> (Option<A>, OrdSet<A>)

Remove the largest value from a set, and return that value as well as the updated set.

Time: O(log n)

source

pub fn union(self, other: OrdSet<A>) -> OrdSet<A>

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));
source

pub fn unions<I>(i: I) -> OrdSet<A>where I: IntoIterator<Item = OrdSet<A>>,

Construct the union of multiple sets.

Time: O(n log n)

source

pub fn difference(self, other: OrdSet<A>) -> OrdSet<A>

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));
source

pub fn symmetric_difference(self, other: OrdSet<A>) -> OrdSet<A>

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));
source

pub fn relative_complement(self, other: OrdSet<A>) -> OrdSet<A>

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));
source

pub fn intersection(self, other: OrdSet<A>) -> OrdSet<A>

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));
source

pub fn split<BA>(self, split: &BA) -> (OrdSet<A>, OrdSet<A>)where BA: Ord + ?Sized, A: Borrow<BA>,

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)

source

pub fn split_member<BA>(self, split: &BA) -> (OrdSet<A>, bool, OrdSet<A>)where BA: Ord + ?Sized, A: Borrow<BA>,

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)

source

pub fn take(&self, n: usize) -> OrdSet<A>

Construct a set with only the n smallest values from a given set.

Time: O(n)

source

pub fn skip(&self, n: usize) -> OrdSet<A>

Construct a set with the n smallest values removed from a given set.

Time: O(n)

Trait Implementations§

source§

impl<'a, A> Add<&'a OrdSet<A>> for &'a OrdSet<A>where A: Ord + Clone,

§

type Output = OrdSet<A>

The resulting type after applying the + operator.
source§

fn add( self, other: &'a OrdSet<A> ) -> <&'a OrdSet<A> as Add<&'a OrdSet<A>>>::Output

Performs the + operation. Read more
source§

impl<A> Add<OrdSet<A>> for OrdSet<A>where A: Ord + Clone,

§

type Output = OrdSet<A>

The resulting type after applying the + operator.
source§

fn add(self, other: OrdSet<A>) -> <OrdSet<A> as Add<OrdSet<A>>>::Output

Performs the + operation. Read more
source§

impl<A> Clone for OrdSet<A>

source§

fn clone(&self) -> OrdSet<A>

Clone a set.

Time: O(1)

1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Data> Data for OrdSet<T>

source§

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

Determine whether two values are the same. Read more
source§

impl<A> Debug for OrdSet<A>where A: Ord + Debug,

source§

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

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

impl<A> Default for OrdSet<A>

source§

fn default() -> OrdSet<A>

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

impl<A, R> Extend<R> for OrdSet<A>where A: Ord + Clone + From<R>,

source§

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

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

fn extend_one(&mut self, item: A)

🔬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<'a, A> From<&'a [A]> for OrdSet<A>where A: Ord + Clone,

source§

fn from(slice: &'a [A]) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<'a, A> From<&'a BTreeSet<A, Global>> for OrdSet<A>where A: Ord + Clone,

source§

fn from(btree_set: &BTreeSet<A, Global>) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<'a, A> From<&'a HashSet<A, RandomState>> for OrdSet<A>where A: Eq + Hash + Ord + Clone,

source§

fn from(hash_set: &HashSet<A, RandomState>) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<'a, A, S> From<&'a HashSet<A, S>> for OrdSet<A>where A: Hash + Eq + Ord + Clone, S: BuildHasher,

source§

fn from(hashset: &HashSet<A, S>) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<'a, A, S> From<&'a OrdSet<A>> for HashSet<A, S>where A: Ord + Hash + Eq + Clone, S: BuildHasher + Default,

source§

fn from(ordset: &OrdSet<A>) -> HashSet<A, S>

Converts to this type from the input type.
source§

impl<'a, A> From<&'a Vec<A, Global>> for OrdSet<A>where A: Ord + Clone,

source§

fn from(vec: &Vec<A, Global>) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<'s, 'a, A, OA> From<&'s OrdSet<&'a A>> for OrdSet<OA>where A: ToOwned<Owned = OA> + Ord + ?Sized, OA: Borrow<A> + Ord + Clone,

source§

fn from(set: &OrdSet<&A>) -> OrdSet<OA>

Converts to this type from the input type.
source§

impl<A> From<BTreeSet<A, Global>> for OrdSet<A>where A: Ord + Clone,

source§

fn from(btree_set: BTreeSet<A, Global>) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<A> From<HashSet<A, RandomState>> for OrdSet<A>where A: Eq + Hash + Ord + Clone,

source§

fn from(hash_set: HashSet<A, RandomState>) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<A, S> From<HashSet<A, S>> for OrdSet<A>where A: Hash + Eq + Ord + Clone, S: BuildHasher,

source§

fn from(hashset: HashSet<A, S>) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<A, S> From<OrdSet<A>> for HashSet<A, S>where A: Ord + Hash + Eq + Clone, S: BuildHasher + Default,

source§

fn from(ordset: OrdSet<A>) -> HashSet<A, S>

Converts to this type from the input type.
source§

impl<A> From<Vec<A, Global>> for OrdSet<A>where A: Ord + Clone,

source§

fn from(vec: Vec<A, Global>) -> OrdSet<A>

Converts to this type from the input type.
source§

impl<A, R> FromIterator<R> for OrdSet<A>where A: Ord + Clone + From<R>,

source§

fn from_iter<T>(i: T) -> OrdSet<A>where T: IntoIterator<Item = R>,

Creates a value from an iterator. Read more
source§

impl<A> Hash for OrdSet<A>where A: Ord + Hash,

source§

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

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<'a, A> IntoIterator for &'a OrdSet<A>where A: 'a + Ord,

§

type Item = &'a A

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, A>

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

fn into_iter(self) -> <&'a OrdSet<A> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
source§

impl<A> IntoIterator for OrdSet<A>where A: Ord + Clone,

§

type Item = A

The type of the elements being iterated over.
§

type IntoIter = ConsumingIter<A>

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

fn into_iter(self) -> <OrdSet<A> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, A> Mul<&'a OrdSet<A>> for &'a OrdSet<A>where A: Ord + Clone,

§

type Output = OrdSet<A>

The resulting type after applying the * operator.
source§

fn mul( self, other: &'a OrdSet<A> ) -> <&'a OrdSet<A> as Mul<&'a OrdSet<A>>>::Output

Performs the * operation. Read more
source§

impl<A> Mul<OrdSet<A>> for OrdSet<A>where A: Ord + Clone,

§

type Output = OrdSet<A>

The resulting type after applying the * operator.
source§

fn mul(self, other: OrdSet<A>) -> <OrdSet<A> as Mul<OrdSet<A>>>::Output

Performs the * operation. Read more
source§

impl<A> Ord for OrdSet<A>where A: Ord,

source§

fn cmp(&self, other: &OrdSet<A>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl<A> PartialEq<OrdSet<A>> for OrdSet<A>where A: Ord,

source§

fn eq(&self, other: &OrdSet<A>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<A> PartialOrd<OrdSet<A>> for OrdSet<A>where A: Ord,

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<A> Sum<OrdSet<A>> for OrdSet<A>where A: Ord + Clone,

source§

fn sum<I>(it: I) -> OrdSet<A>where I: Iterator<Item = OrdSet<A>>,

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<A> Eq for OrdSet<A>where A: Ord + Eq,

Auto Trait Implementations§

§

impl<A> RefUnwindSafe for OrdSet<A>where A: RefUnwindSafe,

§

impl<A> Send for OrdSet<A>where A: Send + Sync,

§

impl<A> Sync for OrdSet<A>where A: Send + Sync,

§

impl<A> Unpin for OrdSet<A>where A: Unpin,

§

impl<A> UnwindSafe for OrdSet<A>where A: UnwindSafe + RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AnyEq for Twhere T: Any + PartialEq<T>,

source§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

source§

fn as_any(&self) -> &(dyn Any + 'static)

source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · 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 Twhere U: From<T>,

const: unstable · 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.

§

impl<T> RoundFrom<T> for T

§

fn round_from(x: T) -> T

Performs the conversion.
§

impl<T, U> RoundInto<U> for Twhere U: RoundFrom<T>,

§

fn round_into(self) -> U

Performs the conversion.
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
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