diff --git a/hash.c b/hash.c index 608a552..681b925 100644 --- a/hash.c +++ b/hash.c @@ -37,6 +37,11 @@ #include #include +#if HASH_DEBUG +#include +#include +#endif + #include "gap.h" typedef void* hash_item; @@ -44,6 +49,16 @@ typedef void* hash_item; static void* const FREE = NULL; + +/* Define DEBUG adds two functions: + * hash_dump() that prints the hash to a file as text + * hash_item_print() takes a pointer to a function that prints a hash item + * See test suite in hashtest/ for an example + * */ +/* +#define HASH_DEBUG 1 +*/ + struct hash { int hash_size; /* Max number of items in the hash */ int item_cnt; /* Number of items in the hash */ @@ -51,6 +66,9 @@ struct hash { hash_make_key_fn hash_make_key; hash_cmp_item_fn cmp_item; +#ifdef HASH_DEBUG + void (*hash_item_print)(FILE* out, hash_item item); +#endif }; typedef struct hash hash; @@ -188,17 +206,16 @@ int hash_remove(hash* h, hash_item item) return 0; } -#if HASH_TESTING -#include -#include -#define STR_LENGTH 16 -struct hash_item { - int wanted_index; - char str[STR_LENGTH]; -}; +/******************************************************************************/ +#if HASH_DEBUG +void hash_item_print(hash* h, void fn(FILE*, hash_item)) +{ + h->hash_item_print = fn; +} + + void hash_dump(hash* h, char* filename) { - char str[STR_LENGTH]; FILE* out = fopen(filename, "w"); if (!out) { @@ -209,14 +226,13 @@ void hash_dump(hash* h, char* filename) fprintf(out, "\n", h->item_cnt); for (int i = 0; i < h->hash_size; i++) { hash_item item = gap_get(h->data, i); - int idx = 0; - memset(str, 0, STR_LENGTH); - if (item) { - idx = hash_make_key(h, item); - memcpy(str, ((struct hash_item*)item)->str, STR_LENGTH); - } - fprintf(out, "\t%d:%d:%s\n", i, idx, str); + fprintf(out, "\t%d:", i); + if (item) + h->hash_item_print(out, item); + else + fprintf(out, ""); + fprintf(out, "\n"); } fprintf(out, "\n"); fclose(out); diff --git a/hashtest/Makefile b/hashtest/Makefile index 9cb67c6..168319c 100644 --- a/hashtest/Makefile +++ b/hashtest/Makefile @@ -1,5 +1,5 @@ -CFLAGS=-DHASH_TESTING -O2 -Wall +CFLAGS=-DHASH_DEBUG -O2 -Wall OBJ=../hash.o ../gap.o htest.o htest: $(OBJ) diff --git a/hashtest/delete.tst.ref b/hashtest/delete.tst.ref index ca930bb..8513842 100644 --- a/hashtest/delete.tst.ref +++ b/hashtest/delete.tst.ref @@ -1,34 +1,34 @@ - 0:0: - 1:0: - 2:0: - 3:0: - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: + 0: + 1: + 2: + 3: + 4: + 5: + 6: + 7: + 8: + 9: 10:10:aa 11:10:ab 12:10:ac - 13:0: - 14:0: - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: + 13: + 14: + 15: + 16: + 17: + 18: + 19: 20:20:ba - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: - 30:0: - 31:0: + 21: + 22: + 23: + 24: + 25: + 26: + 27: + 28: + 29: + 30: + 31: diff --git a/hashtest/delete_at_end.tst.ref b/hashtest/delete_at_end.tst.ref index c9ba1e5..0d57c3b 100644 --- a/hashtest/delete_at_end.tst.ref +++ b/hashtest/delete_at_end.tst.ref @@ -1,34 +1,34 @@ - 0:0: - 1:0: - 2:0: - 3:0: - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: + 0: + 1: + 2: + 3: + 4: + 5: + 6: + 7: + 8: + 9: 10:10:aa 11:10:ab 12:12:ac 13:13:ad - 14:0: - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: - 30:0: - 31:0: + 14: + 15: + 16: + 17: + 18: + 19: + 20: + 21: + 22: + 23: + 24: + 25: + 26: + 27: + 28: + 29: + 30: + 31: diff --git a/hashtest/delete_below_floor.tst.ref b/hashtest/delete_below_floor.tst.ref index d256c1f..e8f70eb 100644 --- a/hashtest/delete_below_floor.tst.ref +++ b/hashtest/delete_below_floor.tst.ref @@ -1,34 +1,34 @@ 0:30:ad - 1:0: + 1: 2:2:ba 3:2:bb - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: - 10:0: - 11:0: - 12:0: - 13:0: - 14:0: - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: + 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:30:aa 31:30:ac diff --git a/hashtest/delete_discont.tst.ref b/hashtest/delete_discont.tst.ref index 18b76f8..76e3be0 100644 --- a/hashtest/delete_discont.tst.ref +++ b/hashtest/delete_discont.tst.ref @@ -1,34 +1,34 @@ - 0:0: - 1:0: - 2:0: - 3:0: - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: + 0: + 1: + 2: + 3: + 4: + 5: + 6: + 7: + 8: + 9: 10:10:aa 11:10:bc 12:12:ac - 13:0: + 13: 14:14:ad - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: - 30:0: - 31:0: + 15: + 16: + 17: + 18: + 19: + 20: + 21: + 22: + 23: + 24: + 25: + 26: + 27: + 28: + 29: + 30: + 31: diff --git a/hashtest/delete_empty.tst.ref b/hashtest/delete_empty.tst.ref index c7539cf..1507083 100644 --- a/hashtest/delete_empty.tst.ref +++ b/hashtest/delete_empty.tst.ref @@ -1,34 +1,34 @@ - 0:0: - 1:0: - 2:0: - 3:0: - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: - 10:0: - 11:0: - 12:0: - 13:0: - 14:0: - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: - 30:0: - 31:0: + 0: + 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: diff --git a/hashtest/delete_full.tst.ref b/hashtest/delete_full.tst.ref index 3cc373a..78d58e8 100644 --- a/hashtest/delete_full.tst.ref +++ b/hashtest/delete_full.tst.ref @@ -20,7 +20,7 @@ 18:18:bi 19:19:bj 20:20:ca - 21:0: + 21: 22:22:cd 23:23:ce 24:24:cf diff --git a/hashtest/delete_middle.tst.ref b/hashtest/delete_middle.tst.ref index 1d27ad8..13994d2 100644 --- a/hashtest/delete_middle.tst.ref +++ b/hashtest/delete_middle.tst.ref @@ -1,34 +1,34 @@ - 0:0: - 1:0: - 2:0: - 3:0: - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: + 0: + 1: + 2: + 3: + 4: + 5: + 6: + 7: + 8: + 9: 10:10:ab - 11:0: + 11: 12:12:ac 13:13:ad 14:14:ae - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: - 30:0: - 31:0: + 15: + 16: + 17: + 18: + 19: + 20: + 21: + 22: + 23: + 24: + 25: + 26: + 27: + 28: + 29: + 30: + 31: diff --git a/hashtest/delete_wrap.tst.ref b/hashtest/delete_wrap.tst.ref index aabc8bd..9bd93d6 100644 --- a/hashtest/delete_wrap.tst.ref +++ b/hashtest/delete_wrap.tst.ref @@ -1,34 +1,34 @@ 0:30:ba 1:30:bb - 2:0: - 3:0: - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: - 10:0: - 11:0: - 12:0: - 13:0: - 14:0: - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: + 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:30:aa 31:30:ab diff --git a/hashtest/delete_wrap_at_end.tst.ref b/hashtest/delete_wrap_at_end.tst.ref index aa6683a..8abdc5b 100644 --- a/hashtest/delete_wrap_at_end.tst.ref +++ b/hashtest/delete_wrap_at_end.tst.ref @@ -1,34 +1,34 @@ 0:30:ac 1:1:ad - 2:0: + 2: 3:3:ae - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: - 10:0: - 11:0: - 12:0: - 13:0: - 14:0: - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: + 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:30:aa 31:30:ab diff --git a/hashtest/delete_wrap_below_floor.tst.ref b/hashtest/delete_wrap_below_floor.tst.ref index c47ff54..6dc5b19 100644 --- a/hashtest/delete_wrap_below_floor.tst.ref +++ b/hashtest/delete_wrap_below_floor.tst.ref @@ -1,34 +1,34 @@ 0:30:ad - 1:0: - 2:0: - 3:0: - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: - 10:0: - 11:0: - 12:0: - 13:0: - 14:0: - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: + 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:30:aa 31:30:ac diff --git a/hashtest/delete_wrap_discont.tst.ref b/hashtest/delete_wrap_discont.tst.ref index d734495..ba4f284 100644 --- a/hashtest/delete_wrap_discont.tst.ref +++ b/hashtest/delete_wrap_discont.tst.ref @@ -1,34 +1,34 @@ 0:31:ad - 1:0: + 1: 2:2:ba 3:3:bb - 4:0: - 5:0: - 6:0: - 7:0: - 8:0: - 9:0: - 10:0: - 11:0: - 12:0: - 13:0: - 14:0: - 15:0: - 16:0: - 17:0: - 18:0: - 19:0: - 20:0: - 21:0: - 22:0: - 23:0: - 24:0: - 25:0: - 26:0: - 27:0: - 28:0: - 29:0: + 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:30:aa 31:30:ac diff --git a/hashtest/htest b/hashtest/htest index 90a62e3..119f688 100755 Binary files a/hashtest/htest and b/hashtest/htest differ diff --git a/hashtest/htest.c b/hashtest/htest.c index dacb27e..55c0cf9 100644 --- a/hashtest/htest.c +++ b/hashtest/htest.c @@ -55,6 +55,11 @@ static void htest_next_key(FILE* f, char* action, int* key, char str[STR_LENGTH] if (res == EOF) exit(0); } +static void item_print(FILE* out, hash_item item) +{ + fprintf(out, "%d:%s", item->wanted_index, item->str); +} + int main(int argc, char* argv[]) { hash* h = hash_init(HASH_SIZE, &hash_make_key, &cmp_item); @@ -63,6 +68,8 @@ int main(int argc, char* argv[]) int line = 0; FILE* f; + hash_item_print(h, item_print); + if (argc != 3) { fprintf(stderr, "Usage: htest