about summary refs log tree commit diff
path: root/src/libcollections
diff options
context:
space:
mode:
Diffstat (limited to 'src/libcollections')
-rw-r--r--src/libcollections/bitv.rs64
1 files changed, 49 insertions, 15 deletions
diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs
index 6d7c91ccfee..f01b1cf9815 100644
--- a/src/libcollections/bitv.rs
+++ b/src/libcollections/bitv.rs
@@ -16,7 +16,6 @@ use core::cmp;
 use core::default::Default;
 use core::fmt;
 use core::iter::Take;
-use core::ops;
 use core::slice;
 use core::uint;
 use std::hash;
@@ -24,6 +23,29 @@ use std::hash;
 use {Collection, Mutable, Set, MutableSet};
 use vec::Vec;
 
+#[cfg(not(stage0))]
+use core::ops::Index;
+
+#[cfg(not(stage0))]
+static TRUE: bool = true;
+
+#[cfg(not(stage0))]
+static FALSE: bool = false;
+
+#[deriving(Clone)]
+struct SmallBitv {
+    /// only the lowest nbits of this value are used. the rest is undefined.
+    bits: uint
+}
+
+#[deriving(Clone)]
+struct BigBitv {
+    storage: Vec<uint>
+}
+
+#[deriving(Clone)]
+enum BitvVariant { Big(BigBitv), Small(SmallBitv) }
+
 /// The bitvector type
 ///
 /// # Example
@@ -58,6 +80,18 @@ pub struct Bitv {
     nbits: uint
 }
 
+#[cfg(not(stage0))]
+impl Index<uint,bool> for Bitv {
+    #[inline]
+    fn index<'a>(&'a self, i: &uint) -> &'a bool {
+        if self.get(*i) {
+            &TRUE
+        } else {
+            &FALSE
+        }
+    }
+}
+
 struct MaskWords<'a> {
     iter: slice::Items<'a, uint>,
     next_word: Option<&'a uint>,
@@ -268,7 +302,7 @@ impl Bitv {
             if offset >= bitv.nbits {
                 0
             } else {
-                bitv[offset] as u8 << (7 - bit)
+                bitv.get(offset) as u8 << (7 - bit)
             }
         }
 
@@ -287,6 +321,13 @@ impl Bitv {
     }
 
     /**
+     * Transform `self` into a `Vec<bool>` by turning each bit into a `bool`.
+     */
+    pub fn to_bools(&self) -> Vec<bool> {
+        Vec::from_fn(self.nbits, |i| self.get(i))
+    }
+
+    /**
      * Compare a bitvector to a vector of `bool`.
      *
      * Both the bitvector and vector must have the same length.
@@ -504,13 +545,6 @@ impl Clone for Bitv {
     }
 }
 
-impl ops::Index<uint,bool> for Bitv {
-    #[inline]
-    fn index(&self, i: &uint) -> bool {
-        self.get(*i)
-    }
-}
-
 impl fmt::Show for Bitv {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
         for bit in self.iter() {
@@ -1369,9 +1403,9 @@ mod tests {
         b2.set(1, true);
         b2.set(2, true);
         assert!(b1.difference(&b2));
-        assert!(b1[0]);
-        assert!(!b1[1]);
-        assert!(!b1[2]);
+        assert!(b1.get(0));
+        assert!(!b1.get(1));
+        assert!(!b1.get(2));
     }
 
     #[test]
@@ -1383,9 +1417,9 @@ mod tests {
         b2.set(40, true);
         b2.set(80, true);
         assert!(b1.difference(&b2));
-        assert!(b1[0]);
-        assert!(!b1[40]);
-        assert!(!b1[80]);
+        assert!(b1.get(0));
+        assert!(!b1.get(40));
+        assert!(!b1.get(80));
     }
 
     #[test]