Convert-Binary-C
view release on metacpan or search on metacpan
ucpp/hash.c view on Meta::CPAN
}
/*
* This function finds the entry corresponding to *data in the
* hashtable ht (using the comparison function given as argument
* to newHT)
*/
void *getHT(struct HT *ht, void *data)
{
int h;
struct hash_item *t;
h = ((*(ht->hash))(data));
#ifndef FAST_HASH
h %= ht->nb_lists;
#endif
if ((t = get_entry(ht->lists[h], data, ht->cmpdata)) == 0)
return 0;
return (t->data);
}
/*
* This function finds and delete the entry corresponding to *data
* in the hashtable ht (using the comparison function given as
* argument to newHT).
*/
int delHT(struct HT *ht, void *data)
{
int h;
h = ((*(ht->hash))(data));
#ifndef FAST_HASH
h %= ht->nb_lists;
#endif
ht->lists[h] = del_entry(ht->lists[h], data, ht->cmpdata, ht->deldata);
return 1;
}
/*
* This function completely eradicates from memory a given hash table,
* releasing all objects
*/
void killHT(struct HT *ht)
{
int i;
struct hash_item *t, *n;
void (*dd)(void *) = ht->deldata;
for (i = 0; i < ht->nb_lists; i ++) for (t = ht->lists[i]; t;) {
n = t->next;
if (dd) (*dd)(t->data);
freemem(t);
t = n;
}
freemem(ht->lists);
freemem(ht);
}
/*
* This function stores a backup of the hash table, for context stacking.
*/
void saveHT(struct HT *ht, void **buffer)
{
struct hash_item **b = (struct hash_item **)buffer;
mmv(b, ht->lists, ht->nb_lists * sizeof(struct hash_item *));
}
/*
* This function restores the saved state of the hash table.
* Do NOT use if some of the entries that were present before the backup
* have been removed (even temporarily).
*/
void restoreHT(struct HT *ht, void **buffer)
{
struct hash_item **b = (struct hash_item **)buffer;
int i;
for (i = 0; i < ht->nb_lists; i ++) {
struct hash_item *t = ht->lists[i], *n;
while (t != b[i]) {
n = t->next;
(*(ht->deldata))(t->data);
freemem(t);
t = n;
}
ht->lists[i] = b[i];
}
}
/*
* This function is evil. It inserts a new item in a saved hash table,
* tweaking the save buffer and the hash table in order to keep things
* stable. There are no checks.
*/
void tweakHT(struct HT *ht, void **buffer, void *data)
{
int h;
struct hash_item *d, *e;
h = ((*(ht->hash))(data));
#ifndef FAST_HASH
h %= ht->nb_lists;
#endif
for (d = ht->lists[h]; d != buffer[h]; d = d->next);
d = add_entry(buffer[h], data);
if (buffer[h] == ht->lists[h]) {
buffer[h] = ht->lists[h] = d;
return;
}
for (e = ht->lists[h]; e->next != buffer[h]; e = e->next);
e->next = d;
buffer[h] = d;
}
/*
* This function scans the whole table and calls the given function on
* each entry.
*/
void scanHT(struct HT *ht, void (*action)(void *))
{
int i;
for (i = 0; i < ht->nb_lists; i ++) {
struct hash_item *t = ht->lists[i];
while (t) {
(*action)(t->data);
t = t->next;
}
( run in 0.513 second using v1.01-cache-2.11-cpan-39bf76dae61 )