Skip to content

Commit b9747e8

Browse files
committed
---
yaml --- r: 152149 b: refs/heads/try2 c: 24e489f h: refs/heads/master i: 152147: 0d15665 v: v3
1 parent 21eec5f commit b9747e8

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 030b3a2499a73a6d3b4629ad676d2d9cd6a79df0
8+
refs/heads/try2: 24e489f1e1326208aecabf390879c2320f212b7c
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcollections/bitv.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313

1414
use std::cmp;
15+
use std::fmt;
1516
use std::iter::RandomAccessIterator;
1617
use std::iter::{Enumerate, Repeat, Map, Zip};
1718
use std::ops;
1819
use std::slice;
19-
use std::string::String;
2020
use std::uint;
2121

2222
#[deriving(Clone)]
@@ -526,25 +526,6 @@ impl Bitv {
526526
Vec::from_fn(self.nbits, |i| self[i])
527527
}
528528

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-
548529
/**
549530
* Compare a bitvector to a vector of `bool`.
550531
*
@@ -604,6 +585,15 @@ impl ops::Index<uint,bool> for Bitv {
604585
}
605586
}
606587

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+
607597
#[inline]
608598
fn iterate_bits(base: uint, bits: uint, f: |uint| -> bool) -> bool {
609599
if bits == 0 {
@@ -827,6 +817,21 @@ impl cmp::Eq for BitvSet {
827817
fn ne(&self, other: &BitvSet) -> bool { !self.eq(other) }
828818
}
829819

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+
830835
impl Container for BitvSet {
831836
#[inline]
832837
fn len(&self) -> uint { self.size }
@@ -1629,6 +1634,16 @@ mod tests {
16291634
assert!(!v.none());
16301635
}
16311636

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+
16321647
fn rng() -> rand::IsaacRng {
16331648
let seed = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
16341649
rand::SeedableRng::from_seed(seed)

0 commit comments

Comments
 (0)