Replace common::new_seq_hash with an adapter around std::smallintmap
It would be better to either convert ast_map to use smallintmap or make smallintmap and hashmap follow the same interface, but I don't feel up to it just now. Closes #585.
This commit is contained in:
parent
7b5d34aa9a
commit
7c500fc0a0
2 changed files with 69 additions and 103 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import std::smallintmap;
|
||||
import std::option;
|
||||
import front::ast::*;
|
||||
import visit::vt;
|
||||
|
||||
|
|
@ -11,7 +13,10 @@ tag ast_node {
|
|||
type map = std::map::hashmap[node_id, ast_node];
|
||||
|
||||
fn map_crate(&crate c) -> map {
|
||||
auto map = util::common::new_seq_int_hash[ast_node]();
|
||||
// FIXME: This is using an adapter to convert the smallintmap
|
||||
// interface to the hashmap interface. It would be better to just
|
||||
// convert everything to use the smallintmap.
|
||||
auto map = new_smallintmap_int_adapter[ast_node]();
|
||||
|
||||
auto v_map = @rec(visit_item=bind map_item(map, _, _, _),
|
||||
visit_native_item=bind map_native_item(map, _, _, _),
|
||||
|
|
@ -42,6 +47,69 @@ fn map_expr(&map map, &@expr ex, &() e, &vt[()] v) {
|
|||
visit::visit_expr(ex, e, v);
|
||||
}
|
||||
|
||||
fn new_smallintmap_int_adapter[V]() -> std::map::hashmap[int, V] {
|
||||
auto key_idx = fn(&int key) -> uint { key as uint };
|
||||
auto idx_key = fn(&uint idx) -> int { idx as int };
|
||||
ret new_smallintmap_adapter(key_idx, idx_key);
|
||||
}
|
||||
|
||||
// This creates an object with the hashmap interface backed
|
||||
// by the smallintmap type, because I don't want to go through
|
||||
// the entire codebase adapting all the callsites to the different
|
||||
// interface.
|
||||
// FIXME: hashmap and smallintmap should support the same interface.
|
||||
fn new_smallintmap_adapter[K, V](fn(&K) -> uint key_idx,
|
||||
fn(&uint) -> K idx_key)
|
||||
-> std::map::hashmap[K, V] {
|
||||
|
||||
obj adapter[K, V](smallintmap::smallintmap[V] map,
|
||||
fn(&K) -> uint key_idx,
|
||||
fn(&uint) -> K idx_key) {
|
||||
|
||||
fn size() -> uint { fail }
|
||||
|
||||
fn insert(&K key, &V value) -> bool {
|
||||
auto exists = smallintmap::contains_key(map, key_idx(key));
|
||||
smallintmap::insert(map, key_idx(key), value);
|
||||
ret !exists;
|
||||
}
|
||||
|
||||
fn contains_key(&K key) -> bool {
|
||||
ret smallintmap::contains_key(map, key_idx(key));
|
||||
}
|
||||
|
||||
fn get(&K key) -> V {
|
||||
ret smallintmap::get(map, key_idx(key));
|
||||
}
|
||||
|
||||
fn find(&K key) -> option::t[V] {
|
||||
ret smallintmap::find(map, key_idx(key));
|
||||
}
|
||||
|
||||
fn remove(&K key) -> option::t[V] { fail }
|
||||
|
||||
fn rehash() { fail }
|
||||
|
||||
iter items() -> @tup(K, V) {
|
||||
auto idx = 0u;
|
||||
for (option::t[V] item in map.v) {
|
||||
alt (item) {
|
||||
case (option::some(?elt)) {
|
||||
auto value = elt;
|
||||
auto key = idx_key(idx);
|
||||
put @tup(key, value);
|
||||
}
|
||||
case (option::none) { }
|
||||
}
|
||||
idx += 1u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto map = smallintmap::mk[V]();
|
||||
ret adapter(map, key_idx, idx_key);
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// mode: rust
|
||||
// fill-column: 78;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue