symbol_map/
table.rs

1use std::cmp::{Eq, Ord, Ordering, PartialEq};
2use std::collections::HashMap;
3use std::default::Default;
4use std::fmt;
5use std::hash::{Hash, Hasher};
6use std::iter::Iterator;
7use std::mem;
8
9/// A table entry that associates an instance of `T` with an atomic symbol.
10///
11/// Types `T` should not be mutated by any means once they are associated with a
12/// `SymbolId` and stored in a `Table`. Doing so may invalidate any caching or
13/// indexing that is done on top of the table.
14#[derive(Debug)]
15pub struct Symbol<T, D> where D: SymbolId {
16    id: D,
17    data: T,
18    next: Option<Box<Symbol<T, D>>>,
19}
20
21impl<T, D> Symbol<T, D> where D: SymbolId {
22    /// Returns the symbol's ID.
23    pub fn id(&self) -> &D {
24        &self.id
25    }
26
27    /// Returns a reference to the symbol's data.
28    ///
29    /// A `Symbol<T>` that is owned by a `Table` does not move in memory as long
30    /// as it is not dropped from the table. As a result, you may retain a raw
31    /// pointer to this data and dereference it as long as its parent
32    /// `Symbol<T>` is not dropped.
33    pub fn data(&self) -> &T {
34        &self.data
35    }
36}
37
38impl<T, D> Hash for Symbol<T, D> where T: Hash, D: SymbolId {
39    fn hash<H>(&self, state: &mut H) where H: Hasher {
40        self.data.hash(state)
41    }
42}
43
44impl<T, D> PartialEq for Symbol<T, D> where T: PartialEq, D: SymbolId {
45    fn eq(&self, other: &Self) -> bool {
46        self.data.eq(&other.data)
47    }
48}
49
50impl<T, D> Eq for Symbol<T, D> where T: Eq, D: SymbolId { }
51
52impl<T, D> PartialOrd for Symbol<T, D> where T: PartialOrd, D: SymbolId {
53    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
54        self.data.partial_cmp(&other.data)
55    }
56}
57
58impl<T, D> Ord for Symbol<T, D> where T: Ord, D: SymbolId {
59    fn cmp(&self, other: &Self) -> Ordering {
60        self.data.cmp(&other.data)
61    }
62}
63
64/// An atomic ID.
65pub trait SymbolId:
66Copy + Clone + fmt::Debug + Default + Eq + Hash + Ord + PartialEq + PartialOrd + Send + Sync {
67    /// Returns the ID immediately subsequent to this one.
68    fn next(&self) -> Self;
69
70    /// Casts the ID to a `usize`.
71    fn as_usize(&self) -> usize;
72}
73
74impl SymbolId for usize {
75    fn next(&self) -> Self { *self + 1 }
76    fn as_usize(&self) -> usize { *self }
77}
78
79impl SymbolId for u8 {
80    fn next(&self) -> Self { *self + 1 }
81
82    fn as_usize(&self) -> usize { *self as usize }
83}
84
85impl SymbolId for u16 {
86    fn next(&self) -> Self { *self + 1 }
87    fn as_usize(&self) -> usize { *self as usize }
88}
89
90impl SymbolId for u32 {
91    fn next(&self) -> Self { *self + 1 }
92    fn as_usize(&self) -> usize { *self as usize }
93}
94
95impl SymbolId for u64 {
96    fn next(&self) -> Self { *self + 1 }
97    fn as_usize(&self) -> usize { *self as usize }
98}
99
100/// The head of a linked list associating `T`s with `SymbolId`s. `SymbolId`
101/// values start at 0 and increase by 1 for each `T` added to the table.
102///
103/// The linked list owns instances of `Symbol<T>`, which wrap around a `T` and a
104/// `SymbolId`. It satisfies the contract: *once allocated, a Symbol<T>'s
105/// address does not change as long as its parent table exists and it is not
106/// dropped from the table*.
107///
108/// As a result, a table index may retain a raw pointer to a `Symbol<T>` as long
109/// as care is taken not to dereference or otherwise make use of such pointers
110/// after the symbol they point to has been dropped by `retain()`.
111#[derive(Debug)]
112pub struct Table<T, D> where D: SymbolId {
113    head: Option<Box<Symbol<T, D>>>,
114    next_id: D,
115}
116
117impl<T, D> Table<T, D> where D: SymbolId {
118    /// Creates a new, empty table.
119    pub fn new() -> Self {
120        Table {
121            head: None,
122            next_id: Default::default(),
123        }
124    }
125
126    /// Returns the number of symbols in the table.
127    pub fn len(&self) -> usize {
128        self.next_id.as_usize()
129    }
130
131    /// Inserts `value` into the table and assigns it an id. The same value may
132    /// be inserted more than once. To prevent such operations, use the
133    /// `get_or_insert()` method of `Indexing`.
134    ///
135    /// Returns a reference to the newly created symbol.
136    pub fn insert(&mut self, value: T) -> &Symbol<T, D> {
137        let next_id = self.next_id;
138        self.next_id = self.next_id.next();
139        let mut new_head = Box::new(Symbol {
140            id: next_id,
141            data: value,
142            next: None,
143        });
144        mem::swap(&mut self.head, &mut new_head.next);
145        self.head = Some(new_head);
146        (&self.head).as_ref().unwrap()
147    }
148
149    /// Remaps associations between `T`s and `D`s, selectively dropping some
150    /// associations entirely. The addresses of `Symbol<T>`s for entries which
151    /// are retained do not change.
152    ///
153    /// `(T, D)` associations for which `f` returns `Some(d)` will be remapped
154    /// to use `d`.
155    ///
156    /// `(T, D)` associations for which `f` returns `None` will be dropped.
157    ///
158    /// It is the responsibility of the caller to maintain the following:
159    ///
160    /// - The final mapping should be a dense range of whole numbers starting at 0.
161    ///
162    /// - No two different `T`s are associated with the same `D`.
163    pub fn remap<F>(&mut self, mut f: F) where F: FnMut(&Symbol<T, D>) -> Option<D> {
164        // Destructively walk linked list, selectively moving boxed symbols into
165        // a new list and reassigning `SymbolId`s as we go. This is done in
166        // place, without making new allocations for the elements that we
167        // retain.
168        let mut remapped = Table::new();
169        let mut head = None;
170        mem::swap(&mut head, &mut self.head);
171        loop {
172            head = match head {
173                None => break,
174                Some(mut symbol) =>
175                    if let Some(new_state_id) = f(&symbol) {
176                        let mut next_head = None;
177                        mem::swap(&mut next_head, &mut symbol.next);
178                        symbol.id = new_state_id;
179                        remapped.emplace_head(symbol);
180                        remapped.next_id = remapped.next_id.next();
181                        next_head
182                    } else {
183                        symbol.next
184                    },
185            }
186        }
187        mem::swap(&mut remapped, self);
188    }
189
190    pub fn into_iter(self) -> TableIntoIter<T, D> {
191        TableIntoIter {
192            remaining: self.len(),
193            item: self.head,
194        }
195    }
196
197    /// Returns an iterator over table entries.
198    pub fn iter<'s>(&'s self) -> TableIter<'s, T, D> {
199        TableIter {
200            remaining: self.len(),
201            item: (&self.head).as_ref(),
202        }
203    }
204
205    /// Sets `value` as the head of this list. If `value` is already the head of
206    /// another list, its subsequent list elements are dropped.
207    fn emplace_head(&mut self, mut value: Box<Symbol<T, D>>) {
208        mem::swap(&mut value.next, &mut self.head);
209        mem::swap(&mut self.head, &mut Some(value));
210    }
211}
212
213impl<T, D> Table<T, D> where T: Eq + Hash, D: SymbolId {
214    /// Converts `self` to a `HashMap` holding the same associations as
215    /// `self`. If the same key occurs in `self` more than once, then duplicate
216    /// occurrences will be dropped arbitrarily.
217    pub fn to_hash_map(mut self) -> HashMap<T, D> {
218        let mut map = HashMap::with_capacity(self.len());
219        loop {
220            self.head = match self.head {
221                None => break,
222                Some(mut symbol) => {
223                    let id = symbol.id().clone();
224                    let mut next_head = None;
225                    mem::swap(&mut next_head, &mut symbol.next);
226                    map.insert(symbol.data, id);
227                    next_head
228                },
229            }
230        }
231        map
232    }
233}
234
235impl<'a, T, D> IntoIterator for &'a Table<T, D> where T: 'a, D: 'a + SymbolId {
236    type Item = &'a Symbol<T, D>;
237    type IntoIter = TableIter<'a, T, D>;
238
239    fn into_iter(self) -> Self::IntoIter {
240        self.iter()
241    }
242}
243
244impl<T, D> IntoIterator for Table<T, D> where D: SymbolId {
245    type Item = Box<Symbol<T, D>>;
246    type IntoIter = TableIntoIter<T, D>;
247
248    fn into_iter(self) -> Self::IntoIter {
249        self.into_iter()
250    }
251}
252
253/// Iterator over table contents.
254#[derive(Debug)]
255pub struct TableIter<'a, T, D> where T: 'a, D: 'a + SymbolId {
256    remaining: usize,
257    item: Option<&'a Box<Symbol<T, D>>>,
258}
259
260impl<'a, T, D> Iterator for TableIter<'a, T, D> where T: 'a, D: 'a + SymbolId {
261    type Item = &'a Symbol<T, D>;
262
263    fn next(&mut self) -> Option<&'a Symbol<T, D>> {
264        let mut item = None;
265        mem::swap(&mut item, &mut self.item);
266        match item {
267            None => None,
268            Some(symbol) => {
269                self.remaining -= 1;
270                self.item = symbol.next.as_ref();
271                Some(symbol)
272            },
273        }
274    }
275
276    fn size_hint(&self) -> (usize, Option<usize>) {
277        (self.remaining, Some(self.remaining))
278    }
279}
280
281/// Iterator that consumes a table.
282#[derive(Debug)]
283pub struct TableIntoIter<T, D> where D: SymbolId {
284    remaining: usize,
285    item: Option<Box<Symbol<T, D>>>,
286}
287
288impl<T, D> Iterator for TableIntoIter<T, D> where D: SymbolId {
289    type Item = Box<Symbol<T, D>>;
290
291    fn next(&mut self) -> Option<Box<Symbol<T, D>>> {
292        let mut item = None;
293        mem::swap(&mut item, &mut self.item);
294        match item {
295            None => None,
296            Some(mut symbol) => {
297                self.remaining -= 1;
298                mem::swap(&mut self.item, &mut symbol.next);
299                Some(symbol)
300            },
301        }
302    }
303
304    fn size_hint(&self) -> (usize, Option<usize>) {
305        (self.remaining, Some(self.remaining))
306    }
307}
308
309#[cfg(test)]
310mod test {
311    use super::{Symbol, SymbolId, Table};
312
313    use std::collections::HashMap;
314    use std::default::Default;
315
316    const VALUES: &'static [usize] = &[101, 203, 500, 30, 0, 1];
317
318    #[test]
319    fn symbol_id_ok() {
320        let id: usize = Default::default();
321        assert_eq!(id.as_usize(), 0);
322        assert_eq!(id.next().as_usize(), 1);
323        assert_eq!(id.next().next().as_usize(), 2);
324        assert_eq!(id.as_usize(), 0);
325    }
326
327    #[test]
328    fn new_table_empty_ok() {
329        let t = Table::<usize, usize>::new();
330        assert!(t.head.is_none());
331        assert!(t.next_id == 0);
332        assert_eq!(t.len(), 0);
333    }
334
335    #[test]
336    fn table_insert_ok() {
337        let mut t = Table::<usize, usize>::new();
338        for (i, v) in VALUES.iter().enumerate() {
339            t.insert(*v);
340            assert_eq!(t.len(), i + 1);
341            assert_eq!(t.next_id.as_usize(), i + 1);
342            assert_eq!(t.head.as_ref().map(|x| x.data), Some(*v));
343        }
344        assert_eq!(t.len(), VALUES.len());
345        assert_eq!(t.next_id.as_usize(), VALUES.len());
346
347        let mut x = t.head.as_ref();
348        let mut count = 0;
349        let mut vs = VALUES.iter().rev().enumerate();
350        loop {
351            x = match x {
352                None => break,
353                Some(symbol) => {
354                    let (i, v) = vs.next().unwrap();
355                    assert_eq!(i, count);
356                    assert_eq!(symbol.data(), v);
357                    count += 1;
358                    symbol.next.as_ref()
359                },
360            }
361        }
362        assert_eq!(vs.next(), None);
363    }
364
365    #[test]
366    fn table_empty_iter_ok() {
367        let t = Table::<usize, usize>::new();
368        let mut i = t.iter();
369        assert_eq!(i.size_hint(), (0, Some(0)));
370        assert!(i.next().is_none());
371        assert_eq!(i.size_hint(), (0, Some(0)));
372    }
373
374    #[test]
375    fn table_iter_ok() {
376        let mut t = Table::<usize, u32>::new();
377        for v in VALUES.iter() {
378            t.insert(*v);
379        }
380        assert_eq!(t.len(), VALUES.len());
381
382        let mut i = t.iter();
383        let mut expected_len = t.len();
384        let mut vs = VALUES.iter().rev();
385        assert_eq!(i.size_hint(), (expected_len, Some(expected_len)));
386        while let Some(symbol) = i.next() {
387            expected_len -= 1;
388            assert_eq!(i.size_hint(), (expected_len, Some(expected_len)));
389            assert_eq!(Some(symbol.data()), vs.next());
390        }
391        assert_eq!(i.size_hint(), (0, Some(0)));
392    }
393
394    #[test]
395    fn moved_table_internal_address_unchanged_ok() {
396        let mut stack_table = Table::<usize, u8>::new();
397        let mut original_data_addresses = Vec::new();
398        let mut original_symbol_addresses = Vec::new();
399        for v in VALUES.iter() {
400            let symbol = stack_table.insert(*v);
401            assert_eq!(*symbol.data(), *v);
402            original_data_addresses.push(symbol.data() as *const usize);
403            original_symbol_addresses.push(symbol as *const Symbol<usize, u8>);
404        }
405
406        let heap_table = Box::new(stack_table);
407        let mut count =0;
408        for (symbol, (value, (data_address, symbol_address))) in heap_table.iter().zip(
409            VALUES.iter().rev().zip(
410                original_data_addresses.into_iter().rev().zip(
411                    original_symbol_addresses.into_iter().rev()))) {
412            assert_eq!(symbol.data(), value);
413            assert_eq!(symbol.data() as *const usize, data_address);
414            assert_eq!(symbol as *const Symbol<usize, u8>, symbol_address);
415            count += 1;
416        }
417        assert_eq!(count, VALUES.len());
418    }
419
420    #[test]
421    fn remap_empty_ok() {
422        let mut t = Table::<usize, u8>::new();
423        assert_eq!(t.len(), 0);
424        t.remap(|symbol| Some(symbol.id().clone()));
425        assert_eq!(t.len(), 0);
426    }
427
428    #[test]
429    fn remap_noop_ok() {
430        let mut t1 = Table::<usize, u8>::new();
431        for v in VALUES.iter() {
432            t1.insert(*v);
433        }
434
435        let mut t2 = Table::<usize, u8>::new();
436        for v in VALUES.iter() {
437            t2.insert(*v);
438        }
439        t2.remap(|symbol| Some(symbol.id().clone()));
440
441        assert_eq!(t2.len(), t1.len());
442        assert_eq!(t2.to_hash_map(), t1.to_hash_map());
443    }
444
445    #[test]
446    fn remap_all_ok() {
447        let mut t = Table::<usize, u8>::new();
448        for v in VALUES.iter() {
449            t.insert(*v);
450        }
451        let mut new_id = 0u8;
452        let mut expected_associations = HashMap::new();
453        t.remap(|symbol| {
454            let id = new_id;
455            new_id += 1;
456            expected_associations.insert(*symbol.data(), id);
457            Some(id)
458        });
459        assert_eq!(t.to_hash_map(), expected_associations);
460    }
461
462    #[test]
463    fn remap_some_ok() {
464        let mut t = Table::<usize, u8>::new();
465        for v in VALUES.iter() {
466            t.insert(*v);
467        }
468        let mut new_id = 0u8;
469        let mut expected_associations = HashMap::new();
470        t.remap(|symbol|
471                if symbol.id() % 2 == 0 {
472                    let id = new_id;
473                    new_id += 1;
474                    expected_associations.insert(*symbol.data(), id);
475                    Some(id)
476                } else {
477                    None
478                });
479        assert_eq!(t.to_hash_map(), expected_associations);
480    }
481
482    #[test]
483    fn remap_none_ok() {
484        let mut t = Table::<usize, u8>::new();
485        for v in VALUES.iter() {
486            t.insert(*v);
487        }
488        t.remap(|_| None);
489        assert_eq!(t.len(), 0);
490    }
491
492    #[test]
493    fn table_empty_into_iter_ok() {
494        let t = Table::<usize, u8>::new();
495        assert!(t.into_iter().next().is_none());
496    }
497
498    #[test]
499    fn table_into_iter_ok() {
500        let mut t = Table::<usize, u32>::new();
501        for v in VALUES.iter() {
502            t.insert(*v);
503        }
504        assert_eq!(t.len(), VALUES.len());
505
506        let mut expected_len = t.len();
507        let mut i = t.into_iter();
508        let mut vs = VALUES.iter().rev();
509        assert_eq!(i.size_hint(), (expected_len, Some(expected_len)));
510        while let Some(symbol) = i.next() {
511            expected_len -= 1;
512            assert_eq!(i.size_hint(), (expected_len, Some(expected_len)));
513            assert_eq!(Some(symbol.data()), vs.next());
514        }
515        assert_eq!(i.size_hint(), (0, Some(0)));
516    }
517}