forked from michiguel/Ordo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relpman.c
205 lines (165 loc) · 3.19 KB
/
relpman.c
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
195
196
197
198
199
200
201
202
203
204
/*
Ordo is program for calculating ratings of engine or chess players
Copyright 2013 Miguel A. Ballicora
This file is part of Ordo.
Ordo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ordo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ordo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdio.h>
#include "mymem.h"
#include "mytypes.h"
#include "relpman.h"
//================================================
static void
rpblock_clear (rpblock_t *r)
{
assert (r);
r->n = 0;
r->sz = MAX_RPBLOCK;
r->next = NULL;
}
static rpblock_t *
rpblock_create(void)
{
rpblock_t *q = memnew (sizeof(rpblock_t));
if (q) rpblock_clear(q);
return q;
}
static void
rpblock_destroy(rpblock_t *p)
{
assert(p);
rpblock_clear(p);
memrel(p);
return;
}
static bool_t
rpblock_add (rpblock_t *b, rpunit_t *u)
{
rpblock_t *q;
rpblock_t *t;
assert(b);
assert(u);
q = b;
assert(q->n <= q->sz);
if (q->n == q->sz) {
t = rpblock_create();
if (t) {
q->next = t;
q = t;
q->n = 0;
} else {
return FALSE;
}
}
q->u[q->n++] = *u;
return TRUE;
}
//------------------ Relative Priors Manager Functions ----------------------------
bool_t
rpman_init (struct rpmanager *rm)
{
rpblock_t *q = rpblock_create();
assert(rm);
rm->first = q;
rm->last = q;
rm->p = NULL;
rm->i = 0;
return q != NULL;
}
void
rpman_done (struct rpmanager *rm)
{
rpblock_t *p;
rpblock_t *q;
assert(rm);
p = rm->first;
rm->first = NULL;
rm->last = NULL;
rm->p = NULL;
rm->i = 0;
while(p) {
q = p->next;
rpblock_destroy(p);
p = q;
}
return;
}
size_t
rpman_count(struct rpmanager *rm)
{
rpblock_t *p;
size_t count = 0;
assert(rm);
for (p = rm->first; p != NULL; p = p->next) {
count += p->n;
}
return count;
}
void
rpman_parkstart(struct rpmanager *rm)
{
assert(rm);
rm->i = 0;
rm->p = rm->first;
}
rpunit_t *
rpman_pointnext_unit(struct rpmanager *rm)
{
size_t i;
rpblock_t *p;
rpunit_t *pu;
assert(rm);
p = rm->p;
i = rm->i;
assert(i <= p->n);
while (i == p->n) {
p = p->next;
if (NULL == p) {
return NULL;
}
i = 0;
}
pu = &p->u[i++];
rm->p = p;
rm->i = i;
return pu;
}
bool_t
rpman_add_unit(struct rpmanager *rm, rpunit_t *u)
{
bool_t ok;
rpblock_t *p;
assert(rm);
p = rm->last;
assert(p);
ok = rpblock_add (p, u);
if (ok && NULL != p->next) rm->last = p->next; // next was created
return ok;
}
rpunit_t *
rpman_to_newarray (struct rpmanager *rm, player_t *psz)
{
rpunit_t *p_ret;
rpunit_t *p;
rpunit_t *pu;
player_t sz = (player_t)rpman_count (rm);
p = memnew(sizeof(rpunit_t) * (size_t) sz);
p_ret = p;
if (p == NULL) return p;
rpman_parkstart (rm);
while (NULL != (pu = rpman_pointnext_unit(rm))) {
*p++ = *pu;
}
*psz = sz;
return p_ret;
}