@@ -158,7 +158,9 @@ use {Collection, Mutable, MutableSeq};
158158use slice;
159159use vec:: Vec ;
160160
161- /// A priority queue implemented with a binary heap
161+ /// A priority queue implemented with a binary heap.
162+ ///
163+ /// This will be a max-heap.
162164#[ deriving( Clone ) ]
163165pub struct PriorityQueue < T > {
164166 data : Vec < T > ,
@@ -180,35 +182,147 @@ impl<T: Ord> Default for PriorityQueue<T> {
180182}
181183
182184impl < T : Ord > PriorityQueue < T > {
185+ /// Create an empty PriorityQueue as a max-heap.
186+ ///
187+ /// # Example
188+ ///
189+ /// ```
190+ /// use std::collections::PriorityQueue;
191+ /// let pq: PriorityQueue<uint> = PriorityQueue::new();
192+ /// ```
193+ pub fn new ( ) -> PriorityQueue < T > { PriorityQueue { data : vec ! ( ) , } }
194+
195+ /// Create an empty PriorityQueue with a specific capacity.
196+ /// This preallocates enough memory for `capacity` elements,
197+ /// so that the PriorityQueue does not have to be reallocated
198+ /// until it contains at least that many values.
199+ ///
200+ /// # Example
201+ ///
202+ /// ```
203+ /// use std::collections::PriorityQueue;
204+ /// let pq: PriorityQueue<uint> = PriorityQueue::with_capacity(10u);
205+ /// ```
206+ pub fn with_capacity ( capacity : uint ) -> PriorityQueue < T > {
207+ PriorityQueue { data : Vec :: with_capacity ( capacity) }
208+ }
209+
210+ /// Create a PriorityQueue from a vector. This is sometimes called
211+ /// `heapifying` the vector.
212+ ///
213+ /// # Example
214+ ///
215+ /// ```
216+ /// use std::collections::PriorityQueue;
217+ /// let pq = PriorityQueue::from_vec(vec![9i, 1, 2, 7, 3, 2]);
218+ /// ```
219+ pub fn from_vec ( xs : Vec < T > ) -> PriorityQueue < T > {
220+ let mut q = PriorityQueue { data : xs, } ;
221+ let mut n = q. len ( ) / 2 ;
222+ while n > 0 {
223+ n -= 1 ;
224+ q. siftdown ( n)
225+ }
226+ q
227+ }
228+
183229 /// An iterator visiting all values in underlying vector, in
184230 /// arbitrary order.
231+ ///
232+ /// # Example
233+ ///
234+ /// ```
235+ /// use std::collections::PriorityQueue;
236+ /// let pq = PriorityQueue::from_vec(vec![1i, 2, 3, 4]);
237+ ///
238+ /// // Print 1, 2, 3, 4 in arbitrary order
239+ /// for x in pq.iter() {
240+ /// println!("{}", x);
241+ /// }
242+ /// ```
185243 pub fn iter < ' a > ( & ' a self ) -> Items < ' a , T > {
186244 Items { iter : self . data . iter ( ) }
187245 }
188246
189- /// Returns the greatest item in a queue or None if it is empty
247+ /// Returns the greatest item in a queue or `None` if it is empty.
248+ ///
249+ /// # Example
250+ ///
251+ /// ```
252+ /// use std::collections::PriorityQueue;
253+ ///
254+ /// let mut pq = PriorityQueue::new();
255+ /// assert_eq!(pq.top(), None);
256+ ///
257+ /// pq.push(1i);
258+ /// pq.push(5i);
259+ /// pq.push(2i);
260+ /// assert_eq!(pq.top(), Some(&5i));
261+ ///
262+ /// ```
190263 pub fn top < ' a > ( & ' a self ) -> Option < & ' a T > {
191264 if self . is_empty ( ) { None } else { Some ( self . data . get ( 0 ) ) }
192265 }
193266
194267 #[ deprecated="renamed to `top`" ]
195268 pub fn maybe_top < ' a > ( & ' a self ) -> Option < & ' a T > { self . top ( ) }
196269
197- /// Returns the number of elements the queue can hold without reallocating
270+ /// Returns the number of elements the queue can hold without reallocating.
271+ ///
272+ /// # Example
273+ ///
274+ /// ```
275+ /// use std::collections::PriorityQueue;
276+ ///
277+ /// let pq: PriorityQueue<uint> = PriorityQueue::with_capacity(100u);
278+ /// assert!(pq.capacity() >= 100u);
279+ /// ```
198280 pub fn capacity ( & self ) -> uint { self . data . capacity ( ) }
199281
200- /// Reserve capacity for exactly n elements in the PriorityQueue.
282+ /// Reserve capacity for exactly `n` elements in the PriorityQueue.
201283 /// Do nothing if the capacity is already sufficient.
284+ ///
285+ /// # Example
286+ ///
287+ /// ```
288+ /// use std::collections::PriorityQueue;
289+ ///
290+ /// let mut pq: PriorityQueue<uint> = PriorityQueue::new();
291+ /// pq.reserve_exact(100u);
292+ /// assert!(pq.capacity() == 100u);
293+ /// ```
202294 pub fn reserve_exact ( & mut self , n : uint ) { self . data . reserve_exact ( n) }
203295
204- /// Reserve capacity for at least n elements in the PriorityQueue.
296+ /// Reserve capacity for at least `n` elements in the PriorityQueue.
205297 /// Do nothing if the capacity is already sufficient.
298+ ///
299+ /// # Example
300+ ///
301+ /// ```
302+ /// use std::collections::PriorityQueue;
303+ ///
304+ /// let mut pq: PriorityQueue<uint> = PriorityQueue::new();
305+ /// pq.reserve(100u);
306+ /// assert!(pq.capacity() >= 100u);
307+ /// ```
206308 pub fn reserve ( & mut self , n : uint ) {
207309 self . data . reserve ( n)
208310 }
209311
210312 /// Remove the greatest item from a queue and return it, or `None` if it is
211313 /// empty.
314+ ///
315+ /// # Example
316+ ///
317+ /// ```
318+ /// use std::collections::PriorityQueue;
319+ ///
320+ /// let mut pq = PriorityQueue::from_vec(vec![1i, 3]);
321+ ///
322+ /// assert_eq!(pq.pop(), Some(3i));
323+ /// assert_eq!(pq.pop(), Some(1i));
324+ /// assert_eq!(pq.pop(), None);
325+ /// ```
212326 pub fn pop ( & mut self ) -> Option < T > {
213327 match self . data . pop ( ) {
214328 None => { None }
@@ -225,14 +339,43 @@ impl<T: Ord> PriorityQueue<T> {
225339 #[ deprecated="renamed to `pop`" ]
226340 pub fn maybe_pop ( & mut self ) -> Option < T > { self . pop ( ) }
227341
228- /// Push an item onto the queue
342+ /// Push an item onto the queue.
343+ ///
344+ /// # Example
345+ ///
346+ /// ```
347+ /// use std::collections::PriorityQueue;
348+ ///
349+ /// let mut pq = PriorityQueue::new();
350+ /// pq.push(3i);
351+ /// pq.push(5i);
352+ /// pq.push(1i);
353+ ///
354+ /// assert_eq!(pq.len(), 3);
355+ /// assert_eq!(pq.top(), Some(&5i));
356+ /// ```
229357 pub fn push ( & mut self , item : T ) {
230358 self . data . push ( item) ;
231359 let new_len = self . len ( ) - 1 ;
232360 self . siftup ( 0 , new_len) ;
233361 }
234362
235- /// Optimized version of a push followed by a pop
363+ /// Optimized version of a push followed by a pop.
364+ ///
365+ /// # Example
366+ ///
367+ /// ```
368+ /// use std::collections::PriorityQueue;
369+ ///
370+ /// let mut pq = PriorityQueue::new();
371+ /// pq.push(1i);
372+ /// pq.push(5i);
373+ ///
374+ /// assert_eq!(pq.push_pop(3i), 5);
375+ /// assert_eq!(pq.push_pop(9i), 9);
376+ /// assert_eq!(pq.len(), 2);
377+ /// assert_eq!(pq.top(), Some(&3i));
378+ /// ```
236379 pub fn push_pop ( & mut self , mut item : T ) -> T {
237380 if !self . is_empty ( ) && * self . top ( ) . unwrap ( ) > item {
238381 swap ( & mut item, self . data . get_mut ( 0 ) ) ;
@@ -243,6 +386,19 @@ impl<T: Ord> PriorityQueue<T> {
243386
244387 /// Optimized version of a pop followed by a push. The push is done
245388 /// regardless of whether the queue is empty.
389+ ///
390+ /// # Example
391+ ///
392+ /// ```
393+ /// use std::collections::PriorityQueue;
394+ ///
395+ /// let mut pq = PriorityQueue::new();
396+ ///
397+ /// assert_eq!(pq.replace(1i), None);
398+ /// assert_eq!(pq.replace(3i), Some(1i));
399+ /// assert_eq!(pq.len(), 1);
400+ /// assert_eq!(pq.top(), Some(&3i));
401+ /// ```
246402 pub fn replace ( & mut self , mut item : T ) -> Option < T > {
247403 if !self . is_empty ( ) {
248404 swap ( & mut item, self . data . get_mut ( 0 ) ) ;
@@ -263,10 +419,38 @@ impl<T: Ord> PriorityQueue<T> {
263419 fn to_sorted_vec ( self ) -> Vec < T > { self . into_sorted_vec ( ) }
264420
265421 /// Consume the PriorityQueue and return the underlying vector
422+ /// in arbitrary order.
423+ ///
424+ /// # Example
425+ ///
426+ /// ```
427+ /// use std::collections::PriorityQueue;
428+ ///
429+ /// let pq = PriorityQueue::from_vec(vec![1i, 2, 3, 4, 5, 6, 7]);
430+ /// let vec = pq.into_vec();
431+ ///
432+ /// // Will print in some order
433+ /// for x in vec.iter() {
434+ /// println!("{}", x);
435+ /// }
436+ /// ```
266437 pub fn into_vec ( self ) -> Vec < T > { let PriorityQueue { data : v} = self ; v }
267438
268439 /// Consume the PriorityQueue and return a vector in sorted
269- /// (ascending) order
440+ /// (ascending) order.
441+ ///
442+ /// # Example
443+ ///
444+ /// ```
445+ /// use std::collections::PriorityQueue;
446+ ///
447+ /// let mut pq = PriorityQueue::from_vec(vec![1i, 2, 4, 5, 7]);
448+ /// pq.push(6);
449+ /// pq.push(3);
450+ ///
451+ /// let vec = pq.into_sorted_vec();
452+ /// assert_eq!(vec, vec![1i, 2, 3, 4, 5, 6, 7]);
453+ /// ```
270454 pub fn into_sorted_vec ( self ) -> Vec < T > {
271455 let mut q = self ;
272456 let mut end = q. len ( ) ;
@@ -278,25 +462,6 @@ impl<T: Ord> PriorityQueue<T> {
278462 q. into_vec ( )
279463 }
280464
281- /// Create an empty PriorityQueue
282- pub fn new ( ) -> PriorityQueue < T > { PriorityQueue { data : vec ! ( ) , } }
283-
284- /// Create an empty PriorityQueue with capacity `capacity`
285- pub fn with_capacity ( capacity : uint ) -> PriorityQueue < T > {
286- PriorityQueue { data : Vec :: with_capacity ( capacity) }
287- }
288-
289- /// Create a PriorityQueue from a vector (heapify)
290- pub fn from_vec ( xs : Vec < T > ) -> PriorityQueue < T > {
291- let mut q = PriorityQueue { data : xs, } ;
292- let mut n = q. len ( ) / 2 ;
293- while n > 0 {
294- n -= 1 ;
295- q. siftdown ( n)
296- }
297- q
298- }
299-
300465 // The implementations of siftup and siftdown use unsafe blocks in
301466 // order to move an element out of the vector (leaving behind a
302467 // zeroed element), shift along the others and move it back into the
@@ -348,7 +513,7 @@ impl<T: Ord> PriorityQueue<T> {
348513 }
349514}
350515
351- /// PriorityQueue iterator
516+ /// PriorityQueue iterator.
352517pub struct Items < ' a , T > {
353518 iter : slice:: Items < ' a , T > ,
354519}
0 commit comments