pub struct OrderSet<T, S = RandomState> {
map: OrderMap<T, (), S>,
}
Expand description
A hash set where the iteration order of the values is independent of their hash values.
The interface is closely compatible with the standard HashSet
, but also
has additional features.
§Order
The values have a consistent order that is determined by the sequence of insertion and removal calls on the set. The order does not depend on the values or the hash function at all. Note that insertion order and value are not affected if a re-insertion is attempted once an element is already present.
All iterators traverse the set in order. Set operation iterators like
union
produce a concatenated order, as do their matching “bitwise”
operators. See their documentation for specifics.
§Indices
The values are indexed in a compact range without holes in the range
0..self.len()
. For example, the method .get_full
looks up the index for
a value, and the method .get_index
looks up the value by index.
§Examples
use ordermap::OrderSet;
// Collects which letters appear in a sentence.
let letters: OrderSet<_> = "a short treatise on fungi".chars().collect();
assert!(letters.contains(&'s'));
assert!(letters.contains(&'t'));
assert!(letters.contains(&'u'));
assert!(!letters.contains(&'y'));
Fields§
§map: OrderMap<T, (), S>
Implementations§
source§impl<T> OrderSet<T>
impl<T> OrderSet<T>
sourcepub fn with_capacity(n: usize) -> Self
pub fn with_capacity(n: usize) -> Self
Create a new set with capacity for n
elements.
(Does not allocate if n
is zero.)
Computes in O(n) time.
source§impl<T, S> OrderSet<T, S>
impl<T, S> OrderSet<T, S>
sourcepub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Selfwhere
S: BuildHasher,
pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Selfwhere
S: BuildHasher,
Create a new set with capacity for n
elements.
(Does not allocate if n
is zero.)
Computes in O(n) time.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the set contains no elements.
Computes in O(1) time.
sourcepub fn with_hasher(hash_builder: S) -> Selfwhere
S: BuildHasher,
pub fn with_hasher(hash_builder: S) -> Selfwhere
S: BuildHasher,
Create a new set with hash_builder
sourcepub fn hasher(&self) -> &Swhere
S: BuildHasher,
pub fn hasher(&self) -> &Swhere
S: BuildHasher,
Return a reference to the set’s BuildHasher
.
source§impl<T, S> OrderSet<T, S>
impl<T, S> OrderSet<T, S>
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Remove all elements in the set, while preserving its capacity.
Computes in O(n) time.
sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Insert the value into the set.
If an equivalent item already exists in the set, it returns
false
leaving the original value in the set and without
altering its insertion order. Otherwise, it inserts the new
item and returns true
.
Computes in O(1) time (amortized average).
sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Return an iterator over the values of the set, in their order
sourcepub fn difference<'a, S2>(
&'a self,
other: &'a OrderSet<T, S2>,
) -> Difference<'a, T, S2> ⓘwhere
S2: BuildHasher,
pub fn difference<'a, S2>(
&'a self,
other: &'a OrderSet<T, S2>,
) -> Difference<'a, T, S2> ⓘwhere
S2: BuildHasher,
Return an iterator over the values that are in self
but not other
.
Values are produced in the same order that they appear in self
.
sourcepub fn symmetric_difference<'a, S2>(
&'a self,
other: &'a OrderSet<T, S2>,
) -> SymmetricDifference<'a, T, S, S2> ⓘwhere
S2: BuildHasher,
pub fn symmetric_difference<'a, S2>(
&'a self,
other: &'a OrderSet<T, S2>,
) -> SymmetricDifference<'a, T, S, S2> ⓘwhere
S2: BuildHasher,
Return an iterator over the values that are in self
or other
,
but not in both.
Values from self
are produced in their original order, followed by
values from other
in their original order.
sourcepub fn intersection<'a, S2>(
&'a self,
other: &'a OrderSet<T, S2>,
) -> Intersection<'a, T, S2> ⓘwhere
S2: BuildHasher,
pub fn intersection<'a, S2>(
&'a self,
other: &'a OrderSet<T, S2>,
) -> Intersection<'a, T, S2> ⓘwhere
S2: BuildHasher,
Return an iterator over the values that are in both self
and other
.
Values are produced in the same order that they appear in self
.
sourcepub fn union<'a, S2>(&'a self, other: &'a OrderSet<T, S2>) -> Union<'a, T, S> ⓘwhere
S2: BuildHasher,
pub fn union<'a, S2>(&'a self, other: &'a OrderSet<T, S2>) -> Union<'a, T, S> ⓘwhere
S2: BuildHasher,
Return an iterator over all values that are in self
or other
.
Values from self
are produced in their original order, followed by
values that are unique to other
in their original order.
sourcepub fn contains<Q>(&self, value: &Q) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
Return true
if an equivalent to value
exists in the set.
Computes in O(1) time (average).
sourcepub fn get<Q>(&self, value: &Q) -> Option<&T>
pub fn get<Q>(&self, value: &Q) -> Option<&T>
Return a reference to the value stored in the set, if it is present,
else None
.
Computes in O(1) time (average).
sourcepub fn replace(&mut self, value: T) -> Option<T>
pub fn replace(&mut self, value: T) -> Option<T>
Adds a value to the set, replacing the existing value, if any, that is equal to the given one. Returns the replaced value.
Computes in O(1) time (average).
sourcepub fn remove<Q>(&mut self, value: &Q) -> bool
pub fn remove<Q>(&mut self, value: &Q) -> bool
FIXME Same as .swap_remove
Computes in O(1) time (average).
sourcepub fn swap_remove<Q>(&mut self, value: &Q) -> bool
pub fn swap_remove<Q>(&mut self, value: &Q) -> bool
Remove the value from the set, and return true
if it was present.
Like Vec::swap_remove
, the value is removed by swapping it with the
last element of the set and popping it off. This perturbs
the postion of what used to be the last element!
Return false
if value
was not in the set.
Computes in O(1) time (average).
sourcepub fn take<Q>(&mut self, value: &Q) -> Option<T>
pub fn take<Q>(&mut self, value: &Q) -> Option<T>
FIXME Same as .swap_take
Computes in O(1) time (average).
sourcepub fn swap_take<Q>(&mut self, value: &Q) -> Option<T>
pub fn swap_take<Q>(&mut self, value: &Q) -> Option<T>
Removes and returns the value in the set, if any, that is equal to the given one.
Like Vec::swap_remove
, the value is removed by swapping it with the
last element of the set and popping it off. This perturbs
the postion of what used to be the last element!
Return None
if value
was not in the set.
Computes in O(1) time (average).
sourcepub fn swap_remove_full<Q>(&mut self, value: &Q) -> Option<(usize, T)>
pub fn swap_remove_full<Q>(&mut self, value: &Q) -> Option<(usize, T)>
Remove the value from the set return it and the index it had.
Like Vec::swap_remove
, the value is removed by swapping it with the
last element of the set and popping it off. This perturbs
the postion of what used to be the last element!
Return None
if value
was not in the set.
sourcepub fn retain<F>(&mut self, keep: F)
pub fn retain<F>(&mut self, keep: F)
Scan through each value in the set and keep those where the
closure keep
returns true
.
The elements are visited in order, and remaining elements keep their order.
Computes in O(n) time (average).
sourcepub fn sort(&mut self)where
T: Ord,
pub fn sort(&mut self)where
T: Ord,
Sort the set’s values by their default ordering.
See sort_by
for details.
sourcepub fn sort_by<F>(&mut self, compare: F)
pub fn sort_by<F>(&mut self, compare: F)
Sort the set’s values in place using the comparison function compare
.
Computes in O(n log n) time and O(n) space. The sort is stable.
source§impl<T, S> OrderSet<T, S>
impl<T, S> OrderSet<T, S>
sourcepub fn get_index(&self, index: usize) -> Option<&T>
pub fn get_index(&self, index: usize) -> Option<&T>
Get a value by index
Valid indices are 0 <= index < self.len()
Computes in O(1) time.
sourcepub fn swap_remove_index(&mut self, index: usize) -> Option<T>
pub fn swap_remove_index(&mut self, index: usize) -> Option<T>
Remove the key-value pair by index
Valid indices are 0 <= index < self.len()
Computes in O(1) time (average).
source§impl<T, S> OrderSet<T, S>
impl<T, S> OrderSet<T, S>
sourcepub fn is_disjoint<S2>(&self, other: &OrderSet<T, S2>) -> boolwhere
S2: BuildHasher,
pub fn is_disjoint<S2>(&self, other: &OrderSet<T, S2>) -> boolwhere
S2: BuildHasher,
Returns true
if self
has no elements in common with other
.
sourcepub fn is_subset<S2>(&self, other: &OrderSet<T, S2>) -> boolwhere
S2: BuildHasher,
pub fn is_subset<S2>(&self, other: &OrderSet<T, S2>) -> boolwhere
S2: BuildHasher,
Returns true
if all elements of self
are contained in other
.
sourcepub fn is_superset<S2>(&self, other: &OrderSet<T, S2>) -> boolwhere
S2: BuildHasher,
pub fn is_superset<S2>(&self, other: &OrderSet<T, S2>) -> boolwhere
S2: BuildHasher,
Returns true
if all elements of other
are contained in self
.
Trait Implementations§
source§impl<'a, 'b, T, S1, S2> BitOr<&'b OrderSet<T, S2>> for &'a OrderSet<T, S1>
impl<'a, 'b, T, S1, S2> BitOr<&'b OrderSet<T, S2>> for &'a OrderSet<T, S1>
source§impl<'a, 'b, T, S1, S2> BitXor<&'b OrderSet<T, S2>> for &'a OrderSet<T, S1>
impl<'a, 'b, T, S1, S2> BitXor<&'b OrderSet<T, S2>> for &'a OrderSet<T, S1>
source§impl<'a, T, S> Extend<&'a T> for OrderSet<T, S>
impl<'a, T, S> Extend<&'a T> for OrderSet<T, S>
source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iterable: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iterable: I)
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<T, S> Extend<T> for OrderSet<T, S>
impl<T, S> Extend<T> for OrderSet<T, S>
source§fn extend<I: IntoIterator<Item = T>>(&mut self, iterable: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iterable: I)
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<T, S> FromIterator<T> for OrderSet<T, S>
impl<T, S> FromIterator<T> for OrderSet<T, S>
source§fn from_iter<I: IntoIterator<Item = T>>(iterable: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iterable: I) -> Self
source§impl<'a, T, S> IntoIterator for &'a OrderSet<T, S>
impl<'a, T, S> IntoIterator for &'a OrderSet<T, S>
source§impl<T, S> IntoIterator for OrderSet<T, S>
impl<T, S> IntoIterator for OrderSet<T, S>
source§impl<T, S1, S2> PartialEq<OrderSet<T, S2>> for OrderSet<T, S1>
impl<T, S1, S2> PartialEq<OrderSet<T, S2>> for OrderSet<T, S1>
impl<T, S> Eq for OrderSet<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for OrderSet<T, S>where
S: Freeze,
impl<T, S> RefUnwindSafe for OrderSet<T, S>where
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, S> Send for OrderSet<T, S>
impl<T, S> Sync for OrderSet<T, S>
impl<T, S> Unpin for OrderSet<T, S>
impl<T, S> UnwindSafe for OrderSet<T, S>where
S: UnwindSafe,
T: 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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.