forked from sysprog21/simrupt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zobrist.c
66 lines (55 loc) · 1.62 KB
/
zobrist.c
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
#include <linux/slab.h>
#include "wyhash.h"
#include "zobrist.h"
u64 zobrist_table[N_GRIDS][2];
#define HASH(key) ((key) % HASH_TABLE_SIZE)
static struct hlist_head *hash_table;
void zobrist_init(void)
{
int i;
for (i = 0; i < N_GRIDS; i++) {
zobrist_table[i][0] = wyhash64();
zobrist_table[i][1] = wyhash64();
}
hash_table =
kmalloc(sizeof(struct hlist_head) * HASH_TABLE_SIZE, GFP_KERNEL);
if (!hash_table) {
pr_info("simrupt: Failed to allocate space for hash_table\n");
return;
}
for (i = 0; i < HASH_TABLE_SIZE; i++)
INIT_HLIST_HEAD(&hash_table[i]);
}
zobrist_entry_t *zobrist_get(u64 key)
{
unsigned long long hash_key = HASH(key);
if (hlist_empty(&hash_table[hash_key]))
return NULL;
zobrist_entry_t *entry = NULL;
hlist_for_each_entry (entry, &hash_table[hash_key], ht_list) {
if (entry->key == key)
return entry;
}
return NULL;
}
void zobrist_put(u64 key, int score, int move)
{
unsigned long long hash_key = HASH(key);
zobrist_entry_t *new_entry = kmalloc(sizeof(zobrist_entry_t), GFP_KERNEL);
new_entry->key = key;
new_entry->move = move;
new_entry->score = score;
hlist_add_head(&new_entry->ht_list, &hash_table[hash_key]);
}
void zobrist_clear(void)
{
for (int i = 0; i < HASH_TABLE_SIZE; i++) {
while (!hlist_empty(&hash_table[i])) {
zobrist_entry_t *entry =
hlist_entry(hash_table[i].first, zobrist_entry_t, ht_list);
hlist_del(&entry->ht_list);
kfree(entry);
}
INIT_HLIST_HEAD(&hash_table[i]);
}
}