changes to get std passing borrowck
This commit is contained in:
parent
4c2bf8e4a7
commit
58988c3565
@ -406,8 +406,8 @@ impl deserializer of serialization::deserializer for ebml_deserializer {
|
||||
let {tag: r_tag, doc: r_doc} =
|
||||
ebml::doc_at(self.parent.data, self.pos);
|
||||
#debug["self.parent=%?-%? self.pos=%? r_tag=%? r_doc=%?-%?",
|
||||
self.parent.start, self.parent.end, self.pos,
|
||||
r_tag, r_doc.start, r_doc.end];
|
||||
copy self.parent.start, copy self.parent.end,
|
||||
copy self.pos, r_tag, r_doc.start, r_doc.end];
|
||||
if r_tag != (exp_tag as uint) {
|
||||
fail #fmt["expected EMBL doc with tag %? but found tag %?",
|
||||
exp_tag, r_tag];
|
||||
|
@ -102,7 +102,7 @@ mod chained {
|
||||
let mut e0 = e_root;
|
||||
let mut comp = 1u; // for logging
|
||||
loop {
|
||||
alt e0.next {
|
||||
alt copy e0.next {
|
||||
absent {
|
||||
#debug("search_tbl: absent, comp %u, hash %u, idx %u",
|
||||
comp, h, idx);
|
||||
@ -110,8 +110,7 @@ mod chained {
|
||||
}
|
||||
present(e1) {
|
||||
comp += 1u;
|
||||
let e1_key = e1.key; // Satisfy alias checker.
|
||||
if e1.hash == h && self.eqer(e1_key, k) {
|
||||
if e1.hash == h && self.eqer(e1.key, k) {
|
||||
#debug("search_tbl: present, comp %u, \
|
||||
hash %u, idx %u",
|
||||
comp, h, idx);
|
||||
@ -126,15 +125,14 @@ mod chained {
|
||||
|
||||
fn search_tbl(k: K, h: uint) -> search_result<K,V> {
|
||||
let idx = h % vec::len(self.chains);
|
||||
alt self.chains[idx] {
|
||||
alt copy self.chains[idx] {
|
||||
absent {
|
||||
#debug("search_tbl: absent, comp %u, hash %u, idx %u",
|
||||
0u, h, idx);
|
||||
ret not_found;
|
||||
}
|
||||
present(e) {
|
||||
// FIXME: This copy of the key is not good for perf
|
||||
if e.hash == h && self.eqer(copy e.key, k) {
|
||||
if e.hash == h && self.eqer(e.key, k) {
|
||||
#debug("search_tbl: present, comp %u, hash %u, idx %u",
|
||||
1u, h, idx);
|
||||
ret found_first(idx, e);
|
||||
|
@ -9,67 +9,74 @@ red-black tree or something else.
|
||||
import core::option::{some, none};
|
||||
import option = core::option;
|
||||
|
||||
export treemap;
|
||||
export treemap;
|
||||
export insert;
|
||||
export find;
|
||||
export traverse;
|
||||
|
||||
type treemap<K, V> = @mut tree_node<K, V>;
|
||||
type treemap<K, V> = @mut tree_edge<K, V>;
|
||||
|
||||
enum tree_node<K, V> { empty, node(@K, @V, treemap<K, V>, treemap<K, V>) }
|
||||
type tree_edge<K, V> = option<@tree_node<K, V>>;
|
||||
|
||||
enum tree_node<K, V> = {
|
||||
key: K,
|
||||
mut value: V,
|
||||
mut left: tree_edge<K, V>,
|
||||
mut right: tree_edge<K, V>
|
||||
};
|
||||
|
||||
#[doc = "Create a treemap"]
|
||||
fn treemap<K, V>() -> treemap<K, V> { @mut empty }
|
||||
fn treemap<K, V>() -> treemap<K, V> { @mut none }
|
||||
|
||||
#[doc = "Insert a value into the map"]
|
||||
fn insert<K: copy, V: copy>(m: treemap<K, V>, k: K, v: V) {
|
||||
alt m {
|
||||
@empty { *m = node(@k, @v, @mut empty, @mut empty); }
|
||||
@node(@kk, _, _, _) {
|
||||
|
||||
// We have to name left and right individually, because
|
||||
// otherwise the alias checker complains.
|
||||
if k < kk {
|
||||
alt check m { @node(_, _, left, _) { insert(left, k, v); } }
|
||||
fn insert<K: copy, V: copy>(m: &mut tree_edge<K, V>, k: K, v: V) {
|
||||
alt copy *m {
|
||||
none {
|
||||
*m = some(@tree_node({key: k,
|
||||
mut value: v,
|
||||
mut left: none,
|
||||
mut right: none}));
|
||||
ret;
|
||||
}
|
||||
some(node) {
|
||||
if k == node.key {
|
||||
node.value = v;
|
||||
} else if k < node.key {
|
||||
insert(&mut node.left, k, v);
|
||||
} else {
|
||||
alt check m {
|
||||
@node(_, _, _, right) { insert(right, k, v); }
|
||||
}
|
||||
}
|
||||
insert(&mut node.right, k, v);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[doc = "Find a value based on the key"]
|
||||
fn find<K: copy, V: copy>(m: treemap<K, V>, k: K) -> option<V> {
|
||||
alt *m {
|
||||
empty { none }
|
||||
fn find<K: copy, V: copy>(m: &const tree_edge<K, V>, k: K) -> option<V> {
|
||||
alt copy *m {
|
||||
none { none }
|
||||
|
||||
// TODO: was that an optimization?
|
||||
node(@kk, @v, left, right) {
|
||||
if k == kk {
|
||||
some(v)
|
||||
} else if k < kk {
|
||||
find(left, k)
|
||||
} else { find(right, k) }
|
||||
some(node) {
|
||||
if k == node.key {
|
||||
some(node.value)
|
||||
} else if k < node.key {
|
||||
find(&const node.left, k)
|
||||
} else {
|
||||
find(&const node.right, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc = "Visit all pairs in the map in order."]
|
||||
fn traverse<K, V>(m: treemap<K, V>, f: fn(K, V)) {
|
||||
alt *m {
|
||||
empty { }
|
||||
/*
|
||||
Previously, this had what looked like redundant
|
||||
matches to me, so I changed it. but that may be a
|
||||
de-optimization -- tjc
|
||||
*/
|
||||
node(k, v, left, right) {
|
||||
let k1 = k, v1 = v;
|
||||
traverse(left, f);
|
||||
f(*k1, *v1);
|
||||
traverse(right, f);
|
||||
fn traverse<K, V: copy>(m: &const tree_edge<K, V>, f: fn(K, V)) {
|
||||
alt copy *m {
|
||||
none { }
|
||||
some(node) {
|
||||
traverse(&const node.left, f);
|
||||
// copy of value is req'd as f() requires an immutable ptr
|
||||
f(node.key, copy node.value);
|
||||
traverse(&const node.right, f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user