Menu

[ba831b]: / src / tree.cpp  Maximize  Restore  History

Download this file

280 lines (253 with data), 7.6 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//////////////////////////////////////////////////////////////////////
//
// FILE: tree.cpp
// TreeCache class methods
//
// Part of: Scid (Shane's Chess Information Database)
// Version: 2.4
//
// Notice: Copyright (c) 2000 Shane Hudson. All rights reserved.
//
// Author: Shane Hudson (sgh@users.sourceforge.net)
//
//////////////////////////////////////////////////////////////////////
#include "common.h"
#include "position.h"
#include "tree.h"
#include <string.h>
void
initTreeNode (treeNodeT * tnode)
{
tnode->freq[RESULT_White] = tnode->freq[RESULT_Black]
= tnode->freq[RESULT_Draw] = tnode->freq[RESULT_None] = 0;
for (uint i=0; i < 8; i++) { tnode->san[i] = 0; }
tnode->total = 0;
tnode->score = 0;
tnode->ecoCode = 0;
tnode->eloCount = 0;
tnode->eloSum = 0;
tnode->perfCount = 0;
tnode->perfSum = 0;
tnode->yearCount = 0;
tnode->yearSum = 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::Init():
//
void
TreeCache::Init ()
{
CacheSize = 0;
NumInUse = 0;
Cache = NULL;
MostRecentIndex = 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::Delete():
//
void
TreeCache::Delete ()
{
if (CacheSize > 0) {
ASSERT (Cache != NULL);
for (uint i=0; i < NumInUse; i++) {
if (Cache[i].cfilter != NULL) { delete Cache[i].cfilter; }
}
#ifdef WINCE
my_Tcl_Free((char*) Cache);
#else
delete[] Cache;
#endif
}
CacheSize = 0;
NumInUse = 0;
Cache = NULL;
MostRecentIndex = 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::SetCacheSize():
//
void
TreeCache::SetCacheSize (uint size)
{
if (CacheSize > 0) { Delete(); }
#ifdef WINCE
Cache = (cachedTreeT*) my_Tcl_Alloc( sizeof(cachedTreeT [size]));
#else
Cache = new cachedTreeT [size];
#endif
CacheSize = size;
NumInUse = 0;
MostRecentIndex = 0;
// Clear all the filters and nodes so they don't contain garbage:
for (uint i=0; i < size; i++) {
cachedTreeT * ctree = &(Cache[i]);
ctree->cfilter = NULL;
for (uint count = 0; count < MAX_TREE_NODES; count++) {
initTreeNode (&(ctree->tree.node[count]));
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::CacheResize():
//
void
TreeCache::CacheResize (uint size)
{
// If cache size decreases, simply reset it
if (CacheSize > size) {
SetCacheSize(size);
return;
}
cachedTreeT* oldCache = Cache;
uint oldSize = CacheSize;
#ifdef WINCE
Cache = (cachedTreeT*) my_Tcl_Alloc( sizeof(cachedTreeT [size]));
#else
Cache = new cachedTreeT [size];
#endif
CacheSize = size;
// Clear all the filters and nodes so they don't contain garbage:
for (uint i=0; i < size; i++) {
cachedTreeT * ctree = &(Cache[i]);
ctree->cfilter = NULL;
for (uint count = 0; count < MAX_TREE_NODES; count++) {
initTreeNode (&(ctree->tree.node[count]));
}
}
// copy old data to new Cache
for (uint i=0; i < oldSize; i++) {
cachedTreeT * ctree = &(oldCache[i]);
Cache[i] = *ctree;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::Clear():
//
void
TreeCache::Clear ()
{
for (uint i=0; i < NumInUse; i++) {
if (Cache[i].cfilter != NULL) { delete Cache[i].cfilter; }
}
NumInUse = 0;
MostRecentIndex = 0;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::Lookup():
// Lookup a position in the tree cache.
//
cachedTreeT *
TreeCache::Lookup (Position * pos)
{
for (uint i=0; i < NumInUse; i++) {
if (Cache[i].toMove != pos->GetToMove()) { continue; }
pieceT * board = pos->GetBoard();
pieceT * board2 = Cache[i].board;
bool found = true;
for (squareT sq=A1; sq <= H8; sq++, board++, board2++) {
if (*board != *board2) { found = false; break; }
}
if (found) { return &(Cache[i]); }
}
// Ended the search, no match:
return NULL;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::LookupIndex():
// Lookup a position in the tree cache, and return its index or -1
// if it is not in the cache.
//
int
TreeCache::LookupIndex (Position * pos)
{
for (uint i=0; i < NumInUse; i++) {
if (Cache[i].toMove != pos->GetToMove()) { continue; }
pieceT * board = pos->GetBoard();
pieceT * board2 = Cache[i].board;
bool found = true;
for (squareT sq=A1; sq <= H8; sq++, board++, board2++) {
if (*board != *board2) { found = false; break; }
}
if (found) { return (int)i; }
}
// Ended the search, no match:
return -1;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::AddTree(): Add a positions tree data to the cache.
//
void
TreeCache::AddTree (uint index, Position * pos, treeT * pTree, Filter * filter)
{
ASSERT (index < CacheSize);
cachedTreeT * pct = &(Cache[index]);
if (index < NumInUse) {
ASSERT (pct->cfilter != NULL);
delete pct->cfilter;
}
// Copy the tree structure:
pct->tree = *pTree;
// Copy the filter and board info:
pct->cfilter = new CompressedFilter;
pct->cfilter->CompressFrom (filter);
pct->toMove = pos->GetToMove();
memcpy (pct->board, pos->GetBoard(), 64);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// TreeCache::Add():
// Add a position to the cache if applicable..
//
bool
TreeCache::Add (Position * pos, treeT * pTree, Filter * filter)
{
// Is this tree in the cache already?
int index = LookupIndex (pos);
if (index >= 0) {
// It is! Set the MostRecentIndex:
MostRecentIndex = index;
return true;
}
// Quick test for the common condition of the cache being full and
// this tree having a count too low to be added:
if (Policy == TREECACHE_Smallest && NumInUse == CacheSize
&& pTree->totalCount < LowestTotal) {
return false;
}
// Now, we add this tree to the end of the cache it is not full, or
// if it is full, check if its count is high enough to be added.
if (NumInUse == CacheSize) {
// Cache is full!
// If replacing the smallest, we know its total is high enough
// to be added, from the test above.
if (Policy == TREECACHE_Oldest) {
// Replace the oldest node:
MostRecentIndex = (MostRecentIndex+1) % CacheSize;
AddTree (MostRecentIndex, pos, pTree, filter);
} else {
// Replace the smallest node:
AddTree (LowestTotalIndex, pos, pTree, filter);
// Find the NEW lowest total, the next tree to be evicted:
LowestTotal = pTree->totalCount;
for (uint i=0; i < CacheSize; i++) {
if (Cache[i].tree.totalCount < LowestTotal) {
LowestTotal = Cache[i].tree.totalCount;
LowestTotalIndex = i;
}
}
}
} else {
// Cache is not yet full. Add the position to the cache:
AddTree (NumInUse, pos, pTree, filter);
// Update LowestTotal if necessary:
if (NumInUse == 0 || pTree->totalCount < LowestTotal) {
LowestTotal = pTree->totalCount;
LowestTotalIndex = NumInUse;
}
NumInUse++;
}
return true;
}
//////////////////////////////////////////////////////////////////////
// EOF: tree.cpp
//////////////////////////////////////////////////////////////////////