From 8e9567dad8289dab79c61438da9229409e07b735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 18 May 2014 14:37:15 -0700 Subject: [PATCH 1/5] Replaced Ord by TotalOrd in priority queue --- src/libcollections/priority_queue.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libcollections/priority_queue.rs b/src/libcollections/priority_queue.rs index f9f4945efc789..43c4d681322e6 100644 --- a/src/libcollections/priority_queue.rs +++ b/src/libcollections/priority_queue.rs @@ -22,17 +22,17 @@ pub struct PriorityQueue { data: Vec, } -impl Container for PriorityQueue { +impl Container for PriorityQueue { /// Returns the length of the queue fn len(&self) -> uint { self.data.len() } } -impl Mutable for PriorityQueue { +impl Mutable for PriorityQueue { /// Drop all items from the queue fn clear(&mut self) { self.data.truncate(0) } } -impl PriorityQueue { +impl PriorityQueue { /// An iterator visiting all values in underlying vector, in /// arbitrary order. pub fn iter<'a>(&'a self) -> Items<'a, T> { @@ -197,7 +197,7 @@ impl<'a, T> Iterator<&'a T> for Items<'a, T> { fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } -impl FromIterator for PriorityQueue { +impl FromIterator for PriorityQueue { fn from_iter>(iter: Iter) -> PriorityQueue { let mut q = PriorityQueue::new(); q.extend(iter); @@ -205,7 +205,7 @@ impl FromIterator for PriorityQueue { } } -impl Extendable for PriorityQueue { +impl Extendable for PriorityQueue { fn extend>(&mut self, mut iter: Iter) { let (lower, _) = iter.size_hint(); From 2b06105c2a9f188df1e24c10c3d28c8f869cd558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 18 May 2014 15:04:43 -0700 Subject: [PATCH 2/5] Rename to_vec and to_sorted_vec --- src/libcollections/priority_queue.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libcollections/priority_queue.rs b/src/libcollections/priority_queue.rs index 43c4d681322e6..56878ebd6edab 100644 --- a/src/libcollections/priority_queue.rs +++ b/src/libcollections/priority_queue.rs @@ -98,12 +98,18 @@ impl PriorityQueue { item } + #[deprecated="renamed to `into_vec`"] + fn to_vec(self) -> Vec { self.into_vec() } + + #[deprecated="renamed to `into_sorted_vec`"] + fn to_sorted_vec(self) -> Vec { self.into_sorted_vec() } + /// Consume the PriorityQueue and return the underlying vector - pub fn to_vec(self) -> Vec { let PriorityQueue{data: v} = self; v } + pub fn into_vec(self) -> Vec { let PriorityQueue{data: v} = self; v } /// Consume the PriorityQueue and return a vector in sorted /// (ascending) order - pub fn to_sorted_vec(self) -> Vec { + pub fn into_sorted_vec(self) -> Vec { let mut q = self; let mut end = q.len(); while end > 1 { From 29806052cec82925c09bc77f39730925a5e4c732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 18 May 2014 15:54:19 -0700 Subject: [PATCH 3/5] Refactored and renamed functions to avoid failure --- src/libcollections/priority_queue.rs | 120 +++++++++++++-------------- 1 file changed, 57 insertions(+), 63 deletions(-) diff --git a/src/libcollections/priority_queue.rs b/src/libcollections/priority_queue.rs index 56878ebd6edab..898b011b23e60 100644 --- a/src/libcollections/priority_queue.rs +++ b/src/libcollections/priority_queue.rs @@ -39,14 +39,14 @@ impl PriorityQueue { Items { iter: self.data.iter() } } - /// Returns the greatest item in the queue - fails if empty - pub fn top<'a>(&'a self) -> &'a T { self.data.get(0) } - - /// Returns the greatest item in the queue - None if empty - pub fn maybe_top<'a>(&'a self) -> Option<&'a T> { - if self.is_empty() { None } else { Some(self.top()) } + /// Returns the greatest item in a queue or None if it is empty + pub fn top<'a>(&'a self) -> Option<&'a T> { + if self.is_empty() { None } else { Some(self.data.get(0)) } } + #[deprecated="renamed to `top`"] + pub fn maybe_top<'a>(&'a self) -> Option<&'a T> { self.top() } + /// Returns the number of elements the queue can hold without reallocating pub fn capacity(&self) -> uint { self.data.capacity() } @@ -60,20 +60,23 @@ impl PriorityQueue { self.data.reserve(n) } - /// Pop the greatest item from the queue - fails if empty - pub fn pop(&mut self) -> T { - let mut item = self.data.pop().unwrap(); - if !self.is_empty() { - swap(&mut item, self.data.get_mut(0)); - self.siftdown(0); + /// Remove the greatest item from a queue and return it, or `None` if it is + /// empty. + pub fn pop(&mut self) -> Option { + match self.data.pop() { + None => { None } + Some(mut item) => { + if !self.is_empty() { + swap(&mut item, self.data.get_mut(0)); + self.siftdown(0); + } + Some(item) + } } - item } - /// Pop the greatest item from the queue - None if empty - pub fn maybe_pop(&mut self) -> Option { - if self.is_empty() { None } else { Some(self.pop()) } - } + #[deprecated="renamed to `pop`"] + pub fn maybe_pop(&mut self) -> Option { self.pop() } /// Push an item onto the queue pub fn push(&mut self, item: T) { @@ -84,18 +87,24 @@ impl PriorityQueue { /// Optimized version of a push followed by a pop pub fn push_pop(&mut self, mut item: T) -> T { - if !self.is_empty() && *self.top() > item { + if !self.is_empty() && *self.top().unwrap() > item { swap(&mut item, self.data.get_mut(0)); self.siftdown(0); } item } - /// Optimized version of a pop followed by a push - fails if empty - pub fn replace(&mut self, mut item: T) -> T { - swap(&mut item, self.data.get_mut(0)); - self.siftdown(0); - item + /// Optimized version of a pop followed by a push. The push is done + /// regardless of whether the queue is empty. + pub fn replace(&mut self, mut item: T) -> Option { + if !self.is_empty() { + swap(&mut item, self.data.get_mut(0)); + self.siftdown(0); + Some(item) + } else { + self.push(item); + None + } } #[deprecated="renamed to `into_vec`"] @@ -117,7 +126,7 @@ impl PriorityQueue { q.data.as_mut_slice().swap(0, end); q.siftdown_range(0, end) } - q.to_vec() + q.into_vec() } /// Create an empty PriorityQueue @@ -247,8 +256,8 @@ mod tests { sorted.sort(); let mut heap = PriorityQueue::from_vec(data); while !heap.is_empty() { - assert_eq!(heap.top(), sorted.last().unwrap()); - assert_eq!(heap.pop(), sorted.pop().unwrap()); + assert_eq!(heap.top().unwrap(), sorted.last().unwrap()); + assert_eq!(heap.pop().unwrap(), sorted.pop().unwrap()); } } @@ -256,44 +265,44 @@ mod tests { fn test_push() { let mut heap = PriorityQueue::from_vec(vec!(2, 4, 9)); assert_eq!(heap.len(), 3); - assert!(*heap.top() == 9); + assert!(*heap.top().unwrap() == 9); heap.push(11); assert_eq!(heap.len(), 4); - assert!(*heap.top() == 11); + assert!(*heap.top().unwrap() == 11); heap.push(5); assert_eq!(heap.len(), 5); - assert!(*heap.top() == 11); + assert!(*heap.top().unwrap() == 11); heap.push(27); assert_eq!(heap.len(), 6); - assert!(*heap.top() == 27); + assert!(*heap.top().unwrap() == 27); heap.push(3); assert_eq!(heap.len(), 7); - assert!(*heap.top() == 27); + assert!(*heap.top().unwrap() == 27); heap.push(103); assert_eq!(heap.len(), 8); - assert!(*heap.top() == 103); + assert!(*heap.top().unwrap() == 103); } #[test] fn test_push_unique() { let mut heap = PriorityQueue::from_vec(vec!(box 2, box 4, box 9)); assert_eq!(heap.len(), 3); - assert!(*heap.top() == box 9); + assert!(*heap.top().unwrap() == box 9); heap.push(box 11); assert_eq!(heap.len(), 4); - assert!(*heap.top() == box 11); + assert!(*heap.top().unwrap() == box 11); heap.push(box 5); assert_eq!(heap.len(), 5); - assert!(*heap.top() == box 11); + assert!(*heap.top().unwrap() == box 11); heap.push(box 27); assert_eq!(heap.len(), 6); - assert!(*heap.top() == box 27); + assert!(*heap.top().unwrap() == box 27); heap.push(box 3); assert_eq!(heap.len(), 7); - assert!(*heap.top() == box 27); + assert!(*heap.top().unwrap() == box 27); heap.push(box 103); assert_eq!(heap.len(), 8); - assert!(*heap.top() == box 103); + assert!(*heap.top().unwrap() == box 103); } #[test] @@ -314,24 +323,24 @@ mod tests { fn test_replace() { let mut heap = PriorityQueue::from_vec(vec!(5, 5, 2, 1, 3)); assert_eq!(heap.len(), 5); - assert_eq!(heap.replace(6), 5); + assert_eq!(heap.replace(6).unwrap(), 5); assert_eq!(heap.len(), 5); - assert_eq!(heap.replace(0), 6); + assert_eq!(heap.replace(0).unwrap(), 6); assert_eq!(heap.len(), 5); - assert_eq!(heap.replace(4), 5); + assert_eq!(heap.replace(4).unwrap(), 5); assert_eq!(heap.len(), 5); - assert_eq!(heap.replace(1), 4); + assert_eq!(heap.replace(1).unwrap(), 4); assert_eq!(heap.len(), 5); } fn check_to_vec(mut data: Vec) { let heap = PriorityQueue::from_vec(data.clone()); - let mut v = heap.clone().to_vec(); + let mut v = heap.clone().into_vec(); v.sort(); data.sort(); assert_eq!(v, data); - assert_eq!(heap.to_sorted_vec(), data); + assert_eq!(heap.into_sorted_vec(), data); } #[test] @@ -352,36 +361,21 @@ mod tests { } #[test] - #[should_fail] fn test_empty_pop() { let mut heap: PriorityQueue = PriorityQueue::new(); - heap.pop(); + assert!(heap.pop().is_none()); } #[test] - fn test_empty_maybe_pop() { - let mut heap: PriorityQueue = PriorityQueue::new(); - assert!(heap.maybe_pop().is_none()); - } - - #[test] - #[should_fail] fn test_empty_top() { let empty: PriorityQueue = PriorityQueue::new(); - empty.top(); - } - - #[test] - fn test_empty_maybe_top() { - let empty: PriorityQueue = PriorityQueue::new(); - assert!(empty.maybe_top().is_none()); + assert!(empty.top().is_none()); } #[test] - #[should_fail] fn test_empty_replace() { let mut heap: PriorityQueue = PriorityQueue::new(); - heap.replace(5); + heap.replace(5).is_none(); } #[test] @@ -391,7 +385,7 @@ mod tests { let mut q: PriorityQueue = xs.as_slice().iter().rev().map(|&x| x).collect(); for &x in xs.iter() { - assert_eq!(q.pop(), x); + assert_eq!(q.pop().unwrap(), x); } } } From 75d3690df938f4c4bf2bff44af7c142090082961 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 18 May 2014 16:33:30 -0700 Subject: [PATCH 4/5] Fix dead code warnings --- src/libcollections/priority_queue.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcollections/priority_queue.rs b/src/libcollections/priority_queue.rs index 898b011b23e60..2e8178cd931e5 100644 --- a/src/libcollections/priority_queue.rs +++ b/src/libcollections/priority_queue.rs @@ -107,9 +107,11 @@ impl PriorityQueue { } } + #[allow(dead_code)] #[deprecated="renamed to `into_vec`"] fn to_vec(self) -> Vec { self.into_vec() } + #[allow(dead_code)] #[deprecated="renamed to `into_sorted_vec`"] fn to_sorted_vec(self) -> Vec { self.into_sorted_vec() } From 3a1b7d47f33ca8004820548a99e562d3f7d60921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20Ochagav=C3=ADa?= Date: Sun, 18 May 2014 16:34:01 -0700 Subject: [PATCH 5/5] Fix insert_ordered in DList --- src/libcollections/dlist.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index 3a797cc935461..58ced1beeed60 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -410,7 +410,7 @@ impl DList { } } -impl DList { +impl DList { /// Insert `elt` sorted in ascending order /// /// O(N)