-
Notifications
You must be signed in to change notification settings - Fork 43
/
AICarbonHash.h
135 lines (116 loc) · 3.12 KB
/
AICarbonHash.h
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
/*
* AICarbon.h
* 08.01.2002, Michal Czardybon
*
* Tablica transpozycji
*
*/
#ifndef _AICARBONHASH
#define _AICARBONHASH
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <assert.h>
#include "OXTypes.h"
extern const ULONG hashValA[4096], hashValB[4096], hashValC[4096];
extern int info_max_memory;
class HashTable
{
private:
struct HashRec
{
ULONG hashB, hashC;
short value;
short depth;
#ifndef NDEBUG
short moves;
#endif
OXPoint best;
};
public:
// kasowanie wszystkiego
void clear()
{
hashASize = 0;
}
// czy aktualna pozycja jest zapamietana
bool present()
{
return currentItem->hashB == hashB && currentItem->hashC == hashC;
}
// uwzglednij wykonanie ruchu
void move(int x, int y, OXPiece who)
{
int offset = x + (y << 6) + (who << 11);
hashA = (hashA + hashValA[offset]) % hashASize;
currentItem = &elem[hashA];
hashB = (hashB + hashValB[offset]);
hashC = (hashC + hashValC[offset]);
}
// cofanie <move>
void undo(int x, int y, OXPiece who)
{
int offset = x + (y << 6) + (who << 11);
hashA = ((int)(hashA - hashValA[offset]) % (int)hashASize);
if((int)hashA < 0) hashA += hashASize;
currentItem = &elem[hashA];
hashB = (hashB - hashValB[offset]);
hashC = (hashC - hashValC[offset]);
}
short value() {return currentItem->value; } // wartosc pozycji
short depth() {return currentItem->depth;} // glebokosc na jaka zostala przeszukana
#ifndef NDEBUG
short moves() {return currentItem->moves;} // ktory to ruch od poczatku gry
#endif
OXPoint best() {return currentItem->best;} // najlepszy ruch w tej sytuacji
// zapamietuje biezaca sytuacje
void update(short _value, short _depth, short _moves, OXPoint _best)
{
HashRec* c = currentItem;
c->value = _value;
c->depth = _depth;
#ifndef NDEBUG
c->moves = _moves;
#endif
c->hashB = hashB;
c->hashC = hashC;
c->best = _best;
}
void resize(ULONG size)
{
if(size > hashASize)
{
ULONG maxBytes = info_max_memory;
if(maxBytes == 0) maxBytes = 1000000000; //1GB
maxBytes = __max(maxBytes, 7500000) - 7000000;
ULONG num = maxBytes / sizeof(HashRec);
if(maxSize * 2 < num || maxSize > num){
elem = (HashRec*)realloc(elem, maxBytes);
maxSize = num;
}
if(hashASize < maxSize){
hashASize = __min(size * 2, maxSize);
memset(elem, 0, hashASize * sizeof(HashRec));
}
}
}
HashTable()
{
maxSize = 0;
hashASize = 0;
elem = 0;
#ifndef NDEBUG
for(int n = 0; n < 4096; n++) assert(hashValA[n] < 2000000000);
#endif
}
~HashTable()
{
if(elem) free(elem);
}
private:
HashRec* currentItem;
ULONG hashASize, maxSize;
ULONG hashA, hashB, hashC; // sygnatura biezacej sytuacji
HashRec* elem; // tablica
};
#endif