repetition table

This commit is contained in:
2024-09-23 21:51:22 +02:00
parent 6c5ef0586f
commit 2c3d1bc021
6 changed files with 71 additions and 48 deletions

View File

@ -1,3 +1,4 @@
#include "chess/types.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@ -22,28 +23,24 @@ uint_least8_t getRank(const uint_least8_t field) {
return field / BOARD_SIZE;
}
void initZobirstTable(struct zobristTableElement *table, size_t length) {
for(size_t i = 0; i < length; ++i) {
table[i].hash = 0; // use 0 for no value
}
struct zobristTable initZobirstTable(size_t length) {
struct zobristTableElement *table = calloc(length, sizeof *table);
return (struct zobristTable){table, length};
}
static size_t zobristTableInitialI(const struct zobristTable table, const uint_least64_t key) {
return key % table.length;
}
enum returnStatus {
CONTINUE,
RETURN,
};
static void zobristTableIter(const struct zobristTable table, const uint_least64_t key, void *result,
enum returnStatus (*foreach)(const struct zobristTableElement element, const uint_least64_t key, void *result)) {
void zobristTableIter(const struct zobristTable table, const uint_least64_t key, void *result,
void (*callback)(struct zobristTableElement *element, const uint_least64_t key, void *result)) {
const size_t initI = zobristTableInitialI(table, key);
size_t i = initI;
do {
const struct zobristTableElement element = table.arr[i];
if(foreach(element, key, result) == RETURN) return;
struct zobristTableElement *element = table.arr + i;
if(element->hash == 0 || element->hash == key) {
callback(element, key, result);
}
++i;
if(i >= table.length) i = 0;
} while(i != initI);
@ -51,29 +48,29 @@ static void zobristTableIter(const struct zobristTable table, const uint_least64
exit(1);
}
static enum returnStatus zobristTableGetForeach(const struct zobristTableElement element, const uint_least64_t key,
static void zobristTableItemPtrCallback(struct zobristTableElement *element, const uint_least64_t key,
void *resultVoid) {
struct zobristTableResult *result = resultVoid;
if(element.hash == key) {
*result = (struct zobristTableResult){true, element.value};
return RETURN;
}
if(element.hash == 0) {
result->success = false;
return RETURN;
}
return CONTINUE;
struct zobristTableElement **result = resultVoid;
if(element->hash == key) {
*result = element;
return;
} // if(hash == 0)
*result = NULL;
}
struct zobristTableResult zobristTableGet(const struct zobristTable table, const uint_least64_t key) {
struct zobristTableResult result;
zobristTableIter(table, key, &result, zobristTableGetForeach);
static struct zobristTableElement* zobristTableItemPtr(const struct zobristTable table, const uint_least64_t key) {
struct zobristTableElement *result;
zobristTableIter(table, key, &result, zobristTableItemPtrCallback);
return result;
}
static enum returnStatus zobristTableSetForeach(const struct zobristTableElement element, const uint_least64_t key,
void *resultVoid) {
void zobristTableDelete(struct zobristTable table, const uint_least64_t key) {
struct zobristTableElement *element = zobristTableItemPtr(table, key);
if(element != NULL) element->hash = 0;
}
void zobristTableSet(struct zobristTable table, const uint_least64_t key, const int_least16_t value) {
int_least16_t* zobristTableGetPtr(const struct zobristTable table, const uint_least64_t key) {
struct zobristTableElement *element = zobristTableItemPtr(table, key);
if(element == NULL) return NULL;
return &element->value;
}