Skip to content

Commit 62acb17

Browse files
brsongraydon
authored andcommitted
---
yaml --- r: 1633 b: refs/heads/master c: c572175 h: refs/heads/master i: 1631: b4f155c v: v3
1 parent e34c68b commit 62acb17

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: cba00ac2a1365093c74afee15b9a6634f7870f1a
2+
refs/heads/master: c5721759bbc6677be3f401d0f54dd53c9063d305

trunk/src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ TEST_XFAILS_RUSTC := $(addprefix test/run-pass/, \
575575
wrong-ret-type.rs \
576576
), \
577577
$(wildcard test/*fail/*.rs test/*fail/*.rc)) \
578+
test/bench/shootout/fannkuchredux.rs \
578579
test/bench/shootout/fasta.rs \
579580
test/bench/shootout/binarytrees.rs \
580581
$(wildcard test/bench/99-bottles/*rs)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Based on Isaac Gouy's fannkuchredux.csharp
2+
3+
use std;
4+
5+
import std._int;
6+
import std._vec;
7+
8+
impure fn fannkuch(int n) -> int {
9+
10+
fn perm1init(uint i) -> mutable int {
11+
ret i as int;
12+
}
13+
auto perm1init_ = perm1init; // Rustboot workaround
14+
15+
auto perm = _vec.init_elt[mutable int](0, n as uint);
16+
auto perm1 = _vec.init_fn[mutable int](perm1init_, n as uint);
17+
auto count = _vec.init_elt[mutable int](0, n as uint);
18+
19+
auto f = 0;
20+
auto i = 0;
21+
auto k = 0;
22+
auto r = 0;
23+
auto flips = 0;
24+
auto nperm = 0;
25+
auto checksum = 0;
26+
27+
r = n;
28+
while (r > 0) {
29+
i = 0;
30+
31+
while (r != 1) {
32+
count.(r - 1) = r;
33+
r -=1;
34+
}
35+
36+
while (i < n) {
37+
perm.(i) = perm1.(i);
38+
i += 1;
39+
}
40+
41+
// Count flips and update max and checksum
42+
f = 0;
43+
k = perm.(0);
44+
while (k != 0) {
45+
i = 0;
46+
while (2 * i < k) {
47+
auto t = perm.(i);
48+
perm.(i) = perm.(k - i);
49+
perm.(k - i) = t;
50+
i += 1;
51+
}
52+
k = perm.(0);
53+
f += 1;
54+
}
55+
56+
if (f > flips) {
57+
flips = f;
58+
}
59+
60+
if ((nperm & 0x1) == 0) {
61+
checksum += f;
62+
} else {
63+
checksum -= f;
64+
}
65+
66+
// Use incremental change to generate another permutation
67+
auto go = true;
68+
while (go) {
69+
if (r == n) {
70+
log checksum;
71+
ret flips;
72+
}
73+
auto p0 = perm1.(0);
74+
i = 0;
75+
while (i < r) {
76+
auto j = i + 1;
77+
perm1.(i) = perm1.(j);
78+
i = j;
79+
}
80+
perm1.(r) = p0;
81+
82+
count.(r) -= 1;
83+
if (count.(r) > 0) {
84+
go = false;
85+
} else {
86+
r += 1;
87+
}
88+
}
89+
90+
nperm += 1;
91+
}
92+
93+
ret flips;
94+
}
95+
96+
impure fn main(vec[str] args) {
97+
auto n = 7;
98+
log #fmt("Pfannkuchen(%d) = %d", n, fannkuch(n));
99+
}

0 commit comments

Comments
 (0)