Skip to content

Commit

Permalink
Replace std::borrow::Borrow with PhfBorrow for ordered maps and sets
Browse files Browse the repository at this point in the history
  • Loading branch information
skyfloogle committed Jun 9, 2021
1 parent 3c087d4 commit f43a9cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
17 changes: 8 additions & 9 deletions phf/src/ordered_map.rs
@@ -1,10 +1,9 @@
//! An order-preserving immutable map constructed at compile time.
use core::borrow::Borrow;
use core::iter::IntoIterator;
use core::ops::Index;
use core::fmt;
use core::slice;
use phf_shared::{self, PhfHash, HashKey};
use phf_shared::{self, PhfHash, PhfBorrow, HashKey};

use crate::Slice;

Expand Down Expand Up @@ -35,7 +34,7 @@ impl<K, V> fmt::Debug for OrderedMap<K, V> where K: fmt::Debug, V: fmt::Debug {
}
}

impl<'a, K, V, T: ?Sized> Index<&'a T> for OrderedMap<K, V> where T: Eq + PhfHash, K: Borrow<T> {
impl<'a, K, V, T: ?Sized> Index<&'a T> for OrderedMap<K, V> where T: Eq + PhfHash, K: PhfBorrow<T> {
type Output = V;

fn index(&self, k: &'a T) -> &V {
Expand All @@ -57,7 +56,7 @@ impl<K, V> OrderedMap<K, V> {
/// Returns a reference to the value that `key` maps to.
pub fn get<T: ?Sized>(&self, key: &T) -> Option<&V>
where T: Eq + PhfHash,
K: Borrow<T>
K: PhfBorrow<T>
{
self.get_entry(key).map(|e| e.1)
}
Expand All @@ -68,15 +67,15 @@ impl<K, V> OrderedMap<K, V> {
/// This can be useful for interning schemes.
pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K>
where T: Eq + PhfHash,
K: Borrow<T>
K: PhfBorrow<T>
{
self.get_entry(key).map(|e| e.0)
}

/// Determines if `key` is in the `Map`.
pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
where T: Eq + PhfHash,
K: Borrow<T>
K: PhfBorrow<T>
{
self.get(key).is_some()
}
Expand All @@ -85,7 +84,7 @@ impl<K, V> OrderedMap<K, V> {
/// the ordered map.
pub fn get_index<T: ?Sized>(&self, key: &T) -> Option<usize>
where T: Eq + PhfHash,
K: Borrow<T>
K: PhfBorrow<T>
{
self.get_internal(key).map(|(i, _)| i)
}
Expand All @@ -99,14 +98,14 @@ impl<K, V> OrderedMap<K, V> {
/// Like `get`, but returns both the key and the value.
pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)>
where T: Eq + PhfHash,
K: Borrow<T>
K: PhfBorrow<T>
{
self.get_internal(key).map(|(_, e)| e)
}

fn get_internal<T: ?Sized>(&self, key: &T) -> Option<(usize, (&K, &V))>
where T: Eq + PhfHash,
K: Borrow<T>
K: PhfBorrow<T>
{
if self.disps.len() == 0 { return None; } //Prevent panic on empty map
let hashes = phf_shared::hash(key, &self.key);
Expand Down
10 changes: 5 additions & 5 deletions phf/src/ordered_set.rs
@@ -1,7 +1,7 @@
//! An order-preserving immutable set constructed at compile time.
use core::borrow::Borrow;
use core::iter::IntoIterator;
use core::fmt;
use phf_shared::PhfBorrow;
use crate::{ordered_map, PhfHash, OrderedMap};

/// An order-preserving immutable set constructed at compile time.
Expand Down Expand Up @@ -42,7 +42,7 @@ impl<T> OrderedSet<T> {
/// This can be useful for interning schemes.
pub fn get_key<U: ?Sized>(&self, key: &U) -> Option<&T>
where U: Eq + PhfHash,
T: Borrow<U>
T: PhfBorrow<U>
{
self.map.get_key(key)
}
Expand All @@ -51,7 +51,7 @@ impl<T> OrderedSet<T> {
/// the ordered set.
pub fn get_index<U: ?Sized>(&self, key: &U) -> Option<usize>
where U: Eq + PhfHash,
T: Borrow<U>
T: PhfBorrow<U>
{
self.map.get_index(key)
}
Expand All @@ -65,7 +65,7 @@ impl<T> OrderedSet<T> {
/// Returns true if `value` is in the `Set`.
pub fn contains<U: ?Sized>(&self, value: &U) -> bool
where U: Eq + PhfHash,
T: Borrow<U>
T: PhfBorrow<U>
{
self.map.contains_key(value)
}
Expand All @@ -78,7 +78,7 @@ impl<T> OrderedSet<T> {
}
}

impl<T> OrderedSet<T> where T: Eq + PhfHash {
impl<T> OrderedSet<T> where T: Eq + PhfHash + PhfBorrow<T> {
/// Returns true if `other` shares no elements with `self`.
#[inline]
pub fn is_disjoint(&self, other: &OrderedSet<T>) -> bool {
Expand Down

0 comments on commit f43a9cf

Please sign in to comment.