int main(int argc, char **argv) { // test ohtbl_init OHTbl *testOHTbl1 = (OHTbl *)malloc(sizeof(OHTbl)); ohtbl_init(testOHTbl1, 16, hash_pjw, hash_sdbm, _match, NULL); printf("'ohtbl_init' is pass ? %d \n", testOHTbl1->table[9] == NULL); // test ohtbl_insert int testData1 = 1; void *testPtr1; testPtr1 = &testData1; ohtbl_insert(testOHTbl1, testPtr1); printf("'ohtbl_insert' is pass ? %d \n", testOHTbl1->size == 1); // test ohtbl_lookup int testData2 = 1; testPtr1 = &testData2; ohtbl_lookup(testOHTbl1, &testPtr1); printf("'ohtbl_lookup' is pass ? %d \n", testPtr1 == &testData1); // test ohtbl_remove testPtr1 = &testData2; ohtbl_remove(testOHTbl1, &testPtr1); printf("'ohtbl_remove' is pass ? %d \n", testPtr1 == &testData1); // test ohtbl_destory ohtbl_destory(testOHTbl1); printf("'ohtbl_destory' is pass ? %d \n", testOHTbl1->table == 0); free(testOHTbl1); return 0; }
int ohtbl_insert(OHTbl * htbl, const void *data) { void *temp; int position, i; /***************************************************************************** * Do not exceed the number of positions in the table. * *****************************************************************************/ if (htbl->size == htbl->positions) return -1; /***************************************************************************** * Do nothing if the data is already in the table. * *****************************************************************************/ temp = (void *)data; if (ohtbl_lookup(htbl, &temp) == 0) return 1; /***************************************************************************** * Use double hashing to hash the key. * *****************************************************************************/ for (i = 0; i < htbl->positions; i++) { position = (htbl->h1(data) + (i * htbl->h2(data))) % htbl->positions; if (htbl->table[position] == NULL || htbl->table[position] == htbl->vacated) { /*********************************************************************** * Insert the data into the table. * ***********************************************************************/ htbl->table[position] = (void *)data; htbl->size++; return 0; } } /***************************************************************************** * Return that the hash functions were selected incorrectly. * *****************************************************************************/ return -1; }
int ohtbl_insert(Ohtbl *htbl, const void *data) { /* * Check if the table is already full */ if(htbl->size == htbl->positions) { return -1; } /* * Check if the key is already in the table */ if(ohtbl_lookup(htbl, &((void*)data)) == 0) { return 1; } /* * Use double hashing to probe the table for an unoccupied position */ int i; int probePosition; for(i = 0; i < htbl->positions; i++) { probePosition = (htbl->h1(data) + i * htbl->h2(data)) % htbl->positions; if(htbl->table[probePosition] == NULL || htbl->table[probePosition] == htbl->vacated) { htbl->table[probePosition] = (void *)data; htbl->size++; return 0; } } /* * Return that the hash functions were selected incorrectly */ return -1; }