diff options
| author | Patrick Walton <pcwalton@mimiga.net> | 2014-07-03 14:32:41 -0700 |
|---|---|---|
| committer | Patrick Walton <pcwalton@mimiga.net> | 2014-07-07 11:43:23 -0700 |
| commit | 7e4e99123a68c92f684e5c4466101c1951e86895 (patch) | |
| tree | 270c38b9308597595fc05b233e25ef8252628439 /src/libcollections | |
| parent | 4f120e6bafe971452adfede158a7957b00562a4e (diff) | |
| download | rust-7e4e99123a68c92f684e5c4466101c1951e86895.tar.gz rust-7e4e99123a68c92f684e5c4466101c1951e86895.zip | |
librustc (RFC #34): Implement the new `Index` and `IndexMut` traits.
This will break code that used the old `Index` trait. Change this code
to use the new `Index` traits. For reference, here are their signatures:
pub trait Index<Index,Result> {
fn index<'a>(&'a self, index: &Index) -> &'a Result;
}
pub trait IndexMut<Index,Result> {
fn index_mut<'a>(&'a mut self, index: &Index) -> &'a mut Result;
}
Closes #6515.
[breaking-change]
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/bitv.rs | 64 |
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] |
