int ltbl_get_to (lookup_table *table, int key) { ltbl_check (table); assert (table->size > 0); int *x = htbl_find (&(table->to), key); if (x != NULL) { return *x; } return 0; }
int test_hash_table(void) { htbl t = htbl_init(NUM_SKEYS); // 64 ht_assert(!htbl_find(&t, "hello")); ht_assert(htbl_insert(&t, "hello")); ht_assert(htbl_find(&t, "hello")); ht_assert(!htbl_find(&t, "goodbye")); ht_assert(htbl_insert(&t, "goodbye")); ht_assert(htbl_find(&t, "goodbye")); ht_assert(htbl_delete(&t, "hello")); ht_assert(!htbl_find(&t, "hello")); ht_assert(htbl_find(&t, "goodbye")); for (uint32_t i = 0; i < NUM_SKEYS; ++i) { if (sprintf(skeys[i], "%" PRIu32, i) < 0) { goto error; } ht_assert(htbl_insert(&t, skeys[i])); } for (uint32_t i = 0; i < NUM_SKEYS; ++i) { //fprintf(stderr, "%" PRIu32 "\n", i); if (!htbl_find(&t, skeys[i])) { fprintf(stderr, "Error: %s %" PRIu32 "\n", skeys[i], i); goto error; } ht_assert(htbl_delete(&t, skeys[i])); ht_assert(!htbl_find(&t, skeys[i])); } htbl_destroy(t); return(0); error: htbl_destroy(t); return(-1); }
/* consider adding bonus arg that specifies which vertex */ void stats(char *cmd)/* if i, do both */ { int done_nothing = 1; /* has done nothing so far */ char *subcmd; char *vertex; int i; int j; if(strcmp("stats", cmd) < 0){ cmd += 6; } else { cmd += 2; } if((subcmd = strstr(cmd, "-n")) != NULL){/* neighbors command */ vertex = extract_vertex(subcmd, &done_nothing); if(vertex == NULL){ show_neighbors(adj_mat, dj_arr, size); } else if((i = htbl_find(t, vertex)) == -1){ printf(" -error [-n \"vertex\"]: no such vertex\n"); printf(" -try typing \"s -v \" for a list of vertices\n"); } else { printf("neighbors of %s:\n", dj_arr[i]->name); for(j = 0; j < size; j++){ if(adj_mat[(i * size) + j] != NULL){ printf(" %s\n", dj_arr[j]->name); } } } free(vertex); } if((subcmd = strstr(cmd, "-c")) != NULL){/* connections command */ vertex = extract_vertex(subcmd, &done_nothing); if(vertex == NULL){ show_connected(adj_mat, dj_arr, size); } else if((i = htbl_find(t, vertex)) == -1){ printf(" -error [-c \"vertex\"]: no such vertex\n"); printf(" -try typing \"s -v\" for a list of vertices\n"); } else { show_one_connection(adj_mat, dj_arr, size, i); } } if(strstr(cmd, "-f") != NULL){/* current file command */ done_nothing = 0; printf("********************************\n"); if(curr_file != NULL){ printf("working in file: %s\n", curr_file); } else { printf("no current working file\n"); } printf("********************************\n"); } if(strstr(cmd, "-v") != NULL){/* vertices command */ done_nothing = 0; printf("current vertices:\n"); for(i = 0; i < size; i++) printf(" %s\n", dj_arr[i]->name); } if(done_nothing) printf(" -error: unrecognized command %s\n", cmd); return; }