Skip to content

Commit 9b69d47

Browse files
committed
Implement append and split_off for BitSet (RFC 509)
1 parent 05d5fca commit 9b69d47

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

src/libcollections/bit.rs

+78
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,84 @@ impl BitSet {
17921792
self.other_op(other, |w1, w2| w1 ^ w2);
17931793
}
17941794

1795+
/// Moves all elements from `other` into `Self`, leaving `other` empty.
1796+
///
1797+
/// # Examples
1798+
///
1799+
/// ```
1800+
/// # #![feature(collections)]
1801+
/// use std::collections::{BitVec, BitSet};
1802+
///
1803+
/// let mut a = BitSet::new();
1804+
/// a.insert(2);
1805+
/// a.insert(6);
1806+
///
1807+
/// let mut b = BitSet::new();
1808+
/// b.insert(1);
1809+
/// b.insert(3);
1810+
/// b.insert(6);
1811+
///
1812+
/// a.append(&mut b);
1813+
///
1814+
/// assert_eq!(a.len(), 4);
1815+
/// assert_eq!(b.len(), 0);
1816+
/// assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b01110010])));
1817+
/// ```
1818+
#[unstable(feature = "bit_set_append_split_off",
1819+
reason = "recently added as part of collections reform 2")]
1820+
pub fn append(&mut self, other: &mut Self) {
1821+
self.union_with(other);
1822+
other.clear();
1823+
}
1824+
1825+
/// Splits the `BitSet` into two at the given key including the key.
1826+
///
1827+
/// # Examples
1828+
///
1829+
/// ```
1830+
/// # #![feature(collections)]
1831+
/// use std::collections::{BitSet, BitVec};
1832+
/// let mut a = BitSet::new();
1833+
/// a.insert(2);
1834+
/// a.insert(6);
1835+
/// a.insert(1);
1836+
/// a.insert(3);
1837+
///
1838+
/// let b = a.split_off(3);
1839+
///
1840+
/// assert_eq!(a.len(), 2);
1841+
/// assert_eq!(b.len(), 2);
1842+
/// assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b01100000])));
1843+
/// assert_eq!(b, BitSet::from_bit_vec(BitVec::from_bytes(&[0b00010010])));
1844+
/// ```
1845+
#[unstable(feature = "bit_set_append_split_off",
1846+
reason = "recently added as part of collections reform 2")]
1847+
pub fn split_off(&mut self, at: usize) -> Self {
1848+
let mut other = BitSet::new();
1849+
1850+
if at == 0 {
1851+
swap(self, &mut other);
1852+
return other;
1853+
} else if at > self.bit_vec.len() {
1854+
return other;
1855+
}
1856+
1857+
let w = at / u32::BITS;
1858+
let b = at % u32::BITS;
1859+
let mut second = self.bit_vec.storage.split_off(w);
1860+
1861+
if b > 0 {
1862+
self.bit_vec.storage.push(second[0] & (!0u32 >> (u32::BITS - b)));
1863+
second[0] &= !0 << b;
1864+
}
1865+
1866+
other.bit_vec.storage.extend(repeat(0u32).take(w).chain(second.drain(..)));
1867+
other.bit_vec.nbits = self.bit_vec.nbits;
1868+
self.bit_vec.nbits = w * u32::BITS + b;
1869+
1870+
other
1871+
}
1872+
17951873
/// Returns the number of set bits in this set.
17961874
#[inline]
17971875
#[stable(feature = "rust1", since = "1.0.0")]

src/libcollectionstest/bit/set.rs

+61
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,67 @@ fn test_bit_vec_clone() {
387387
assert!(b.contains(&1000));
388388
}
389389

390+
#[test]
391+
fn test_bit_set_append() {
392+
let mut a = BitSet::new();
393+
a.insert(2);
394+
a.insert(6);
395+
396+
let mut b = BitSet::new();
397+
b.insert(1);
398+
b.insert(3);
399+
b.insert(6);
400+
401+
a.append(&mut b);
402+
403+
assert_eq!(a.len(), 4);
404+
assert_eq!(b.len(), 0);
405+
assert!(b.capacity() >= 6);
406+
407+
assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b01110010])));
408+
}
409+
410+
#[test]
411+
fn test_bit_set_split_off() {
412+
// Split at 0
413+
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100000, 0b00010010, 0b10010010,
414+
0b00110011, 0b01101011, 0b10101101]));
415+
416+
let b = a.split_off(0);
417+
418+
assert_eq!(a.len(), 0);
419+
assert_eq!(b.len(), 21);
420+
421+
assert_eq!(b, BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100000, 0b00010010, 0b10010010,
422+
0b00110011, 0b01101011, 0b10101101])));
423+
424+
// Split behind last element
425+
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100000, 0b00010010, 0b10010010,
426+
0b00110011, 0b01101011, 0b10101101]));
427+
428+
let b = a.split_off(50);
429+
430+
assert_eq!(a.len(), 21);
431+
assert_eq!(b.len(), 0);
432+
433+
assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100000, 0b00010010, 0b10010010,
434+
0b00110011, 0b01101011, 0b10101101])));
435+
436+
// Split at arbitrary element
437+
let mut a = BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100000, 0b00010010, 0b10010010,
438+
0b00110011, 0b01101011, 0b10101101]));
439+
440+
let b = a.split_off(34);
441+
442+
assert_eq!(a.len(), 12);
443+
assert_eq!(b.len(), 9);
444+
445+
assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b10100000, 0b00010010, 0b10010010,
446+
0b00110011, 0b01000000])));
447+
assert_eq!(b, BitSet::from_bit_vec(BitVec::from_bytes(&[0, 0, 0, 0,
448+
0b00101011, 0b10101101])));
449+
}
450+
390451
mod bench {
391452
use std::collections::{BitSet, BitVec};
392453
use std::__rand::{Rng, thread_rng, ThreadRng};

src/libcollectionstest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![feature(bit_set_append_split_off)]
1112
#![feature(bit_vec_append_split_off)]
1213
#![feature(box_syntax)]
1314
#![feature(collections)]

0 commit comments

Comments
 (0)