Struct nonempty_collections::set::NESet
source · pub struct NESet<T, S = RandomState> {
pub head: T,
pub tail: HashSet<T, S>,
}
Expand description
A non-empty, growable HashSet
.
§Construction and Access
The nes
macro is the simplest way to construct an NESet
:
use nonempty_collections::*;
let s = nes![1,1,2,2,3,3,4,4];
let mut v: NEVec<_> = s.iter().collect();
v.sort();
assert_eq!(nev![&1,&2,&3,&4], v);
With NESet
, the first element can always be accessed in constant time.
use nonempty_collections::nes;
let s = nes!["Fëanor", "Fingolfin", "Finarfin"];
assert_eq!("Fëanor", s.head);
§Conversion
If you have a HashSet
but want an NESet
, try NESet::from_set
.
Naturally, this might not succeed.
If you have an NESet
but want a HashSet
, try their corresponding
From
instance. This will always succeed.
use nonempty_collections::nes;
use std::collections::HashSet;
let n0 = nes![1,2,3];
let s0 = HashSet::from(n0);
// Or just use `Into`.
let n1 = nes![1,2,3];
let s1: HashSet<_> = n1.into();
§API Differences with HashSet
Note that the following methods aren’t implemented for NESet
:
clear
drain
drain_filter
remove
retain
take
As these methods are all “mutate-in-place” style and are difficult to reconcile with the non-emptiness guarantee.
Fields§
§head: T
An element of the non-empty HashSet
. Always exists.
tail: HashSet<T, S>
The remaining elements, perhaps empty.
Implementations§
source§impl<T, S> NESet<T, S>
impl<T, S> NESet<T, S>
source§impl<T> NESet<T>
impl<T> NESet<T>
sourcepub fn from_set(set: HashSet<T>) -> Option<NESet<T>>
pub fn from_set(set: HashSet<T>) -> Option<NESet<T>>
Attempt a conversion from a HashSet
, consuming the given HashSet
.
Will fail if the HashSet
is empty.
Slightly inefficient, as it requires a reallocation of the “tail”
HashSet
after the initial head
has been extracted.
use nonempty_collections::{nes, NESet};
use std::collections::HashSet;
let mut s = HashSet::new();
s.insert(1);
s.insert(2);
s.insert(3);
let n = NESet::from_set(s);
assert_eq!(Some(nes![1,2,3]), n);
source§impl<T, S> NESet<T, S>
impl<T, S> NESet<T, S>
sourcepub fn contains<Q>(&self, value: &Q) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
Returns true if the set contains a value.
use nonempty_collections::nes;
let s = nes![1,2,3];
assert!(s.contains(&3));
assert!(!s.contains(&10));
sourcepub fn difference<'a>(&'a self, other: &'a NESet<T, S>) -> Difference<'a, T, S> ⓘ
pub fn difference<'a>(&'a self, other: &'a NESet<T, S>) -> Difference<'a, T, S> ⓘ
Visits the values representing the difference, i.e., the values that are
in self
but not in other
.
use nonempty_collections::nes;
let s0 = nes![1,2,3];
let s1 = nes![3,4,5];
let mut v: Vec<_> = s0.difference(&s1).collect();
v.sort();
assert_eq!(vec![&1, &2], v);
sourcepub fn get<Q>(&self, value: &Q) -> Option<&T>
pub fn get<Q>(&self, value: &Q) -> Option<&T>
Returns a reference to the value in the set, if any, that is equal to the given value.
The value may be any borrowed form of the set’s value type, but Hash
and Eq
on the borrowed form must match those for the value type.
use nonempty_collections::nes;
let s = nes![1,2,3];
assert_eq!(Some(&3), s.get(&3));
assert_eq!(None, s.get(&10));
sourcepub fn insert(&mut self, value: T) -> bool
pub fn insert(&mut self, value: T) -> bool
Adds a value to the set.
If the set did not have this value present, true
is returned.
If the set did have this value present, false
is returned.
use nonempty_collections::nes;
let mut s = nes![1,2,3];
assert_eq!(false, s.insert(2));
assert_eq!(true, s.insert(4));
sourcepub fn intersection<'a>(
&'a self,
other: &'a NESet<T, S>
) -> Intersection<'a, T, S> ⓘ
pub fn intersection<'a>( &'a self, other: &'a NESet<T, S> ) -> Intersection<'a, T, S> ⓘ
Visits the values representing the interesection, i.e., the values that
are both in self
and other
.
use nonempty_collections::nes;
let s0 = nes![1,2,3];
let s1 = nes![3,4,5];
let mut v: Vec<_> = s0.intersection(&s1).collect();
v.sort();
assert_eq!(vec![&3], v);
sourcepub fn is_disjoint(&self, other: &NESet<T, S>) -> bool
pub fn is_disjoint(&self, other: &NESet<T, S>) -> bool
Returns true
if self
has no elements in common with other
.
This is equivalent to checking for an empty intersection.
use nonempty_collections::nes;
let s0 = nes![1,2,3];
let s1 = nes![4,5,6];
assert!(s0.is_disjoint(&s1));
sourcepub fn is_subset(&self, other: &NESet<T>) -> bool
pub fn is_subset(&self, other: &NESet<T>) -> bool
Returns true
if the set is a subset of another, i.e., other
contains
at least all the values in self
.
use nonempty_collections::nes;
let sub = nes![1,2,3];
let sup = nes![1,2,3,4];
assert!(sub.is_subset(&sup));
assert!(!sup.is_subset(&sub));
sourcepub fn is_superset(&self, other: &NESet<T>) -> bool
pub fn is_superset(&self, other: &NESet<T>) -> bool
Returns true
if the set is a superset of another, i.e., self
contains at least all the values in other
.
use nonempty_collections::nes;
let sub = nes![1,2,3];
let sup = nes![1,2,3,4];
assert!(sup.is_superset(&sub));
assert!(!sub.is_superset(&sup));
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.
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more elements to be inserted
in the NESet
. The collection may reserve more space to avoid frequent
reallocations.
§Panics
Panics if the new allocation size overflows usize
.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the set as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
sourcepub fn union<'a>(&'a self, other: &'a NESet<T, S>) -> Union<'a, T, S>
pub fn union<'a>(&'a self, other: &'a NESet<T, S>) -> Union<'a, T, S>
Visits the values representing the union, i.e., all the values in self
or other
, without duplicates.
Note that a Union is always non-empty.
use nonempty_collections::*;
let s0 = nes![1,2,3];
let s1 = nes![3,4,5];
let mut v: NEVec<_> = s0.union(&s1).collect();
v.sort();
assert_eq!(nev![&1, &2, &3, &4, &5], v);
sourcepub fn with_capacity(capacity: usize, value: T) -> NESet<T>
pub fn with_capacity(capacity: usize, value: T) -> NESet<T>
Creates a new NESet
with a single element and specified capacity.
sourcepub fn with_capacity_and_hasher(
capacity: usize,
hasher: S,
value: T
) -> NESet<T, S>
pub fn with_capacity_and_hasher( capacity: usize, hasher: S, value: T ) -> NESet<T, S>
sourcepub fn with_hasher(hasher: S, value: T) -> NESet<T, S>
pub fn with_hasher(hasher: S, value: T) -> NESet<T, S>
See HashSet::with_hasher
.
Trait Implementations§
source§impl<T, S> FromNonEmptyIterator<T> for NESet<T, S>
impl<T, S> FromNonEmptyIterator<T> for NESet<T, S>
use nonempty_collections::*;
let s0 = nes![1, 2, 3];
let s1: NESet<_> = s0.iter().cloned().collect();
assert_eq!(s0, s1);
source§fn from_nonempty_iter<I>(iter: I) -> Selfwhere
I: IntoNonEmptyIterator<Item = T>,
fn from_nonempty_iter<I>(iter: I) -> Selfwhere
I: IntoNonEmptyIterator<Item = T>,
use nonempty_collections::{nes, nev, FromNonEmptyIterator, NESet};
let v = nev![1, 1, 2, 3, 2];
let s = NESet::from_nonempty_iter(v);
assert_eq!(nes![1, 2, 3], s);
source§impl<'a, T> IntoIterator for &'a NESet<T>
impl<'a, T> IntoIterator for &'a NESet<T>
source§impl<T> IntoIterator for NESet<T>
impl<T> IntoIterator for NESet<T>
source§impl<T, S> IntoNonEmptyIterator for NESet<T, S>
impl<T, S> IntoNonEmptyIterator for NESet<T, S>
§type IntoIter = Chain<Once<T>, IntoIter<<NESet<T, S> as IntoNonEmptyIterator>::Item>>
type IntoIter = Chain<Once<T>, IntoIter<<NESet<T, S> as IntoNonEmptyIterator>::Item>>
NonEmptyIterator
are we turning this into?source§fn into_nonempty_iter(self) -> Self::IntoIter
fn into_nonempty_iter(self) -> Self::IntoIter
NonEmptyIterator
from a value.source§impl<T, S> PartialEq for NESet<T, S>
impl<T, S> PartialEq for NESet<T, S>
impl<T, S> Eq for NESet<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for NESet<T, S>
impl<T, S> RefUnwindSafe for NESet<T, S>where
T: RefUnwindSafe,
S: RefUnwindSafe,
impl<T, S> Send for NESet<T, S>
impl<T, S> Sync for NESet<T, S>
impl<T, S> Unpin for NESet<T, S>
impl<T, S> UnwindSafe for NESet<T, S>where
T: UnwindSafe,
S: 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§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.source§impl<T> IntoIteratorExt for Twhere
T: IntoIterator,
impl<T> IntoIteratorExt for Twhere
T: IntoIterator,
source§fn try_into_nonempty_iter(self) -> Option<<T as IntoIteratorExt>::IntoIter>
fn try_into_nonempty_iter(self) -> Option<<T as IntoIteratorExt>::IntoIter>
Tries to convert self
into NonEmptyIterator
. Calls self.next()
once. If self
doesn’t return Some
upon the first call to next()
,
returns None
.
§type Item = <T as IntoIterator>::Item
type Item = <T as IntoIterator>::Item
§type IntoIter = Chain<Once<<T as IntoIteratorExt>::Item>, <T as IntoIterator>::IntoIter>
type IntoIter = Chain<Once<<T as IntoIteratorExt>::Item>, <T as IntoIterator>::IntoIter>
NonEmptyIterator
are we turning this into?