diff options
author | JSDurand <mmemmew@gmail.com> | 2022-01-22 02:36:54 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2022-01-22 02:36:54 +0800 |
commit | bad2b1934da66021cbc7f0d715686706bd444449 (patch) | |
tree | 78c340d5f834bbc5864a97dcb23ff4ec41e23072 /src/test | |
parent | 53865aad225ffbe5cf3c42736e5a2095092f9fff (diff) |
Implemented a hash table with any type of keys
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/check_grammar.c | 48 | ||||
-rw-r--r-- | src/test/check_ht.c | 21 |
2 files changed, 37 insertions, 32 deletions
diff --git a/src/test/check_grammar.c b/src/test/check_grammar.c index ca70fa9..2b6c8f2 100644 --- a/src/test/check_grammar.c +++ b/src/test/check_grammar.c @@ -3,6 +3,8 @@ #include <stdlib.h> #include "../grammar.h" +#define GET_KEY(T, K, N) *((T*)*(K+N)) + #define READ_INTO_CPA(N, U, I, VA, VI, CP) do { \ U = new_utf8(N, 1); \ I = get_info((str *)U, 0); \ @@ -174,11 +176,11 @@ main(UNUSED int argc, UNUSED char **argv) for (NUM i = 0; i < left_len; i++) { ht *temp = NULL; - temp = new_ht(left_len << 1); + temp = new_ht(left_len << 1, 0); *(terminal_hts+i) = *temp; free(temp); - temp = new_ht(left_len << 1); + temp = new_ht(left_len << 1, 0); *(predicate_hts+i) = *temp; free(temp); } @@ -188,15 +190,15 @@ main(UNUSED int argc, UNUSED char **argv) goto cleanup; } - NUM ht_len = 0, *keys = NULL; + NUM ht_len = 0, **keys = NULL; for (NUM i = 0; i < left_len;) { printf("Nonterminal %ld contains the following terminals in the " "FIRST set:\n", i); ht_len = ht_size(terminal_hts+i); - keys = ht_keys(terminal_hts+i++); + keys = (NUM **) ht_keys(terminal_hts+i++); for (NUM j = 0; j < ht_len;) { - printf("T %ld", *(keys+j)); + printf("T %ld", GET_KEY(NUM, keys, j)); if (++j<ht_len) printf(", "); else printf("\n"); } @@ -210,11 +212,11 @@ main(UNUSED int argc, UNUSED char **argv) for (NUM i = 0; i < left_len; i++) { ht *temp = NULL; - temp = new_ht(left_len << 1); + temp = new_ht(left_len << 1, 0); *(test_terminal_hts+i) = *temp; free(temp); - temp = new_ht(left_len << 1); + temp = new_ht(left_len << 1, 0); *(test_predicate_hts+i) = *temp; free(temp); } @@ -238,17 +240,17 @@ main(UNUSED int argc, UNUSED char **argv) printf(" has the following FIRST set:\n"); ht_len = ht_size(test_terminal_hts); - keys = ht_keys(test_terminal_hts); + keys = (NUM **) ht_keys(test_terminal_hts); for (NUM i = 0; i < ht_len;) { - printf("T %ld", *(keys+i)); + printf("T %ld", GET_KEY(NUM, keys, i)); if (++i<ht_len) printf(", "); else printf("\n"); } ht_len = ht_size(test_predicate_hts); - keys = ht_keys(test_predicate_hts); + keys = (NUM **) ht_keys(test_predicate_hts); for (NUM i = 0; i < ht_len;) { - printf("PT %ld", *(keys+i)); + printf("PT %ld", GET_KEY(NUM, keys, i)); if (++i<ht_len) printf(", "); else printf("\n"); } @@ -265,11 +267,11 @@ main(UNUSED int argc, UNUSED char **argv) for (NUM i = 0; i < left_len; i++) { ht *temp = NULL; - temp = new_ht(left_len << 1); + temp = new_ht(left_len << 1, 0); *(result_terminal_hts+i) = *temp; free(temp); - temp = new_ht(left_len << 1); + temp = new_ht(left_len << 1, 0); *(result_predicate_hts+i) = *temp; free(temp); } @@ -284,10 +286,10 @@ main(UNUSED int argc, UNUSED char **argv) printf("Nonterminal %ld contains the following terminals in the " "FOLLOW set:\n", i); ht_len = ht_size(result_terminal_hts+i); - keys = ht_keys(result_terminal_hts+i++); + keys = (NUM **) ht_keys(result_terminal_hts+i++); for (NUM j = 0; j < ht_len;) { - if (*(keys+j) == END_OF_INPUT) printf("T $"); - else printf("T %ld", *(keys+j)); + if (GET_KEY(NUM, keys, j) == END_OF_INPUT) printf("T $"); + else printf("T %ld", GET_KEY(NUM, keys, j)); if (++j<ht_len) printf(", "); else printf("\n"); } @@ -297,42 +299,42 @@ main(UNUSED int argc, UNUSED char **argv) if (epsilon_array) free(epsilon_array); if (terminal_hts) { for (NUM i = 0; i < left_len; i++) - destroy_ht(terminal_hts+i, DESTROY_NONE_NO_SELF); + destroy_ht(terminal_hts+i, DESTROY_KEY_NO_SELF); free(terminal_hts); } if (predicate_hts) { for (NUM i = 0; i < left_len; i++) - destroy_ht(predicate_hts+i, DESTROY_NONE_NO_SELF); + destroy_ht(predicate_hts+i, DESTROY_KEY_NO_SELF); free(predicate_hts); } if (test_terminal_hts) { for (NUM i = 0; i < left_len; i++) - destroy_ht(test_terminal_hts+i, DESTROY_NONE_NO_SELF); + destroy_ht(test_terminal_hts+i, DESTROY_KEY_NO_SELF); free(test_terminal_hts); } if (test_predicate_hts) { for (NUM i = 0; i < left_len; i++) - destroy_ht(test_predicate_hts+i, DESTROY_NONE_NO_SELF); + destroy_ht(test_predicate_hts+i, DESTROY_KEY_NO_SELF); free(test_predicate_hts); } if (result_terminal_hts) { for (NUM i = 0; i < left_len; i++) - destroy_ht(result_terminal_hts+i, DESTROY_NONE_NO_SELF); + destroy_ht(result_terminal_hts+i, DESTROY_KEY_NO_SELF); free(result_terminal_hts); } if (result_predicate_hts) { for (NUM i = 0; i < left_len; i++) - destroy_ht(result_predicate_hts+i, DESTROY_NONE_NO_SELF); + destroy_ht(result_predicate_hts+i, DESTROY_KEY_NO_SELF); free(result_predicate_hts); } if (test_tnt_string) destroy_list(test_tnt_string, 1); - if (tnt_ht) destroy_ht(tnt_ht, DESTROY_NONE_SELF); + if (tnt_ht) destroy_ht(tnt_ht, DESTROY_KEY_NO_SELF); destroy_grammar(g, 1); diff --git a/src/test/check_ht.c b/src/test/check_ht.c index 00cd2f1..0298087 100644 --- a/src/test/check_ht.c +++ b/src/test/check_ht.c @@ -3,19 +3,20 @@ int main(int UNUSED argc, char ** UNUSED argv) { - ht *htp = new_ht(HT_INIT_CAP); + + ht *htp = new_ht(HT_INIT_CAP, 0); NUM *temp = MYALLOC(NUM, 1), key = 1023; *temp = 12345; - if (ht_insert(htp, key, temp)) { + if (ht_insert(htp, &key, temp)) { fleprintf0("Fail to insert\n"); free(temp); destroy_ht(htp, DESTROY_EVERY_SELF); return 1; } - if ((temp = ht_find(htp, key))) { + if ((temp = ht_find(htp, &key))) { fleprintf("We found value %ld for key %ld\n", *temp, key); } else @@ -27,25 +28,27 @@ int main(int UNUSED argc, char ** UNUSED argv) for (NUM i = 0; i < size; i++) fleprintf("The %ld-th element has key %ld and value %ld\n", - i, *(ht_keys(htp)+i), *((NUM *)*(ht_values(htp)+i))); + i, + *((NUM *)*(ht_keys(htp)+i)), + *((NUM *)*(ht_values(htp)+i))); - if (ht_delete(htp, key, 1)) { + if (ht_delete(htp, &key, DELETE_VALUE)) { fleprintf("Fail to delete key %ld\n", key); - destroy_ht(htp, DESTROY_EVERY_SELF); + destroy_ht(htp, DESTROY_VALUE_SELF); return 1; } fleprintf0("After the deletion, "); - if ((temp = ht_find(htp, key))) { + if ((temp = ht_find(htp, &key))) { eprintf("We found value %ld for key %ld\n", *temp, key); - destroy_ht(htp, DESTROY_EVERY_SELF); + destroy_ht(htp, DESTROY_VALUE_SELF); return 1; } else { eprintf("We found no value for key %ld\n", key); } - destroy_ht(htp, DESTROY_EVERY_SELF); + destroy_ht(htp, DESTROY_VALUE_SELF); return 0; } |