Files
wmaker/WINGs/hashtable.c
T

501 lines
9.9 KiB
C
Raw Normal View History

1998-09-29 22:36:29 +00:00
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "WUtil.h"
#define INITIAL_CAPACITY 23
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
# define INLINE inline
#else
# define INLINE
#endif
typedef struct HashItem {
2001-01-02 14:17:26 +00:00
const void *key;
const void *data;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
struct HashItem *next; /* collided item list */
} HashItem;
typedef struct W_HashTable {
WMHashTableCallbacks callbacks;
unsigned itemCount;
unsigned size; /* table size */
HashItem **table;
} HashTable;
#define HASH(table, key) (((table)->callbacks.hash ? \
2004-10-12 21:28:27 +00:00
(*(table)->callbacks.hash)(key) : hashPtr(key)) % (table)->size)
1998-09-29 22:36:29 +00:00
#define DUPKEY(table, key) ((table)->callbacks.retainKey ? \
2004-10-12 21:28:27 +00:00
(*(table)->callbacks.retainKey)(key) : (key))
1998-09-29 22:36:29 +00:00
#define RELKEY(table, key) if ((table)->callbacks.releaseKey) \
2004-10-12 21:28:27 +00:00
(*(table)->callbacks.releaseKey)(key)
1998-09-29 22:36:29 +00:00
static INLINE unsigned
hashString(const char *key)
{
unsigned ret = 0;
unsigned ctr = 0;
while (*key) {
2004-10-12 21:28:27 +00:00
ret ^= *(char*)key++ << ctr;
ctr = (ctr + 1) % sizeof (char *);
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
return ret;
}
static INLINE unsigned
hashPtr(const void *key)
{
return ((size_t)key / sizeof(char*));
}
static void
rellocateItem(WMHashTable *table, HashItem *item)
{
unsigned h;
h = HASH(table, item->key);
item->next = table->table[h];
table->table[h] = item;
}
static void
rebuildTable(WMHashTable *table)
{
HashItem *next;
HashItem **oldArray;
int i;
int oldSize;
int newSize;
oldArray = table->table;
oldSize = table->size;
newSize = table->size*2;
table->table = wmalloc(sizeof(char*)*newSize);
memset(table->table, 0, sizeof(char*)*newSize);
table->size = newSize;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
for (i = 0; i < oldSize; i++) {
2004-10-12 21:28:27 +00:00
while (oldArray[i]!=NULL) {
next = oldArray[i]->next;
rellocateItem(table, oldArray[i]);
oldArray[i] = next;
}
1998-09-29 22:36:29 +00:00
}
1999-10-09 20:07:23 +00:00
wfree(oldArray);
1998-09-29 22:36:29 +00:00
}
WMHashTable*
WMCreateHashTable(WMHashTableCallbacks callbacks)
{
HashTable *table;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
table = wmalloc(sizeof(HashTable));
memset(table, 0, sizeof(HashTable));
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
table->callbacks = callbacks;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
table->size = INITIAL_CAPACITY;
table->table = wmalloc(sizeof(HashItem*)*table->size);
memset(table->table, 0, sizeof(HashItem*)*table->size);
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
return table;
}
void
WMResetHashTable(WMHashTable *table)
{
HashItem *item, *tmp;
int i;
for (i = 0; i < table->size; i++) {
2004-10-12 21:28:27 +00:00
item = table->table[i];
while (item) {
tmp = item->next;
RELKEY(table, item->key);
wfree(item);
item = tmp;
}
1998-09-29 22:36:29 +00:00
}
table->itemCount = 0;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
if (table->size > INITIAL_CAPACITY) {
2004-10-12 21:28:27 +00:00
wfree(table->table);
table->size = INITIAL_CAPACITY;
table->table = wmalloc(sizeof(HashItem*)*table->size);
1998-09-29 22:36:29 +00:00
}
memset(table->table, 0, sizeof(HashItem*)*table->size);
}
void
WMFreeHashTable(WMHashTable *table)
{
HashItem *item, *tmp;
int i;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
for (i = 0; i < table->size; i++) {
2004-10-12 21:28:27 +00:00
item = table->table[i];
while (item) {
tmp = item->next;
RELKEY(table, item->key);
wfree(item);
item = tmp;
}
1998-09-29 22:36:29 +00:00
}
1999-10-09 20:07:23 +00:00
wfree(table->table);
wfree(table);
1998-09-29 22:36:29 +00:00
}
unsigned
WMCountHashTable(WMHashTable *table)
{
return table->itemCount;
}
1998-09-29 22:36:29 +00:00
2001-01-05 23:32:10 +00:00
void*
1998-09-29 22:36:29 +00:00
WMHashGet(WMHashTable *table, const void *key)
{
unsigned h;
HashItem *item;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
h = HASH(table, key);
item = table->table[h];
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
if (table->callbacks.keyIsEqual) {
2004-10-12 21:28:27 +00:00
while (item) {
if ((*table->callbacks.keyIsEqual)(key, item->key)) {
break;
}
item = item->next;
}
1998-09-29 22:36:29 +00:00
} else {
2004-10-12 21:28:27 +00:00
while (item) {
if (key == item->key) {
break;
}
item = item->next;
}
1998-09-29 22:36:29 +00:00
}
if (item)
2004-10-12 21:28:27 +00:00
return (void*)item->data;
1998-09-29 22:36:29 +00:00
else
2004-10-12 21:28:27 +00:00
return NULL;
1998-09-29 22:36:29 +00:00
}
Bool
WMHashGetItemAndKey(WMHashTable *table, const void *key,
void **retItem, void **retKey)
{
unsigned h;
HashItem *item;
2004-10-12 21:28:27 +00:00
h = HASH(table, key);
item = table->table[h];
2004-10-12 21:28:27 +00:00
if (table->callbacks.keyIsEqual) {
2004-10-12 21:28:27 +00:00
while (item) {
if ((*table->callbacks.keyIsEqual)(key, item->key)) {
break;
}
item = item->next;
}
} else {
2004-10-12 21:28:27 +00:00
while (item) {
if (key == item->key) {
break;
}
item = item->next;
}
}
if (item) {
if (retKey)
*retKey = (void*)item->key;
if (retItem)
*retItem = (void*)item->data;
2004-10-12 21:28:27 +00:00
return True;
} else {
return False;
}
}
1998-09-29 22:36:29 +00:00
2001-01-05 23:32:10 +00:00
void*
2001-01-02 14:17:26 +00:00
WMHashInsert(WMHashTable *table, const void *key, const void *data)
1998-09-29 22:36:29 +00:00
{
unsigned h;
HashItem *item;
int replacing = 0;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
h = HASH(table, key);
/* look for the entry */
item = table->table[h];
if (table->callbacks.keyIsEqual) {
2004-10-12 21:28:27 +00:00
while (item) {
if ((*table->callbacks.keyIsEqual)(key, item->key)) {
replacing = 1;
break;
}
item = item->next;
}
1998-09-29 22:36:29 +00:00
} else {
2004-10-12 21:28:27 +00:00
while (item) {
if (key == item->key) {
replacing = 1;
break;
}
item = item->next;
}
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
if (replacing) {
2004-10-12 21:28:27 +00:00
const void *old;
1998-09-29 22:36:29 +00:00
2004-10-12 21:28:27 +00:00
old = item->data;
item->data = data;
RELKEY(table, item->key);
item->key = DUPKEY(table, key);
1998-09-29 22:36:29 +00:00
2004-10-12 21:28:27 +00:00
return (void*)old;
1998-09-29 22:36:29 +00:00
} else {
2004-10-12 21:28:27 +00:00
HashItem *nitem;
1998-09-29 22:36:29 +00:00
2004-10-12 21:28:27 +00:00
nitem = wmalloc(sizeof(HashItem));
nitem->key = DUPKEY(table, key);
nitem->data = data;
nitem->next = table->table[h];
table->table[h] = nitem;
1998-09-29 22:36:29 +00:00
2004-10-12 21:28:27 +00:00
table->itemCount++;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
/* OPTIMIZE: put this in an idle handler.*/
if (table->itemCount > table->size) {
#ifdef DEBUG0
2004-10-12 21:28:27 +00:00
printf("rebuilding hash table...\n");
1998-09-29 22:36:29 +00:00
#endif
2004-10-12 21:28:27 +00:00
rebuildTable(table);
1998-09-29 22:36:29 +00:00
#ifdef DEBUG0
2004-10-12 21:28:27 +00:00
printf("finished rebuild.\n");
1998-09-29 22:36:29 +00:00
#endif
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
return NULL;
}
static HashItem*
deleteFromList(HashTable *table, HashItem *item, const void *key)
{
HashItem *next;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
if (item==NULL)
2004-10-12 21:28:27 +00:00
return NULL;
if ((table->callbacks.keyIsEqual
&& (*table->callbacks.keyIsEqual)(key, item->key))
|| (!table->callbacks.keyIsEqual && key==item->key)) {
next = item->next;
RELKEY(table, item->key);
wfree(item);
table->itemCount--;
return next;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
item->next = deleteFromList(table, item->next, key);
return item;
}
void
WMHashRemove(WMHashTable *table, const void *key)
{
unsigned h;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
h = HASH(table, key);
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
table->table[h] = deleteFromList(table, table->table[h], key);
}
WMHashEnumerator
WMEnumerateHashTable(WMHashTable *table)
{
WMHashEnumerator enumerator;
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
enumerator.table = table;
enumerator.index = 0;
enumerator.nextItem = table->table[0];
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
return enumerator;
}
2001-01-05 23:32:10 +00:00
void*
1998-09-29 22:36:29 +00:00
WMNextHashEnumeratorItem(WMHashEnumerator *enumerator)
{
2001-01-02 14:17:26 +00:00
const void *data = NULL;
1998-09-29 22:36:29 +00:00
2004-10-12 21:28:27 +00:00
/* this assumes the table doesn't change between
1998-09-29 22:36:29 +00:00
* WMEnumerateHashTable() and WMNextHashEnumeratorItem() calls */
if (enumerator->nextItem==NULL) {
2004-10-12 21:28:27 +00:00
HashTable *table = enumerator->table;
while (++enumerator->index < table->size) {
if (table->table[enumerator->index]!=NULL) {
enumerator->nextItem = table->table[enumerator->index];
break;
}
}
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
1998-09-29 22:36:29 +00:00
if (enumerator->nextItem) {
2004-10-12 21:28:27 +00:00
data = ((HashItem*)enumerator->nextItem)->data;
enumerator->nextItem = ((HashItem*)enumerator->nextItem)->next;
1998-09-29 22:36:29 +00:00
}
2004-10-12 21:28:27 +00:00
2001-01-05 23:32:10 +00:00
return (void*)data;
1998-09-29 22:36:29 +00:00
}
2001-01-11 02:35:21 +00:00
void*
WMNextHashEnumeratorKey(WMHashEnumerator *enumerator)
{
const void *key = NULL;
2004-10-12 21:28:27 +00:00
/* this assumes the table doesn't change between
2001-01-11 02:35:21 +00:00
* WMEnumerateHashTable() and WMNextHashEnumeratorKey() calls */
if (enumerator->nextItem==NULL) {
HashTable *table = enumerator->table;
while (++enumerator->index < table->size) {
if (table->table[enumerator->index]!=NULL) {
enumerator->nextItem = table->table[enumerator->index];
break;
}
}
}
if (enumerator->nextItem) {
key = ((HashItem*)enumerator->nextItem)->key;
enumerator->nextItem = ((HashItem*)enumerator->nextItem)->next;
}
return (void*)key;
}
Bool
WMNextHashEnumeratorItemAndKey(WMHashEnumerator *enumerator,
void **item, void **key)
1998-09-29 22:36:29 +00:00
{
2004-10-12 21:28:27 +00:00
/* this assumes the table doesn't change between
* WMEnumerateHashTable() and WMNextHashEnumeratorItemAndKey() calls */
if (enumerator->nextItem==NULL) {
2004-10-12 21:28:27 +00:00
HashTable *table = enumerator->table;
while (++enumerator->index < table->size) {
if (table->table[enumerator->index]!=NULL) {
enumerator->nextItem = table->table[enumerator->index];
break;
}
}
}
if (enumerator->nextItem) {
if (item)
*item = (void*)((HashItem*)enumerator->nextItem)->data;
if (key)
*key = (void*)((HashItem*)enumerator->nextItem)->key;
enumerator->nextItem = ((HashItem*)enumerator->nextItem)->next;
return True;
}
return False;
1998-09-29 22:36:29 +00:00
}
static Bool
compareStrings(const char *key1, const char *key2)
{
return strcmp(key1, key2)==0;
}
typedef unsigned (*hashFunc)(const void*);
typedef Bool (*isEqualFunc)(const void*, const void*);
typedef void* (*retainFunc)(const void*);
typedef void (*releaseFunc)(const void*);
const WMHashTableCallbacks WMIntHashCallbacks = {
NULL,
NULL,
NULL,
NULL
1998-09-29 22:36:29 +00:00
};
const WMHashTableCallbacks WMStringHashCallbacks = {
(hashFunc)hashString,
(isEqualFunc)compareStrings,
(retainFunc)wstrdup,
(releaseFunc)wfree
1998-09-29 22:36:29 +00:00
};
const WMHashTableCallbacks WMStringPointerHashCallbacks = {
(hashFunc)hashString,
(isEqualFunc)compareStrings,
NULL,
NULL
1998-09-29 22:36:29 +00:00
};