phf-0.8.0/Cargo.toml.orig010064400017500001750000000012751355143646600134260ustar0000000000000000[package] name = "phf" authors = ["Steven Fackler "] version = "0.8.0" license = "MIT" description = "Runtime support for perfect hash function data structures" repository = "https://github.com/sfackler/rust-phf" edition = "2018" [lib] name = "phf" path = "src/lib.rs" test = false [features] default = ["std"] std = ["phf_shared/std"] unicase = ["phf_shared/unicase"] macros = [ "phf_macros", "proc-macro-hack", ] [dependencies] phf_shared = { version = "0.8.0", path = "../phf_shared" } proc-macro-hack = { version = "0.5.4", optional = true } phf_macros = { version = "0.8.0", optional = true, path = "../phf_macros" } [package.metadata.docs.rs] features = ["macros", ] phf-0.8.0/Cargo.toml0000644000000022330000000000000076530ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "phf" version = "0.8.0" authors = ["Steven Fackler "] description = "Runtime support for perfect hash function data structures" license = "MIT" repository = "https://github.com/sfackler/rust-phf" [package.metadata.docs.rs] features = ["macros"] [lib] name = "phf" path = "src/lib.rs" test = false [dependencies.phf_macros] version = "0.8.0" optional = true [dependencies.phf_shared] version = "0.8.0" [dependencies.proc-macro-hack] version = "0.5.4" optional = true [features] default = ["std"] macros = ["phf_macros", "proc-macro-hack"] std = ["phf_shared/std"] unicase = ["phf_shared/unicase"] phf-0.8.0/src/lib.rs010064400017500001750000000054271355141571300124350ustar0000000000000000//! Compile-time generated maps and sets. //! //! The `phf::Map` and `phf::Set` types have roughly comparable performance to //! a standard hash table, but can be generated as compile-time static values. //! //! # Usage //! //! If the `macros` Cargo feature is enabled, the `phf_map`, `phf_set`, //! `phf_ordered_map`, and `phf_ordered_set` macros can be used to construct //! the PHF type. This method can be used with a stable compiler //! (`rustc` version 1.30+) //! //! ```toml //! [dependencies] //! phf = { version = "0.7.24", features = ["macros"] } //! ``` //! //! ``` //! use phf::{phf_map, phf_set}; //! //! static MY_MAP: phf::Map<&'static str, u32> = phf_map! { //! "hello" => 1, //! "world" => 2, //! }; //! //! static MY_SET: phf::Set<&'static str> = phf_set! { //! "hello world", //! "hola mundo", //! }; //! //! fn main() { //! assert_eq!(MY_MAP["hello"], 1); //! assert!(MY_SET.contains("hello world")); //! } //! ``` //! //! (Alternatively, you can use the phf_codegen crate to generate PHF datatypes //! in a build script) #![doc(html_root_url="https://docs.rs/phf/0.7")] #![warn(missing_docs)] #![cfg_attr(not(feature = "std"), no_std)] #[cfg(feature = "std")] extern crate std as core; #[cfg(feature = "macros")] /// Macro to create a `static` (compile-time) [`Map`]. /// /// Requires the `"macros"` feature. /// /// # Example /// /// ```rust,edition2018 /// use ::phf::{phf_map, Map}; /// /// static MY_MAP: Map<&'static str, u32> = phf_map! { /// "hello" => 1, /// "world" => 2, /// }; /// /// fn main () /// { /// assert_eq!(MY_MAP["hello"], 1); /// } /// ``` #[::proc_macro_hack::proc_macro_hack] pub use phf_macros:: phf_map; #[cfg(feature = "macros")] /// Macro to create a `static` (compile-time) [`Set`]. /// /// Requires the `"macros"` feature. /// /// # Example /// /// ```rust,edition2018 /// use ::phf::{phf_set, Set}; /// /// static MY_SET: Set<&'static str> = phf_set! { /// "hello world", /// "hola mundo", /// }; /// /// fn main () /// { /// assert!(MY_SET.contains("hello world")); /// } /// ``` #[::proc_macro_hack::proc_macro_hack] pub use phf_macros::phf_set; use core::ops::Deref; pub use phf_shared::PhfHash; #[doc(inline)] pub use self::map::Map; #[doc(inline)] pub use self::set::Set; pub mod map; pub mod 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 { Static(&'static [T]), #[cfg(feature = "std")] Dynamic(Vec), } impl Deref for Slice { type Target = [T]; fn deref(&self) -> &[T] { match *self { Slice::Static(t) => t, #[cfg(feature = "std")] Slice::Dynamic(ref t) => t, } } } phf-0.8.0/src/map.rs010064400017500001750000000124201355141562300124330ustar0000000000000000//! An immutable map constructed at compile time. use core::borrow::Borrow; use core::ops::Index; use core::slice; use core::fmt; use core::iter::IntoIterator; use phf_shared::{self, PhfHash, HashKey}; use crate::Slice; /// An immutable map constructed at compile time. /// /// ## Note /// /// The fields of this struct are public so that they may be initialized by the /// `phf_map!` macro and code generation. They are subject to change at any /// time and should never be accessed directly. pub struct Map { #[doc(hidden)] pub key: HashKey, #[doc(hidden)] pub disps: Slice<(u32, u32)>, #[doc(hidden)] pub entries: Slice<(K, V)>, } impl fmt::Debug for Map where K: fmt::Debug, V: fmt::Debug { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_map().entries(self.entries()).finish() } } impl<'a, K, V, T: ?Sized> Index<&'a T> for Map where T: Eq + PhfHash, K: Borrow { type Output = V; fn index(&self, k: &'a T) -> &V { self.get(k).expect("invalid key") } } impl Map { /// Returns true if the `Map` is empty. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Returns the number of entries in the `Map`. pub fn len(&self) -> usize { self.entries.len() } /// Determines if `key` is in the `Map`. pub fn contains_key(&self, key: &T) -> bool where T: Eq + PhfHash, K: Borrow { self.get(key).is_some() } /// Returns a reference to the value that `key` maps to. pub fn get(&self, key: &T) -> Option<&V> where T: Eq + PhfHash, K: Borrow { self.get_entry(key).map(|e| e.1) } /// Returns a reference to the map's internal static instance of the given /// key. /// /// This can be useful for interning schemes. pub fn get_key(&self, key: &T) -> Option<&K> where T: Eq + PhfHash, K: Borrow { self.get_entry(key).map(|e| e.0) } /// Like `get`, but returns both the key and the value. pub fn get_entry(&self, key: &T) -> Option<(&K, &V)> where T: Eq + PhfHash, K: Borrow { if self.disps.len() == 0 { return None; } //Prevent panic on empty map let hashes = phf_shared::hash(key, &self.key); let index = phf_shared::get_index(&hashes, &*self.disps, self.entries.len()); let entry = &self.entries[index as usize]; let b: &T = entry.0.borrow(); if b == key { Some((&entry.0, &entry.1)) } else { None } } /// Returns an iterator over the key/value pairs in the map. /// /// Entries are returned in an arbitrary but fixed order. pub fn entries<'a>(&'a self) -> Entries<'a, K, V> { Entries { iter: self.entries.iter() } } /// Returns an iterator over the keys in the map. /// /// Keys are returned in an arbitrary but fixed order. pub fn keys<'a>(&'a self) -> Keys<'a, K, V> { Keys { iter: self.entries() } } /// Returns an iterator over the values in the map. /// /// Values are returned in an arbitrary but fixed order. pub fn values<'a>(&'a self) -> Values<'a, K, V> { Values { iter: self.entries() } } } impl<'a, K, V> IntoIterator for &'a Map { type Item = (&'a K, &'a V); type IntoIter = Entries<'a, K, V>; fn into_iter(self) -> Entries<'a, K, V> { self.entries() } } /// An iterator over the key/value pairs in a `Map`. pub struct Entries<'a, K: 'a, V: 'a> { iter: slice::Iter<'a, (K, V)>, } impl<'a, K, V> Iterator for Entries<'a, K, V> { type Item = (&'a K, &'a V); fn next(&mut self) -> Option<(&'a K, &'a V)> { self.iter.next().map(|&(ref k, ref v)| (k, v)) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> { fn next_back(&mut self) -> Option<(&'a K, &'a V)> { self.iter.next_back().map(|e| (&e.0, &e.1)) } } impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {} /// An iterator over the keys in a `Map`. pub struct Keys<'a, K: 'a, V: 'a> { iter: Entries<'a, K, V>, } impl<'a, K, V> Iterator for Keys<'a, K, V> { type Item = &'a K; fn next(&mut self) -> Option<&'a K> { self.iter.next().map(|e| e.0) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> { fn next_back(&mut self) -> Option<&'a K> { self.iter.next_back().map(|e| e.0) } } impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {} /// An iterator over the values in a `Map`. pub struct Values<'a, K: 'a, V: 'a> { iter: Entries<'a, K, V>, } impl<'a, K, V> Iterator for Values<'a, K, V> { type Item = &'a V; fn next(&mut self) -> Option<&'a V> { self.iter.next().map(|e| e.1) } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> { fn next_back(&mut self) -> Option<&'a V> { self.iter.next_back().map(|e| e.1) } } impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {} phf-0.8.0/src/set.rs010064400017500001750000000055611355141562300124610ustar0000000000000000//! An immutable set constructed at compile time. use core::borrow::Borrow; use core::iter::IntoIterator; use core::fmt; use crate::{map, Map, PhfHash}; /// An immutable set constructed at compile time. /// /// ## Note /// /// The fields of this struct are public so that they may be initialized by the /// `phf_set!` macro and code generation. They are subject to change at any /// time and should never be accessed directly. pub struct Set { #[doc(hidden)] pub map: Map, } impl fmt::Debug for Set where T: fmt::Debug { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { fmt.debug_set().entries(self).finish() } } impl Set { /// Returns the number of elements in the `Set`. pub fn len(&self) -> usize { self.map.len() } /// Returns true if the `Set` contains no elements. pub fn is_empty(&self) -> bool { self.len() == 0 } /// Returns a reference to the set's internal static instance of the given /// key. /// /// This can be useful for interning schemes. pub fn get_key(&self, key: &U) -> Option<&T> where U: Eq + PhfHash, T: Borrow { self.map.get_key(key) } /// Returns true if `value` is in the `Set`. pub fn contains(&self, value: &U) -> bool where U: Eq + PhfHash, T: Borrow { self.map.contains_key(value) } /// Returns an iterator over the values in the set. /// /// Values are returned in an arbitrary but fixed order. pub fn iter<'a>(&'a self) -> Iter<'a, T> { Iter { iter: self.map.keys() } } } impl Set where T: Eq + PhfHash { /// Returns true if `other` shares no elements with `self`. pub fn is_disjoint(&self, other: &Set) -> bool { !self.iter().any(|value| other.contains(value)) } /// Returns true if `other` contains all values in `self`. pub fn is_subset(&self, other: &Set) -> bool { self.iter().all(|value| other.contains(value)) } /// Returns true if `self` contains all values in `other`. pub fn is_superset(&self, other: &Set) -> bool { other.is_subset(self) } } impl<'a, T> IntoIterator for &'a Set { type Item = &'a T; type IntoIter = Iter<'a, T>; fn into_iter(self) -> Iter<'a, T> { self.iter() } } /// An iterator over the values in a `Set`. pub struct Iter<'a, T: 'static> { iter: map::Keys<'a, T, ()>, } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<&'a T> { self.iter.next() } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } impl<'a, T> DoubleEndedIterator for Iter<'a, T> { fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() } } impl<'a, T> ExactSizeIterator for Iter<'a, T> {} phf-0.8.0/.cargo_vcs_info.json0000644000000001120000000000000116470ustar00{ "git": { "sha1": "81c7cc5b48649108428671d3b8ad151f6fbdb359" } }