rustc: Move the interner to a new module intended to be used for general data structures

This commit is contained in:
Patrick Walton 2011-05-20 13:57:09 -07:00
parent 02b995f428
commit 7e43a31f08
7 changed files with 52 additions and 50 deletions

View File

@ -10,7 +10,7 @@
import driver::session::session;
import util::common;
import util::common::new_str_hash;
import util::interner;
import util::data::interner;
state type reader = state obj {
fn is_eof() -> bool;
@ -800,7 +800,7 @@ fn read_block_comment(&reader rdr) -> cmnt {
fn gather_comments(session sess, str path) -> vec[cmnt] {
auto srdr = io::file_reader(path);
auto itr = @interner::mk_interner[str](str::hash, str::eq);
auto itr = @interner::mk[str](str::hash, str::eq);
auto rdr = new_reader(sess, srdr, codemap::new_filemap(path, 0u), itr);
let vec[cmnt] comments = [];
while (!rdr.is_eof()) {

View File

@ -11,7 +11,7 @@
import util::common::filename;
import util::common::span;
import util::common::new_str_hash;
import util::interner;
import util::data::interner;
tag restriction {
UNRESTRICTED;
@ -165,7 +165,7 @@ fn next_ann_num() -> uint {
auto srdr = io::file_reader(path);
auto filemap = codemap::new_filemap(path, pos);
vec::push[codemap::filemap](sess.get_codemap().files, filemap);
auto itr = @interner::mk_interner[str](str::hash, str::eq);
auto itr = @interner::mk[str](str::hash, str::eq);
auto rdr = lexer::new_reader(sess, srdr, filemap, itr);
// Make sure npos points at first actual token:
lexer::consume_any_whitespace(rdr);

View File

@ -1,7 +1,7 @@
import util::common::ty_mach;
import util::common::ty_mach_to_str;
import util::common::new_str_hash;
import util::interner;
import util::data::interner;
import std::int;
import std::uint;
import std::str;

View File

@ -34,7 +34,7 @@
import util::common::span;
import middle::tstate::ann::ts_ann;
import util::interner;
import util::data::interner;
// Data types
@ -232,7 +232,7 @@ fn mk_ctxt(session::session s, resolve::def_map dm) -> ctxt {
common::new_def_hash[ty::ty_param_count_and_ty]();
auto items = common::new_def_hash[any_item]();
auto ts = @interner::mk_interner[raw_t](hash_raw_ty, eq_raw_ty);
auto ts = @interner::mk[raw_t](hash_raw_ty, eq_raw_ty);
auto cx =
rec(ts = ts,

View File

@ -62,7 +62,7 @@ mod driver {
mod util {
mod common;
mod interner;
mod data;
}
auth front::creader::load_crate = unsafe;

44
src/comp/util/data.rs Normal file
View File

@ -0,0 +1,44 @@
// 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::vec;
import std::map;
import std::map::hashmap;
import std::map::hashfn;
import std::map::eqfn;
import std::option;
import std::option::none;
import std::option::some;
mod interner {
type interner[T] = rec(
hashmap[T,uint] map,
mutable vec[T] vect,
hashfn[T] hasher,
eqfn[T] eqer
);
fn mk[T](hashfn[T] hasher, eqfn[T] eqer) -> interner[T] {
auto m = map::mk_hashmap[T,uint](hasher, eqer);
let vec[T] vect = [];
ret rec(map=m, mutable vect=vect, hasher=hasher, eqer=eqer);
}
fn intern[T](&interner[T] itr, &T val) -> uint {
alt (itr.map.find(val)) {
case (some[uint](?idx)) { ret idx; }
case (none[uint]) {
auto new_idx = vec::len[T](itr.vect);
itr.map.insert(val, new_idx);
itr.vect += [val];
ret new_idx;
}
}
}
fn get[T](&interner[T] itr, uint idx) -> T {
ret itr.vect.(idx);
}
}

View File

@ -1,42 +0,0 @@
// 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::vec;
import std::map;
import std::map::hashmap;
import std::map::hashfn;
import std::map::eqfn;
import std::option;
import std::option::none;
import std::option::some;
type interner[T] = rec(
hashmap[T,uint] map,
mutable vec[T] vect,
hashfn[T] hasher,
eqfn[T] eqer
);
fn mk_interner[T](hashfn[T] hasher, eqfn[T] eqer) -> interner[T] {
auto m = map::mk_hashmap[T,uint](hasher, eqer);
let vec[T] vect = [];
ret rec(map=m, mutable vect=vect, hasher=hasher, eqer=eqer);
}
fn intern[T](&interner[T] itr, &T val) -> uint {
alt (itr.map.find(val)) {
case (some[uint](?idx)) { ret idx; }
case (none[uint]) {
auto new_idx = vec::len[T](itr.vect);
itr.map.insert(val, new_idx);
itr.vect += [val];
ret new_idx;
}
}
}
fn get[T](&interner[T] itr, uint idx) -> T {
ret itr.vect.(idx);
}