2012-02-03 19:46:17 -06:00
|
|
|
// based on:
|
2012-02-03 21:50:32 -06:00
|
|
|
// http://shootout.alioth.debian.org/
|
|
|
|
// u64q/program.php?test=mandelbrot&lang=python3&id=2
|
2012-02-03 19:46:17 -06:00
|
|
|
//
|
2012-02-03 22:43:48 -06:00
|
|
|
// takes 3 optional args:
|
|
|
|
// square image size, defaults to 80_u
|
|
|
|
// yield frequency, defaults to 10_u (yield every 10 spawns)
|
|
|
|
// output path, default is "" (no output), "-" means stdout
|
|
|
|
//
|
2012-02-03 19:46:17 -06:00
|
|
|
// in the shootout, they use 16000 as image size
|
|
|
|
// yield frequency doesn't seem to have much effect
|
|
|
|
//
|
2012-02-03 22:43:48 -06:00
|
|
|
// writes pbm image to output path
|
2012-02-03 19:46:17 -06:00
|
|
|
|
|
|
|
use std;
|
2012-03-12 22:04:27 -05:00
|
|
|
import io::writer_util;
|
2012-03-07 18:48:57 -06:00
|
|
|
import std::map::hashmap;
|
2012-02-03 19:46:17 -06:00
|
|
|
|
|
|
|
type cmplx = {re: f64, im: f64};
|
|
|
|
type line = {i: uint, b: [u8]};
|
|
|
|
|
|
|
|
impl arith for cmplx {
|
2012-02-03 21:50:32 -06:00
|
|
|
fn *(x: cmplx) -> cmplx {
|
|
|
|
{re: self.re*x.re - self.im*x.im, im: self.re*x.im + self.im*x.re}
|
|
|
|
}
|
2012-02-03 19:46:17 -06:00
|
|
|
|
2012-02-03 21:50:32 -06:00
|
|
|
fn +(x: cmplx) -> cmplx {
|
|
|
|
{re: self.re + x.re, im: self.im + x.im}
|
|
|
|
}
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pure fn cabs(x: cmplx) -> f64
|
|
|
|
{
|
2012-02-03 21:50:32 -06:00
|
|
|
x.re*x.re + x.im*x.im
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn mb(x: cmplx) -> bool
|
|
|
|
{
|
2012-02-03 21:50:32 -06:00
|
|
|
let z = {re: 0., im: 0.};
|
|
|
|
let i = 0;
|
|
|
|
let in = true;
|
|
|
|
while i < 50 {
|
|
|
|
z = z*z + x;
|
|
|
|
if cabs(z) >= 4. {
|
|
|
|
in = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
in
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fillbyte(x: cmplx, incr: f64) -> u8 {
|
2012-02-03 21:50:32 -06:00
|
|
|
let rv = 0_u8;
|
|
|
|
let i = 0_u8;
|
|
|
|
while i < 8_u8 {
|
|
|
|
let z = {re: x.re + (i as f64)*incr, im: x.im};
|
|
|
|
if mb(z) {
|
|
|
|
rv += 1_u8 << (7_u8 - i);
|
|
|
|
}
|
|
|
|
i += 1_u8;
|
|
|
|
}
|
|
|
|
rv
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn chanmb(i: uint, size: uint, ch: comm::chan<line>) -> ()
|
|
|
|
{
|
2012-02-03 21:50:32 -06:00
|
|
|
let crv = [];
|
|
|
|
let incr = 2./(size as f64);
|
|
|
|
let y = incr*(i as f64) - 1.;
|
|
|
|
let xincr = 8.*incr;
|
|
|
|
uint::range(0_u, size/8_u) {
|
|
|
|
|j|
|
|
|
|
let x = {re: xincr*(j as f64) - 1.5, im: y};
|
|
|
|
crv += [fillbyte(x, incr)];
|
|
|
|
};
|
|
|
|
comm::send(ch, {i:i, b:crv});
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
2012-02-03 22:43:48 -06:00
|
|
|
type devnull = {dn: int};
|
|
|
|
|
2012-03-12 22:04:27 -05:00
|
|
|
impl of io::writer for devnull {
|
2012-02-03 22:43:48 -06:00
|
|
|
fn write(_b: [const u8]) {}
|
2012-03-12 22:04:27 -05:00
|
|
|
fn seek(_i: int, _s: io::seek_style) {}
|
2012-02-03 22:43:48 -06:00
|
|
|
fn tell() -> uint {0_u}
|
|
|
|
fn flush() -> int {0}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn writer(path: str, writech: comm::chan<comm::chan<line>>, size: uint)
|
2012-02-03 19:46:17 -06:00
|
|
|
{
|
2012-02-03 21:50:32 -06:00
|
|
|
let p: comm::port<line> = comm::port();
|
|
|
|
let ch = comm::chan(p);
|
|
|
|
comm::send(writech, ch);
|
2012-03-12 22:04:27 -05:00
|
|
|
let cout: io::writer = alt path {
|
2012-02-03 22:43:48 -06:00
|
|
|
"" {
|
2012-03-12 22:04:27 -05:00
|
|
|
{dn: 0} as io::writer
|
2012-02-03 22:43:48 -06:00
|
|
|
}
|
|
|
|
"-" {
|
2012-03-12 22:04:27 -05:00
|
|
|
io::stdout()
|
2012-02-03 22:43:48 -06:00
|
|
|
}
|
|
|
|
_ {
|
|
|
|
result::get(
|
2012-03-12 22:04:27 -05:00
|
|
|
io::file_writer(path,
|
|
|
|
[io::create, io::truncate]))
|
2012-02-03 22:43:48 -06:00
|
|
|
}
|
|
|
|
};
|
2012-02-03 21:50:32 -06:00
|
|
|
cout.write_line("P4");
|
|
|
|
cout.write_line(#fmt("%u %u", size, size));
|
|
|
|
let lines = std::map::new_uint_hash();
|
|
|
|
let done = 0_u;
|
|
|
|
let i = 0_u;
|
|
|
|
while i < size {
|
|
|
|
let aline = comm::recv(p);
|
|
|
|
if aline.i == done {
|
|
|
|
#debug("W %u", aline.i);
|
|
|
|
cout.write(aline.b);
|
|
|
|
done += 1_u;
|
|
|
|
let prev = done;
|
|
|
|
while prev <= i {
|
|
|
|
if lines.contains_key(prev) {
|
|
|
|
#debug("WS %u", prev);
|
|
|
|
cout.write(lines.get(prev));
|
|
|
|
done += 1_u;
|
|
|
|
lines.remove(prev);
|
|
|
|
prev += 1_u;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#debug("S %u", aline.i);
|
|
|
|
lines.insert(aline.i, aline.b);
|
|
|
|
};
|
|
|
|
i += 1_u;
|
|
|
|
}
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|
|
|
|
|
2012-02-22 06:18:15 -06:00
|
|
|
fn main(argv: [str]) {
|
|
|
|
let size = if vec::len(argv) < 2_u { 80u }
|
|
|
|
else { option::get(uint::from_str(argv[1])) };
|
|
|
|
let yieldevery = if vec::len(argv) < 3_u { 10_u }
|
|
|
|
else { option::get(uint::from_str(argv[2])) };
|
|
|
|
let path = if vec::len(argv) < 4_u { "" }
|
|
|
|
else { argv[3] };
|
2012-02-03 21:50:32 -06:00
|
|
|
let writep = comm::port();
|
|
|
|
let writech = comm::chan(writep);
|
2012-02-22 06:18:15 -06:00
|
|
|
task::spawn {||
|
|
|
|
writer(path, writech, size);
|
2012-02-03 21:50:32 -06:00
|
|
|
};
|
|
|
|
let ch = comm::recv(writep);
|
2012-02-22 06:18:15 -06:00
|
|
|
uint::range(0_u, size) {|j|
|
|
|
|
task::spawn {|| chanmb(j, size, ch);};
|
2012-02-03 21:50:32 -06:00
|
|
|
if j % yieldevery == 0_u {
|
|
|
|
#debug("Y %u", j);
|
|
|
|
task::yield();
|
|
|
|
};
|
|
|
|
};
|
2012-02-03 19:46:17 -06:00
|
|
|
}
|