Skip to content

Commit 35cb455

Browse files
author
Gianmarco Garrisi
committed
Implement contains
1 parent f6b1535 commit 35cb455

5 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/double_priority_queue/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,17 @@ where
760760
self.store.get_priority(item)
761761
}
762762

763+
/// Check if the queue contains `item`.
764+
///
765+
/// Returns `true` if `item` is in the queue, `false` if it is not.
766+
pub fn contains<Q>(&self, item: &Q) -> bool
767+
where
768+
I: Borrow<Q>,
769+
Q: Eq + Hash + ?Sized,
770+
{
771+
self.store.contains(item)
772+
}
773+
763774
/// Get the couple (item, priority) of an arbitrary element, as reference
764775
/// or `None` if the item is not in the queue.
765776
pub fn get<Q>(&self, item: &Q) -> Option<(&I, &P)>

src/priority_queue/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,17 @@ where
635635
self.store.get_priority(item)
636636
}
637637

638+
/// Check if the queue contains `item`.
639+
///
640+
/// Returns `true` if `item` is in the queue, `false` if it is not.
641+
pub fn contains<Q>(&self, item: &Q) -> bool
642+
where
643+
I: Borrow<Q>,
644+
Q: Eq + Hash + ?Sized,
645+
{
646+
self.store.contains(item)
647+
}
648+
638649
/// Get the couple `(item, priority)` of an arbitrary element, as reference
639650
/// or `None` if the item is not in the queue.
640651
pub fn get<Q>(&self, item: &Q) -> Option<(&I, &P)>

src/store.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,17 @@ where
437437
self.map.get(item)
438438
}
439439

440+
/// Check if the store contains `item`.
441+
///
442+
/// Returns `true` if `item` is in the store, `false` if it is not.
443+
pub fn contains<Q>(&self, item: &Q) -> bool
444+
where
445+
I: Borrow<Q>,
446+
Q: Eq + Hash + ?Sized,
447+
{
448+
self.map.contains_key(item)
449+
}
450+
440451
/// Get the couple (item, priority) of an arbitrary element, as reference
441452
/// or `None` if the item is not in the queue.
442453
pub fn get<Q>(&self, item: &Q) -> Option<(&I, &P)>

tests/double_priority_queue.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ mod doublepq_tests {
6666
assert_eq!(pq.len(), 3);
6767
}
6868

69+
#[test]
70+
fn contains() {
71+
let mut pq = DoublePriorityQueue::new();
72+
assert!(!pq.contains("a"));
73+
pq.push("a", 1);
74+
pq.push("b", 2);
75+
pq.push("f", 7);
76+
pq.push("g", 5);
77+
pq.push("h", 3);
78+
79+
assert!(pq.contains("f"));
80+
81+
pq.pop_max();
82+
83+
assert!(!pq.contains("f"));
84+
}
85+
6986
#[test]
7087
fn pop_if() {
7188
let mut pq = DoublePriorityQueue::new();

tests/priority_queue.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@ mod pqueue_tests {
101101
assert_eq!(pq.len(), 3);
102102
}
103103

104+
#[test]
105+
fn contains() {
106+
let mut pq = PriorityQueue::new();
107+
assert!(!pq.contains("a"));
108+
pq.push("a", 1);
109+
pq.push("b", 2);
110+
pq.push("f", 7);
111+
pq.push("g", 5);
112+
pq.push("h", 3);
113+
114+
assert!(pq.contains("f"));
115+
116+
pq.pop();
117+
118+
assert!(!pq.contains("f"));
119+
}
120+
104121
#[test]
105122
fn peek_get_mut() {
106123
use std::hash::{Hash, Hasher};

0 commit comments

Comments
 (0)