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, or I/O. 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);

Creates a new Ham with a specific seed.

Creates a new Ham with a specific capacity and a specific seed.

Insert key into the table.

Returns true if it was already in the table

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Extends a collection with the contents of an iterator. Read more

🔬 This is a nightly-only experimental API. (extend_one)

Extends a collection with exactly one element.

🔬 This is a nightly-only experimental API. (extend_one)

Reserves capacity in a collection for the given number of additional elements. Read more

Creates a value from an iterator. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.