@@ -17,6 +17,7 @@ use std::ops;
17
17
use std:: uint;
18
18
use std:: vec;
19
19
20
+ #[ deriving( Clone ) ]
20
21
struct SmallBitv {
21
22
/// only the lowest nbits of this value are used. the rest is undefined.
22
23
bits : uint
@@ -107,6 +108,7 @@ impl SmallBitv {
107
108
pub fn negate ( & mut self ) { self . bits = !self . bits ; }
108
109
}
109
110
111
+ #[ deriving( Clone ) ]
110
112
struct BigBitv {
111
113
storage : ~[ uint ]
112
114
}
@@ -212,11 +214,13 @@ impl BigBitv {
212
214
}
213
215
}
214
216
217
+ #[ deriving( Clone ) ]
215
218
enum BitvVariant { Big ( ~BigBitv ) , Small ( ~SmallBitv ) }
216
219
217
220
enum Op { Union , Intersect , Assign , Difference }
218
221
219
222
/// The bitvector type
223
+ #[ deriving( Clone ) ]
220
224
pub struct Bitv {
221
225
/// Internal representation of the bit vector (small or large)
222
226
rep : BitvVariant ,
@@ -504,24 +508,6 @@ impl Bitv {
504
508
505
509
}
506
510
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 , 0 u) ;
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
-
525
511
/**
526
512
* Transform a byte-vector into a bitv. Each byte becomes 8 bits,
527
513
* with the most significant bits of each byte coming first. Each
@@ -604,6 +590,7 @@ impl<'self> Iterator<bool> for BitvIterator<'self> {
604
590
/// It should also be noted that the amount of storage necessary for holding a
605
591
/// set of objects is proportional to the maximum of the objects when viewed
606
592
/// as a uint.
593
+ #[ deriving( Clone ) ]
607
594
pub struct BitvSet {
608
595
priv size : uint ,
609
596
@@ -1454,6 +1441,25 @@ mod tests {
1454
1441
assert_eq ! ( a. capacity( ) , uint:: bits) ;
1455
1442
}
1456
1443
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
+
1457
1463
fn rng ( ) -> rand:: IsaacRng {
1458
1464
let seed = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] ;
1459
1465
rand:: IsaacRng :: new_seeded ( seed)
0 commit comments