@@ -22,31 +22,31 @@ pub struct PriorityQueue<T> {
22
22
data : Vec < T > ,
23
23
}
24
24
25
- impl < T : Ord > Container for PriorityQueue < T > {
25
+ impl < T : TotalOrd > Container for PriorityQueue < T > {
26
26
/// Returns the length of the queue
27
27
fn len ( & self ) -> uint { self . data . len ( ) }
28
28
}
29
29
30
- impl < T : Ord > Mutable for PriorityQueue < T > {
30
+ impl < T : TotalOrd > Mutable for PriorityQueue < T > {
31
31
/// Drop all items from the queue
32
32
fn clear ( & mut self ) { self . data . truncate ( 0 ) }
33
33
}
34
34
35
- impl < T : Ord > PriorityQueue < T > {
35
+ impl < T : TotalOrd > PriorityQueue < T > {
36
36
/// An iterator visiting all values in underlying vector, in
37
37
/// arbitrary order.
38
38
pub fn iter < ' a > ( & ' a self ) -> Items < ' a , 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:Ord> 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,34 +87,48 @@ impl<T:Ord> 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
110
+ #[ allow( dead_code) ]
111
+ #[ deprecated="renamed to `into_vec`" ]
112
+ fn to_vec ( self ) -> Vec < T > { self . into_vec ( ) }
113
+
114
+ #[ allow( dead_code) ]
115
+ #[ deprecated="renamed to `into_sorted_vec`" ]
116
+ fn to_sorted_vec ( self ) -> Vec < T > { self . into_sorted_vec ( ) }
117
+
101
118
/// Consume the PriorityQueue and return the underlying vector
102
- pub fn to_vec ( self ) -> Vec < T > { let PriorityQueue { data : v} = self ; v }
119
+ pub fn into_vec ( self ) -> Vec < T > { let PriorityQueue { data : v} = self ; v }
103
120
104
121
/// Consume the PriorityQueue and return a vector in sorted
105
122
/// (ascending) order
106
- pub fn to_sorted_vec ( self ) -> Vec < T > {
123
+ pub fn into_sorted_vec ( self ) -> Vec < T > {
107
124
let mut q = self ;
108
125
let mut end = q. len ( ) ;
109
126
while end > 1 {
110
127
end -= 1 ;
111
128
q. data . as_mut_slice ( ) . swap ( 0 , end) ;
112
129
q. siftdown_range ( 0 , end)
113
130
}
114
- q. to_vec ( )
131
+ q. into_vec ( )
115
132
}
116
133
117
134
/// Create an empty PriorityQueue
@@ -197,15 +214,15 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> {
197
214
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
198
215
}
199
216
200
- impl < T : Ord > FromIterator < T > for PriorityQueue < T > {
217
+ impl < T : TotalOrd > FromIterator < T > for PriorityQueue < T > {
201
218
fn from_iter < Iter : Iterator < T > > ( iter : Iter ) -> PriorityQueue < T > {
202
219
let mut q = PriorityQueue :: new ( ) ;
203
220
q. extend ( iter) ;
204
221
q
205
222
}
206
223
}
207
224
208
- impl < T : Ord > Extendable < T > for PriorityQueue < T > {
225
+ impl < T : TotalOrd > Extendable < T > for PriorityQueue < T > {
209
226
fn extend < Iter : Iterator < T > > ( & mut self , mut iter : Iter ) {
210
227
let ( lower, _) = iter. size_hint ( ) ;
211
228
@@ -241,53 +258,53 @@ mod tests {
241
258
sorted. sort ( ) ;
242
259
let mut heap = PriorityQueue :: from_vec ( data) ;
243
260
while !heap. is_empty ( ) {
244
- assert_eq ! ( heap. top( ) , sorted. last( ) . unwrap( ) ) ;
245
- assert_eq ! ( heap. pop( ) , sorted. pop( ) . unwrap( ) ) ;
261
+ assert_eq ! ( heap. top( ) . unwrap ( ) , sorted. last( ) . unwrap( ) ) ;
262
+ assert_eq ! ( heap. pop( ) . unwrap ( ) , sorted. pop( ) . unwrap( ) ) ;
246
263
}
247
264
}
248
265
249
266
#[ test]
250
267
fn test_push ( ) {
251
268
let mut heap = PriorityQueue :: from_vec ( vec ! ( 2 , 4 , 9 ) ) ;
252
269
assert_eq ! ( heap. len( ) , 3 ) ;
253
- assert ! ( * heap. top( ) == 9 ) ;
270
+ assert ! ( * heap. top( ) . unwrap ( ) == 9 ) ;
254
271
heap. push ( 11 ) ;
255
272
assert_eq ! ( heap. len( ) , 4 ) ;
256
- assert ! ( * heap. top( ) == 11 ) ;
273
+ assert ! ( * heap. top( ) . unwrap ( ) == 11 ) ;
257
274
heap. push ( 5 ) ;
258
275
assert_eq ! ( heap. len( ) , 5 ) ;
259
- assert ! ( * heap. top( ) == 11 ) ;
276
+ assert ! ( * heap. top( ) . unwrap ( ) == 11 ) ;
260
277
heap. push ( 27 ) ;
261
278
assert_eq ! ( heap. len( ) , 6 ) ;
262
- assert ! ( * heap. top( ) == 27 ) ;
279
+ assert ! ( * heap. top( ) . unwrap ( ) == 27 ) ;
263
280
heap. push ( 3 ) ;
264
281
assert_eq ! ( heap. len( ) , 7 ) ;
265
- assert ! ( * heap. top( ) == 27 ) ;
282
+ assert ! ( * heap. top( ) . unwrap ( ) == 27 ) ;
266
283
heap. push ( 103 ) ;
267
284
assert_eq ! ( heap. len( ) , 8 ) ;
268
- assert ! ( * heap. top( ) == 103 ) ;
285
+ assert ! ( * heap. top( ) . unwrap ( ) == 103 ) ;
269
286
}
270
287
271
288
#[ test]
272
289
fn test_push_unique ( ) {
273
290
let mut heap = PriorityQueue :: from_vec ( vec ! ( box 2 , box 4 , box 9 ) ) ;
274
291
assert_eq ! ( heap. len( ) , 3 ) ;
275
- assert ! ( * heap. top( ) == box 9 ) ;
292
+ assert ! ( * heap. top( ) . unwrap ( ) == box 9 ) ;
276
293
heap. push ( box 11 ) ;
277
294
assert_eq ! ( heap. len( ) , 4 ) ;
278
- assert ! ( * heap. top( ) == box 11 ) ;
295
+ assert ! ( * heap. top( ) . unwrap ( ) == box 11 ) ;
279
296
heap. push ( box 5 ) ;
280
297
assert_eq ! ( heap. len( ) , 5 ) ;
281
- assert ! ( * heap. top( ) == box 11 ) ;
298
+ assert ! ( * heap. top( ) . unwrap ( ) == box 11 ) ;
282
299
heap. push ( box 27 ) ;
283
300
assert_eq ! ( heap. len( ) , 6 ) ;
284
- assert ! ( * heap. top( ) == box 27 ) ;
301
+ assert ! ( * heap. top( ) . unwrap ( ) == box 27 ) ;
285
302
heap. push ( box 3 ) ;
286
303
assert_eq ! ( heap. len( ) , 7 ) ;
287
- assert ! ( * heap. top( ) == box 27 ) ;
304
+ assert ! ( * heap. top( ) . unwrap ( ) == box 27 ) ;
288
305
heap. push ( box 103 ) ;
289
306
assert_eq ! ( heap. len( ) , 8 ) ;
290
- assert ! ( * heap. top( ) == box 103 ) ;
307
+ assert ! ( * heap. top( ) . unwrap ( ) == box 103 ) ;
291
308
}
292
309
293
310
#[ test]
@@ -308,24 +325,24 @@ mod tests {
308
325
fn test_replace ( ) {
309
326
let mut heap = PriorityQueue :: from_vec ( vec ! ( 5 , 5 , 2 , 1 , 3 ) ) ;
310
327
assert_eq ! ( heap. len( ) , 5 ) ;
311
- assert_eq ! ( heap. replace( 6 ) , 5 ) ;
328
+ assert_eq ! ( heap. replace( 6 ) . unwrap ( ) , 5 ) ;
312
329
assert_eq ! ( heap. len( ) , 5 ) ;
313
- assert_eq ! ( heap. replace( 0 ) , 6 ) ;
330
+ assert_eq ! ( heap. replace( 0 ) . unwrap ( ) , 6 ) ;
314
331
assert_eq ! ( heap. len( ) , 5 ) ;
315
- assert_eq ! ( heap. replace( 4 ) , 5 ) ;
332
+ assert_eq ! ( heap. replace( 4 ) . unwrap ( ) , 5 ) ;
316
333
assert_eq ! ( heap. len( ) , 5 ) ;
317
- assert_eq ! ( heap. replace( 1 ) , 4 ) ;
334
+ assert_eq ! ( heap. replace( 1 ) . unwrap ( ) , 4 ) ;
318
335
assert_eq ! ( heap. len( ) , 5 ) ;
319
336
}
320
337
321
338
fn check_to_vec ( mut data : Vec < int > ) {
322
339
let heap = PriorityQueue :: from_vec ( data. clone ( ) ) ;
323
- let mut v = heap. clone ( ) . to_vec ( ) ;
340
+ let mut v = heap. clone ( ) . into_vec ( ) ;
324
341
v. sort ( ) ;
325
342
data. sort ( ) ;
326
343
327
344
assert_eq ! ( v, data) ;
328
- assert_eq ! ( heap. to_sorted_vec ( ) , data) ;
345
+ assert_eq ! ( heap. into_sorted_vec ( ) , data) ;
329
346
}
330
347
331
348
#[ test]
@@ -346,36 +363,21 @@ mod tests {
346
363
}
347
364
348
365
#[ test]
349
- #[ should_fail]
350
366
fn test_empty_pop ( ) {
351
367
let mut heap: PriorityQueue < int > = PriorityQueue :: new ( ) ;
352
- heap. pop ( ) ;
353
- }
354
-
355
- #[ test]
356
- fn test_empty_maybe_pop ( ) {
357
- let mut heap: PriorityQueue < int > = PriorityQueue :: new ( ) ;
358
- assert ! ( heap. maybe_pop( ) . is_none( ) ) ;
368
+ assert ! ( heap. pop( ) . is_none( ) ) ;
359
369
}
360
370
361
371
#[ test]
362
- #[ should_fail]
363
372
fn test_empty_top ( ) {
364
373
let empty: PriorityQueue < int > = PriorityQueue :: new ( ) ;
365
- empty. top ( ) ;
366
- }
367
-
368
- #[ test]
369
- fn test_empty_maybe_top ( ) {
370
- let empty: PriorityQueue < int > = PriorityQueue :: new ( ) ;
371
- assert ! ( empty. maybe_top( ) . is_none( ) ) ;
374
+ assert ! ( empty. top( ) . is_none( ) ) ;
372
375
}
373
376
374
377
#[ test]
375
- #[ should_fail]
376
378
fn test_empty_replace ( ) {
377
379
let mut heap: PriorityQueue < int > = PriorityQueue :: new ( ) ;
378
- heap. replace ( 5 ) ;
380
+ heap. replace ( 5 ) . is_none ( ) ;
379
381
}
380
382
381
383
#[ test]
@@ -385,7 +387,7 @@ mod tests {
385
387
let mut q: PriorityQueue < uint > = xs. as_slice ( ) . iter ( ) . rev ( ) . map ( |& x| x) . collect ( ) ;
386
388
387
389
for & x in xs. iter ( ) {
388
- assert_eq ! ( q. pop( ) , x) ;
390
+ assert_eq ! ( q. pop( ) . unwrap ( ) , x) ;
389
391
}
390
392
}
391
393
}
0 commit comments