Skip to main content

Function2

Struct Function2 

Source
pub struct Function2<SS, SC = ShiftOnlyWrapped, CA = DefaultCompressedArray, S = BuildDefaultSeededHasher>
where SS: SeedSize,
{ /* private fields */ }
Expand description

PHast (Perfect Hashing made fast) - Minimal Perfect Hash Function with very fast evaluation and size below 2 bits/key developed by Piotr Beling and Peter Sanders.

The last layer (when the number of keys is small) is constructed using regular PHast. This makes Function2 compatible with almost all SeedChoosers (including non-wrapping ShiftOnly).

It can be used with the following SeedChooser (which specify a particular PHast variant): [ShiftOnly] (PHast+ without wrapping), ShiftOnlyWrapped (PHast+ with wrapping), [ShiftSeedWrapped] (PHast/PHast+ hybrid), SeedOnly (regular PHast).

Note that some SeedChoosers can also be used with Function.

See: Piotr Beling, Peter Sanders, PHast - Perfect Hashing made fast, 2025, https://arxiv.org/abs/2504.17918

Implementations§

Source§

impl<SS: SeedSize, SC: SeedChooser, CA: CompressedArray, S: BuildSeededHasher> Function2<SS, SC, CA, S>

Source

pub fn get<K>(&self, key: &K) -> usize
where K: Hash + ?Sized,

Returns value assigned to the given key.

The returned value is in the range from 0 (inclusive) to the number of elements in the input key collection (exclusive). key must come from the input key collection given during construction.

Source

pub fn with_vec_p_hash_sc<K>( keys: Vec<K>, params: &Params<SS>, hasher: S, seed_chooser: SC, ) -> Self
where K: Hash,

Constructs [Function] for given keys, using a single thread and given parameters: number of bits per seed, average bucket size (equals bucket_size100/100.0) and hasher.

bits_per_seed_to_100_bucket_size can be used to calculate good bucket_size100. keys cannot contain duplicates.

Source

pub fn with_vec_p_threads_hash_sc<K>( keys: Vec<K>, params: &Params<SS>, threads_num: usize, hasher: S, seed_chooser: SC, ) -> Self
where K: Hash + Sync + Send, S: Sync, SC: Sync,

Constructs [Function] for given keys, using multiple (given number of) threads and given parameters: number of bits per seed, average bucket size (equals bucket_size100/100.0) and hasher.

bits_per_seed_to_100_bucket_size can be used to calculate good bucket_size100. keys cannot contain duplicates.

Source

pub fn with_slice_p_hash_sc<K>( keys: &[K], params: &Params<SS>, hasher: S, seed_chooser: SC, ) -> Self
where K: Hash + Clone,

Constructs [Function] for given keys, using a single thread and given parameters: number of bits per seed, average bucket size (equals bucket_size100/100.0) and hasher.

bits_per_seed_to_100_bucket_size can be used to calculate good bucket_size100. keys cannot contain duplicates.

Source

pub fn with_slice_p_threads_hash_sc<K>( keys: &[K], params: &Params<SS>, threads_num: usize, hasher: S, seed_chooser: SC, ) -> Self
where K: Hash + Sync + Send + Clone, S: Sync, SC: Sync,

Constructs [Function] for given keys, using multiple (given number of) threads and given parameters: number of bits per seed, average bucket size (equals bucket_size100/100.0) and hasher.

bits_per_seed_to_100_bucket_size can be used to calculate good bucket_size100. keys cannot contain duplicates.

Source

pub fn minimal_output_range(&self, num_of_keys: usize) -> usize

Returns output range of minimal (perfect or k-perfect) function for given number of keys, i.e. 1 + maximum value that minimal function can return.

Source

pub fn output_range(&self) -> usize

Returns output range of self, i.e. 1 + maximum value that self can return.

Source

pub fn write(&self, output: &mut dyn Write) -> Result<()>

Writes self to the output.

Source

pub fn write_bytes(&self) -> usize

Returns number of bytes which write will write.

Source

pub fn read_with_hasher_sc( input: &mut dyn Read, hasher: S, seed_chooser: SC, ) -> Result<Self>

Read Self from the input. hasher and seed_chooser must be the same as used by the structure written.

Source§

