Skip to content

Commit

Permalink
Fix for upstream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Jan 9, 2015
1 parent cba0a95 commit 0b22188
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 99 deletions.
3 changes: 2 additions & 1 deletion phf/src/lib.rs
@@ -1,9 +1,10 @@
//! Compile time optimized maps and sets.
//!
//! Keys can be string literals, byte string literals, byte literals, char
//! literals, or any of the fixed-size integral types.
//! literals, or any of the fixed-size isizeegral types.
#![doc(html_root_url="https://sfackler.github.io/doc")]
#![warn(missing_docs)]
#![allow(unstable)]
#![feature(old_orphan_check)]
#![no_std]

Expand Down
35 changes: 25 additions & 10 deletions phf/src/map.rs
Expand Up @@ -17,7 +17,7 @@ use phf_shared;
/// #[plugin] #[no_link]
/// extern crate phf_mac;
///
/// static MY_MAP: phf::Map<&'static str, int> = phf_map! {
/// static MY_MAP: phf::Map<&'static str, isize> = phf_map! {
/// "hello" => 10,
/// "world" => 11,
/// };
Expand All @@ -39,7 +39,7 @@ pub struct Map<K:'static, V:'static> {
pub entries: &'static [(K, V)],
}

impl<K, V> fmt::Show for Map<K, V> where K: fmt::Show, V: fmt::Show {
impl<K, V> fmt::String for Map<K, V> where K: fmt::String, V: fmt::String {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
Expand All @@ -54,6 +54,21 @@ impl<K, V> fmt::Show for Map<K, V> where K: fmt::Show, V: fmt::Show {
}
}

impl<K, V> fmt::Show for Map<K, V> where K: fmt::Show, V: fmt::Show {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
for (k, v) in self.entries() {
if !first {
try!(write!(fmt, ", "));
}
try!(write!(fmt, "{:?}: {:?}", k, v));
first = false;
}
write!(fmt, "}}")
}
}

impl<K, V, T: ?Sized> Index<T> for Map<K, V> where T: Eq + PhfHash + BorrowFrom<K> {
type Output = V;

Expand All @@ -69,7 +84,7 @@ impl<K, V> Map<K, V> {
}

/// Returns the number of entries in the `Map`.
pub fn len(&self) -> uint {
pub fn len(&self) -> usize {
self.entries.len()
}

Expand All @@ -83,10 +98,10 @@ impl<K, V> Map<K, V> {
self.get_entry(key).map(|e| e.1)
}

/// Returns a reference to the map's internal static instance of the given
/// Returns a reference to the map's isizeernal static instance of the given
/// key.
///
/// This can be useful for interning schemes.
/// This can be useful for isizeerning schemes.
pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K> where T: Eq + PhfHash + BorrowFrom<K> {
self.get_entry(key).map(|e| e.0)
}
Expand All @@ -95,9 +110,9 @@ impl<K, V> Map<K, V> {
pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)>
where T: Eq + PhfHash + BorrowFrom<K> {
let (g, f1, f2) = key.phf_hash(self.key);
let (d1, d2) = self.disps[(g % (self.disps.len() as u32)) as uint];
let (d1, d2) = self.disps[(g % (self.disps.len() as u32)) as usize];
let entry = &self.entries[(phf_shared::displace(f1, f2, d1, d2) % (self.entries.len() as u32))
as uint];
as usize];
let b: &T = BorrowFrom::borrow_from(&entry.0);
if b == key {
Some((&entry.0, &entry.1))
Expand Down Expand Up @@ -140,7 +155,7 @@ impl<'a, K, V> Iterator for Entries<'a, K, V> {
self.iter.next().map(|&(ref k, ref v)| (k, v))
}

fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
Expand All @@ -165,7 +180,7 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
self.iter.next().map(|e| e.0)
}

fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
Expand All @@ -190,7 +205,7 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
self.iter.next().map(|e| e.1)
}

fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
Expand Down
55 changes: 35 additions & 20 deletions phf/src/ordered_map.rs
Expand Up @@ -22,7 +22,7 @@ use phf_shared;
/// #[plugin] #[no_link]
/// extern crate phf_mac;
///
/// static MY_MAP: phf::OrderedMap<&'static str, int> = phf_ordered_map! {
/// static MY_MAP: phf::OrderedMap<&'static str, isize> = phf_ordered_map! {
/// "hello" => 10,
/// "world" => 11,
/// };
Expand All @@ -41,12 +41,27 @@ pub struct OrderedMap<K:'static, V:'static> {
#[doc(hidden)]
pub disps: &'static [(u32, u32)],
#[doc(hidden)]
pub idxs: &'static [uint],
pub idxs: &'static [usize],
#[doc(hidden)]
pub entries: &'static [(K, V)],
}

impl<K, V> fmt::Show for OrderedMap<K, V> where K: fmt::Show, V: fmt::Show {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
for (k, v) in self.entries() {
if !first {
try!(write!(fmt, ", "));
}
try!(write!(fmt, "{:?}: {:?}", k, v));
first = false;
}
write!(fmt, "}}")
}
}

