Struct hash_arr_map::set::HashArrSet [−][src]
pub struct HashArrSet<T> { /* fields omitted */ }
Expand description
A [hash set] implemented as a Ham
where the value is ()
.
As with the Ham
type, a HashArrSet
requires that the elements
implement the Eq
, Hash
, IntoIndex
and FromIndex
traits.
This can frequently be achieved by using #[derive(PartialEq, Eq, Hash)]
for Eq
and Hash
, and implementing the other two yourself.
If you implement Eq
and Hash
yourself, it is important that the following
property holds:
k1 == k2 -> hash(k1) == hash(k2)
In other words, if two keys are equal, their hashes must be equal.
It is a logic error for an item to be modified in such a way that the
item’s hash, as determined by the Hash
trait, or its equality, as
determined by the Eq
trait, changes while it is in the set. This is
normally only possible through Cell
, RefCell
, global state, I/O, or
unsafe code. The behavior resulting from such a logic error is not
specified, but will not result in undefined behavior. This could include
panics, incorrect results, aborts, memory leaks, and non-termination.
Examples
use hash_arr_map::HashArrSet; // Type inference lets us omit an explicit type signature (which // would be `HashArrSet<i32>` in this example). let mut primes = HashArrSet::new(); // Add some primes. primes.insert(1); primes.insert(2); primes.insert(3); primes.insert(5); primes.insert(7); // Check for a specific one. if !primes.contains(&6) { println!("We have {} primes, but 6 isn't one.", primes.len()); } // Actually, 1 isn't a prime primes.remove(&1); // Iterate over everything. for prime in &primes { println!("{}", prime); }
The easiest way to use HashArrSet
with a custom type is to derive
Eq
and Hash
, then implement IntoIndex
and FromIndex
manually. We must also derive PartialEq
, this will in the
future be implied by Eq
.
use core::num::NonZeroUsize; use hash_arr_map::{FromIndex, HashArrSet, Idx, IntoIndex}; #[derive(Hash, Eq, PartialEq, Debug)] struct Price { dollars: u8, cents: u8, } impl IntoIndex for Price { fn into_index(&self) -> Option<Idx<Self>> { unsafe { Idx::from_usize(((self.cents as usize) << 8) + self.dollars as usize) } } } impl FromIndex for Price { fn from_index(idx: Idx<Self>) -> Self { Self { dollars: idx.get().get() as u8, cents: (idx.get().get() >> 8) as u8, } } } let mut prices = HashArrSet::new(); prices.insert(Price { dollars: 3, cents: 50, }); prices.insert(Price { dollars: 5, cents: 99, }); prices.insert(Price { dollars: 9, cents: 99, }); prices.insert(Price { dollars: 5, cents: 99, }); // We only have 3 unique prices: assert_eq!(prices.len(), 3); // Use derived implementation to print the prices. for x in &prices { println!("{:?}", x); }
A HashArrSet
with fixed list of elements can be initialized from an array:
use hash_arr_map::HashArrSet; let primes: HashArrSet<u64> = [2, 3, 5, 7, 11, 13, 17, 18].iter().copied().collect(); // use the values stored in the set
Implementations
Creates an empty HashArrSet
.
It is created with a capacity of 0, so it will not allocate until inserted into.
Examples
use hash_arr_map::HashArrSet; let set: HashArrSet<i32> = HashArrSet::new();
Creates an empty HashArrSet
with the specified capacities.
The set will be able to hold at least the amount of elements in each section without reallocating.
Examples
use hash_arr_map::HashArrSet; let set: HashArrSet<i32> = HashArrSet::with_capacity(5, 10); assert!(set.capacity().0 >= 5 && set.capacity().1 >= 10);
Insert key
into the table.
Returns true
if it was already in the table
Trait Implementations
Extends a collection with the contents of an iterator. Read more
extend_one
)Extends a collection with exactly one element.
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
type Item = K
type Item = K
The type of the elements being iterated over.
Auto Trait Implementations
impl<T> RefUnwindSafe for HashArrSet<T> where
T: RefUnwindSafe,
impl<T> Send for HashArrSet<T> where
T: Send,
impl<T> Sync for HashArrSet<T> where
T: Sync,
impl<T> Unpin for HashArrSet<T> where
T: Unpin,
impl<T> UnwindSafe for HashArrSet<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more