Skip to content

Commit 8c3251f

Browse files
committed
Polish bitv docs.
1 parent 487d281 commit 8c3251f

File tree

2 files changed

+65
-56
lines changed

2 files changed

+65
-56
lines changed

src/libcollections/bitv.rs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
//!
2626
//! // Store the primes as a BitvSet
2727
//! let primes = {
28+
//! // Assume all numbers are prime to begin, and then we
29+
//! // cross off non-primes progressively
2830
//! let mut bv = Bitv::with_capacity(max_prime, true);
2931
//!
3032
//! // Neither 0 nor 1 are prime
@@ -33,8 +35,8 @@
3335
//!
3436
//! for i in range(2, max_prime) {
3537
//! // if i is a prime
36-
//! if bv.get(i) {
37-
//! // mark all multiples of i as non-prime (any multiples below i * i
38+
//! if bv[i] {
39+
//! // Mark all multiples of i as non-prime (any multiples below i * i
3840
//! // will have been marked as non-prime previously)
3941
//! for j in iter::range_step(i * i, max_prime, i) { bv.set(j, false) }
4042
//! }
@@ -252,6 +254,9 @@ impl Bitv {
252254
/// let bv: Bitv = [false, true].iter().map(|n| *n).collect();
253255
/// assert_eq!(bv.get(0), false);
254256
/// assert_eq!(bv.get(1), true);
257+
///
258+
/// // Can also use array indexing
259+
/// assert_eq!(bv[1], true);
255260
/// ```
256261
#[inline]
257262
pub fn get(&self, i: uint) -> bool {
@@ -275,7 +280,7 @@ impl Bitv {
275280
///
276281
/// let mut bv = Bitv::with_capacity(5, false);
277282
/// bv.set(3, true);
278-
/// assert_eq!(bv.get(3), true);
283+
/// assert_eq!(bv[3], true);
279284
/// ```
280285
#[inline]
281286
pub fn set(&mut self, i: uint, x: bool) {
@@ -478,7 +483,7 @@ impl Bitv {
478483
/// Organise the bits into bytes, such that the first bit in the
479484
/// `Bitv` becomes the high-order bit of the first byte. If the
480485
/// size of the `Bitv` is not a multiple of 8 then trailing bits
481-
/// will be filled-in with false/0.
486+
/// will be filled-in with `false`.
482487
///
483488
/// # Example
484489
///
@@ -716,9 +721,9 @@ impl Bitv {
716721
/// # Example
717722
///
718723
/// ```
719-
/// use std::collections::bitv::from_bytes;
724+
/// use std::collections::bitv;
720725
///
721-
/// let bv = from_bytes([0b10100000, 0b00010010]);
726+
/// let bv = bitv::from_bytes([0b10100000, 0b00010010]);
722727
/// assert!(bv.eq_vec([true, false, true, false,
723728
/// false, false, false, false,
724729
/// false, false, false, true,
@@ -898,7 +903,7 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
898903
///
899904
/// ```
900905
/// use std::collections::{BitvSet, Bitv};
901-
/// use std::collections::bitv::from_bytes;
906+
/// use std::collections::bitv;
902907
///
903908
/// // It's a regular set
904909
/// let mut s = BitvSet::new();
@@ -913,7 +918,7 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
913918
/// }
914919
///
915920
/// // Can initialize from a `Bitv`
916-
/// let other = BitvSet::from_bitv(from_bytes([0b11010000]));
921+
/// let other = BitvSet::from_bitv(bitv::from_bytes([0b11010000]));
917922
///
918923
/// s.union_with(&other);
919924
///
@@ -1048,7 +1053,7 @@ impl BitvSet {
10481053
/// s.insert(0);
10491054
///
10501055
/// let bv = s.get_ref();
1051-
/// assert_eq!(bv.get(0), true);
1056+
/// assert_eq!(bv[0], true);
10521057
/// ```
10531058
#[inline]
10541059
pub fn get_ref<'a>(&'a self) -> &'a Bitv {
@@ -1131,9 +1136,9 @@ impl BitvSet {
11311136
///
11321137
/// ```
11331138
/// use std::collections::BitvSet;
1134-
/// use std::collections::bitv::from_bytes;
1139+
/// use std::collections::bitv;
11351140
///
1136-
/// let s = BitvSet::from_bitv(from_bytes([0b01001010]));
1141+
/// let s = BitvSet::from_bitv(bitv::from_bytes([0b01001010]));
11371142
///
11381143
/// // Print 1, 4, 6 in arbitrary order
11391144
/// for x in s.iter() {
@@ -1152,10 +1157,10 @@ impl BitvSet {
11521157
///
11531158
/// ```
11541159
/// use std::collections::BitvSet;
1155-
/// use std::collections::bitv::from_bytes;
1160+
/// use std::collections::bitv;
11561161
///
1157-
/// let a = BitvSet::from_bitv(from_bytes([0b01101000]));
1158-
/// let b = BitvSet::from_bitv(from_bytes([0b10100000]));
1162+
/// let a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
1163+
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
11591164
///
11601165
/// // Print 0, 1, 2, 4 in arbitrary order
11611166
/// for x in a.union(&b) {
@@ -1180,10 +1185,10 @@ impl BitvSet {
11801185
///
11811186
/// ```
11821187
/// use std::collections::BitvSet;
1183-
/// use std::collections::bitv::from_bytes;
1188+
/// use std::collections::bitv;
11841189
///
1185-
/// let a = BitvSet::from_bitv(from_bytes([0b01101000]));
1186-
/// let b = BitvSet::from_bitv(from_bytes([0b10100000]));
1190+
/// let a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
1191+
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
11871192
///
11881193
/// // Print 2
11891194
/// for x in a.intersection(&b) {
@@ -1209,10 +1214,10 @@ impl BitvSet {
12091214
///
12101215
/// ```
12111216
/// use std::collections::BitvSet;
1212-
/// use std::collections::bitv::from_bytes;
1217+
/// use std::collections::bitv;
12131218
///
1214-
/// let a = BitvSet::from_bitv(from_bytes([0b01101000]));
1215-
/// let b = BitvSet::from_bitv(from_bytes([0b10100000]));
1219+
/// let a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
1220+
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
12161221
///
12171222
/// // Print 2, 4 in arbitrary order
12181223
/// for x in a.difference(&b) {
@@ -1245,10 +1250,10 @@ impl BitvSet {
12451250
///
12461251
/// ```
12471252
/// use std::collections::BitvSet;
1248-
/// use std::collections::bitv::from_bytes;
1253+
/// use std::collections::bitv;
12491254
///
1250-
/// let a = BitvSet::from_bitv(from_bytes([0b01101000]));
1251-
/// let b = BitvSet::from_bitv(from_bytes([0b10100000]));
1255+
/// let a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
1256+
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
12521257
///
12531258
/// // Print 0, 1, 4 in arbitrary order
12541259
/// for x in a.symmetric_difference(&b) {
@@ -1272,13 +1277,13 @@ impl BitvSet {
12721277
///
12731278
/// ```
12741279
/// use std::collections::BitvSet;
1275-
/// use std::collections::bitv::from_bytes;
1280+
/// use std::collections::bitv;
12761281
///
1277-
/// let mut a = BitvSet::from_bitv(from_bytes([0b01101000]));
1278-
/// let b = BitvSet::from_bitv(from_bytes([0b10100000]));
1282+
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
1283+
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
12791284
///
12801285
/// a.union_with(&b);
1281-
/// assert_eq!(a.unwrap(), from_bytes([0b11101000]));
1286+
/// assert_eq!(a.unwrap(), bitv::from_bytes([0b11101000]));
12821287
/// ```
12831288
#[inline]
12841289
pub fn union_with(&mut self, other: &BitvSet) {
@@ -1291,13 +1296,13 @@ impl BitvSet {
12911296
///
12921297
/// ```
12931298
/// use std::collections::BitvSet;
1294-
/// use std::collections::bitv::from_bytes;
1299+
/// use std::collections::bitv;
12951300
///
1296-
/// let mut a = BitvSet::from_bitv(from_bytes([0b01101000]));
1297-
/// let b = BitvSet::from_bitv(from_bytes([0b10100000]));
1301+
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
1302+
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
12981303
///
12991304
/// a.intersect_with(&b);
1300-
/// assert_eq!(a.unwrap(), from_bytes([0b00100000]));
1305+
/// assert_eq!(a.unwrap(), bitv::from_bytes([0b00100000]));
13011306
/// ```
13021307
#[inline]
13031308
pub fn intersect_with(&mut self, other: &BitvSet) {
@@ -1310,13 +1315,13 @@ impl BitvSet {
13101315
///
13111316
/// ```
13121317
/// use std::collections::BitvSet;
1313-
/// use std::collections::bitv::from_bytes;
1318+
/// use std::collections::bitv;
13141319
///
1315-
/// let mut a = BitvSet::from_bitv(from_bytes([0b01101000]));
1316-
/// let b = BitvSet::from_bitv(from_bytes([0b10100000]));
1320+
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
1321+
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
13171322
///
13181323
/// a.difference_with(&b);
1319-
/// assert_eq!(a.unwrap(), from_bytes([0b01001000]));
1324+
/// assert_eq!(a.unwrap(), bitv::from_bytes([0b01001000]));
13201325
/// ```
13211326
#[inline]
13221327
pub fn difference_with(&mut self, other: &BitvSet) {
@@ -1329,13 +1334,13 @@ impl BitvSet {
13291334
///
13301335
/// ```
13311336
/// use std::collections::BitvSet;
1332-
/// use std::collections::bitv::from_bytes;
1337+
/// use std::collections::bitv;
13331338
///
1334-
/// let mut a = BitvSet::from_bitv(from_bytes([0b01101000]));
1335-
/// let b = BitvSet::from_bitv(from_bytes([0b10100000]));
1339+
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([0b01101000]));
1340+
/// let b = BitvSet::from_bitv(bitv::from_bytes([0b10100000]));
13361341
///
13371342
/// a.symmetric_difference_with(&b);
1338-
/// assert_eq!(a.unwrap(), from_bytes([0b11001000]));
1343+
/// assert_eq!(a.unwrap(), bitv::from_bytes([0b11001000]));
13391344
/// ```
13401345
#[inline]
13411346
pub fn symmetric_difference_with(&mut self, other: &BitvSet) {

src/libcollections/lib.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -157,36 +157,36 @@ pub trait MutableSet<T>: Set<T> + Mutable {
157157
///
158158
/// # Example
159159
///
160-
/// With a `Deque` we can simulate a stack:
160+
/// With a `Deque` we can simulate a queue efficiently:
161161
///
162162
/// ```
163163
/// use std::collections::{RingBuf, Deque};
164164
///
165-
/// let mut stack = RingBuf::new();
166-
/// stack.push_front(1i);
167-
/// stack.push_front(2i);
168-
/// stack.push_front(3i);
165+
/// let mut queue = RingBuf::new();
166+
/// queue.push_back(1i);
167+
/// queue.push_back(2i);
168+
/// queue.push_back(3i);
169169
///
170-
/// // Will print 3, 2, 1
171-
/// while !stack.is_empty() {
172-
/// let x = stack.pop_front().unwrap();
170+
/// // Will print 1, 2, 3
171+
/// while !queue.is_empty() {
172+
/// let x = queue.pop_front().unwrap();
173173
/// println!("{}", x);
174174
/// }
175175
/// ```
176176
///
177-
/// We can simulate a queue:
177+
/// We can also simulate a stack:
178178
///
179179
/// ```
180180
/// use std::collections::{RingBuf, Deque};
181181
///
182-
/// let mut queue = RingBuf::new();
183-
/// queue.push_back(1i);
184-
/// queue.push_back(2i);
185-
/// queue.push_back(3i);
182+
/// let mut stack = RingBuf::new();
183+
/// stack.push_front(1i);
184+
/// stack.push_front(2i);
185+
/// stack.push_front(3i);
186186
///
187-
/// // Will print 1, 2, 3
188-
/// while !queue.is_empty() {
189-
/// let x = queue.pop_front().unwrap();
187+
/// // Will print 3, 2, 1
188+
/// while !stack.is_empty() {
189+
/// let x = stack.pop_front().unwrap();
190190
/// println!("{}", x);
191191
/// }
192192
/// ```
@@ -212,7 +212,7 @@ pub trait MutableSet<T>: Set<T> + Mutable {
212212
/// }
213213
/// ```
214214
pub trait Deque<T> : Mutable {
215-
/// Provide a reference to the front element, or `None` if the sequence is.
215+
/// Provide a reference to the front element, or `None` if the sequence is
216216
/// empty.
217217
///
218218
/// # Example
@@ -299,6 +299,7 @@ pub trait Deque<T> : Mutable {
299299
/// d.push_front(1i);
300300
/// d.push_front(2i);
301301
/// assert_eq!(d.front(), Some(&2i));
302+
/// ```
302303
fn push_front(&mut self, elt: T);
303304

304305
/// Insert an element last in the sequence.
@@ -312,6 +313,7 @@ pub trait Deque<T> : Mutable {
312313
/// d.push_back(1i);
313314
/// d.push_back(2i);
314315
/// assert_eq!(d.front(), Some(&1i));
316+
/// ```
315317
fn push_back(&mut self, elt: T);
316318

317319
/// Remove the last element and return it, or `None` if the sequence is empty.
@@ -328,6 +330,7 @@ pub trait Deque<T> : Mutable {
328330
/// assert_eq!(d.pop_back(), Some(2i));
329331
/// assert_eq!(d.pop_back(), Some(1i));
330332
/// assert_eq!(d.pop_back(), None);
333+
/// ```
331334
fn pop_back(&mut self) -> Option<T>;
332335

333336
/// Remove the first element and return it, or `None` if the sequence is empty.
@@ -344,6 +347,7 @@ pub trait Deque<T> : Mutable {
344347
/// assert_eq!(d.pop_front(), Some(1i));
345348
/// assert_eq!(d.pop_front(), Some(2i));
346349
/// assert_eq!(d.pop_front(), None);
350+
/// ```
347351
fn pop_front(&mut self) -> Option<T>;
348352
}
349353

0 commit comments

Comments
 (0)