mih-rs 0.3.1

Multi-index hashing (MIH) for neighbor searches on binary codes in the Hamming space.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
use anyhow::{anyhow, Result};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};

use crate::{hamdist, index::*, Index};

impl<T: CodeInt> Index<T> {
    /// Builds an index from binary codes.
    /// The number of blocks for multi-index is set to the optimal one
    /// estimated from the number of input codes.
    /// The input database `codes` is stolen, but the reference can be gotten with [`Index::codes()`].
    ///
    /// # Arguments
    ///
    /// - `codes`: Vector of binary codes of type [`CodeInt`].
    ///
    /// # Errors
    ///
    /// `anyhow::Error` will be returned when
    ///
    ///  - the `codes` is empty, or
    ///  - the number of entries in `codes` is more than `u32::max_value()`.
    pub fn new(codes: Vec<T>) -> Result<Self> {
        let num_codes = codes.len() as f64;
        let dimensions = T::dimensions() as f64;

        let blocks = (dimensions / num_codes.log2()).round() as usize;
        if blocks < 2 {
            Self::with_blocks(codes, 2)
        } else {
            Self::with_blocks(codes, blocks)
        }
    }

    /// Builds an index from binary codes with a manually specified number of blocks.
    /// The input database `codes` is stolen, but the reference can be gotten with [`Index::codes()`].
    ///
    /// # Arguments
    ///
    /// - `codes`: Vector of binary codes of type [`CodeInt`].
    /// - `num_blocks`: The number of blocks for multi-index.
    ///
    /// # Errors
    ///
    /// `anyhow::Error` will be returned when
    ///
    ///  - the `codes` is empty,
    ///  - the number of entries in `codes` is more than `u32::max_value()`, or
    ///  - `num_blocks` is less than 2 or more than the number of dimensions in a binary code.
    pub fn with_blocks(codes: Vec<T>, num_blocks: usize) -> Result<Self> {
        if codes.is_empty() {
            return Err(anyhow!("The input codes must not be empty"));
        }

        if (u32::max_value() as usize) < codes.len() {
            return Err(anyhow!(
                "The number of codes {} must not be no more than {}.",
                codes.len(),
                u32::max_value()
            ));
        }

        let num_dimensions = T::dimensions();
        if num_blocks < 2 || num_dimensions < num_blocks {
            return Err(anyhow!(
                "The number of blocks {} must not be in [2,{}]",
                num_blocks,
                num_dimensions
            ));
        }

        let mut masks = vec![T::default(); num_blocks];
        let mut begs = vec![0; num_blocks + 1];

        for b in 0..num_blocks {
            let dim = (b + num_dimensions) / num_blocks;
            if 64 == dim {
                masks[b] = T::from_u64(u64::max_value()).unwrap();
            } else {
                masks[b] = T::from_u64((1 << dim) - 1).unwrap();
            }
            begs[b + 1] = begs[b] + dim;
        }

        let mut tables = Vec::<sparsehash::Table>::with_capacity(num_blocks);

        for b in 0..num_blocks {
            let beg = begs[b];
            let dim = begs[b + 1] - begs[b];

            let mut table = sparsehash::Table::new(dim)?;

            for &code in &codes {
                let chunk = (code >> beg) & masks[b];
                table.count_insert(chunk.to_u64().unwrap() as usize);
            }

            for (id, &code) in codes.iter().enumerate() {
                let chunk = (code >> beg) & masks[b];
                table.data_insert(chunk.to_u64().unwrap() as usize, id as u32);
            }

            tables.push(table);
        }

        Ok(Self {
            num_blocks,
            codes,
            tables,
            masks,
            begs,
        })
    }

