|
12 | 12 |
|
13 | 13 |
|
14 | 14 | use std::cmp;
|
| 15 | +use std::fmt; |
15 | 16 | use std::iter::RandomAccessIterator;
|
16 | 17 | use std::iter::{Enumerate, Repeat, Map, Zip};
|
17 | 18 | use std::ops;
|
18 | 19 | use std::slice;
|
19 |
| -use std::string::String; |
20 | 20 | use std::uint;
|
21 | 21 |
|
22 | 22 | #[deriving(Clone)]
|
@@ -526,25 +526,6 @@ impl Bitv {
|
526 | 526 | Vec::from_fn(self.nbits, |i| self[i])
|
527 | 527 | }
|
528 | 528 |
|
529 |
| - /** |
530 |
| - * Converts `self` to a string. |
531 |
| - * |
532 |
| - * The resulting string has the same length as `self`, and each |
533 |
| - * character is either '0' or '1'. |
534 |
| - */ |
535 |
| - pub fn to_str(&self) -> String { |
536 |
| - let mut rs = String::new(); |
537 |
| - for i in self.iter() { |
538 |
| - if i { |
539 |
| - rs.push_char('1'); |
540 |
| - } else { |
541 |
| - rs.push_char('0'); |
542 |
| - } |
543 |
| - }; |
544 |
| - rs |
545 |
| - } |
546 |
| - |
547 |
| - |
548 | 529 | /**
|
549 | 530 | * Compare a bitvector to a vector of `bool`.
|
550 | 531 | *
|
@@ -604,6 +585,15 @@ impl ops::Index<uint,bool> for Bitv {
|
604 | 585 | }
|
605 | 586 | }
|
606 | 587 |
|
| 588 | +impl fmt::Show for Bitv { |
| 589 | + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 590 | + for bit in self.iter() { |
| 591 | + try!(write!(fmt, "{}", if bit { 1 } else { 0 })); |
| 592 | + } |
| 593 | + Ok(()) |
| 594 | + } |
| 595 | +} |
| 596 | + |
607 | 597 | #[inline]
|
608 | 598 | fn iterate_bits(base: uint, bits: uint, f: |uint| -> bool) -> bool {
|
609 | 599 | if bits == 0 {
|
@@ -827,6 +817,21 @@ impl cmp::Eq for BitvSet {
|
827 | 817 | fn ne(&self, other: &BitvSet) -> bool { !self.eq(other) }
|
828 | 818 | }
|
829 | 819 |
|
| 820 | +impl fmt::Show for BitvSet { |
| 821 | + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 822 | + try!(write!(fmt, r"\{")); |
| 823 | + let mut first = true; |
| 824 | + for n in self.iter() { |
| 825 | + if !first { |
| 826 | + try!(write!(fmt, ", ")); |
| 827 | + } |
| 828 | + try!(write!(fmt, "{}", n)); |
| 829 | + first = false; |
| 830 | + } |
| 831 | + write!(fmt, r"\}") |
| 832 | + } |
| 833 | +} |
| 834 | + |
830 | 835 | impl Container for BitvSet {
|
831 | 836 | #[inline]
|
832 | 837 | fn len(&self) -> uint { self.size }
|
@@ -1629,6 +1634,16 @@ mod tests {
|
1629 | 1634 | assert!(!v.none());
|
1630 | 1635 | }
|
1631 | 1636 |
|
| 1637 | + #[test] |
| 1638 | + fn test_bitv_set_show() { |
| 1639 | + let mut s = BitvSet::new(); |
| 1640 | + s.insert(1); |
| 1641 | + s.insert(10); |
| 1642 | + s.insert(50); |
| 1643 | + s.insert(2); |
| 1644 | + assert_eq!("{1, 2, 10, 50}".to_string(), s.to_str()); |
| 1645 | + } |
| 1646 | + |
1632 | 1647 | fn rng() -> rand::IsaacRng {
|
1633 | 1648 | let seed = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
|
1634 | 1649 | rand::SeedableRng::from_seed(seed)
|
|
0 commit comments