1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/* +-------------------------------------------------------------------+ */
/* | Copyright 1992, David Koblas. | */
/* | Copyright 1995, 1996 Torsten Martinsen (bullestock@dk-online.dk) | */
/* | | */
/* | Permission to use, copy, modify, and distribute this software | */
/* | and its documentation for any purpose and without fee is hereby | */
/* | granted, provided that the above copyright notice appear in all | */
/* | copies and that both that copyright notice and this permission | */
/* | notice appear in supporting documentation. This software is | */
/* | provided "as is" without express or implied warranty. | */
/* +-------------------------------------------------------------------+ */
/* $Id: hash.c,v 1.3 1996/04/15 14:19:09 torsten Exp $ */
/*
** A simple useful set of hash routines:
**
** void *HashCreate(int (*cmp)(void *, void*), void (*free)(void *), int nelem)
** -- Create a hash table with cmp() function, and optional free() function.
** void HashDestroy(HashTable *tbl)
** -- Destroy the whole hash table
** void *HashFind(HashTable *tbl, int value, void *val)
** -- Find an element in the hash table, returns NULL if not found
** int HashAdd(HashTable *tbl, int value, void *val)
** -- Add an element to the table, returns non-zero on failure
** int HashRemove(HashTable *tbl, int value, void *elem)
** -- Remove a particular hash entry from the table, pointer comparison
*/
#include <stdio.h>
#include <stdlib.h>
#include <X11/Intrinsic.h>
#include "misc.h"
#include "hash.h"
typedef struct hash_s {
int *val;
struct hash_s *left, *right, *same, **parentp;
} HashElement;
typedef struct {
int (*cmp) (void *, void *);
void (*free) (void *);
HashElement **table;
int nelem;
} HashTable;
/*
** Create a hashtable, with cmp() compare function, and free() free function
** with a hash table width of nelem, free can be null in which case the
** system free function will be used.
*/
void *
HashCreate(int (*cmp) (void *, void *), void (*free) (void *), int nelem)
{
HashTable *new;
int i;
new = (HashTable *) xmalloc(sizeof(HashTable));
new->nelem = nelem;
new->cmp = cmp;
new->free = free;
new->table = (HashElement **) xmalloc(nelem * sizeof(HashElement *));
for (i = 0; i < nelem; i++)
new->table[i] = NULL;
return (void *) new;
}
/*
* Helper function for HashDestroy()
*/
static void
hashDestory(void (*func) (void *), HashElement * entry)
{
if (entry->left != NULL) {
hashDestory(func, entry->left);
free(entry->left);
}
if (entry->right != NULL) {
hashDestory(func, entry->right);
free(entry->right);
}
if (entry->same != NULL) {
hashDestory(func, entry->same);
free(entry->same);
}
if (entry->val)
func(entry->val);
}
/*
** Free the entire hash table, and all elements
*/
void
HashDestroy(void *t)
{
int i;
HashTable *tbl = (HashTable *) t;
if (tbl == NULL)
return;
for (i = 0; i < tbl->nelem; i++) {
if (tbl->table[i] != NULL) {
hashDestory(tbl->free ? tbl->free : free, tbl->table[i]);
free(tbl->table[i]);
}
}
free(tbl->table);
free(tbl);
}
/*
** Find a particular element in the hash table, returns NULL if it isn't found
*/
void *
HashFind(void *t, int value, void *val)
{
HashTable *tbl = (HashTable *) t;
HashElement *cur;
int v;
if (tbl == NULL)
return NULL;
cur = tbl->table[value];
while (cur != NULL) {
if ((v = tbl->cmp(cur->val, val)) == 0)
return cur->val;
if (v < 0)
cur = cur->left;
else
cur = cur->right;
}
return NULL;
}
/*
** Add a new element to the hash table, returns non-zero on failure
*/
int
HashAdd(void *t, int value, void *val)
{
HashTable *tbl = (HashTable *) t;
HashElement *new;
HashElement *cur = tbl->table[value];
HashElement **prev;
int v;
if (tbl == NULL)
return 1;
new = (HashElement *) xmalloc(sizeof(HashElement));
new->left = NULL;
new->right = NULL;
new->same = NULL;
new->val = val;
new->parentp = NULL;
prev = &tbl->table[value];
while (cur != NULL) {
if ((v = tbl->cmp(cur->val, val)) < 0) {
prev = &cur->left;
cur = cur->left;
} else if (v > 0) {
prev = &cur->right;
cur = cur->right;
} else {
prev = &cur->same;
new->same = cur->same;
if (cur->same != NULL)
cur->same->parentp = &new->same;
break;
}
}
*prev = new;
new->parentp = prev;
return 0;
}
static HashElement *
find(HashElement * pos, void *elem)
{
HashElement *v;
if (pos == NULL)
return NULL;
if (pos->val == elem)
return pos;
if (pos->left != NULL && (v = find(pos->left, elem)) != NULL)
return v;
if (pos->right != NULL && (v = find(pos->right, elem)) != NULL)
return v;
if (pos->same != NULL && (v = find(pos->same, elem)) != NULL)
return v;
return NULL;
}
/*
** Remove a particular element from the hash table, done using pointer compares
*/
int
HashRemove(void *t, int value, void *elem)
{
HashTable *tbl = (HashTable *) t;
HashElement *v;
if (elem == NULL || tbl == NULL)
return 1;
if ((v = find(tbl->table[value], elem)) == NULL)
return 0; /* The element was not actually in the table */
if (v->same != NULL) {
/*
** Same links are not allowed to have left & right children
** so just inherit the parent's children.
*/
if (v->left != NULL)
v->left->parentp = &v->same->left;
if (v->right != NULL)
v->right->parentp = &v->same->right;
v->same->left = v->left;
v->same->right = v->right;
*v->parentp = v->same;
v->same->parentp = v->parentp;
} else if (v->left != NULL) {
*v->parentp = v->left;
v->left->parentp = v->parentp;
if (v->right != NULL) {
HashElement *cur = v->left, **prev = &v->left;
while (cur != NULL) {
if (tbl->cmp(cur->val, v->right->val) < 0) {
prev = &cur->left;
cur = cur->left;
} else {
prev = &cur->right;
cur = cur->right;
}
}
*prev = v->right;
v->right->parentp = prev;
}
} else {
/* no left side, just attach the right */
*v->parentp = v->right;
if (v->right != NULL)
v->right->parentp = v->parentp;
}
free(v);
return 1;
}
|