~ubuntu-branches/ubuntu/trusty/polyglot/trusty

« back to all changes in this revision

Viewing changes to list.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Korff
  • Date: 2005-10-09 21:56:20 UTC
  • Revision ID: james.westby@ubuntu.com-20051009215620-dcuqynujzvmiglpj
Tags: upstream-1.3
ImportĀ upstreamĀ versionĀ 1.3

Show diffs side-by-side

added added

removed removed

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