|
2 | 2 | // http://shootout.alioth.debian.org/
|
3 | 3 | // u64q/program.php?test=mandelbrot&lang=python3&id=2
|
4 | 4 | //
|
5 |
| -// takes 2 optional numeric args: square image size and yield frequency |
| 5 | +// takes 3 optional args: |
| 6 | +// square image size, defaults to 80_u |
| 7 | +// yield frequency, defaults to 10_u (yield every 10 spawns) |
| 8 | +// output path, default is "" (no output), "-" means stdout |
| 9 | +// |
6 | 10 | // in the shootout, they use 16000 as image size
|
7 | 11 | // yield frequency doesn't seem to have much effect
|
8 | 12 | //
|
9 |
| -// writes pbm image to stdout |
| 13 | +// writes pbm image to output path |
10 | 14 |
|
11 | 15 | use std;
|
12 | 16 | import std::io::writer_util;
|
@@ -72,12 +76,33 @@ fn chanmb(i: uint, size: uint, ch: comm::chan<line>) -> ()
|
72 | 76 | comm::send(ch, {i:i, b:crv});
|
73 | 77 | }
|
74 | 78 |
|
75 |
| -fn writer(writech: comm::chan<comm::chan<line>>, size: uint) |
| 79 | +type devnull = {dn: int}; |
| 80 | + |
| 81 | +impl of std::io::writer for devnull { |
| 82 | + fn write(_b: [const u8]) {} |
| 83 | + fn seek(_i: int, _s: std::io::seek_style) {} |
| 84 | + fn tell() -> uint {0_u} |
| 85 | + fn flush() -> int {0} |
| 86 | +} |
| 87 | + |
| 88 | +fn writer(path: str, writech: comm::chan<comm::chan<line>>, size: uint) |
76 | 89 | {
|
77 | 90 | let p: comm::port<line> = comm::port();
|
78 | 91 | let ch = comm::chan(p);
|
79 | 92 | comm::send(writech, ch);
|
80 |
| - let cout = std::io::stdout(); |
| 93 | + let cout: std::io::writer = alt path { |
| 94 | + "" { |
| 95 | + {dn: 0} as std::io::writer |
| 96 | + } |
| 97 | + "-" { |
| 98 | + std::io::stdout() |
| 99 | + } |
| 100 | + _ { |
| 101 | + result::get( |
| 102 | + std::io::file_writer(path, |
| 103 | + [std::io::create, std::io::truncate])) |
| 104 | + } |
| 105 | + }; |
81 | 106 | cout.write_line("P4");
|
82 | 107 | cout.write_line(#fmt("%u %u", size, size));
|
83 | 108 | let lines = std::map::new_uint_hash();
|
@@ -125,10 +150,16 @@ fn main(argv: [str])
|
125 | 150 | else {
|
126 | 151 | uint::from_str(argv[2])
|
127 | 152 | };
|
| 153 | + let path = if vec::len(argv) < 4_u { |
| 154 | + "" |
| 155 | + } |
| 156 | + else { |
| 157 | + argv[3] |
| 158 | + }; |
128 | 159 | let writep = comm::port();
|
129 | 160 | let writech = comm::chan(writep);
|
130 | 161 | task::spawn {
|
131 |
| - || writer(writech, size); |
| 162 | + || writer(path, writech, size); |
132 | 163 | };
|
133 | 164 | let ch = comm::recv(writep);
|
134 | 165 | uint::range(0_u, size) {
|
|
0 commit comments