Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 48 additions & 25 deletions src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use core::iter::{Enumerate, Repeat, Map, Zip};
use core::ops;
use core::slice;
use core::uint;
use std::hash;

use vec::Vec;

Expand All @@ -34,12 +35,12 @@ fn small_mask(nbits: uint) -> uint {
}

impl SmallBitv {
pub fn new(bits: uint) -> SmallBitv {
fn new(bits: uint) -> SmallBitv {
SmallBitv {bits: bits}
}

#[inline]
pub fn bits_op(&mut self,
fn bits_op(&mut self,
right_bits: uint,
nbits: uint,
f: |uint, uint| -> uint)
Expand All @@ -52,32 +53,32 @@ impl SmallBitv {
}

#[inline]
pub fn union(&mut self, s: &SmallBitv, nbits: uint) -> bool {
fn union(&mut self, s: &SmallBitv, nbits: uint) -> bool {
self.bits_op(s.bits, nbits, |u1, u2| u1 | u2)
}

#[inline]
pub fn intersect(&mut self, s: &SmallBitv, nbits: uint) -> bool {
fn intersect(&mut self, s: &SmallBitv, nbits: uint) -> bool {
self.bits_op(s.bits, nbits, |u1, u2| u1 & u2)
}

#[inline]
pub fn become(&mut self, s: &SmallBitv, nbits: uint) -> bool {
fn become(&mut self, s: &SmallBitv, nbits: uint) -> bool {
self.bits_op(s.bits, nbits, |_u1, u2| u2)
}

#[inline]
pub fn difference(&mut self, s: &SmallBitv, nbits: uint) -> bool {
fn difference(&mut self, s: &SmallBitv, nbits: uint) -> bool {
self.bits_op(s.bits, nbits, |u1, u2| u1 & !u2)
}

#[inline]
pub fn get(&self, i: uint) -> bool {
fn get(&self, i: uint) -> bool {
(self.bits & (1 << i)) != 0
}

#[inline]
pub fn set(&mut self, i: uint, x: bool) {
fn set(&mut self, i: uint, x: bool) {
if x {
self.bits |= 1<<i;
}
Expand All @@ -87,29 +88,29 @@ impl SmallBitv {
}

#[inline]
pub fn equals(&self, b: &SmallBitv, nbits: uint) -> bool {
fn equals(&self, b: &SmallBitv, nbits: uint) -> bool {
let mask = small_mask(nbits);
mask & self.bits == mask & b.bits
}

#[inline]
pub fn clear(&mut self) { self.bits = 0; }
fn clear(&mut self) { self.bits = 0; }

#[inline]
pub fn set_all(&mut self) { self.bits = !0; }
fn set_all(&mut self) { self.bits = !0; }

#[inline]
pub fn all(&self, nbits: uint) -> bool {
fn all(&self, nbits: uint) -> bool {
small_mask(nbits) & !self.bits == 0
}

#[inline]
pub fn none(&self, nbits: uint) -> bool {
fn none(&self, nbits: uint) -> bool {
small_mask(nbits) & self.bits == 0
}

#[inline]
pub fn negate(&mut self) { self.bits = !self.bits; }
fn negate(&mut self) { self.bits = !self.bits; }
}

#[deriving(Clone)]
Expand All @@ -134,12 +135,12 @@ fn big_mask(nbits: uint, elem: uint) -> uint {
}

impl BigBitv {
pub fn new(storage: Vec<uint>) -> BigBitv {
fn new(storage: Vec<uint>) -> BigBitv {
BigBitv {storage: storage}
}

#[inline]
pub fn process(&mut self,
fn process(&mut self,
b: &BigBitv,
nbits: uint,
op: |uint, uint| -> uint)
Expand All @@ -163,45 +164,45 @@ impl BigBitv {
}

#[inline]
pub fn each_storage(&mut self, op: |v: &mut uint| -> bool) -> bool {
fn each_storage(&mut self, op: |v: &mut uint| -> bool) -> bool {
self.storage.mut_iter().advance(|elt| op(elt))
}

#[inline]
pub fn negate(&mut self) {
fn negate(&mut self) {
self.each_storage(|w| { *w = !*w; true });
}

#[inline]
pub fn union(&mut self, b: &BigBitv, nbits: uint) -> bool {
fn union(&mut self, b: &BigBitv, nbits: uint) -> bool {
self.process(b, nbits, |w1, w2| w1 | w2)
}

#[inline]
pub fn intersect(&mut self, b: &BigBitv, nbits: uint) -> bool {
fn intersect(&mut self, b: &BigBitv, nbits: uint) -> bool {
self.process(b, nbits, |w1, w2| w1 & w2)
}

#[inline]
pub fn become(&mut self, b: &BigBitv, nbits: uint) -> bool {
fn become(&mut self, b: &BigBitv, nbits: uint) -> bool {
self.process(b, nbits, |_, w| w)
}

#[inline]
pub fn difference(&mut self, b: &BigBitv, nbits: uint) -> bool {
fn difference(&mut self, b: &BigBitv, nbits: uint) -> bool {
self.process(b, nbits, |w1, w2| w1 & !w2)
}

#[inline]
pub fn get(&self, i: uint) -> bool {
fn get(&self, i: uint) -> bool {
let w = i / uint::BITS;
let b = i % uint::BITS;
let x = 1 & self.storage.get(w) >> b;
x == 1
}

#[inline]
pub fn set(&mut self, i: uint, x: bool) {
fn set(&mut self, i: uint, x: bool) {
let w = i / uint::BITS;
let b = i % uint::BITS;
let flag = 1 << b;
Expand All @@ -210,7 +211,7 @@ impl BigBitv {
}

#[inline]
pub fn equals(&self, b: &BigBitv, nbits: uint) -> bool {
fn equals(&self, b: &BigBitv, nbits: uint) -> bool {
for (i, elt) in b.storage.iter().enumerate() {
let mask = big_mask(nbits, i);
if mask & *self.storage.get(i) != mask & *elt {
Expand Down Expand Up @@ -596,6 +597,20 @@ impl fmt::Show for Bitv {
}
}

impl<S: hash::Writer> hash::Hash<S> for Bitv {
fn hash(&self, state: &mut S) {
self.nbits.hash(state);
match self.rep {
Small(ref s) => (s.bits & small_mask(self.nbits)).hash(state),
Big(ref b) => {
for (i, ele) in b.storage.iter().enumerate() {
(ele & big_mask(self.nbits, i)).hash(state);
}
}
}
}
}

#[inline]
fn iterate_bits(base: uint, bits: uint, f: |uint| -> bool) -> bool {
if bits == 0 {
Expand Down Expand Up @@ -834,6 +849,14 @@ impl fmt::Show for BitvSet {
}
}

impl<S: hash::Writer> hash::Hash<S> for BitvSet {
fn hash(&self, state: &mut S) {
for pos in self.iter() {
pos.hash(state);
}
}
}

impl Container for BitvSet {
#[inline]
fn len(&self) -> uint { self.size }
Expand Down