2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
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.
|
2013-01-08 13:12:16 -06:00
|
|
|
use std::map;
|
2013-01-08 21:29:16 -06:00
|
|
|
use std::map::HashMap;
|
|
|
|
use dvec::DVec;
|
|
|
|
use cmp::Eq;
|
|
|
|
use hash::Hash;
|
|
|
|
use to_bytes::IterBytes;
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2012-09-07 16:52:28 -05:00
|
|
|
type hash_interner<T: Const> =
|
2012-09-10 17:38:28 -05:00
|
|
|
{map: HashMap<T, uint>,
|
2012-09-07 19:24:02 -05:00
|
|
|
vect: DVec<T>};
|
2011-07-05 04:48:19 -05:00
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
fn mk<T:Eq IterBytes Hash Const Copy>() -> Interner<T> {
|
2012-09-10 17:38:28 -05:00
|
|
|
let m = map::HashMap::<T, uint>();
|
2012-07-17 13:22:11 -05:00
|
|
|
let hi: hash_interner<T> =
|
2012-09-07 19:24:02 -05:00
|
|
|
{map: m, vect: DVec()};
|
2012-10-15 16:56:42 -05:00
|
|
|
move ((move hi) as Interner::<T>)
|
2011-07-05 04:48:19 -05:00
|
|
|
}
|
2011-08-04 12:46:10 -05:00
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
fn mk_prefill<T:Eq IterBytes Hash Const Copy>(init: ~[T]) -> Interner<T> {
|
2012-09-07 19:24:02 -05:00
|
|
|
let rv = mk();
|
2012-09-19 18:55:01 -05:00
|
|
|
for init.each() |v| { rv.intern(*v); }
|
2012-08-02 17:34:13 -05:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-17 13:22:11 -05:00
|
|
|
/* when traits can extend traits, we should extend index<uint,T> to get [] */
|
2012-10-15 16:56:42 -05:00
|
|
|
trait Interner<T:Eq IterBytes Hash Const Copy> {
|
2012-07-17 13:22:11 -05:00
|
|
|
fn intern(T) -> uint;
|
2012-07-18 18:18:02 -05:00
|
|
|
fn gensym(T) -> uint;
|
2012-07-17 13:22:11 -05:00
|
|
|
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-10-15 16:56:42 -05:00
|
|
|
impl <T:Eq IterBytes Hash 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-20 14:23:37 -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
|
|
|
}
|
2012-07-18 18:18:02 -05:00
|
|
|
fn gensym(val: T) -> uint {
|
|
|
|
let new_idx = self.vect.len();
|
|
|
|
// leave out of .map to avoid colliding
|
|
|
|
self.vect.push(val);
|
|
|
|
return new_idx;
|
|
|
|
}
|
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-09-19 11:41:06 -05:00
|
|
|
}
|