impl<K, V> fmt::String for OrderedMap<K, V> where K: fmt::String, V: fmt::String {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
Expand All @@ -71,7 +86,7 @@ impl<K, V, T: ?Sized> Index<T> for OrderedMap<K, V> where T: Eq + PhfHash + Borr

impl<K, V> OrderedMap<K, V> {
/// Returns the number of entries in the `Map`.
pub fn len(&self) -> uint {
pub fn len(&self) -> usize {
self.entries.len()
}

Expand All @@ -85,10 +100,10 @@ impl<K, V> OrderedMap<K, V> {
self.get_entry(key).map(|e| e.1)
}

/// Returns a reference to the map's internal static instance of the given
/// Returns a reference to the map's isizeernal static instance of the given
/// key.
///
/// This can be useful for interning schemes.
/// This can be useful for isizeerning schemes.
pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K> where T: Eq + PhfHash + BorrowFrom<K> {
self.get_entry(key).map(|e| e.0)
}
Expand All @@ -100,22 +115,22 @@ impl<K, V> OrderedMap<K, V> {

/// Returns the index of the key within the list used to initialize
/// the ordered map.
pub fn get_index<T: ?Sized>(&self, key: &T) -> Option<uint>
pub fn get_index<T: ?Sized>(&self, key: &T) -> Option<usize>
where T: Eq + PhfHash + BorrowFrom<K> {
self.get_internal(key).map(|(i, _)| i)
self.get_isizeernal(key).map(|(i, _)| i)
}

/// 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 + BorrowFrom<K> {
self.get_internal(key).map(|(_, e)| e)
self.get_isizeernal(key).map(|(_, e)| e)
}

fn get_internal<T: ?Sized>(&self, key: &T) -> Option<(uint, (&K, &V))>
fn get_isizeernal<T: ?Sized>(&self, key: &T) -> Option<(usize, (&K, &V))>
where T: Eq + PhfHash + BorrowFrom<K> {
let (g, f1, f2) = key.phf_hash(self.key);
let (d1, d2) = self.disps[(g % (self.disps.len() as u32)) as uint];
let idx = self.idxs[(phf_shared::displace(f1, f2, d1, d2) % (self.idxs.len() as u32)) as uint];
let (d1, d2) = self.disps[(g % (self.disps.len() as u32)) as usize];
let idx = self.idxs[(phf_shared::displace(f1, f2, d1, d2) % (self.idxs.len() as u32)) as usize];
let entry = &self.entries[idx];

let b: &T = BorrowFrom::borrow_from(&entry.0);
Expand Down Expand Up @@ -160,7 +175,7 @@ impl<'a, K, V> Iterator for Entries<'a, K, V> {
self.iter.next().map(|e| (&e.0, &e.1))
}

fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
Expand All @@ -172,11 +187,11 @@ impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> {
}

impl<'a, K, V> RandomAccessIterator for Entries<'a, K, V> {
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable()
}

fn idx(&mut self, index: uint) -> Option<(&'a K, &'a V)> {
fn idx(&mut self, index: usize) -> Option<(&'a K, &'a V)> {
self.iter.idx(index).map(|e| (&e.0, &e.1))
}
}
Expand All @@ -195,7 +210,7 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
self.iter.next().map(|e| e.0)
}

fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
Expand All @@ -207,11 +222,11 @@ impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
}

impl<'a, K, V> RandomAccessIterator for Keys<'a, K, V> {
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable()
}

fn idx(&mut self, index: uint) -> Option<&'a K> {
fn idx(&mut self, index: usize) -> Option<&'a K> {
self.iter.idx(index).map(|e| e.0)
}
}
Expand All @@ -230,7 +245,7 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
self.iter.next().map(|e| e.1)
}

fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
Expand All @@ -242,11 +257,11 @@ impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
}

impl<'a, K, V> RandomAccessIterator for Values<'a, K, V> {
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable()
}

fn idx(&mut self, index: uint) -> Option<&'a V> {
fn idx(&mut self, index: usize) -> Option<&'a V> {
self.iter.idx(index).map(|e| e.1)
}
}
Expand Down
33 changes: 24 additions & 9 deletions phf/src/ordered_set.rs
Expand Up @@ -37,7 +37,7 @@ pub struct OrderedSet<T:'static> {
pub map: OrderedMap<T, ()>,
}

impl<T> fmt::Show for OrderedSet<T> where T: fmt::Show {
impl<T> fmt::String for OrderedSet<T> where T: fmt::String {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
Expand All @@ -52,9 +52,24 @@ impl<T> fmt::Show for OrderedSet<T> where T: fmt::Show {
}
}

impl<T> fmt::Show for OrderedSet<T> where T: fmt::Show {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
let mut first = true;
for entry in self.iter() {
if !first {
try!(write!(fmt, ", "));
}
try!(write!(fmt, "{:?}", entry));
first = false;
}
write!(fmt, "}}")
}
}

impl<T> OrderedSet<T> {
/// Returns the number of elements in the `OrderedSet`.
pub fn len(&self) -> uint {
pub fn len(&self) -> usize {
self.map.len()
}

Expand All @@ -63,17 +78,17 @@ impl<T> OrderedSet<T> {
self.len() == 0
}

/// Returns a reference to the set's internal static instance of the given
/// Returns a reference to the set's isizeernal static instance of the given
/// key.
///
/// This can be useful for interning schemes.
/// This can be useful for isizeerning schemes.
pub fn get_key<U: ?Sized>(&self, key: &U) -> Option<&T> where U: Eq + PhfHash + BorrowFrom<T> {
self.map.get_key(key)
}

/// Returns the index of the key within the list used to initialize
/// the ordered set.
pub fn get_index<U: ?Sized>(&self, key: &U) -> Option<uint>
pub fn get_index<U: ?Sized>(&self, key: &U) -> Option<usize>
where U: Eq + PhfHash + BorrowFrom<T> {
self.map.get_index(key)
}
Expand All @@ -94,7 +109,7 @@ impl<T> OrderedSet<T> {
impl<T> OrderedSet<T> where T: Eq + PhfHash {
/// Returns true if `other` shares no elements with `self`.
#[inline]
pub fn is_disjoint(&self, other: &OrderedSet<T>) -> bool {
pub fn is_disjoisize(&self, other: &OrderedSet<T>) -> bool {
!self.iter().any(|value| other.contains(value))
}

Expand Down Expand Up @@ -125,7 +140,7 @@ impl<'a, T> Iterator for Iter<'a, T> {
}

#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
Expand All @@ -139,12 +154,12 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {

impl<'a, T> RandomAccessIterator for Iter<'a, T> {
#[inline]
fn indexable(&self) -> uint {
fn indexable(&self) -> usize {
self.iter.indexable()
}

#[inline]
fn idx(&mut self, index: uint) -> Option<&'a T> {
fn idx(&mut self, index: usize) -> Option<&'a T> {
self.iter.idx(index)
}
}
Expand Down

0 comments on commit 0b22188

Please sign in to comment.