@@ -39,14 +39,14 @@ impl<T: TotalOrd> PriorityQueue<T> {
39
39
Items { iter : self . data . iter ( ) }
40
40
}
41
41
42
- /// Returns the greatest item in the queue - fails if empty
43
- pub fn top < ' a > ( & ' a self ) -> & ' a T { self . data . get ( 0 ) }
44
-
45
- /// Returns the greatest item in the queue - None if empty
46
- pub fn maybe_top < ' a > ( & ' a self ) -> Option < & ' a T > {
47
- if self . is_empty ( ) { None } else { Some ( self . top ( ) ) }
42
+ /// Returns the greatest item in a queue or None if it is empty
43
+ pub fn top < ' a > ( & ' a self ) -> Option < & ' a T > {
44
+ if self . is_empty ( ) { None } else { Some ( self . data . get ( 0 ) ) }
48
45
}
49
46
47
+ #[ deprecated="renamed to `top`" ]
48
+ pub fn maybe_top < ' a > ( & ' a self ) -> Option < & ' a T > { self . top ( ) }
49
+
50
50
/// Returns the number of elements the queue can hold without reallocating
51
51
pub fn capacity ( & self ) -> uint { self . data . capacity ( ) }
52
52
@@ -60,20 +60,23 @@ impl<T: TotalOrd> PriorityQueue<T> {
60
60
self . data . reserve ( n)
61
61
}
62
62
63
- /// Pop the greatest item from the queue - fails if empty
64
- pub fn pop ( & mut self ) -> T {
65
- let mut item = self . data . pop ( ) . unwrap ( ) ;
66
- if !self . is_empty ( ) {
67
- swap ( & mut item, self . data . get_mut ( 0 ) ) ;
68
- self . siftdown ( 0 ) ;
63
+ /// Remove the greatest item from a queue and return it, or `None` if it is
64
+ /// empty.
65
+ pub fn pop ( & mut self ) -> Option < T > {
66
+ match self . data . pop ( ) {
67
+ None => { None }
68
+ Some ( mut item) => {
69
+ if !self . is_empty ( ) {
70
+ swap ( & mut item, self . data . get_mut ( 0 ) ) ;
71
+ self . siftdown ( 0 ) ;
72
+ }
73
+ Some ( item)
74
+ }
69
75
}
70
- item
71
76
}
72
77
73
- /// Pop the greatest item from the queue - None if empty
74
- pub fn maybe_pop ( & mut self ) -> Option < T > {
75
- if self . is_empty ( ) { None } else { Some ( self . pop ( ) ) }
76
- }
78
+ #[ deprecated="renamed to `pop`" ]
79
+ pub fn maybe_pop ( & mut self ) -> Option < T > { self . pop ( ) }
77
80
78
81
/// Push an item onto the queue
79
82
pub fn push ( & mut self , item : T ) {
@@ -84,18 +87,24 @@ impl<T: TotalOrd> PriorityQueue<T> {
84
87
85
88
/// Optimized version of a push followed by a pop
86
89
pub fn push_pop ( & mut self , mut item : T ) -> T {
87
- if !self . is_empty ( ) && * self . top ( ) > item {
90
+ if !self . is_empty ( ) && * self . top ( ) . unwrap ( ) > item {
88
91
swap ( & mut item, self . data . get_mut ( 0 ) ) ;
89
92
self . siftdown ( 0 ) ;
90
93
}
91
94
item
92
95
}
93
96
94
- /// Optimized version of a pop followed by a push - fails if empty
95
- pub fn replace ( & mut self , mut item : T ) -> T {
96
- swap ( & mut item, self . data . get_mut ( 0 ) ) ;
97
- self . siftdown ( 0 ) ;
98
- item
97
+ /// Optimized version of a pop followed by a push. The push is done
98
+ /// regardless of whether the queue is empty.
99
+ pub fn replace ( & mut self , mut item : T ) -> Option < T > {
100
+ if !self . is_empty ( ) {
101
+ swap ( & mut item, self . data . get_mut ( 0 ) ) ;
102
+ self . siftdown ( 0 ) ;
103
+ Some ( item)
104
+ } else {
105
+ self . push ( item) ;
106
+ None
107
+ }
99
108
}
100
109
101
110
#[ deprecated="renamed to `into_vec`" ]
@@ -117,7 +126,7 @@ impl<T: TotalOrd> PriorityQueue<T> {
117
126
q. data . as_mut_slice ( ) . swap ( 0 , end) ;
118
127
q. siftdown_range ( 0 , end)
119
128
}
120
- q. to_vec ( )
129
+ q. into_vec ( )
121
130
}
122
131
123
132
/// Create an empty PriorityQueue
@@ -247,53 +256,53 @@ mod tests {
247
256
sorted. sort ( ) ;
248
257
let mut heap = PriorityQueue :: from_vec ( data) ;
249
258
while !heap. is_empty ( ) {
250
- assert_eq ! ( heap. top( ) , sorted. last( ) . unwrap( ) ) ;
251
- assert_eq ! ( heap. pop( ) , sorted. pop( ) . unwrap( ) ) ;
259
+ assert_eq ! ( heap. top( ) . unwrap ( ) , sorted. last( ) . unwrap( ) ) ;
260
+ assert_eq ! ( heap. pop( ) . unwrap ( ) , sorted. pop( ) . unwrap( ) ) ;
252
261
}
253
262
}
254
263
255
264
#[ test]
256
265
fn test_push ( ) {
257
266
let mut heap = PriorityQueue :: from_vec ( vec ! ( 2 , 4 , 9 ) ) ;
258
267
assert_eq ! ( heap. len( ) , 3 ) ;
259
- assert ! ( * heap. top( ) == 9 ) ;
268
+ assert ! ( * heap. top( ) . unwrap ( ) == 9 ) ;
260
269
heap. push ( 11 ) ;
261
270
assert_eq ! ( heap. len( ) , 4 ) ;
262
- assert ! ( * heap. top( ) == 11 ) ;
271
+ assert ! ( * heap. top( ) . unwrap ( ) == 11 ) ;
263
272
heap. push ( 5 ) ;
264
273
assert_eq ! ( heap. len( ) , 5 ) ;
265
- assert ! ( * heap. top( ) == 11 ) ;
274
+ assert ! ( * heap. top( ) . unwrap ( ) == 11 ) ;
266
275
heap. push ( 27 ) ;
267
276
assert_eq ! ( heap. len( ) , 6 ) ;
268
- assert ! ( * heap. top( ) == 27 ) ;
277
+ assert ! ( * heap. top( ) . unwrap ( ) == 27 ) ;
269
278
heap. push ( 3 ) ;
270
279
assert_eq ! ( heap. len( ) , 7 ) ;
271
- assert ! ( * heap. top( ) == 27 ) ;
280
+ assert ! ( * heap. top( ) . unwrap ( ) == 27 ) ;
272
281
heap. push ( 103 ) ;
273
282
assert_eq ! ( heap. len( ) , 8 ) ;
274
- assert ! ( * heap. top( ) == 103 ) ;
283
+ assert ! ( * heap. top( ) . unwrap ( ) == 103 ) ;
275
284
}
276
285
277
286
#[ test]
278
287
fn test_push_unique ( ) {
279
288
let mut heap = PriorityQueue :: from_vec ( vec ! ( box 2 , box 4 , box 9 ) ) ;
280
289
assert_eq ! ( heap. len( ) , 3 ) ;
281
- assert ! ( * heap. top( ) == box 9 ) ;
290
+ assert ! ( * heap. top( ) . unwrap ( ) == box 9 ) ;
282
291
heap. push ( box 11 ) ;
283
292
assert_eq ! ( heap. len( ) , 4 ) ;
284
- assert ! ( * heap. top( ) == box 11 ) ;
293
+ assert ! ( * heap. top( ) . unwrap ( ) == box 11 ) ;
285
294
heap. push ( box 5 ) ;
286
295
assert_eq ! ( heap. len( ) , 5 ) ;
287
- assert ! ( * heap. top( ) == box 11 ) ;
296
+ assert ! ( * heap. top( ) . unwrap ( ) == box 11 ) ;
288
297
heap. push ( box 27 ) ;
289
298
assert_eq ! ( heap. len( ) , 6 ) ;
290
- assert ! ( * heap. top( ) == box 27 ) ;
299
+ assert ! ( * heap. top( ) . unwrap ( ) == box 27 ) ;
291
300
heap. push ( box 3 ) ;
292
301
assert_eq ! ( heap. len( ) , 7 ) ;
293
- assert ! ( * heap. top( ) == box 27 ) ;
302
+ assert ! ( * heap. top( ) . unwrap ( ) == box 27 ) ;
294
303
heap. push ( box 103 ) ;
295
304
assert_eq ! ( heap. len( ) , 8 ) ;
296
- assert ! ( * heap. top( ) == box 103 ) ;
305
+ assert ! ( * heap. top( ) . unwrap ( ) == box 103 ) ;
297
306
}
298
307
299
308
#[ test]
@@ -314,24 +323,24 @@ mod tests {
314
323
fn test_replace ( ) {
315
324
let mut heap = PriorityQueue :: from_vec ( vec ! ( 5 , 5 , 2 , 1 , 3 ) ) ;
316
325
assert_eq ! ( heap. len( ) , 5 ) ;
317
- assert_eq ! ( heap. replace( 6 ) , 5 ) ;
326
+ assert_eq ! ( heap. replace( 6 ) . unwrap ( ) , 5 ) ;
318
327
assert_eq ! ( heap. len( ) , 5 ) ;
319
- assert_eq ! ( heap. replace( 0 ) , 6 ) ;
328
+ assert_eq ! ( heap. replace( 0 ) . unwrap ( ) , 6 ) ;
320
329
assert_eq ! ( heap. len( ) , 5 ) ;
321
- assert_eq ! ( heap. replace( 4 ) , 5 ) ;
330
+ assert_eq ! ( heap. replace( 4 ) . unwrap ( ) , 5 ) ;
322
331
assert_eq ! ( heap. len( ) , 5 ) ;
323
- assert_eq ! ( heap. replace( 1 ) , 4 ) ;
332
+ assert_eq ! ( heap. replace( 1 ) . unwrap ( ) , 4 ) ;
324
333
assert_eq ! ( heap. len( ) , 5 ) ;
325
334
}
326
335
327
336
fn check_to_vec ( mut data : Vec < int > ) {
328
337
let heap = PriorityQueue :: from_vec ( data. clone ( ) ) ;
329
- let mut v = heap. clone ( ) . to_vec ( ) ;
338
+ let mut v = heap. clone ( ) . into_vec ( ) ;
330
339
v. sort ( ) ;
331
340
data. sort ( ) ;
332
341
333
342
assert_eq ! ( v, data) ;
334
- assert_eq ! ( heap. to_sorted_vec ( ) , data) ;
343
+ assert_eq ! ( heap. into_sorted_vec ( ) , data) ;
335
344
}
336
345
337
346
#[ test]
@@ -352,36 +361,21 @@ mod tests {
352
361
}
353
362
354
363
#[ test]
355
- #[ should_fail]
356
364
fn test_empty_pop ( ) {
357
365
let mut heap: PriorityQueue < int > = PriorityQueue :: new ( ) ;
358
- heap. pop ( ) ;
366
+ assert ! ( heap. pop( ) . is_none ( ) ) ;
359
367
}
360
368
361
369
#[ test]
362
- fn test_empty_maybe_pop ( ) {
363
- let mut heap: PriorityQueue < int > = PriorityQueue :: new ( ) ;
364
- assert ! ( heap. maybe_pop( ) . is_none( ) ) ;
365
- }
366
-
367
- #[ test]
368
- #[ should_fail]
369
370
fn test_empty_top ( ) {
370
371
let empty: PriorityQueue < int > = PriorityQueue :: new ( ) ;
371
- empty. top ( ) ;
372
- }
373
-
374
- #[ test]
375
- fn test_empty_maybe_top ( ) {
376
- let empty: PriorityQueue < int > = PriorityQueue :: new ( ) ;
377
- assert ! ( empty. maybe_top( ) . is_none( ) ) ;
372
+ assert ! ( empty. top( ) . is_none( ) ) ;
378
373
}
379
374
380
375
#[ test]
381
- #[ should_fail]
382
376
fn test_empty_replace ( ) {
383
377
let mut heap: PriorityQueue < int > = PriorityQueue :: new ( ) ;
384
- heap. replace ( 5 ) ;
378
+ heap. replace ( 5 ) . is_none ( ) ;
385
379
}
386
380
387
381
#[ test]
@@ -391,7 +385,7 @@ mod tests {
391
385
let mut q: PriorityQueue < uint > = xs. as_slice ( ) . iter ( ) . rev ( ) . map ( |& x| x) . collect ( ) ;
392
386
393
387
for & x in xs. iter ( ) {
394
- assert_eq ! ( q. pop( ) , x) ;
388
+ assert_eq ! ( q. pop( ) . unwrap ( ) , x) ;
395
389
}
396
390
}
397
391
}
0 commit comments