-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpvtable.hpp
92 lines (68 loc) · 1.73 KB
/
pvtable.hpp
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
#pragma once
#include <memory>
#include <stdexcept>
#include "move.hpp"
#include "board.hpp"
#include "movegen.hpp"
#include "bitboard.hpp"
namespace pvtable {
struct Entry {
Entry(bitboard::Bitboard position_key, move::Move move) : position_key(position_key), move(move) {}
Entry() : position_key(), move() {}
move::Move move;
bitboard::Bitboard position_key;
};
class PVTable {
public:
PVTable(size_t size) : size(size) {
data = std::make_unique<Entry[]>(size);
}
PVTable() : size(10000) {
data = std::make_unique<Entry[]>(size);
}
void clear() {
// maybe a bit slow, but no need for optimization
for (int i = 0; i < size; i++) {
data[i].move = {};
data[i].position_key = {};
}
}
void add(bitboard::Bitboard key, move::Move value) {
size_t index = key % size;
assert(0 <= index && index < size);
data[index] = Entry(key, value);
}
move::Move probe(bitboard::Bitboard key) const {
size_t index = key % size;
assert(0 <= index && index < size);
if (data[index].position_key == key)
return data[index].move;
return {};
}
int getLine(board::BoardState& state, int depth) {
state.pv_array.clear();
move::Move move = probe(state.position_key);
int count = 0;
for (int i = 0; i < depth; i++) {
if (movegen::moveExists(state, move)) {
state.step(move);
state.pv_array.push_back(move);
count++;
}
else {
break;
}
move = probe(state.position_key);
if (move.isNull())
break;
}
while (state.ply > 0) {
state.undo();
}
return count;
}
private:
size_t size;
std::unique_ptr<Entry[]> data;
};
}