Debian Codegraph @

benchmarks.larceny/src/triangl.c

raw
1 #include <stdio.h>
2
3 /*---------------------------------------------------------------------------*/
4
5 /* Support for Scheme like stuff */
6
7 #ifdef FAST_ALLOCATOR
8 #define CUSTOM_ALLOC 1 /* Set to 1 for custom allocation */
9 #else
10 #define CUSTOM_ALLOC 0
11 #endif
12
13 int *alloc;
14
15 #define CAR(pair) *(pair)
16 #define CDR(pair) *((pair)+1)
17
18 #if CUSTOM_ALLOC
19
20 int *old_alloc;
21
22 #define CONS(car,cdr) ( *(--alloc) = cdr, *(--alloc) = car, alloc )
23
24 #define HEAP_SIZE 1000000
25
26 void init()
27 {
28 /* char *heap = (char *) malloc( HEAP_SIZE ); */
29 int heap_size_in_bytes = HEAP_SIZE * sizeof(int);
30 int * heap = (int *) malloc( heap_size_in_bytes );
31
32 if (heap == NULL)
33 {
34 printf( "Not enough memory (%d bytes needed)\n", heap_size_in_bytes );
35 exit(1);
36 }
37
38 alloc = heap + HEAP_SIZE; /* alloc from end */
39 }
40
41 #else
42
43 #define CONS(car,cdr) ( alloc = (int *) malloc(2*sizeof(int)), \
44 *(alloc) = (car), \
45 *(alloc+1) = (cdr), \
46 alloc)
47
48 void init()
49 {
50 return;
51 }
52
53 #endif
54
55 /* Other globals */
56
57 int result;
58
59 /*=============================================================================
60
61 THE BENCHMARKS
62
63 =============================================================================*/
64
65 /*-----------------------------------------------------------------------------
66
67 ;;; TRIANGL -- Board game benchmark.
68
69 (define *board*
70 (list->vector '(1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1)))
71
72 (define *sequence*
73 (list->vector '(0 0 0 0 0 0 0 0 0 0 0 0 0 0)))
74
75 (define *a*
76 (list->vector '(1 2 4 3 5 6 1 3 6 2 5 4 11 12
77 13 7 8 4 4 7 11 8 12 13 6 10
78 15 9 14 13 13 14 15 9 10
79 6 6)))
80
81 (define *b*
82 (list->vector '(2 4 7 5 8 9 3 6 10 5 9 8
83 12 13 14 8 9 5 2 4 7 5 8
84 9 3 6 10 5 9 8 12 13 14
85 8 9 5 5)))
86
87 (define *c*
88 (list->vector '(4 7 11 8 12 13 6 10 15 9 14 13
89 13 14 15 9 10 6 1 2 4 3 5 6 1
90 3 6 2 5 4 11 12 13 7 8 4 4)))
91
92 (define *answer* '())
93
94 (define (attempt i depth)
95 (cond ((= depth 14)
96 (set! *answer*
97 (cons (cdr (vector->list *sequence*)) *answer*))
98 #t)
99 ((and (= 1 (vector-ref *board* (vector-ref *a* i)))
100 (= 1 (vector-ref *board* (vector-ref *b* i)))
101 (= 0 (vector-ref *board* (vector-ref *c* i))))
102 (vector-set! *board* (vector-ref *a* i) 0)
103 (vector-set! *board* (vector-ref *b* i) 0)
104 (vector-set! *board* (vector-ref *c* i) 1)
105 (vector-set! *sequence* depth i)
106 (do ((j 0 (+ j 1))
107 (depth (+ depth 1)))
108 ((or (= j 36) (attempt j depth)) #f))
109 (vector-set! *board* (vector-ref *a* i) 1)
110 (vector-set! *board* (vector-ref *b* i) 1)
111 (vector-set! *board* (vector-ref *c* i) 0) #f)
112 (else #f)))
113
114 (define (test)
115 (set! *answer* '())
116 (attempt 22 1)
117 (car *answer*))
118
119 (define (main . args)
120 (run-benchmark
121 "triangl"
122 triangl-iters
123 (lambda () (test))
124 (lambda (result) (equal? result '(22 34 31 15 7 1 20 17 25 6 5 13 32)))))
125 */
126
127 /* Given a list of integers, prints it. For debugging. */
128
129 void printlist (x)
130 int * x;
131 {
132 printf ("( ");
133 while (x != NULL) {
134 printf ("%d ", CAR(x));
135 x = (int *) CDR(x);
136 }
137 printf (")\n");
138 }
139
140 /* Given an array v of length n, returns a list with the same elements. */
141
142 int *vector_list(v, n)
143 int *v;
144 int n;
145 {
146 register int i, *l = NULL;
147 for (i = n-1; i >= 0; i--) l = CONS( v[i], (int) l );
148 return l;
149 }
150
151 /* Given two lists of integers, are they the same? */
152
153 int equalp (x, y)
154 int *x;
155 int *y;
156 {
157 if (x == NULL)
158 return y == NULL;
159 else if (y == NULL)
160 return 0;
161 else if (CAR(x) == CAR(y))
162 return equalp ((int *) CDR(x), (int *) CDR(y));
163 else
164 return 0;
165 }
166
167 int _board_[] = {1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1};
168 int _sequence_[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
169 int _a_[] = {1,2,4,3,5,6,1,3,6,2,5,4,11,12,
170 13,7,8,4,4,7,11,8,12,13,6,10,
171 15,9,14,13,13,14,15,9,10,
172 6,6};
173 int _b_[] = {2,4,7,5,8,9,3,6,10,5,9,8,
174 12,13,14,8,9,5,2,4,7,5,8,
175 9,3,6,10,5,9,8,12,13,14,
176 8,9,5,5};
177 int _c_[] = {4,7,11,8,12,13,6,10,15,9,14,13,
178 13,14,15,9,10,6,1,2,4,3,5,6,1,
179 3,6,2,5,4,11,12,13,7,8,4,4};
180 int *_answer_ = NULL;
181
182 int attempt(i,depth)
183 int i,depth;
184 {
185 if (depth == 14)
186 {
187 int * temp = (int *) CDR( vector_list( _sequence_, 14 ) );
188 _answer_ = CONS( (int) temp, (int) _answer_ );
189 return 1;
190 }
191 else
192 if ((_board_[_a_[i]] == 1) &&
193 (_board_[_b_[i]] == 1) &&
194 (_board_[_c_[i]] == 0))
195 {
196 int j;
197 _board_[_a_[i]] = 0;
198 _board_[_b_[i]] = 0;
199 _board_[_c_[i]] = 1;
200 _sequence_[depth] = i;
201 depth = depth + 1;
202 for (j = 0; (j != 36) && !attempt(j,depth); j++) ;
203 _board_[_a_[i]] = 1;
204 _board_[_b_[i]] = 1;
205 _board_[_c_[i]] = 0;
206 return 0;
207 }
208 else
209 return 0;
210 }
211
212 int test(i)
213 int i;
214 {
215 _answer_ = NULL;
216 attempt(i,1);
217 return CAR(_answer_);
218 }
219
220 void test_triangle()
221 {
222 result = test(22);
223 }
224
225 /*===========================================================================*/
226
227 int correct[] = {22, 34, 31, 15, 7, 1, 20, 17, 25, 6, 5, 13, 32};
228
229 int main (argc, argv)
230 int argc;
231 char *argv[];
232 {
233 int i;
234
235 init();
236 for (i=0; i<10; i++)
237 test_triangle();
238 /* printlist (result); */
239 if (!equalp (result, vector_list (correct, 13)))
240 printf ("*** wrong result ***\n");
241 return 0;
242 }