diff options
| author | Kevin Cantu <me@kevincantu.org> | 2012-05-29 21:35:12 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-05-31 11:15:00 -0700 |
| commit | 7dcbaedd329295e1f2692bb2d9eae860a820d0a8 (patch) | |
| tree | be0272a0ec2e259da231f9147aaf1dc09b5758c5 /src/libsyntax/util | |
| parent | ff6cde788229484e128849d67e4f32b178b18c84 (diff) | |
| download | rust-7dcbaedd329295e1f2692bb2d9eae860a820d0a8.tar.gz rust-7dcbaedd329295e1f2692bb2d9eae860a820d0a8.zip | |
Rename librustsyntax to libsyntax
Per issue #2418.
Diffstat (limited to 'src/libsyntax/util')
| -rw-r--r-- | src/libsyntax/util/interner.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs new file mode 100644 index 00000000000..89078bfaa36 --- /dev/null +++ b/src/libsyntax/util/interner.rs @@ -0,0 +1,40 @@ +// 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. +import std::map; +import std::map::{hashmap, hashfn, eqfn}; +import dvec::{dvec, extensions}; + +type interner<T> = + {map: hashmap<T, uint>, + vect: dvec<T>, + hasher: hashfn<T>, + eqer: eqfn<T>}; + +fn mk<T: copy>(hasher: hashfn<T>, eqer: eqfn<T>) -> interner<T> { + let m = map::hashmap::<T, uint>(hasher, eqer); + ret {map: m, vect: dvec(), hasher: hasher, eqer: eqer}; +} + +fn intern<T: copy>(itr: interner<T>, val: T) -> uint { + alt itr.map.find(val) { + some(idx) { ret idx; } + none { + let new_idx = itr.vect.len(); + itr.map.insert(val, new_idx); + itr.vect.push(val); + ret new_idx; + } + } +} + +// |get| 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<T: copy>(itr: interner<T>, idx: uint) -> T { + unchecked { + itr.vect.get_elt(idx) + } +} + +fn len<T>(itr: interner<T>) -> uint { ret itr.vect.len(); } |
