Skip to content

Commit

Permalink
Fix #6 for change_priority and change_priority_by. Publishing new ver…
Browse files Browse the repository at this point in the history
…sion of crate
  • Loading branch information
Gianmarco Garrisi committed Jan 13, 2018
1 parent 96c91be commit 053b729
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "priority-queue"
version = "0.4.4"
version = "0.4.5"
authors = ["Gianmarco Garrisi <gianmarcogarrisi@tutanota.com>"]
description = "A Priority Queue implemented as a heap with a function to efficiently change the priority of an item."
repository = "https://github.com/garro95/priority-queue"
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Feel free to contribute to this project with pull requests and/or issues. All co
Changes
-------

* 0.4.5 Fix #6 for `change_priority` and `change_priority_by`
* 0.4.4 Fix #6
* 0.4.3 Fix #4 changing the way PriorityQueue serializes.
Note that old serialized PriorityQueues may be incompatible with the new version.
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ mod tests {
let mut pq = PriorityQueue::new();
pq.push("Processor", 1);
pq.push("Mainboard", 2);
pq.push("Ram", 5);
pq.push("RAM", 5);
pq.push("GPU", 4);
pq.push("Disk", 3);
pq.change_priority("Processor", 10);
assert_eq!(pq.peek(), Some((&"Processor", &10)));

pq.change_priority("Ram", 11);
assert_eq!(pq.peek(), Some((&"Ram", &11)));
pq.change_priority("RAM", 11);
assert_eq!(pq.peek(), Some((&"RAM", &11)));
}

#[test]
Expand Down Expand Up @@ -166,11 +168,11 @@ mod tests {
fn change_priority_by() {
use std::iter::FromIterator;

let v = vec!(("a", 1), ("b", 2), ("f", 7));
let v = vec!(("a", 1), ("b", 2), ("f", 7), ("g", 6), ("h", 5));
let mut pq = PriorityQueue::from_iter(v.into_iter());

pq.change_priority_by("b", |b| b+8);
assert_eq!(pq.into_sorted_vec().as_slice(), &["b", "f", "a"]);
assert_eq!(pq.into_sorted_vec().as_slice(), &["b", "f", "g", "h", "a"]);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/pqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<I, P> PriorityQueue<I, P>
let tmp = *self.heap.get_unchecked(pos);
while (pos > 0) &&
(self.map.get_index(*self.heap.get_unchecked(parent(pos))).unwrap().1 <
self.map.get_index(*self.heap.get_unchecked(pos)).unwrap().1)
self.map.get_index(tmp).unwrap().1)
{
*self.heap.get_unchecked_mut(pos) = *self.heap.get_unchecked(parent(pos));
*self.qp.get_unchecked_mut(*self.heap.get_unchecked(pos)) = pos;
Expand Down Expand Up @@ -285,7 +285,7 @@ impl<I, P> PriorityQueue<I, P>
let tmp = *self.heap.get_unchecked(pos);
while (pos > 0) &&
(self.map.get_index(*self.heap.get_unchecked(parent(pos))).unwrap().1 <
self.map.get_index(*self.heap.get_unchecked(pos)).unwrap().1)
self.map.get_index(tmp).unwrap().1)
{
*self.heap.get_unchecked_mut(pos) = *self.heap.get_unchecked(parent(pos));
*self.qp.get_unchecked_mut(*self.heap.get_unchecked(pos)) = pos;
Expand Down

0 comments on commit 053b729

Please sign in to comment.