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
//! This is a special priority queue specifically for 128-bit hamming weight searches. //! //! This queue works by having 129 vectors, one for each distance. When we find that an internal node //! 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 a node 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, //! because of the nature of hamming weight trees, we are guaranteed that nothing will ever have a distance //! that low again, since the sum of the distance of bit substrings can only be higher than the distance of //! their parents. This means we only have to move the lowest weight vector forwards. Also, typically every //! removal will be constant time since we are incredibly likely to find all the nearest neighbor's required //! before we reach a distance of 64, which is the lowest possible max distance in the root node (distances //! of the hamming weights 0-64 and 64-128). The more things in the hamming weight tree, the less likely //! this becomes. Assuming randomly distributed features, we expect half of the features to have a distance //! below 64, so it is incredibly likely that all removals are constant time since we will always encounter //! a removal below or equal to 64. use std::fmt; use crate::InternalMap; type Distances = [Vec<(&'static InternalMap, u8)>; 129]; type NodeEntry = (u32, &'static InternalMap, u8); #[derive(Clone)] pub struct NodeQueue { distances: Distances, lowest: usize, } impl NodeQueue { /// Takes all the entries in the root node (level 0) and adds them to the queue. /// /// This is passed the (distance, tp, node). pub fn new() -> Self { Default::default() } /// This allows the queue to be cleared so that we don't need to reallocate memory. pub(crate) fn clear(&mut self) { for v in self.distances.iter_mut() { v.clear(); } self.lowest = 0; } #[inline] pub(crate) fn pop(&mut self) -> Option<NodeEntry> { loop { if let Some((node, level)) = self.distances[self.lowest].pop() { return Some((self.lowest as u32, node, level)); } else if self.lowest == 128 { return None; } else { self.lowest += 1; } } } /// Takes an iterator over (distance, tp, node, level) #[inline] pub(crate) fn add_one(&mut self, (distance, node, level): NodeEntry) { self.distances[distance as usize].push((node, level)); } /// Returns the distance if not empty. pub(crate) fn distance(&mut self) -> Option<u32> { self.distances[self.lowest..] .iter() .position(|v| !v.is_empty()) .map(|n| (n + self.lowest) as u32) } } impl fmt::Debug for NodeQueue { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { self.distances[..].fmt(formatter) } } impl Default for NodeQueue { fn default() -> Self { Self { distances: [ vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], vec![], ], lowest: 0, } } }