| 1 | /// UnitID is an alias of `usize`. |
| 2 | pub type UnitID = usize; |
| 3 | |
| 4 | /// The size of `Unit` (4). |
| 5 | pub const UNIT_SIZE: usize = std::mem::size_of::<u32>(); |
| 6 | |
| 7 | /// An unit represents an element in a double-array. |
| 8 | #[derive(Copy, Clone)] |
| 9 | pub struct Unit(u32); |
| 10 | |
| 11 | /// Unit represents one node of a double array trie. The bit width of each node is 32-bits. |
| 12 | /// |
| 13 | /// The bit layout of a non-leaf node: |
| 14 | /// |
| 15 | /// 0 1 2 3 |
| 16 | /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 17 | /// +---------------+-+-+-----------------------------------------+-+ |
| 18 | /// | LABEL |H|E| OFFSET |I| |
| 19 | /// +---------------+-+-+-----------------------------------------+-+ |
| 20 | /// |
| 21 | /// LABEL 8-bits value that represents a label of the double array node. |
| 22 | /// HAS_LEAF (H) 1-bit flag that indicates whether the node has leaf nodes or not. |
| 23 | /// EXTEND_OFFSET (E) 1-bit flag that indicates whether the offset should be extended or not. |
| 24 | /// OFFSET 21-bits value that represents an offset of the double array node. |
| 25 | /// IS_LEAF (I) 1-bit flag that indicates whether the node is a leaf node or not. |
| 26 | /// This flag is always 0 in this case. |
| 27 | /// |
| 28 | /// The bit layout of a leaf node: |
| 29 | /// |
| 30 | /// 0 1 2 3 |
| 31 | /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 32 | /// +-------------------------------------------------------------+-+ |
| 33 | /// | VALUE |I| |
| 34 | /// +-------------------------------------------------------------+-+ |
| 35 | /// |
| 36 | /// VALUE 31-bits value that represents a value of the double array node. |
| 37 | /// IS_LEAF (I) 1-bit flag that indicates whether the node is a leaf node or not. |
| 38 | /// This flag is always 1 in this case. |
| 39 | impl Unit { |
| 40 | /// Creates a new Unit. |
| 41 | #[inline] |
| 42 | pub const fn new() -> Self { |
| 43 | Self(0) |
| 44 | } |
| 45 | |
| 46 | /// Creates a new Unit from `value`. |
| 47 | #[inline] |
| 48 | pub fn from_u32(value: u32) -> Self { |
| 49 | Self(value) |
| 50 | } |
| 51 | |
| 52 | /// Returns an internal 32 bit integer. |
| 53 | #[inline] |
| 54 | pub fn as_u32(&self) -> u32 { |
| 55 | self.0 |
| 56 | } |
| 57 | |
| 58 | /// Returns true if the unit have a leaf as a child unit. Otherwise, returns false. |
| 59 | #[inline] |
| 60 | pub fn has_leaf(&self) -> bool { |
| 61 | self.0 >> 8 & 1 == 1 |
| 62 | } |
| 63 | |
| 64 | /// Returns true if the unit is a leaf which have a value. Otherwise, return false. |
| 65 | #[inline] |
| 66 | pub fn is_leaf(&self) -> bool { |
| 67 | self.0 >> 31 == 1 |
| 68 | } |
| 69 | |
| 70 | /// Returns a 31 bits unsigned integer value associated with the unit. |
| 71 | #[inline] |
| 72 | pub fn value(&self) -> u32 { |
| 73 | self.0 & 0x7FFFFFFF |
| 74 | } |
| 75 | |
| 76 | /// Returns a label (<= 255) if the unit is not a leaf. Otherwise, returns an integer value greater |
| 77 | /// than 255. |
| 78 | #[inline] |
| 79 | pub fn label(&self) -> u32 { |
| 80 | self.0 & ((1 << 31) | 0xFF) |
| 81 | } |
| 82 | |
| 83 | /// Returns an offset value within the unit. If the offset extension flag is true, returns the |
| 84 | /// offset multiplied by 256. |
| 85 | #[inline] |
| 86 | pub fn offset(&self) -> u32 { |
| 87 | (self.0 >> 10) << ((self.0 & (1 << 9)) >> 6) |
| 88 | } |
| 89 | |
| 90 | /// Sets an offset to the unit. `offset` should be a value less than or equal to 29 bits. If the |
| 91 | /// `offset` is greater than 21 bits, sets the offset extension flag and the `offset` without |
| 92 | /// lower 8 bits (then, lower 8 bits of the given `offset` should be 0). |
| 93 | #[inline] |
| 94 | pub fn set_offset(&mut self, offset: u32) { |
| 95 | assert!(offset < (1u32 << 29)); |
| 96 | |
| 97 | if offset < (1u32 << 21) { |
| 98 | // don't extend offset |
| 99 | self.0 = offset << 10 | (self.0 << 23 as u32) >> 23; |
| 100 | } else { |
| 101 | // extend offset |
| 102 | assert_eq!((offset << 2) & (1 << 31), 0, "MSB of offset should be 0"); |
| 103 | assert_eq!(offset & 0xFF, 0, "lower 8 bits of offset should be 0"); |
| 104 | self.0 = offset << 2 | (1 << 9) | (self.0 << 23 as u32) >> 23; // with offset extension flag |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /// Sets a `has_leaf` flag to the unit. |
| 109 | #[inline] |
| 110 | pub fn set_has_leaf(&mut self, has_leaf: bool) { |
| 111 | self.0 = if has_leaf { |
| 112 | self.0 | 1 << 8 |
| 113 | } else { |
| 114 | self.0 & !(1 << 8) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /// Sets a label to the unit. |
| 119 | #[inline] |
| 120 | pub fn set_label(&mut self, label: u8) { |
| 121 | self.0 = (self.0 >> 8) << 8 | (label as u32) |
| 122 | } |
| 123 | |
| 124 | /// Sets a value to the unit. |
| 125 | #[inline] |
| 126 | pub fn set_value(&mut self, value: u32) { |
| 127 | self.0 = value | 1 << 31 |
| 128 | } |
| 129 | |
| 130 | /// Returns a string representation of the unit. |
| 131 | pub fn to_string(&self) -> String { |
| 132 | if self.is_leaf() { |
| 133 | // leaf node |
| 134 | format!("Unit {{ value: {} }}", self.value()).to_string() |
| 135 | } else { |
| 136 | // internal node |
| 137 | let label = self.label(); |
| 138 | format!( |
| 139 | "Unit {{ offset: {}, label: {}, has_leaf: {} }}", |
| 140 | self.offset(), |
| 141 | match label { |
| 142 | 0 => "NULL".to_string(), |
| 143 | 1..=255 => ((label as u8) as char).escape_default().to_string(), |
| 144 | _ => "INVALID".to_string(), |
| 145 | }, |
| 146 | self.has_leaf() |
| 147 | ) |
| 148 | .to_string() |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl std::fmt::Debug for Unit { |
| 154 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 155 | write!(f, "{}", self.to_string()) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #[cfg(test)] |
| 160 | mod tests { |
| 161 | use crate::Unit; |
| 162 | |
| 163 | #[test] |
| 164 | fn test_unit_value() { |
| 165 | let mut unit = Unit::new(); |
| 166 | assert_eq!(unit.value(), 0); |
| 167 | |
| 168 | unit.set_value(5); |
| 169 | assert_eq!(unit.value(), 5); |
| 170 | |
| 171 | unit.set_value((1 << 31) - 1); |
| 172 | assert_eq!(unit.value(), (1 << 31) - 1); |
| 173 | |
| 174 | unit.set_value(1 << 31); |
| 175 | assert_eq!(unit.value(), 0); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn test_label() { |
| 180 | let unit = Unit::new(); |
| 181 | assert_eq!(unit.label(), 0); |
| 182 | |
| 183 | let mut unit = Unit::new(); |
| 184 | unit.set_label(0); |
| 185 | assert_eq!(unit.label(), 0); |
| 186 | |
| 187 | let mut unit = Unit::new(); |
| 188 | unit.set_label(1); |
| 189 | assert_eq!(unit.label(), 1); |
| 190 | |
| 191 | let mut unit = Unit::new(); |
| 192 | unit.set_label(255); |
| 193 | assert_eq!(unit.label(), 255); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn test_offset() { |
| 198 | let unit = Unit::new(); |
| 199 | assert_eq!(unit.offset(), 0); |
| 200 | |
| 201 | let mut unit = Unit::new(); |
| 202 | unit.set_offset(0); |
| 203 | assert_eq!(unit.offset(), 0); |
| 204 | |
| 205 | let mut unit = Unit::new(); |
| 206 | unit.set_offset(1); |
| 207 | assert_eq!(unit.offset(), 1); |
| 208 | |
| 209 | let mut unit = Unit::new(); |
| 210 | unit.set_offset((1 << 21) - 1); |
| 211 | assert_eq!(unit.offset(), (1 << 21) - 1); |
| 212 | |
| 213 | let mut unit = Unit::new(); |
| 214 | unit.set_offset(1 << 21); |
| 215 | assert_eq!(unit.offset(), 1 << 21); |
| 216 | |
| 217 | let mut unit = Unit::new(); |
| 218 | unit.set_offset(1 << 28); |
| 219 | assert_eq!(unit.offset(), 1 << 28); |
| 220 | } |
| 221 | } |