A Vec of Bits

Allow making instances of `BitVec` with `B≠u32`

Previously it was not possible to construct such instances of `BitVec`
other than using the `Default` trait.

authored by

Columbus240 and committed by blackson.tngl.sh 7a249e3a 9413fed8

+98 -17
+98 -17
src/lib.rs
··· 150 150 + BitOr<Self, Output = Self> 151 151 + BitXor<Self, Output = Self> 152 152 + Rem<Self, Output = Self> 153 + + BitOrAssign<Self> 153 154 + Eq 154 155 + Ord 155 156 + hash::Hash ··· 297 298 (!B::zero()) >> ((B::bits() - bits % B::bits()) % B::bits()) 298 299 } 299 300 300 - type B = u32; 301 - 302 301 impl BitVec<u32> { 303 302 /// Creates an empty `BitVec`. 304 303 /// ··· 331 330 /// ``` 332 331 #[inline] 333 332 pub fn from_elem(len: usize, bit: bool) -> Self { 333 + BitVec::<u32>::from_elem_general(len, bit) 334 + } 335 + 336 + /// Constructs a new, empty `BitVec` with the specified capacity. 337 + /// 338 + /// The bitvector will be able to hold at least `capacity` bits without 339 + /// reallocating. If `capacity` is 0, it will not allocate. 340 + /// 341 + /// It is important to note that this function does not specify the 342 + /// *length* of the returned bitvector, but only the *capacity*. 343 + #[inline] 344 + pub fn with_capacity(capacity: usize) -> Self { 345 + BitVec::<u32>::with_capacity_general(capacity) 346 + } 347 + 348 + /// Transforms a byte-vector into a `BitVec`. Each byte becomes eight bits, 349 + /// with the most significant bits of each byte coming first. Each 350 + /// bit becomes `true` if equal to 1 or `false` if equal to 0. 351 + /// 352 + /// # Examples 353 + /// 354 + /// ``` 355 + /// use bit_vec::BitVec; 356 + /// 357 + /// let bv = BitVec::from_bytes(&[0b10100000, 0b00010010]); 358 + /// assert!(bv.eq_vec(&[true, false, true, false, 359 + /// false, false, false, false, 360 + /// false, false, false, true, 361 + /// false, false, true, false])); 362 + /// ``` 363 + pub fn from_bytes(bytes: &[u8]) -> Self { 364 + BitVec::<u32>::from_bytes_general(bytes) 365 + } 366 + 367 + /// Creates a `BitVec` of the specified length where the value at each index 368 + /// is `f(index)`. 369 + /// 370 + /// # Examples 371 + /// 372 + /// ``` 373 + /// use bit_vec::BitVec; 374 + /// 375 + /// let bv = BitVec::from_fn(5, |i| { i % 2 == 0 }); 376 + /// assert!(bv.eq_vec(&[true, false, true, false, true])); 377 + /// ``` 378 + #[inline] 379 + pub fn from_fn<F>(len: usize, f: F) -> Self 380 + where 381 + F: FnMut(usize) -> bool, 382 + { 383 + BitVec::<u32>::from_fn_general(len, f) 384 + } 385 + } 386 + 387 + impl<B: BitBlock> BitVec<B> { 388 + /// Creates an empty `BitVec`. 389 + /// 390 + /// # Examples 391 + /// 392 + /// ``` 393 + /// use bit_vec::BitVec; 394 + /// let mut bv = BitVec::<usize>::new_general(); 395 + /// ``` 396 + #[inline] 397 + pub fn new_general() -> Self { 398 + Default::default() 399 + } 400 + 401 + /// Creates a `BitVec` that holds `nbits` elements, setting each element 402 + /// to `bit`. 403 + /// 404 + /// # Examples 405 + /// 406 + /// ``` 407 + /// use bit_vec::BitVec; 408 + /// 409 + /// let mut bv = BitVec::<usize>::from_elem_general(10, false); 410 + /// assert_eq!(bv.len(), 10); 411 + /// for x in bv.iter() { 412 + /// assert_eq!(x, false); 413 + /// } 414 + /// ``` 415 + #[inline] 416 + pub fn from_elem_general(len: usize, bit: bool) -> Self { 334 417 let nblocks = blocks_for_bits::<B>(len); 335 418 let mut bit_vec = BitVec { 336 419 storage: vec![if bit { !B::zero() } else { B::zero() }; nblocks], ··· 348 431 /// It is important to note that this function does not specify the 349 432 /// *length* of the returned bitvector, but only the *capacity*. 350 433 #[inline] 351 - pub fn with_capacity(capacity: usize) -> Self { 434 + pub fn with_capacity_general(capacity: usize) -> Self { 352 435 BitVec { 353 436 storage: Vec::with_capacity(blocks_for_bits::<B>(capacity)), 354 437 nbits: 0, ··· 364 447 /// ``` 365 448 /// use bit_vec::BitVec; 366 449 /// 367 - /// let bv = BitVec::from_bytes(&[0b10100000, 0b00010010]); 450 + /// let bv = BitVec::<usize>::from_bytes_general(&[0b10100000, 0b00010010]); 368 451 /// assert!(bv.eq_vec(&[true, false, true, false, 369 452 /// false, false, false, false, 370 453 /// false, false, false, true, 371 454 /// false, false, true, false])); 372 455 /// ``` 373 - pub fn from_bytes(bytes: &[u8]) -> Self { 456 + pub fn from_bytes_general(bytes: &[u8]) -> Self { 374 457 let len = bytes 375 458 .len() 376 459 .checked_mul(u8::bits()) 377 460 .expect("capacity overflow"); 378 - let mut bit_vec = BitVec::with_capacity(len); 461 + let mut bit_vec = BitVec::with_capacity_general(len); 379 462 let complete_words = bytes.len() / B::bytes(); 380 463 let extra_bytes = bytes.len() % B::bytes(); 381 464 ··· 408 491 /// ``` 409 492 /// use bit_vec::BitVec; 410 493 /// 411 - /// let bv = BitVec::from_fn(5, |i| { i % 2 == 0 }); 494 + /// let bv = BitVec::<usize>::from_fn_general(5, |i| { i % 2 == 0 }); 412 495 /// assert!(bv.eq_vec(&[true, false, true, false, true])); 413 496 /// ``` 414 497 #[inline] 415 - pub fn from_fn<F>(len: usize, mut f: F) -> Self 498 + pub fn from_fn_general<F>(len: usize, mut f: F) -> Self 416 499 where 417 500 F: FnMut(usize) -> bool, 418 501 { 419 - let mut bit_vec = BitVec::from_elem(len, false); 502 + let mut bit_vec = BitVec::from_elem_general(len, false); 420 503 for i in 0..len { 421 504 bit_vec.set(i, f(i)); 422 505 } 423 506 bit_vec 424 507 } 425 - } 426 508 427 - impl<B: BitBlock> BitVec<B> { 428 509 /// Applies the given operation to the blocks of self and other, and sets 429 510 /// self to be the result. This relies on the caller not to corrupt the 430 511 /// last word. ··· 446 527 447 528 /// Iterator over mutable refs to the underlying blocks of data. 448 529 #[inline] 449 - fn blocks_mut(&mut self) -> MutBlocks<B> { 530 + fn blocks_mut(&mut self) -> MutBlocks<'_, B> { 450 531 // (2) 451 532 self.storage.iter_mut() 452 533 } 453 534 454 535 /// Iterator over the underlying blocks of data 455 536 #[inline] 456 - pub fn blocks(&self) -> Blocks<B> { 537 + pub fn blocks(&self) -> Blocks<'_, B> { 457 538 // (2) 458 539 Blocks { 459 540 iter: self.storage.iter(), ··· 614 695 /// assert_eq!(bv, BitVec::from_bytes(&[0b10100000])); 615 696 /// ``` 616 697 #[inline] 617 - pub fn get_mut(&mut self, index: usize) -> Option<MutBorrowedBit<B>> { 698 + pub fn get_mut(&mut self, index: usize) -> Option<MutBorrowedBit<'_, B>> { 618 699 self.get(index).map(move |value| MutBorrowedBit { 619 700 vec: Rc::new(RefCell::new(self)), 620 701 index, ··· 644 725 /// assert_eq!(bv, BitVec::from_bytes(&[0b10100000])); 645 726 /// ``` 646 727 #[inline] 647 - pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> MutBorrowedBit<B> { 728 + pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> MutBorrowedBit<'_, B> { 648 729 let value = self.get_unchecked(index); 649 730 MutBorrowedBit { 650 731 #[cfg(debug_assertions)] ··· 1119 1200 /// assert_eq!(bv.iter().filter(|x| *x).count(), 7); 1120 1201 /// ``` 1121 1202 #[inline] 1122 - pub fn iter(&self) -> Iter<B> { 1203 + pub fn iter(&self) -> Iter<'_, B> { 1123 1204 self.ensure_invariant(); 1124 1205 Iter { 1125 1206 bit_vec: self, ··· 1143 1224 /// ])); 1144 1225 /// ``` 1145 1226 #[inline] 1146 - pub fn iter_mut(&mut self) -> IterMut<B> { 1227 + pub fn iter_mut(&mut self) -> IterMut<'_, B> { 1147 1228 self.ensure_invariant(); 1148 1229 let nbits = self.nbits; 1149 1230 IterMut {