    /// Returns a searcher [`RangeSearcher`] to find neighbor codes
    /// whose Hamming distances to a query code are within a query radius.
    ///
    /// # Examples
    ///
    /// ```
    /// use mih_rs::Index;
    ///
    /// let codes: Vec<u64> = vec![
    ///     0b1111111111111111111111011111111111111111111111111011101111111111, // #zeros = 3
    ///     0b1111111111111111111111111111111101111111111011111111111111111111, // #zeros = 2
    ///     0b1111111011011101111111111111111101111111111111111111111111111111, // #zeros = 4
    ///     0b1111111111111101111111111111111111111000111111111110001111111110, // #zeros = 8
    ///     0b1101111111111111111111111111111111111111111111111111111111111111, // #zeros = 1
    ///     0b1111111111111111101111111011111111111111111101001110111111111111, // #zeros = 6
    ///     0b1111111111111111111111111111111111101111111111111111011111111111, // #zeros = 2
    ///     0b1110110101011011011111111111111101111111111111111000011111111111, // #zeros = 11
    /// ];
    ///
    /// let index = Index::new(codes).unwrap();
    /// let mut searcher = index.range_searcher();
    ///
    /// let qcode: u64 = 0b1111111111111111111111111111111111111111111111111111111111111111; // #zeros = 0
    /// let answers = searcher.run(qcode, 2);
    /// assert_eq!(answers, vec![1, 4, 6]);
    /// ```
    pub fn range_searcher(&self) -> RangeSearcher<T> {
        RangeSearcher {
            index: self,
            siggen: siggen::SigGenerator64::new(),
            answers: Vec::with_capacity(1 << 10),
        }
    }

    /// Returns a searcher [`TopkSearcher`] to find top-K codes that are closest to a query code.
    ///
    /// # Examples
    ///
    /// ```
    /// use mih_rs::Index;
    ///
    /// let codes: Vec<u64> = vec![
    ///     0b1111111111111111111111011111111111111111111111111011101111111111, // #zeros = 3
    ///     0b1111111111111111111111111111111101111111111011111111111111111111, // #zeros = 2
    ///     0b1111111011011101111111111111111101111111111111111111111111111111, // #zeros = 4
    ///     0b1111111111111101111111111111111111111000111111111110001111111110, // #zeros = 8
    ///     0b1101111111111111111111111111111111111111111111111111111111111111, // #zeros = 1
    ///     0b1111111111111111101111111011111111111111111101001110111111111111, // #zeros = 6
    ///     0b1111111111111111111111111111111111101111111111111111011111111111, // #zeros = 2
    ///     0b1110110101011011011111111111111101111111111111111000011111111111, // #zeros = 11
    /// ];
    ///
    /// let index = Index::new(codes).unwrap();
    /// let mut searcher = index.topk_searcher();
    ///
    /// let qcode: u64 = 0b1111111111111111111111111111111111111111111111111111111111111111; // #zeros = 0
    /// let answers = searcher.run(qcode, 4);
    /// assert_eq!(answers, vec![4, 1, 6, 0]);
    /// ```
    pub fn topk_searcher(&self) -> TopkSearcher<T> {
        TopkSearcher {
            index: self,
            siggen: siggen::SigGenerator64::new(),
            answers: Vec::with_capacity(1 << 10),
            checked: std::collections::HashSet::new(),
        }
    }

    /// Gets the reference of the input database.
    ///
    /// # Examples
    ///
    /// ```
    /// use mih_rs::Index;
    ///
    /// let codes: Vec<u64> = vec![
    ///     0b1111111111111111111111011111111111111111111111111011101111111111, // #zeros = 3
    ///     0b1111111111111111111111111111111101111111111011111111111111111111, // #zeros = 2
    ///     0b1111111011011101111111111111111101111111111111111111111111111111, // #zeros = 4
    ///     0b1111111111111101111111111111111111111000111111111110001111111110, // #zeros = 8
    ///     0b1101111111111111111111111111111111111111111111111111111111111111, // #zeros = 1
    ///     0b1111111111111111101111111011111111111111111101001110111111111111, // #zeros = 6
    ///     0b1111111111111111111111111111111111101111111111111111011111111111, // #zeros = 2
    ///     0b1110110101011011011111111111111101111111111111111000011111111111, // #zeros = 11
    /// ];
    ///
    /// let index = Index::new(codes.clone()).unwrap();
    /// assert_eq!(codes, index.codes());
    /// ```
    pub fn codes(&self) -> &[T] {
        &self.codes
    }

    /// Gets the number of defined blocks in multi-index.
    pub fn num_blocks(&self) -> usize {
        self.num_blocks
    }

    /// Serializes the index into the file.
    pub fn serialize_into<W: std::io::Write>(&self, mut writer: W) -> Result<()> {
        writer.write_u64::<LittleEndian>(self.num_blocks as u64)?;
        writer.write_u64::<LittleEndian>(self.codes.len() as u64)?;
        for x in &self.codes {
            x.serialize_into(&mut writer)?;
        }
        writer.write_u64::<LittleEndian>(self.tables.len() as u64)?;
        for x in &self.tables {
            x.serialize_into(&mut writer)?;
        }
        writer.write_u64::<LittleEndian>(self.masks.len() as u64)?;
        for x in &self.masks {
            x.serialize_into(&mut writer)?;
        }
        writer.write_u64::<LittleEndian>(self.begs.len() as u64)?;
        for &x in &self.begs {
            writer.write_u64::<LittleEndian>(x as u64)?;
        }
        Ok(())
    }

