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
/* * This Source Code Form is subject to the terms of the Mozilla Public License, * v. 2.0. If a copy of the MPL was not distributed with this file, You can * obtain one at http://mozilla.org/MPL/2.0/. */ //! Bit sets. use contrail::{ storage::{Backtrackable, NonBacktrackable, StorageMode}, Array, Trail, TrailBuilder, }; const BLOCK_SIZE: u64 = 64; /// A bit set stored in backtrackable storage on the trail. pub type BacktrackableBitSet = BitSet<Backtrackable>; /// A bit set stored in non-backtrackable storage on the trail. pub type NonBacktrackableBitSet = BitSet<NonBacktrackable>; /// A bit set. #[derive(Clone, Copy)] pub struct BitSet<M> { blocks: Array<M, u64>, max: u64, } impl<M> BitSet<M> where M: StorageMode, { /// Creates a new bit set with a capacity of `len` with the values `0..len` in the set. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_full(&mut builder, 100); /// let trail = builder.finish(); /// /// assert_eq!(bit_set.capacity(), 100); /// for i in 0..100 { /// assert!(bit_set.contains(&trail, i)); /// } /// ``` pub fn new_full(builder: &mut TrailBuilder, len: u64) -> Self { assert!(len > 0); let max = len - 1; let num_blocks = max / BLOCK_SIZE + 1; let blocks = Array::new(builder, vec![!0; num_blocks as usize]); Self { blocks, max } } /// Creates a new bit set with a capacity of `len` with no values in the set. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let trail = builder.finish(); /// /// assert_eq!(bit_set.capacity(), 100); /// for i in 0..100 { /// assert!(!bit_set.contains(&trail, i)); /// } /// ``` pub fn new_empty(builder: &mut TrailBuilder, len: u64) -> Self { assert!(len > 0); let max = len - 1; let num_blocks = max / BLOCK_SIZE + 1; let blocks = Array::new(builder, vec![0; num_blocks as usize]); Self { blocks, max } } /// Returns the capacity of the bit set. /// /// The bit set can store elements in the range `0..capacity`. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let trail = builder.finish(); /// /// assert_eq!(bit_set.capacity(), 100); /// ``` pub fn capacity(&self) -> u64 { self.max + 1 } /// Removes all elements from the bit set. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let mut trail = builder.finish(); /// /// bit_set.insert(&mut trail, 42); /// assert!(bit_set.contains(&trail, 42)); /// /// bit_set.clear(&mut trail); /// assert!(!bit_set.contains(&trail, 42)); /// ``` pub fn clear(&self, trail: &mut Trail) { for i in 0..self.blocks.len() { self.blocks.set(trail, i, 0); } } /// Inserts an element in the bit set. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let mut trail = builder.finish(); /// /// assert!(!bit_set.contains(&trail, 42)); /// bit_set.insert(&mut trail, 42); /// assert!(bit_set.contains(&trail, 42)); /// ``` pub fn insert(&self, trail: &mut Trail, value: u64) { if value <= self.max { let index = (value / BLOCK_SIZE) as usize; let block = self.blocks.get(trail, index); self.blocks .set(trail, index, block | (1 << (value % BLOCK_SIZE))); } } /// Checks if the bit set contains the given element. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let mut trail = builder.finish(); /// /// assert!(!bit_set.contains(&trail, 42)); /// bit_set.insert(&mut trail, 42); /// assert!(bit_set.contains(&trail, 42)); /// ``` pub fn contains(&self, trail: &Trail, value: u64) -> bool { if value > self.max { false } else { let index = (value / BLOCK_SIZE) as usize; let block = self.blocks.get(trail, index); (block >> (value % BLOCK_SIZE)) & 1 == 1 } } /// Removes the given element from the bit set. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let mut trail = builder.finish(); /// /// bit_set.insert(&mut trail, 42); /// assert!(bit_set.contains(&trail, 42)); /// /// bit_set.remove(&mut trail, 42); /// assert!(!bit_set.contains(&trail, 42)); /// ``` pub fn remove(&self, trail: &mut Trail, value: u64) { if value <= self.max { let index = (value / BLOCK_SIZE) as usize; let block = self.blocks.get(trail, index); self.blocks .set(trail, index, block & !(1 << (value % BLOCK_SIZE))); } } /// Counts the number of elements in the bitset between the two given values (inclusive). /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let mut trail = builder.finish(); /// /// bit_set.insert(&mut trail, 10); /// bit_set.insert(&mut trail, 20); /// bit_set.insert(&mut trail, 30); /// /// assert_eq!(bit_set.count_between(&trail, 10, 30), 3); /// assert_eq!(bit_set.count_between(&trail, 11, 29), 1); /// ``` pub fn count_between(&self, trail: &Trail, min: u64, max: u64) -> u64 { if min <= max && min <= self.max { let max = max.min(self.max); let min_block_index = (min / BLOCK_SIZE) as usize; let max_block_index = (max / BLOCK_SIZE) as usize; let min_offset = min % BLOCK_SIZE; let max_offset = max % BLOCK_SIZE; let min_mask = !0 << min_offset; let max_mask = !0 >> (BLOCK_SIZE - max_offset - 1); if min_block_index == max_block_index { let mask = min_mask & max_mask; let block = self.blocks.get(trail, min_block_index); u64::from((block & mask).count_ones()) } else { let min_block = self.blocks.get(trail, min_block_index); let min_block_count = u64::from((min_block & min_mask).count_ones()); let max_block = self.blocks.get(trail, max_block_index); let max_block_count = u64::from((max_block & max_mask).count_ones()); let mut tot = min_block_count + max_block_count; for i in (min_block_index + 1)..max_block_index { let block = self.blocks.get(trail, i as usize); tot += u64::from(block.count_ones()); } tot } } else { 0 } } /// Returns the next element above the given element in the bit set, or `None` if no such /// element exists. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let mut trail = builder.finish(); /// /// bit_set.insert(&mut trail, 42); /// /// assert_eq!(bit_set.next_above(&trail, 33), Some(42)); /// assert_eq!(bit_set.next_above(&trail, 50), None); /// ``` pub fn next_above(&self, trail: &Trail, value: u64) -> Option<u64> { if value > self.max { None } else { let block = value / BLOCK_SIZE; let offset = value % BLOCK_SIZE; let to_skip = (self.blocks.get(trail, block as usize) >> offset).trailing_zeros() as u64; if to_skip == BLOCK_SIZE { self.next_above(trail, (block + 1) * BLOCK_SIZE) } else if value + to_skip > self.max { None } else { Some((value + to_skip) as u64) } } } /// Returns the next element below the given element in the bit set, or `None` if no such /// element exists. /// /// # Examples /// /// ``` /// use contrail::TrailBuilder; /// use contrail_collections::bit_set::BacktrackableBitSet; /// /// let mut builder = TrailBuilder::new(); /// let bit_set = BacktrackableBitSet::new_empty(&mut builder, 100); /// let mut trail = builder.finish(); /// /// bit_set.insert(&mut trail, 42); /// /// assert_eq!(bit_set.next_below(&trail, 50), Some(42)); /// assert_eq!(bit_set.next_below(&trail, 33), None); /// ``` pub fn next_below(&self, trail: &Trail, value: u64) -> Option<u64> { let value = value.min(self.max); let block = value / BLOCK_SIZE; let offset = value % BLOCK_SIZE; let to_skip = (self.blocks.get(trail, block as usize) << (BLOCK_SIZE - offset - 1)) .leading_zeros() as u64; if to_skip == BLOCK_SIZE { if block == 0 { None } else { self.next_below(trail, block * BLOCK_SIZE - 1) } } else { Some((value - to_skip) as u64) } } } #[cfg(test)] mod tests { use super::*; #[test] fn new() { let mut builder = TrailBuilder::new(); let empty = BacktrackableBitSet::new_empty(&mut builder, 10); let full = BacktrackableBitSet::new_full(&mut builder, 10); let trail = builder.finish(); assert_eq!(empty.capacity(), 10); assert_eq!(full.capacity(), 10); for i in 0..10 { assert!(!empty.contains(&trail, i)); assert!(full.contains(&trail, i)); } } #[test] fn clear() { let mut builder = TrailBuilder::new(); let bit_set = BacktrackableBitSet::new_full(&mut builder, 100); let mut trail = builder.finish(); for i in 0..100 { assert!(bit_set.contains(&trail, i)); } bit_set.clear(&mut trail); for i in 0..100 { assert!(!bit_set.contains(&trail, i)); } } #[test] fn insert_remove_contains() { let mut builder = TrailBuilder::new(); let bit_set = BacktrackableBitSet::new_empty(&mut builder, 10); let mut trail = builder.finish(); assert!(!bit_set.contains(&trail, 100)); for i in 0..10 { assert!(!bit_set.contains(&trail, i)); bit_set.insert(&mut trail, i); assert!(bit_set.contains(&trail, i)); bit_set.remove(&mut trail, i); assert!(!bit_set.contains(&trail, i)); } } #[test] fn count_between() { let mut builder = TrailBuilder::new(); let small = BacktrackableBitSet::new_full(&mut builder, 10); let medium = BacktrackableBitSet::new_full(&mut builder, 100); let large = BacktrackableBitSet::new_full(&mut builder, 1000); let trail = builder.finish(); assert_eq!(small.count_between(&trail, 0, 9), 10); assert_eq!(small.count_between(&trail, 1, 8), 8); assert_eq!(small.count_between(&trail, 5, 5), 1); assert_eq!(small.count_between(&trail, 6, 5), 0); assert_eq!(medium.count_between(&trail, 0, 99), 100); assert_eq!(medium.count_between(&trail, 1, 98), 98); assert_eq!(medium.count_between(&trail, 50, 50), 1); assert_eq!(medium.count_between(&trail, 51, 50), 0); assert_eq!(large.count_between(&trail, 0, 999), 1000); assert_eq!(large.count_between(&trail, 1, 998), 998); assert_eq!(large.count_between(&trail, 500, 500), 1); assert_eq!(large.count_between(&trail, 501, 500), 0); } #[test] fn next_above_below() { let mut builder = TrailBuilder::new(); let bit_set = BacktrackableBitSet::new_empty(&mut builder, 1000); let mut trail = builder.finish(); for i in 1..10 { bit_set.insert(&mut trail, i * 100); } for i in 0..100 { assert_eq!(bit_set.next_below(&trail, i), None); assert_eq!(bit_set.next_above(&trail, i), Some(100)); } for i in 401..500 { assert_eq!(bit_set.next_below(&trail, i), Some(400)); assert_eq!(bit_set.next_above(&trail, i), Some(500)); } for i in 901..1000 { assert_eq!(bit_set.next_below(&trail, i), Some(900)); assert_eq!(bit_set.next_above(&trail, i), None); } } }