mih_rs/
ls.rs

1use crate::hamdist;
2use crate::CodeInt;
3
4/// Finds the neighbors in codes, whose Hamming distances to qcode are within radius.
5/// Returns the ids of the neighbor codes.
6pub fn range_search<T: CodeInt>(codes: &[T], qcode: T, radius: usize) -> Vec<u32> {
7    let mut answers = Vec::<u32>::with_capacity(1 << 8);
8    range_search_with_buf(codes, qcode, radius, &mut answers);
9    answers
10}
11
12/// Finds the neighbors in codes, whose Hamming distances to qcode are within radius.
13/// The ids of the neighbor codes are stored in answers.
14pub fn range_search_with_buf<T: CodeInt>(
15    codes: &[T],
16    qcode: T,
17    radius: usize,
18    answers: &mut Vec<u32>,
19) {
20    answers.clear();
21    for (i, &code) in codes.iter().enumerate() {
22        let dist = hamdist(code, qcode);
23        if dist <= radius {
24            answers.push(i as u32);
25        }
26    }
27}
28
29/// Computes all the Hamming distances bwtween codes and qcode.
30/// Returns the tuples of code id and the distance.
31pub fn exhaustive_search<T: CodeInt>(codes: &[T], qcode: T) -> Vec<(u32, u32)> {
32    let mut answers = Vec::<(u32, u32)>::new();
33    exhaustive_search_with_buf(codes, qcode, &mut answers);
34    answers
35}
36
37/// Computes all the Hamming distances bwtween codes and qcode.
38/// The tuples of code id and the distance are stored in answers.
39pub fn exhaustive_search_with_buf<T: CodeInt>(
40    codes: &[T],
41    qcode: T,
42    answers: &mut Vec<(u32, u32)>,
43) {
44    answers.resize(codes.len(), Default::default());
45    for i in 0..codes.len() {
46        let dist = hamdist(codes[i], qcode);
47        answers[i] = (i as u32, dist as u32);
48    }
49}