File: hash.c

package info (click to toggle)
libcgns 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,740 kB
  • ctags: 4,493
  • sloc: ansic: 46,717; fortran: 1,341; sh: 368; makefile: 259
file content (327 lines) | stat: -rw-r--r-- 8,040 bytes parent folder | download
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * hash.c - general purpose hash table functions
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"

/*#define STATISTICS    define this for hit/seeks counts*/
/*#define REORDER       define this for reordering after find*/

typedef struct hash_element_ {
    void *entry;
    struct hash_element_ *next;
    struct hash_element_ **prev;
#ifdef STATISTICS
    int hits;
    int seeks;
#endif
} BUCKET;

typedef struct hash_tab_ {
    BUCKET **table;     /* pointer to hash table */
    int size;           /* Max number of elements in table */
    int numsyms;        /* number of elements currently in table */
    BUCKET *lastpos;    /* last bucket accessed by find */
    int (*compare)(     /* entry compare function */
#ifdef PROTOTYPE
        void *, void *
#endif
    );
    unsigned (*hash)(   /* pointer to hashing routine */
#ifdef PROTOTYPE
        void *
#endif
    );
} HASH_TAB;

/*---------- HashCreate ----------------------------------------------
 * creates a hash table of indicated size.
 * Should be a prime number for best results.
 * Useful values are:
 *   47 61 89 113 127 157 193 211 257 293 359 401
 *--------------------------------------------------------------------*/

HASH HashCreate (
#ifdef PROTOTYPE
    int size, int (*compare)(void *entry1, void *entry2),
    unsigned (*hashfunc)(void *entry))
#else
    size, compare, hashfunc)
int size;
int (*compare)();
unsigned (*hashfunc)();
#endif
{
    HASH_TAB *tabp;

    if (NULL == compare || NULL == hashfunc)
        return (NULL);
    if (size < 1) size = 11;
    tabp = (HASH_TAB *) malloc (sizeof(HASH_TAB) + size * sizeof(BUCKET *));
    if (NULL != tabp) {
        tabp->table   = (BUCKET **)(tabp + 1);
        tabp->size    = size;
        tabp->numsyms = 0;
        tabp->lastpos = NULL;
        tabp->compare = compare;
        tabp->hash    = hashfunc;
        for (size = 0; size < tabp->size; size++)
            tabp->table[size] = NULL;
    }
    return ((HASH)tabp);
}

/*---------- HashDestroy ---------------------------------------------
 * destroy a hash table
 *--------------------------------------------------------------------*/

void HashDestroy (
#ifdef PROTOTYPE
    HASH hash, void (*freeentry)(void *entry))
#else
    hash, freeentry)
HASH hash;
void (*freeentry)();
#endif
{
    HASH_TAB *tabp = (HASH_TAB *)hash;
    BUCKET *p, *next;
    int i;

    for (i = 0; i < tabp->size; i++) {
        for (p = tabp->table[i]; p != NULL; p = next) {
            next = p->next;
            if (NULL != freeentry)
                (*freeentry) (p->entry);
            free (p);
        }
    }
    free (tabp);
}

/*---------- HashFind -------------------------------------------------
 * finds symbol in hash table or NULL if no match
 * If more than one such entry is in the table, the most
 * recently added one is found (which will be first in the list).
 *---------------------------------------------------------------------*/

void *HashFind (
#ifdef PROTOTYPE
    HASH hash, void *entry)
#else
    hash, entry)
HASH hash;
void *entry;
#endif
{
    BUCKET *p, **pf;
    HASH_TAB *tabp = (HASH_TAB *)hash;

    pf = &(tabp->table)[(*tabp->hash) (entry) % tabp->size];
    p = *pf;

    while (NULL != p && (*tabp->compare) (entry, p->entry)) {
#ifdef STATISTICS
        p->seeks++;
#endif
        p = p->next;
    }
    if (NULL == p)
        return (NULL);

#ifdef STATISTICS
    p->hits++;
#endif
    tabp->lastpos = p;

    /*
     * this mod places the symbol at the beginning of the list
     * if not already there. This tends to cluster the most often
     * accessed symbols at the beginning of the table, thus
     * reducing the length of the search path
     */

#ifdef REORDER
    if (p != *pf) {
        BUCKET *sym = *pf;
        if ((*(p->prev) = p->next) != NULL)
            p->next->prev = p->prev;
        *pf = p;
        p->prev = pf;
        p->next = sym;
        sym->prev = &p->next;
    }
#endif

    return (p->entry);
}

