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
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// An "interner" is a data structure that associates values with uint tags and
// allows bidirectional lookup; i.e. given a value, one can easily find the
// type, and vice versa.
use std::cmp::Equiv;
use std::hashmap::HashMap;
pub struct Interner<T> {
priv map: @mut HashMap<T, uint>,
priv vect: @mut ~[T],
}
// when traits can extend traits, we should extend index<uint,T> to get []
impl<T:Eq + IterBytes + Hash + Freeze + Copy> Interner<T> {
pub fn new() -> Interner<T> {
Interner {
map: @mut HashMap::new(),
vect: @mut ~[],
}
}
pub fn prefill(init: &[T]) -> Interner<T> {
let rv = Interner::new();
for init.iter().advance |v| { rv.intern(copy *v); }
rv
}
pub fn intern(&self, val: T) -> uint {
match self.map.find(&val) {
Some(&idx) => return idx,
None => (),
}
let vect = &mut *self.vect;
let new_idx = vect.len();
self.map.insert(copy val, new_idx);
vect.push(val);
new_idx
}
pub fn gensym(&self, val: T) -> uint {
let new_idx = {
let vect = &*self.vect;
vect.len()
};
// leave out of .map to avoid colliding
self.vect.push(val);
new_idx
}
pub fn get(&self, idx: uint) -> T { copy self.vect[idx] }
pub fn len(&self) -> uint { let vect = &*self.vect; vect.len() }
pub fn find_equiv<Q:Hash + IterBytes + Equiv<T>>(&self, val: &Q)
-> Option<uint> {
match self.map.find_equiv(val) {
Some(v) => Some(*v),
None => None,
}
}
}
// A StrInterner differs from Interner<String> in that it accepts
// borrowed pointers rather than @ ones, resulting in less allocation.
pub struct StrInterner {
priv map: @mut HashMap<@str, uint>,
priv vect: @mut ~[@str],
}
// when traits can extend traits, we should extend index<uint,T> to get []
impl StrInterner {
pub fn new() -> StrInterner {
StrInterner {
map: @mut HashMap::new(),
vect: @mut ~[],
}
}
pub fn prefill(init: &[&str]) -> StrInterner {
let rv = StrInterner::new();
for init.iter().advance |&v| { rv.intern(v); }
rv
}
pub fn intern(&self, val: &str) -> uint {
match self.map.find_equiv(&val) {
Some(&idx) => return idx,
None => (),
}
let new_idx = self.len();
let val = val.to_managed();
self.map.insert(val, new_idx);
self.vect.push(val);
new_idx
}
pub fn gensym(&self, val: &str) -> uint {
let new_idx = self.len();
// leave out of .map to avoid colliding
self.vect.push(val.to_managed());
new_idx
}
// this isn't "pure" in the traditional sense, because it can go from
// failing to returning a value as items are interned. But for typestate,
// where we first check a pred and then rely on it, ceasing to fail is ok.
pub fn get(&self, idx: uint) -> @str { self.vect[idx] }
pub fn len(&self) -> uint { let vect = &*self.vect; vect.len() }
pub fn find_equiv<Q:Hash + IterBytes + Equiv<@str>>(&self, val: &Q)
-> Option<uint> {
match self.map.find_equiv(val) {
Some(v) => Some(*v),
None => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_fail]
fn i1 () {
let i : Interner<@str> = Interner::new();
i.get(13);
}
#[test]
fn i2 () {
let i : Interner<@str> = Interner::new();
// first one is zero:
assert_eq!(i.intern (@"dog"), 0);
// re-use gets the same entry:
assert_eq!(i.intern (@"dog"), 0);
// different string gets a different #:
assert_eq!(i.intern (@"cat"), 1);
assert_eq!(i.intern (@"cat"), 1);
// dog is still at zero
assert_eq!(i.intern (@"dog"), 0);
// gensym gets 3
assert_eq!(i.gensym (@"zebra" ), 2);
// gensym of same string gets new number :
assert_eq!(i.gensym (@"zebra" ), 3);
// gensym of *existing* string gets new number:
assert_eq!(i.gensym (@"dog"), 4);
assert_eq!(i.get(0), @"dog");
assert_eq!(i.get(1), @"cat");
assert_eq!(i.get(2), @"zebra");
assert_eq!(i.get(3), @"zebra");
assert_eq!(i.get(4), @"dog");
}
#[test]
fn i3 () {
let i : Interner<@str> = Interner::prefill([@"Alan",@"Bob",@"Carol"]);
assert_eq!(i.get(0), @"Alan");
assert_eq!(i.get(1), @"Bob");
assert_eq!(i.get(2), @"Carol");
assert_eq!(i.intern(@"Bob"), 1);
}
}
|