Debian Codegraph @

benchmarks.larceny/src/perm9.java

raw
1 // PERM9 - Memory system benchmark using Zaks's permutation generator.
2
3 class Pair {
4
5 // A Java compiler ought to generate inline code for these.
6
7 public static Pair cons (int n, Pair y) { return new Pair(n, y); }
8 public static int car (Pair x) { return x.hd; }
9 public static Pair cdr (Pair x) { return x.tl; }
10
11 // If it doesn't, then we'll inline them by hand.
12 // That's why the following are public.
13 // (But Sun's Java 1.2 does the inlining ok.)
14
15 public Pair (int n, Pair y) { hd = n; tl = y; }
16 public int hd;
17 public Pair tl;
18
19 public static int length (Pair x) {
20 int n = 0;
21 while (x != null) {
22 n = n + 1;
23 x = Pair.cdr (x);
24 }
25 return n;
26 }
27 }
28
29 class Perm {
30
31 // A Java compiler ought to generate inline code for these.
32
33 public static Perm cons (Pair p, Perm y) { return new Perm(p, y); }
34 public static Pair car (Perm x) { return x.hd; }
35 public static Perm cdr (Perm x) { return x.tl; }
36
37 public Perm (Pair n, Perm y) { hd = n; tl = y; }
38 public Pair hd;
39 public Perm tl;
40
41 }
42
43 class perm9 {
44
45 /******************************************************************************
46 *
47 * permutations
48 *
49 ******************************************************************************/
50
51 private static Perm perms; /* local to these functions */
52 private static Pair x; /* local to these functions */
53
54 static Pair revloop( Pair x, int n, Pair y )
55 {
56 while (n-- != 0) {
57 y = Pair.cons ( Pair.car(x), y );
58 x = Pair.cdr(x);
59 }
60 return y;
61 }
62
63 static Pair tail( Pair l, int n )
64 {
65 while (n-- > 0) {
66 l = Pair.cdr(l);
67 }
68 return l;
69 }
70
71 static void F( int n )
72 {
73 x = revloop( x, n, tail( x, n ) );
74 perms = Perm.cons ( x, perms );
75 }
76
77 static void P( int n )
78 {
79 if (n > 1) {
80 for ( int j = n-1; j != 0; --j ) {
81 P( n-1 );
82 F( n );
83 }
84 P( n-1 );
85 }
86 }
87
88 static Perm permutations( Pair the_x )
89 {
90 x = the_x;
91 perms = Perm.cons ( the_x, null );
92 P( Pair.length( the_x ) );
93 return perms;
94 }
95
96 /******************************************************************************
97 *
98 * sumperms
99 *
100 ******************************************************************************/
101
102 static long sumperms( Perm x ) {
103 Pair y;
104 long sum = 0;
105
106 for (; x != null; x = Perm.cdr(x))
107 for (y = Perm.car(x); y != null; y = Pair.cdr(y))
108 sum = sum + Pair.car(y);
109
110 return sum;
111 }
112
113 /******************************************************************************
114 *
115 * factorial
116 *
117 ******************************************************************************/
118
119 static long factorial( int n ) {
120 int f = 1;
121 while (n > 0) {
122 f = n * f;
123 n = n - 1;
124 }
125 return f;
126 }
127
128 public static void main( String args[] )
129 {
130 int n = 9;
131 Perm m, m2;
132 long sum;
133 int k;
134
135 /* Create the list [1, 2, ..., n]. */
136
137 Pair one_to_n = null;
138 int nn = n;
139 while (nn > 0) {
140 one_to_n = Pair.cons( nn--, one_to_n );
141 }
142
143 m = permutations( one_to_n );
144 //printperms( m );
145
146 for ( k = 5; k > 0; --k ) {
147 m2 = permutations( one_to_n );
148 m = m2;
149 }
150
151 sum = sumperms( m );
152 if (sum != (n * (n + 1) * factorial (n)) / 2)
153 System.out.println ("*** wrong result ***");
154 }
155
156 /******************************************************************************
157 *
158 * Miscellaneous.
159 *
160 ******************************************************************************/
161
162 /* Prints a list for debugging. */
163
164 static void printints ( Pair l ) {
165 System.out.print( "(" );
166 while (l != null) {
167 System.out.print( Pair.car(l) );
168 l = Pair.cdr(l);
169 if (l != null) System.out.print( " " );
170 }
171 System.out.println( ")" );
172 }
173
174 static void printperms( Perm perms ) {
175 while (perms != null) {
176 printints( Perm.car(perms) );
177 perms = Perm.cdr(perms);
178 }
179 System.out.println( "" );
180 }
181 }
182
183 /* eof */