From 58988c356539b356201b19886edc61a5231656f3 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 23 May 2012 17:18:31 -0700 Subject: [PATCH] changes to get std passing borrowck --- src/libstd/ebml.rs | 4 +- src/libstd/map.rs | 10 ++--- src/libstd/treemap.rs | 97 +++++++++++++++++++++++-------------------- 3 files changed, 58 insertions(+), 53 deletions(-) diff --git a/src/libstd/ebml.rs b/src/libstd/ebml.rs index ba7064bd5c4..6697c58252e 100644 --- a/src/libstd/ebml.rs +++ b/src/libstd/ebml.rs @@ -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]; diff --git a/src/libstd/map.rs b/src/libstd/map.rs index 998fc55afe8..4967eaf9f85 100644 --- a/src/libstd/map.rs +++ b/src/libstd/map.rs @@ -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 { 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); diff --git a/src/libstd/treemap.rs b/src/libstd/treemap.rs index ebf532ab19d..b1ca3d5c50d 100644 --- a/src/libstd/treemap.rs +++ b/src/libstd/treemap.rs @@ -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 = @mut tree_node; +type treemap = @mut tree_edge; -enum tree_node { empty, node(@K, @V, treemap, treemap) } +type tree_edge = option<@tree_node>; + +enum tree_node = { + key: K, + mut value: V, + mut left: tree_edge, + mut right: tree_edge +}; #[doc = "Create a treemap"] -fn treemap() -> treemap { @mut empty } +fn treemap() -> treemap { @mut none } #[doc = "Insert a value into the map"] -fn insert(m: treemap, 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(m: &mut tree_edge, 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(m: &const tree_edge, k: K) -> option { + alt copy *m { + none { none } + + // TODO: was that an optimization? + 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 = "Find a value based on the key"] -fn find(m: treemap, k: K) -> option { - alt *m { - empty { 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) } - } - } -} - #[doc = "Visit all pairs in the map in order."] -fn traverse(m: treemap, 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(m: &const tree_edge, 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); } } } @@ -134,4 +141,4 @@ mod tests { assert (find(m, k2) == some("bar")); assert (find(m, k1) == some("foo")); } -} \ No newline at end of file +}