Implement Hash for Bitv and BitvSet

Closes #14744
This commit is contained in:
Steven Fackler 2014-06-08 09:30:03 -07:00
parent 17ba0cf428
commit 89c76bb142

View File

@ -18,6 +18,7 @@ use core::iter::{Enumerate, Repeat, Map, Zip};
use core::ops; use core::ops;
use core::slice; use core::slice;
use core::uint; use core::uint;
use std::hash;
use vec::Vec; use vec::Vec;
@ -34,12 +35,12 @@ fn small_mask(nbits: uint) -> uint {
} }
impl SmallBitv { impl SmallBitv {
pub fn new(bits: uint) -> SmallBitv { fn new(bits: uint) -> SmallBitv {
SmallBitv {bits: bits} SmallBitv {bits: bits}
} }
#[inline] #[inline]
pub fn bits_op(&mut self, fn bits_op(&mut self,
right_bits: uint, right_bits: uint,
nbits: uint, nbits: uint,
f: |uint, uint| -> uint) f: |uint, uint| -> uint)
@ -52,32 +53,32 @@ impl SmallBitv {
} }
#[inline] #[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) self.bits_op(s.bits, nbits, |u1, u2| u1 | u2)
} }
#[inline] #[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) self.bits_op(s.bits, nbits, |u1, u2| u1 & u2)
} }
#[inline] #[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) self.bits_op(s.bits, nbits, |_u1, u2| u2)
} }
#[inline] #[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) self.bits_op(s.bits, nbits, |u1, u2| u1 & !u2)
} }
#[inline] #[inline]
pub fn get(&self, i: uint) -> bool { fn get(&self, i: uint) -> bool {
(self.bits & (1 << i)) != 0 (self.bits & (1 << i)) != 0
} }
#[inline] #[inline]
pub fn set(&mut self, i: uint, x: bool) { fn set(&mut self, i: uint, x: bool) {
if x { if x {
self.bits |= 1<<i; self.bits |= 1<<i;
} }
@ -87,29 +88,29 @@ impl SmallBitv {
} }
#[inline] #[inline]
pub fn equals(&self, b: &SmallBitv, nbits: uint) -> bool { fn equals(&self, b: &SmallBitv, nbits: uint) -> bool {
let mask = small_mask(nbits); let mask = small_mask(nbits);
mask & self.bits == mask & b.bits mask & self.bits == mask & b.bits
} }
#[inline] #[inline]
pub fn clear(&mut self) { self.bits = 0; } fn clear(&mut self) { self.bits = 0; }
#[inline] #[inline]
pub fn set_all(&mut self) { self.bits = !0; } fn set_all(&mut self) { self.bits = !0; }
#[inline] #[inline]
pub fn all(&self, nbits: uint) -> bool { fn all(&self, nbits: uint) -> bool {
small_mask(nbits) & !self.bits == 0 small_mask(nbits) & !self.bits == 0
} }
#[inline] #[inline]
pub fn none(&self, nbits: uint) -> bool { fn none(&self, nbits: uint) -> bool {
small_mask(nbits) & self.bits == 0 small_mask(nbits) & self.bits == 0
} }
#[inline] #[inline]
pub fn negate(&mut self) { self.bits = !self.bits; } fn negate(&mut self) { self.bits = !self.bits; }
} }
#[deriving(Clone)] #[deriving(Clone)]
@ -134,12 +135,12 @@ fn big_mask(nbits: uint, elem: uint) -> uint {
} }
impl BigBitv { impl BigBitv {
pub fn new(storage: Vec<uint>) -> BigBitv { fn new(storage: Vec<uint>) -> BigBitv {
BigBitv {storage: storage} BigBitv {storage: storage}
} }
#[inline] #[inline]
pub fn process(&mut self, fn process(&mut self,
b: &BigBitv, b: &BigBitv,
nbits: uint, nbits: uint,
op: |uint, uint| -> uint) op: |uint, uint| -> uint)
@ -163,37 +164,37 @@ impl BigBitv {
} }
#[inline] #[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)) self.storage.mut_iter().advance(|elt| op(elt))
} }
#[inline] #[inline]
pub fn negate(&mut self) { fn negate(&mut self) {
self.each_storage(|w| { *w = !*w; true }); self.each_storage(|w| { *w = !*w; true });
} }
#[inline] #[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) self.process(b, nbits, |w1, w2| w1 | w2)
} }
#[inline] #[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) self.process(b, nbits, |w1, w2| w1 & w2)
} }
#[inline] #[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) self.process(b, nbits, |_, w| w)
} }
#[inline] #[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) self.process(b, nbits, |w1, w2| w1 & !w2)
} }
#[inline] #[inline]
pub fn get(&self, i: uint) -> bool { fn get(&self, i: uint) -> bool {
let w = i / uint::BITS; let w = i / uint::BITS;
let b = i % uint::BITS; let b = i % uint::BITS;
let x = 1 & self.storage.get(w) >> b; let x = 1 & self.storage.get(w) >> b;
@ -201,7 +202,7 @@ impl BigBitv {
} }
#[inline] #[inline]
pub fn set(&mut self, i: uint, x: bool) { fn set(&mut self, i: uint, x: bool) {
let w = i / uint::BITS; let w = i / uint::BITS;
let b = i % uint::BITS; let b = i % uint::BITS;
let flag = 1 << b; let flag = 1 << b;
@ -210,7 +211,7 @@ impl BigBitv {
} }
#[inline] #[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() { for (i, elt) in b.storage.iter().enumerate() {
let mask = big_mask(nbits, i); let mask = big_mask(nbits, i);
if mask & *self.storage.get(i) != mask & *elt { if mask & *self.storage.get(i) != mask & *elt {
@ -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] #[inline]
fn iterate_bits(base: uint, bits: uint, f: |uint| -> bool) -> bool { fn iterate_bits(base: uint, bits: uint, f: |uint| -> bool) -> bool {
if bits == 0 { if bits == 0 {
@ -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 { impl Container for BitvSet {
#[inline] #[inline]
fn len(&self) -> uint { self.size } fn len(&self) -> uint { self.size }