Skip to content

Commit b32a02c

Browse files
committed
Derive Clone for bitv stuff
1 parent 5f208b8 commit b32a02c

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/libextra/bitv.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::ops;
1717
use std::uint;
1818
use std::vec;
1919

20+
#[deriving(Clone)]
2021
struct SmallBitv {
2122
/// only the lowest nbits of this value are used. the rest is undefined.
2223
bits: uint
@@ -107,6 +108,7 @@ impl SmallBitv {
107108
pub fn negate(&mut self) { self.bits = !self.bits; }
108109
}
109110

111+
#[deriving(Clone)]
110112
struct BigBitv {
111113
storage: ~[uint]
112114
}
@@ -212,11 +214,13 @@ impl BigBitv {
212214
}
213215
}
214216

217+
#[deriving(Clone)]
215218
enum BitvVariant { Big(~BigBitv), Small(~SmallBitv) }
216219

217220
enum Op {Union, Intersect, Assign, Difference}
218221

219222
/// The bitvector type
223+
#[deriving(Clone)]
220224
pub struct Bitv {
221225
/// Internal representation of the bit vector (small or large)
222226
rep: BitvVariant,
@@ -504,24 +508,6 @@ impl Bitv {
504508

505509
}
506510

507-
impl Clone for Bitv {
508-
/// Makes a copy of a bitvector
509-
#[inline]
510-
fn clone(&self) -> Bitv {
511-
match self.rep {
512-
Small(ref b) => {
513-
Bitv{nbits: self.nbits, rep: Small(~SmallBitv{bits: b.bits})}
514-
}
515-
Big(ref b) => {
516-
let mut st = vec::from_elem(self.nbits / uint::bits + 1, 0u);
517-
let len = st.len();
518-
for uint::range(0, len) |i| { st[i] = b.storage[i]; };
519-
Bitv{nbits: self.nbits, rep: Big(~BigBitv{storage: st})}
520-
}
521-
}
522-
}
523-
}
524-
525511
/**
526512
* Transform a byte-vector into a bitv. Each byte becomes 8 bits,
527513
* with the most significant bits of each byte coming first. Each
@@ -604,6 +590,7 @@ impl<'self> Iterator<bool> for BitvIterator<'self> {
604590
/// It should also be noted that the amount of storage necessary for holding a
605591
/// set of objects is proportional to the maximum of the objects when viewed
606592
/// as a uint.
593+
#[deriving(Clone)]
607594
pub struct BitvSet {
608595
priv size: uint,
609596

@@ -1454,6 +1441,25 @@ mod tests {
14541441
assert_eq!(a.capacity(), uint::bits);
14551442
}
14561443

1444+
#[test]
1445+
fn test_bitv_clone() {
1446+
let mut a = BitvSet::new();
1447+
1448+
assert!(a.insert(1));
1449+
assert!(a.insert(100));
1450+
assert!(a.insert(1000));
1451+
1452+
let mut b = a.clone();
1453+
1454+
assert_eq!(&a, &b);
1455+
1456+
assert!(b.remove(&1));
1457+
assert!(a.contains(&1));
1458+
1459+
assert!(a.remove(&1000));
1460+
assert!(b.contains(&1000));
1461+
}
1462+
14571463
fn rng() -> rand::IsaacRng {
14581464
let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
14591465
rand::IsaacRng::new_seeded(seed)

0 commit comments

Comments
 (0)