diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/phf | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/phf')
-rw-r--r-- | third_party/rust/phf/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/phf/Cargo.toml | 43 | ||||
-rw-r--r-- | third_party/rust/phf/src/lib.rs | 124 | ||||
-rw-r--r-- | third_party/rust/phf/src/map.rs | 199 | ||||
-rw-r--r-- | third_party/rust/phf/src/set.rs | 113 |
5 files changed, 480 insertions, 0 deletions
diff --git a/third_party/rust/phf/.cargo-checksum.json b/third_party/rust/phf/.cargo-checksum.json new file mode 100644 index 0000000000..24195aff64 --- /dev/null +++ b/third_party/rust/phf/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"cf0f974f175cd6eb806b55258f4b481f59e5fdb069d4329d434dda3637908740","src/lib.rs":"85fd20b54f9595ceb449879fa88fb11eff9d71dd80728c2f5e5a130026d98d2f","src/map.rs":"2b927cf172e2335207df1efb21f2ef30720bee76214fd0aa7aed914fd24ab676","src/set.rs":"e73a20ae25a3a7a3a234639416992e537f6a36e6d8bc9b12359ed74c58624632"},"package":"3dfb61232e34fcb633f43d12c58f83c1df82962dcdfa565a4e866ffc17dafe12"}
\ No newline at end of file diff --git a/third_party/rust/phf/Cargo.toml b/third_party/rust/phf/Cargo.toml new file mode 100644 index 0000000000..6d17124ffe --- /dev/null +++ b/third_party/rust/phf/Cargo.toml @@ -0,0 +1,43 @@ +# 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 <sfackler@gmail.com>"] +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"] diff --git a/third_party/rust/phf/src/lib.rs b/third_party/rust/phf/src/lib.rs new file mode 100644 index 0000000000..b5b587d445 --- /dev/null +++ b/third_party/rust/phf/src/lib.rs @@ -0,0 +1,124 @@ +//! 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<T: 'static> { + Static(&'static [T]), + #[cfg(feature = "std")] + Dynamic(Vec<T>), +} + +impl<T> Deref for Slice<T> { + type Target = [T]; + + fn deref(&self) -> &[T] { + match *self { + Slice::Static(t) => t, + #[cfg(feature = "std")] + Slice::Dynamic(ref t) => t, + } + } +} diff --git a/third_party/rust/phf/src/map.rs b/third_party/rust/phf/src/map.rs new file mode 100644 index 0000000000..30fc26487e --- /dev/null +++ b/third_party/rust/phf/src/map.rs @@ -0,0 +1,199 @@ +//! 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<K: 'static, V: 'static> { + #[doc(hidden)] + pub key: HashKey, + #[doc(hidden)] + pub disps: Slice<(u32, u32)>, + #[doc(hidden)] + pub entries: Slice<(K, V)>, +} + +impl<K, V> fmt::Debug for Map<K, V> 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<K, V> where T: Eq + PhfHash, K: Borrow<T> { + type Output = V; + + fn index(&self, k: &'a T) -> &V { + self.get(k).expect("invalid key") + } +} + +impl<K, V> Map<K, V> { + /// 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<T: ?Sized>(&self, key: &T) -> bool + where T: Eq + PhfHash, + K: Borrow<T> + { + self.get(key).is_some() + } + + /// 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> + { + 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<T: ?Sized>(&self, key: &T) -> Option<&K> + where T: Eq + PhfHash, + K: Borrow<T> + { + self.get_entry(key).map(|e| e.0) + } + + /// 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> + { + 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<K, V> { + 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<usize>) { + 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<usize>) { + 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<usize>) { + 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> {} diff --git a/third_party/rust/phf/src/set.rs b/third_party/rust/phf/src/set.rs new file mode 100644 index 0000000000..9588d4a747 --- /dev/null +++ b/third_party/rust/phf/src/set.rs @@ -0,0 +1,113 @@ +//! 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<T: 'static> { + #[doc(hidden)] + pub map: Map<T, ()>, +} + +impl<T> fmt::Debug for Set<T> where T: fmt::Debug { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_set().entries(self).finish() + } +} + +impl<T> Set<T> { + /// 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<U: ?Sized>(&self, key: &U) -> Option<&T> + where U: Eq + PhfHash, + T: Borrow<U> + { + self.map.get_key(key) + } + + /// Returns true if `value` is in the `Set`. + pub fn contains<U: ?Sized>(&self, value: &U) -> bool + where U: Eq + PhfHash, + T: Borrow<U> + { + 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<T> Set<T> where T: Eq + PhfHash { + /// Returns true if `other` shares no elements with `self`. + pub fn is_disjoint(&self, other: &Set<T>) -> bool { + !self.iter().any(|value| other.contains(value)) + } + + /// Returns true if `other` contains all values in `self`. + pub fn is_subset(&self, other: &Set<T>) -> bool { + self.iter().all(|value| other.contains(value)) + } + + /// Returns true if `self` contains all values in `other`. + pub fn is_superset(&self, other: &Set<T>) -> bool { + other.is_subset(self) + } +} + +impl<'a, T> IntoIterator for &'a Set<T> { + 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<usize>) { + 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> {} |