phf/
map.rs

1//! An immutable map constructed at compile time.
2use core::borrow::Borrow;
3use core::ops::Index;
4use core::slice;
5use core::fmt;
6use core::iter::IntoIterator;
7use phf_shared::{self, PhfHash, HashKey};
8use crate::Slice;
9
10/// An immutable map constructed at compile time.
11///
12/// ## Note
13///
14/// The fields of this struct are public so that they may be initialized by the
15/// `phf_map!` macro and code generation. They are subject to change at any
16/// time and should never be accessed directly.
17pub struct Map<K: 'static, V: 'static> {
18    #[doc(hidden)]
19    pub key: HashKey,
20    #[doc(hidden)]
21    pub disps: Slice<(u32, u32)>,
22    #[doc(hidden)]
23    pub entries: Slice<(K, V)>,
24}
25
26impl<K, V> fmt::Debug for Map<K, V> where K: fmt::Debug, V: fmt::Debug {
27    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
28        fmt.debug_map().entries(self.entries()).finish()
29    }
30}
31
32impl<'a, K, V, T: ?Sized> Index<&'a T> for Map<K, V> where T: Eq + PhfHash, K: Borrow<T> {
33    type Output = V;
34
35    fn index(&self, k: &'a T) -> &V {
36        self.get(k).expect("invalid key")
37    }
38}
39
40impl<K, V> Map<K, V> {
41    /// Returns true if the `Map` is empty.
42    pub fn is_empty(&self) -> bool {
43        self.len() == 0
44    }
45
46    /// Returns the number of entries in the `Map`.
47    pub fn len(&self) -> usize {
48        self.entries.len()
49    }
50
51    /// Determines if `key` is in the `Map`.
52    pub fn contains_key<T: ?Sized>(&self, key: &T) -> bool
53        where T: Eq + PhfHash,
54              K: Borrow<T>
55    {
56        self.get(key).is_some()
57    }
58
59    /// Returns a reference to the value that `key` maps to.
60    pub fn get<T: ?Sized>(&self, key: &T) -> Option<&V>
61        where T: Eq + PhfHash,
62              K: Borrow<T>
63    {
64        self.get_entry(key).map(|e| e.1)
65    }
66
67    /// Returns a reference to the map's internal static instance of the given
68    /// key.
69    ///
70    /// This can be useful for interning schemes.
71    pub fn get_key<T: ?Sized>(&self, key: &T) -> Option<&K>
72        where T: Eq + PhfHash,
73              K: Borrow<T>
74    {
75        self.get_entry(key).map(|e| e.0)
76    }
77
78    /// Like `get`, but returns both the key and the value.
79    pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)>
80        where T: Eq + PhfHash,
81              K: Borrow<T>
82    {
83        if self.disps.len() == 0 { return None; } //Prevent panic on empty map
84        let hashes = phf_shared::hash(key, &self.key);
85        let index = phf_shared::get_index(&hashes, &*self.disps, self.entries.len());
86        let entry = &self.entries[index as usize];
87        let b: &T = entry.0.borrow();
88        if b == key {
89            Some((&entry.0, &entry.1))
90        } else {
91            None
92        }
93    }
94
95    /// Returns an iterator over the key/value pairs in the map.
96    ///
97    /// Entries are returned in an arbitrary but fixed order.
98    pub fn entries<'a>(&'a self) -> Entries<'a, K, V> {
99        Entries { iter: self.entries.iter() }
100    }
101
102    /// Returns an iterator over the keys in the map.
103    ///
104    /// Keys are returned in an arbitrary but fixed order.
105    pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
106        Keys { iter: self.entries() }
107    }
108
109    /// Returns an iterator over the values in the map.
110    ///
111    /// Values are returned in an arbitrary but fixed order.
112    pub fn values<'a>(&'a self) -> Values<'a, K, V> {
113        Values { iter: self.entries() }
114    }
115}
116
117impl<'a, K, V> IntoIterator for &'a Map<K, V> {
118    type Item = (&'a K, &'a V);
119    type IntoIter = Entries<'a, K, V>;
120
121    fn into_iter(self) -> Entries<'a, K, V> {
122        self.entries()
123    }
124}
125
126/// An iterator over the key/value pairs in a `Map`.
127pub struct Entries<'a, K: 'a, V: 'a> {
128    iter: slice::Iter<'a, (K, V)>,
129}
130
131impl<'a, K, V> Iterator for Entries<'a, K, V> {
132    type Item = (&'a K, &'a V);
133
134    fn next(&mut self) -> Option<(&'a K, &'a V)> {
135        self.iter.next().map(|&(ref k, ref v)| (k, v))
136    }
137
138    fn size_hint(&self) -> (usize, Option<usize>) {
139        self.iter.size_hint()
140    }
141}
142
143impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> {
144    fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
145        self.iter.next_back().map(|e| (&e.0, &e.1))
146    }
147}
148
149impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {}
150
151/// An iterator over the keys in a `Map`.
152pub struct Keys<'a, K: 'a, V: 'a> {
153    iter: Entries<'a, K, V>,
154}
155
156impl<'a, K, V> Iterator for Keys<'a, K, V> {
157    type Item = &'a K;
158
159    fn next(&mut self) -> Option<&'a K> {
160        self.iter.next().map(|e| e.0)
161    }
162
163    fn size_hint(&self) -> (usize, Option<usize>) {
164        self.iter.size_hint()
165    }
166}
167
168impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
169    fn next_back(&mut self) -> Option<&'a K> {
170        self.iter.next_back().map(|e| e.0)
171    }
172}
173
174impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {}
175
176/// An iterator over the values in a `Map`.
177pub struct Values<'a, K: 'a, V: 'a> {
178    iter: Entries<'a, K, V>,
179}
180
181impl<'a, K, V> Iterator for Values<'a, K, V> {
182    type Item = &'a V;
183
184    fn next(&mut self) -> Option<&'a V> {
185        self.iter.next().map(|e| e.1)
186    }
187
188    fn size_hint(&self) -> (usize, Option<usize>) {
189        self.iter.size_hint()
190    }
191}
192
193impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
194    fn next_back(&mut self) -> Option<&'a V> {
195        self.iter.next_back().map(|e| e.1)
196    }
197}
198
199impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {}