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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use std::hash::Hash;
use std::borrow::{Borrow, ToOwned};
#[cfg(debug_assertions)] use std::sync::atomic::{self, AtomicUsize, Ordering};
use traits::{Intern, Resolve, ResolveUnchecked, Len, SymbolId};
use {core, Result, ErrorKind};
use sym::{Symbol as ISymbol, Pool as IPool};
#[cfg(debug_assertions)]
static NEXT_POOL_ID: AtomicUsize = atomic::ATOMIC_USIZE_INIT;
#[cfg(feature = "fnv")]
type HashMap<K, V> = ::fnv::FnvHashMap<K, V>;
#[cfg(not(feature = "fnv"))]
type HashMap<K, V> = ::std::collections::HashMap<K, V>;
make_sym! {
pub Sym<I>:
"Symbol type used by [`Pool`](struct.Pool.html)'s [`Intern`](../traits/trait.Intern.html) and [`Resolve`](../traits/trait.Resolve.html) implementations.";
}
#[derive(Debug)]
pub struct Pool<T: ?Sized, I = usize>
where T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash,
I: SymbolId
{
ids_map: HashMap<u64, I>,
lookup_vec: Vec<T::Owned>,
#[cfg(debug_assertions)]
pool_id: usize
}
impl<T: ?Sized, I> Clone for Pool<T, I>
where T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash + Clone,
I: SymbolId,
{
#[cfg(debug_assertions)]
fn clone(&self) -> Self {
Pool{ids_map: self.ids_map.clone(),
lookup_vec: self.lookup_vec.clone(),
pool_id: self.pool_id}
}
#[cfg(not(debug_assertions))]
fn clone(&self) -> Self {
Pool{ids_map: self.ids_map.clone(),
lookup_vec: self.lookup_vec.clone()}
}
}
impl<T: ?Sized, I> Pool<T, I>
where T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash,
I: SymbolId
{
pub fn new() -> Self {
Default::default()
}
}
impl<'a, T: ?Sized, I> Len for Pool<T, I>
where T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash,
I: SymbolId
{
fn len(&self) -> usize {
self.lookup_vec.len()
}
fn is_empty(&self) -> bool {
self.lookup_vec.is_empty()
}
fn is_full(&self) -> bool {
let len = self.len();
len >= 1 && len - 1 >= I::max_value().to_usize().expect("Unexpected failure to convert index type `max_value()` result to usize")
}
}
impl<'a, T: ?Sized, I> ::sym::Pool for Pool<T, I>
where T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash,
I: SymbolId
{
type Symbol = Sym<I>;
#[cfg(debug_assertions)]
fn id(&self) -> ::sym::PoolId {
self.pool_id
}
#[cfg(not(debug_assertions))]
fn create_symbol(&self, id: <Self::Symbol as ::sym::Symbol>::Id) -> Self::Symbol {
Sym::create(id)
}
#[cfg(debug_assertions)]
fn create_symbol(&self, id: <Self::Symbol as ::sym::Symbol>::Id) -> Self::Symbol {
Sym::create(id, self.id())
}
}
impl<T: ?Sized, I> Default for Pool<T, I>
where T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash,
I: SymbolId
{
#[cfg(not(debug_assertions))]
fn default() -> Self {
Pool{ids_map: Default::default(),
lookup_vec: Default::default()}
}
#[cfg(debug_assertions)]
fn default() -> Self {
Pool{ids_map: Default::default(),
lookup_vec: Default::default(),
pool_id: NEXT_POOL_ID.fetch_add(1, Ordering::SeqCst)}
}
}
impl<'a, T: ?Sized, I> Intern for &'a mut Pool<T, I>
where I: SymbolId,
T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash + Borrow<T>,
{
type Input = T;
type Symbol = Sym<I>;
fn intern(mut self, value: &Self::Input) -> Result<Self::Symbol> {
let key = core::hash::<T, core::DefaultHashAlgo>(value);
if let Some(&id) = self.ids_map.get(&key) {
return Ok(self.create_symbol(id))
} else if self.is_full() {
return Err(ErrorKind::PoolOverflow.into())
} else {
self.lookup_vec.push(value.to_owned());
let id = I::from_usize(self.lookup_vec.len() - 1)
.expect("Unexpected failure to convert symbol ID from usize");
self.ids_map.insert(key, id);
Ok(self.create_symbol(id))
}
}
}
#[cfg(debug_assertions)]
macro_rules! check_matching_pool {
($slf: ident, $sym: ident) => {
if $sym.pool_id() != $slf.id() {
panic!(concat!("\nDetected an invalid attempt to resolve a symbol on a pool that did not\n",
"create it. This is a bug in the program or library using Symtern; do not\n",
"report it to the Symtern developers."));
}
};
}
#[cfg(not(debug_assertions))]
macro_rules! check_matching_pool {
($slf: ident, $sym: ident) => {};
}
impl<'a,T: ?Sized, I> Resolve for &'a Pool<T, I>
where T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash + Borrow<T>,
I: SymbolId
{
type Input = <&'a mut Pool<T, I> as Intern>::Symbol;
type Output = &'a T;
fn resolve(self, s: Self::Input) -> Result<Self::Output> {
check_matching_pool!(self, s);
let idx = s.id().to_usize().expect("Unexpected failure to convert symbol ID to usize");
if self.lookup_vec.len() > idx {
Ok(self.lookup_vec[idx].borrow())
} else {
Err(ErrorKind::NoSuchSymbol.into())
}
}
}
impl<'a, T: ?Sized, I> ResolveUnchecked for &'a Pool<T, I>
where T: ToOwned + Eq + Hash,
T::Owned: Eq + Hash + Borrow<T>,
I: SymbolId
{
unsafe fn resolve_unchecked(self, symbol: Self::Input) -> Self::Output {
let idx = symbol.id().to_usize().expect("Unexpected failure to convert symbol ID to usize");
self.lookup_vec.get_unchecked(idx).borrow()
}
}
#[cfg(test)]
mod tests {
use super::Pool;
use traits::*;
use ErrorKind;
#[test]
fn resolve_returns_expected_results() {
let mut p1 = Pool::<str,u16>::new();
let mut p2 = Pool::<str,u16>::new();
let s1 = p1.intern("foo").unwrap();
let s2 = p2.intern("bar").unwrap();
assert_eq!(Ok("foo"), p1.resolve(s1));
assert_eq!(Ok("bar"), p2.resolve(s2));
}
#[test]
fn has_expected_len_and_capacity() {
let mut pool = Pool::<u16,u8>::new();
assert!(pool.is_empty());
for i in 0u16..200 {
pool.intern(&i).expect("failed to intern value");
}
assert_eq!(200, pool.len());
assert!(! pool.is_full());
for i in 150u16..250 {
pool.intern(&i).expect("failed to intern value");
}
assert_eq!(250, pool.len());
assert!(! pool.is_full());
for i in 250u16..256 {
pool.intern(&i).expect("failed to intern value");
}
assert_eq!(256, pool.len());
assert!(pool.is_full());
pool.intern(&123).expect("failed to intern previously-interned value");
match pool.intern(&456) {
Ok(_) => panic!("unexpected `Ok` when interning unseen value in full pool"),
Err(e) => assert_eq!(ErrorKind::PoolOverflow, e.kind()),
}
}
}