Skip to content

Commit adc9b4c

Browse files
committed
---
yaml --- r: 67229 b: refs/heads/master c: 978e5d9 h: refs/heads/master i: 67227: 86728d7 v: v3
1 parent 34edc38 commit adc9b4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+7712
-1532
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: ff0c2ae8124661b1bdc233f77126e312671563e9
2+
refs/heads/master: 978e5d94bc09a18cdfaa699508848bf8a39b46a9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/src/libextra/bitv.rs

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

20-
#[deriving(Clone)]
2120
struct SmallBitv {
2221
/// only the lowest nbits of this value are used. the rest is undefined.
2322
bits: uint
@@ -108,7 +107,6 @@ impl SmallBitv {
108107
pub fn negate(&mut self) { self.bits = !self.bits; }
109108
}
110109

111-
#[deriving(Clone)]
112110
struct BigBitv {
113111
storage: ~[uint]
114112
}
@@ -214,13 +212,11 @@ impl BigBitv {
214212
}
215213
}
216214

217-
#[deriving(Clone)]
218-
enum BitvVariant { Big(BigBitv), Small(SmallBitv) }
215+
enum BitvVariant { Big(~BigBitv), Small(~SmallBitv) }
219216

220217
enum Op {Union, Intersect, Assign, Difference}
221218

222219
/// The bitvector type
223-
#[deriving(Clone)]
224220
pub struct Bitv {
225221
/// Internal representation of the bit vector (small or large)
226222
rep: BitvVariant,
@@ -241,20 +237,20 @@ impl Bitv {
241237
match self.rep {
242238
Small(ref mut s) => match other.rep {
243239
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)
248244
},
249245
Big(_) => die()
250246
},
251247
Big(ref mut s) => match other.rep {
252248
Small(_) => die(),
253249
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)
258254
}
259255
}
260256
}
@@ -265,14 +261,14 @@ impl Bitv {
265261
impl Bitv {
266262
pub fn new(nbits: uint, init: bool) -> Bitv {
267263
let rep = if nbits <= uint::bits {
268-
Small(SmallBitv::new(if init {!0} else {0}))
264+
Small(~SmallBitv::new(if init {!0} else {0}))
269265
}
270266
else {
271267
let nelems = nbits/uint::bits +
272268
if nbits % uint::bits == 0 {0} else {1};
273269
let elem = if init {!0u} else {0u};
274270
let s = vec::from_elem(nelems, elem);
275-
Big(BigBitv::new(s))
271+
Big(~BigBitv::new(s))
276272
};
277273
Bitv {rep: rep, nbits: nbits}
278274
}
@@ -341,11 +337,11 @@ impl Bitv {
341337
if self.nbits != v1.nbits { return false; }
342338
match self.rep {
343339
Small(ref b) => match v1.rep {
344-
Small(ref b1) => b.equals(b1, self.nbits),
340+
Small(ref b1) => b.equals(*b1, self.nbits),
345341
_ => false
346342
},
347343
Big(ref s) => match v1.rep {
348-
Big(ref s1) => s.equals(s1, self.nbits),
344+
Big(ref s1) => s.equals(*s1, self.nbits),
349345
Small(_) => return false
350346
}
351347
}
@@ -396,23 +392,28 @@ impl Bitv {
396392
match self.rep {
397393
Small(ref b) => b.is_true(self.nbits),
398394
_ => {
399-
for self.iter().advance |i| { if !i { return false; } }
395+
for self.each() |i| { if !i { return false; } }
400396
true
401397
}
402398
}
403399
}
404400

405401
#[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;
408409
}
409410

410411
/// Returns true if all bits are 0
411412
pub fn is_false(&self) -> bool {
412413
match self.rep {
413414
Small(ref b) => b.is_false(self.nbits),
414415
Big(_) => {
415-
for self.iter().advance |i| { if i { return false; } }
416+
for self.each() |i| { if i { return false; } }
416417
true
417418
}
418419
}
@@ -476,7 +477,7 @@ impl Bitv {
476477
*/
477478
pub fn to_str(&self) -> ~str {
478479
let mut rs = ~"";
479-
for self.iter().advance |i| {
480+
for self.each() |i| {
480481
if i {
481482
rs.push_char('1');
482483
} else {
@@ -508,6 +509,24 @@ impl Bitv {
508509

509510
}
510511

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, 0u);
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+
511530
/**
512531
* Transform a byte-vector into a bitv. Each byte becomes 8 bits,
513532
* 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 {
561580
return true;
562581
}
563582

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-
588583
/// An implementation of a set using a bit vector as an underlying
589584
/// representation for holding numerical elements.
590585
///
591586
/// It should also be noted that the amount of storage necessary for holding a
592587
/// set of objects is proportional to the maximum of the objects when viewed
593588
/// as a uint.
594-
#[deriving(Clone)]
595589
pub struct BitvSet {
596590
priv size: uint,
597591

@@ -615,8 +609,8 @@ impl BitvSet {
615609
}
616610
let Bitv{rep, _} = bitv;
617611
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}) =>
620614
BitvSet{ size: size, bitv: BigBitv{ storage: ~[bits] } },
621615
}
622616
}
@@ -629,7 +623,7 @@ impl BitvSet {
629623
pub fn unwrap(self) -> Bitv {
630624
let cap = self.capacity();
631625
let BitvSet{bitv, _} = self;
632-
return Bitv{ nbits:cap, rep: Big(bitv) };
626+
return Bitv{ nbits:cap, rep: Big(~bitv) };
633627
}
634628

635629
#[inline]
@@ -676,8 +670,13 @@ impl BitvSet {
676670
self.other_op(other, |w1, w2| w1 ^ w2);
677671
}
678672

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;
681680
}
682681
}
683682

@@ -861,31 +860,6 @@ impl BitvSet {
861860
}
862861
}
863862

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-
889863
#[cfg(test)]
890864
mod tests {
891865
use extra::test::BenchHarness;
@@ -1267,25 +1241,6 @@ mod tests {
12671241
assert_eq!(from_bytes([0b00100110]).to_bools(), bools);
12681242
}
12691243
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-
12891244
#[test]
12901245
fn test_small_difference() {
12911246
let mut b1 = Bitv::new(3, false);
@@ -1462,25 +1417,6 @@ mod tests {
14621417
assert_eq!(a.capacity(), uint::bits);
14631418
}
14641419

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-
14841420
fn rng() -> rand::IsaacRng {
14851421
let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
14861422
rand::IsaacRng::new_seeded(seed)
@@ -1568,38 +1504,4 @@ mod tests {
15681504
b1.union(&b2);
15691505
}
15701506
}
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-
}
16051507
}

0 commit comments

Comments
 (0)