Skip to content

Commit 42be687

Browse files
committed
auto merge of #14279 : aochagavia/rust/pr2, r=huonw
The breaking changes are: * Changed `DList::insert_ordered` to use `TotalOrd`, not `Ord` * Changed `PriorityQueue` to use `TotalOrd`, not `Ord` * Deprecated `PriorityQueue::maybe_top()` (renamed to replace `PriorityQueue::top()`) * Deprecated `PriorityQueue::maybe_pop()` (renamed to replace `PriorityQueue::pop()`) * Deprecated `PriorityQueue::to_vec()` (renamed to `PriorityQueue::into_vec()`) * Deprecated `PriorityQueue::to_sorted_vec()` (renamed to `PriorityQueue::into_sorted_vec()`) * Changed `PriorityQueue::replace(...)` to return an `Option<T>` instead of failing when the queue is empty. [breaking-change]
2 parents ed15677 + 3a1b7d4 commit 42be687

File tree

2 files changed

+73
-71
lines changed

2 files changed

+73
-71
lines changed

src/libcollections/dlist.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<T> DList<T> {
410410
}
411411
}
412412

413-
impl<T: Ord> DList<T> {
413+
impl<T: TotalOrd> DList<T> {
414414
/// Insert `elt` sorted in ascending order
415415
///
416416
/// O(N)

src/libcollections/priority_queue.rs

+72-70
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,31 @@ pub struct PriorityQueue<T> {
2222
data: Vec<T>,
2323
}
2424

25-
impl<T:Ord> Container for PriorityQueue<T> {
25+
impl<T: TotalOrd> Container for PriorityQueue<T> {
2626
/// Returns the length of the queue
2727
fn len(&self) -> uint { self.data.len() }
2828
}
2929

30-
impl<T:Ord> Mutable for PriorityQueue<T> {
30+
impl<T: TotalOrd> Mutable for PriorityQueue<T> {
3131
/// Drop all items from the queue
3232
fn clear(&mut self) { self.data.truncate(0) }
3333
}
3434

35-
impl<T:Ord> PriorityQueue<T> {
35+
impl<T: TotalOrd> PriorityQueue<T> {
3636
/// An iterator visiting all values in underlying vector, in
3737
/// arbitrary order.
3838
pub fn iter<'a>(&'a self) -> Items<'a, 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:Ord> 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,34 +87,48 @@ impl<T:Ord> 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

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+
101118
/// 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 }
103120

104121
/// Consume the PriorityQueue and return a vector in sorted
105122
/// (ascending) order
106-
pub fn to_sorted_vec(self) -> Vec<T> {
123+
pub fn into_sorted_vec(self) -> Vec<T> {
107124
let mut q = self;
108125
let mut end = q.len();
109126
while end > 1 {
110127
end -= 1;
111128
q.data.as_mut_slice().swap(0, end);
112129
q.siftdown_range(0, end)
113130
}
114-
q.to_vec()
131+
q.into_vec()
115132
}
116133

117134
/// Create an empty PriorityQueue
@@ -197,15 +214,15 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> {
197214
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
198215
}
199216

200-
impl<T: Ord> FromIterator<T> for PriorityQueue<T> {
217+
impl<T: TotalOrd> FromIterator<T> for PriorityQueue<T> {
201218
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> PriorityQueue<T> {
202219
let mut q = PriorityQueue::new();
203220
q.extend(iter);
204221
q
205222
}
206223
}
207224

208-
impl<T: Ord> Extendable<T> for PriorityQueue<T> {
225+
impl<T: TotalOrd> Extendable<T> for PriorityQueue<T> {
209226
fn extend<Iter: Iterator<T>>(&mut self, mut iter: Iter) {
210227
let (lower, _) = iter.size_hint();
211228

@@ -241,53 +258,53 @@ mod tests {
241258
sorted.sort();
242259
let mut heap = PriorityQueue::from_vec(data);
243260
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());
246263
}
247264
}
248265

249266
#[test]
250267
fn test_push() {
251268
let mut heap = PriorityQueue::from_vec(vec!(2, 4, 9));
252269
assert_eq!(heap.len(), 3);
253-
assert!(*heap.top() == 9);
270+
assert!(*heap.top().unwrap() == 9);
254271
heap.push(11);
255272
assert_eq!(heap.len(), 4);
256-
assert!(*heap.top() == 11);
273+
assert!(*heap.top().unwrap() == 11);
257274
heap.push(5);
258275
assert_eq!(heap.len(), 5);
259-
assert!(*heap.top() == 11);
276+
assert!(*heap.top().unwrap() == 11);
260277
heap.push(27);
261278
assert_eq!(heap.len(), 6);
262-
assert!(*heap.top() == 27);
279+
assert!(*heap.top().unwrap() == 27);
263280
heap.push(3);
264281
assert_eq!(heap.len(), 7);
265-
assert!(*heap.top() == 27);
282+
assert!(*heap.top().unwrap() == 27);
266283
heap.push(103);
267284
assert_eq!(heap.len(), 8);
268-
assert!(*heap.top() == 103);
285+
assert!(*heap.top().unwrap() == 103);
269286
}
270287

271288
#[test]
272289
fn test_push_unique() {
273290
let mut heap = PriorityQueue::from_vec(vec!(box 2, box 4, box 9));
274291
assert_eq!(heap.len(), 3);
275-
assert!(*heap.top() == box 9);
292+
assert!(*heap.top().unwrap() == box 9);
276293
heap.push(box 11);
277294
assert_eq!(heap.len(), 4);
278-
assert!(*heap.top() == box 11);
295+
assert!(*heap.top().unwrap() == box 11);
279296
heap.push(box 5);
280297
assert_eq!(heap.len(), 5);
281-
assert!(*heap.top() == box 11);
298+
assert!(*heap.top().unwrap() == box 11);
282299
heap.push(box 27);
283300
assert_eq!(heap.len(), 6);
284-
assert!(*heap.top() == box 27);
301+
assert!(*heap.top().unwrap() == box 27);
285302
heap.push(box 3);
286303
assert_eq!(heap.len(), 7);
287-
assert!(*heap.top() == box 27);
304+
assert!(*heap.top().unwrap() == box 27);
288305
heap.push(box 103);
289306
assert_eq!(heap.len(), 8);
290-
assert!(*heap.top() == box 103);
307+
assert!(*heap.top().unwrap() == box 103);
291308
}
292309

293310
#[test]
@@ -308,24 +325,24 @@ mod tests {
308325
fn test_replace() {
309326
let mut heap = PriorityQueue::from_vec(vec!(5, 5, 2, 1, 3));
310327
assert_eq!(heap.len(), 5);
311-
assert_eq!(heap.replace(6), 5);
328+
assert_eq!(heap.replace(6).unwrap(), 5);
312329
assert_eq!(heap.len(), 5);
313-
assert_eq!(heap.replace(0), 6);
330+
assert_eq!(heap.replace(0).unwrap(), 6);
314331
assert_eq!(heap.len(), 5);
315-
assert_eq!(heap.replace(4), 5);
332+
assert_eq!(heap.replace(4).unwrap(), 5);
316333
assert_eq!(heap.len(), 5);
317-
assert_eq!(heap.replace(1), 4);
334+
assert_eq!(heap.replace(1).unwrap(), 4);
318335
assert_eq!(heap.len(), 5);
319336
}
320337

321338
fn check_to_vec(mut data: Vec<int>) {
322339
let heap = PriorityQueue::from_vec(data.clone());
323-
let mut v = heap.clone().to_vec();
340+
let mut v = heap.clone().into_vec();
324341
v.sort();
325342
data.sort();
326343

327344
assert_eq!(v, data);
328-
assert_eq!(heap.to_sorted_vec(), data);
345+
assert_eq!(heap.into_sorted_vec(), data);
329346
}
330347

331348
#[test]
@@ -346,36 +363,21 @@ mod tests {
346363
}
347364

348365
#[test]
349-
#[should_fail]
350366
fn test_empty_pop() {
351367
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());
359369
}
360370

361371
#[test]
362-
#[should_fail]
363372
fn test_empty_top() {
364373
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());
372375
}
373376

374377
#[test]
375-
#[should_fail]
376378
fn test_empty_replace() {
377379
let mut heap: PriorityQueue<int> = PriorityQueue::new();
378-
heap.replace(5);
380+
heap.replace(5).is_none();
379381
}
380382

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

387389
for &x in xs.iter() {
388-
assert_eq!(q.pop(), x);
390+
assert_eq!(q.pop().unwrap(), x);
389391
}
390392
}
391393
}

0 commit comments

Comments
 (0)