tf_idf_vectorizer/utils/datastruct/map/
bit_map.rs1use std::ops::{BitAnd, BitOr, BitXor, Not};
2
3pub struct BitMap {
4 pub buckets: Vec<L2Bits>,
5 pub size: usize,
6}
7
8#[derive(Clone, Debug, Default)]
9pub struct L2Bits {
10 pub bucket_map: u64,
11 pub buckets: Box<[L1Bits]>,
12}
13
14impl BitAnd for &L2Bits {
15 type Output = L2Bits;
16
17 fn bitand(self, rhs: Self) -> Self::Output {
18 let mut result_buckets = Vec::new();
19 let mut result_map = 0u64;
20
21 let mut si = 0usize;
22 let mut ri = 0usize;
23
24 for idx in 0..64 {
25 let mask = 1u64 << idx;
26 let sh = (self.bucket_map & mask) != 0;
27 let rh = (rhs.bucket_map & mask) != 0;
28
29 if sh && rh {
30 result_map |= mask;
31 result_buckets.push(&self.buckets[si] & &rhs.buckets[ri]);
32 }
33 if sh { si += 1; }
34 if rh { ri += 1; }
35 }
36
37 L2Bits { bucket_map: result_map, buckets: result_buckets.into_boxed_slice() }
38 }
39}
40
41
42#[derive(Clone, Copy, Debug, Default)]
44#[repr(align(64))]
45pub struct L1Bits {
46 bucket: [u64; 8],
47}
48
49impl BitAnd for &L1Bits {
50 type Output = L1Bits;
51
52 fn bitand(self, rhs: Self) -> Self::Output {
53 let mut result = L1Bits::default();
54 for i in 0..8 {
55 result.bucket[i] = self.bucket[i] & rhs.bucket[i];
56 }
57 result
58 }
59}
60
61impl BitOr for &L1Bits {
62 type Output = L1Bits;
63
64 fn bitor(self, rhs: Self) -> Self::Output {
65 let mut result = L1Bits::default();
66 for i in 0..8 {
67 result.bucket[i] = self.bucket[i] | rhs.bucket[i];
68 }
69 result
70 }
71}
72
73impl BitXor for &L1Bits {
74 type Output = L1Bits;
75
76 fn bitxor(self, rhs: Self) -> Self::Output {
77 let mut result = L1Bits::default();
78 for i in 0..8 {
79 result.bucket[i] = self.bucket[i] ^ rhs.bucket[i];
80 }
81 result
82 }
83}
84
85impl Not for &L1Bits {
86 type Output = L1Bits;
87
88 fn not(self) -> Self::Output {
89 let mut result = L1Bits::default();
90 for i in 0..8 {
91 result.bucket[i] = !self.bucket[i];
92 }
93 result
94 }
95}