]> git.lizzy.rs Git - bit-set.git/blobdiff - src/lib.rs
Merge pull request #9 from apasel422/update
[bit-set.git] / src / lib.rs
index cee18dbb3b55c093482dce785d66cd2596665cdb..c88f61ebde61639aad5f4f485e0c1f7529e7f2db 100644 (file)
@@ -57,8 +57,7 @@ use std::cmp::Ordering;
 use std::cmp;
 use std::fmt;
 use std::hash;
-use std::iter::{Chain, Enumerate, Repeat, Skip, Take, repeat};
-use std::iter::{self, FromIterator};
+use std::iter::{self, Chain, Enumerate, FromIterator, Repeat, Skip, Take};
 
 type MatchWords<'a, B> = Chain<Enumerate<Blocks<'a, B>>, Skip<Take<Enumerate<Repeat<B>>>>>;
 
@@ -81,8 +80,9 @@ fn blocks_for_bits<B: BitBlock>(bits: usize) -> usize {
 
 // Take two BitVec's, and return iterators of their words, where the shorter one
 // has been padded with 0's
-fn match_words<'a,'b, B: BitBlock>(a: &'a BitVec<B>, b: &'b BitVec<B>)
-               -> (MatchWords<'a, B>, MatchWords<'b, B>) {
+fn match_words<'a, 'b, B: BitBlock>(a: &'a BitVec<B>, b: &'b BitVec<B>)
+    -> (MatchWords<'a, B>, MatchWords<'b, B>)
+{
     let a_len = a.storage().len();
     let b_len = b.storage().len();
 
@@ -96,16 +96,20 @@ fn match_words<'a,'b, B: BitBlock>(a: &'a BitVec<B>, b: &'b BitVec<B>)
     }
 }
 
-pub struct BitSet<B=u32> {
+pub struct BitSet<B = u32> {
     bit_vec: BitVec<B>,
 }
 
 impl<B: BitBlock> Clone for BitSet<B> {
-       fn clone(&self) -> Self {
-               BitSet {
-                       bit_vec: self.bit_vec.clone(),
-               }
-       }
+    fn clone(&self) -> Self {
+        BitSet {
+            bit_vec: self.bit_vec.clone(),
+        }
+    }
+
+    fn clone_from(&mut self, other: &Self) {
+        self.bit_vec.clone_from(&other.bit_vec);
+    }
 }
 
 impl<B: BitBlock> Default for BitSet<B> {
@@ -114,8 +118,8 @@ impl<B: BitBlock> Default for BitSet<B> {
 }
 
 impl<B: BitBlock> FromIterator<usize> for BitSet<B> {
-    fn from_iter<I: IntoIterator<Item=usize>>(iter: I) -> Self {
-        let mut ret: Self = Default::default();
+    fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> Self {
+        let mut ret = Self::default();
         ret.extend(iter);
         ret
     }
@@ -123,7 +127,7 @@ impl<B: BitBlock> FromIterator<usize> for BitSet<B> {
 
 impl<B: BitBlock> Extend<usize> for BitSet<B> {
     #[inline]
-    fn extend<I: IntoIterator<Item=usize>>(&mut self, iter: I) {
+    fn extend<I: IntoIterator<Item = usize>>(&mut self, iter: I) {
         for i in iter {
             self.insert(i);
         }
@@ -133,37 +137,25 @@ impl<B: BitBlock> Extend<usize> for BitSet<B> {
 impl<B: BitBlock> PartialOrd for BitSet<B> {
     #[inline]
     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
-        Some(self.cmp(other))
+        self.iter().partial_cmp(other)
     }
 }
 
 impl<B: BitBlock> Ord for BitSet<B> {
     #[inline]
     fn cmp(&self, other: &Self) -> Ordering {
-        let mut a = self.iter();
-        let mut b = other.iter();
-        loop {
-            match (a.next(), b.next()) {
-                (Some(x), Some(y)) => match x.cmp(&y) {
-                    Ordering::Equal => {}
-                    otherwise => return otherwise,
-                },
-                (None, None) => return Ordering::Equal,
-                (None, _) => return Ordering::Less,
-                (_, None) => return Ordering::Greater,
-            }
-        }
+        self.iter().cmp(other)
     }
 }
 
-impl<B: BitBlock> cmp::PartialEq for BitSet<B> {
+impl<B: BitBlock> PartialEq for BitSet<B> {
     #[inline]
     fn eq(&self, other: &Self) -> bool {
-        self.cmp(other) == Ordering::Equal
+        self.iter().eq(other)
     }
 }
 
-impl<B: BitBlock> cmp::Eq for BitSet<B> {}
+impl<B: BitBlock> Eq for BitSet<B> {}
 
 impl BitSet<u32> {
     /// Creates a new empty `BitSet`.
@@ -177,7 +169,7 @@ impl BitSet<u32> {
     /// ```
     #[inline]
     pub fn new() -> Self {
-        Default::default()
+        Self::default()
     }
 
     /// Creates a new `BitSet` with initially no contents, able to
@@ -194,7 +186,7 @@ impl BitSet<u32> {
     #[inline]
     pub fn with_capacity(nbits: usize) -> Self {
         let bit_vec = BitVec::from_elem(nbits, false);
-        BitSet::from_bit_vec(bit_vec)
+        Self::from_bit_vec(bit_vec)
     }
 
     /// Creates a new `BitSet` from the given bit vector.
@@ -294,7 +286,6 @@ impl<B: BitBlock> BitSet<B> {
         }
     }
 
-
     /// Consumes this set to return the underlying bit vector.
     ///
     /// # Examples
@@ -358,7 +349,7 @@ impl<B: BitBlock> BitSet<B> {
             let old = self_bit_vec.storage()[i];
             let new = f(old, w);
             unsafe {
-               self_bit_vec.storage_mut()[i] = new;
+                self_bit_vec.storage_mut()[i] = new;
             }
         }
     }
@@ -391,8 +382,8 @@ impl<B: BitBlock> BitSet<B> {
         // Truncate
         let trunc_len = cmp::max(old_len - n, 1);
         unsafe {
-               bit_vec.storage_mut().truncate(trunc_len);
-               bit_vec.set_len(trunc_len * B::bits());
+            bit_vec.storage_mut().truncate(trunc_len);
+            bit_vec.set_len(trunc_len * B::bits());
         }
     }
 
@@ -797,16 +788,7 @@ impl<B: BitBlock> BitSet<B> {
 
 impl<B: BitBlock> fmt::Debug for BitSet<B> {
     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
-        try!(write!(fmt, "{{"));
-        let mut first = true;
-        for n in self {
-            if !first {
-                try!(write!(fmt, ", "));
-            }
-            try!(write!(fmt, "{:?}", n));
-            first = false;
-        }
-        write!(fmt, "}}")
+        fmt.debug_set().entries(self).finish()
     }
 }
 
@@ -954,14 +936,6 @@ impl<'a, B: BitBlock> IntoIterator for &'a BitSet<B> {
     }
 }
 
-
-
-
-
-
-
-
-
 #[cfg(test)]
 mod tests {
     use std::cmp::Ordering::{Equal, Greater, Less};