    /// Deserializes the index from the file.
    pub fn deserialize_from<R: std::io::Read>(mut reader: R) -> Result<Self> {
        let num_blocks = reader.read_u64::<LittleEndian>()? as usize;
        let codes = {
            let len = reader.read_u64::<LittleEndian>()? as usize;
            let mut codes = Vec::with_capacity(len);
            for _ in 0..len {
                codes.push(T::deserialize_from(&mut reader)?);
            }
            codes
        };
        let tables = {
            let len = reader.read_u64::<LittleEndian>()? as usize;
            let mut tables = Vec::with_capacity(len);
            for _ in 0..len {
                tables.push(sparsehash::Table::deserialize_from(&mut reader)?);
            }
            tables
        };
        let masks = {
            let len = reader.read_u64::<LittleEndian>()? as usize;
            let mut masks = Vec::with_capacity(len);
            for _ in 0..len {
                masks.push(T::deserialize_from(&mut reader)?);
            }
            masks
        };
        let begs = {
            let len = reader.read_u64::<LittleEndian>()? as usize;
            let mut begs = Vec::with_capacity(len);
            for _ in 0..len {
                begs.push(reader.read_u64::<LittleEndian>()? as usize);
            }
            begs
        };
        Ok(Self {
            num_blocks,
            codes,
            tables,
            masks,
            begs,
        })
    }

    fn get_dim(&self, b: usize) -> usize {
        self.begs[b + 1] - self.begs[b]
    }

    fn get_chunk(&self, code: T, b: usize) -> u64 {
        let chunk = (code >> self.begs[b]) & self.masks[b];
        chunk.to_u64().unwrap()
    }
}

impl<'a, T> RangeSearcher<'a, T>
where
    T: CodeInt,
{
    /// Searches neighbor codes whose Hamming distances to a query code are within a query radius.
    ///
    /// # Arguments
    ///
    /// - `qcode`: Binary code of the query.
    /// - `radius`: Threshold to be searched.
    ///
    /// # Returns
    ///
    /// A slice of ids of codes whose Hamming distances to `qcode` are within `radius`.
    /// The ids are sorted.
    /// Note that the values of the slice will be updated in the next [`RangeSearcher::run()`].
    ///
    /// # Examples
    ///
    /// ```
    /// use mih_rs::Index;
    ///
    /// let codes: Vec<u64> = vec![
    ///     0b1111111111111111111111011111111111111111111111111011101111111111, // #zeros = 3
    ///     0b1111111111111111111111111111111101111111111011111111111111111111, // #zeros = 2
    ///     0b1111111011011101111111111111111101111111111111111111111111111111, // #zeros = 4
    ///     0b1111111111111101111111111111111111111000111111111110001111111110, // #zeros = 8
    ///     0b1101111111111111111111111111111111111111111111111111111111111111, // #zeros = 1
    ///     0b1111111111111111101111111011111111111111111101001110111111111111, // #zeros = 6
    ///     0b1111111111111111111111111111111111101111111111111111011111111111, // #zeros = 2
    ///     0b1110110101011011011111111111111101111111111111111000011111111111, // #zeros = 11
    /// ];
    ///
    /// let index = Index::new(codes).unwrap();
    /// let mut searcher = index.range_searcher();
    ///
    /// let qcode: u64 = 0b1111111111111111111111111111111111111111111111111111111111111111; // #zeros = 0
    /// let answers = searcher.run(qcode, 2);
    /// assert_eq!(answers, vec![1, 4, 6]);
    /// ```
    pub fn run(&mut self, qcode: T, radius: usize) -> &[u32] {
        self.answers.clear();
        let num_blocks = self.index.num_blocks();

        for b in 0..num_blocks {
            // Based on the general pigeonhole principle
            if b + radius + 1 < num_blocks {
                continue;
            }

            let rad = (b + radius + 1 - num_blocks) / num_blocks;
            let dim = self.index.get_dim(b);
            let qcd = self.index.get_chunk(qcode, b);

            let table = &self.index.tables[b];

            // Search with r errors
            for r in 0..rad + 1 {
                self.siggen.init(qcd, dim, r);
                while self.siggen.has_next() {
                    let sig = self.siggen.next();
                    if let Some(a) = table.access(sig as usize) {
                        for v in a {
                            self.answers.push(*v as u32);
                        }
                    }
                }
            }
        }

        let mut n = 0;
        if !self.answers.is_empty() {
            self.answers.sort_unstable();
            for i in 0..self.answers.len() {
                if i == 0 || self.answers[i - 1] != self.answers[i] {
                    let dist = hamdist(qcode, self.index.codes[self.answers[i] as usize]);
                    if dist <= radius {
                        self.answers[n] = self.answers[i];
                        n += 1;
                    }
                }
            }
        }

        self.answers.resize(n, u32::default());
        &self.answers
    }
}

