@@ -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,15 +182,40 @@ impl<T: Ord> Default for PriorityQueue<T> {
180182}
181183
182184impl < T : Ord > PriorityQueue < T > {
183- /// Create an empty PriorityQueue
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+ /// ```
184193 pub fn new ( ) -> PriorityQueue < T > { PriorityQueue { data : vec ! ( ) , } }
185194
186- /// Create an empty PriorityQueue with capacity `capacity`
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+ /// ```
187206 pub fn with_capacity ( capacity : uint ) -> PriorityQueue < T > {
188207 PriorityQueue { data : Vec :: with_capacity ( capacity) }
189208 }
190209
191- /// Create a PriorityQueue from a vector (heapify)
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+ /// ```
192219 pub fn from_vec ( xs : Vec < T > ) -> PriorityQueue < T > {
193220 let mut q = PriorityQueue { data : xs, } ;
194221 let mut n = q. len ( ) / 2 ;
@@ -201,33 +228,101 @@ impl<T: Ord> PriorityQueue<T> {
201228
202229 /// An iterator visiting all values in underlying vector, in
203230 /// 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+ /// ```
204243 pub fn iter < ' a > ( & ' a self ) -> Items < ' a , T > {
205244 Items { iter : self . data . iter ( ) }
206245 }
207246
208- /// 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+ /// ```
209263 pub fn top < ' a > ( & ' a self ) -> Option < & ' a T > {
210264 if self . is_empty ( ) { None } else { Some ( self . data . get ( 0 ) ) }
211265 }
212266
213267 #[ deprecated="renamed to `top`" ]
214268 pub fn maybe_top < ' a > ( & ' a self ) -> Option < & ' a T > { self . top ( ) }
215269
216- /// 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+ /// ```
217280 pub fn capacity ( & self ) -> uint { self . data . capacity ( ) }
218281
219- /// Reserve capacity for exactly n elements in the PriorityQueue.
282+ /// Reserve capacity for exactly `n` elements in the PriorityQueue.
220283 /// 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+ /// ```
221294 pub fn reserve_exact ( & mut self , n : uint ) { self . data . reserve_exact ( n) }
222295
223- /// Reserve capacity for at least n elements in the PriorityQueue.
296+ /// Reserve capacity for at least `n` elements in the PriorityQueue.
224297 /// 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+ /// ```
225308 pub fn reserve ( & mut self , n : uint ) {
226309 self . data . reserve ( n)
227310 }
228311
229312 /// Remove the greatest item from a queue and return it, or `None` if it is
230313 /// 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+ /// ```
231326 pub fn pop ( & mut self ) -> Option < T > {
232327 match self . data . pop ( ) {
233328 None => { None }
@@ -244,14 +339,43 @@ impl<T: Ord> PriorityQueue<T> {
244339 #[ deprecated="renamed to `pop`" ]
245340 pub fn maybe_pop ( & mut self ) -> Option < T > { self . pop ( ) }
246341
247- /// 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+ /// ```
248357 pub fn push ( & mut self , item : T ) {
249358 self . data . push ( item) ;
250359 let new_len = self . len ( ) - 1 ;
251360 self . siftup ( 0 , new_len) ;
252361 }
253362
254- /// 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+ /// ```
255379 pub fn push_pop ( & mut self , mut item : T ) -> T {
256380 if !self . is_empty ( ) && * self . top ( ) . unwrap ( ) > item {
257381 swap ( & mut item, self . data . get_mut ( 0 ) ) ;
@@ -262,6 +386,19 @@ impl<T: Ord> PriorityQueue<T> {
262386
263387 /// Optimized version of a pop followed by a push. The push is done
264388 /// 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+ /// ```
265402 pub fn replace ( & mut self , mut item : T ) -> Option < T > {
266403 if !self . is_empty ( ) {
267404 swap ( & mut item, self . data . get_mut ( 0 ) ) ;
@@ -282,10 +419,38 @@ impl<T: Ord> PriorityQueue<T> {
282419 fn to_sorted_vec ( self ) -> Vec < T > { self . into_sorted_vec ( ) }
283420
284421 /// 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+ /// ```
285437 pub fn into_vec ( self ) -> Vec < T > { let PriorityQueue { data : v} = self ; v }
286438
287439 /// Consume the PriorityQueue and return a vector in sorted
288- /// (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+ /// ```
289454 pub fn into_sorted_vec ( self ) -> Vec < T > {
290455 let mut q = self ;
291456 let mut end = q. len ( ) ;
@@ -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