Skip to content

Commit 7bd0d71

Browse files
thestingerbrson
authored andcommitted
priority_queue: avoid copy with top and maybe_top
1 parent e00c3b0 commit 7bd0d71

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/libstd/priority_queue.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ pub struct PriorityQueue <T: Copy Ord>{
88

99
impl <T: Copy Ord> PriorityQueue<T> {
1010
/// Returns the greatest item in the queue - fails if empty
11-
pure fn top(&self) -> T { self.data[0] }
11+
pure fn top(&self) -> &self/T { &self.data[0] }
1212

1313
/// Returns the greatest item in the queue - None if empty
14-
pure fn maybe_top(&self) -> Option<T> {
14+
pure fn maybe_top(&self) -> Option<&self/T> {
1515
if self.is_empty() { None } else { Some(self.top()) }
1616
}
1717

@@ -151,7 +151,7 @@ mod tests {
151151
let mut sorted = merge_sort(data, le);
152152
let mut heap = from_vec(data);
153153
while heap.is_not_empty() {
154-
assert heap.top() == sorted.last();
154+
assert *heap.top() == sorted.last();
155155
assert heap.pop() == sorted.pop();
156156
}
157157
}
@@ -160,22 +160,22 @@ mod tests {
160160
fn test_push() {
161161
let mut heap = from_vec(~[2, 4, 9]);
162162
assert heap.len() == 3;
163-
assert heap.top() == 9;
163+
assert *heap.top() == 9;
164164
heap.push(11);
165165
assert heap.len() == 4;
166-
assert heap.top() == 11;
166+
assert *heap.top() == 11;
167167
heap.push(5);
168168
assert heap.len() == 5;
169-
assert heap.top() == 11;
169+
assert *heap.top() == 11;
170170
heap.push(27);
171171
assert heap.len() == 6;
172-
assert heap.top() == 27;
172+
assert *heap.top() == 27;
173173
heap.push(3);
174174
assert heap.len() == 7;
175-
assert heap.top() == 27;
175+
assert *heap.top() == 27;
176176
heap.push(103);
177177
assert heap.len() == 8;
178-
assert heap.top() == 103;
178+
assert *heap.top() == 103;
179179
}
180180

181181
#[test]
@@ -241,11 +241,12 @@ mod tests {
241241

242242
#[test]
243243
#[should_fail]
244-
fn test_empty_top() { from_vec::<int>(~[]).top(); }
244+
fn test_empty_top() { let empty = from_vec::<int>(~[]); empty.top(); }
245245

246246
#[test]
247247
fn test_empty_maybe_top() {
248-
assert from_vec::<int>(~[]).maybe_top().is_none();
248+
let empty = from_vec::<int>(~[]);
249+
assert empty.maybe_top().is_none();
249250
}
250251

251252
#[test]

0 commit comments

Comments
 (0)