Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 184 lines (165 sloc) 4.349 kb
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
1 // based on:
63ae16f Ted Horst fix formatting
tedhorst authored
2 // http://shootout.alioth.debian.org/
3 // u64q/program.php?test=mandelbrot&lang=python3&id=2
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
4 //
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
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 //
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
10 // in the shootout, they use 16000 as image size
11 // yield frequency doesn't seem to have much effect
12 //
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
13 // writes pbm image to output path
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
14
e653d49 Patrick Walton rustc: Remove legacy mode inference, unless #[legacy_modes] is used
pcwalton authored
15 #[legacy_modes];
16
ea01ee2 Brian Anderson Convert 'use' to 'extern mod'. Remove old 'use' syntax
brson authored
17 extern mod std;
f686896 Patrick Walton test: "import" -> "use"
pcwalton authored
18 use io::WriterUtil;
cb7a539 Brian Anderson Convert std::map to camel case
brson authored
19 use std::map::HashMap;
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
20
93c2f5e Patrick Walton rustc: Use coherence for operator overloading.
pcwalton authored
21 struct cmplx {
f0eae8f Brian Anderson Convert field terminators to commas. Stop parsing semis.
brson authored
22 re: f64,
23 im: f64
db020ab Patrick Walton rustc: Implement and enforce instance coherence
pcwalton authored
24 }
25
9117dcb Patrick Walton rustc: De-mode all overloaded operators
pcwalton authored
26 impl cmplx : ops::Mul<cmplx,cmplx> {
27 pure fn mul(x: &cmplx) -> cmplx {
28 cmplx {
29 re: self.re*(*x).re - self.im*(*x).im,
30 im: self.re*(*x).im + self.im*(*x).re
31 }
32 }
33 }
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
34
9117dcb Patrick Walton rustc: De-mode all overloaded operators
pcwalton authored
35 impl cmplx : ops::Add<cmplx,cmplx> {
36 pure fn add(x: &cmplx) -> cmplx {
37 cmplx {
38 re: self.re + (*x).re,
39 im: self.im + (*x).im
40 }
41 }
42 }
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
43
93c2f5e Patrick Walton rustc: Use coherence for operator overloading.
pcwalton authored
44 type line = {i: uint, b: ~[u8]};
45
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
46 pure fn cabs(x: cmplx) -> f64
47 {
63ae16f Ted Horst fix formatting
tedhorst authored
48 x.re*x.re + x.im*x.im
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
49 }
50
51 fn mb(x: cmplx) -> bool
52 {
93c2f5e Patrick Walton rustc: Use coherence for operator overloading.
pcwalton authored
53 let mut z = cmplx {re: 0f64, im: 0f64};
dc07280 Niko Matsakis make --enforce-mut-vars always on, add mut annotations to remaining file...
nikomatsakis authored
54 let mut i = 0;
55 let mut in = true;
63ae16f Ted Horst fix formatting
tedhorst authored
56 while i < 50 {
57 z = z*z + x;
1e8f501 Eric Holk Machine types are different from int/uint, etc (Issue #2187)
eholk authored
58 if cabs(z) >= 4f64 {
63ae16f Ted Horst fix formatting
tedhorst authored
59 in = false;
60 break;
61 }
62 i += 1;
63 }
64 in
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
65 }
66
67 fn fillbyte(x: cmplx, incr: f64) -> u8 {
dc07280 Niko Matsakis make --enforce-mut-vars always on, add mut annotations to remaining file...
nikomatsakis authored
68 let mut rv = 0_u8;
69 let mut i = 0_u8;
63ae16f Ted Horst fix formatting
tedhorst authored
70 while i < 8_u8 {
93c2f5e Patrick Walton rustc: Use coherence for operator overloading.
pcwalton authored
71 let z = cmplx {re: x.re + (i as f64)*incr, im: x.im};
63ae16f Ted Horst fix formatting
tedhorst authored
72 if mb(z) {
73 rv += 1_u8 << (7_u8 - i);
74 }
75 i += 1_u8;
76 }
77 rv
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
78 }
79
9c6890f Brian Anderson Convert more core types to camel case
brson authored
80 fn chanmb(i: uint, size: uint, ch: comm::Chan<line>) -> ()
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
81 {
98e161f Michael J. Sullivan Switch the compiler over to using ~[] notation instead of []/~. Closes #...
msullivan authored
82 let mut crv = ~[];
1e8f501 Eric Holk Machine types are different from int/uint, etc (Issue #2187)
eholk authored
83 let incr = 2f64/(size as f64);
84 let y = incr*(i as f64) - 1f64;
85 let xincr = 8f64*incr;
d1fc2b5 Brian Anderson Convert to new closure syntax
brson authored
86 for uint::range(0_u, size/8_u) |j| {
93c2f5e Patrick Walton rustc: Use coherence for operator overloading.
pcwalton authored
87 let x = cmplx {re: xincr*(j as f64) - 1.5f64, im: y};
67a8e71 Niko Matsakis Demode vec::push (and convert to method)
nikomatsakis authored
88 crv.push(fillbyte(x, incr));
63ae16f Ted Horst fix formatting
tedhorst authored
89 };
90 comm::send(ch, {i:i, b:crv});
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
91 }
92
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
93 type devnull = {dn: int};
94
1125831 Brian Anderson Convert more core types to camel case
brson authored
95 impl devnull: io::Writer {
98e161f Michael J. Sullivan Switch the compiler over to using ~[] notation instead of []/~. Closes #...
msullivan authored
96 fn write(_b: &[const u8]) {}
1125831 Brian Anderson Convert more core types to camel case
brson authored
97 fn seek(_i: int, _s: io::SeekStyle) {}
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
98 fn tell() -> uint {0_u}
99 fn flush() -> int {0}
1125831 Brian Anderson Convert more core types to camel case
brson authored
100 fn get_type() -> io::WriterType { io::File }
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
101 }
102
9c6890f Brian Anderson Convert more core types to camel case
brson authored
103 fn writer(path: ~str, writech: comm::Chan<comm::Chan<line>>, size: uint)
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
104 {
161a82e Brian Anderson Camel case various core constructors
brson authored
105 let p: comm::Port<line> = comm::Port();
106 let ch = comm::Chan(p);
63ae16f Ted Horst fix formatting
tedhorst authored
107 comm::send(writech, ch);
1125831 Brian Anderson Convert more core types to camel case
brson authored
108 let cout: io::Writer = match path {
025d866 Brian Anderson Switch alts to use arrows
brson authored
109 ~"" => {
1125831 Brian Anderson Convert more core types to camel case
brson authored
110 {dn: 0} as io::Writer
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
111 }
025d866 Brian Anderson Switch alts to use arrows
brson authored
112 ~"-" => {
6f5853f Graydon Hoare Libc/os/run/rand/io reorganization. Close #1373. Close #1638.
graydon authored
113 io::stdout()
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
114 }
025d866 Brian Anderson Switch alts to use arrows
brson authored
115 _ => {
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
116 result::get(
d05e2ad Brian Anderson Demode core::result
brson authored
117 &io::file_writer(&Path(path),
1125831 Brian Anderson Convert more core types to camel case
brson authored
118 ~[io::Create, io::Truncate]))
9b46e87 Ted Horst add third arg for output path, default to no output
tedhorst authored
119 }
120 };
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
121 cout.write_line(~"P4");
29f32b4 Paul Stansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
122 cout.write_line(fmt!("%u %u", size, size));
1ffd90e Graydon Hoare Remove redundant hashmap constructor functions.
graydon authored
123 let lines: HashMap<uint, ~[u8]> = HashMap();
dc07280 Niko Matsakis make --enforce-mut-vars always on, add mut annotations to remaining file...
nikomatsakis authored
124 let mut done = 0_u;
125 let mut i = 0_u;
63ae16f Ted Horst fix formatting
tedhorst authored
126 while i < size {
127 let aline = comm::recv(p);
128 if aline.i == done {
29f32b4 Paul Stansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
129 debug!("W %u", aline.i);
63ae16f Ted Horst fix formatting
tedhorst authored
130 cout.write(aline.b);
131 done += 1_u;
dc07280 Niko Matsakis make --enforce-mut-vars always on, add mut annotations to remaining file...
nikomatsakis authored
132 let mut prev = done;
63ae16f Ted Horst fix formatting
tedhorst authored
133 while prev <= i {
134 if lines.contains_key(prev) {
29f32b4 Paul Stansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
135 debug!("WS %u", prev);
7e56612 Niko Matsakis Remove unnecessary temporary, add type annot to help inferencer (cc #228...
nikomatsakis authored
136 cout.write(lines.get(prev));
63ae16f Ted Horst fix formatting
tedhorst authored
137 done += 1_u;
138 lines.remove(prev);
139 prev += 1_u;
140 }
141 else {
142 break
143 }
144 };
145 }
146 else {
29f32b4 Paul Stansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
147 debug!("S %u", aline.i);
63ae16f Ted Horst fix formatting
tedhorst authored
148 lines.insert(aline.i, aline.b);
149 };
150 i += 1_u;
151 }
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
152 }
153
267ab11 Niko Matsakis use + mode for (almost) everything when not using legacy modes
nikomatsakis authored
154 fn main(++args: ~[~str]) {
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
155 let args = if os::getenv(~"RUST_BENCH").is_some() {
156 ~[~"", ~"4000", ~"10"]
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
157 } else {
158 args
159 };
160
92743dc Michael J. Sullivan Move the world over to using the new style string literals and types. Cl...
msullivan authored
161 let path = if vec::len(args) < 4_u { ~"" }
3c4a1ab Ted Horst fix args to shootout-mandelbrot benchmark
tedhorst authored
162 else { args[3] };
163
164 let yieldevery = if vec::len(args) < 3_u { 10_u }
165 else { uint::from_str(args[2]).get() };
166
167 let size = if vec::len(args) < 2_u { 80_u }
168 else { uint::from_str(args[1]).get() };
4fec4cd Brian Anderson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
169
161a82e Brian Anderson Camel case various core constructors
brson authored
170 let writep = comm::Port();
171 let writech = comm::Chan(writep);
f2e2a14 Ben Striegel Remove empty argument lists from do expressions
bstrie authored
172 do task::spawn {
ffd50b9 Marijn Haverbeke Make the various from_str functions return options
marijnh authored
173 writer(path, writech, size);
63ae16f Ted Horst fix formatting
tedhorst authored
174 };
175 let ch = comm::recv(writep);
d1fc2b5 Brian Anderson Convert to new closure syntax
brson authored
176 for uint::range(0_u, size) |j| {
177 task::spawn(|| chanmb(j, size, ch) );
63ae16f Ted Horst fix formatting
tedhorst authored
178 if j % yieldevery == 0_u {
29f32b4 Paul Stansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
179 debug!("Y %u", j);
63ae16f Ted Horst fix formatting
tedhorst authored
180 task::yield();
181 };
182 };
e7738ca Ted Horst add mandelbrot image generator from shootout
tedhorst authored
183 }
Something went wrong with that request. Please try again.