Struct imbl::HashSet

source ·
pub struct HashSet<A, S = RandomState> { /* private fields */ }
Expand description

An unordered set.

An immutable hash set using [hash array mapped tries] 1.

Most operations on this set are O(logx n) for a suitably high x that it should be nearly O(1) for most sets. Because of this, it’s a great choice for a generic set as long as you don’t mind that values will need to implement Hash and Eq.

Values will have a predictable order based on the hasher being used. Unless otherwise specified, this will be the standard RandomState hasher.

Implementations§

source§

impl<A> HashSet<A, RandomState>

source

pub fn new() -> Self

Construct an empty set.

source§

impl<A> HashSet<A, RandomState>where A: Hash + Eq + Clone,

source

pub fn unit(a: A) -> Self

Construct a set with a single value.

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

impl<A, S> HashSet<A, S>

source

pub fn is_empty(&self) -> bool

Test whether a set is empty.

Time: O(1)

Examples
assert!(
  !hashset![1, 2, 3].is_empty()
);
assert!(
  HashSet::<i32>::new().is_empty()
);
source

pub fn len(&self) -> usize

Get the size of a set.

Time: O(1)

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

pub fn ptr_eq(&self, other: &Self) -> 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 with_hasher<RS>(hasher: RS) -> Selfwhere Arc<S>: From<RS>,

Construct an empty hash set using the provided hasher.

source

pub fn hasher(&self) -> &Arc<S>

Get a reference to the set’s BuildHasher.

source

pub fn new_from<A1>(&self) -> HashSet<A1, S>where A1: Hash + Eq + Clone,

Construct an empty hash set using the same hasher as the current hash set.

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 = hashset![1, 2, 3];
set.clear();
assert!(set.is_empty());
source

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

Get an iterator over the values in a hash set.

Please note that the order is consistent between sets using the same hasher, but no other ordering guarantee is offered. Items will not come out in insertion order or sort order. They will, however, come out in the same order every time for the same set.

source§

impl<A, S> HashSet<A, S>where A: Hash + Eq, S: BuildHasher,

source

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

Test if a value is part of a set.

Time: O(log n)

source

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

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 n)

source

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

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 n)

source§

impl<A, S> HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,

source

pub fn insert(&mut self, a: A) -> Option<A>

Insert a value into a set.

Time: O(log n)

source

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

Remove a value from a set if it exists.

Time: O(log n)

source

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

Construct a new set from the current set with the given value added.

Time: O(log n)

Examples
let set = hashset![123];
assert_eq!(
  set.update(456),
  hashset![123, 456]
);
source

pub fn without<BA>(&self, a: &BA) -> Selfwhere BA: Hash + Eq + ?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 retain<F>(&mut self, f: F)where F: FnMut(&A) -> bool,

Filter out values from a set which don’t satisfy a predicate.

This is slightly more efficient than filtering using an iterator, in that it doesn’t need to rehash the retained values, but it still needs to reconstruct the entire tree structure of the set.

Time: O(n log n)

Examples
let mut set = hashset![1, 2, 3];
set.retain(|v| *v > 1);
let expected = hashset![2, 3];
assert_eq!(expected, set);
source

pub fn union(self, other: Self) -> Self

Construct the union of two sets.

Time: O(n log n)

Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 2, 3};
assert_eq!(expected, set1.union(set2));
source

pub fn unions<I>(i: I) -> Selfwhere I: IntoIterator<Item = Self>, S: Default,

Construct the union of multiple sets.

Time: O(n log n)

source

pub fn difference(self, other: Self) -> Self

👎Deprecated since 2.0.1: to avoid conflicting behaviors between std and imbl, the difference alias for symmetric_difference will be removed.

Construct the symmetric difference between two sets.

This is an alias for the symmetric_difference method.

Time: O(n log n)

Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 3};
assert_eq!(expected, set1.difference(set2));
source

pub fn symmetric_difference(self, other: Self) -> Self

Construct the symmetric difference between two sets.

Time: O(n log n)

Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{1, 3};
assert_eq!(expected, set1.symmetric_difference(set2));
source

pub fn relative_complement(self, other: Self) -> Self

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

Construct the intersection of two sets.

Time: O(n log n)

Examples
let set1 = hashset!{1, 2};
let set2 = hashset!{2, 3};
let expected = hashset!{2};
assert_eq!(expected, set1.intersection(set2));

Trait Implementations§

source§

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

§

type Output = HashSet<A, S>

The resulting type after applying the + operator.
source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<A, S> Add for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,

§

type Output = HashSet<A, S>

The resulting type after applying the + operator.
source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<'a, A, S> Arbitrary<'a> for HashSet<A, S>where A: Arbitrary<'a> + Hash + Eq + Clone, S: BuildHasher + Default + 'static,

