about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-06-08 10:36:54 -0700
committerbors <bors@rust-lang.org>2014-06-08 10:36:54 -0700
commit8dcbdaaeb79c69cc3ba6efb68650c89ca47969e4 (patch)
treeaa70e7ee34cc543423934376869b245841c25c72
parent17ba0cf4289d6da632aab4cf242ad74dea94fe37 (diff)
parent89c76bb1426539f2c6ea2b7c5892296efc96df9a (diff)
downloadrust-8dcbdaaeb79c69cc3ba6efb68650c89ca47969e4.tar.gz
rust-8dcbdaaeb79c69cc3ba6efb68650c89ca47969e4.zip
auto merge of #14749 : sfackler/rust/bitv-hash, r=alexcrichton
Closes #14744
-rw-r--r--src/libcollections/bitv.rs73
1 files changed, 48 insertions, 25 deletions
diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs
index 58f081b25e3..79e0c2ffea8 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<<i;
         }
@@ -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)]
@@ -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)
@@ -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<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 {
@@ -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 }