Skip to main content

NEIndexSet

Struct NEIndexSet 

Source
pub struct NEIndexSet<T, S = RandomState> { /* private fields */ }
Expand description

A non-empty, growable IndexSet.

§Construction and Access

The neis macro is the simplest way to construct an NEIndexSet:

use nonempty_collections::*;

let s = neis![1, 1, 2, 2, 3, 3, 4, 4];
let mut v: NEVec<_> = s.nonempty_iter().collect();
v.sort();
assert_eq!(nev![&1, &2, &3, &4], v);
use nonempty_collections::neis;

let s = neis!["Fëanor", "Fingolfin", "Finarfin"];
assert!(s.contains(&"Fëanor"));

§Conversion

If you have a IndexSet but want an NEIndexSet, try NEIndexSet::try_from_set. Naturally, this might not succeed.

If you have an NEIndexSet but want a IndexSet, try their corresponding From instance. This will always succeed.

use indexmap::set::IndexSet;

use nonempty_collections::neis;

let n0 = neis![1, 2, 3];
let s0 = IndexSet::from(n0);

// Or just use `Into`.
let n1 = neis![1, 2, 3];
let s1: IndexSet<_> = n1.into();

§API Differences with IndexSet

Note that the following methods aren’t implemented for NEIndexSet:

  • 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.

Implementations§

Source§

impl<T, S> NEIndexSet<T, S>

Source

pub fn capacity(&self) -> NonZeroUsize

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>

Returns a regular iterator over the values in this non-empty set.

For a NonEmptyIterator see Self::nonempty_iter().

Source

pub fn nonempty_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::neis;

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

pub const fn is_empty(&self) -> bool

👎Deprecated since 0.1.0:

A NEIndexSet is never empty.

A NEIndexSet is never empty.

Source§

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

Source

pub fn new(value: T) -> Self

Creates a new NEIndexSet with a single element.

Source

pub fn with_capacity(capacity: NonZeroUsize, value: T) -> NEIndexSet<T>

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

use std::hash::RandomState;
use std::num::NonZeroUsize;

use nonempty_collections::*;
let set = NEIndexSet::with_capacity(NonZeroUsize::MIN, "hello");
assert_eq!(neis! {"hello"}, set);
assert!(set.capacity().get() >= 1);
Source§

impl<T, S> NEIndexSet<T, S>
where T: Eq + Hash, S: BuildHasher,

Source

pub fn try_from_set(set: IndexSet<T, S>) -> Option<NEIndexSet<T, S>>

Attempt a conversion from a IndexSet, consuming the given IndexSet. Will return None if the IndexSet is empty.

use indexmap::set::IndexSet;

use nonempty_collections::neis;
use nonempty_collections::NEIndexSet;

let mut s = IndexSet::new();
s.extend([1, 2, 3]);

let n = NEIndexSet::try_from_set(s);
assert_eq!(Some(neis![1, 2, 3]), n);
let s: IndexSet<()> = IndexSet::new();
assert_eq!(None, NEIndexSet::try_from_set(s));
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::neis;

let s = neis![1, 2, 3];
assert!(s.contains(&3));
assert!(!s.contains(&10));
Source

pub fn difference<'a>( &'a self, other: &'a NEIndexSet<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::neis;

let s0 = neis![1, 2, 3];
let s1 = neis![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::neis;

let s = neis![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::neis;

let mut s = neis![1, 2, 3];
assert_eq!(false, s.insert(2));
assert_eq!(true, s.insert(4));
Source

pub fn intersection<'a>( &'a self, other: &'a NEIndexSet<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::neis;

let s0 = neis![1, 2, 3];
let s1 = neis![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: &NEIndexSet<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::neis;

let s0 = neis![1, 2, 3];
let s1 = neis![4, 5, 6];
assert!(s0.is_disjoint(&s1));
Source

pub fn is_subset(&self, other: &NEIndexSet<T, S>) -> 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::neis;

let sub = neis![1, 2, 3];
let sup = neis![1, 2, 3, 4];

assert!(sub.is_subset(&sup));
assert!(!sup.is_subset(&sub));
Source

pub fn is_superset(&self, other: &NEIndexSet<T, S>) -> 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::neis;

let sub = neis![1, 2, 3];
let sup = neis![1, 2, 3, 4];

assert!(sup.is_superset(&sub));
assert!(!sub.is_superset(&sup));
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 NEIndexSet. 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 NEIndexSet<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 = neis![1, 2, 3];
let s1 = neis![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_and_hasher( capacity: NonZeroUsize, hasher: S, value: T, ) -> NEIndexSet<T, S>

Source

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

Trait Implementations§

Source§

impl<T, S> AsMut<IndexSet<T, S>> for NEIndexSet<T, S>

Source§

fn as_mut(&mut self) -> &mut IndexSet<T, S>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T, S> AsRef<IndexSet<T, S>> for NEIndexSet<T, S>

Source§

fn as_ref(&self) -> &IndexSet<T, S>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

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

Source§

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

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

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

Source§

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

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

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

Source§

impl<T> Extend<T> for NEIndexSet<T>
where T: Eq + Hash,

Source§

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

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

Source§

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

use indexmap::set::IndexSet;

use nonempty_collections::neis;

let s: IndexSet<_> = neis![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 NEIndexSet<T, S>
where T: Eq + Hash, S: BuildHasher + Default,

use nonempty_collections::*;

let s0 = neis![1, 2, 3];
let s1: NEIndexSet<_> = s0.nonempty_iter().cloned().collect();
assert_eq!(s0, s1);
Source§

fn from_nonempty_iter<I>(iter: I) -> Self
where I: IntoNonEmptyIterator<Item = T>,

use nonempty_collections::*;

let v = nev![1, 1, 2, 3, 2];
let s = NEIndexSet::from_nonempty_iter(v);

assert_eq!(neis![1, 2, 3], s);
Source§

impl<T, S> IntoIterator for NEIndexSet<T, S>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<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<'a, T, S> IntoIterator for &'a NEIndexSet<T, S>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = 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, S> IntoNonEmptyIterator for NEIndexSet<T, S>

Source§

type IntoNEIter = IntoIter<T>

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

fn into_nonempty_iter(self) -> Self::IntoNEIter

Creates a NonEmptyIterator from a value.
Source§

impl<'a, T, S> IntoNonEmptyIterator for &'a NEIndexSet<T, S>

Source§

type IntoNEIter = Iter<'a, T>

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

fn into_nonempty_iter(self) -> Self::IntoNEIter

Creates a NonEmptyIterator from a value.
Source§

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

Source§

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

use nonempty_collections::neis;

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

assert!(s0 == s1);
assert!(s0 != s2);
assert!(s0 != s3);
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<T> Singleton for NEIndexSet<T>
where T: Eq + Hash,

Source§

fn singleton(item: Self::Item) -> Self

use nonempty_collections::{NEIndexSet, Singleton, neis};

let s = NEIndexSet::singleton(1);
assert_eq!(neis![1], s);
Source§

type Item = T

Source§

impl<T, S> TryFrom<IndexSet<T, S>> for NEIndexSet<T, S>
where T: Eq + Hash, S: BuildHasher + Default,

Source§

type Error = Error

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

fn try_from(set: IndexSet<T, S>) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T, S> UnsafeUnpin for NEIndexSet<T, S>
where S: UnsafeUnpin,

§

impl<T, S> UnwindSafe for NEIndexSet<T, S>
where S: UnwindSafe, T: 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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoIteratorExt for T
where T: IntoIterator,

Source§

fn try_into_nonempty_iter(self) -> Option<<T as IntoIteratorExt>::IntoIter>

Converts self into a non-empty iterator or returns None if the iterator is empty.

Source§

type Item = <T as IntoIterator>::Item

The type of the elements being iterated over.
Source§

type IntoIter = NonEmptyIterAdapter<Peekable<<T as IntoIterator>::IntoIter>>

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

impl<T> ToOwned for T
where T: Clone,

Source§

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>,

Source§

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>,

Source§

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.