board_t *board_hash_table_search(board_hash_table_t *table, board_t *board) { uint64_t hash, hash2; uint32_t index; board_list_t *list; if(table == NULL || board == NULL) return NULL; hash = board_hash(board, &hash2); index = hash & (table->size - 1); for(list = table->data[index]; list != NULL; list = list->next) { if(list->hash == hash && list->hash2 == hash2 && board_compare(list->item, board)) { return list->item; } } return NULL; }
board_t *board_hash_table_insert(board_hash_table_t *table, board_t *board) { board_t *board_new; board_list_t *list = NULL, *list_new; uint32_t index; uint64_t hash, hash2; if(table == NULL || board == NULL) return NULL; if(table->used >= ((table->size * 3) / 4)) { board_hash_table_expand(table, table->size * 2); } hash = board_hash(board, &hash2); index = hash & (table->size - 1); list = table->data[index]; if(list != NULL) { for(;;) { if(list->hash == hash && list->hash2 == hash2 && board_compare(list->item, board)) { return list->item; } if(list->next == NULL) break; list = list->next; } } board_new = board_clone(board); if(board_new == NULL) return NULL; list_new = malloc(sizeof(*list_new)); if(list_new == NULL) { board_free(board_new); return NULL; } list_new->item = board_new; list_new->hash = hash; list_new->hash2 = hash2; list_new->next = NULL; if(list != NULL) list->next = list_new; else table->data[index] = list_new; table->used++; return NULL; }
void test_board_compare(void) { Board *a, *b; a = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL); b = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), 0); a = board_delete(a); b = board_delete(b); a = board_new(0xFFFFFFFFFFFFFFFFULL, 0x0000000000000000ULL); b = board_new(0x0000000000000000ULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), +1); a = board_delete(a); b = board_delete(b); a = board_new(0x0000000000000000ULL, 0x0000000000000000ULL); b = board_new(0x0000000000000001ULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), -1); a = board_delete(a); b = board_delete(b); a = board_new(0x0000000000000007ULL, 0x0000000000000000ULL); b = board_new(0x0000000000000007ULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), 0); a = board_delete(a); b = board_delete(b); a = board_new(0x0000000000000007ULL, 0x0100000000000000ULL); b = board_new(0x0000000000000007ULL, 0x0000000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), +1); a = board_delete(a); b = board_delete(b); a = board_new(0x0000000000000007ULL, 0x0000000000000000ULL); b = board_new(0x0000000000000007ULL, 0x0100000000000000ULL); CU_ASSERT_EQUAL(board_compare(a, b), -1); a = board_delete(a); b = board_delete(b); }