Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/double_priority_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,17 +472,25 @@ where
}

/// Change the priority of an Item using the provided function.
/// Return a boolean value where `true` means the item was in the queue and update was successful
///
/// The argument `item` is only used for lookup, and is not used to overwrite the item's data
/// in the priority queue.
///
/// The item is found in **O(1)** thanks to the hash table.
/// The operation is performed in **O(log(N))** time (worst case).
pub fn change_priority_by<Q: ?Sized, F>(&mut self, item: &Q, priority_setter: F)
pub fn change_priority_by<Q: ?Sized, F>(&mut self, item: &Q, priority_setter: F) -> bool
where
I: Borrow<Q>,
Q: Eq + Hash,
F: FnOnce(&mut P),
{
if let Some(pos) = self.store.change_priority_by(item, priority_setter) {
self.up_heapify(pos);
}
self.store
.change_priority_by(item, priority_setter)
.map(|pos| {
self.up_heapify(pos);
})
.is_some()
}

/// Get the priority of an item, or `None`, if the item is not in the queue
Expand Down
16 changes: 12 additions & 4 deletions src/priority_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,17 +406,25 @@ where
}

/// Change the priority of an Item using the provided function.
/// Return a boolean value where `true` means the item was in the queue and update was successful
///
/// The argument `item` is only used for lookup, and is not used to overwrite the item's data
/// in the priority queue.
///
/// The item is found in **O(1)** thanks to the hash table.
/// The operation is performed in **O(log(N))** time (worst case).
pub fn change_priority_by<Q: ?Sized, F>(&mut self, item: &Q, priority_setter: F)
pub fn change_priority_by<Q: ?Sized, F>(&mut self, item: &Q, priority_setter: F) -> bool
where
I: Borrow<Q>,
Q: Eq + Hash,
F: FnOnce(&mut P),
{
if let Some(pos) = self.store.change_priority_by(item, priority_setter) {
self.up_heapify(pos);
}
self.store
.change_priority_by(item, priority_setter)
.map(|pos| {
self.up_heapify(pos);
})
.is_some()
}

/// Get the priority of an item, or `None`, if the item is not in the queue
Expand Down
4 changes: 2 additions & 2 deletions tests/double_priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ mod doublepq_tests {
let v = vec![("a", 1), ("b", 2), ("f", 7), ("g", 6), ("h", 5)];
let mut pq: DoublePriorityQueue<_, _> = DoublePriorityQueue::from_iter(v.into_iter());

pq.change_priority_by("z", |z| *z += 8);
assert!(!pq.change_priority_by("z", |z| *z += 8));

pq.change_priority_by("b", |b| *b += 8);
assert!(pq.change_priority_by("b", |b| *b += 8));
assert_eq!(
pq.into_descending_sorted_vec().as_slice(),
&["b", "f", "g", "h", "a"]
Expand Down
2 changes: 1 addition & 1 deletion tests/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod pqueue_tests {
let v = vec![("a", 1), ("b", 2), ("f", 7), ("g", 6), ("h", 5)];
let mut pq: PriorityQueue<_, _> = PriorityQueue::from_iter(v.into_iter());

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

Expand Down