boolean hashtable_remove(hashtable_t* hashtable, void* key) { uint hash = hashtable->hash(key, hashtable->key_size); uint start = hash % hashtable->capacity; uint bucketId = start; byte* target = HASHKEY(hashtable->keys, bucketId, hashtable->key_size); while (*target) { if (hashtable->match(target + 1, key, hashtable->key_size)) { *target = 0; hashtable->items[bucketId] = 0; hashtable->count--; return true; } bucketId = (bucketId + 1) % hashtable->capacity; if (bucketId == start) break; target = HASHKEY(hashtable->keys, bucketId, hashtable->key_size); } return false; }
boolean hashtable_resize(hashtable_t* hashtable, int capacity) { if (hashtable->storage == 0) return false; size_t size = capacity*(sizeof(void*) + hashtable->key_size + 1); arena_t* storage = arena_create(size); void** itemStore = arena_alloc(&storage, capacity*sizeof(void*)); byte* keyStore = arena_alloc(&storage, capacity*(hashtable->key_size + 1)); for (int b = 0; b < hashtable->capacity; b++) { byte* key = HASHKEY(hashtable->keys, b, hashtable->key_size); if (*key) { uint hash = hashtable->hash(key + 1, hashtable->key_size); if (!_inner_hashtable_add(itemStore, keyStore, capacity, hash, hashtable->key_size, key + 1, hashtable->items[b])) { arena_destroy(storage); return false; } } } arena_destroy(hashtable->storage); hashtable->storage = storage; hashtable->keys = keyStore; hashtable->items = itemStore; hashtable->capacity = capacity; return true; }
void getTableBounds(Position *pos, int *alpha, int *beta, int depth) { tableEntry *t; int i; i = HASHKEY(pos->hash); t = &transpositionTable[i]; if (t->hash == pos->hash) { if (t->depth >= depth) { switch(t->nodeType) { case EXACT: *alpha = *beta = t->value; break; case LOWER: if (*alpha < t->value) { *alpha = t->value; } break; case UPPER: if (*beta > t->value) { *beta = t->value; } break; } } } }
int getBookMove(Position *pos) { tableEntry *t; int i; i = HASHKEY(pos->hash); t = &transpositionTable[i]; if (t->hash == pos->hash && t->bookMove) { //the opening book doesn't contain information like en-passant //and castling rights, so we must provide it int move; int p, sq0, sq1, cap, prom; int sd; sd = 6 * (pos->ply % 2); move = t->bookMove; sq0 = FROM(move); sq1 = TO(move); p = pos->squares[sq0]; cap = pos->squares[sq1]; if ((p == WP || p == BP) && cap == EMPTY && sq1%8 != sq0%8) { //en passant cap = BP - sd; } prom = PROM(move); return MOV(sq0, sq1, cap, prom, pos->castle, pos->enpas); } return 0; }
int addToTable(Position *pos, int score, int depth, int nodeType, int bestMove) { tableEntry *t; int i; i = HASHKEY(pos->hash); t = &transpositionTable[i]; if (t->hash == pos->hash) { //previously seen position if (!(bestMove && ! t->move) && t->depth >= depth) return 0; } else if (HASHKEY(t->hash) == i) { //type-2 error if (0) return 0; //todo -- add replacement policy } t->hash = pos->hash; t->depth = depth; t->value = score; t->nodeType = nodeType; t->move = bestMove; t->utility = 0; return 1; }
void* hashtable_get(hashtable_t* hashtable, void* key) { uint hash = hashtable->hash(key, hashtable->key_size); uint start = hash % hashtable->capacity; uint bucketId = start; byte* target = HASHKEY(hashtable->keys, bucketId, hashtable->key_size); while (*target) { if (hashtable->match(target + 1, key, hashtable->key_size)) return hashtable->items[bucketId]; bucketId = (bucketId + 1) % hashtable->capacity; if (bucketId == start) break; target = HASHKEY(hashtable->keys, bucketId, hashtable->key_size); } return 0; }
boolean hashtable_contains(hashtable_t* hashtable, void* key) { uint hash = hashtable->hash(key, hashtable->key_size); uint start = hash % hashtable->capacity; uint bucketId = start; byte* target = HASHKEY(hashtable->keys, bucketId, hashtable->key_size); while (*target) { if (hashtable->match(target + 1, key, hashtable->key_size)) return true; bucketId = (bucketId + 1) % hashtable->capacity; if (bucketId == start) break; target = HASHKEY(hashtable->keys, bucketId, hashtable->key_size); } return false; }
/* * return the disk entry for given uid. return NULL if not found. */ static struct disk * hash_find(uid_t uid) { struct disk *curdisk; for (curdisk = usglist[HASHKEY(uid)]; curdisk != NULL; curdisk = curdisk->next) { if (curdisk->dsk_uid == uid) { return (curdisk); } } return (NULL); }
internal boolean _inner_hashtable_add(void** itemStore, byte* keyStore, int capacity, uint hash, int keySize, void* key, void* item) { uint start = hash % capacity; uint bucketId = start; byte* targetKey = HASHKEY(keyStore, bucketId, keySize); while (*targetKey) { bucketId = (bucketId + 1) % capacity; if (bucketId == start) return false; targetKey = HASHKEY(keyStore, bucketId, keySize); } *targetKey = 1; itemStore[bucketId] = item; memcpy(targetKey + 1, key, keySize); return true; }
int getTableMove(Position *pos) { tableEntry *t; int i; i = HASHKEY(pos->hash); t = &transpositionTable[i]; if (t->hash == pos->hash && t->nodeType != BOOK) { ++t->utility; return t->move; } else { --t->utility; return 0; } }
int add_ptr_hash(ptr_hash *hash, void *value) { int key = HASHKEY(hash, value); ptr_hash_node *node; for(node = hash->chains[key]; node; node = node->next) { if ( node->value == value ) return FALSE; /* already in hash */ } node = PL_malloc(sizeof(*node)); node->value = value; node->next = hash->chains[key]; hash->chains[key] = node; return TRUE; }
/* * create a new entry and insert. */ static struct disk * hash_insert(uid_t uid) { struct disk *curdisk; int key = HASHKEY(uid); if ((curdisk = malloc(sizeof (struct disk))) == NULL) { (void) fprintf(stderr, "acctdusg: cannot allocate memory " "for hash table entry\n"); exit(1); } curdisk->dsk_uid = uid; curdisk->dsk_du = 0; curdisk->validuser = 0; /* initially invalid */ curdisk->next = usglist[key]; usglist[key] = curdisk; return (curdisk); }