Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Bitarray Hash Table
- *
- * 15-122 Principles of Imperative Computation */
- #include <stdlib.h>
- #include <stdbool.h>
- #include "lib/contracts.h"
- #include "lib/ba-ht.h"
- #include "lib/hset.h"
- #include "lib/xalloc.h"
- bitarray to_key(hset_elem x){
- REQUIRES(x!=NULL);
- struct board_data *b = x;
- bitarray k = b->board;
- return k;
- }
- bool ba_equal (hset_elem x, hset_elem y){
- REQUIRES (x!=NULL && y!=NULL);
- bitarray a = to_key(x);
- bitarray b = to_key(y);
- return a==b;
- }
- size_t ba_hash (hset_elem x){
- REQUIRES (x!=NULL);
- bitarray a = to_key(x);
- return (size_t) a;
- }
- void ba_free(hset_elem x){
- free(x);
- }
- /* Initializes a new hash table with the given capacity
- */
- hset ht_new (size_t capacity){
- REQUIRES (capacity > 0);
- return hset_new(capacity,&ba_equal,&ba_hash,&free);
- }
- /* ht_insert(H,e) has no return value, because it should have as
- * a precondition that no struct currently in the hashtable contains
- * the same board as DAT->board
- */
- void ht_insert (hset H, struct board_data *DAT){
- REQUIRES(H!=NULL && DAT!=NULL);
- REQUIRES(ht_lookup(H,DAT->board)==NULL);
- hset_insert(H,DAT);
- }
- /* ht_lookup(H,B) returns
- * NULL if no struct containing the board B exists in H
- * A struct containing the board B if one exists in H
- */
- struct board_data *ht_lookup(hset H, bitarray B){
- REQUIRES(H!=NULL);
- struct board_data *k = xmalloc(sizeof(struct board_data));
- k->last_move = 0;
- k->board = B;
- hset_elem x = hset_lookup(H,k);
- free(k);
- return x;
- }
- /* ht_lookup(H,B) returns
- * NULL if no struct containing the board B exists in H
- * A struct containing the board B if one exists in H
- */
- void ht_free(hset H){
- hset_free(H);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement