@@ -15,8 +15,9 @@ use std::cmp;
15
15
use std:: iter:: RandomAccessIterator ;
16
16
use std:: iter:: { Rev , Enumerate , Repeat , Map , Zip } ;
17
17
use std:: ops;
18
- use std:: uint;
19
18
use std:: slice;
19
+ use std:: strbuf:: StrBuf ;
20
+ use std:: uint;
20
21
21
22
#[ deriving( Clone ) ]
22
23
struct SmallBitv {
@@ -111,7 +112,7 @@ impl SmallBitv {
111
112
112
113
#[ deriving( Clone ) ]
113
114
struct BigBitv {
114
- storage : ~ [ uint ]
115
+ storage : Vec < uint >
115
116
}
116
117
117
118
/**
@@ -131,7 +132,7 @@ fn big_mask(nbits: uint, elem: uint) -> uint {
131
132
}
132
133
133
134
impl BigBitv {
134
- pub fn new ( storage : ~ [ uint ] ) -> BigBitv {
135
+ pub fn new ( storage : Vec < uint > ) -> BigBitv {
135
136
BigBitv { storage : storage}
136
137
}
137
138
@@ -193,7 +194,7 @@ impl BigBitv {
193
194
pub fn get ( & self , i : uint ) -> bool {
194
195
let w = i / uint:: BITS ;
195
196
let b = i % uint:: BITS ;
196
- let x = 1 & self . storage [ w ] >> b;
197
+ let x = 1 & self . storage . get ( w ) >> b;
197
198
x == 1
198
199
}
199
200
@@ -202,15 +203,15 @@ impl BigBitv {
202
203
let w = i / uint:: BITS ;
203
204
let b = i % uint:: BITS ;
204
205
let flag = 1 << b;
205
- self . storage [ w ] = if x { self . storage [ w ] | flag }
206
- else { self . storage [ w ] & !flag } ;
206
+ * self . storage . get_mut ( w ) = if x { * self . storage . get ( w ) | flag }
207
+ else { * self . storage . get ( w ) & !flag } ;
207
208
}
208
209
209
210
#[ inline]
210
211
pub fn equals ( & self , b : & BigBitv , nbits : uint ) -> bool {
211
212
for ( i, elt) in b. storage . iter ( ) . enumerate ( ) {
212
213
let mask = big_mask ( nbits, i) ;
213
- if mask & self . storage [ i ] != mask & * elt {
214
+ if mask & * self . storage . get ( i ) != mask & * elt {
214
215
return false ;
215
216
}
216
217
}
@@ -278,13 +279,13 @@ impl Bitv {
278
279
let s =
279
280
if init {
280
281
if exact {
281
- slice :: from_elem ( nelems, !0 u)
282
+ Vec :: from_elem ( nelems, !0 u)
282
283
} else {
283
- let mut v = slice :: from_elem ( nelems-1 , !0 u) ;
284
+ let mut v = Vec :: from_elem ( nelems-1 , !0 u) ;
284
285
v. push ( ( 1 <<nbits % uint:: BITS ) -1 ) ;
285
286
v
286
287
}
287
- } else { slice :: from_elem ( nelems, 0 u) } ;
288
+ } else { Vec :: from_elem ( nelems, 0 u) } ;
288
289
Big ( BigBitv :: new ( s) )
289
290
} ;
290
291
Bitv { rep : rep, nbits : nbits}
@@ -451,8 +452,8 @@ impl Bitv {
451
452
*
452
453
* Each `uint` in the resulting vector has either value `0u` or `1u`.
453
454
*/
454
- pub fn to_vec ( & self ) -> ~ [ uint ] {
455
- slice :: from_fn ( self . nbits , |x| self . init_to_vec ( x) )
455
+ pub fn to_vec ( & self ) -> Vec < uint > {
456
+ Vec :: from_fn ( self . nbits , |x| self . init_to_vec ( x) )
456
457
}
457
458
458
459
/**
@@ -461,7 +462,7 @@ impl Bitv {
461
462
* size of the `Bitv` is not a multiple of 8 then trailing bits
462
463
* will be filled-in with false/0
463
464
*/
464
- pub fn to_bytes ( & self ) -> ~ [ u8 ] {
465
+ pub fn to_bytes ( & self ) -> Vec < u8 > {
465
466
fn bit ( bitv : & Bitv , byte : uint , bit : uint ) -> u8 {
466
467
let offset = byte * 8 + bit;
467
468
if offset >= bitv. nbits {
@@ -473,7 +474,7 @@ impl Bitv {
473
474
474
475
let len = self . nbits /8 +
475
476
if self . nbits % 8 == 0 { 0 } else { 1 } ;
476
- slice :: from_fn ( len, |i|
477
+ Vec :: from_fn ( len, |i|
477
478
bit ( self , i, 0 ) |
478
479
bit ( self , i, 1 ) |
479
480
bit ( self , i, 2 ) |
@@ -486,10 +487,10 @@ impl Bitv {
486
487
}
487
488
488
489
/**
489
- * Transform `self` into a `[ bool] ` by turning each bit into a `bool`.
490
+ * Transform `self` into a `Vec< bool> ` by turning each bit into a `bool`.
490
491
*/
491
- pub fn to_bools ( & self ) -> ~ [ bool ] {
492
- slice :: from_fn ( self . nbits , |i| self [ i] )
492
+ pub fn to_bools ( & self ) -> Vec < bool > {
493
+ Vec :: from_fn ( self . nbits , |i| self [ i] )
493
494
}
494
495
495
496
/**
@@ -499,15 +500,15 @@ impl Bitv {
499
500
* character is either '0' or '1'.
500
501
*/
501
502
pub fn to_str ( & self ) -> ~str {
502
- let mut rs = ~"" ;
503
+ let mut rs = StrBuf :: new ( ) ;
503
504
for i in self . iter ( ) {
504
505
if i {
505
506
rs. push_char ( '1' ) ;
506
507
} else {
507
508
rs. push_char ( '0' ) ;
508
509
}
509
510
} ;
510
- rs
511
+ rs. into_owned ( )
511
512
}
512
513
513
514
@@ -659,7 +660,7 @@ pub struct BitvSet {
659
660
impl BitvSet {
660
661
/// Creates a new bit vector set with initially no contents
661
662
pub fn new ( ) -> BitvSet {
662
- BitvSet { size : 0 , bitv : BigBitv :: new ( ~ [ 0 ] ) }
663
+ BitvSet { size : 0 , bitv : BigBitv :: new ( vec ! ( 0 ) ) }
663
664
}
664
665
665
666
/// Creates a new bit vector set from the given bit vector
@@ -673,7 +674,7 @@ impl BitvSet {
673
674
match rep {
674
675
Big ( b) => BitvSet { size : size, bitv : b } ,
675
676
Small ( SmallBitv { bits} ) =>
676
- BitvSet { size : size, bitv : BigBitv { storage : ~ [ bits] } } ,
677
+ BitvSet { size : size, bitv : BigBitv { storage : vec ! ( bits) } } ,
677
678
}
678
679
}
679
680
@@ -705,9 +706,9 @@ impl BitvSet {
705
706
self . bitv . storage . grow ( other. capacity ( ) / uint:: BITS , & 0 ) ;
706
707
}
707
708
for ( i, & w) in other. bitv . storage . iter ( ) . enumerate ( ) {
708
- let old = self . bitv . storage [ i ] ;
709
+ let old = * self . bitv . storage . get ( i ) ;
709
710
let new = f ( old, w) ;
710
- self . bitv . storage [ i ] = new;
711
+ * self . bitv . storage . get_mut ( i ) = new;
711
712
self . size += nbits ( new) - nbits ( old) ;
712
713
}
713
714
}
@@ -863,7 +864,7 @@ impl MutableSet<uint> for BitvSet {
863
864
864
865
// Attempt to truncate our storage
865
866
let mut i = self . bitv . storage . len ( ) ;
866
- while i > 1 && self . bitv . storage [ i - 1 ] == 0 {
867
+ while i > 1 && * self . bitv . storage . get ( i - 1 ) == 0 {
867
868
i -= 1 ;
868
869
}
869
870
self . bitv . storage . truncate ( i) ;
@@ -878,12 +879,12 @@ impl BitvSet {
878
879
/// w1, w2) where the bit location is the number of bits offset so far,
879
880
/// and w1/w2 are the words coming from the two vectors self, other.
880
881
fn commons < ' a > ( & ' a self , other : & ' a BitvSet )
881
- -> Map < ' static , ( ( uint , & ' a uint ) , & ' a ~ [ uint ] ) , ( uint , uint , uint ) ,
882
- Zip < Enumerate < slice:: Items < ' a , uint > > , Repeat < & ' a ~ [ uint ] > > > {
882
+ -> Map < ' static , ( ( uint , & ' a uint ) , & ' a Vec < uint > ) , ( uint , uint , uint ) ,
883
+ Zip < Enumerate < slice:: Items < ' a , uint > > , Repeat < & ' a Vec < uint > > > > {
883
884
let min = cmp:: min ( self . bitv . storage . len ( ) , other. bitv . storage . len ( ) ) ;
884
885
self . bitv . storage . slice ( 0 , min) . iter ( ) . enumerate ( )
885
886
. zip ( Repeat :: new ( & other. bitv . storage ) )
886
- . map ( |( ( i, & w) , o_store) | ( i * uint:: BITS , w, o_store[ i ] ) )
887
+ . map ( |( ( i, & w) , o_store) | ( i * uint:: BITS , w, * o_store. get ( i ) ) )
887
888
}
888
889
889
890
/// Visits each word in `self` or `other` that extends beyond the other. This
@@ -946,7 +947,6 @@ mod tests {
946
947
use bitv;
947
948
948
949
use std:: uint;
949
- use std:: slice;
950
950
use rand;
951
951
use rand:: Rng ;
952
952
@@ -964,8 +964,8 @@ mod tests {
964
964
#[test]
965
965
fn test_0_elements() {
966
966
let act = Bitv::new(0u, false);
967
- let exp = slice ::from_elem::<bool> (0u, false);
968
- assert!(act.eq_vec(exp));
967
+ let exp = Vec ::from_elem(0u, false);
968
+ assert!(act.eq_vec(exp.as_slice() ));
969
969
}
970
970
971
971
#[test]
@@ -1299,12 +1299,12 @@ mod tests {
1299
1299
fn test_to_bytes() {
1300
1300
let mut bv = Bitv::new(3, true);
1301
1301
bv.set(1, false);
1302
- assert_eq!(bv.to_bytes(), ~[ 0b10100000] );
1302
+ assert_eq!(bv.to_bytes(), vec!( 0b10100000) );
1303
1303
1304
1304
let mut bv = Bitv::new(9, false);
1305
1305
bv.set(2, true);
1306
1306
bv.set(8, true);
1307
- assert_eq!(bv.to_bytes(), ~[ 0b00100000, 0b10000000] );
1307
+ assert_eq!(bv.to_bytes(), vec!( 0b00100000, 0b10000000) );
1308
1308
}
1309
1309
1310
1310
#[test]
@@ -1315,7 +1315,7 @@ mod tests {
1315
1315
1316
1316
#[test]
1317
1317
fn test_to_bools() {
1318
- let bools = ~[ false, false, true, false, false, true, true, false] ;
1318
+ let bools = vec!( false, false, true, false, false, true, true, false) ;
1319
1319
assert_eq!(from_bytes([0b00100110]).to_bools(), bools);
1320
1320
}
1321
1321
@@ -1334,8 +1334,8 @@ mod tests {
1334
1334
let bools = [true, false, true, true];
1335
1335
let bitv = BitvSet::from_bitv(from_bools(bools));
1336
1336
1337
- let idxs: ~[ uint] = bitv.iter().collect();
1338
- assert_eq!(idxs, ~[ 0, 2, 3] );
1337
+ let idxs: Vec< uint> = bitv.iter().collect();
1338
+ assert_eq!(idxs, vec!( 0, 2, 3) );
1339
1339
}
1340
1340
1341
1341
#[test]
@@ -1579,7 +1579,7 @@ mod tests {
1579
1579
#[ bench]
1580
1580
fn bench_big_bitv_small ( b : & mut BenchHarness ) {
1581
1581
let mut r = rng ( ) ;
1582
- let mut bitv = BigBitv :: new ( ~ [ 0 ] ) ;
1582
+ let mut bitv = BigBitv :: new ( vec ! ( 0 ) ) ;
1583
1583
b. iter ( || {
1584
1584
bitv. set ( ( r. next_u32 ( ) as uint ) % uint:: BITS , true ) ;
1585
1585
& bitv
@@ -1589,7 +1589,7 @@ mod tests {
1589
1589
#[ bench]
1590
1590
fn bench_big_bitv_big ( b : & mut BenchHarness ) {
1591
1591
let mut r = rng ( ) ;
1592
- let mut storage = ~ [ ] ;
1592
+ let mut storage = vec ! ( ) ;
1593
1593
storage. grow ( BENCH_BITS / uint:: BITS , & 0 u) ;
1594
1594
let mut bitv = BigBitv :: new ( storage) ;
1595
1595
b. iter ( || {
0 commit comments