return lists of multiple matching elements

This commit is contained in:
Bill Currie 2002-05-30 21:56:57 +00:00
parent 8a762bdaaf
commit 9134e05aec
2 changed files with 66 additions and 0 deletions

View file

@ -96,6 +96,16 @@ int Hash_AddElement (hashtab_t *tab, void *ele);
void *Hash_Find (hashtab_t *tab, const char *key);
void *Hash_FindElement (hashtab_t *tab, void *ele);
/*
find a list of elements within a hash table:
tab: the table to search
key: the key string identifying the elements being searched for
returns a null terminated list of element pointers if at least one found,
otherwise 0.
*/
void **Hash_FindList (hashtab_t *tab, const char *key);
void **Hash_FindElementList (hashtab_t *tab, void *ele);
/*
delete an element from a hash table:
tab: the table to remove the element from

View file

@ -250,6 +250,62 @@ Hash_FindElement (hashtab_t *tab, void *ele)
return 0;
}
void **
Hash_FindList (hashtab_t *tab, const char *key)
{
unsigned long h = Hash_String (key);
size_t ind = get_index (h, tab->tab_size, tab->size_bits);
struct hashlink_s *lnk = tab->tab[ind], *start = 0;
int count = 0;
void **list;
while (lnk) {
if (strequal (key, tab->get_key (lnk->data, tab->user_data))) {
count++;
if (!start)
start = lnk;
}
lnk = lnk->next;
}
if (!count)
return 0;
list = malloc ((count + 1) * sizeof (void *));
for (count = 0, lnk = start; lnk; lnk = lnk->next) {
if (strequal (key, tab->get_key (lnk->data, tab->user_data)))
list[count++] = lnk->data;
}
list[count] = 0;
return list;
}
void **
Hash_FindElementList (hashtab_t *tab, void *ele)
{
unsigned long h = tab->get_hash (ele, tab->user_data);
size_t ind = get_index (h, tab->tab_size, tab->size_bits);
struct hashlink_s *lnk = tab->tab[ind], *start = 0;
int count = 0;
void **list;
while (lnk) {
if (tab->compare (lnk->data, ele, tab->user_data)) {
count++;
if (!start)
start = lnk;
}
lnk = lnk->next;
}
if (!count)
return 0;
list = malloc ((count + 1) * sizeof (void *));
for (count = 0, lnk = start; lnk; lnk = lnk->next) {
if (tab->compare (lnk->data, ele, tab->user_data))
list[count++] = lnk->data;
}
list[count] = 0;
return list;
}
void *
Hash_Del (hashtab_t *tab, const char *key)
{