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};
8use 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: u64,
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        let hash = phf_shared::hash(key, self.key);
84        let index = phf_shared::get_index(hash, &*self.disps, self.entries.len());
85        let entry = &self.entries[index as usize];
86        let b: &T = entry.0.borrow();
87        if b == key {
88            Some((&entry.0, &entry.1))
89        } else {
90            None
91        }
92    }
93
94    /// Returns an iterator over the key/value pairs in the map.
95    ///
96    /// Entries are returned in an arbitrary but fixed order.
97    pub fn entries<'a>(&'a self) -> Entries<'a, K, V> {
98        Entries { iter: self.entries.iter() }
99    }
100
101    /// Returns an iterator over the keys in the map.
102    ///
103    /// Keys are returned in an arbitrary but fixed order.
104    pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
105        Keys { iter: self.entries() }
106    }
107
108    /// Returns an iterator over the values in the map.
109    ///
110    /// Values are returned in an arbitrary but fixed order.
111    pub fn values<'a>(&'a self) -> Values<'a, K, V> {
112        Values { iter: self.entries() }
113    }
114}
115
116impl<'a, K, V> IntoIterator for &'a Map<K, V> {
117    type Item = (&'a K, &'a V);
118    type IntoIter = Entries<'a, K, V>;
119
120    fn into_iter(self) -> Entries<'a, K, V> {
121        self.entries()
122    }
123}
124
125/// An iterator over the key/value pairs in a `Map`.
126pub struct Entries<'a, K: 'a, V: 'a> {
127    iter: slice::Iter<'a, (K, V)>,
128}
129
130impl<'a, K, V> Iterator for Entries<'a, K, V> {
131    type Item = (&'a K, &'a V);
132
133    fn next(&mut self) -> Option<(&'a K, &'a V)> {
134        self.iter.next().map(|&(ref k, ref v)| (k, v))
135    }
136
137    fn size_hint(&self) -> (usize, Option<usize>) {
138        self.iter.size_hint()
139    }
140}
141
142impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> {
143    fn next_back(&mut self) -> Option<(&'a K, &'a V)> {
144        self.iter.next_back().map(|e| (&e.0, &e.1))
145    }
146}
147
148impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {}
149
150/// An iterator over the keys in a `Map`.
151pub struct Keys<'a, K: 'a, V: 'a> {
152    iter: Entries<'a, K, V>,
153}
154
155impl<'a, K, V> Iterator for Keys<'a, K, V> {
156    type Item = &'a K;
157
158    fn next(&mut self) -> Option<&'a K> {
159        self.iter.next().map(|e| e.0)
160    }
161
162    fn size_hint(&self) -> (usize, Option<usize>) {
163        self.iter.size_hint()
164    }
165}
166
167impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {
168    fn next_back(&mut self) -> Option<&'a K> {
169        self.iter.next_back().map(|e| e.0)
170    }
171}
172
173impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {}
174
175/// An iterator over the values in a `Map`.
176pub struct Values<'a, K: 'a, V: 'a> {
177    iter: Entries<'a, K, V>,
178}
179
180impl<'a, K, V> Iterator for Values<'a, K, V> {
181    type Item = &'a V;
182
183    fn next(&mut self) -> Option<&'a V> {
184        self.iter.next().map(|e| e.1)
185    }
186
187    fn size_hint(&self) -> (usize, Option<usize>) {
188        self.iter.size_hint()
189    }
190}
191
192impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
193    fn next_back(&mut self) -> Option<&'a V> {
194        self.iter.next_back().map(|e| e.1)
195    }
196}
197
198impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {}