Skip to content

Commit b41b73b

Browse files
committed
---
yaml --- r: 113649 b: refs/heads/snap-stage3 c: 2980605 h: refs/heads/master i: 113647: 833fe9b v: v3
1 parent f167db4 commit b41b73b

File tree

2 files changed

+58
-64
lines changed

2 files changed

+58
-64
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: abdacecdf86b4b5a4f432560445a24e1c5f4751b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 2b06105c2a9f188df1e24c10c3d28c8f869cd558
4+
refs/heads/snap-stage3: 29806052cec82925c09bc77f39730925a5e4c732
55
refs/heads/try: 7c6c492fb2af9a85f21ff952942df3523b22fd17
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libcollections/priority_queue.rs

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ impl<T: TotalOrd> PriorityQueue<T> {
3939
Items { iter: self.data.iter() }
4040
}
4141

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)) }
4845
}
4946

47+
#[deprecated="renamed to `top`"]
48+
pub fn maybe_top<'a>(&'a self) -> Option<&'a T> { self.top() }
49+
5050
/// Returns the number of elements the queue can hold without reallocating
5151
pub fn capacity(&self) -> uint { self.data.capacity() }
5252

@@ -60,20 +60,23 @@ impl<T: TotalOrd> PriorityQueue<T> {
6060
self.data.reserve(n)
6161
}
6262

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+
}
6975
}
70-
item
7176
}
7277

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() }
7780

7881
/// Push an item onto the queue
7982
pub fn push(&mut self, item: T) {
@@ -84,18 +87,24 @@ impl<T: TotalOrd> PriorityQueue<T> {
8487

8588
/// Optimized version of a push followed by a pop
8689
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 {
8891
swap(&mut item, self.data.get_mut(0));
8992
self.siftdown(0);
9093
}
9194
item
9295
}
9396

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+
}
99108
}
100109

101110
#[deprecated="renamed to `into_vec`"]
@@ -117,7 +126,7 @@ impl<T: TotalOrd> PriorityQueue<T> {
117126
q.data.as_mut_slice().swap(0, end);
118127
q.siftdown_range(0, end)
119128
}
120-
q.to_vec()
129+
q.into_vec()
121130
}
122131

123132
/// Create an empty PriorityQueue
@@ -247,53 +256,53 @@ mod tests {
247256
sorted.sort();
248257
let mut heap = PriorityQueue::from_vec(data);
249258
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());
252261
}
253262
}
254263

255264
#[test]
256265
fn test_push() {
257266
let mut heap = PriorityQueue::from_vec(vec!(2, 4, 9));
258267
assert_eq!(heap.len(), 3);
259-
assert!(*heap.top() == 9);
268+
assert!(*heap.top().unwrap() == 9);
260269
heap.push(11);
261270
assert_eq!(heap.len(), 4);
262-
assert!(*heap.top() == 11);
271+
assert!(*heap.top().unwrap() == 11);
263272
heap.push(5);
264273
assert_eq!(heap.len(), 5);
265-
assert!(*heap.top() == 11);
274+
assert!(*heap.top().unwrap() == 11);
266275
heap.push(27);
267276
assert_eq!(heap.len(), 6);
268-
assert!(*heap.top() == 27);
277+
assert!(*heap.top().unwrap() == 27);
269278
heap.push(3);
270279
assert_eq!(heap.len(), 7);
271-
assert!(*heap.top() == 27);
280+
assert!(*heap.top().unwrap() == 27);
272281
heap.push(103);
273282
assert_eq!(heap.len(), 8);
274-
assert!(*heap.top() == 103);
283+
assert!(*heap.top().unwrap() == 103);
275284
}
276285

277286
#[test]
278287
fn test_push_unique() {
279288
let mut heap = PriorityQueue::from_vec(vec!(box 2, box 4, box 9));
280289
assert_eq!(heap.len(), 3);
281-
assert!(*heap.top() == box 9);
290+
assert!(*heap.top().unwrap() == box 9);
282291
heap.push(box 11);
283292
assert_eq!(heap.len(), 4);
284-
assert!(*heap.top() == box 11);
293+
assert!(*heap.top().unwrap() == box 11);
285294
heap.push(box 5);
286295
assert_eq!(heap.len(), 5);
287-
assert!(*heap.top() == box 11);
296+
assert!(*heap.top().unwrap() == box 11);
288297
heap.push(box 27);
289298
assert_eq!(heap.len(), 6);
290-
assert!(*heap.top() == box 27);
299+
assert!(*heap.top().unwrap() == box 27);
291300
heap.push(box 3);
292301
assert_eq!(heap.len(), 7);
293-
assert!(*heap.top() == box 27);
302+
assert!(*heap.top().unwrap() == box 27);
294303
heap.push(box 103);
295304
assert_eq!(heap.len(), 8);
296-
assert!(*heap.top() == box 103);
305+
assert!(*heap.top().unwrap() == box 103);
297306
}
298307

299308
#[test]
@@ -314,24 +323,24 @@ mod tests {
314323
fn test_replace() {
315324
let mut heap = PriorityQueue::from_vec(vec!(5, 5, 2, 1, 3));
316325
assert_eq!(heap.len(), 5);
317-
assert_eq!(heap.replace(6), 5);
326+
assert_eq!(heap.replace(6).unwrap(), 5);
318327
assert_eq!(heap.len(), 5);
319-
assert_eq!(heap.replace(0), 6);
328+
assert_eq!(heap.replace(0).unwrap(), 6);
320329
assert_eq!(heap.len(), 5);
321-
assert_eq!(heap.replace(4), 5);
330+
assert_eq!(heap.replace(4).unwrap(), 5);
322331
assert_eq!(heap.len(), 5);
323-
assert_eq!(heap.replace(1), 4);
332+
assert_eq!(heap.replace(1).unwrap(), 4);
324333
assert_eq!(heap.len(), 5);
325334
}
326335

327336
fn check_to_vec(mut data: Vec<int>) {
328337
let heap = PriorityQueue::from_vec(data.clone());
329-
let mut v = heap.clone().to_vec();
338+
let mut v = heap.clone().into_vec();
330339
v.sort();
331340
data.sort();
332341

333342
assert_eq!(v, data);
334-
assert_eq!(heap.to_sorted_vec(), data);
343+
assert_eq!(heap.into_sorted_vec(), data);
335344
}
336345

337346
#[test]
@@ -352,36 +361,21 @@ mod tests {
352361
}
353362

354363
#[test]
355-
#[should_fail]
356364
fn test_empty_pop() {
357365
let mut heap: PriorityQueue<int> = PriorityQueue::new();
358-
heap.pop();
366+
assert!(heap.pop().is_none());
359367
}
360368

361369
#[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]
369370
fn test_empty_top() {
370371
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());
378373
}
379374

380375
#[test]
381-
#[should_fail]
382376
fn test_empty_replace() {
383377
let mut heap: PriorityQueue<int> = PriorityQueue::new();
384-
heap.replace(5);
378+
heap.replace(5).is_none();
385379
}
386380

387381
#[test]
@@ -391,7 +385,7 @@ mod tests {
391385
let mut q: PriorityQueue<uint> = xs.as_slice().iter().rev().map(|&x| x).collect();
392386

393387
for &x in xs.iter() {
394-
assert_eq!(q.pop(), x);
388+
assert_eq!(q.pop().unwrap(), x);
395389
}
396390
}
397391
}

0 commit comments

Comments
 (0)