/
lib.rs
189 lines (165 loc) · 4.8 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! 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.
#![doc(html_root_url="https://sfackler.github.io/doc")]
#![warn(missing_docs)]
#![feature(macro_rules, tuple_indexing, phase, globs)]
#![no_std]
#[phase(plugin, link)]
extern crate core;
extern crate collections;
use core::fmt;
use core::prelude::*;
use collections::Map as MapTrait;
use collections::Set as SetTrait;
pub use shared::PhfHash;
pub use map::Map;
pub use set::Set;
pub use ordered_map::OrderedMap;
#[path="../../shared/mod.rs"]
mod shared;
pub mod map;
pub mod set;
pub mod ordered_map;
mod std {
pub use core::fmt;
}
/// An order-preserving immutable set constructed at compile time.
///
/// Unlike a `PhfSet`, iteration order is guaranteed to match the definition
/// order.
///
/// `PhfOrderedSet`s may be created with the `phf_ordered_set` macro:
///
/// ```rust
/// # #![feature(phase)]
/// extern crate phf;
/// #[phase(plugin)]
/// extern crate phf_mac;
///
/// use phf::PhfOrderedSet;
///
/// static MY_SET: PhfOrderedSet<&'static str> = phf_ordered_set! {
/// "hello",
/// "world",
/// };
///
/// # fn main() {}
/// ```
///
/// # Note
///
/// The fields of this struct are public so that they may be initialized by the
/// `phf_ordered_set` macro. They are subject to change at any time and should
/// never be accessed directly.
pub struct PhfOrderedSet<T:'static> {
#[doc(hidden)]
pub map: OrderedMap<T, ()>,
}
impl<T> fmt::Show for PhfOrderedSet<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> Collection for PhfOrderedSet<T> {
#[inline]
fn len(&self) -> uint {
self.map.len()
}
}
impl<T> SetTrait<T> for PhfOrderedSet<T> where T: PhfHash+Eq {
#[inline]
fn contains(&self, value: &T) -> bool {
self.map.contains_key(value)
}
#[inline]
fn is_disjoint(&self, other: &PhfOrderedSet<T>) -> bool {
!self.iter().any(|value| other.contains(value))
}
#[inline]
fn is_subset(&self, other: &PhfOrderedSet<T>) -> bool {
self.iter().all(|value| other.contains(value))
}
}
impl<T: PhfHash+Eq> PhfOrderedSet<T> {
/// Returns a reference to the set's internal static instance of the given
/// key.
///
/// This can be useful for interning schemes.
#[inline]
pub fn find_key(&self, key: &T) -> Option<&T> {
self.map.find_key(key)
}
/// Returns the index of the key within the list used to initialize
/// the ordered set.
pub fn find_index(&self, key: &T) -> Option<uint> {
self.map.find_index(key)
}
}
impl<T> PhfOrderedSet<T> {
/// Like `contains`, but can operate on any type that is equivalent to a
/// value
#[inline]
pub fn contains_equiv<Sized? U>(&self, key: &U) -> bool where U: PhfHash+Equiv<T> {
self.map.find_equiv(key).is_some()
}
/// Like `find_key`, but can operate on any type that is equivalent to a
/// value
#[inline]
pub fn find_key_equiv<Sized? U>(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv<T> {
self.map.find_key_equiv(key)
}
/// Like `find_index`, but can operate on any type that is equivalent to a
/// key.
pub fn find_index_equiv<Sized? U>(&self, key: &U) -> Option<uint> where U: PhfHash+Equiv<T> {
self.map.find_index_equiv(key)
}
/// Returns an iterator over the values in the set.
///
/// Values are returned in the same order in which they were defined.
#[inline]
pub fn iter<'a>(&'a self) -> PhfOrderedSetValues<'a, T> {
PhfOrderedSetValues { iter: self.map.keys() }
}
}
/// An iterator over the values in a `PhfOrderedSet`.
pub struct PhfOrderedSetValues<'a, T:'a> {
iter: ordered_map::Keys<'a, T, ()>,
}
impl<'a, T> Iterator<&'a T> for PhfOrderedSetValues<'a, T> {
#[inline]
fn next(&mut self) -> Option<&'a T> {
self.iter.next()
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
self.iter.size_hint()
}
}
impl<'a, T> DoubleEndedIterator<&'a T> for PhfOrderedSetValues<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<&'a T> {
self.iter.next_back()
}
}
impl<'a, T> RandomAccessIterator<&'a T> for PhfOrderedSetValues<'a, T> {
#[inline]
fn indexable(&self) -> uint {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<&'a T> {
self.iter.idx(index)
}
}
impl<'a, T> ExactSize<&'a T> for PhfOrderedSetValues<'a, T> {}