From b32a02cdba6a56d62650616299ad04d0f4a128b0 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 10 Jul 2013 01:57:07 -0400 Subject: [PATCH] Derive Clone for bitv stuff --- src/libextra/bitv.rs | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/libextra/bitv.rs b/src/libextra/bitv.rs index 8ec72f3da346..7e6b2490b7ee 100644 --- a/src/libextra/bitv.rs +++ b/src/libextra/bitv.rs @@ -17,6 +17,7 @@ use std::ops; use std::uint; use std::vec; +#[deriving(Clone)] struct SmallBitv { /// only the lowest nbits of this value are used. the rest is undefined. bits: uint @@ -107,6 +108,7 @@ impl SmallBitv { pub fn negate(&mut self) { self.bits = !self.bits; } } +#[deriving(Clone)] struct BigBitv { storage: ~[uint] } @@ -212,11 +214,13 @@ impl BigBitv { } } +#[deriving(Clone)] enum BitvVariant { Big(~BigBitv), Small(~SmallBitv) } enum Op {Union, Intersect, Assign, Difference} /// The bitvector type +#[deriving(Clone)] pub struct Bitv { /// Internal representation of the bit vector (small or large) rep: BitvVariant, @@ -504,24 +508,6 @@ impl Bitv { } -impl Clone for Bitv { - /// Makes a copy of a bitvector - #[inline] - fn clone(&self) -> Bitv { - match self.rep { - Small(ref b) => { - Bitv{nbits: self.nbits, rep: Small(~SmallBitv{bits: b.bits})} - } - Big(ref b) => { - let mut st = vec::from_elem(self.nbits / uint::bits + 1, 0u); - let len = st.len(); - for uint::range(0, len) |i| { st[i] = b.storage[i]; }; - Bitv{nbits: self.nbits, rep: Big(~BigBitv{storage: st})} - } - } - } -} - /** * Transform a byte-vector into a bitv. Each byte becomes 8 bits, * with the most significant bits of each byte coming first. Each @@ -604,6 +590,7 @@ impl<'self> Iterator for BitvIterator<'self> { /// It should also be noted that the amount of storage necessary for holding a /// set of objects is proportional to the maximum of the objects when viewed /// as a uint. +#[deriving(Clone)] pub struct BitvSet { priv size: uint, @@ -1454,6 +1441,25 @@ mod tests { assert_eq!(a.capacity(), uint::bits); } + #[test] + fn test_bitv_clone() { + let mut a = BitvSet::new(); + + assert!(a.insert(1)); + assert!(a.insert(100)); + assert!(a.insert(1000)); + + let mut b = a.clone(); + + assert_eq!(&a, &b); + + assert!(b.remove(&1)); + assert!(a.contains(&1)); + + assert!(a.remove(&1000)); + assert!(b.contains(&1000)); + } + fn rng() -> rand::IsaacRng { let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; rand::IsaacRng::new_seeded(seed)