Skip to content

Commit 9e8b3e3

Browse files
committed
---
yaml --- r: 3450 b: refs/heads/master c: 7c500fc h: refs/heads/master v: v3
1 parent 6dbfe5c commit 9e8b3e3

File tree

3 files changed

+70
-104
lines changed

3 files changed

+70
-104
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 7b5d34aa9ac5c82e08c82b6e865c3b697299f65d
2+
refs/heads/master: 7c500fc0a0354ab72f0d76ccae6fae8c2ee5e62d

trunk/src/comp/middle/ast_map.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import std::smallintmap;
2+
import std::option;
13
import front::ast::*;
24
import visit::vt;
35

@@ -11,7 +13,10 @@ tag ast_node {
1113
type map = std::map::hashmap[node_id, ast_node];
1214

1315
fn map_crate(&crate c) -> map {
14-
auto map = util::common::new_seq_int_hash[ast_node]();
16+
// FIXME: This is using an adapter to convert the smallintmap
17+
// interface to the hashmap interface. It would be better to just
18+
// convert everything to use the smallintmap.
19+
auto map = new_smallintmap_int_adapter[ast_node]();
1520

1621
auto v_map = @rec(visit_item=bind map_item(map, _, _, _),
1722
visit_native_item=bind map_native_item(map, _, _, _),
@@ -42,6 +47,69 @@ fn map_expr(&map map, &@expr ex, &() e, &vt[()] v) {
4247
visit::visit_expr(ex, e, v);
4348
}
4449

50+
fn new_smallintmap_int_adapter[V]() -> std::map::hashmap[int, V] {
51+
auto key_idx = fn(&int key) -> uint { key as uint };
52+
auto idx_key = fn(&uint idx) -> int { idx as int };
53+
ret new_smallintmap_adapter(key_idx, idx_key);
54+
}
55+
56+
// This creates an object with the hashmap interface backed
57+
// by the smallintmap type, because I don't want to go through
58+
// the entire codebase adapting all the callsites to the different
59+
// interface.
60+
// FIXME: hashmap and smallintmap should support the same interface.
61+
fn new_smallintmap_adapter[K, V](fn(&K) -> uint key_idx,
62+
fn(&uint) -> K idx_key)
63+
-> std::map::hashmap[K, V] {
64+
65+
obj adapter[K, V](smallintmap::smallintmap[V] map,
66+
fn(&K) -> uint key_idx,
67+
fn(&uint) -> K idx_key) {
68+
69+
fn size() -> uint { fail }
70+
71+
fn insert(&K key, &V value) -> bool {
72+
auto exists = smallintmap::contains_key(map, key_idx(key));
73+
smallintmap::insert(map, key_idx(key), value);
74+
ret !exists;
75+
}
76+
77+
fn contains_key(&K key) -> bool {
78+
ret smallintmap::contains_key(map, key_idx(key));
79+
}
80+
81+
fn get(&K key) -> V {
82+
ret smallintmap::get(map, key_idx(key));
83+
}
84+
85+
fn find(&K key) -> option::t[V] {
86+
ret smallintmap::find(map, key_idx(key));
87+
}
88+
89+
fn remove(&K key) -> option::t[V] { fail }
90+
91+
fn rehash() { fail }
92+
93+
iter items() -> @tup(K, V) {
94+
auto idx = 0u;
95+
for (option::t[V] item in map.v) {
96+
alt (item) {
97+
case (option::some(?elt)) {
98+
auto value = elt;
99+
auto key = idx_key(idx);
100+
put @tup(key, value);
101+
}
102+
case (option::none) { }
103+
}
104+
idx += 1u;
105+
}
106+
}
107+
}
108+
109+
auto map = smallintmap::mk[V]();
110+
ret adapter(map, key_idx, idx_key);
111+
}
112+
45113
// Local Variables:
46114
// mode: rust
47115
// fill-column: 78;

trunk/src/comp/util/common.rs

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -105,108 +105,6 @@ fn new_uint_hash[V]() -> std::map::hashmap[uint, V] {
105105
ret std::map::mk_hashmap[uint, V](hasher, eqer);
106106
}
107107

108-
fn new_seq_hash[K, V](fn(&K) -> uint key_idx,
109-
fn(&uint) -> K idx_key) -> std::map::hashmap[K, V] {
110-
fn ensure_size[V](&mutable vec[mutable option::t[V]] bkts, uint idx) {
111-
auto bkt_len = vec::len(bkts);
112-
if (idx >= bkt_len) {
113-
auto needed = idx - bkt_len + 1u;
114-
auto new = vec::init_elt_mut(option::none[V], needed);
115-
bkts += new;
116-
}
117-
}
118-
119-
obj seq_hash[K, V](mutable uint nelts,
120-
mutable vec[mutable option::t[V]] bkts,
121-
fn(&K) -> uint key_idx,
122-
fn(&uint) -> K idx_key) {
123-
124-
fn size() -> uint { nelts }
125-
126-
fn insert(&K key, &V value) -> bool {
127-
auto idx = key_idx(key);
128-
ensure_size(bkts, idx);
129-
if (option::is_some(bkts.(idx))) {
130-
bkts.(idx) = option::some(value);
131-
ret false;
132-
} else {
133-
bkts.(idx) = option::some(value);
134-
nelts += 1u;
135-
ret true;
136-
}
137-
}
138-
139-
fn contains_key(&K key) -> bool {
140-
auto idx = key_idx(key);
141-
ensure_size(bkts, idx);
142-
if (option::is_some(bkts.(idx))) {
143-
ret true;
144-
} else {
145-
ret false;
146-
}
147-
}
148-
149-
fn get(&K key) -> V {
150-
ret alt (self.find(key)) {
151-
case (option::some(?v)) { v }
152-
case (_) { fail }
153-
};
154-
}
155-
156-
fn find(&K key) -> option::t[V] {
157-
auto idx = key_idx(key);
158-
ensure_size(bkts, idx);
159-
ret bkts.(idx);
160-
}
161-
162-
fn remove(&K key) -> option::t[V] {
163-
auto idx = key_idx(key);
164-
ensure_size(bkts, idx);
165-
auto val = bkts.(idx);
166-
if (option::is_some(val)) {
167-
bkts.(idx) = option::none;
168-
nelts -= 1u;
169-
}
170-
ret val;
171-
}
172-
173-
fn rehash() { }
174-
175-
iter items() -> @tup(K, V) {
176-
auto idx = 0u;
177-
auto bktsize = vec::len(bkts);
178-
while (idx < bktsize) {
179-
alt (bkts.(idx)) {
180-
case (option::some(?v)) {
181-
// FIXME: Appease alias analysis
182-
auto value = v;
183-
put @tup(idx_key(idx), value);
184-
}
185-
case (_) { }
186-
}
187-
idx += 1u;
188-
}
189-
}
190-
}
191-
192-
let vec[mutable option::t[V]] bkts = [mutable];
193-
ret seq_hash[K, V](0u, bkts, key_idx, idx_key);
194-
}
195-
196-
// A specialized map for keys that are sequential ints
197-
fn new_seq_int_hash[V]() -> std::map::hashmap[int, V] {
198-
auto key_idx = fn(&int key) -> uint { key as uint };
199-
auto idx_key = fn(&uint idx) -> int { idx as int };
200-
ret new_seq_hash(key_idx, idx_key);
201-
}
202-
203-
// A specialized map for keys that are sequential uints
204-
fn new_seq_uint_hash[V]() -> std::map::hashmap[uint, V] {
205-
auto key_idx = fn(&uint key) -> uint { key };
206-
auto idx_key = fn(&uint idx) -> uint { idx };
207-
ret new_seq_hash(key_idx, idx_key);
208-
}
209-
210108
fn istr(int i) -> str { ret int::to_str(i, 10u); }
211109

212110
fn uistr(uint i) -> str { ret uint::to_str(i, 10u); }

0 commit comments

Comments
 (0)