source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
source§

impl<A, S> Arbitrary for HashSet<A, S>where A: Hash + Eq + Arbitrary + Sync, S: BuildHasher + Default + Send + Sync + 'static,

source§

fn arbitrary(g: &mut Gen) -> Self

Return an arbitrary value. Read more
source§

fn shrink(&self) -> Box<dyn Iterator<Item = Self>>

Return an iterator of values that are smaller than itself. Read more
source§

impl<A, S> Clone for HashSet<A, S>where A: Clone,

source§

fn clone(&self) -> Self

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<A, S> Debug for HashSet<A, S>where A: Hash + Eq + Debug, S: BuildHasher,

source§

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

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

impl<A, S> Default for HashSet<A, S>where S: BuildHasher + Default,

source§

fn default() -> Self

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

impl<'de, A: Deserialize<'de> + Hash + Eq + Clone, S: BuildHasher + Default> Deserialize<'de> for HashSet<A, S>

source§

fn deserialize<D>(des: D) -> Result<Self, D::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

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

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, S> From<&'a [A]> for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher + Default,

source§

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

Converts to this type from the input type.
source§

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

source§

fn from(btree_set: &BTreeSet<A>) -> Self

Converts to this type from the input type.
source§

impl<'s, 'a, A, OA, SA, SB> From<&'s HashSet<&'a A, SA>> for HashSet<OA, SB>where A: ToOwned<Owned = OA> + Hash + Eq + ?Sized, OA: Borrow<A> + Hash + Eq + Clone, SA: BuildHasher, SB: BuildHasher + Default,

source§

fn from(set: &HashSet<&A, SA>) -> Self

Converts to this type from the input type.
source§

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

source§

fn from(hash_set: &HashSet<A>) -> Self

Converts to this type from the input type.
source§

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

source§

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

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

Converts to this type from the input type.
source§

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

source§

fn from(vec: &Vec<A>) -> Self

Converts to this type from the input type.
source§

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

source§

fn from(vector: &Vector<A>) -> Self

Converts to this type from the input type.
source§

impl<A, S, const N: usize> From<[A; N]> for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher + Default,

source§

fn from(arr: [A; N]) -> Self

Converts to this type from the input type.
source§

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

source§

fn from(hash_set: HashSet<A>) -> Self

Converts to this type from the input type.
source§

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

source§

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

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

Converts to this type from the input type.
source§

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

source§

fn from(vec: Vec<A>) -> Self

Converts to this type from the input type.
source§

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

source§

fn from(vector: Vector<A>) -> Self

Converts to this type from the input type.
source§

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

source§

fn from_iter<T>(i: T) -> Selfwhere T: IntoIterator<Item = RA>,

Creates a value from an iterator. Read more
source§

impl<'a, A, S> IntoIterator for &'a HashSet<A, S>where A: Hash + Eq, S: BuildHasher,

§

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) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<A, S> IntoIterator for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,

§

type Item = A

The type of the elements being iterated over.
§

type IntoIter = ConsumingIter<<HashSet<A, S> 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<'a, A, S> Mul for &'a HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,

§

type Output = HashSet<A, S>

The resulting type after applying the * operator.
source§

fn mul(self, other: Self) -> Self::Output

Performs the * operation. Read more
source§

impl<A, S> Mul for HashSet<A, S>where A: Hash + Eq + Clone, S: BuildHasher,

§

type Output = HashSet<A, S>

The resulting type after applying the * operator.
source§

fn mul(self, other: Self) -> Self::Output

Performs the * operation. Read more
source§

impl<A, S> PartialEq for HashSet<A, S>where A: Hash + Eq, S: BuildHasher + Default,

source§

fn eq(&self, other: &Self) -> 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: Serialize + Hash + Eq + Clone, S: BuildHasher + Default> Serialize for HashSet<A, S>

source§

fn serialize<Ser>(&self, ser: Ser) -> Result<Ser::Ok, Ser::Error>where Ser: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

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

source§

fn sum<I>(it: I) -> Selfwhere I: Iterator<Item = Self>,

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

impl<A, S> Eq for HashSet<A, S>where A: Hash + Eq, S: BuildHasher + Default,

Auto Trait Implementations§

§

impl<A, S> RefUnwindSafe for HashSet<A, S>where A: RefUnwindSafe, S: RefUnwindSafe,

§

impl<A, S> Send for HashSet<A, S>where A: Send + Sync, S: Send + Sync,

§

impl<A, S> Sync for HashSet<A, S>where A: Send + Sync, S: Send + Sync,

§

impl<A, S> Unpin for HashSet<A, S>where A: Unpin,

§

impl<A, S> UnwindSafe for HashSet<A, S>where A: UnwindSafe + RefUnwindSafe, S: 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> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
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.
source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,