Add forgotten files 1.4.70b
[polyglot.git] / list.c
1
2 // list.c
3
4 // includes
5
6 #include "board.h"
7 #include "list.h"
8 #include "move.h"
9 #include "util.h"
10
11 // functions
12
13 // list_is_ok()
14
15 bool list_is_ok(const list_t * list) {
16
17    if (list == NULL) return FALSE;
18
19    if (list->size >= ListSize) return FALSE;
20
21    return TRUE;
22 }
23
24 // list_clear()
25
26 void list_clear(list_t * list) {
27
28    ASSERT(list!=NULL);
29
30    list->size = 0;
31 }
32
33 // list_add
34
35 void list_add(list_t * list, int move){
36     list_add_ex(list, move, 0);
37 }
38
39 // list_add_ex()
40
41 void list_add_ex(list_t * list, int move, int value) {
42
43    ASSERT(list_is_ok(list));
44    ASSERT(move_is_ok(move));
45    ASSERT(value>=-32767&&value<=+32767);
46
47    ASSERT(list->size<ListSize-1);
48
49    list->move[list->size] = move;
50    list->value[list->size] = value;
51    list->size++;
52 }
53
54 // list_remove()
55
56 void list_remove(list_t * list, int index) {
57
58    int i;
59
60    ASSERT(list_is_ok(list));
61    ASSERT(index>=0&&index<list->size);
62
63    for (i = index; i < list->size-1; i++) {
64       list->move[i] = list->move[i+1];
65       list->value[i] = list->value[i+1];
66    }
67
68    list->size--;
69 }
70
71 // list_is_empty()
72
73 bool list_is_empty(const list_t * list) {
74
75    ASSERT(list_is_ok(list));
76
77    return list->size == 0;
78 }
79
80 // list_size()
81
82 int list_size(const list_t * list) {
83
84    ASSERT(list_is_ok(list));
85
86    return list->size;
87 }
88
89 // list_move()
90
91 int list_move(const list_t * list, int index) {
92
93    ASSERT(list_is_ok(list));
94    ASSERT(index>=0&&index<list->size);
95
96    return list->move[index];
97 }
98
99 // list_value()
100
101 int list_value(const list_t * list, int index) {
102
103    ASSERT(list_is_ok(list));
104    ASSERT(index>=0&&index<list->size);
105
106    return list->value[index];
107 }
108
109 // list_copy()
110
111 void list_copy(list_t * dst, const list_t * src) {
112
113    int i;
114
115    ASSERT(dst!=NULL);
116    ASSERT(list_is_ok(src));
117
118    dst->size = src->size;
119
120    for (i = 0; i < src->size; i++) {
121       dst->move[i] = src->move[i];
122       dst->value[i] = src->value[i];
123    }
124 }
125
126 // list_move_to_front()
127
128 void list_move_to_front(list_t * list, int index) {
129
130    int i;
131    int move, value;
132
133    ASSERT(list_is_ok(list));
134    ASSERT(index>=0&&index<list->size);
135
136    if (index != 0) {
137
138       move = list->move[index];
139       value = list->value[index];
140
141       for (i = index; i > 0; i--) {
142          list->move[i] = list->move[i-1];
143          list->value[i] = list->value[i-1];
144       }
145
146       list->move[0] = move;
147       list->value[0] = value;
148    }
149 }
150
151 // list_note()
152
153 void list_note(list_t * list) {
154
155    int i, move;
156
157    ASSERT(list_is_ok(list));
158
159    for (i = 0; i < list->size; i++) {
160       move = list->move[i];
161       ASSERT(move_is_ok(move));
162       list->value[i] = -move_order(move);
163    }
164 }
165
166 // list_sort()
167
168 void list_sort(list_t * list) {
169
170    int i, j;
171    int best_index, best_move, best_value;
172
173    ASSERT(list_is_ok(list));
174
175    for (i = 0; i < list->size-1; i++) {
176
177       best_index = i;
178       best_value = list->value[i];
179
180       for (j = i+1; j < list->size; j++) {
181          if (list->value[j] > best_value) {
182             best_index = j;
183             best_value = list->value[j];
184          }
185       }
186
187       if (best_index != i) {
188
189          best_move = list->move[best_index];
190          ASSERT(best_value==list->value[best_index]);
191
192          for (j = best_index; j > i; j--) {
193             list->move[j] = list->move[j-1];
194             list->value[j] = list->value[j-1];
195          }
196
197          list->move[i] = best_move;
198          list->value[i] = best_value;
199       }
200    }
201
202    if (DEBUG) {
203       for (i = 0; i < list->size-1; i++) {
204          ASSERT(list->value[i]>=list->value[i+1]);
205       }
206    }
207 }
208
209 // list_contain()
210
211 bool list_contain(const list_t * list, int move) {
212
213    int i;
214
215    ASSERT(list_is_ok(list));
216    ASSERT(move_is_ok(move));
217
218    for (i = 0; i < list->size; i++) {
219       if (list->move[i] == move) return TRUE;
220    }
221
222    return FALSE;
223 }
224
225 // list_equal()
226
227 bool list_equal(list_t * list_1, list_t * list_2) {
228
229    list_t copy_1[1], copy_2[1];
230    int i;
231
232    ASSERT(list_is_ok(list_1));
233    ASSERT(list_is_ok(list_2));
234
235    if (list_1->size != list_2->size) return FALSE;
236
237    list_copy(copy_1,list_1);
238    list_note(copy_1);
239    list_sort(copy_1);
240
241    list_copy(copy_2,list_2);
242    list_note(copy_2);
243    list_sort(copy_2);
244
245    for (i = 0; i < copy_1->size; i++) {
246       if (copy_1->move[i] != copy_2->move[i]) return FALSE;
247    }
248
249    return TRUE;
250 }
251
252 // list_disp()
253
254 void list_disp(const list_t * list, const board_t * board) {
255
256    int i, move, value;
257    char string[256];
258
259    ASSERT(list_is_ok(list));
260    ASSERT(board_is_ok(board));
261
262    for (i = 0; i < list->size; i++) {
263
264       move = list->move[i];
265       value = list->value[i];
266
267       if (!move_to_can(move,board,string,256)) ASSERT(FALSE);
268       my_log("POLYGLOT %-5s %04X %+4d\n",string,move,value);
269    }
270
271    my_log("POLYGLOT\n");
272 }
273
274 // end of list.cpp
275