2001-02-21 19:35:06 +00:00
|
|
|
/*
|
|
|
|
hash.h
|
|
|
|
|
|
|
|
hash tables
|
|
|
|
|
2001-06-19 02:01:18 +00:00
|
|
|
Copyright (C) 2000 Bill Currie <bill@taniwha.org>
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
2001-09-28 06:26:31 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2001-06-11 19:37:25 +00:00
|
|
|
#include <stdlib.h> // should be sys/types.h, but bc is stupid
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/hash.h"
|
2001-10-02 23:53:07 +00:00
|
|
|
#include "QF/sys.h"
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2001-06-11 19:17:40 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
|
|
|
struct hashlink_s {
|
|
|
|
struct hashlink_s *next;
|
|
|
|
struct hashlink_s **prev;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hashtab_s {
|
|
|
|
size_t tab_size;
|
2001-10-02 23:17:25 +00:00
|
|
|
size_t num_ele;
|
2001-06-11 19:17:40 +00:00
|
|
|
void *user_data;
|
2001-08-16 02:51:53 +00:00
|
|
|
int (*compare)(void*,void*,void*);
|
|
|
|
unsigned long (*get_hash)(void*,void*);
|
2001-06-11 19:17:40 +00:00
|
|
|
const char *(*get_key)(void*,void*);
|
|
|
|
void (*free_ele)(void*,void*);
|
|
|
|
struct hashlink_s *tab[1]; // variable size
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-08-16 02:51:53 +00:00
|
|
|
unsigned long
|
|
|
|
Hash_String (const char *str)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2001-03-05 16:19:10 +00:00
|
|
|
//FIXME not 64 bit clean
|
|
|
|
#if 0
|
2001-02-21 19:35:06 +00:00
|
|
|
unsigned long h = 0;
|
|
|
|
while (*str) {
|
|
|
|
h = (h << 4) + (unsigned char)*str++;
|
|
|
|
if (h&0xf0000000)
|
|
|
|
h = (h ^ (h >> 24)) & 0xfffffff;
|
|
|
|
}
|
|
|
|
return h;
|
2001-03-05 16:19:10 +00:00
|
|
|
#else
|
|
|
|
// dx_hack_hash
|
|
|
|
// shamelessly stolen from Daniel Phillips <phillips@innominate.de>
|
|
|
|
// from his post to lkml
|
|
|
|
unsigned long hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
|
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
unsigned long hash = hash1 + (hash0 ^ (*str++ * 71523));
|
|
|
|
if (hash < 0) hash -= 0x7fffffff;
|
|
|
|
hash1 = hash0;
|
|
|
|
hash0 = hash;
|
|
|
|
}
|
|
|
|
return hash0;
|
|
|
|
#endif
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2001-08-16 02:51:53 +00:00
|
|
|
static unsigned long
|
|
|
|
get_hash (void *ele, void *data)
|
|
|
|
{
|
|
|
|
return (unsigned long)ele;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
compare (void *a, void *b, void *data)
|
|
|
|
{
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
hashtab_t *
|
2001-08-16 02:51:53 +00:00
|
|
|
Hash_NewTable (int tsize, const char *(*gk)(void*,void*),
|
|
|
|
void (*f)(void*,void*), void *ud)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2001-06-06 16:10:28 +00:00
|
|
|
hashtab_t *tab = calloc (1, field_offset (hashtab_t, tab[tsize]));
|
2001-02-21 19:35:06 +00:00
|
|
|
if (!tab)
|
|
|
|
return 0;
|
|
|
|
tab->tab_size = tsize;
|
2001-03-05 05:11:26 +00:00
|
|
|
tab->user_data = ud;
|
2001-02-21 19:35:06 +00:00
|
|
|
tab->get_key = gk;
|
|
|
|
tab->free_ele = f;
|
2001-08-16 02:51:53 +00:00
|
|
|
|
|
|
|
tab->get_hash = get_hash;
|
|
|
|
tab->compare = compare;
|
2001-02-21 19:35:06 +00:00
|
|
|
return tab;
|
|
|
|
}
|
|
|
|
|
2001-08-16 02:51:53 +00:00
|
|
|
void
|
|
|
|
Hash_SetHashCompare (hashtab_t *tab, unsigned long (*gh)(void*,void*),
|
|
|
|
int (*cmp)(void*,void*,void*))
|
|
|
|
{
|
|
|
|
tab->get_hash = gh;
|
|
|
|
tab->compare = cmp;
|
|
|
|
}
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
void
|
|
|
|
Hash_DelTable (hashtab_t *tab)
|
2001-03-05 05:11:26 +00:00
|
|
|
{
|
|
|
|
Hash_FlushTable (tab);
|
|
|
|
free (tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Hash_FlushTable (hashtab_t *tab)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < tab->tab_size; i++) {
|
|
|
|
while (tab->tab[i]) {
|
2001-06-11 19:17:40 +00:00
|
|
|
struct hashlink_s *t = tab->tab[i]->next;
|
2001-06-19 02:01:18 +00:00
|
|
|
void *data = tab->tab[i]->data;
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
free (tab->tab[i]);
|
|
|
|
tab->tab[i] = t;
|
2001-06-19 02:01:18 +00:00
|
|
|
if (tab->free_ele)
|
|
|
|
tab->free_ele (data, tab->user_data);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-02 23:17:25 +00:00
|
|
|
tab->num_ele = 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Hash_Add (hashtab_t *tab, void *ele)
|
|
|
|
{
|
2001-08-16 02:51:53 +00:00
|
|
|
unsigned long h = Hash_String (tab->get_key(ele, tab->user_data));
|
|
|
|
size_t ind = h % tab->tab_size;
|
|
|
|
struct hashlink_s *lnk = malloc (sizeof (struct hashlink_s));
|
|
|
|
|
|
|
|
if (!lnk)
|
|
|
|
return -1;
|
|
|
|
if (tab->tab[ind])
|
|
|
|
tab->tab[ind]->prev = &lnk->next;
|
|
|
|
lnk->next = tab->tab[ind];
|
|
|
|
lnk->prev = &tab->tab[ind];
|
|
|
|
lnk->data = ele;
|
|
|
|
tab->tab[ind] = lnk;
|
2001-10-02 23:17:25 +00:00
|
|
|
tab->num_ele++;
|
2001-08-16 02:51:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
Hash_AddElement (hashtab_t *tab, void *ele)
|
|
|
|
{
|
|
|
|
unsigned long h = tab->get_hash (ele, tab->user_data);
|
2001-02-21 19:35:06 +00:00
|
|
|
size_t ind = h % tab->tab_size;
|
2001-06-11 19:17:40 +00:00
|
|
|
struct hashlink_s *lnk = malloc (sizeof (struct hashlink_s));
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
if (!lnk)
|
|
|
|
return -1;
|
|
|
|
if (tab->tab[ind])
|
|
|
|
tab->tab[ind]->prev = &lnk->next;
|
|
|
|
lnk->next = tab->tab[ind];
|
|
|
|
lnk->prev = &tab->tab[ind];
|
|
|
|
lnk->data = ele;
|
|
|
|
tab->tab[ind] = lnk;
|
2001-10-02 23:17:25 +00:00
|
|
|
tab->num_ele++;
|
2001-02-21 19:35:06 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
Hash_Find (hashtab_t *tab, const char *key)
|
|
|
|
{
|
2001-08-16 02:51:53 +00:00
|
|
|
unsigned long h = Hash_String (key);
|
2001-02-21 19:35:06 +00:00
|
|
|
size_t ind = h % tab->tab_size;
|
2001-06-11 19:17:40 +00:00
|
|
|
struct hashlink_s *lnk = tab->tab[ind];
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
while (lnk) {
|
2001-03-05 05:11:26 +00:00
|
|
|
if (strequal (key, tab->get_key (lnk->data, tab->user_data)))
|
2001-02-21 19:35:06 +00:00
|
|
|
return lnk->data;
|
|
|
|
lnk = lnk->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-16 02:51:53 +00:00
|
|
|
void *
|
|
|
|
Hash_FindElement (hashtab_t *tab, void *ele)
|
|
|
|
{
|
|
|
|
unsigned long h = tab->get_hash (ele, tab->user_data);
|
|
|
|
size_t ind = h % tab->tab_size;
|
|
|
|
struct hashlink_s *lnk = tab->tab[ind];
|
|
|
|
|
|
|
|
while (lnk) {
|
|
|
|
if (tab->compare (lnk->data, ele, tab->user_data))
|
|
|
|
return lnk->data;
|
|
|
|
lnk = lnk->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-07-06 17:43:47 +00:00
|
|
|
void *
|
2001-02-21 19:35:06 +00:00
|
|
|
Hash_Del (hashtab_t *tab, const char *key)
|
|
|
|
{
|
2001-08-16 02:51:53 +00:00
|
|
|
unsigned long h = Hash_String (key);
|
2001-02-21 19:35:06 +00:00
|
|
|
size_t ind = h % tab->tab_size;
|
2001-06-11 19:17:40 +00:00
|
|
|
struct hashlink_s *lnk = tab->tab[ind];
|
2001-07-06 17:43:47 +00:00
|
|
|
void *data;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
while (lnk) {
|
2001-03-05 05:11:26 +00:00
|
|
|
if (strequal (key, tab->get_key (lnk->data, tab->user_data))) {
|
2001-07-06 17:43:47 +00:00
|
|
|
data = lnk->data;
|
2001-02-21 19:35:06 +00:00
|
|
|
if (lnk->next)
|
|
|
|
lnk->next->prev = lnk->prev;
|
|
|
|
*lnk->prev = lnk->next;
|
|
|
|
free (lnk);
|
2001-10-02 23:17:25 +00:00
|
|
|
tab->num_ele--;
|
2001-07-06 17:43:47 +00:00
|
|
|
return data;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
lnk = lnk->next;
|
|
|
|
}
|
2001-07-06 17:43:47 +00:00
|
|
|
return 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
2001-08-16 02:51:53 +00:00
|
|
|
|
|
|
|
void *
|
|
|
|
Hash_DelElement (hashtab_t *tab, void *ele)
|
|
|
|
{
|
|
|
|
unsigned long h = tab->get_hash (ele, tab->user_data);
|
|
|
|
size_t ind = h % tab->tab_size;
|
|
|
|
struct hashlink_s *lnk = tab->tab[ind];
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
while (lnk) {
|
|
|
|
if (tab->compare (lnk->data, ele, tab->user_data)) {
|
|
|
|
data = lnk->data;
|
|
|
|
if (lnk->next)
|
|
|
|
lnk->next->prev = lnk->prev;
|
|
|
|
*lnk->prev = lnk->next;
|
|
|
|
free (lnk);
|
2001-10-02 23:17:25 +00:00
|
|
|
tab->num_ele--;
|
2001-08-16 02:51:53 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
lnk = lnk->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2001-10-02 23:17:25 +00:00
|
|
|
|
|
|
|
void **
|
|
|
|
Hash_GetList (hashtab_t *tab)
|
|
|
|
{
|
|
|
|
void **list;
|
|
|
|
void **l;
|
|
|
|
size_t ind;
|
|
|
|
|
|
|
|
l = list = malloc ((tab->num_ele + 1) * sizeof (void*));
|
|
|
|
if (!list)
|
|
|
|
return 0;
|
|
|
|
for (ind = 0; ind < tab->tab_size; ind++) {
|
|
|
|
struct hashlink_s *lnk;
|
|
|
|
|
|
|
|
for (lnk = tab->tab[ind]; lnk; lnk = lnk->next) {
|
|
|
|
*l++ = lnk->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*l++ = 0;
|
|
|
|
return list;
|
|
|
|
}
|