-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathmq.h
More file actions
194 lines (156 loc) · 3.83 KB
/
mq.h
File metadata and controls
194 lines (156 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef PACHI_MQ_H
#define PACHI_MQ_H
/* Move queues; in fact, they are more like move lists, usually used
* to accumulate equally good move candidates, then choosing from them
* randomly. But they are also used to juggle group lists (using the
* fact that coord_t == group_t). */
#include <assert.h>
#include <stdlib.h>
#include "move.h"
#include "random.h"
#define MQL 512 /* XXX: On larger board this might not be enough. */
/* Move queue */
typedef struct {
int moves;
coord_t move[MQL];
} mq_t;
static void mq_init(mq_t *q);
/* Pick a random move from the queue. */
static coord_t mq_pick(mq_t *q);
/* Add a move to the queue (no dupe check). */
static void mq_add(mq_t *q, coord_t c);
/* Add a move to the queue (except if already in). */
#define mq_add_nodup(q, c) do { mq_add((q), (c)); mq_nodup(q); } while(0)
/* Remove move from queue */
static void mq_remove(mq_t *q, coord_t c);
/* Remove i'th item from queue */
static void mq_remove_index(mq_t *q, int i);
/* Is move in the queue ? */
static bool mq_has(mq_t *q, coord_t c);
/* Same but return coord index, or -1 if not found. */
static int mq_index(mq_t *q, coord_t c);
/* Cat two queues together. */
static void mq_append(mq_t *qd, mq_t *qs);
/* Copy qs into qd */
static void mq_copy(mq_t *qd, mq_t *qs);
/* Subtract two queues (find elements in a not in b) */
static void mq_sub(mq_t *a, mq_t *b, mq_t *res);
/* Check if the last move in queue is not a dupe, and remove it
* in that case. */
static void mq_nodup(mq_t *q);
/* Sort moves in canonical order */
static void mq_sort(mq_t *q);
/* Print queue contents. */
static int mq_print_file(mq_t *q, FILE *f, char *label);
/* Print queue contents on stderr. */
static int mq_print(mq_t *q, char *label);
static void mq_print_line(mq_t *q, char *label);
static inline void
mq_init(mq_t *q)
{
q->moves = 0;
}
static inline coord_t
mq_pick(mq_t *q)
{
return q->moves ? q->move[fast_random(q->moves)] : pass;
}
static inline void
mq_add(mq_t *q, coord_t c)
{
assert(q->moves < MQL);
q->move[q->moves++] = c;
}
static inline void
mq_remove(mq_t *q, coord_t c)
{
for (int i = 0; i < q->moves; i++)
if (q->move[i] == c)
mq_remove_index(q, i--);
}
static inline void
mq_remove_index(mq_t *q, int i)
{
assert(q->moves);
q->move[i] = q->move[q->moves-- - 1];
}
static inline bool
mq_has(mq_t *q, coord_t c)
{
for (int i = 0; i < q->moves; i++)
if (q->move[i] == c)
return true;
return false;
}
static inline int
mq_index(mq_t *q, coord_t c)
{
for (int i = 0; i < q->moves; i++)
if (q->move[i] == c)
return i;
return -1;
}
static inline void
mq_append(mq_t *qd, mq_t *qs)
{
assert(qd->moves + qs->moves < MQL);
memcpy(&qd->move[qd->moves], qs->move, qs->moves * sizeof(qs->move[0]));
qd->moves += qs->moves;
}
static inline void
mq_copy(mq_t *qd, mq_t *qs)
{
qd->moves = qs->moves;
memcpy(qd->move, qs->move, qs->moves * sizeof(qs->move[0]));
}
static inline void
mq_sub(mq_t *a, mq_t *b, mq_t *res)
{
int n = a->moves;
for (int i = 0; i < n; i++)
if (!mq_has(b, a->move[i]))
mq_add(res, a->move[i]);
}
static inline void
mq_nodup(mq_t *q)
{
int n = q->moves;
for (int i = 0; i < n - 1; i++) {
if (q->move[i] == q->move[n - 1]) {
q->moves--;
return;
}
}
}
static inline int
mq_sort_compare(const void *p1, const void *p2)
{
coord_t *c1 = (coord_t *)p1;
coord_t *c2 = (coord_t *)p2;
return (*c1 - *c2);
}
static inline void
mq_sort(mq_t *q)
{
qsort(q->move, q->moves, sizeof(q->move[0]), mq_sort_compare);
}
static inline int
mq_print_file(mq_t *q, FILE *f, char *label)
{
int n = fprintf(f, "%s", label);
for (int i = 0; i < q->moves; i++)
n += fprintf(f, "%s ", coord2sstr(q->move[i]));
return n;
}
static inline int
mq_print(mq_t *q, char *label)
{
return mq_print_file(q, stderr, label);
}
static inline void
mq_print_line(mq_t *q, char *label)
{
mq_print(q, label);
fprintf(stderr, "\n");
}
#endif