/
set.rs
147 lines (128 loc) · 3.62 KB
/
set.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
//! An immutable set constructed at compile time.
use core::prelude::*;
use Map;
use core::fmt;
use shared::PhfHash;
use map;
/// An immutable set constructed at compile time.
///
/// `Set`s may be created with the `phf_set` macro:
///
/// ```rust
/// # #![feature(phase)]
/// extern crate phf;
/// #[phase(plugin)]
/// extern crate phf_mac;
///
/// static MY_SET: phf::Set<&'static str> = phf_set! {
/// "hello",
/// "world",
/// };
///
/// # fn main() {}
/// ```
///
/// ## Note
///
/// The fields of this struct are public so that they may be initialized by the
/// `phf_set` macro. 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::Show for Set<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> Set<T> where T: PhfHash+Eq {
/// 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 true if `value` is in the `Set`.
#[inline]
pub fn contains(&self, value: &T) -> bool {
self.map.contains_key(value)
}
/// Returns true if `other` shares no elements with `self`.
#[inline]
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`.
#[inline]
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`.
#[inline]
pub fn is_superset(&self, other: &Set<T>) -> bool {
other.is_subset(self)
}
}
impl<T> Set<T> {
/// Returns the number of elements in the `Set`.
#[inline]
pub fn len(&self) -> uint {
self.map.len()
}
/// Returns true if the `Set` contains no elements.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// 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)
}
}
impl<T> Set<T> {
/// Returns an iterator over the values in the set.
///
/// Values are returned in an arbitrary but fixed order.
#[inline]
pub fn iter<'a>(&'a self) -> Items<'a, T> {
Items { iter: self.map.keys() }
}
}
/// An iterator over the values in a `Set`.
pub struct Items<'a, T:'static> {
iter: map::Keys<'a, T, ()>,
}
impl<'a, T> Iterator<&'a T> for Items<'a, T> {
fn next(&mut self) -> Option<&'a T> {
self.iter.next()
}
fn size_hint(&self) -> (uint, Option<uint>) {
self.iter.size_hint()
}
}
impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
fn next_back(&mut self) -> Option<&'a T> {
self.iter.next_back()
}
}
impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}