Skip to content

Commit cf3b2e4

Browse files
committed
Implement low-hanging fruit of collection conventions
* Renames/deprecates the simplest and most obvious methods * Adds FIXME(conventions)s for outstanding work * Marks "handled" methods as unstable NOTE: the semantics of reserve and reserve_exact have changed! Other methods have had their semantics changed as well, but in a way that should obviously not typecheck if used incorrectly. Lots of work and breakage to come, but this handles most of the core APIs and most eggregious breakage. Future changes should *mostly* focus on niche collections, APIs, or simply back-compat additions. [breaking-change]
1 parent 60a669a commit cf3b2e4

File tree

21 files changed

+1386
-1062
lines changed

21 files changed

+1386
-1062
lines changed

src/libcollections/binary_heap.rs

+40-10
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ use core::ptr;
162162
use slice;
163163
use vec::Vec;
164164

165+
// FIXME(conventions): implement into_iter
166+
165167
/// A priority queue implemented with a binary heap.
166168
///
167169
/// This will be a max-heap.
@@ -184,6 +186,7 @@ impl<T: Ord> BinaryHeap<T> {
184186
/// use std::collections::BinaryHeap;
185187
/// let pq: BinaryHeap<uint> = BinaryHeap::new();
186188
/// ```
189+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
187190
pub fn new() -> BinaryHeap<T> { BinaryHeap{data: vec!(),} }
188191

189192
/// Creates an empty `BinaryHeap` with a specific capacity.
@@ -197,6 +200,7 @@ impl<T: Ord> BinaryHeap<T> {
197200
/// use std::collections::BinaryHeap;
198201
/// let pq: BinaryHeap<uint> = BinaryHeap::with_capacity(10u);
199202
/// ```
203+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
200204
pub fn with_capacity(capacity: uint) -> BinaryHeap<T> {
201205
BinaryHeap { data: Vec::with_capacity(capacity) }
202206
}
@@ -234,6 +238,7 @@ impl<T: Ord> BinaryHeap<T> {
234238
/// println!("{}", x);
235239
/// }
236240
/// ```
241+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
237242
pub fn iter<'a>(&'a self) -> Items<'a, T> {
238243
Items { iter: self.data.iter() }
239244
}
@@ -268,10 +273,19 @@ impl<T: Ord> BinaryHeap<T> {
268273
/// let pq: BinaryHeap<uint> = BinaryHeap::with_capacity(100u);
269274
/// assert!(pq.capacity() >= 100u);
270275
/// ```
276+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
271277
pub fn capacity(&self) -> uint { self.data.capacity() }
272278

273-
/// Reserves capacity for exactly `n` elements in the `BinaryHeap`.
274-
/// Do nothing if the capacity is already sufficient.
279+
/// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
280+
/// given `BinaryHeap`. Does nothing if the capacity is already sufficient.
281+
///
282+
/// Note that the allocator may give the collection more space than it requests. Therefore
283+
/// capacity can not be relied upon to be precisely minimal. Prefer `reserve` if future
284+
/// insertions are expected.
285+
///
286+
/// # Panics
287+
///
288+
/// Panics if the new capacity overflows `uint`.
275289
///
276290
/// # Example
277291
///
@@ -280,12 +294,17 @@ impl<T: Ord> BinaryHeap<T> {
280294
///
281295
/// let mut pq: BinaryHeap<uint> = BinaryHeap::new();
282296
/// pq.reserve_exact(100u);
283-
/// assert!(pq.capacity() == 100u);
297+
/// assert!(pq.capacity() >= 100u);
284298
/// ```
285-
pub fn reserve_exact(&mut self, n: uint) { self.data.reserve_exact(n) }
299+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
300+
pub fn reserve_exact(&mut self, additional: uint) { self.data.reserve_exact(additional) }
286301

287-
/// Reserves capacity for at least `n` elements in the `BinaryHeap`.
288-
/// Do nothing if the capacity is already sufficient.
302+
/// Reserves capacity for at least `additional` more elements to be inserted in the
303+
/// `BinaryHeap`. The collection may reserve more space to avoid frequent reallocations.
304+
///
305+
/// # Panics
306+
///
307+
/// Panics if the new capacity overflows `uint`.
289308
///
290309
/// # Example
291310
///
@@ -296,8 +315,15 @@ impl<T: Ord> BinaryHeap<T> {
296315
/// pq.reserve(100u);
297316
/// assert!(pq.capacity() >= 100u);
298317
/// ```
299-
pub fn reserve(&mut self, n: uint) {
300-
self.data.reserve(n)
318+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
319+
pub fn reserve(&mut self, additional: uint) {
320+
self.data.reserve(additional)
321+
}
322+
323+
/// Discards as much additional capacity as possible.
324+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
325+
pub fn shrink_to_fit(&mut self) {
326+
self.data.shrink_to_fit()
301327
}
302328

303329
/// Removes the greatest item from a queue and returns it, or `None` if it
@@ -314,6 +340,7 @@ impl<T: Ord> BinaryHeap<T> {
314340
/// assert_eq!(pq.pop(), Some(1i));
315341
/// assert_eq!(pq.pop(), None);
316342
/// ```
343+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
317344
pub fn pop(&mut self) -> Option<T> {
318345
match self.data.pop() {
319346
None => { None }
@@ -342,6 +369,7 @@ impl<T: Ord> BinaryHeap<T> {
342369
/// assert_eq!(pq.len(), 3);
343370
/// assert_eq!(pq.top(), Some(&5i));
344371
/// ```
372+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
345373
pub fn push(&mut self, item: T) {
346374
self.data.push(item);
347375
let new_len = self.len() - 1;
@@ -495,12 +523,15 @@ impl<T: Ord> BinaryHeap<T> {
495523
}
496524

497525
/// Returns the length of the queue.
526+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
498527
pub fn len(&self) -> uint { self.data.len() }
499528

500529
/// Returns true if the queue contains no elements
530+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
501531
pub fn is_empty(&self) -> bool { self.len() == 0 }
502532

503533
/// Drops all items from the queue.
534+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
504535
pub fn clear(&mut self) { self.data.truncate(0) }
505536
}
506537

@@ -528,8 +559,7 @@ impl<T: Ord> Extendable<T> for BinaryHeap<T> {
528559
fn extend<Iter: Iterator<T>>(&mut self, mut iter: Iter) {
529560
let (lower, _) = iter.size_hint();
530561

531-
let len = self.capacity();
532-
self.reserve(len + lower);
562+
self.reserve(lower);
533563

534564
for elem in iter {
535565
self.push(elem);

src/libcollections/bit.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ use std::hash;
7575

7676
use vec::Vec;
7777

78+
// FIXME(conventions): look, we just need to refactor this whole thing. Inside and out.
79+
7880
type MatchWords<'a> = Chain<MaskWords<'a>, Skip<Take<Enumerate<Repeat<u32>>>>>;
7981
// Take two BitV's, and return iterators of their words, where the shorter one
8082
// has been padded with 0's
@@ -216,6 +218,7 @@ impl Bitv {
216218
/// use std::collections::Bitv;
217219
/// let mut bv = Bitv::new();
218220
/// ```
221+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
219222
pub fn new() -> Bitv {
220223
Bitv { storage: Vec::new(), nbits: 0 }
221224
}
@@ -613,6 +616,7 @@ impl Bitv {
613616
/// bv.truncate(2);
614617
/// assert!(bv.eq_vec([false, true]));
615618
/// ```
619+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
616620
pub fn truncate(&mut self, len: uint) {
617621
if len < self.len() {
618622
self.nbits = len;
@@ -760,14 +764,17 @@ impl Bitv {
760764

761765
/// Return the total number of bits in this vector
762766
#[inline]
767+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
763768
pub fn len(&self) -> uint { self.nbits }
764769

765770
/// Returns true if there are no bits in this vector
766771
#[inline]
772+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
767773
pub fn is_empty(&self) -> bool { self.len() == 0 }
768774

769775
/// Clears all bits in this vector.
770776
#[inline]
777+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
771778
pub fn clear(&mut self) {
772779
for w in self.storage.iter_mut() { *w = 0u32; }
773780
}
@@ -849,8 +856,7 @@ impl Clone for Bitv {
849856
#[inline]
850857
fn clone_from(&mut self, source: &Bitv) {
851858
self.nbits = source.nbits;
852-
self.storage.reserve(source.storage.len());
853-
for (i, w) in self.storage.iter_mut().enumerate() { *w = source.storage[i]; }
859+
self.storage.clone_from(&source.storage);
854860
}
855861
}
856862

@@ -1052,6 +1058,7 @@ impl BitvSet {
10521058
/// let mut s = BitvSet::new();
10531059
/// ```
10541060
#[inline]
1061+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
10551062
pub fn new() -> BitvSet {
10561063
BitvSet(Bitv::new())
10571064
}
@@ -1067,6 +1074,7 @@ impl BitvSet {
10671074
/// assert!(s.capacity() >= 100);
10681075
/// ```
10691076
#[inline]
1077+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
10701078
pub fn with_capacity(nbits: uint) -> BitvSet {
10711079
let bitv = Bitv::with_capacity(nbits, false);
10721080
BitvSet::from_bitv(bitv)
@@ -1106,6 +1114,7 @@ impl BitvSet {
11061114
/// assert!(s.capacity() >= 100);
11071115
/// ```
11081116
#[inline]
1117+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
11091118
pub fn capacity(&self) -> uint {
11101119
let &BitvSet(ref bitv) = self;
11111120
bitv.capacity()
@@ -1212,6 +1221,7 @@ impl BitvSet {
12121221
/// println!("new capacity: {}", s.capacity());
12131222
/// ```
12141223
#[inline]
1224+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
12151225
pub fn shrink_to_fit(&mut self) {
12161226
let &BitvSet(ref mut bitv) = self;
12171227
// Obtain original length
@@ -1240,6 +1250,7 @@ impl BitvSet {
12401250
/// }
12411251
/// ```
12421252
#[inline]
1253+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
12431254
pub fn iter<'a>(&'a self) -> BitPositions<'a> {
12441255
BitPositions {set: self, next_idx: 0u}
12451256
}
@@ -1262,6 +1273,7 @@ impl BitvSet {
12621273
/// }
12631274
/// ```
12641275
#[inline]
1276+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
12651277
pub fn union<'a>(&'a self, other: &'a BitvSet) -> TwoBitPositions<'a> {
12661278
TwoBitPositions {
12671279
set: self,
@@ -1290,6 +1302,7 @@ impl BitvSet {
12901302
/// }
12911303
/// ```
12921304
#[inline]
1305+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
12931306
pub fn intersection<'a>(&'a self, other: &'a BitvSet) -> Take<TwoBitPositions<'a>> {
12941307
let min = cmp::min(self.capacity(), other.capacity());
12951308
TwoBitPositions {
@@ -1326,6 +1339,7 @@ impl BitvSet {
13261339
/// }
13271340
/// ```
13281341
#[inline]
1342+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
13291343
pub fn difference<'a>(&'a self, other: &'a BitvSet) -> TwoBitPositions<'a> {
13301344
TwoBitPositions {
13311345
set: self,
@@ -1355,6 +1369,7 @@ impl BitvSet {
13551369
/// }
13561370
/// ```
13571371
#[inline]
1372+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
13581373
pub fn symmetric_difference<'a>(&'a self, other: &'a BitvSet) -> TwoBitPositions<'a> {
13591374
TwoBitPositions {
13601375
set: self,
@@ -1473,27 +1488,31 @@ impl BitvSet {
14731488

14741489
/// Return the number of set bits in this set.
14751490
#[inline]
1491+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
14761492
pub fn len(&self) -> uint {
14771493
let &BitvSet(ref bitv) = self;
14781494
bitv.storage.iter().fold(0, |acc, &n| acc + n.count_ones())
14791495
}
14801496

14811497
/// Returns whether there are no bits set in this set
14821498
#[inline]
1499+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
14831500
pub fn is_empty(&self) -> bool {
14841501
let &BitvSet(ref bitv) = self;
14851502
bitv.storage.iter().all(|&n| n == 0)
14861503
}
14871504

14881505
/// Clears all bits in this set
14891506
#[inline]
1507+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
14901508
pub fn clear(&mut self) {
14911509
let &BitvSet(ref mut bitv) = self;
14921510
bitv.clear();
14931511
}
14941512

14951513
/// Returns `true` if this set contains the specified integer.
14961514
#[inline]
1515+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
14971516
pub fn contains(&self, value: &uint) -> bool {
14981517
let &BitvSet(ref bitv) = self;
14991518
*value < bitv.nbits && bitv.get(*value)
@@ -1502,12 +1521,14 @@ impl BitvSet {
15021521
/// Returns `true` if the set has no elements in common with `other`.
15031522
/// This is equivalent to checking for an empty intersection.
15041523
#[inline]
1524+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
15051525
pub fn is_disjoint(&self, other: &BitvSet) -> bool {
15061526
self.intersection(other).next().is_none()
15071527
}
15081528

15091529
/// Returns `true` if the set is a subset of another.
15101530
#[inline]
1531+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
15111532
pub fn is_subset(&self, other: &BitvSet) -> bool {
15121533
let &BitvSet(ref self_bitv) = self;
15131534
let &BitvSet(ref other_bitv) = other;
@@ -1521,12 +1542,14 @@ impl BitvSet {
15211542

15221543
/// Returns `true` if the set is a superset of another.
15231544
#[inline]
1545+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
15241546
pub fn is_superset(&self, other: &BitvSet) -> bool {
15251547
other.is_subset(self)
15261548
}
15271549

15281550
/// Adds a value to the set. Returns `true` if the value was not already
15291551
/// present in the set.
1552+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
15301553
pub fn insert(&mut self, value: uint) -> bool {
15311554
if self.contains(&value) {
15321555
return false;
@@ -1545,6 +1568,7 @@ impl BitvSet {
15451568

15461569
/// Removes a value from the set. Returns `true` if the value was
15471570
/// present in the set.
1571+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
15481572
pub fn remove(&mut self, value: &uint) -> bool {
15491573
if !self.contains(value) {
15501574
return false;

0 commit comments

Comments
 (0)