2011-07-05 04:48:19 -05:00
|
|
|
// 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.
|
2011-12-13 18:25:51 -06:00
|
|
|
import std::map;
|
2011-09-12 18:13:28 -05:00
|
|
|
import std::map::{hashmap, hashfn, eqfn};
|
2012-08-14 18:54:13 -05:00
|
|
|
import dvec::{DVec, dvec};
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2012-07-17 13:22:11 -05:00
|
|
|
type hash_interner<T: const> =
|
2011-08-12 09:15:18 -05:00
|
|
|
{map: hashmap<T, uint>,
|
2012-08-14 18:54:13 -05:00
|
|
|
vect: DVec<T>,
|
2011-08-12 09:15:18 -05:00
|
|
|
hasher: hashfn<T>,
|
|
|
|
eqer: eqfn<T>};
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2012-08-02 17:42:56 -05:00
|
|
|
fn mk<T: const copy>(+hasher: hashfn<T>, +eqer: eqfn<T>) -> interner<T> {
|
|
|
|
let m = map::hashmap::<T, uint>(copy hasher, copy eqer);
|
2012-07-17 13:22:11 -05:00
|
|
|
let hi: hash_interner<T> =
|
|
|
|
{map: m, vect: dvec(), hasher: hasher, eqer: eqer};
|
2012-08-01 19:30:05 -05:00
|
|
|
return hi as interner::<T>;
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
2012-07-17 13:22:11 -05:00
|
|
|
/* when traits can extend traits, we should extend index<uint,T> to get [] */
|
2012-07-31 12:27:51 -05:00
|
|
|
trait interner<T: const copy> {
|
2012-07-17 13:22:11 -05:00
|
|
|
fn intern(T) -> uint;
|
|
|
|
pure fn get(uint) -> T;
|
|
|
|
fn len() -> uint;
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
2012-08-07 20:10:06 -05:00
|
|
|
impl <T: const copy> hash_interner<T>: interner<T> {
|
2012-07-17 13:22:11 -05:00
|
|
|
fn intern(val: T) -> uint {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.map.find(val) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(idx) => return idx,
|
|
|
|
none => {
|
2012-07-17 13:22:11 -05:00
|
|
|
let new_idx = self.vect.len();
|
|
|
|
self.map.insert(val, new_idx);
|
|
|
|
self.vect.push(val);
|
2012-08-01 19:30:05 -05:00
|
|
|
return new_idx;
|
2012-07-17 13:22:11 -05:00
|
|
|
}
|
|
|
|
}
|
2011-09-24 18:33:26 -05:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
2012-07-17 13:22:11 -05:00
|
|
|
// 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.
|
|
|
|
pure fn get(idx: uint) -> T { self.vect.get_elt(idx) }
|
2012-07-17 19:05:38 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
fn len() -> uint { return self.vect.len(); }
|
2012-07-17 13:22:11 -05:00
|
|
|
}
|