Online Book Reader

Home Category

Mastering Algorithms With C - Kyle Loudon [70]

By Root 1449 0

* *

* Initialize the buckets. *

* *

*****************************************************************************/

htbl->buckets = buckets;

for (i = 0; i < htbl->buckets; i++)

list_init(&htbl->table[i], destroy);

/*****************************************************************************

* *

* Encapsulate the functions. *

* *

*****************************************************************************/

htbl->h = h;

htbl->match = match;

htbl->destroy = destroy;

/*****************************************************************************

* *

* Initialize the number of elements in the table. *

* *

*****************************************************************************/

htbl->size = 0;

return 0;

}

/*****************************************************************************

* *

* ---------------------------- chtbl_destroy ----------------------------- *

* *

*****************************************************************************/

void chtbl_destroy(CHTbl *htbl) {

int i;

/*****************************************************************************

* *

* Destroy each bucket. *

* *

*****************************************************************************/

for (i = 0; i < htbl->buckets; i++) {

list_destroy(&htbl->table[i]);

}

/*****************************************************************************

* *

* Free the storage allocated for the hash table. *

* *

*****************************************************************************/

free(htbl->table);

/*****************************************************************************

* *

* No operations are allowed now, but clear the structure as a precaution. *

* *

*****************************************************************************/

memset(htbl, 0, sizeof(CHTbl));

return;

}

/*****************************************************************************

* *

* ----------------------------- chtbl_insert ----------------------------- *

* *

*****************************************************************************/

int chtbl_insert(CHTbl *htbl, const void *data) {

void *temp;

int bucket,

retval;

/*****************************************************************************

* *

* Do nothing if the data is already in the table. *

* *

*****************************************************************************/

temp = (void *)data;

if (chtbl_lookup(htbl, &temp) == 0)

return 1;

/*****************************************************************************

* *

* Hash the key. *

* *

*****************************************************************************/

bucket = htbl->h(data) % htbl->buckets;

/*****************************************************************************

* *

* Insert the data into the bucket. *

* *

*****************************************************************************/

if ((retval = list_ins_next(&htbl->table[bucket], NULL, data)) == 0)

htbl->size++;

return retval;

}

/*****************************************************************************

* *

* ----------------------------- chtbl_remove ----------------------------- *

* *

*****************************************************************************/

int chtbl_remove(CHTbl *htbl, void **data) {

ListElmt *element,

*prev;

int bucket;

/*****************************************************************************

* *

* Hash the key. *

* *

*****************************************************************************/

bucket = htbl->h(*data) % htbl->buckets;

/*****************************************************************************

* *

* Search for the data in the bucket. *

* *

*****************************************************************************/

prev = NULL;

for (element = list_head(&htbl->table[bucket]); element != NULL; element =

list_next(element)) {

if (htbl->match(*data, list_data(element))) {

/***********************************************************************

Return Main Page Previous Page Next Page

®Online Book Reader