| 1 |
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution and at |
| 3 |
// http://rust-lang.org/COPYRIGHT. |
| 4 |
// |
| 5 |
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 |
// option. This file may not be copied, modified, or distributed |
| 9 |
// except according to those terms. |
| 10 |
|
| 11 |
//! # Description |
| 12 |
//! |
| 13 |
//! An implementation of a set using a bit vector as an underlying |
| 14 |
//! representation for holding unsigned numerical elements. |
| 15 |
//! |
| 16 |
//! It should also be noted that the amount of storage necessary for holding a |
| 17 |
//! set of objects is proportional to the maximum of the objects when viewed |
| 18 |
//! as a `usize`. |
| 19 |
//! |
| 20 |
//! # Examples |
| 21 |
//! |
| 22 |
//! ``` |
| 23 |
//! use bit_set::BitSet; |
| 24 |
//! |
| 25 |
//! // It's a regular set |
| 26 |
//! let mut s = BitSet::new(); |
| 27 |
//! s.insert(0); |
| 28 |
//! s.insert(3); |
| 29 |
//! s.insert(7); |
| 30 |
//! |
| 31 |
//! s.remove(7); |
| 32 |
//! |
| 33 |
//! if !s.contains(7) { |
| 34 |
//! println!("There is no 7"); |
| 35 |
//! } |
| 36 |
//! |
| 37 |
//! // Can initialize from a `BitVec` |
| 38 |
//! let other = BitSet::from_bytes(&[0b11010000]); |
| 39 |
//! |
| 40 |
//! s.union_with(&other); |
| 41 |
//! |
| 42 |
//! // Print 0, 1, 3 in some order |
| 43 |
//! for x in s.iter() { |
| 44 |
//! println!("{}", x); |
| 45 |
//! } |
| 46 |
//! |
| 47 |
//! // Can convert back to a `BitVec` |
| 48 |
//! let bv = s.into_bit_vec(); |
| 49 |
//! assert!(bv[3]); |
| 50 |
//! ``` |
| 51 |
#![doc(html_root_url = "https://docs.rs/bit-set/0.8.0")] |
| 52 |
#![no_std] |
| 53 |
|
| 54 |
extern crate bit_vec; |
| 55 |
|
| 56 |
#[cfg(feature = "serde")] |
| 57 |
extern crate serde; |
| 58 |
|
| 59 |
#[cfg(any(test, feature = "std"))] |
| 60 |
extern crate std; |
| 61 |
|
| 62 |
use bit_vec::{BitBlock, BitVec, Blocks}; |
| 63 |
use core::cmp; |
| 64 |
use core::cmp::Ordering; |
| 65 |
use core::fmt; |
| 66 |
use core::hash; |
| 67 |
use core::iter::{self, Chain, Enumerate, FromIterator, Repeat, Skip, Take}; |
| 68 |
|
| 69 |
type MatchWords<'a, B> = Chain<Enumerate<Blocks<'a, B>>, Skip<Take<Enumerate<Repeat<B>>>>>; |
| 70 |
|
| 71 |
/// Computes how many blocks are needed to store that many bits |
| 72 |
fn blocks_for_bits<B: BitBlock>(bits: usize) -> usize { |
| 73 |
// If we want 17 bits, dividing by 32 will produce 0. So we add 1 to make sure we |
| 74 |
// reserve enough. But if we want exactly a multiple of 32, this will actually allocate |
| 75 |
// one too many. So we need to check if that's the case. We can do that by computing if |
| 76 |
// bitwise AND by `32 - 1` is 0. But LLVM should be able to optimize the semantically |
| 77 |
// superior modulo operator on a power of two to this. |
| 78 |
// |
| 79 |
// Note that we can technically avoid this branch with the expression |
| 80 |
// `(nbits + BITS - 1) / 32::BITS`, but if nbits is almost usize::MAX this will overflow. |
| 81 |
if bits % B::bits() == 0 { |
| 82 |
bits / B::bits() |
| 83 |
} else { |
| 84 |
bits / B::bits() + 1 |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[allow(clippy::iter_skip_zero)] |
| 89 |
// Take two BitVec's, and return iterators of their words, where the shorter one |
| 90 |
// has been padded with 0's |
| 91 |
fn match_words<'a, 'b, B: BitBlock>( |
| 92 |
a: &'a BitVec<B>, |
| 93 |
b: &'b BitVec<B>, |
| 94 |
) -> (MatchWords<'a, B>, MatchWords<'b, B>) { |
| 95 |
let a_len = a.storage().len(); |
| 96 |
let b_len = b.storage().len(); |
| 97 |
|
| 98 |
// have to uselessly pretend to pad the longer one for type matching |
| 99 |
if a_len < b_len { |
| 100 |
( |
| 101 |
a.blocks() |
| 102 |
.enumerate() |
| 103 |
.chain(iter::repeat(B::zero()).enumerate().take(b_len).skip(a_len)), |
| 104 |
b.blocks() |
| 105 |
.enumerate() |
| 106 |
.chain(iter::repeat(B::zero()).enumerate().take(0).skip(0)), |
| 107 |
) |
| 108 |
} else { |
| 109 |
( |
| 110 |
a.blocks() |
| 111 |
.enumerate() |
| 112 |
.chain(iter::repeat(B::zero()).enumerate().take(0).skip(0)), |
| 113 |
b.blocks() |
| 114 |
.enumerate() |
| 115 |
.chain(iter::repeat(B::zero()).enumerate().take(a_len).skip(b_len)), |
| 116 |
) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] |
| 121 |
pub struct BitSet<B = u32> { |
| 122 |
bit_vec: BitVec<B>, |
| 123 |
} |
| 124 |
|
| 125 |
impl<B: BitBlock> Clone for BitSet<B> { |
| 126 |
fn clone(&self) -> Self { |
| 127 |
BitSet { |
| 128 |
bit_vec: self.bit_vec.clone(), |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
fn clone_from(&mut self, other: &Self) { |
| 133 |
self.bit_vec.clone_from(&other.bit_vec); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl<B: BitBlock> Default for BitSet<B> { |
| 138 |
#[inline] |
| 139 |
fn default() -> Self { |
| 140 |
BitSet { |
| 141 |
bit_vec: Default::default(), |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
impl<B: BitBlock> FromIterator<usize> for BitSet<B> { |
| 147 |
fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> Self { |
| 148 |
let mut ret = Self::default(); |
| 149 |
ret.extend(iter); |
| 150 |
ret |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
impl<B: BitBlock> Extend<usize> for BitSet<B> { |
| 155 |
#[inline] |
| 156 |
fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I) { |
| 157 |
for i in iter { |
| 158 |
self.insert(i); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
impl<B: BitBlock> PartialOrd for BitSet<B> { |
| 164 |
#[inline] |
| 165 |
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 166 |
Some(self.cmp(other)) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
impl<B: BitBlock> Ord for BitSet<B> { |
| 171 |
#[inline] |
| 172 |
fn cmp(&self, other: &Self) -> Ordering { |
| 173 |
self.iter().cmp(other) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
impl<B: BitBlock> PartialEq for BitSet<B> { |
| 178 |
#[inline] |
| 179 |
fn eq(&self, other: &Self) -> bool { |
| 180 |
self.iter().eq(other) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
impl<B: BitBlock> Eq for BitSet<B> {} |
| 185 |
|
| 186 |
impl BitSet<u32> { |
| 187 |
/// Creates a new empty `BitSet`. |
| 188 |
/// |
| 189 |
/// # Examples |
| 190 |
/// |
| 191 |
/// ``` |
| 192 |
/// use bit_set::BitSet; |
| 193 |
/// |
| 194 |
/// let mut s = BitSet::new(); |
| 195 |
/// ``` |
| 196 |
#[inline] |
| 197 |
pub fn new() -> Self { |
| 198 |
Self::default() |
| 199 |
} |
| 200 |
|
| 201 |
/// Creates a new `BitSet` with initially no contents, able to |
| 202 |
/// hold `nbits` elements without resizing. |
| 203 |
/// |
| 204 |
/// # Examples |
| 205 |
/// |
| 206 |
/// ``` |
| 207 |
/// use bit_set::BitSet; |
| 208 |
/// |
| 209 |
/// let mut s = BitSet::with_capacity(100); |
| 210 |
/// assert!(s.capacity() >= 100); |
| 211 |
/// ``` |
| 212 |
#[inline] |
| 213 |
pub fn with_capacity(nbits: usize) -> Self { |
| 214 |
let bit_vec = BitVec::from_elem(nbits, false); |
| 215 |
Self::from_bit_vec(bit_vec) |
| 216 |
} |
| 217 |
|
| 218 |
/// Creates a new `BitSet` from the given bit vector. |
| 219 |
/// |
| 220 |
/// # Examples |
| 221 |
/// |
| 222 |
/// ``` |
| 223 |
/// extern crate bit_vec; |
| 224 |
/// extern crate bit_set; |
| 225 |
/// |
| 226 |
/// fn main() { |
| 227 |
/// use bit_vec::BitVec; |
| 228 |
/// use bit_set::BitSet; |
| 229 |
/// |
| 230 |
/// let bv = BitVec::from_bytes(&[0b01100000]); |
| 231 |
/// let s = BitSet::from_bit_vec(bv); |
| 232 |
/// |
| 233 |
/// // Print 1, 2 in arbitrary order |
| 234 |
/// for x in s.iter() { |
| 235 |
/// println!("{}", x); |
| 236 |
/// } |
| 237 |
/// } |
| 238 |
/// ``` |
| 239 |
#[inline] |
| 240 |
pub fn from_bit_vec(bit_vec: BitVec) -> Self { |
| 241 |
BitSet { bit_vec } |
| 242 |
} |
| 243 |
|
| 244 |
pub fn from_bytes(bytes: &[u8]) -> Self { |
| 245 |
BitSet { |
| 246 |
bit_vec: BitVec::from_bytes(bytes), |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
impl<B: BitBlock> BitSet<B> { |
| 252 |
/// Returns the capacity in bits for this bit vector. Inserting any |
| 253 |
/// element less than this amount will not trigger a resizing. |
| 254 |
/// |
| 255 |
/// # Examples |
| 256 |
/// |
| 257 |
/// ``` |
| 258 |
/// use bit_set::BitSet; |
| 259 |
/// |
| 260 |
/// let mut s = BitSet::with_capacity(100); |
| 261 |
/// assert!(s.capacity() >= 100); |
| 262 |
/// ``` |
| 263 |
#[inline] |
| 264 |
pub fn capacity(&self) -> usize { |
| 265 |
self.bit_vec.capacity() |
| 266 |
} |
| 267 |
|
| 268 |
/// Reserves capacity for the given `BitSet` to contain `len` distinct elements. In the case |
| 269 |
/// of `BitSet` this means reallocations will not occur as long as all inserted elements |
| 270 |
/// are less than `len`. |
| 271 |
/// |
| 272 |
/// The collection may reserve more space to avoid frequent reallocations. |
| 273 |
/// |
| 274 |
/// |
| 275 |
/// # Examples |
| 276 |
/// |
| 277 |
/// ``` |
| 278 |
/// use bit_set::BitSet; |
| 279 |
/// |
| 280 |
/// let mut s = BitSet::new(); |
| 281 |
/// s.reserve_len(10); |
| 282 |
/// assert!(s.capacity() >= 10); |
| 283 |
/// ``` |
| 284 |
pub fn reserve_len(&mut self, len: usize) { |
| 285 |
let cur_len = self.bit_vec.len(); |
| 286 |
if len >= cur_len { |
| 287 |
self.bit_vec.reserve(len - cur_len); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/// Reserves the minimum capacity for the given `BitSet` to contain `len` distinct elements. |
| 292 |
/// In the case of `BitSet` this means reallocations will not occur as long as all inserted |
| 293 |
/// elements are less than `len`. |
| 294 |
/// |
| 295 |
/// Note that the allocator may give the collection more space than it requests. Therefore |
| 296 |
/// capacity can not be relied upon to be precisely minimal. Prefer `reserve_len` if future |
| 297 |
/// insertions are expected. |
| 298 |
/// |
| 299 |
/// |
| 300 |
/// # Examples |
| 301 |
/// |
| 302 |
/// ``` |
| 303 |
/// use bit_set::BitSet; |
| 304 |
/// |
| 305 |
/// let mut s = BitSet::new(); |
| 306 |
/// s.reserve_len_exact(10); |
| 307 |
/// assert!(s.capacity() >= 10); |
| 308 |
/// ``` |
| 309 |
pub fn reserve_len_exact(&mut self, len: usize) { |
| 310 |
let cur_len = self.bit_vec.len(); |
| 311 |
if len >= cur_len { |
| 312 |
self.bit_vec.reserve_exact(len - cur_len); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
/// Consumes this set to return the underlying bit vector. |
| 317 |
/// |
| 318 |
/// # Examples |
| 319 |
/// |
| 320 |
/// ``` |
| 321 |
/// use bit_set::BitSet; |
| 322 |
/// |
| 323 |
/// let mut s = BitSet::new(); |
| 324 |
/// s.insert(0); |
| 325 |
/// s.insert(3); |
| 326 |
/// |
| 327 |
/// let bv = s.into_bit_vec(); |
| 328 |
/// assert!(bv[0]); |
| 329 |
/// assert!(bv[3]); |
| 330 |
/// ``` |
| 331 |
#[inline] |
| 332 |
pub fn into_bit_vec(self) -> BitVec<B> { |
| 333 |
self.bit_vec |
| 334 |
} |
| 335 |
|
| 336 |
/// Returns a reference to the underlying bit vector. |
| 337 |
/// |
| 338 |
/// # Examples |
| 339 |
/// |
| 340 |
/// ``` |
| 341 |
/// use bit_set::BitSet; |
| 342 |
/// |
| 343 |
/// let mut set = BitSet::new(); |
| 344 |
/// set.insert(0); |
| 345 |
/// |
| 346 |
/// let bv = set.get_ref(); |
| 347 |
/// assert_eq!(bv[0], true); |
| 348 |
/// ``` |
| 349 |
#[inline] |
| 350 |
pub fn get_ref(&self) -> &BitVec<B> { |
| 351 |
&self.bit_vec |
| 352 |
} |
| 353 |
|
| 354 |
/// Returns a mutable reference to the underlying bit vector. |
| 355 |
/// |
| 356 |
/// # Examples |
| 357 |
/// |
| 358 |
/// ``` |
| 359 |
/// use bit_set::BitSet; |
| 360 |
/// |
| 361 |
/// let mut set = BitSet::new(); |
| 362 |
/// set.insert(0); |
| 363 |
/// set.insert(3); |
| 364 |
/// |
| 365 |
/// { |
| 366 |
/// let bv = set.get_mut(); |
| 367 |
/// bv.set(1, true); |
| 368 |
/// } |
| 369 |
/// |
| 370 |
/// assert!(set.contains(0)); |
| 371 |
/// assert!(set.contains(1)); |
| 372 |
/// assert!(set.contains(3)); |
| 373 |
/// ``` |
| 374 |
#[inline] |
| 375 |
pub fn get_mut(&mut self) -> &mut BitVec<B> { |
| 376 |
&mut self.bit_vec |
| 377 |
} |
| 378 |
|
| 379 |
#[inline] |
| 380 |
fn other_op<F>(&mut self, other: &Self, mut f: F) |
| 381 |
where |
| 382 |
F: FnMut(B, B) -> B, |
| 383 |
{ |
| 384 |
// Unwrap BitVecs |
| 385 |
let self_bit_vec = &mut self.bit_vec; |
| 386 |
let other_bit_vec = &other.bit_vec; |
| 387 |
|
| 388 |
let self_len = self_bit_vec.len(); |
| 389 |
let other_len = other_bit_vec.len(); |
| 390 |
|
| 391 |
// Expand the vector if necessary |
| 392 |
if self_len < other_len { |
| 393 |
self_bit_vec.grow(other_len - self_len, false); |
| 394 |
} |
| 395 |
|
| 396 |
// virtually pad other with 0's for equal lengths |
| 397 |
let other_words = { |
| 398 |
let (_, result) = match_words(self_bit_vec, other_bit_vec); |
| 399 |
result |
| 400 |
}; |
| 401 |
|
| 402 |
// Apply values found in other |
| 403 |
for (i, w) in other_words { |
| 404 |
let old = self_bit_vec.storage()[i]; |
| 405 |
let new = f(old, w); |
| 406 |
unsafe { |
| 407 |
self_bit_vec.storage_mut()[i] = new; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
/// Truncates the underlying vector to the least length required. |
| 413 |
/// |
| 414 |
/// # Examples |
| 415 |
/// |
| 416 |
/// ``` |
| 417 |
/// use bit_set::BitSet; |
| 418 |
/// |
| 419 |
/// let mut s = BitSet::new(); |
| 420 |
/// s.insert(3231); |
| 421 |
/// s.remove(3231); |
| 422 |
/// |
| 423 |
/// // Internal storage will probably be bigger than necessary |
| 424 |
/// println!("old capacity: {}", s.capacity()); |
| 425 |
/// assert!(s.capacity() >= 3231); |
| 426 |
/// |
| 427 |
/// // Now should be smaller |
| 428 |
/// s.shrink_to_fit(); |
| 429 |
/// println!("new capacity: {}", s.capacity()); |
| 430 |
/// ``` |
| 431 |
#[inline] |
| 432 |
pub fn shrink_to_fit(&mut self) { |
| 433 |
let bit_vec = &mut self.bit_vec; |
| 434 |
// Obtain original length |
| 435 |
let old_len = bit_vec.storage().len(); |
| 436 |
// Obtain coarse trailing zero length |
| 437 |
let n = bit_vec |
| 438 |
.storage() |
| 439 |
.iter() |
| 440 |
.rev() |
| 441 |
.take_while(|&&n| n == B::zero()) |
| 442 |
.count(); |
| 443 |
// Truncate away all empty trailing blocks, then shrink_to_fit |
| 444 |
let trunc_len = old_len - n; |
| 445 |
unsafe { |
| 446 |
bit_vec.storage_mut().truncate(trunc_len); |
| 447 |
bit_vec.set_len(trunc_len * B::bits()); |
| 448 |
} |
| 449 |
bit_vec.shrink_to_fit(); |
| 450 |
} |
| 451 |
|
| 452 |
/// Iterator over each usize stored in the `BitSet`. |
| 453 |
/// |
| 454 |
/// # Examples |
| 455 |
/// |
| 456 |
/// ``` |
| 457 |
/// use bit_set::BitSet; |
| 458 |
/// |
| 459 |
/// let s = BitSet::from_bytes(&[0b01001010]); |
| 460 |
/// |
| 461 |
/// // Print 1, 4, 6 in arbitrary order |
| 462 |
/// for x in s.iter() { |
| 463 |
/// println!("{}", x); |
| 464 |
/// } |
| 465 |
/// ``` |
| 466 |
#[inline] |
| 467 |
pub fn iter(&self) -> Iter<B> { |
| 468 |
Iter(BlockIter::from_blocks(self.bit_vec.blocks())) |
| 469 |
} |
| 470 |
|
| 471 |
/// Iterator over each usize stored in `self` union `other`. |
| 472 |
/// See [`union_with`] for an efficient in-place version. |
| 473 |
/// |
| 474 |
/// # Examples |
| 475 |
/// |
| 476 |
/// ``` |
| 477 |
/// use bit_set::BitSet; |
| 478 |
/// |
| 479 |
/// let a = BitSet::from_bytes(&[0b01101000]); |
| 480 |
/// let b = BitSet::from_bytes(&[0b10100000]); |
| 481 |
/// |
| 482 |
/// // Print 0, 1, 2, 4 in arbitrary order |
| 483 |
/// for x in a.union(&b) { |
| 484 |
/// println!("{}", x); |
| 485 |
/// } |
| 486 |
/// ``` |
| 487 |
/// |
| 488 |
/// [`union_with`]: Self::union_with |
| 489 |
#[inline] |
| 490 |
pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, B> { |
| 491 |
fn or<B: BitBlock>(w1: B, w2: B) -> B { |
| 492 |
w1 | w2 |
| 493 |
} |
| 494 |
|
| 495 |
Union(BlockIter::from_blocks(TwoBitPositions { |
| 496 |
set: self.bit_vec.blocks(), |
| 497 |
other: other.bit_vec.blocks(), |
| 498 |
merge: or, |
| 499 |
})) |
| 500 |
} |
| 501 |
|
| 502 |
/// Iterator over each usize stored in `self` intersect `other`. |
| 503 |
/// See [`intersect_with`] for an efficient in-place version. |
| 504 |
/// |
| 505 |
/// # Examples |
| 506 |
/// |
| 507 |
/// ``` |
| 508 |
/// use bit_set::BitSet; |
| 509 |
/// |
| 510 |
/// let a = BitSet::from_bytes(&[0b01101000]); |
| 511 |
/// let b = BitSet::from_bytes(&[0b10100000]); |
| 512 |
/// |
| 513 |
/// // Print 2 |
| 514 |
/// for x in a.intersection(&b) { |
| 515 |
/// println!("{}", x); |
| 516 |
/// } |
| 517 |
/// ``` |
| 518 |
/// |
| 519 |
/// [`intersect_with`]: Self::intersect_with |
| 520 |
#[inline] |
| 521 |
pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, B> { |
| 522 |
fn bitand<B: BitBlock>(w1: B, w2: B) -> B { |
| 523 |
w1 & w2 |
| 524 |
} |
| 525 |
let min = cmp::min(self.bit_vec.len(), other.bit_vec.len()); |
| 526 |
|
| 527 |
Intersection { |
| 528 |
iter: BlockIter::from_blocks(TwoBitPositions { |
| 529 |
set: self.bit_vec.blocks(), |
| 530 |
other: other.bit_vec.blocks(), |
| 531 |
merge: bitand, |
| 532 |
}), |
| 533 |
n: min, |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
/// Iterator over each usize stored in the `self` setminus `other`. |
| 538 |
/// See [`difference_with`] for an efficient in-place version. |
| 539 |
/// |
| 540 |
/// # Examples |
| 541 |
/// |
| 542 |
/// ``` |
| 543 |
/// use bit_set::BitSet; |
| 544 |
/// |
| 545 |
/// let a = BitSet::from_bytes(&[0b01101000]); |
| 546 |
/// let b = BitSet::from_bytes(&[0b10100000]); |
| 547 |
/// |
| 548 |
/// // Print 1, 4 in arbitrary order |
| 549 |
/// for x in a.difference(&b) { |
| 550 |
/// println!("{}", x); |
| 551 |
/// } |
| 552 |
/// |
| 553 |
/// // Note that difference is not symmetric, |
| 554 |
/// // and `b - a` means something else. |
| 555 |
/// // This prints 0 |
| 556 |
/// for x in b.difference(&a) { |
| 557 |
/// println!("{}", x); |
| 558 |
/// } |
| 559 |
/// ``` |
| 560 |
/// |
| 561 |
/// [`difference_with`]: Self::difference_with |
| 562 |
#[inline] |
| 563 |
pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, B> { |
| 564 |
fn diff<B: BitBlock>(w1: B, w2: B) -> B { |
| 565 |
w1 & !w2 |
| 566 |
} |
| 567 |
|
| 568 |
Difference(BlockIter::from_blocks(TwoBitPositions { |
| 569 |
set: self.bit_vec.blocks(), |
| 570 |
other: other.bit_vec.blocks(), |
| 571 |
merge: diff, |
| 572 |
})) |
| 573 |
} |
| 574 |
|
| 575 |
/// Iterator over each usize stored in the symmetric difference of `self` and `other`. |
| 576 |
/// See [`symmetric_difference_with`] for an efficient in-place version. |
| 577 |
/// |
| 578 |
/// # Examples |
| 579 |
/// |
| 580 |
/// ``` |
| 581 |
/// use bit_set::BitSet; |
| 582 |
/// |
| 583 |
/// let a = BitSet::from_bytes(&[0b01101000]); |
| 584 |
/// let b = BitSet::from_bytes(&[0b10100000]); |
| 585 |
/// |
| 586 |
/// // Print 0, 1, 4 in arbitrary order |
| 587 |
/// for x in a.symmetric_difference(&b) { |
| 588 |
/// println!("{}", x); |
| 589 |
/// } |
| 590 |
/// ``` |
| 591 |
/// |
| 592 |
/// [`symmetric_difference_with`]: Self::symmetric_difference_with |
| 593 |
#[inline] |
| 594 |
pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, B> { |
| 595 |
fn bitxor<B: BitBlock>(w1: B, w2: B) -> B { |
| 596 |
w1 ^ w2 |
| 597 |
} |
| 598 |
|
| 599 |
SymmetricDifference(BlockIter::from_blocks(TwoBitPositions { |
| 600 |
set: self.bit_vec.blocks(), |
| 601 |
other: other.bit_vec.blocks(), |
| 602 |
merge: bitxor, |
| 603 |
})) |
| 604 |
} |
| 605 |
|
| 606 |
/// Unions in-place with the specified other bit vector. |
| 607 |
/// |
| 608 |
/// # Examples |
| 609 |
/// |
| 610 |
/// ``` |
| 611 |
/// use bit_set::BitSet; |
| 612 |
/// |
| 613 |
/// let a = 0b01101000; |
| 614 |
/// let b = 0b10100000; |
| 615 |
/// let res = 0b11101000; |
| 616 |
/// |
| 617 |
/// let mut a = BitSet::from_bytes(&[a]); |
| 618 |
/// let b = BitSet::from_bytes(&[b]); |
| 619 |
/// let res = BitSet::from_bytes(&[res]); |
| 620 |
/// |
| 621 |
/// a.union_with(&b); |
| 622 |
/// assert_eq!(a, res); |
| 623 |
/// ``` |
| 624 |
#[inline] |
| 625 |
pub fn union_with(&mut self, other: &Self) { |
| 626 |
self.other_op(other, |w1, w2| w1 | w2); |
| 627 |
} |
| 628 |
|
| 629 |
/// Intersects in-place with the specified other bit vector. |
| 630 |
/// |
| 631 |
/// # Examples |
| 632 |
/// |
| 633 |
/// ``` |
| 634 |
/// use bit_set::BitSet; |
| 635 |
/// |
| 636 |
/// let a = 0b01101000; |
| 637 |
/// let b = 0b10100000; |
| 638 |
/// let res = 0b00100000; |
| 639 |
/// |
| 640 |
/// let mut a = BitSet::from_bytes(&[a]); |
| 641 |
/// let b = BitSet::from_bytes(&[b]); |
| 642 |
/// let res = BitSet::from_bytes(&[res]); |
| 643 |
/// |
| 644 |
/// a.intersect_with(&b); |
| 645 |
/// assert_eq!(a, res); |
| 646 |
/// ``` |
| 647 |
#[inline] |
| 648 |
pub fn intersect_with(&mut self, other: &Self) { |
| 649 |
self.other_op(other, |w1, w2| w1 & w2); |
| 650 |
} |
| 651 |
|
| 652 |
/// Makes this bit vector the difference with the specified other bit vector |
| 653 |
/// in-place. |
| 654 |
/// |
| 655 |
/// # Examples |
| 656 |
/// |
| 657 |
/// ``` |
| 658 |
/// use bit_set::BitSet; |
| 659 |
/// |
| 660 |
/// let a = 0b01101000; |
| 661 |
/// let b = 0b10100000; |
| 662 |
/// let a_b = 0b01001000; // a - b |
| 663 |
/// let b_a = 0b10000000; // b - a |
| 664 |
/// |
| 665 |
/// let mut bva = BitSet::from_bytes(&[a]); |
| 666 |
/// let bvb = BitSet::from_bytes(&[b]); |
| 667 |
/// let bva_b = BitSet::from_bytes(&[a_b]); |
| 668 |
/// let bvb_a = BitSet::from_bytes(&[b_a]); |
| 669 |
/// |
| 670 |
/// bva.difference_with(&bvb); |
| 671 |
/// assert_eq!(bva, bva_b); |
| 672 |
/// |
| 673 |
/// let bva = BitSet::from_bytes(&[a]); |
| 674 |
/// let mut bvb = BitSet::from_bytes(&[b]); |
| 675 |
/// |
| 676 |
/// bvb.difference_with(&bva); |
| 677 |
/// assert_eq!(bvb, bvb_a); |
| 678 |
/// ``` |
| 679 |
#[inline] |
| 680 |
pub fn difference_with(&mut self, other: &Self) { |
| 681 |
self.other_op(other, |w1, w2| w1 & !w2); |
| 682 |
} |
| 683 |
|
| 684 |
/// Makes this bit vector the symmetric difference with the specified other |
| 685 |
/// bit vector in-place. |
| 686 |
/// |
| 687 |
/// # Examples |
| 688 |
/// |
| 689 |
/// ``` |
| 690 |
/// use bit_set::BitSet; |
| 691 |
/// |
| 692 |
/// let a = 0b01101000; |
| 693 |
/// let b = 0b10100000; |
| 694 |
/// let res = 0b11001000; |
| 695 |
/// |
| 696 |
/// let mut a = BitSet::from_bytes(&[a]); |
| 697 |
/// let b = BitSet::from_bytes(&[b]); |
| 698 |
/// let res = BitSet::from_bytes(&[res]); |
| 699 |
/// |
| 700 |
/// a.symmetric_difference_with(&b); |
| 701 |
/// assert_eq!(a, res); |
| 702 |
/// ``` |
| 703 |
#[inline] |
| 704 |
pub fn symmetric_difference_with(&mut self, other: &Self) { |
| 705 |
self.other_op(other, |w1, w2| w1 ^ w2); |
| 706 |
} |
| 707 |
|
| 708 |
/* |
| 709 |
/// Moves all elements from `other` into `Self`, leaving `other` empty. |
| 710 |
/// |
| 711 |
/// # Examples |
| 712 |
/// |
| 713 |
/// ``` |
| 714 |
/// use bit_set::BitSet; |
| 715 |
/// |
| 716 |
/// let mut a = BitSet::new(); |
| 717 |
/// a.insert(2); |
| 718 |
/// a.insert(6); |
| 719 |
/// |
| 720 |
/// let mut b = BitSet::new(); |
| 721 |
/// b.insert(1); |
| 722 |
/// b.insert(3); |
| 723 |
/// b.insert(6); |
| 724 |
/// |
| 725 |
/// a.append(&mut b); |
| 726 |
/// |
| 727 |
/// assert_eq!(a.len(), 4); |
| 728 |
/// assert_eq!(b.len(), 0); |
| 729 |
/// assert_eq!(a, BitSet::from_bytes(&[0b01110010])); |
| 730 |
/// ``` |
| 731 |
pub fn append(&mut self, other: &mut Self) { |
| 732 |
self.union_with(other); |
| 733 |
other.clear(); |
| 734 |
} |
| 735 |
|
| 736 |
/// Splits the `BitSet` into two at the given key including the key. |
| 737 |
/// Retains the first part in-place while returning the second part. |
| 738 |
/// |
| 739 |
/// # Examples |
| 740 |
/// |
| 741 |
/// ``` |
| 742 |
/// use bit_set::BitSet; |
| 743 |
/// |
| 744 |
/// let mut a = BitSet::new(); |
| 745 |
/// a.insert(2); |
| 746 |
/// a.insert(6); |
| 747 |
/// a.insert(1); |
| 748 |
/// a.insert(3); |
| 749 |
/// |
| 750 |
/// let b = a.split_off(3); |
| 751 |
/// |
| 752 |
/// assert_eq!(a.len(), 2); |
| 753 |
/// assert_eq!(b.len(), 2); |
| 754 |
/// assert_eq!(a, BitSet::from_bytes(&[0b01100000])); |
| 755 |
/// assert_eq!(b, BitSet::from_bytes(&[0b00010010])); |
| 756 |
/// ``` |
| 757 |
pub fn split_off(&mut self, at: usize) -> Self { |
| 758 |
let mut other = BitSet::new(); |
| 759 |
|
| 760 |
if at == 0 { |
| 761 |
swap(self, &mut other); |
| 762 |
return other; |
| 763 |
} else if at >= self.bit_vec.len() { |
| 764 |
return other; |
| 765 |
} |
| 766 |
|
| 767 |
// Calculate block and bit at which to split |
| 768 |
let w = at / BITS; |
| 769 |
let b = at % BITS; |
| 770 |
|
| 771 |
// Pad `other` with `w` zero blocks, |
| 772 |
// append `self`'s blocks in the range from `w` to the end to `other` |
| 773 |
other.bit_vec.storage_mut().extend(repeat(0u32).take(w) |
| 774 |
.chain(self.bit_vec.storage()[w..].iter().cloned())); |
| 775 |
other.bit_vec.nbits = self.bit_vec.nbits; |
| 776 |
|
| 777 |
if b > 0 { |
| 778 |
other.bit_vec.storage_mut()[w] &= !0 << b; |
| 779 |
} |
| 780 |
|
| 781 |
// Sets `bit_vec.len()` and fixes the last block as well |
| 782 |
self.bit_vec.truncate(at); |
| 783 |
|
| 784 |
other |
| 785 |
} |
| 786 |
*/ |
| 787 |
|
| 788 |
/// Returns the number of set bits in this set. |
| 789 |
#[inline] |
| 790 |
pub fn len(&self) -> usize { |
| 791 |
self.bit_vec.blocks().fold(0, |acc, n| acc + n.count_ones()) |
| 792 |
} |
| 793 |
|
| 794 |
/// Returns whether there are no bits set in this set |
| 795 |
#[inline] |
| 796 |
pub fn is_empty(&self) -> bool { |
| 797 |
self.bit_vec.none() |
| 798 |
} |
| 799 |
|
| 800 |
/// Clears all bits in this set |
| 801 |
#[inline] |
| 802 |
pub fn clear(&mut self) { |
| 803 |
self.bit_vec.clear(); |
| 804 |
} |
| 805 |
|
| 806 |
/// Returns `true` if this set contains the specified integer. |
| 807 |
#[inline] |
| 808 |
pub fn contains(&self, value: usize) -> bool { |
| 809 |
let bit_vec = &self.bit_vec; |
| 810 |
value < bit_vec.len() && bit_vec[value] |
| 811 |
} |
| 812 |
|
| 813 |
/// Returns `true` if the set has no elements in common with `other`. |
| 814 |
/// This is equivalent to checking for an empty intersection. |
| 815 |
#[inline] |
| 816 |
pub fn is_disjoint(&self, other: &Self) -> bool { |
| 817 |
self.intersection(other).next().is_none() |
| 818 |
} |
| 819 |
|
| 820 |
/// Returns `true` if the set is a subset of another. |
| 821 |
#[inline] |
| 822 |
pub fn is_subset(&self, other: &Self) -> bool { |
| 823 |
let self_bit_vec = &self.bit_vec; |
| 824 |
let other_bit_vec = &other.bit_vec; |
| 825 |
let other_blocks = blocks_for_bits::<B>(other_bit_vec.len()); |
| 826 |
|
| 827 |
// Check that `self` intersect `other` is self |
| 828 |
self_bit_vec.blocks().zip(other_bit_vec.blocks()).all(|(w1, w2)| w1 & w2 == w1) && |
| 829 |
// Make sure if `self` has any more blocks than `other`, they're all 0 |
| 830 |
self_bit_vec.blocks().skip(other_blocks).all(|w| w == B::zero()) |
| 831 |
} |
| 832 |
|
| 833 |
/// Returns `true` if the set is a superset of another. |
| 834 |
#[inline] |
| 835 |
pub fn is_superset(&self, other: &Self) -> bool { |
| 836 |
other.is_subset(self) |
| 837 |
} |
| 838 |
|
| 839 |
/// Adds a value to the set. Returns `true` if the value was not already |
| 840 |
/// present in the set. |
| 841 |
pub fn insert(&mut self, value: usize) -> bool { |
| 842 |
if self.contains(value) { |
| 843 |
return false; |
| 844 |
} |
| 845 |
|
| 846 |
// Ensure we have enough space to hold the new element |
| 847 |
let len = self.bit_vec.len(); |
| 848 |
if value >= len { |
| 849 |
self.bit_vec.grow(value - len + 1, false); |
| 850 |
} |
| 851 |
|
| 852 |
self.bit_vec.set(value, true); |
| 853 |
true |
| 854 |
} |
| 855 |
|
| 856 |
/// Removes a value from the set. Returns `true` if the value was |
| 857 |
/// present in the set. |
| 858 |
pub fn remove(&mut self, value: usize) -> bool { |
| 859 |
if !self.contains(value) { |
| 860 |
return false; |
| 861 |
} |
| 862 |
|
| 863 |
self.bit_vec.set(value, false); |
| 864 |
|
| 865 |
true |
| 866 |
} |
| 867 |
|
| 868 |
/// Excludes `element` and all greater elements from the `BitSet`. |
| 869 |
pub fn truncate(&mut self, element: usize) { |
| 870 |
self.bit_vec.truncate(element); |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
impl<B: BitBlock> fmt::Debug for BitSet<B> { |
| 875 |
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 876 |
fmt.debug_set().entries(self).finish() |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
impl<B: BitBlock> hash::Hash for BitSet<B> { |
| 881 |
fn hash<H: hash::Hasher>(&self, state: &mut H) { |
| 882 |
for pos in self { |
| 883 |
pos.hash(state); |
| 884 |
} |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
#[derive(Clone)] |
| 889 |
struct BlockIter<T, B> { |
| 890 |
head: B, |
| 891 |
head_offset: usize, |
| 892 |
tail: T, |
| 893 |
} |
| 894 |
|
| 895 |
impl<T, B: BitBlock> BlockIter<T, B> |
| 896 |
where |
| 897 |
T: Iterator<Item = B>, |
| 898 |
{ |
| 899 |
fn from_blocks(mut blocks: T) -> BlockIter<T, B> { |
| 900 |
let h = blocks.next().unwrap_or_else(B::zero); |
| 901 |
BlockIter { |
| 902 |
tail: blocks, |
| 903 |
head: h, |
| 904 |
head_offset: 0, |
| 905 |
} |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
/// An iterator combining two `BitSet` iterators. |
| 910 |
#[derive(Clone)] |
| 911 |
struct TwoBitPositions<'a, B: 'a> { |
| 912 |
set: Blocks<'a, B>, |
| 913 |
other: Blocks<'a, B>, |
| 914 |
merge: fn(B, B) -> B, |
| 915 |
} |
| 916 |
|
| 917 |
/// An iterator for `BitSet`. |
| 918 |
#[derive(Clone)] |
| 919 |
pub struct Iter<'a, B: 'a>(BlockIter<Blocks<'a, B>, B>); |
| 920 |
#[derive(Clone)] |
| 921 |
pub struct Union<'a, B: 'a>(BlockIter<TwoBitPositions<'a, B>, B>); |
| 922 |
#[derive(Clone)] |
| 923 |
pub struct Intersection<'a, B: 'a> { |
| 924 |
iter: BlockIter<TwoBitPositions<'a, B>, B>, |
| 925 |
// as an optimization, we compute the maximum possible |
| 926 |
// number of elements in the intersection, and count it |
| 927 |
// down as we return elements. If we reach zero, we can |
| 928 |
// stop. |
| 929 |
n: usize, |
| 930 |
} |
| 931 |
#[derive(Clone)] |
| 932 |
pub struct Difference<'a, B: 'a>(BlockIter<TwoBitPositions<'a, B>, B>); |
| 933 |
#[derive(Clone)] |
| 934 |
pub struct SymmetricDifference<'a, B: 'a>(BlockIter<TwoBitPositions<'a, B>, B>); |
| 935 |
|
| 936 |
impl<T, B: BitBlock> Iterator for BlockIter<T, B> |
| 937 |
where |
| 938 |
T: Iterator<Item = B>, |
| 939 |
{ |
| 940 |
type Item = usize; |
| 941 |
|
| 942 |
fn next(&mut self) -> Option<usize> { |
| 943 |
while self.head == B::zero() { |
| 944 |
match self.tail.next() { |
| 945 |
Some(w) => self.head = w, |
| 946 |
None => return None, |
| 947 |
} |
| 948 |
self.head_offset += B::bits(); |
| 949 |
} |
| 950 |
|
| 951 |
// from the current block, isolate the |
| 952 |
// LSB and subtract 1, producing k: |
| 953 |
// a block with a number of set bits |
| 954 |
// equal to the index of the LSB |
| 955 |
let k = (self.head & (!self.head + B::one())) - B::one(); |
| 956 |
// update block, removing the LSB |
| 957 |
self.head = self.head & (self.head - B::one()); |
| 958 |
// return offset + (index of LSB) |
| 959 |
Some(self.head_offset + (B::count_ones(k))) |
| 960 |
} |
| 961 |
|
| 962 |
fn count(self) -> usize { |
| 963 |
self.head.count_ones() + self.tail.map(|block| block.count_ones()).sum::<usize>() |
| 964 |
} |
| 965 |
|
| 966 |
#[inline] |
| 967 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 968 |
match self.tail.size_hint() { |
| 969 |
(_, Some(h)) => (0, Some((1 + h) * B::bits())), |
| 970 |
_ => (0, None), |
| 971 |
} |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
impl<'a, B: BitBlock> Iterator for TwoBitPositions<'a, B> { |
| 976 |
type Item = B; |
| 977 |
|
| 978 |
fn next(&mut self) -> Option<B> { |
| 979 |
match (self.set.next(), self.other.next()) { |
| 980 |
(Some(a), Some(b)) => Some((self.merge)(a, b)), |
| 981 |
(Some(a), None) => Some((self.merge)(a, B::zero())), |
| 982 |
(None, Some(b)) => Some((self.merge)(B::zero(), b)), |
| 983 |
_ => None, |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
#[inline] |
| 988 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 989 |
let (a, au) = self.set.size_hint(); |
| 990 |
let (b, bu) = self.other.size_hint(); |
| 991 |
|
| 992 |
let upper = match (au, bu) { |
| 993 |
(Some(au), Some(bu)) => Some(cmp::max(au, bu)), |
| 994 |
_ => None, |
| 995 |
}; |
| 996 |
|
| 997 |
(cmp::max(a, b), upper) |
| 998 |
} |
| 999 |
} |
| 1000 |
|
| 1001 |
impl<'a, B: BitBlock> Iterator for Iter<'a, B> { |
| 1002 |
type Item = usize; |
| 1003 |
|
| 1004 |
#[inline] |
| 1005 |
fn next(&mut self) -> Option<usize> { |
| 1006 |
self.0.next() |
| 1007 |
} |
| 1008 |
#[inline] |
| 1009 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 1010 |
self.0.size_hint() |
| 1011 |
} |
| 1012 |
#[inline] |
| 1013 |
fn count(self) -> usize { |
| 1014 |
self.0.count() |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
impl<'a, B: BitBlock> Iterator for Union<'a, B> { |
| 1019 |
type Item = usize; |
| 1020 |
|
| 1021 |
#[inline] |
| 1022 |
fn next(&mut self) -> Option<usize> { |
| 1023 |
self.0.next() |
| 1024 |
} |
| 1025 |
#[inline] |
| 1026 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 1027 |
self.0.size_hint() |
| 1028 |
} |
| 1029 |
#[inline] |
| 1030 |
fn count(self) -> usize { |
| 1031 |
self.0.count() |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
impl<'a, B: BitBlock> Iterator for Intersection<'a, B> { |
| 1036 |
type Item = usize; |
| 1037 |
|
| 1038 |
#[inline] |
| 1039 |
fn next(&mut self) -> Option<usize> { |
| 1040 |
if self.n != 0 { |
| 1041 |
self.n -= 1; |
| 1042 |
self.iter.next() |
| 1043 |
} else { |
| 1044 |
None |
| 1045 |
} |
| 1046 |
} |
| 1047 |
#[inline] |
| 1048 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 1049 |
// We could invoke self.iter.size_hint() and incorporate that into the hint. |
| 1050 |
// In practice, that does not seem worthwhile because the lower bound will |
| 1051 |
// always be zero and the upper bound could only possibly less then n in a |
| 1052 |
// partially iterated iterator. However, it makes little sense ask for size_hint |
| 1053 |
// in a partially iterated iterator, so it did not seem worthwhile. |
| 1054 |
(0, Some(self.n)) |
| 1055 |
} |
| 1056 |
#[inline] |
| 1057 |
fn count(self) -> usize { |
| 1058 |
self.iter.count() |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
impl<'a, B: BitBlock> Iterator for Difference<'a, B> { |
| 1063 |
type Item = usize; |
| 1064 |
|
| 1065 |
#[inline] |
| 1066 |
fn next(&mut self) -> Option<usize> { |
| 1067 |
self.0.next() |
| 1068 |
} |
| 1069 |
#[inline] |
| 1070 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 1071 |
self.0.size_hint() |
| 1072 |
} |
| 1073 |
#[inline] |
| 1074 |
fn count(self) -> usize { |
| 1075 |
self.0.count() |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
impl<'a, B: BitBlock> Iterator for SymmetricDifference<'a, B> { |
| 1080 |
type Item = usize; |
| 1081 |
|
| 1082 |
#[inline] |
| 1083 |
fn next(&mut self) -> Option<usize> { |
| 1084 |
self.0.next() |
| 1085 |
} |
| 1086 |
#[inline] |
| 1087 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 1088 |
self.0.size_hint() |
| 1089 |
} |
| 1090 |
#[inline] |
| 1091 |
fn count(self) -> usize { |
| 1092 |
self.0.count() |
| 1093 |
} |
| 1094 |
} |
| 1095 |
|
| 1096 |
impl<'a, B: BitBlock> IntoIterator for &'a BitSet<B> { |
| 1097 |
type Item = usize; |
| 1098 |
type IntoIter = Iter<'a, B>; |
| 1099 |
|
| 1100 |
fn into_iter(self) -> Iter<'a, B> { |
| 1101 |
self.iter() |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
#[cfg(test)] |
| 1106 |
mod tests { |
| 1107 |
use super::BitSet; |
| 1108 |
use bit_vec::BitVec; |
| 1109 |
use std::cmp::Ordering::{Equal, Greater, Less}; |
| 1110 |
use std::vec::Vec; |
| 1111 |
use std::{format, vec}; |
| 1112 |
|
| 1113 |
#[test] |
| 1114 |
fn test_bit_set_show() { |
| 1115 |
let mut s = BitSet::new(); |
| 1116 |
s.insert(1); |
| 1117 |
s.insert(10); |
| 1118 |
s.insert(50); |
| 1119 |
s.insert(2); |
| 1120 |
assert_eq!("{1, 2, 10, 50}", format!("{:?}", s)); |
| 1121 |
} |
| 1122 |
|
| 1123 |
#[test] |
| 1124 |
fn test_bit_set_from_usizes() { |
| 1125 |
let usizes = vec![0, 2, 2, 3]; |
| 1126 |
let a: BitSet = usizes.into_iter().collect(); |
| 1127 |
let mut b = BitSet::new(); |
| 1128 |
b.insert(0); |
| 1129 |
b.insert(2); |
| 1130 |
b.insert(3); |
| 1131 |
assert_eq!(a, b); |
| 1132 |
} |
| 1133 |
|
| 1134 |
#[test] |
| 1135 |
fn test_bit_set_iterator() { |
| 1136 |
let usizes = vec![0, 2, 2, 3]; |
| 1137 |
let bit_vec: BitSet = usizes.into_iter().collect(); |
| 1138 |
|
| 1139 |
let idxs: Vec<_> = bit_vec.iter().collect(); |
| 1140 |
assert_eq!(idxs, [0, 2, 3]); |
| 1141 |
assert_eq!(bit_vec.iter().count(), 3); |
| 1142 |
|
| 1143 |
let long: BitSet = (0..10000).filter(|&n| n % 2 == 0).collect(); |
| 1144 |
let real: Vec<_> = (0..10000 / 2).map(|x| x * 2).collect(); |
| 1145 |
|
| 1146 |
let idxs: Vec<_> = long.iter().collect(); |
| 1147 |
assert_eq!(idxs, real); |
| 1148 |
assert_eq!(long.iter().count(), real.len()); |
| 1149 |
} |
| 1150 |
|
| 1151 |
#[test] |
| 1152 |
fn test_bit_set_frombit_vec_init() { |
| 1153 |
let bools = [true, false]; |
| 1154 |
let lengths = [10, 64, 100]; |
| 1155 |
for &b in &bools { |
| 1156 |
for &l in &lengths { |
| 1157 |
let bitset = BitSet::from_bit_vec(BitVec::from_elem(l, b)); |
| 1158 |
assert_eq!(bitset.contains(1), b); |
| 1159 |
assert_eq!(bitset.contains(l - 1), b); |
| 1160 |
assert!(!bitset.contains(l)); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
#[test] |
| 1166 |
fn test_bit_vec_masking() { |
| 1167 |
let b = BitVec::from_elem(140, true); |
| 1168 |
let mut bs = BitSet::from_bit_vec(b); |
| 1169 |
assert!(bs.contains(139)); |
| 1170 |
assert!(!bs.contains(140)); |
| 1171 |
assert!(bs.insert(150)); |
| 1172 |
assert!(!bs.contains(140)); |
| 1173 |
assert!(!bs.contains(149)); |
| 1174 |
assert!(bs.contains(150)); |
| 1175 |
assert!(!bs.contains(151)); |
| 1176 |
} |
| 1177 |
|
| 1178 |
#[test] |
| 1179 |
fn test_bit_set_basic() { |
| 1180 |
let mut b = BitSet::new(); |
| 1181 |
assert!(b.insert(3)); |
| 1182 |
assert!(!b.insert(3)); |
| 1183 |
assert!(b.contains(3)); |
| 1184 |
assert!(b.insert(4)); |
| 1185 |
assert!(!b.insert(4)); |
| 1186 |
assert!(b.contains(3)); |
| 1187 |
assert!(b.insert(400)); |
| 1188 |
assert!(!b.insert(400)); |
| 1189 |
assert!(b.contains(400)); |
| 1190 |
assert_eq!(b.len(), 3); |
| 1191 |
} |
| 1192 |
|
| 1193 |
#[test] |
| 1194 |
fn test_bit_set_intersection() { |
| 1195 |
let mut a = BitSet::new(); |
| 1196 |
let mut b = BitSet::new(); |
| 1197 |
|
| 1198 |
assert!(a.insert(11)); |
| 1199 |
assert!(a.insert(1)); |
| 1200 |
assert!(a.insert(3)); |
| 1201 |
assert!(a.insert(77)); |
| 1202 |
assert!(a.insert(103)); |
| 1203 |
assert!(a.insert(5)); |
| 1204 |
|
| 1205 |
assert!(b.insert(2)); |
| 1206 |
assert!(b.insert(11)); |
| 1207 |
assert!(b.insert(77)); |
| 1208 |
assert!(b.insert(5)); |
| 1209 |
assert!(b.insert(3)); |
| 1210 |
|
| 1211 |
let expected = [3, 5, 11, 77]; |
| 1212 |
let actual: Vec<_> = a.intersection(&b).collect(); |
| 1213 |
assert_eq!(actual, expected); |
| 1214 |
assert_eq!(a.intersection(&b).count(), expected.len()); |
| 1215 |
} |
| 1216 |
|
| 1217 |
#[test] |
| 1218 |
fn test_bit_set_difference() { |
| 1219 |
let mut a = BitSet::new(); |
| 1220 |
let mut b = BitSet::new(); |
| 1221 |
|
| 1222 |
assert!(a.insert(1)); |
| 1223 |
assert!(a.insert(3)); |
| 1224 |
assert!(a.insert(5)); |
| 1225 |
assert!(a.insert(200)); |
| 1226 |
assert!(a.insert(500)); |
| 1227 |
|
| 1228 |
assert!(b.insert(3)); |
| 1229 |
assert!(b.insert(200)); |
| 1230 |
|
| 1231 |
let expected = [1, 5, 500]; |
| 1232 |
let actual: Vec<_> = a.difference(&b).collect(); |
| 1233 |
assert_eq!(actual, expected); |
| 1234 |
assert_eq!(a.difference(&b).count(), expected.len()); |
| 1235 |
} |
| 1236 |
|
| 1237 |
#[test] |
| 1238 |
fn test_bit_set_symmetric_difference() { |
| 1239 |
let mut a = BitSet::new(); |
| 1240 |
let mut b = BitSet::new(); |
| 1241 |
|
| 1242 |
assert!(a.insert(1)); |
| 1243 |
assert!(a.insert(3)); |
| 1244 |
assert!(a.insert(5)); |
| 1245 |
assert!(a.insert(9)); |
| 1246 |
assert!(a.insert(11)); |
| 1247 |
|
| 1248 |
assert!(b.insert(3)); |
| 1249 |
assert!(b.insert(9)); |
| 1250 |
assert!(b.insert(14)); |
| 1251 |
assert!(b.insert(220)); |
| 1252 |
|
| 1253 |
let expected = [1, 5, 11, 14, 220]; |
| 1254 |
let actual: Vec<_> = a.symmetric_difference(&b).collect(); |
| 1255 |
assert_eq!(actual, expected); |
| 1256 |
assert_eq!(a.symmetric_difference(&b).count(), expected.len()); |
| 1257 |
} |
| 1258 |
|
| 1259 |
#[test] |
| 1260 |
fn test_bit_set_union() { |
| 1261 |
let mut a = BitSet::new(); |
| 1262 |
let mut b = BitSet::new(); |
| 1263 |
assert!(a.insert(1)); |
| 1264 |
assert!(a.insert(3)); |
| 1265 |
assert!(a.insert(5)); |
| 1266 |
assert!(a.insert(9)); |
| 1267 |
assert!(a.insert(11)); |
| 1268 |
assert!(a.insert(160)); |
| 1269 |
assert!(a.insert(19)); |
| 1270 |
assert!(a.insert(24)); |
| 1271 |
assert!(a.insert(200)); |
| 1272 |
|
| 1273 |
assert!(b.insert(1)); |
| 1274 |
assert!(b.insert(5)); |
| 1275 |
assert!(b.insert(9)); |
| 1276 |
assert!(b.insert(13)); |
| 1277 |
assert!(b.insert(19)); |
| 1278 |
|
| 1279 |
let expected = [1, 3, 5, 9, 11, 13, 19, 24, 160, 200]; |
| 1280 |
let actual: Vec<_> = a.union(&b).collect(); |
| 1281 |
assert_eq!(actual, expected); |
| 1282 |
assert_eq!(a.union(&b).count(), expected.len()); |
| 1283 |
} |
| 1284 |
|
| 1285 |
#[test] |
| 1286 |
fn test_bit_set_subset() { |
| 1287 |
let mut set1 = BitSet::new(); |
| 1288 |
let mut set2 = BitSet::new(); |
| 1289 |
|
| 1290 |
assert!(set1.is_subset(&set2)); // {} {} |
| 1291 |
set2.insert(100); |
| 1292 |
assert!(set1.is_subset(&set2)); // {} { 1 } |
| 1293 |
set2.insert(200); |
| 1294 |
assert!(set1.is_subset(&set2)); // {} { 1, 2 } |
| 1295 |
set1.insert(200); |
| 1296 |
assert!(set1.is_subset(&set2)); // { 2 } { 1, 2 } |
| 1297 |
set1.insert(300); |
| 1298 |
assert!(!set1.is_subset(&set2)); // { 2, 3 } { 1, 2 } |
| 1299 |
set2.insert(300); |
| 1300 |
assert!(set1.is_subset(&set2)); // { 2, 3 } { 1, 2, 3 } |
| 1301 |
set2.insert(400); |
| 1302 |
assert!(set1.is_subset(&set2)); // { 2, 3 } { 1, 2, 3, 4 } |
| 1303 |
set2.remove(100); |
| 1304 |
assert!(set1.is_subset(&set2)); // { 2, 3 } { 2, 3, 4 } |
| 1305 |
set2.remove(300); |
| 1306 |
assert!(!set1.is_subset(&set2)); // { 2, 3 } { 2, 4 } |
| 1307 |
set1.remove(300); |
| 1308 |
assert!(set1.is_subset(&set2)); // { 2 } { 2, 4 } |
| 1309 |
} |
| 1310 |
|
| 1311 |
#[test] |
| 1312 |
fn test_bit_set_is_disjoint() { |
| 1313 |
let a = BitSet::from_bytes(&[0b10100010]); |
| 1314 |
let b = BitSet::from_bytes(&[0b01000000]); |
| 1315 |
let c = BitSet::new(); |
| 1316 |
let d = BitSet::from_bytes(&[0b00110000]); |
| 1317 |
|
| 1318 |
assert!(!a.is_disjoint(&d)); |
| 1319 |
assert!(!d.is_disjoint(&a)); |
| 1320 |
|
| 1321 |
assert!(a.is_disjoint(&b)); |
| 1322 |
assert!(a.is_disjoint(&c)); |
| 1323 |
assert!(b.is_disjoint(&a)); |
| 1324 |
assert!(b.is_disjoint(&c)); |
| 1325 |
assert!(c.is_disjoint(&a)); |
| 1326 |
assert!(c.is_disjoint(&b)); |
| 1327 |
} |
| 1328 |
|
| 1329 |
#[test] |
| 1330 |
fn test_bit_set_union_with() { |
| 1331 |
//a should grow to include larger elements |
| 1332 |
let mut a = BitSet::new(); |
| 1333 |
a.insert(0); |
| 1334 |
let mut b = BitSet::new(); |
| 1335 |
b.insert(5); |
| 1336 |
let expected = BitSet::from_bytes(&[0b10000100]); |
| 1337 |
a.union_with(&b); |
| 1338 |
assert_eq!(a, expected); |
| 1339 |
|
| 1340 |
// Standard |
| 1341 |
let mut a = BitSet::from_bytes(&[0b10100010]); |
| 1342 |
let mut b = BitSet::from_bytes(&[0b01100010]); |
| 1343 |
let c = a.clone(); |
| 1344 |
a.union_with(&b); |
| 1345 |
b.union_with(&c); |
| 1346 |
assert_eq!(a.len(), 4); |
| 1347 |
assert_eq!(b.len(), 4); |
| 1348 |
} |
| 1349 |
|
| 1350 |
#[test] |
| 1351 |
fn test_bit_set_intersect_with() { |
| 1352 |
// Explicitly 0'ed bits |
| 1353 |
let mut a = BitSet::from_bytes(&[0b10100010]); |
| 1354 |
let mut b = BitSet::from_bytes(&[0b00000000]); |
| 1355 |
let c = a.clone(); |
| 1356 |
a.intersect_with(&b); |
| 1357 |
b.intersect_with(&c); |
| 1358 |
assert!(a.is_empty()); |
| 1359 |
assert!(b.is_empty()); |
| 1360 |
|
| 1361 |
// Uninitialized bits should behave like 0's |
| 1362 |
let mut a = BitSet::from_bytes(&[0b10100010]); |
| 1363 |
let mut b = BitSet::new(); |
| 1364 |
let c = a.clone(); |
| 1365 |
a.intersect_with(&b); |
| 1366 |
b.intersect_with(&c); |
| 1367 |
assert!(a.is_empty()); |
| 1368 |
assert!(b.is_empty()); |
| 1369 |
|
| 1370 |
// Standard |
| 1371 |
let mut a = BitSet::from_bytes(&[0b10100010]); |
| 1372 |
let mut b = BitSet::from_bytes(&[0b01100010]); |
| 1373 |
let c = a.clone(); |
| 1374 |
a.intersect_with(&b); |
| 1375 |
b.intersect_with(&c); |
| 1376 |
assert_eq!(a.len(), 2); |
| 1377 |
assert_eq!(b.len(), 2); |
| 1378 |
} |
| 1379 |
|
| 1380 |
#[test] |
| 1381 |
fn test_bit_set_difference_with() { |
| 1382 |
// Explicitly 0'ed bits |
| 1383 |
let mut a = BitSet::from_bytes(&[0b00000000]); |
| 1384 |
let b = BitSet::from_bytes(&[0b10100010]); |
| 1385 |
a.difference_with(&b); |
| 1386 |
assert!(a.is_empty()); |
| 1387 |
|
| 1388 |
// Uninitialized bits should behave like 0's |
| 1389 |
let mut a = BitSet::new(); |
| 1390 |
let b = BitSet::from_bytes(&[0b11111111]); |
| 1391 |
a.difference_with(&b); |
| 1392 |
assert!(a.is_empty()); |
| 1393 |
|
| 1394 |
// Standard |
| 1395 |
let mut a = BitSet::from_bytes(&[0b10100010]); |
| 1396 |
let mut b = BitSet::from_bytes(&[0b01100010]); |
| 1397 |
let c = a.clone(); |
| 1398 |
a.difference_with(&b); |
| 1399 |
b.difference_with(&c); |
| 1400 |
assert_eq!(a.len(), 1); |
| 1401 |
assert_eq!(b.len(), 1); |
| 1402 |
} |
| 1403 |
|
| 1404 |
#[test] |
| 1405 |
fn test_bit_set_symmetric_difference_with() { |
| 1406 |
//a should grow to include larger elements |
| 1407 |
let mut a = BitSet::new(); |
| 1408 |
a.insert(0); |
| 1409 |
a.insert(1); |
| 1410 |
let mut b = BitSet::new(); |
| 1411 |
b.insert(1); |
| 1412 |
b.insert(5); |
| 1413 |
let expected = BitSet::from_bytes(&[0b10000100]); |
| 1414 |
a.symmetric_difference_with(&b); |
| 1415 |
assert_eq!(a, expected); |
| 1416 |
|
| 1417 |
let mut a = BitSet::from_bytes(&[0b10100010]); |
| 1418 |
let b = BitSet::new(); |
| 1419 |
let c = a.clone(); |
| 1420 |
a.symmetric_difference_with(&b); |
| 1421 |
assert_eq!(a, c); |
| 1422 |
|
| 1423 |
// Standard |
| 1424 |
let mut a = BitSet::from_bytes(&[0b11100010]); |
| 1425 |
let mut b = BitSet::from_bytes(&[0b01101010]); |
| 1426 |
let c = a.clone(); |
| 1427 |
a.symmetric_difference_with(&b); |
| 1428 |
b.symmetric_difference_with(&c); |
| 1429 |
assert_eq!(a.len(), 2); |
| 1430 |
assert_eq!(b.len(), 2); |
| 1431 |
} |
| 1432 |
|
| 1433 |
#[test] |
| 1434 |
fn test_bit_set_eq() { |
| 1435 |
let a = BitSet::from_bytes(&[0b10100010]); |
| 1436 |
let b = BitSet::from_bytes(&[0b00000000]); |
| 1437 |
let c = BitSet::new(); |
| 1438 |
|
| 1439 |
assert!(a == a); |
| 1440 |
assert!(a != b); |
| 1441 |
assert!(a != c); |
| 1442 |
assert!(b == b); |
| 1443 |
assert!(b == c); |
| 1444 |
assert!(c == c); |
| 1445 |
} |
| 1446 |
|
| 1447 |
#[test] |
| 1448 |
fn test_bit_set_cmp() { |
| 1449 |
let a = BitSet::from_bytes(&[0b10100010]); |
| 1450 |
let b = BitSet::from_bytes(&[0b00000000]); |
| 1451 |
let c = BitSet::new(); |
| 1452 |
|
| 1453 |
assert_eq!(a.cmp(&b), Greater); |
| 1454 |
assert_eq!(a.cmp(&c), Greater); |
| 1455 |
assert_eq!(b.cmp(&a), Less); |
| 1456 |
assert_eq!(b.cmp(&c), Equal); |
| 1457 |
assert_eq!(c.cmp(&a), Less); |
| 1458 |
assert_eq!(c.cmp(&b), Equal); |
| 1459 |
} |
| 1460 |
|
| 1461 |
#[test] |
| 1462 |
fn test_bit_set_shrink_to_fit_new() { |
| 1463 |
// There was a strange bug where we refused to truncate to 0 |
| 1464 |
// and this would end up actually growing the array in a way |
| 1465 |
// that (safely corrupted the state). |
| 1466 |
let mut a = BitSet::new(); |
| 1467 |
assert_eq!(a.len(), 0); |
| 1468 |
assert_eq!(a.capacity(), 0); |
| 1469 |
a.shrink_to_fit(); |
| 1470 |
assert_eq!(a.len(), 0); |
| 1471 |
assert_eq!(a.capacity(), 0); |
| 1472 |
assert!(!a.contains(1)); |
| 1473 |
a.insert(3); |
| 1474 |
assert!(a.contains(3)); |
| 1475 |
assert_eq!(a.len(), 1); |
| 1476 |
assert!(a.capacity() > 0); |
| 1477 |
a.shrink_to_fit(); |
| 1478 |
assert!(a.contains(3)); |
| 1479 |
assert_eq!(a.len(), 1); |
| 1480 |
assert!(a.capacity() > 0); |
| 1481 |
} |
| 1482 |
|
| 1483 |
#[test] |
| 1484 |
fn test_bit_set_shrink_to_fit() { |
| 1485 |
let mut a = BitSet::new(); |
| 1486 |
assert_eq!(a.len(), 0); |
| 1487 |
assert_eq!(a.capacity(), 0); |
| 1488 |
a.insert(259); |
| 1489 |
a.insert(98); |
| 1490 |
a.insert(3); |
| 1491 |
assert_eq!(a.len(), 3); |
| 1492 |
assert!(a.capacity() > 0); |
| 1493 |
assert!(!a.contains(1)); |
| 1494 |
assert!(a.contains(259)); |
| 1495 |
assert!(a.contains(98)); |
| 1496 |
assert!(a.contains(3)); |
| 1497 |
|
| 1498 |
a.shrink_to_fit(); |
| 1499 |
assert!(!a.contains(1)); |
| 1500 |
assert!(a.contains(259)); |
| 1501 |
assert!(a.contains(98)); |
| 1502 |
assert!(a.contains(3)); |
| 1503 |
assert_eq!(a.len(), 3); |
| 1504 |
assert!(a.capacity() > 0); |
| 1505 |
|
| 1506 |
let old_cap = a.capacity(); |
| 1507 |
assert!(a.remove(259)); |
| 1508 |
a.shrink_to_fit(); |
| 1509 |
assert!(a.capacity() < old_cap, "{} {}", a.capacity(), old_cap); |
| 1510 |
assert!(!a.contains(1)); |
| 1511 |
assert!(!a.contains(259)); |
| 1512 |
assert!(a.contains(98)); |
| 1513 |
assert!(a.contains(3)); |
| 1514 |
assert_eq!(a.len(), 2); |
| 1515 |
|
| 1516 |
let old_cap2 = a.capacity(); |
| 1517 |
a.clear(); |
| 1518 |
assert_eq!(a.capacity(), old_cap2); |
| 1519 |
assert_eq!(a.len(), 0); |
| 1520 |
assert!(!a.contains(1)); |
| 1521 |
assert!(!a.contains(259)); |
| 1522 |
assert!(!a.contains(98)); |
| 1523 |
assert!(!a.contains(3)); |
| 1524 |
|
| 1525 |
a.insert(512); |
| 1526 |
assert!(a.capacity() > 0); |
| 1527 |
assert_eq!(a.len(), 1); |
| 1528 |
assert!(a.contains(512)); |
| 1529 |
assert!(!a.contains(1)); |
| 1530 |
assert!(!a.contains(259)); |
| 1531 |
assert!(!a.contains(98)); |
| 1532 |
assert!(!a.contains(3)); |
| 1533 |
|
| 1534 |
a.remove(512); |
| 1535 |
a.shrink_to_fit(); |
| 1536 |
assert_eq!(a.capacity(), 0); |
| 1537 |
assert_eq!(a.len(), 0); |
| 1538 |
assert!(!a.contains(512)); |
| 1539 |
assert!(!a.contains(1)); |
| 1540 |
assert!(!a.contains(259)); |
| 1541 |
assert!(!a.contains(98)); |
| 1542 |
assert!(!a.contains(3)); |
| 1543 |
assert!(!a.contains(0)); |
| 1544 |
} |
| 1545 |
|
| 1546 |
#[test] |
| 1547 |
fn test_bit_vec_remove() { |
| 1548 |
let mut a = BitSet::new(); |
| 1549 |
|
| 1550 |
assert!(a.insert(1)); |
| 1551 |
assert!(a.remove(1)); |
| 1552 |
|
| 1553 |
assert!(a.insert(100)); |
| 1554 |
assert!(a.remove(100)); |
| 1555 |
|
| 1556 |
assert!(a.insert(1000)); |
| 1557 |
assert!(a.remove(1000)); |
| 1558 |
a.shrink_to_fit(); |
| 1559 |
} |
| 1560 |
|
| 1561 |
#[test] |
| 1562 |
fn test_bit_vec_clone() { |
| 1563 |
let mut a = BitSet::new(); |
| 1564 |
|
| 1565 |
assert!(a.insert(1)); |
| 1566 |
assert!(a.insert(100)); |
| 1567 |
assert!(a.insert(1000)); |
| 1568 |
|
| 1569 |
let mut b = a.clone(); |
| 1570 |
|
| 1571 |
assert!(a == b); |
| 1572 |
|
| 1573 |
assert!(b.remove(1)); |
| 1574 |
assert!(a.contains(1)); |
| 1575 |
|
| 1576 |
assert!(a.remove(1000)); |
| 1577 |
assert!(b.contains(1000)); |
| 1578 |
} |
| 1579 |
|
| 1580 |
#[test] |
| 1581 |
fn test_truncate() { |
| 1582 |
let bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]; |
| 1583 |
|
| 1584 |
let mut s = BitSet::from_bytes(&bytes); |
| 1585 |
s.truncate(5 * 8); |
| 1586 |
|
| 1587 |
assert_eq!(s, BitSet::from_bytes(&bytes[..5])); |
| 1588 |
assert_eq!(s.len(), 5 * 8); |
| 1589 |
s.truncate(4 * 8); |
| 1590 |
assert_eq!(s, BitSet::from_bytes(&bytes[..4])); |
| 1591 |
assert_eq!(s.len(), 4 * 8); |
| 1592 |
// Truncating to a size > s.len() should be a noop |
| 1593 |
s.truncate(5 * 8); |
| 1594 |
assert_eq!(s, BitSet::from_bytes(&bytes[..4])); |
| 1595 |
assert_eq!(s.len(), 4 * 8); |
| 1596 |
s.truncate(8); |
| 1597 |
assert_eq!(s, BitSet::from_bytes(&bytes[..1])); |
| 1598 |
assert_eq!(s.len(), 8); |
| 1599 |
s.truncate(0); |
| 1600 |
assert_eq!(s, BitSet::from_bytes(&[])); |
| 1601 |
assert_eq!(s.len(), 0); |
| 1602 |
} |
| 1603 |
|
| 1604 |
#[cfg(feature = "serde")] |
| 1605 |
#[test] |
| 1606 |
fn test_serialization() { |
| 1607 |
let bset: BitSet = BitSet::new(); |
| 1608 |
let serialized = serde_json::to_string(&bset).unwrap(); |
| 1609 |
let unserialized: BitSet = serde_json::from_str(&serialized).unwrap(); |
| 1610 |
assert_eq!(bset, unserialized); |
| 1611 |
|
| 1612 |
let elems: Vec<usize> = vec![11, 42, 100, 101]; |
| 1613 |
let bset: BitSet = elems.iter().map(|n| *n).collect(); |
| 1614 |
let serialized = serde_json::to_string(&bset).unwrap(); |
| 1615 |
let unserialized = serde_json::from_str(&serialized).unwrap(); |
| 1616 |
assert_eq!(bset, unserialized); |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* |
| 1620 |
#[test] |
| 1621 |
fn test_bit_set_append() { |
| 1622 |
let mut a = BitSet::new(); |
| 1623 |
a.insert(2); |
| 1624 |
a.insert(6); |
| 1625 |
|
| 1626 |
let mut b = BitSet::new(); |
| 1627 |
b.insert(1); |
| 1628 |
b.insert(3); |
| 1629 |
b.insert(6); |
| 1630 |
|
| 1631 |
a.append(&mut b); |
| 1632 |
|
| 1633 |
assert_eq!(a.len(), 4); |
| 1634 |
assert_eq!(b.len(), 0); |
| 1635 |
assert!(b.capacity() >= 6); |
| 1636 |
|
| 1637 |
assert_eq!(a, BitSet::from_bytes(&[0b01110010])); |
| 1638 |
} |
| 1639 |
|
| 1640 |
#[test] |
| 1641 |
fn test_bit_set_split_off() { |
| 1642 |
// Split at 0 |
| 1643 |
let mut a = BitSet::from_bytes(&[0b10100000, 0b00010010, 0b10010010, |
| 1644 |
0b00110011, 0b01101011, 0b10101101]); |
| 1645 |
|
| 1646 |
let b = a.split_off(0); |
| 1647 |
|
| 1648 |
assert_eq!(a.len(), 0); |
| 1649 |
assert_eq!(b.len(), 21); |
| 1650 |
|
| 1651 |
assert_eq!(b, BitSet::from_bytes(&[0b10100000, 0b00010010, 0b10010010, |
| 1652 |
0b00110011, 0b01101011, 0b10101101]); |
| 1653 |
|
| 1654 |
// Split behind last element |
| 1655 |
let mut a = BitSet::from_bytes(&[0b10100000, 0b00010010, 0b10010010, |
| 1656 |
0b00110011, 0b01101011, 0b10101101]); |
| 1657 |
|
| 1658 |
let b = a.split_off(50); |
| 1659 |
|
| 1660 |
assert_eq!(a.len(), 21); |
| 1661 |
assert_eq!(b.len(), 0); |
| 1662 |
|
| 1663 |
assert_eq!(a, BitSet::from_bytes(&[0b10100000, 0b00010010, 0b10010010, |
| 1664 |
0b00110011, 0b01101011, 0b10101101])); |
| 1665 |
|
| 1666 |
// Split at arbitrary element |
| 1667 |
let mut a = BitSet::from_bytes(&[0b10100000, 0b00010010, 0b10010010, |
| 1668 |
0b00110011, 0b01101011, 0b10101101]); |
| 1669 |
|
| 1670 |
let b = a.split_off(34); |
| 1671 |
|
| 1672 |
assert_eq!(a.len(), 12); |
| 1673 |
assert_eq!(b.len(), 9); |
| 1674 |
|
| 1675 |
assert_eq!(a, BitSet::from_bytes(&[0b10100000, 0b00010010, 0b10010010, |
| 1676 |
0b00110011, 0b01000000])); |
| 1677 |
assert_eq!(b, BitSet::from_bytes(&[0, 0, 0, 0, |
| 1678 |
0b00101011, 0b10101101])); |
| 1679 |
} |
| 1680 |
*/ |
| 1681 |
} |