@@ -162,6 +162,8 @@ use core::ptr;
162
162
use slice;
163
163
use vec:: Vec ;
164
164
165
+ // FIXME(conventions): implement into_iter
166
+
165
167
/// A priority queue implemented with a binary heap.
166
168
///
167
169
/// This will be a max-heap.
@@ -184,6 +186,7 @@ impl<T: Ord> BinaryHeap<T> {
184
186
/// use std::collections::BinaryHeap;
185
187
/// let pq: BinaryHeap<uint> = BinaryHeap::new();
186
188
/// ```
189
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
187
190
pub fn new ( ) -> BinaryHeap < T > { BinaryHeap { data : vec ! ( ) , } }
188
191
189
192
/// Creates an empty `BinaryHeap` with a specific capacity.
@@ -197,6 +200,7 @@ impl<T: Ord> BinaryHeap<T> {
197
200
/// use std::collections::BinaryHeap;
198
201
/// let pq: BinaryHeap<uint> = BinaryHeap::with_capacity(10u);
199
202
/// ```
203
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
200
204
pub fn with_capacity ( capacity : uint ) -> BinaryHeap < T > {
201
205
BinaryHeap { data : Vec :: with_capacity ( capacity) }
202
206
}
@@ -234,6 +238,7 @@ impl<T: Ord> BinaryHeap<T> {
234
238
/// println!("{}", x);
235
239
/// }
236
240
/// ```
241
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
237
242
pub fn iter < ' a > ( & ' a self ) -> Items < ' a , T > {
238
243
Items { iter : self . data . iter ( ) }
239
244
}
@@ -268,10 +273,19 @@ impl<T: Ord> BinaryHeap<T> {
268
273
/// let pq: BinaryHeap<uint> = BinaryHeap::with_capacity(100u);
269
274
/// assert!(pq.capacity() >= 100u);
270
275
/// ```
276
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
271
277
pub fn capacity ( & self ) -> uint { self . data . capacity ( ) }
272
278
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`.
275
289
///
276
290
/// # Example
277
291
///
@@ -280,12 +294,17 @@ impl<T: Ord> BinaryHeap<T> {
280
294
///
281
295
/// let mut pq: BinaryHeap<uint> = BinaryHeap::new();
282
296
/// pq.reserve_exact(100u);
283
- /// assert!(pq.capacity() = = 100u);
297
+ /// assert!(pq.capacity() > = 100u);
284
298
/// ```
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) }
286
301
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`.
289
308
///
290
309
/// # Example
291
310
///
@@ -296,8 +315,15 @@ impl<T: Ord> BinaryHeap<T> {
296
315
/// pq.reserve(100u);
297
316
/// assert!(pq.capacity() >= 100u);
298
317
/// ```
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 ( )
301
327
}
302
328
303
329
/// Removes the greatest item from a queue and returns it, or `None` if it
@@ -314,6 +340,7 @@ impl<T: Ord> BinaryHeap<T> {
314
340
/// assert_eq!(pq.pop(), Some(1i));
315
341
/// assert_eq!(pq.pop(), None);
316
342
/// ```
343
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
317
344
pub fn pop ( & mut self ) -> Option < T > {
318
345
match self . data . pop ( ) {
319
346
None => { None }
@@ -342,6 +369,7 @@ impl<T: Ord> BinaryHeap<T> {
342
369
/// assert_eq!(pq.len(), 3);
343
370
/// assert_eq!(pq.top(), Some(&5i));
344
371
/// ```
372
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
345
373
pub fn push ( & mut self , item : T ) {
346
374
self . data . push ( item) ;
347
375
let new_len = self . len ( ) - 1 ;
@@ -495,12 +523,15 @@ impl<T: Ord> BinaryHeap<T> {
495
523
}
496
524
497
525
/// Returns the length of the queue.
526
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
498
527
pub fn len ( & self ) -> uint { self . data . len ( ) }
499
528
500
529
/// Returns true if the queue contains no elements
530
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
501
531
pub fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
502
532
503
533
/// Drops all items from the queue.
534
+ #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
504
535
pub fn clear ( & mut self ) { self . data . truncate ( 0 ) }
505
536
}
506
537
@@ -528,8 +559,7 @@ impl<T: Ord> Extendable<T> for BinaryHeap<T> {
528
559
fn extend < Iter : Iterator < T > > ( & mut self , mut iter : Iter ) {
529
560
let ( lower, _) = iter. size_hint ( ) ;
530
561
531
- let len = self . capacity ( ) ;
532
- self . reserve ( len + lower) ;
562
+ self . reserve ( lower) ;
533
563
534
564
for elem in iter {
535
565
self . push ( elem) ;
0 commit comments