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>

source

pub fn capacity(&self) -> usize

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

source

pub fn hasher(&self) -> &S

Returns a reference to the set’s BuildHasher.

source

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

An iterator visiting all elements in arbitrary order.

source

pub fn len(&self) -> NonZeroUsize

Returns the number of elements in the set. Always 1 or more.

use nonempty_collections::nes;

let s = nes![1,2,3];
assert_eq!(3, s.len().get());
source

pub const fn is_empty(&self) -> bool

👎Deprecated since 0.1.0: A NESet is never empty.

A NESet is never empty.

source§

impl<T> NESet<T>
where T: Eq + Hash,

source

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>
where T: Eq + Hash, S: BuildHasher,

source

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

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

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

pub fn get<Q>(&self, value: &Q) -> Option<&T>
where T: Borrow<Q>, Q: Eq + Hash,

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

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

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

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

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

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

pub fn new(value: T) -> NESet<T>

Creates a new NESet with a single element.

source

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.

source

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.

source

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.

source

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

pub fn with_capacity(capacity: usize, value: T) -> NESet<T>

Creates a new NESet with a single element and specified capacity.

source

pub fn with_capacity_and_hasher( capacity: usize, hasher: S, value: T ) -> NESet<T, S>

source

pub fn with_hasher(hasher: S, value: T) -> NESet<T, S>

Trait Implementations§

source§

impl<T: Clone, S: Clone> Clone for NESet<T, S>

source§

fn clone(&self) -> NESet<T, S>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug, S: Debug> Debug for NESet<T, S>

source§

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

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

impl<T, S> From<NESet<T, S>> for HashSet<T, S>
where T: Eq + Hash, S: BuildHasher,

source§

fn from(s: NESet<T, S>) -> Self

use nonempty_collections::nes;
use std::collections::HashSet;

let s: HashSet<_> = nes![1,2,3].into();
let mut v: Vec<_> = s.into_iter().collect();
v.sort();
assert_eq!(vec![1,2,3], v);
source§

impl<T, S> FromNonEmptyIterator<T> for NESet<T, S>
where T: Eq + Hash, S: BuildHasher + Default,

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) -> Self
where 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>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = Chain<Once<&'a T>, Iter<'a, T>>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for NESet<T>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = Chain<Once<T>, IntoIter<<NESet<T> as IntoIterator>::Item>>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T, S> IntoNonEmptyIterator for NESet<T, S>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = Chain<Once<T>, IntoIter<<NESet<T, S> as IntoNonEmptyIterator>::Item>>

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

fn into_nonempty_iter(self) -> Self::IntoIter

Creates a NonEmptyIterator from a value.
source§

impl<T, S> PartialEq for NESet<T, S>
where T: Eq + Hash, S: BuildHasher,

source§

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

use nonempty_collections::nes;

let s0 = nes![1,2,3];
let s1 = nes![1,2,3];
let s2 = nes![1,2];
let s3 = nes![1,2,3,4];

assert!(s0 == s1);
assert!(s0 != s2);
assert!(s0 != s3);
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<T, S> Eq for NESet<T, S>
where T: Eq + Hash, S: BuildHasher,

Auto Trait Implementations§

§

impl<T, S> Freeze for NESet<T, S>
where T: Freeze, S: Freeze,

§

impl<T, S> RefUnwindSafe for NESet<T, S>

§

impl<T, S> Send for NESet<T, S>
where T: Send, S: Send,

§

impl<T, S> Sync for NESet<T, S>
where T: Sync, S: Sync,

§

impl<T, S> Unpin for NESet<T, S>
where T: Unpin, S: Unpin,

§

impl<T, S> UnwindSafe for NESet<T, S>
where T: UnwindSafe, S: UnwindSafe,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
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> IntoIteratorExt for T
where T: IntoIterator,

source§

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

The type of the elements being iterated over.
§

type IntoIter = Chain<Once<<T as IntoIteratorExt>::Item>, <T as IntoIterator>::IntoIter>

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

impl<T> ToOwned for T
where 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 T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

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.