Skip to content

Commit bef0b4b

Browse files
committed
Auto merge of #24934 - jooert:bitset-append-split_off, r=Gankro
cc #19986
2 parents 9ecc989 + f95c812 commit bef0b4b

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

src/libcollections/bit.rs

+83
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,89 @@ impl BitSet {
17961796
self.other_op(other, |w1, w2| w1 ^ w2);
17971797
}
17981798

1799+
/// Moves all elements from `other` into `Self`, leaving `other` empty.
1800+
///
1801+
/// # Examples
1802+
///
1803+
/// ```
1804+
/// # #![feature(collections, bit_set_append_split_off)]
1805+
/// use std::collections::{BitVec, BitSet};
1806+
///
1807+
/// let mut a = BitSet::new();
1808+
/// a.insert(2);
1809+
/// a.insert(6);
1810+
///
1811+
/// let mut b = BitSet::new();
1812+
/// b.insert(1);
1813+
/// b.insert(3);
1814+
/// b.insert(6);
1815+
///
1816+
/// a.append(&mut b);
1817+
///
1818+
/// assert_eq!(a.len(), 4);
1819+
/// assert_eq!(b.len(), 0);
1820+
/// assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b01110010])));
1821+
/// ```
1822+
#[unstable(feature = "bit_set_append_split_off",
1823+
reason = "recently added as part of collections reform 2")]
1824+
pub fn append(&mut self, other: &mut Self) {
1825+
self.union_with(other);
1826+
other.clear();
1827+
}
1828+
1829+
/// Splits the `BitSet` into two at the given key including the key.
1830+
/// Retains the first part in-place while returning the second part.
1831+
///
1832+
/// # Examples
1833+
///
1834+
/// ```
1835+
/// # #![feature(collections, bit_set_append_split_off)]
1836+
/// use std::collections::{BitSet, BitVec};
1837+
/// let mut a = BitSet::new();
1838+
/// a.insert(2);
1839+
/// a.insert(6);
1840+
/// a.insert(1);
1841+
/// a.insert(3);
1842+
///
1843+
/// let b = a.split_off(3);
1844+
///
1845+
/// assert_eq!(a.len(), 2);
1846+
/// assert_eq!(b.len(), 2);
1847+
/// assert_eq!(a, BitSet::from_bit_vec(BitVec::from_bytes(&[0b01100000])));
1848+
/// assert_eq!(b, BitSet::from_bit_vec(BitVec::from_bytes(&[0b00010010])));
1849+
/// ```
1850+
#[unstable(feature = "bit_set_append_split_off",
1851+
reason = "recently added as part of collections reform 2")]
1852+
pub fn split_off(&mut self, at: usize) -> Self {
1853+
let mut other = BitSet::new();
1854+
1855+
if at == 0 {
1856+
swap(self, &mut other);
1857+
return other;
1858+
} else if at >= self.bit_vec.len() {
1859+
return other;
1860+
}
1861+
1862+
// Calculate block and bit at which to split
1863+
let w = at / u32::BITS;
1864+
let b = at % u32::BITS;
1865+
1866+
// Pad `other` with `w` zero blocks,
1867+
// append `self`'s blocks in the range from `w` to the end to `other`
1868+
other.bit_vec.storage.extend(repeat(0u32).take(w)
1869+
.chain(self.bit_vec.storage[w..].iter().cloned()));
1870+
other.bit_vec.nbits = self.bit_vec.nbits;
1871+
1872+
if b > 0 {
1873+
other.bit_vec.storage[w] &= !0 << b;
1874+
}
1875+
1876+
// Sets `bit_vec.len()` and fixes the last block as well
1877+
self.bit_vec.truncate(at);
1878+
1879+
other
1880+
}
1881+
17991882
/// Returns the number of set bits in this set.
18001883
#[inline]
18011884
#[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)