@@ -17,7 +17,6 @@ use std::ops;
17
17
use std:: uint;
18
18
use std:: vec;
19
19
20
- #[ deriving( Clone ) ]
21
20
struct SmallBitv {
22
21
/// only the lowest nbits of this value are used. the rest is undefined.
23
22
bits : uint
@@ -108,7 +107,6 @@ impl SmallBitv {
108
107
pub fn negate ( & mut self ) { self . bits = !self . bits ; }
109
108
}
110
109
111
- #[ deriving( Clone ) ]
112
110
struct BigBitv {
113
111
storage : ~[ uint ]
114
112
}
@@ -214,13 +212,11 @@ impl BigBitv {
214
212
}
215
213
}
216
214
217
- #[ deriving( Clone ) ]
218
- enum BitvVariant { Big ( BigBitv ) , Small ( SmallBitv ) }
215
+ enum BitvVariant { Big ( ~BigBitv ) , Small ( ~SmallBitv ) }
219
216
220
217
enum Op { Union , Intersect , Assign , Difference }
221
218
222
219
/// The bitvector type
223
- #[ deriving( Clone ) ]
224
220
pub struct Bitv {
225
221
/// Internal representation of the bit vector (small or large)
226
222
rep : BitvVariant ,
@@ -241,20 +237,20 @@ impl Bitv {
241
237
match self . rep {
242
238
Small ( ref mut s) => match other. rep {
243
239
Small ( ref s1) => match op {
244
- Union => s. union ( s1, self . nbits ) ,
245
- Intersect => s. intersect ( s1, self . nbits ) ,
246
- Assign => s. become ( s1, self . nbits ) ,
247
- Difference => s. difference ( s1, self . nbits )
240
+ Union => s. union ( * s1, self . nbits ) ,
241
+ Intersect => s. intersect ( * s1, self . nbits ) ,
242
+ Assign => s. become ( * s1, self . nbits ) ,
243
+ Difference => s. difference ( * s1, self . nbits )
248
244
} ,
249
245
Big ( _) => die ( )
250
246
} ,
251
247
Big ( ref mut s) => match other. rep {
252
248
Small ( _) => die ( ) ,
253
249
Big ( ref s1) => match op {
254
- Union => s. union ( s1, self . nbits ) ,
255
- Intersect => s. intersect ( s1, self . nbits ) ,
256
- Assign => s. become ( s1, self . nbits ) ,
257
- Difference => s. difference ( s1, self . nbits )
250
+ Union => s. union ( * s1, self . nbits ) ,
251
+ Intersect => s. intersect ( * s1, self . nbits ) ,
252
+ Assign => s. become ( * s1, self . nbits ) ,
253
+ Difference => s. difference ( * s1, self . nbits )
258
254
}
259
255
}
260
256
}
@@ -265,14 +261,14 @@ impl Bitv {
265
261
impl Bitv {
266
262
pub fn new ( nbits : uint , init : bool ) -> Bitv {
267
263
let rep = if nbits <= uint:: bits {
268
- Small ( SmallBitv :: new ( if init { !0 } else { 0 } ) )
264
+ Small ( ~ SmallBitv :: new ( if init { !0 } else { 0 } ) )
269
265
}
270
266
else {
271
267
let nelems = nbits/uint:: bits +
272
268
if nbits % uint:: bits == 0 { 0 } else { 1 } ;
273
269
let elem = if init { !0 u} else { 0 u} ;
274
270
let s = vec:: from_elem ( nelems, elem) ;
275
- Big ( BigBitv :: new ( s) )
271
+ Big ( ~ BigBitv :: new ( s) )
276
272
} ;
277
273
Bitv { rep : rep, nbits : nbits}
278
274
}
@@ -341,11 +337,11 @@ impl Bitv {
341
337
if self . nbits != v1. nbits { return false ; }
342
338
match self . rep {
343
339
Small ( ref b) => match v1. rep {
344
- Small ( ref b1) => b. equals ( b1, self . nbits ) ,
340
+ Small ( ref b1) => b. equals ( * b1, self . nbits ) ,
345
341
_ => false
346
342
} ,
347
343
Big ( ref s) => match v1. rep {
348
- Big ( ref s1) => s. equals ( s1, self . nbits ) ,
344
+ Big ( ref s1) => s. equals ( * s1, self . nbits ) ,
349
345
Small ( _) => return false
350
346
}
351
347
}
@@ -396,23 +392,28 @@ impl Bitv {
396
392
match self . rep {
397
393
Small ( ref b) => b. is_true ( self . nbits ) ,
398
394
_ => {
399
- for self . iter ( ) . advance |i| { if !i { return false; } }
395
+ for self . each ( ) |i| { if !i { return false ; } }
400
396
true
401
397
}
402
398
}
403
399
}
404
400
405
401
#[ inline]
406
- pub fn iter < ' a > ( & ' a self) -> BitvIterator < ' a > {
407
- BitvIterator { bitv : self , next_idx : 0 }
402
+ pub fn each ( & self , f : & fn ( bool ) -> bool ) -> bool {
403
+ let mut i = 0 ;
404
+ while i < self . nbits {
405
+ if !f ( self . get ( i) ) { return false ; }
406
+ i += 1 ;
407
+ }
408
+ return true ;
408
409
}
409
410
410
411
/// Returns true if all bits are 0
411
412
pub fn is_false ( & self ) -> bool {
412
413
match self . rep {
413
414
Small ( ref b) => b. is_false ( self . nbits ) ,
414
415
Big ( _) => {
415
- for self . iter ( ) . advance |i| { if i { return false; } }
416
+ for self . each ( ) |i| { if i { return false ; } }
416
417
true
417
418
}
418
419
}
@@ -476,7 +477,7 @@ impl Bitv {
476
477
*/
477
478
pub fn to_str ( & self ) -> ~str {
478
479
let mut rs = ~"";
479
- for self . iter ( ) . advance |i| {
480
+ for self . each ( ) |i| {
480
481
if i {
481
482
rs. push_char ( '1' ) ;
482
483
} else {
@@ -508,6 +509,24 @@ impl Bitv {
508
509
509
510
}
510
511
512
+ impl Clone for Bitv {
513
+ /// Makes a copy of a bitvector
514
+ #[ inline]
515
+ fn clone ( & self ) -> Bitv {
516
+ match self . rep {
517
+ Small ( ref b) => {
518
+ Bitv { nbits : self . nbits , rep : Small ( ~SmallBitv { bits : b. bits } ) }
519
+ }
520
+ Big ( ref b) => {
521
+ let mut st = vec:: from_elem ( self . nbits / uint:: bits + 1 , 0 u) ;
522
+ let len = st. len ( ) ;
523
+ for uint:: range( 0 , len) |i| { st[ i] = b. storage [ i] ; } ;
524
+ Bitv { nbits : self . nbits , rep : Big ( ~BigBitv { storage : st} ) }
525
+ }
526
+ }
527
+ }
528
+ }
529
+
511
530
/**
512
531
* Transform a byte-vector into a bitv. Each byte becomes 8 bits,
513
532
* with the most significant bits of each byte coming first. Each
@@ -561,37 +580,12 @@ fn iterate_bits(base: uint, bits: uint, f: &fn(uint) -> bool) -> bool {
561
580
return true ;
562
581
}
563
582
564
- /// An iterator for Bitv
565
- pub struct BitvIterator < ' self > {
566
- priv bitv : & ' self Bitv ,
567
- priv next_idx : uint
568
- }
569
-
570
- impl < ' self > Iterator < bool > for BitvIterator < ' self > {
571
- #[ inline]
572
- fn next ( & mut self ) -> Option < bool > {
573
- if self . next_idx < self . bitv . nbits {
574
- let idx = self . next_idx ;
575
- self . next_idx += 1 ;
576
- Some ( self . bitv . get ( idx) )
577
- } else {
578
- None
579
- }
580
- }
581
-
582
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
583
- let rem = self . bitv . nbits - self . next_idx ;
584
- ( rem, Some ( rem) )
585
- }
586
- }
587
-
588
583
/// An implementation of a set using a bit vector as an underlying
589
584
/// representation for holding numerical elements.
590
585
///
591
586
/// It should also be noted that the amount of storage necessary for holding a
592
587
/// set of objects is proportional to the maximum of the objects when viewed
593
588
/// as a uint.
594
- #[ deriving( Clone ) ]
595
589
pub struct BitvSet {
596
590
priv size : uint ,
597
591
@@ -615,8 +609,8 @@ impl BitvSet {
615
609
}
616
610
let Bitv { rep, _} = bitv;
617
611
match rep {
618
- Big ( b) => BitvSet { size : size, bitv : b } ,
619
- Small ( SmallBitv { bits} ) =>
612
+ Big ( ~ b) => BitvSet { size : size, bitv : b } ,
613
+ Small ( ~ SmallBitv { bits} ) =>
620
614
BitvSet { size : size, bitv : BigBitv { storage : ~[ bits] } } ,
621
615
}
622
616
}
@@ -629,7 +623,7 @@ impl BitvSet {
629
623
pub fn unwrap ( self ) -> Bitv {
630
624
let cap = self . capacity ( ) ;
631
625
let BitvSet { bitv, _} = self ;
632
- return Bitv { nbits : cap, rep : Big ( bitv) } ;
626
+ return Bitv { nbits : cap, rep : Big ( ~ bitv) } ;
633
627
}
634
628
635
629
#[ inline]
@@ -676,8 +670,13 @@ impl BitvSet {
676
670
self . other_op ( other, |w1, w2| w1 ^ w2) ;
677
671
}
678
672
679
- pub fn iter < ' a > ( & ' a self ) -> BitvSetIterator < ' a > {
680
- BitvSetIterator { set : self , next_idx : 0 }
673
+ pub fn each ( & self , blk : & fn ( v : & uint ) -> bool ) -> bool {
674
+ for self . bitv. storage. iter( ) . enumerate( ) . advance |( i, & w) | {
675
+ if !iterate_bits( i * uint:: bits, w, |b| blk( & b) ) {
676
+ return false ;
677
+ }
678
+ }
679
+ return true ;
681
680
}
682
681
}
683
682
@@ -861,31 +860,6 @@ impl BitvSet {
861
860
}
862
861
}
863
862
864
- pub struct BitvSetIterator < ' self > {
865
- priv set: & ' self BitvSet ,
866
- priv next_idx: uint
867
- }
868
-
869
- impl < ' self > Iterator < uint > for BitvSetIterator < ' self > {
870
- #[ inline]
871
- fn next( & mut self ) -> Option < uint > {
872
- while self . next_idx < self . set. capacity ( ) {
873
- let idx = self . next_idx ;
874
- self . next_idx += 1 ;
875
-
876
- if self . set . contains ( & idx) {
877
- return Some ( idx) ;
878
- }
879
- }
880
-
881
- return None ;
882
- }
883
-
884
- fn size_hint ( & self ) -> ( uint , Option < uint > ) {
885
- ( 0 , Some ( self . set . capacity ( ) - self . next_idx ) )
886
- }
887
- }
888
-
889
863
#[ cfg( test) ]
890
864
mod tests {
891
865
use extra:: test:: BenchHarness ;
@@ -1267,25 +1241,6 @@ mod tests {
1267
1241
assert_eq!(from_bytes([0b00100110]).to_bools(), bools);
1268
1242
}
1269
1243
1270
- #[test]
1271
- fn test_bitv_iterator() {
1272
- let bools = [true, false, true, true];
1273
- let bitv = from_bools(bools);
1274
-
1275
- for bitv.iter().zip(bools.iter()).advance |(act, &ex)| {
1276
- assert_eq!(ex, act);
1277
- }
1278
- }
1279
-
1280
- #[test]
1281
- fn test_bitv_set_iterator() {
1282
- let bools = [true, false, true, true];
1283
- let bitv = BitvSet::from_bitv(from_bools(bools));
1284
-
1285
- let idxs: ~[uint] = bitv.iter().collect();
1286
- assert_eq!(idxs, ~[0, 2, 3]);
1287
- }
1288
-
1289
1244
#[test]
1290
1245
fn test_small_difference() {
1291
1246
let mut b1 = Bitv::new(3, false);
@@ -1462,25 +1417,6 @@ mod tests {
1462
1417
assert_eq ! ( a. capacity( ) , uint:: bits) ;
1463
1418
}
1464
1419
1465
- #[ test]
1466
- fn test_bitv_clone ( ) {
1467
- let mut a = BitvSet :: new ( ) ;
1468
-
1469
- assert ! ( a. insert( 1 ) ) ;
1470
- assert ! ( a. insert( 100 ) ) ;
1471
- assert ! ( a. insert( 1000 ) ) ;
1472
-
1473
- let mut b = a. clone ( ) ;
1474
-
1475
- assert_eq ! ( & a, & b) ;
1476
-
1477
- assert ! ( b. remove( & 1 ) ) ;
1478
- assert ! ( a. contains( & 1 ) ) ;
1479
-
1480
- assert ! ( a. remove( & 1000 ) ) ;
1481
- assert ! ( b. contains( & 1000 ) ) ;
1482
- }
1483
-
1484
1420
fn rng( ) -> rand:: IsaacRng {
1485
1421
let seed = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] ;
1486
1422
rand:: IsaacRng :: new_seeded( seed)
@@ -1568,38 +1504,4 @@ mod tests {
1568
1504
b1. union ( & b2) ;
1569
1505
}
1570
1506
}
1571
-
1572
- #[ bench]
1573
- fn bench_btv_small_iter ( b : & mut BenchHarness ) {
1574
- let bitv = Bitv :: new ( uint:: bits, false ) ;
1575
- do b. iter {
1576
- let mut sum = 0 ;
1577
- for bitv . iter( ) . advance |pres| {
1578
- sum += pres as uint;
1579
- }
1580
- }
1581
- }
1582
-
1583
- #[ bench]
1584
- fn bench_bitv_big_iter ( b : & mut BenchHarness ) {
1585
- let bitv = Bitv :: new ( BENCH_BITS , false ) ;
1586
- do b. iter {
1587
- let mut sum = 0 ;
1588
- for bitv . iter( ) . advance |pres| {
1589
- sum += pres as uint;
1590
- }
1591
- }
1592
- }
1593
-
1594
- #[ bench]
1595
- fn bench_bitvset_iter ( b : & mut BenchHarness ) {
1596
- let bitv = BitvSet :: from_bitv ( from_fn ( BENCH_BITS ,
1597
- |idx| { idx % 3 == 0 } ) ) ;
1598
- do b. iter {
1599
- let mut sum = 0 ;
1600
- for bitv . iter( ) . advance |idx| {
1601
- sum += idx;
1602
- }
1603
- }
1604
- }
1605
1507
}
0 commit comments