-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransp_table.go
94 lines (83 loc) · 2.04 KB
/
transp_table.go
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
package main
import (
"log"
"sync"
)
type TranspTable struct {
entryByHash map[ZHash]TTEntry
mu sync.Mutex
}
func NewTranspTable() *TranspTable {
return &TranspTable{
entryByHash: make(map[ZHash]TTEntry),
}
}
func (tt *TranspTable) PostResults(hash ZHash, score int16, isLowerBound bool, move Move, depth uint8) {
tt.mu.Lock()
defer tt.mu.Unlock()
prevEntry, ok := tt.entryByHash[hash]
if ok {
if prevEntry.Depth > depth {
return
}
isBetterEst := !prevEntry.IsExact() && !isLowerBound
if prevEntry.Depth == depth && !isBetterEst {
return
}
}
tt.entryByHash[hash] = TTEntry{
Score: score,
IsLowerBound: isLowerBound,
Depth: depth,
Move: move,
}
}
func (tt *TranspTable) GetEntry(hash ZHash) (entry TTEntry, exists bool) {
tt.mu.Lock()
defer tt.mu.Unlock()
var ok bool
if entry, ok = tt.entryByHash[hash]; ok {
return entry, true
}
return
}
func (tt *TranspTable) Line(pos *Position, depth uint8) []Move {
nMoves := depth
frozenPoss := make([]*FrozenPos, nMoves)
capturedPieces := make([]Piece, nMoves)
line := make([]Move, nMoves)
for moveIdx := uint8(0); moveIdx < nMoves; moveIdx++ {
entry, entryExists := tt.GetEntry(pos.hash)
if DEBUG {
if !entryExists {
log.Fatalf("could not get entry for line after %s at depth %d", pos.FEN(), depth)
}
if entry.Depth < depth {
log.Fatalf("entry depth (%d) lower than requested depth (%d) while building line", entry.Depth, depth)
}
}
line[moveIdx] = entry.Move
isLastMove := moveIdx == nMoves-1
if !isLastMove {
capturedPieces[moveIdx], frozenPoss[moveIdx] = pos.MakeMove(entry.Move)
}
}
//undo moves on pos
for moveIdx := int(nMoves) - 2; moveIdx >= 0; moveIdx-- {
move := line[moveIdx]
captPiece := capturedPieces[moveIdx]
frozenPos := frozenPoss[moveIdx]
pos.UnmakeMove(move, frozenPos, captPiece)
}
return line
}
type TTEntryType uint8
type TTEntry struct {
Score int16
IsLowerBound bool
Depth uint8
Move Move
}
func (e *TTEntry) IsExact() bool {
return !e.IsLowerBound
}