impl<SS: SeedSize> Function2<SS, ShiftOnlyWrapped, DefaultCompressedArray, BuildDefaultSeededHasher>

Source

pub fn read(input: &mut dyn Read) -> Result<Self>

Read Self from the input. Uses default hasher and seed chooser.

Source§

impl Function2<Bits8, ShiftOnlyWrapped, DefaultCompressedArray, BuildDefaultSeededHasher>

Source

pub fn from_vec_st<K>(keys: Vec<K>) -> Self
where K: Hash,

Constructs [Function] for given keys, using a single thread.

keys cannot contain duplicates.

Source

pub fn from_vec_mt<K>(keys: Vec<K>) -> Self
where K: Hash + Send + Sync,

Constructs [Function] for given keys, using multiple threads.

keys cannot contain duplicates.

Source

pub fn from_slice_st<K>(keys: &[K]) -> Self
where K: Hash + Clone,

Constructs [Function] for given keys, using a single thread.

keys cannot contain duplicates.

Source

pub fn from_slice_mt<K>(keys: &[K]) -> Self
where K: Hash + Clone + Send + Sync,

Constructs [Function] for given keys, using multiple threads.

keys cannot contain duplicates.

Trait Implementations§

Source§

impl<SC, SS: SeedSize, CA, S> GetSize for Function2<SS, SC, CA, S>
where CA: GetSize,

Source§

const USES_DYN_MEM: bool = true

true if and only if the variables of this type can use dynamic (heap) memory.
Source§

fn size_bytes_dyn(&self) -> usize

Returns approximate number of bytes occupied by dynamic (heap) part of self. Same as self.size_bytes() - std::mem::size_of_val(self).
Source§

fn size_bytes_content_dyn(&self) -> usize

Returns approximate number of bytes occupied by dynamic (heap) part of self content. It usually equals to size_bytes_dyn(). However, sometimes it is smaller by the amount of memory reserved but not yet used (e.g., size_bytes_content_dyn() only takes into account the length of the vector and not its capacity).
Source§

fn size_bytes(&self) -> usize

Returns approximate, total (including heap memory) number of bytes occupied by self.

Auto Trait Implementations§

§

impl<SS, SC, CA, S> Freeze for Function2<SS, SC, CA, S>
where CA: Freeze, S: Freeze, SC: Freeze, SS: Freeze,

§

impl<SS, SC, CA, S> RefUnwindSafe for Function2<SS, SC, CA, S>

§

impl<SS, SC, CA, S> Send for Function2<SS, SC, CA, S>
where CA: Send, S: Send, SC: Send, SS: Send,

§

impl<SS, SC, CA, S> Sync for Function2<SS, SC, CA, S>
where CA: Sync, S: Sync, SC: Sync,

§

impl<SS, SC, CA, S> Unpin for Function2<SS, SC, CA, S>
where CA: Unpin, S: Unpin, SC: Unpin, SS: Unpin,

§

impl<SS, SC, CA, S> UnsafeUnpin for Function2<SS, SC, CA, S>

§

impl<SS, SC, CA, S> UnwindSafe for Function2<SS, SC, CA, S>

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> CastableFrom<T> for T

Source§

fn cast_from(value: T) -> T

Call Self as W
Source§

impl<T, U> CastableInto<U> for T
where U: CastableFrom<T>,

Source§

fn cast(self) -> U

Call W::cast_from(self)
Source§

impl<T> DowncastableFrom<T> for T

Source§

fn downcast_from(value: T) -> T

Truncate the current UnsignedInt to a possibly smaller size
Source§

impl<T, U> DowncastableInto<U> for T
where U: DowncastableFrom<T>,

Source§

fn downcast(self) -> U

Call W::downcast_from(self)
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Splat<T> for T

Source§

fn splat(value: T) -> T

Source§

impl<T> To<T> for T

Source§

fn to(self) -> T

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.
Source§

impl<T> UpcastableFrom<T> for T

Source§

fn upcast_from(value: T) -> T

Extend the current UnsignedInt to a possibly bigger size.
Source§

impl<T, U> UpcastableInto<U> for T
where U: UpcastableFrom<T>,

Source§

fn upcast(self) -> U

Call W::upcast_from(self)
Source§

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

Source§

fn vzip(self) -> V