Permalink
Browse files

changing mutability handling to simplify some code

  • Loading branch information...
1 parent 2bf8050 commit 02759b89ce0ce697995e4f9d219045a90488561b @dbp committed Oct 9, 2012
Showing with 27 additions and 35 deletions.
  1. +9 −9 load.rs
  2. +14 −22 query.rs
  3. +4 −4 types.rs
View
18 load.rs
@@ -132,17 +132,17 @@ fn load_args(arg_list: ~str, self: Option<~str>) -> (~[Arg], Arg, uint) {
// bucket_sort takes definitions and builds the Data structure, by putting
// them into the appropriate buckets
fn bucket_sort(ds: ~[(@Definition, bool)]) -> Data {
- let data = empty_data();
+ let mut data = empty_data();
for vec::each(ds) |dc| {
let (d, canonical) = *dc;
match vec::len(d.args) {
- 0 => bucket_drop(&data.ar0, d),
- 1 => bucket_drop(&data.ar1, d),
- 2 => bucket_drop(&data.ar2, d),
- 3 => bucket_drop(&data.ar3, d),
- 4 => bucket_drop(&data.ar4, d),
- 5 => bucket_drop(&data.ar5, d),
- _ => bucket_drop(&data.arn, d)
+ 0 => bucket_drop(&mut data.ar0, d),
+ 1 => bucket_drop(&mut data.ar1, d),
+ 2 => bucket_drop(&mut data.ar2, d),
+ 3 => bucket_drop(&mut data.ar3, d),
+ 4 => bucket_drop(&mut data.ar4, d),
+ 5 => bucket_drop(&mut data.ar5, d),
+ _ => bucket_drop(&mut data.arn, d)
}
if canonical {
let mut name = copy d.name;
@@ -153,7 +153,7 @@ fn bucket_sort(ds: ~[(@Definition, bool)]) -> Data {
}
// bucket_drop places a definition into the right part of the bucket
-fn bucket_drop(b: &Bucket, d: @Definition) {
+fn bucket_drop(b: &mut Bucket, d: @Definition) {
// for now, just put all in np0
b.defs.push(d);
}
View
@@ -28,21 +28,17 @@ pub fn query(q: ~str) -> ~[Query] {
// what it finds
pub fn search_type(qs: ~[Query], d: &Data) -> ~[@Definition] {
let mut results = ~[];
- let mut n = 0;
- let len = qs.len();
- while n < len {
- let q = copy qs[n];
+ for qs.each |q| {
let res = match vec::len(q.args) {
- 0 => search_bucket(&mut d.ar0, &q),
- 1 => search_bucket(&mut d.ar1, &q),
- 2 => search_bucket(&mut d.ar2, &q),
- 3 => search_bucket(&mut d.ar3, &q),
- 4 => search_bucket(&mut d.ar4, &q),
- 5 => search_bucket(&mut d.ar5, &q),
- _ => search_bucket(&mut d.arn, &q)
+ 0 => search_bucket(&d.ar0, q),
+ 1 => search_bucket(&d.ar1, q),
+ 2 => search_bucket(&d.ar2, q),
+ 3 => search_bucket(&d.ar3, q),
+ 4 => search_bucket(&d.ar4, q),
+ 5 => search_bucket(&d.ar5, q),
+ _ => search_bucket(&d.arn, q)
};
results.push_all_move(res);
- n += 1;
}
return results;
}
@@ -56,20 +52,16 @@ pub fn search_name(q: ~str, d: &Data) -> ~[@Definition] {
}
// search_bucket looks for matches in a bucket
-fn search_bucket(b: &mut Bucket, q: &Query) -> ~[@Definition] {
- let mut n = 0;
- let len = b.defs.len();
+fn search_bucket(b: &Bucket, q: &Query) -> ~[@Definition] {
let mut results = ~[];
let q_args = set_from_vec(&q.args);
- while (n < len) {
- let d = b.defs[n];
+ for b.defs.each |d| {
let d_args = set_from_vec(&d.args);
let e = set_equals(&d_args, &q_args);
if e && d.ret == q.ret {
- results.push(d);
+ results.push(*d);
}
- n += 1;
}
// only give 10 responses per query
@@ -137,12 +129,12 @@ mod tests {
let def = @Definition { name: ~"foo", path: ~"foo",
desc: ~"", anchor: ~"function-foo", args: ~[],
ret: Arg {name: ~"()", inner: ~[]}, signature: ~"fn foo()"};
- let mut bucket = Bucket {defs: ~[def]};
+ let bucket = Bucket {defs: ~[def]};
let query = Query { args: ~[], ret: copy def.ret };
- assert search_bucket(&mut bucket, &query) == ~[def];
+ assert search_bucket(&bucket, &query) == ~[def];
let query2 = Query { args: ~[copy def.ret], ret: copy def.ret };
- assert search_bucket(&mut bucket, &query2) == ~[];
+ assert search_bucket(&bucket, &query2) == ~[];
}
#[test]
View
@@ -102,7 +102,7 @@ impl Definition {
}
// A bucket holds a bunch of definitions
-struct Bucket { mut defs: ~[@Definition] }
+struct Bucket { defs: ~[@Definition] }
// A trie is used to look up names efficiently by prefix. We assume that
// most searches will be by the beginning of names, and can expand later.
@@ -111,9 +111,9 @@ struct Bucket { mut defs: ~[@Definition] }
struct Trie { children: HashMap<~str,@Trie>, mut defs: ~[@Definition] }
// Data stores all the definitions in buckets, based on function arity.
-struct Data { mut ar0: Bucket, mut ar1: Bucket, mut ar2: Bucket,
- mut ar3: Bucket, mut ar4: Bucket, mut ar5: Bucket,
- mut arn: Bucket, mut names: @Trie }
+struct Data { ar0: Bucket, ar1: Bucket, ar2: Bucket,
+ ar3: Bucket, ar4: Bucket, ar5: Bucket,
+ arn: Bucket, names: @Trie }
fn empty_data() -> Data {
let empty_bucket = Bucket { defs: ~[] };

0 comments on commit 02759b8

Please sign in to comment.