Struct HammingHeap

Source
pub struct HammingHeap<T> { /* private fields */ }
Expand description

This is a special heap specifically for hamming space searches.

This queue works by having n-bits + 1 vectors, one for each hamming distance. When we find that any item achieves a distance of n at the least, we place the index of that node into the vector associated with that distance. Any time we take an item off, we place all of its children into the appropriate distance priorities.

We maintain the lowest weight vector at any given time in the queue. When a vector runs out, we iterate until we find the next-best non-empty distance vector.

To use this you will need to call set_distances before use. This should be passed the maximum number of distances. Please keep in mind that the maximum number of hamming distances between an n bit number is n + 1. An example would be:

assert_eq!((0u128 ^ !0).count_ones(), 128);

So make sure you use n + 1 as your distances or else you may encounter a runtime panic.

use hamming_heap::HammingHeap;
let mut candidates = HammingHeap::new_distances(129);
candidates.push((0u128 ^ !0u128).count_ones(), ());

Implementations§

Source§

impl<T> HammingHeap<T>

Source

pub fn new() -> Self

Source

pub fn new_distances(distances: usize) -> Self

Automatically initializes self with distances distances.

Source

pub fn clear(&mut self)

This allows the queue to be cleared so that we don’t need to reallocate memory.

Source

pub fn set_distances(&mut self, distances: usize)

Set number of distances. Also clears the heap.

This does not preserve the allocated memory, so don’t call this on each search.

If you have a 128-bit number, keep in mind that it has 129 distances because 128 is one of the possible distances.

Source

pub fn pop(&mut self) -> Option<(u32, T)>

This removes the nearest candidate from the queue.

Source

pub fn push(&mut self, distance: u32, node: T)

Inserts a node.

Source

pub fn best(&self) -> Option<u32>

Returns the best distance if not empty.

Source

pub fn iter(&self) -> impl Iterator<Item = (u32, &T)>

Iterate over the entire queue in best-to-worse order.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (u32, &mut T)>

Iterate over the entire queue in best-to-worse order.

Trait Implementations§

Source§

impl<T: Clone> Clone for HammingHeap<T>

Source§

fn clone(&self) -> HammingHeap<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for HammingHeap<T>

Source§

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

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

impl<T> Default for HammingHeap<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> Freeze for HammingHeap<T>

§

impl<T> RefUnwindSafe for HammingHeap<T>
where T: RefUnwindSafe,

§

impl<T> Send for HammingHeap<T>
where T: Send,

§

impl<T> Sync for HammingHeap<T>
where T: Sync,

§

impl<T> Unpin for HammingHeap<T>
where T: Unpin,

§

impl<T> UnwindSafe for HammingHeap<T>
where 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<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> 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.