Skip to content

Commit 0c4285a

Browse files
committed
---
yaml --- r: 22591 b: refs/heads/master c: c858eb0 h: refs/heads/master i: 22589: 6dbc851 22587: d1b4098 22583: 0273c9e 22575: 88dc75a 22559: 8067642 22527: 8f84e2e v: v3
1 parent f532375 commit 0c4285a

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
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: ee855caa5df4a9224aaf7b4ab5f644d3ef071f1b
2+
refs/heads/master: c858eb06546ce2be77fd284f707f76cb820c6211
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be

trunk/src/libstd/bitv.rs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ export is_false;
1515
export to_vec;
1616
export to_str;
1717
export eq_vec;
18+
export methods;
1819

1920
// FIXME (#2341): With recursive object types, we could implement binary
2021
// methods like union, intersection, and difference. At that point, we could
2122
// write an optimizing version of this module that produces a different obj
2223
// for the case where nbits <= 32.
2324

2425
/// The bitvector type
25-
type bitv = @{storage: ~[mut uint], nbits: uint};
26+
type bitv = {storage: ~[mut uint], nbits: uint};
2627

27-
const uint_bits: uint = 32u + (1u << 32u >> 27u);
28+
#[cfg(target_arch="x86")]
29+
const uint_bits: uint = 32;
30+
#[cfg(target_arch="x86_64")]
31+
const uint_bits: uint = 64;
2832

2933
/**
3034
* Constructs a bitvector
@@ -37,7 +41,7 @@ const uint_bits: uint = 32u + (1u << 32u >> 27u);
3741
fn bitv(nbits: uint, init: bool) -> bitv {
3842
let elt = if init { !0u } else { 0u };
3943
let storage = vec::to_mut(vec::from_elem(nbits / uint_bits + 1u, elt));
40-
ret @{storage: storage, nbits: nbits};
44+
ret {storage: storage, nbits: nbits};
4145
}
4246

4347
fn process(v0: bitv, v1: bitv, op: fn(uint, uint) -> uint) -> bool {
@@ -55,23 +59,24 @@ fn process(v0: bitv, v1: bitv, op: fn(uint, uint) -> uint) -> bool {
5559
}
5660

5761

58-
fn lor(w0: uint, w1: uint) -> uint { ret w0 | w1; }
59-
62+
/**
63+
* Calculates the union of two bitvectors
64+
*
65+
* Sets `v0` to the union of `v0` and `v1`. Both bitvectors must be the
66+
* same length. Returns 'true' if `v0` was changed.
67+
*/
6068
fn union(v0: bitv, v1: bitv) -> bool {
61-
let sub = lor; ret process(v0, v1, sub);
69+
process(v0, v1, |a, b| a | b)
6270
}
6371

64-
fn land(w0: uint, w1: uint) -> uint { ret w0 & w1; }
65-
6672
/**
6773
* Calculates the intersection of two bitvectors
6874
*
6975
* Sets `v0` to the intersection of `v0` and `v1`. Both bitvectors must be the
7076
* same length. Returns 'true' if `v0` was changed.
7177
*/
7278
fn intersect(v0: bitv, v1: bitv) -> bool {
73-
let sub = land;
74-
ret process(v0, v1, sub);
79+
process(v0, v1, |a, b| a & b)
7580
}
7681

7782
fn right(_w0: uint, w1: uint) -> uint { ret w1; }
@@ -87,10 +92,7 @@ fn assign(v0: bitv, v1: bitv) -> bool {
8792

8893
/// Makes a copy of a bitvector
8994
fn clone(v: bitv) -> bitv {
90-
let storage = vec::to_mut(vec::from_elem(v.nbits / uint_bits + 1u, 0u));
91-
let len = vec::len(v.storage);
92-
for uint::range(0u, len) |i| { storage[i] = v.storage[i]; };
93-
ret @{storage: storage, nbits: v.nbits};
95+
copy v
9496
}
9597

9698
/// Retrieve the value at index `i`
@@ -174,18 +176,13 @@ fn is_false(v: bitv) -> bool {
174176
ret true;
175177
}
176178

177-
fn init_to_vec(v: bitv, i: uint) -> uint {
178-
ret if get(v, i) { 1u } else { 0u };
179-
}
180-
181179
/**
182180
* Converts the bitvector to a vector of uint with the same length.
183181
*
184182
* Each uint in the resulting vector has either value 0u or 1u.
185183
*/
186184
fn to_vec(v: bitv) -> ~[uint] {
187-
let sub = |x| init_to_vec(v, x);
188-
ret vec::from_fn::<uint>(v.nbits, sub);
185+
vec::from_fn::<uint>(v.nbits, |i| if get(v, i) { 1 } else { 0 })
189186
}
190187

191188
#[inline(always)]
@@ -238,6 +235,30 @@ fn eq_vec(v0: bitv, v1: ~[uint]) -> bool {
238235
ret true;
239236
}
240237

238+
impl methods for bitv {
239+
fn union(rhs: bitv) -> bool { union(self, rhs) }
240+
fn intersect(rhs: bitv) -> bool { intersect(self, rhs) }
241+
fn assign(rhs: bitv) -> bool { assign(self, rhs) }
242+
fn get(i: uint) -> bool { get(self, i) }
243+
fn [](i: uint) -> bool { self.get(i) }
244+
fn eq(rhs: bitv) -> bool { equal(self, rhs) }
245+
fn clear() { clear(self) }
246+
fn set_all() { set_all(self) }
247+
fn invert() { invert(self) }
248+
fn difference(rhs: bitv) -> bool { difference(self, rhs) }
249+
fn set(i: uint, x: bool) { set(self, i, x) }
250+
fn is_true() -> bool { is_true(self) }
251+
fn is_false() -> bool { is_false(self) }
252+
fn to_vec() -> ~[uint] { to_vec(self) }
253+
fn each(f: fn(bool) -> bool) { each(self, f) }
254+
fn each_storage(f: fn(&uint) -> bool) { each_storage(self, f) }
255+
fn eq_vec(v: ~[uint]) -> bool { eq_vec(self, v) }
256+
}
257+
258+
impl of to_str::to_str for bitv {
259+
fn to_str() -> ~str { to_str(self) }
260+
}
261+
241262
#[cfg(test)]
242263
mod tests {
243264
#[test]

0 commit comments

Comments
 (0)