Remove a bunch of unnecessary allocations and copies

This commit is contained in:
Björn Steinbrink 2013-05-29 20:10:16 +02:00
parent ca74cbdc5c
commit 1720d9f663
27 changed files with 83 additions and 91 deletions

View file

@ -26,8 +26,8 @@ fn print_complements() {
let all = ~[Blue, Red, Yellow];
for vec::each(all) |aa| {
for vec::each(all) |bb| {
io::println(show_color(*aa) + ~" + " + show_color(*bb) +
~" -> " + show_color(transform(*aa, *bb)));
io::println(show_color(*aa) + " + " + show_color(*bb) +
" -> " + show_color(transform(*aa, *bb)));
}
}
}
@ -50,7 +50,7 @@ fn show_color(cc: color) -> ~str {
fn show_color_list(set: ~[color]) -> ~str {
let mut out = ~"";
for vec::eachi(set) |_ii, col| {
out += ~" ";
out += " ";
out += show_color(*col);
}
return out;
@ -82,7 +82,7 @@ fn show_number(nn: uint) -> ~str {
while num != 0 {
dig = num % 10;
num = num / 10;
out = show_digit(dig) + ~" " + out;
out = show_digit(dig) + " " + out;
}
return out;
@ -131,8 +131,8 @@ fn creature(
}
option::None => {
// log creatures met and evil clones of self
let report = fmt!("%u", creatures_met) + ~" " +
show_number(evil_clones_met);
let report = fmt!("%u %s",
creatures_met, show_number(evil_clones_met));
to_rendezvous_log.send(report);
break;
}

View file

@ -49,7 +49,7 @@ fn make_cumulative(aa: ~[AminoAcids]) -> ~[AminoAcids] {
let mut ans: ~[AminoAcids] = ~[];
for aa.each |a| {
cp += a.prob;
ans += ~[AminoAcids {ch: a.ch, prob: cp}];
ans += [AminoAcids {ch: a.ch, prob: cp}];
}
return ans;
}
@ -72,7 +72,7 @@ fn make_random_fasta(wr: @io::Writer,
desc: ~str,
genelist: ~[AminoAcids],
n: int) {
wr.write_line(~">" + id + ~" " + desc);
wr.write_line(~">" + id + " " + desc);
let mut rng = rand::rng();
let rng = @mut MyRandom {
last: rng.next()
@ -91,7 +91,7 @@ fn make_random_fasta(wr: @io::Writer,
fn make_repeat_fasta(wr: @io::Writer, id: ~str, desc: ~str, s: ~str, n: int) {
unsafe {
wr.write_line(~">" + id + ~" " + desc);
wr.write_line(~">" + id + " " + desc);
let mut op: ~str = ~"";
let sl: uint = str::len(s);
for uint::range(0u, n as uint) |i| {
@ -139,13 +139,13 @@ fn main() {
make_cumulative(~[acid('a', 30u32), acid('c', 20u32), acid('g', 20u32),
acid('t', 30u32)]);
let alu: ~str =
~"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" +
~"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" +
~"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" +
~"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" +
~"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
~"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
~"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
~"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
make_repeat_fasta(writer, ~"ONE", ~"Homo sapiens alu", alu, n * 2);
make_random_fasta(writer, ~"TWO", ~"IUB ambiguity codes", iub, n * 3);
make_random_fasta(writer, ~"THREE",

View file

@ -103,7 +103,7 @@ pub impl Sudoku {
for u8::range(0u8, 9u8) |row| {
for u8::range(0u8, 9u8) |col| {
let color = self.grid[row][col];
if color == 0u8 { work += ~[(row, col)]; }
if color == 0u8 { work += [(row, col)]; }
}
}

View file

@ -96,7 +96,7 @@ fn recurse_or_fail(depth: int, st: Option<State>) {
fn_box: || @Cons((), fn_box()),
tuple: (@Cons((), st.tuple.first()),
~Cons((), @*st.tuple.second())),
vec: st.vec + ~[@Cons((), *st.vec.last())],
vec: st.vec + [@Cons((), *st.vec.last())],
res: r(@Cons((), st.res._l))
}
}

View file

@ -26,7 +26,7 @@ impl to_str for int {
impl<T:to_str> to_str for ~[T] {
fn to_str(&self) -> ~str {
~"[" + str::connect(vec::map(*self, |e| e.to_str() ), ~", ") + ~"]"
~"[" + str::connect(vec::map(*self, |e| e.to_str() ), ", ") + "]"
}
}