void ltbl_init (lookup_table *table) { htbl_init (&(table->to)); table->rev = NULL; table->size = 0; table->currId = 1; table->hash = poly_h (table->rev, table->size); }
/* returns 1 if read was unsuccesful, 2 if graph is empty, 0 otherwise */ int read(char *cmd) { char *past_file = curr_file; if(strcmp("read", cmd) < 0){ cmd = cmd + 5; } else { cmd = cmd + 2; } curr_file = strdup(cmd); if(fopen(curr_file, "r") == NULL){/* if file doesn't exit */ free(curr_file); if(past_file != NULL){ curr_file = strdup(past_file); } else { curr_file = NULL; } return 1; } /* parsing vertices */ if(fgetc(fopen(curr_file, "r")) != '#'){/* if file isn't well formed */ free(curr_file); /* NOTE: this isn't a very good test */ if(past_file != NULL){ curr_file = strdup(past_file); } else { curr_file = NULL; } return 2; } /* builds the machinery to perform dj calculate here */ f = fopen(curr_file, "r"); fscanf(f, "#vertices\n"); if(t != NULL) htbl_free(t); t = htbl_init(79); sll *dj_sll = NULL; dj_sll = parse_vertices(t, f); if(dj_sll == NULL){ printf(" -this is the empty graph\n"); printf(" -there is no shortest distance to calculate\n"); return 0; } size = dj_sll->i; dj_arr = sll_to_djtab_arr(dj_sll, size); /* parsing edges */ fscanf(f, "edges\n"); adj_mat = (edge_cost *)malloc(sizeof(edge_cost) * size * size); parse_edges(adj_mat, t, size, f); return 0; }
int rms_meta_peer_init(rms_meta_peer **peer, char* hostname, unsigned long id) { (*peer) = (rms_meta_peer *)malloc(sizeof(rms_meta_peer)); (*peer)->id = id; alloc_strcpy(&(*peer)->hostname, hostname); (*peer)->sequence_sent = 0; (*peer)->sequence_rcvd = 0; (*peer)->sequence_sync = 0; (*peer)->lost = 0; (*peer)->last_hbeat.tv_sec = 0; (*peer)->last_hbeat.tv_usec = 0; htbl_init(&(*peer)->ht_unseq_msgs, 10, 0); 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); }