Skip to content

Commit 280e459

Browse files
committed
---
yaml --- r: 3352 b: refs/heads/master c: 29a8219 h: refs/heads/master v: v3
1 parent 9bc211f commit 280e459

File tree

3 files changed

+105
-2
lines changed

3 files changed

+105
-2
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: 08b49a5d8fe975bcd8a0526526eb9692f3f3be4c
2+
refs/heads/master: 29a8219c4cb110baa5ad7123af187d881a02fd69

trunk/src/comp/middle/ast_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tag ast_node {
1111
type map = std::map::hashmap[node_id, ast_node];
1212

1313
fn map_crate(&crate c) -> map {
14-
auto map = util::common::new_int_hash[ast_node]();
14+
auto map = util::common::new_seq_int_hash[ast_node]();
1515

1616
auto v_map = @rec(visit_item=bind map_item(map, _, _, _),
1717
visit_native_item=bind map_native_item(map, _, _, _),

trunk/src/comp/util/common.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import std::map::hashmap;
44
import std::uint;
55
import std::int;
66
import std::vec;
7+
import std::option;
78
import std::option::none;
89
import std::option::some;
910
import front::ast;
@@ -104,6 +105,108 @@ fn new_uint_hash[V]() -> std::map::hashmap[uint, V] {
104105
ret std::map::mk_hashmap[uint, V](hasher, eqer);
105106
}
106107

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+
107210
fn istr(int i) -> str { ret int::to_str(i, 10u); }
108211

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

0 commit comments

Comments
 (0)