-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.coffee
51 lines (42 loc) · 1.16 KB
/
cache.coffee
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
{lru_cache} = require './util'
make_key = (board) -> JSON.stringify board.board
module.exports = (size) ->
cache = lru_cache(size)
get: (board, turn, depth, lower, upper) ->
item = cache.get(make_key board)
return null unless item
[t, d, l, u] = item
return null if d < depth
if t != turn
[l, u] = [-u, -l]
if l >= upper
l
else if u <= lower
u
else if u == l
u
else
null
get_lower: (board, turn, depth) ->
item = cache.get(make_key board)
return null unless item
[t, d, l, u] = item
return null if d < depth
if t != turn then -u else l
set: (board, turn, depth, lower, upper, value) ->
key = make_key board
item = cache.get(key)
return if item and item[0] > depth
if value >= upper
item = [turn, depth, value, Infinity]
else if value <= lower
item = [turn, depth, -Infinity, value]
else
item = [turn, depth, value, value]
cache.put key, item
stats: ->
{n, hit, miss, evict} = cache.stats()
console.log 'cache entry:', n
console.log 'cache hit:', hit
console.log 'cache miss:', miss
console.log 'cache evict:', evict