hwt 0.3.1

Hamming Weight Tree for finding neighbors in Hamming space
Documentation
use hwt::*;

#[test]
fn test_neighbors() {
    // That number triggers an overflow because the
    // bucket size is precisely as large as `usize`.
    let features = [
        0b1001,
        0b1010,
        0b1100,
        0b1000,
        0xAAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA_AAAA,
    ];
    let lookup = |n| features[n as usize];
    let mut hwt = Hwt::new();
    for (ix, &feature) in features.iter().enumerate() {
        hwt.insert(feature, ix as u32, lookup);
    }

    for (ix, &feature) in features.iter().enumerate() {
        let mut neighbors = hwt.nearest(feature, &lookup).take(1).collect::<Vec<u32>>();
        neighbors.sort_unstable();
        assert_eq!(&neighbors, &[ix as u32]);
    }

    let mut neighbors = hwt.search_radius(1, 0b1000, &lookup).collect::<Vec<u32>>();
    neighbors.sort_unstable();
    assert_eq!(&neighbors, &[0, 1, 2, 3]);

    let mut neighbors = hwt.search_radius(1, 0b1001, &lookup).collect::<Vec<u32>>();
    neighbors.sort_unstable();
    assert_eq!(&neighbors, &[0, 3]);

    let mut neighbors = hwt.search_radius(1, 0b1010, &lookup).collect::<Vec<u32>>();
    neighbors.sort_unstable();
    assert_eq!(&neighbors, &[1, 3]);

    let mut neighbors = hwt.search_radius(1, 0b1100, &lookup).collect::<Vec<u32>>();
    neighbors.sort_unstable();
    assert_eq!(&neighbors, &[2, 3]);

    let range = (0..).take(1 << 4);
    let mut hwt = Hwt::new();
    for i in range.clone() {
        hwt.insert(u128::from(i), i, u128::from);
    }
    for feature in range.clone() {
        assert!(
            hwt.search_radius(2, u128::from(feature), &u128::from)
                .count()
                < 8128
        );
    }
}