diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2012-12-11 12:19:20 -0500 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-12-16 19:27:06 -0800 |
| commit | 7bd0d7155495486b35c67772eb96c930af2cf7d0 (patch) | |
| tree | 3c77aeba9bc9955e87eb3ab5687763c09567558f /src/libstd/priority_queue.rs | |
| parent | e00c3b05e110ab4b3cf6d60e29c59fad2b5921d6 (diff) | |
| download | rust-7bd0d7155495486b35c67772eb96c930af2cf7d0.tar.gz rust-7bd0d7155495486b35c67772eb96c930af2cf7d0.zip | |
priority_queue: avoid copy with top and maybe_top
Diffstat (limited to 'src/libstd/priority_queue.rs')
| -rw-r--r-- | src/libstd/priority_queue.rs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/src/libstd/priority_queue.rs b/src/libstd/priority_queue.rs index 35d238b4d7c..d73722dc55b 100644 --- a/src/libstd/priority_queue.rs +++ b/src/libstd/priority_queue.rs @@ -8,10 +8,10 @@ pub struct PriorityQueue <T: Copy Ord>{ impl <T: Copy Ord> PriorityQueue<T> { /// Returns the greatest item in the queue - fails if empty - pure fn top(&self) -> T { self.data[0] } + pure fn top(&self) -> &self/T { &self.data[0] } /// Returns the greatest item in the queue - None if empty - pure fn maybe_top(&self) -> Option<T> { + pure fn maybe_top(&self) -> Option<&self/T> { if self.is_empty() { None } else { Some(self.top()) } } @@ -151,7 +151,7 @@ mod tests { let mut sorted = merge_sort(data, le); let mut heap = from_vec(data); while heap.is_not_empty() { - assert heap.top() == sorted.last(); + assert *heap.top() == sorted.last(); assert heap.pop() == sorted.pop(); } } @@ -160,22 +160,22 @@ mod tests { fn test_push() { let mut heap = from_vec(~[2, 4, 9]); assert heap.len() == 3; - assert heap.top() == 9; + assert *heap.top() == 9; heap.push(11); assert heap.len() == 4; - assert heap.top() == 11; + assert *heap.top() == 11; heap.push(5); assert heap.len() == 5; - assert heap.top() == 11; + assert *heap.top() == 11; heap.push(27); assert heap.len() == 6; - assert heap.top() == 27; + assert *heap.top() == 27; heap.push(3); assert heap.len() == 7; - assert heap.top() == 27; + assert *heap.top() == 27; heap.push(103); assert heap.len() == 8; - assert heap.top() == 103; + assert *heap.top() == 103; } #[test] @@ -241,11 +241,12 @@ mod tests { #[test] #[should_fail] - fn test_empty_top() { from_vec::<int>(~[]).top(); } + fn test_empty_top() { let empty = from_vec::<int>(~[]); empty.top(); } #[test] fn test_empty_maybe_top() { - assert from_vec::<int>(~[]).maybe_top().is_none(); + let empty = from_vec::<int>(~[]); + assert empty.maybe_top().is_none(); } #[test] |
