Skip to content
Open
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
19 changes: 13 additions & 6 deletions quickphf/src/set.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! An immutable set constructed at compile time with perfect hashing.
use core::hash::Hash;
use core::{borrow::Borrow, hash::Hash};

// TODO: Debug impls

Expand Down Expand Up @@ -86,7 +86,11 @@ impl<K: Eq + Hash> PhfSet<K> {
/// assert!(PRIME_DIGITS.contains(&2));
/// assert!(!PRIME_DIGITS.contains(&1));
/// ```
pub fn contains(&self, element: &K) -> bool {
pub fn contains<Q>(&self, element: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.get(element).is_some()
}

Expand All @@ -101,13 +105,17 @@ impl<K: Eq + Hash> PhfSet<K> {
/// assert_eq!(EVEN_DIGITS.get(&2), Some(&2));
/// assert_eq!(EVEN_DIGITS.get(&3), None);
/// ```
pub fn get(&self, element: &K) -> Option<&K> {
pub fn get<Q>(&self, element: &Q) -> Option<&K>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
if self.is_empty() {
return None;
}

let result = self.raw_map.get(element);
if result == element {
if result.borrow() == element {
Some(result)
} else {
None
Expand Down Expand Up @@ -392,9 +400,8 @@ impl<'a, K> core::iter::FusedIterator for Union<'a, K> where K: Eq + Hash {}

#[cfg(test)]
mod tests {
use crate::examples::EMPTY_SET;

use super::*;
use crate::examples::EMPTY_SET;

#[test]
fn test_empty() {
Expand Down