Skip to content

Commit 42344b5

Browse files
committed
Make bitv's APIs match RFC + fixup
1 parent 24329d7 commit 42344b5

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

src/libcollections/bit.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ use core::num::Int;
9191
use core::slice::{Items, MutItems};
9292
use core::{u8, u32, uint};
9393

94-
use hash;
94+
use core::hash;
9595
use Vec;
9696

9797
type Blocks<'a> = Cloned<Items<'a, u32>>;
@@ -922,7 +922,7 @@ pub fn from_bytes(bytes: &[u8]) -> Bitv {
922922

923923
/// Deprecated: Now a static method on Bitv.
924924
#[deprecated = "Now a static method on Bitv"]
925-
pub fn from_fn<F>(len: uint, mut f: F) -> Bitv where F: FnMut(uint) -> bool {
925+
pub fn from_fn<F>(len: uint, f: F) -> Bitv where F: FnMut(uint) -> bool {
926926
Bitv::from_fn(len, f)
927927
}
928928

@@ -1226,55 +1226,53 @@ impl BitvSet {
12261226
self.bitv.capacity()
12271227
}
12281228

1229-
/// Reserves capacity for an element to be inserted at `index` in the given
1230-
/// `Bitv`. The collection may reserve more space to avoid frequent reallocations.
1229+
/// Reserves capacity for the given `BitvSet` to contain `len` distinct elements. In the case
1230+
/// of `BitvSet` this means reallocations will not occur as long as all inserted elements
1231+
/// are less than `len`.
12311232
///
1232-
/// # Panics
1233+
/// The collection may reserve more space to avoid frequent reallocations.
12331234
///
1234-
/// Panics if the new capacity overflows `uint`.
12351235
///
12361236
/// # Examples
12371237
///
12381238
/// ```
12391239
/// use std::collections::BitvSet;
12401240
///
12411241
/// let mut s = BitvSet::new();
1242-
/// s.reserve_index(10);
1243-
/// assert!(s.capacity() >= 11);
1242+
/// s.reserve_len(10);
1243+
/// assert!(s.capacity() >= 10);
12441244
/// ```
12451245
#[unstable = "matches collection reform specification, waiting for dust to settle"]
1246-
pub fn reserve_index(&mut self, index: uint) {
1247-
let len = self.bitv.len();
1248-
if index >= len {
1249-
self.bitv.reserve(index - len + 1);
1246+
pub fn reserve_len(&mut self, len: uint) {
1247+
let cur_len = self.bitv.len();
1248+
if len >= cur_len {
1249+
self.bitv.reserve(len - cur_len);
12501250
}
12511251
}
12521252

1253-
/// Reserves the minimum capacity for an element to be inserted at `index`
1254-
/// in the given `BitvSet`. Does nothing if the capacity is already sufficient.
1253+
/// Reserves the minimum capacity for the given `BitvSet` to contain `len` distinct elements.
1254+
/// In the case of `BitvSet` this means reallocations will not occur as long as all inserted
1255+
/// elements are less than `len`.
12551256
///
12561257
/// Note that the allocator may give the collection more space than it requests. Therefore
1257-
/// capacity can not be relied upon to be precisely minimal. Prefer `reserve_index` if future
1258+
/// capacity can not be relied upon to be precisely minimal. Prefer `reserve_len` if future
12581259
/// insertions are expected.
12591260
///
1260-
/// # Panics
1261-
///
1262-
/// Panics if the new capacity overflows `uint`.
12631261
///
12641262
/// # Examples
12651263
///
12661264
/// ```
12671265
/// use std::collections::BitvSet;
12681266
///
12691267
/// let mut s = BitvSet::new();
1270-
/// s.reserve_index_exact(10);
1271-
/// assert!(s.capacity() >= 11);
1268+
/// s.reserve_len_exact(10);
1269+
/// assert!(s.capacity() >= 10);
12721270
/// ```
12731271
#[unstable = "matches collection reform specification, waiting for dust to settle"]
1274-
pub fn reserve_index_exact(&mut self, index: uint) {
1275-
let len = self.bitv.len();
1276-
if index >= len {
1277-
self.bitv.reserve_exact(index - len + 1);
1272+
pub fn reserve_len_exact(&mut self, len: uint) {
1273+
let cur_len = self.bitv.len();
1274+
if len >= cur_len {
1275+
self.bitv.reserve_exact(len - cur_len);
12781276
}
12791277
}
12801278

@@ -2636,9 +2634,9 @@ mod bitv_set_test {
26362634
for &b in bools.iter() {
26372635
for &l in lengths.iter() {
26382636
let bitset = BitvSet::from_bitv(Bitv::from_elem(l, b));
2639-
assert_eq!(bitset.contains(&1u), b)
2640-
assert_eq!(bitset.contains(&(l-1u)), b)
2641-
assert!(!bitset.contains(&l))
2637+
assert_eq!(bitset.contains(&1u), b);
2638+
assert_eq!(bitset.contains(&(l-1u)), b);
2639+
assert!(!bitset.contains(&l));
26422640
}
26432641
}
26442642
}

0 commit comments

Comments
 (0)