@@ -105,8 +105,11 @@ impl <T: Ord> PriorityQueue<T> {
105
105
q. to_vec ( )
106
106
}
107
107
108
+ /// Create an empty PriorityQueue
109
+ static pure fn new( ) -> PriorityQueue <T > { PriorityQueue { data: ~[ ] , } }
110
+
108
111
/// Create a PriorityQueue from a vector (heapify)
109
- static pub pure fn from_vec ( xs : ~[ T ] ) -> PriorityQueue < T > {
112
+ static pure fn from_vec( xs: ~[ T ] ) -> PriorityQueue < T > {
110
113
let mut q = PriorityQueue { data : xs, } ;
111
114
let mut n = q. len ( ) / 2 ;
112
115
while n > 0 {
@@ -168,7 +171,7 @@ impl <T: Ord> PriorityQueue<T> {
168
171
mod tests {
169
172
use sort:: merge_sort;
170
173
use core:: cmp:: le;
171
- use priority_queue:: PriorityQueue :: from_vec;
174
+ use priority_queue:: PriorityQueue :: { from_vec, new } ;
172
175
173
176
#[ test]
174
177
fn test_top_and_pop ( ) {
@@ -279,30 +282,27 @@ mod tests {
279
282
#[ test]
280
283
#[ should_fail]
281
284
#[ ignore( cfg( windows) ) ]
282
- fn test_empty_pop ( ) { let mut heap = from_vec :: < int > ( ~ [ ] ) ; heap. pop ( ) ; }
285
+ fn test_empty_pop ( ) { let mut heap = new :: < int > ( ) ; heap. pop ( ) ; }
283
286
284
287
#[ test]
285
288
fn test_empty_maybe_pop ( ) {
286
- let mut heap = from_vec :: < int > ( ~ [ ] ) ;
289
+ let mut heap = new :: < int > ( ) ;
287
290
assert heap. maybe_pop ( ) . is_none ( ) ;
288
291
}
289
292
290
293
#[ test]
291
294
#[ should_fail]
292
295
#[ ignore( cfg( windows) ) ]
293
- fn test_empty_top ( ) { let empty = from_vec :: < int > ( ~ [ ] ) ; empty. top ( ) ; }
296
+ fn test_empty_top ( ) { let empty = new :: < int > ( ) ; empty. top ( ) ; }
294
297
295
298
#[ test]
296
299
fn test_empty_maybe_top ( ) {
297
- let empty = from_vec :: < int > ( ~ [ ] ) ;
300
+ let empty = new :: < int > ( ) ;
298
301
assert empty. maybe_top ( ) . is_none ( ) ;
299
302
}
300
303
301
304
#[ test]
302
305
#[ should_fail]
303
306
#[ ignore( cfg( windows) ) ]
304
- fn test_empty_replace ( ) {
305
- let mut heap = from_vec :: < int > ( ~[ ] ) ;
306
- heap. replace ( 5 ) ;
307
- }
307
+ fn test_empty_replace ( ) { let mut heap = new ( ) ; heap. replace ( 5 ) ; }
308
308
}
0 commit comments