Skip to content

Commit

Permalink
Introduce a Slice abstraction for buffers
Browse files Browse the repository at this point in the history
This will allow for dynamic creation of PHF data structures.
  • Loading branch information
sfackler committed Mar 6, 2016
1 parent d9351e1 commit 0cc3844
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
25 changes: 25 additions & 0 deletions phf/src/lib.rs
Expand Up @@ -13,6 +13,8 @@ extern crate std as core;

extern crate phf_shared;

use core::ops::Deref;

pub use phf_shared::PhfHash;
#[doc(inline)]
pub use map::Map;
Expand All @@ -27,3 +29,26 @@ pub mod map;
pub mod set;
pub mod ordered_map;
pub mod ordered_set;

// WARNING: this is not considered part of phf's public API and is subject to
// change at any time.
//
// Basically Cow, but with the Owned version conditionally compiled
#[doc(hidden)]
pub enum Slice<T: 'static> {
Static(&'static [T]),
#[cfg(not(feature = "core"))]
Dynamic(Vec<T>),
}

impl<T> Deref for Slice<T> {
type Target = [T];

fn deref(&self) -> &[T] {
match *self {
Slice::Static(t) => t,
#[cfg(not(feature = "core"))]
Slice::Dynamic(ref t) => t,
}
}
}
10 changes: 5 additions & 5 deletions phf/src/map.rs
Expand Up @@ -4,8 +4,8 @@ use core::ops::Index;
use core::slice;
use core::fmt;
use core::iter::IntoIterator;
use PhfHash;
use phf_shared;
use phf_shared::{self, PhfHash};
use Slice;

/// An immutable map constructed at compile time.
///
Expand All @@ -18,9 +18,9 @@ pub struct Map<K: 'static, V: 'static> {
#[doc(hidden)]
pub key: u64,
#[doc(hidden)]
pub disps: &'static [(u32, u32)],
pub disps: Slice<(u32, u32)>,
#[doc(hidden)]
pub entries: &'static [(K, V)],
pub entries: Slice<(K, V)>,
}

impl<K, V> fmt::Debug for Map<K, V> where K: fmt::Debug, V: fmt::Debug {
Expand Down Expand Up @@ -81,7 +81,7 @@ impl<K, V> Map<K, V> {
K: Borrow<T>
{
let hash = phf_shared::hash(key, self.key);
let index = phf_shared::get_index(hash, self.disps, self.entries.len());
let index = phf_shared::get_index(hash, &*self.disps, self.entries.len());
let entry = &self.entries[index as usize];
let b: &T = entry.0.borrow();
if b == key {
Expand Down
12 changes: 6 additions & 6 deletions phf/src/ordered_map.rs
Expand Up @@ -4,9 +4,9 @@ use core::iter::IntoIterator;
use core::ops::Index;
use core::fmt;
use core::slice;
use phf_shared::{self, PhfHash};

use PhfHash;
use phf_shared;
use Slice;

/// An order-preserving immutable map constructed at compile time.
///
Expand All @@ -22,11 +22,11 @@ pub struct OrderedMap<K: 'static, V: 'static> {
#[doc(hidden)]
pub key: u64,
#[doc(hidden)]
pub disps: &'static [(u32, u32)],
pub disps: Slice<(u32, u32)>,
#[doc(hidden)]
pub idxs: &'static [usize],
pub idxs: Slice<usize>,
#[doc(hidden)]
pub entries: &'static [(K, V)],
pub entries: Slice<(K, V)>,
}

impl<K, V> fmt::Debug for OrderedMap<K, V> where K: fmt::Debug, V: fmt::Debug {
Expand Down Expand Up @@ -109,7 +109,7 @@ impl<K, V> OrderedMap<K, V> {
K: Borrow<T>
{
let hash = phf_shared::hash(key, self.key);
let idx_index = phf_shared::get_index(hash, self.disps, self.idxs.len());
let idx_index = phf_shared::get_index(hash, &*self.disps, self.idxs.len());
let idx = self.idxs[idx_index as usize];
let entry = &self.entries[idx];

Expand Down
20 changes: 10 additions & 10 deletions phf_codegen/src/lib.rs
Expand Up @@ -142,7 +142,7 @@ impl<K: Hash+PhfHash+Eq+fmt::Debug> Map<K> {
try!(write!(w,
"::phf::Map {{
key: {},
disps: &[",
disps: ::phf::Slice::Static(&[",
state.key));
for &(d1, d2) in &state.disps {
try!(write!(w,
Expand All @@ -153,8 +153,8 @@ impl<K: Hash+PhfHash+Eq+fmt::Debug> Map<K> {
}
try!(write!(w,
"
],
entries: &["));
]),
entries: ::phf::Slice::Static(&["));
for &idx in &state.map {
try!(write!(w,
"
Expand All @@ -164,7 +164,7 @@ impl<K: Hash+PhfHash+Eq+fmt::Debug> Map<K> {
}
write!(w,
"
]
]),
}}")
}
}
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<K: Hash+PhfHash+Eq+fmt::Debug> OrderedMap<K> {
try!(write!(w,
"::phf::OrderedMap {{
key: {},
disps: &[",
disps: ::phf::Slice::Static(&[",
state.key));
for &(d1, d2) in &state.disps {
try!(write!(w,
Expand All @@ -252,8 +252,8 @@ impl<K: Hash+PhfHash+Eq+fmt::Debug> OrderedMap<K> {
}
try!(write!(w,
"
],
idxs: &["));
]),
idxs: ::phf::Slice::Static(&["));
for &idx in &state.map {
try!(write!(w,
"
Expand All @@ -262,8 +262,8 @@ impl<K: Hash+PhfHash+Eq+fmt::Debug> OrderedMap<K> {
}
try!(write!(w,
"
],
entries: &["));
]),
entries: ::phf::Slice::Static(&["));
for (key, value) in self.keys.iter().zip(self.values.iter()) {
try!(write!(w,
"
Expand All @@ -273,7 +273,7 @@ impl<K: Hash+PhfHash+Eq+fmt::Debug> OrderedMap<K> {
}
write!(w,
"
]
]),
}}")
}
}
Expand Down
10 changes: 5 additions & 5 deletions phf_macros/src/util.rs
Expand Up @@ -100,8 +100,8 @@ pub fn create_map(cx: &mut ExtCtxt,
let key = state.key;
MacEager::expr(quote_expr!(cx, ::phf::Map {
key: $key,
disps: &$disps,
entries: &$entries,
disps: ::phf::Slice::Static(&$disps),
entries: ::phf::Slice::Static(&$entries),
}))
}

Expand Down Expand Up @@ -138,9 +138,9 @@ pub fn create_ordered_map(cx: &mut ExtCtxt,
let key = state.key;
MacEager::expr(quote_expr!(cx, ::phf::OrderedMap {
key: $key,
disps: &$disps,
idxs: &$idxs,
entries: &$entries,
disps: ::phf::Slice::Static(&$disps),
idxs: ::phf::Slice::Static(&$idxs),
entries: ::phf::Slice::Static(&$entries),
}))
}

Expand Down

0 comments on commit 0cc3844

Please sign in to comment.