-
Notifications
You must be signed in to change notification settings - Fork 27
/
memo.go
55 lines (46 loc) · 874 Bytes
/
memo.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
package rush
type MemoKey [MaxPieces]int
func MakeMemoKey(pieces []Piece) MemoKey {
var key MemoKey
for i, piece := range pieces {
key[i] = piece.Position
}
return key
}
func (a *MemoKey) Less(b *MemoKey, primary bool) bool {
var i int
if !primary {
i++
}
for ; i < MaxPieces; i++ {
if a[i] != b[i] {
return a[i] < b[i]
}
}
return false
}
type Memo struct {
data map[MemoKey]int
hits uint64
}
func NewMemo() *Memo {
data := make(map[MemoKey]int)
return &Memo{data, 0}
}
func (memo *Memo) Size() int {
return len(memo.data)
}
func (memo *Memo) Hits() uint64 {
return memo.hits
}
func (memo *Memo) Add(key *MemoKey, depth int) bool {
memo.hits++
if before, ok := memo.data[*key]; ok && before >= depth {
return false
}
memo.data[*key] = depth
return true
}
func (memo *Memo) Set(key *MemoKey, depth int) {
memo.data[*key] = depth
}