@@ -22,30 +22,30 @@ pub struct PriorityQueue<T> {
22
22
data : Vec < T > ,
23
23
}
24
24
25
- impl < T : TotalOrd > Container for PriorityQueue < T > {
25
+ impl < T : Ord > 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 : TotalOrd > Mutable for PriorityQueue < T > {
30
+ impl < T : Ord > 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 : TotalOrd > PriorityQueue < T > {
35
+ impl < T : Ord > 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 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 ) ) }
45
- }
42
+ /// Returns the greatest item in the queue - fails if empty
43
+ pub fn top < ' a > ( & ' a self ) -> & ' a T { self . data . get ( 0 ) }
46
44
47
- #[ deprecated="renamed to `top`" ]
48
- pub fn maybe_top < ' a > ( & ' a self ) -> Option < & ' a T > { self . top ( ) }
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 ( ) ) }
48
+ }
49
49
50
50
/// Returns the number of elements the queue can hold without reallocating
51
51
pub fn capacity ( & self ) -> uint { self . data . capacity ( ) }
@@ -60,23 +60,20 @@ impl<T: TotalOrd> PriorityQueue<T> {
60
60
self . data . reserve ( n)
61
61
}
62
62
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
- }
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 ) ;
75
69
}
70
+ item
76
71
}
77
72
78
- #[ deprecated="renamed to `pop`" ]
79
- pub fn maybe_pop ( & mut self ) -> Option < T > { self . pop ( ) }
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
+ }
80
77
81
78
/// Push an item onto the queue
82
79
pub fn push ( & mut self , item : T ) {
@@ -87,48 +84,34 @@ impl<T: TotalOrd> PriorityQueue<T> {
87
84
88
85
/// Optimized version of a push followed by a pop
89
86
pub fn push_pop ( & mut self , mut item : T ) -> T {
90
- if !self . is_empty ( ) && * self . top ( ) . unwrap ( ) > item {
87
+ if !self . is_empty ( ) && * self . top ( ) > item {
91
88
swap ( & mut item, self . data . get_mut ( 0 ) ) ;
92
89
self . siftdown ( 0 ) ;
93
90
}
94
91
item
95
92
}
96
93
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
- }
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
108
99
}
109
100
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
-
118
101
/// Consume the PriorityQueue and return the underlying vector
119
- pub fn into_vec ( self ) -> Vec < T > { let PriorityQueue { data : v} = self ; v }
102
+ pub fn to_vec ( self ) -> Vec < T > { let PriorityQueue { data : v} = self ; v }
120
103
121
104
/// Consume the PriorityQueue and return a vector in sorted
122
105
/// (ascending) order
123
- pub fn into_sorted_vec ( self ) -> Vec < T > {
106
+ pub fn to_sorted_vec ( self ) -> Vec < T > {
124
107
let mut q = self ;
125
108
let mut end = q. len ( ) ;
126
109
while end > 1 {
127
110
end -= 1 ;
128
111
q. data . as_mut_slice ( ) . swap ( 0 , end) ;
129
112
q. siftdown_range ( 0 , end)
130
113
}
131
- q. into_vec ( )
114
+ q. to_vec ( )
132
115
}
133
116
134
117
/// Create an empty PriorityQueue
@@ -214,15 +197,15 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> {
214
197
fn size_hint ( & self ) -> ( uint , Option < uint > ) { self . iter . size_hint ( ) }
215
198
}
216
199
217
- impl < T : TotalOrd > FromIterator < T > for PriorityQueue < T > {
200
+ impl < T : Ord > FromIterator < T > for PriorityQueue < T > {
218
201
fn from_iter < Iter : Iterator < T > > ( iter : Iter ) -> PriorityQueue < T > {
219
202
let mut q = PriorityQueue :: new ( ) ;
220
203
q. extend ( iter) ;
221
204
q
222
205
}
223
206
}
224
207
225
- impl < T : TotalOrd > Extendable < T > for PriorityQueue < T > {
208
+ impl < T : Ord > Extendable < T > for PriorityQueue < T > {
226
209
fn extend < Iter : Iterator < T > > ( & mut self , mut iter : Iter ) {
227
210
let ( lower, _) = iter. size_hint ( ) ;
228
211
@@ -258,53 +241,53 @@ mod tests {
258
241
sorted. sort ( ) ;
259
242
let mut heap = PriorityQueue :: from_vec ( data) ;
260
243
while !heap. is_empty ( ) {
261
- assert_eq ! ( heap. top( ) . unwrap ( ) , sorted. last( ) . unwrap( ) ) ;
262
- assert_eq ! ( heap. pop( ) . unwrap ( ) , sorted. pop( ) . unwrap( ) ) ;
244
+ assert_eq ! ( heap. top( ) , sorted. last( ) . unwrap( ) ) ;
245
+ assert_eq ! ( heap. pop( ) , sorted. pop( ) . unwrap( ) ) ;
263
246
}
264
247
}
265
248
266
249
#[ test]
267
250
fn test_push ( ) {
268
251
let mut heap = PriorityQueue :: from_vec ( vec ! ( 2 , 4 , 9 ) ) ;
269
252
assert_eq ! ( heap. len( ) , 3 ) ;
270
- assert ! ( * heap. top( ) . unwrap ( ) == 9 ) ;
253
+ assert ! ( * heap. top( ) == 9 ) ;
271
254
heap. push ( 11 ) ;
272
255
assert_eq ! ( heap. len( ) , 4 ) ;
273
- assert ! ( * heap. top( ) . unwrap ( ) == 11 ) ;
256
+ assert ! ( * heap. top( ) == 11 ) ;
274
257
heap. push ( 5 ) ;
275
258
assert_eq ! ( heap. len( ) , 5 ) ;
276
- assert ! ( * heap. top( ) . unwrap ( ) == 11 ) ;
259
+ assert ! ( * heap. top( ) == 11 ) ;
277
260
heap. push ( 27 ) ;
278
261
assert_eq ! ( heap. len( ) , 6 ) ;
279
- assert ! ( * heap. top( ) . unwrap ( ) == 27 ) ;
262
+ assert ! ( * heap. top( ) == 27 ) ;
280
263
heap. push ( 3 ) ;
281
264
assert_eq ! ( heap. len( ) , 7 ) ;
282
- assert ! ( * heap. top( ) . unwrap ( ) == 27 ) ;
265
+ assert ! ( * heap. top( ) == 27 ) ;
283
266
heap. push ( 103 ) ;
284
267
assert_eq ! ( heap. len( ) , 8 ) ;
285
- assert ! ( * heap. top( ) . unwrap ( ) == 103 ) ;
268
+ assert ! ( * heap. top( ) == 103 ) ;
286
269
}
287
270
288
271
#[ test]
289
272
fn test_push_unique ( ) {
290
273
let mut heap = PriorityQueue :: from_vec ( vec ! ( box 2 , box 4 , box 9 ) ) ;
291
274
assert_eq ! ( heap. len( ) , 3 ) ;
292
- assert ! ( * heap. top( ) . unwrap ( ) == box 9 ) ;
275
+ assert ! ( * heap. top( ) == box 9 ) ;
293
276
heap. push ( box 11 ) ;
294
277
assert_eq ! ( heap. len( ) , 4 ) ;
295
- assert ! ( * heap. top( ) . unwrap ( ) == box 11 ) ;
278
+ assert ! ( * heap. top( ) == box 11 ) ;
296
279
heap. push ( box 5 ) ;
297
280
assert_eq ! ( heap. len( ) , 5 ) ;
298
- assert ! ( * heap. top( ) . unwrap ( ) == box 11 ) ;
281
+ assert ! ( * heap. top( ) == box 11 ) ;
299
282
heap. push ( box 27 ) ;
300
283
assert_eq ! ( heap. len( ) , 6 ) ;
301
- assert ! ( * heap. top( ) . unwrap ( ) == box 27 ) ;
284
+ assert ! ( * heap. top( ) == box 27 ) ;
302
285
heap. push ( box 3 ) ;
303
286
assert_eq ! ( heap. len( ) , 7 ) ;
304
- assert ! ( * heap. top( ) . unwrap ( ) == box 27 ) ;
287
+ assert ! ( * heap. top( ) == box 27 ) ;
305
288
heap. push ( box 103 ) ;
306
289
assert_eq ! ( heap. len( ) , 8 ) ;
307
- assert ! ( * heap. top( ) . unwrap ( ) == box 103 ) ;
290
+ assert ! ( * heap. top( ) == box 103 ) ;
308
291
}
309
292
310
293
#[ test]
@@ -325,24 +308,24 @@ mod tests {
325
308
fn test_replace ( ) {
326
309
let mut heap = PriorityQueue :: from_vec ( vec ! ( 5 , 5 , 2 , 1 , 3 ) ) ;
327
310
assert_eq ! ( heap. len( ) , 5 ) ;
328
- assert_eq ! ( heap. replace( 6 ) . unwrap ( ) , 5 ) ;
311
+ assert_eq ! ( heap. replace( 6 ) , 5 ) ;
329
312
assert_eq ! ( heap. len( ) , 5 ) ;
330
- assert_eq ! ( heap. replace( 0 ) . unwrap ( ) , 6 ) ;
313
+ assert_eq ! ( heap. replace( 0 ) , 6 ) ;
331
314
assert_eq ! ( heap. len( ) , 5 ) ;
332
- assert_eq ! ( heap. replace( 4 ) . unwrap ( ) , 5 ) ;
315
+ assert_eq ! ( heap. replace( 4 ) , 5 ) ;
333
316
assert_eq ! ( heap. len( ) , 5 ) ;
334
- assert_eq ! ( heap. replace( 1 ) . unwrap ( ) , 4 ) ;
317
+ assert_eq ! ( heap. replace( 1 ) , 4 ) ;
335
318
assert_eq ! ( heap. len( ) , 5 ) ;
336
319
}
337
320
338
321
fn check_to_vec ( mut data : Vec < int > ) {
339
322
let heap = PriorityQueue :: from_vec ( data. clone ( ) ) ;
340
- let mut v = heap. clone ( ) . into_vec ( ) ;
323
+ let mut v = heap. clone ( ) . to_vec ( ) ;
341
324
v. sort ( ) ;
342
325
data. sort ( ) ;
343
326
344
327
assert_eq ! ( v, data) ;
345
- assert_eq ! ( heap. into_sorted_vec ( ) , data) ;
328
+ assert_eq ! ( heap. to_sorted_vec ( ) , data) ;
346
329
}
347
330
348
331
#[ test]
@@ -363,21 +346,36 @@ mod tests {
363
346
}
364
347
365
348
#[ test]
349
+ #[ should_fail]
366
350
fn test_empty_pop ( ) {
367
351
let mut heap: PriorityQueue < int > = PriorityQueue :: new ( ) ;
368
- assert ! ( heap. pop( ) . is_none( ) ) ;
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( ) ) ;
369
359
}
370
360
371
361
#[ test]
362
+ #[ should_fail]
372
363
fn test_empty_top ( ) {
373
364
let empty: PriorityQueue < int > = PriorityQueue :: new ( ) ;
374
- assert ! ( empty. top( ) . is_none( ) ) ;
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( ) ) ;
375
372
}
376
373
377
374
#[ test]
375
+ #[ should_fail]
378
376
fn test_empty_replace ( ) {
379
377
let mut heap: PriorityQueue < int > = PriorityQueue :: new ( ) ;
380
- heap. replace ( 5 ) . is_none ( ) ;
378
+ heap. replace ( 5 ) ;
381
379
}
382
380
383
381
#[ test]
@@ -387,7 +385,7 @@ mod tests {
387
385
let mut q: PriorityQueue < uint > = xs. as_slice ( ) . iter ( ) . rev ( ) . map ( |& x| x) . collect ( ) ;
388
386
389
387
for & x in xs. iter ( ) {
390
- assert_eq ! ( q. pop( ) . unwrap ( ) , x) ;
388
+ assert_eq ! ( q. pop( ) , x) ;
391
389
}
392
390
}
393
391
}
0 commit comments