/*---------- HashAdd -------------------------------------------------
 * add a symbol to the hash table
 * The new symbol is placed first in the bucket.
 *--------------------------------------------------------------------*/

void *HashAdd (
#ifdef PROTOTYPE
    HASH hash, void *entry)
#else
    hash, entry)
HASH hash;
void *entry;
#endif
{
    BUCKET **p, *tmp;
    BUCKET *sym;
    HASH_TAB *tabp = (HASH_TAB *)hash;

    if ((sym = (BUCKET *) malloc (sizeof(BUCKET))) == NULL)
        return (NULL);

    p = &(tabp->table)[(*tabp->hash) (entry) % tabp->size];

    tmp = *p;
    *p = sym;
    sym->entry = entry;
    sym->prev = p;
    sym->next = tmp;

    if (NULL != tmp)
        tmp->prev = &sym->next;

    tabp->numsyms++;
    tabp->lastpos = NULL;
    return (entry);
}

/*---------- HashDelete ----------------------------------------------
 * removes a symbol from the hash table
 *--------------------------------------------------------------------*/

void *HashDelete (
#ifdef PROTOTYPE
    HASH hash, void *entry)
#else
    hash, entry)
HASH hash;
void *entry;
#endif
{
    BUCKET *p;
    HASH_TAB *tabp = (HASH_TAB *)hash;

    if (NULL == tabp->lastpos || entry != tabp->lastpos->entry) {
        if (NULL == HashFind (hash, entry))
            return (NULL);
    }
    p = tabp->lastpos;
    tabp->numsyms--;
    if ((*(p->prev) = p->next) != NULL)
        p->next->prev = p->prev;
    free (p);
    return (entry);
}

/*---------- HashList ------------------------------------------------
 * output the hash table
 *--------------------------------------------------------------------*/

int HashList (
#ifdef PROTOTYPE
    HASH hash, int (*listentry)(void *entry, void *userdata),
    void *userdata)
#else
    hash, listentry, userdata)
HASH hash;
int (*listentry)();
void *userdata;
#endif
{
    HASH_TAB *tabp = (HASH_TAB *)hash;
    BUCKET *p;
    int i, cnt = 0;

    if (NULL == listentry)
        return (tabp->numsyms);
    for (i = 0; i < tabp->size; i++) {
        for (p = tabp->table[i]; p != NULL; p = p->next)
            cnt += (*listentry) (p->entry, userdata);
    }
    return (cnt);
}

/*---------- HashStats ------------------------------------------------
 * print a variety of statistics about the hash table
 *---------------------------------------------------------------------*/

#define MAXINT  (((unsigned) ~ 0) >> 1)
#define MAXLEN  128

void HastStats (
#ifdef PROTOTYPE
    HASH hash)
#else
    hash)
HASH hash;
#endif
{
    HASH_TAB *tabp = (HASH_TAB *)hash;
    BUCKET *p;
    int i;
    int chain_len;
    int chain_avg;
    int deviation = 0;
    int maxlen = 0;
    int minlen = MAXINT;
    int lengths[MAXLEN];
    int longer = 0;
#ifdef STATISTICS
    long hits = 0;
    long seeks = 0;
#endif

    chain_avg = tabp->numsyms / tabp->size;
    memset (lengths, 0, sizeof(lengths));

    for (i = 0; i < tabp->size; i++) {
        chain_len = 0;
        for (p = tabp->table[i]; p; p = p->next) {
#ifdef STATISTICS
            hits += p->hits;
            seeks += p->seeks;
#endif
            chain_len++;
        }

        if (chain_len >= MAXLEN)
            longer++;
        else
            ++lengths[chain_len];

        if (minlen > chain_len) minlen = chain_len;
        if (maxlen < chain_len) maxlen = chain_len;

        deviation += abs (chain_len - chain_avg);
    }

    printf ("%d entries in %d element hash table, ",
        tabp->numsyms, tabp->size);
    printf ("%d (%1.0f%%) empty.\n",
        lengths[0], ((double)lengths[0] / tabp->size) * 100.0);
    printf ("Mean chain length = %d, min = %d, max = %d, dev = %d\n",
        chain_avg, minlen, maxlen, deviation / tabp->size);
#ifdef STATISTICS
    printf ("Table searchs = %ld, found = %ld, percent = %.2f\n", seeks, hits,
        (seeks ? (double)hits / seeks * 100.0 : (double)0));
#endif

    for (i = 0; i < MAXLEN; i++)
        if (lengths[i])
            printf ("%3d chains of length %d\n", lengths[i], i);

    if (longer)
        printf ("%3d chains of length %d or longer\n", longer, MAXLEN);
}