diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 58f081b25e3e5..79e0c2ffea877 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -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; @@ -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) @@ -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< 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)] @@ -134,12 +135,12 @@ fn big_mask(nbits: uint, elem: uint) -> uint { } impl BigBitv { - pub fn new(storage: Vec) -> BigBitv { + fn new(storage: Vec) -> BigBitv { BigBitv {storage: storage} } #[inline] - pub fn process(&mut self, + fn process(&mut self, b: &BigBitv, nbits: uint, op: |uint, uint| -> uint) @@ -163,37 +164,37 @@ 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; @@ -201,7 +202,7 @@ impl BigBitv { } #[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; @@ -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 { @@ -596,6 +597,20 @@ impl fmt::Show for Bitv { } } +impl hash::Hash 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 { @@ -834,6 +849,14 @@ impl fmt::Show for BitvSet { } } +impl hash::Hash 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 }