impl<'a, T> TopkSearcher<'a, T>
where
    T: CodeInt,
{
    /// Searches top-K codes that are closest to a query code.
    ///
    /// # Arguments
    ///
    /// - `qcode`: Binary code of the query.
    /// - `topk`: Threshold to be searched.
    ///
    /// # Returns
    ///
    /// A slice of ids of the `topk` nearest neighbor codes to `qcode`.
    /// The ids are sorted in the Hamming distances to `qcode`.
    /// Note that the values of the slice will be updated in the next [`TopkSearcher::run()`].
    ///
    /// # Examples
    ///
    /// ```
    /// use mih_rs::Index;
    ///
    /// let codes: Vec<u64> = vec![
    ///     0b1111111111111111111111011111111111111111111111111011101111111111, // #zeros = 3
    ///     0b1111111111111111111111111111111101111111111011111111111111111111, // #zeros = 2
    ///     0b1111111011011101111111111111111101111111111111111111111111111111, // #zeros = 4
    ///     0b1111111111111101111111111111111111111000111111111110001111111110, // #zeros = 8
    ///     0b1101111111111111111111111111111111111111111111111111111111111111, // #zeros = 1
    ///     0b1111111111111111101111111011111111111111111101001110111111111111, // #zeros = 6
    ///     0b1111111111111111111111111111111111101111111111111111011111111111, // #zeros = 2
    ///     0b1110110101011011011111111111111101111111111111111000011111111111, // #zeros = 11
    /// ];
    ///
    /// let index = Index::new(codes).unwrap();
    /// let mut searcher = index.topk_searcher();
    ///
    /// let qcode: u64 = 0b1111111111111111111111111111111111111111111111111111111111111111; // #zeros = 0
    /// let answers = searcher.run(qcode, 4);
    /// assert_eq!(answers, vec![4, 1, 6, 0]);
    /// ```
    pub fn run(&mut self, qcode: T, topk: usize) -> &[u32] {
        let num_blocks = self.index.num_blocks();
        let num_dimensions = T::dimensions();

        let mut n = 0;
        let mut r = 0;

        let mut counts = vec![0; num_dimensions + 1];

        self.answers
            .resize((num_dimensions + 1) * topk, u32::default());
        self.checked.clear();

        while n < topk {
            for b in 0..num_blocks {
                let dim = self.index.get_dim(b);
                let qcd = self.index.get_chunk(qcode, b);
                let table = &self.index.tables[b];

                self.siggen.init(qcd, dim, r);
                while self.siggen.has_next() {
                    let sig = self.siggen.next();
                    if let Some(a) = table.access(sig as usize) {
                        for &v in a {
                            let id = v as usize;
                            if self.checked.insert(id) {
                                let dist = hamdist(qcode, self.index.codes[id]);
                                if counts[dist] < topk {
                                    self.answers[dist * topk + counts[dist]] = id as u32;
                                }
                                counts[dist] += 1;
                            }
                        }
                    }
                }

                n += counts[r * num_blocks + b];
                if topk <= n {
                    break;
                }
            }

            r += 1;
        }

        n = 0;
        r = 0;
        while n < topk {
            let mut i = 0;
            while i < counts[r] && n < topk {
                self.answers[n] = self.answers[r * topk + i];
                i += 1;
                n += 1;
            }
            r += 1;
        }

        self.answers.resize(topk, u32::default());
        &self.answers
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ls;

    use rand::distributions::{Distribution, Standard};
    use rand::{thread_rng, Rng};

    use std::collections::BTreeSet;

    pub fn gen_random_codes<T>(size: usize) -> Vec<T>
    where
        Standard: Distribution<T>,
    {
        let mut rng = thread_rng();
        let mut codes: Vec<T> = Vec::with_capacity(size);
        for _ in 0..size {
            codes.push(rng.gen::<T>());
        }
        codes
    }

    fn naive_topk_search<T: CodeInt>(codes: &[T], qcode: T, topk: usize) -> Vec<u32> {
        let mut cands = ls::exhaustive_search(codes, qcode);
        cands.sort_by_key(|x| x.1);

        let max_dist = cands[topk - 1].1;

        let mut i = 0;
        let mut answers = Vec::new();

        while cands[i].1 <= max_dist {
            answers.push(cands[i].0);
            i += 1;
        }
        answers
    }

    fn do_range_search<T: CodeInt>(codes: Vec<T>) {
        let index = Index::new(codes).unwrap();
        let mut searcher = index.range_searcher();

        for rad in 0..6 {
            for qi in (0..10000).step_by(100) {
                let qcode = index.codes()[qi];
                let ans1 = ls::range_search(index.codes(), qcode, rad);
                let ans2 = searcher.run(qcode, rad);
                assert_eq!(ans1, ans2);
            }
        }
    }

    fn do_topk_search<T: CodeInt>(codes: Vec<T>) {
        let index = Index::new(codes).unwrap();
        let mut searcher = index.topk_searcher();

        for topk in &[1, 10, 100] {
            for qi in (0..10000).step_by(100) {
                let qcode = index.codes()[qi];
                let ans1 = naive_topk_search(index.codes(), qcode, *topk);
                let ans2 = searcher.run(qcode, *topk);
                let set1: BTreeSet<u32> = ans1.into_iter().collect();
                let set2: BTreeSet<u32> = ans2.into_iter().cloned().collect();
                assert_eq!(set2.is_subset(&set1), true);
            }
        }
    }

    #[test]
    fn range_search_u8_works() {
        let codes = gen_random_codes::<u8>(10000);
        do_range_search(codes);
    }

    #[test]
    fn range_search_u16_works() {
        let codes = gen_random_codes::<u16>(10000);
        do_range_search(codes);
    }

    #[test]
    fn range_search_u32_works() {
        let codes = gen_random_codes::<u32>(10000);
        do_range_search(codes);
    }

    #[test]
    fn range_search_u64_works() {
        let codes = gen_random_codes::<u64>(10000);
        do_range_search(codes);
    }

    #[test]
    fn topk_search_u8_works() {
        let codes = gen_random_codes::<u8>(10000);
        do_topk_search(codes);
    }

    #[test]
    fn topk_search_u16_works() {
        let codes = gen_random_codes::<u16>(10000);
        do_topk_search(codes);
    }

    #[test]
    fn topk_search_u32_works() {
        let codes = gen_random_codes::<u32>(10000);
        do_topk_search(codes);
    }

    #[test]
    fn topk_search_u64_works() {
        let codes = gen_random_codes::<u64>(10000);
        do_topk_search(codes);
    }

    #[test]
    fn serialize_u8_works() {
        let codes = gen_random_codes::<u8>(10000);
        let index = Index::new(codes).unwrap();

        let mut data = vec![];
        index.serialize_into(&mut data).unwrap();
        let other = Index::<u8>::deserialize_from(&data[..]).unwrap();

        assert_eq!(index, other);
    }

    #[test]
    fn serialize_u16_works() {
        let codes = gen_random_codes::<u16>(10000);
        let index = Index::new(codes).unwrap();

        let mut data = vec![];
        index.serialize_into(&mut data).unwrap();
        let other = Index::<u16>::deserialize_from(&data[..]).unwrap();

        assert_eq!(index, other);
    }

    #[test]
    fn serialize_u32_works() {
        let codes = gen_random_codes::<u32>(10000);
        let index = Index::new(codes).unwrap();

        let mut data = vec![];
        index.serialize_into(&mut data).unwrap();
        let other = Index::<u32>::deserialize_from(&data[..]).unwrap();

        assert_eq!(index, other);
    }

    #[test]
    fn serialize_u64_works() {
        let codes = gen_random_codes::<u64>(10000);
        let index = Index::new(codes).unwrap();

        let mut data = vec![];
        index.serialize_into(&mut data).unwrap();
        let other = Index::<u64>::deserialize_from(&data[..]).unwrap();

        assert_eq!(index, other);
    }
}