diff --git a/src/double_priority_queue/mod.rs b/src/double_priority_queue/mod.rs index 6e9b228..f92b155 100644 --- a/src/double_priority_queue/mod.rs +++ b/src/double_priority_queue/mod.rs @@ -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(&mut self, item: &Q, priority_setter: F) + pub fn change_priority_by(&mut self, item: &Q, priority_setter: F) -> bool where I: Borrow, 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 diff --git a/src/priority_queue/mod.rs b/src/priority_queue/mod.rs index 2dc4e25..20b4950 100644 --- a/src/priority_queue/mod.rs +++ b/src/priority_queue/mod.rs @@ -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(&mut self, item: &Q, priority_setter: F) + pub fn change_priority_by(&mut self, item: &Q, priority_setter: F) -> bool where I: Borrow, 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 diff --git a/tests/double_priority_queue.rs b/tests/double_priority_queue.rs index db66eb4..77a84fc 100644 --- a/tests/double_priority_queue.rs +++ b/tests/double_priority_queue.rs @@ -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"] diff --git a/tests/priority_queue.rs b/tests/priority_queue.rs index d5e1fe8..73cceba 100644 --- a/tests/priority_queue.rs +++ b/tests/priority_queue.rs @@ -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"]); }