summaryrefslogtreecommitdiffstats
path: root/Utilities/cmtar/listhash/hash.c.in
blob: 41a1618ab74c3a9e5b86b2ed6db3c0748a55ba5e (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* @configure_input@ */

/*
**  Copyright 1998-2002 University of Illinois Board of Trustees
**  Copyright 1998-2002 Mark D. Roth
**  All rights reserved. 
**
**  @LISTHASH_PREFIX@_hash.c - hash table routines
**
**  Mark D. Roth <roth@uiuc.edu>
**  Campus Information Technologies and Educational Services
**  University of Illinois at Urbana-Champaign
*/

#include <@LISTHASH_PREFIX@/config.h>
#include <@LISTHASH_PREFIX@/compat.h>

#include <@LISTHASH_PREFIX@/@LISTHASH_PREFIX@_listhash.h>

#include <stdio.h>
#include <errno.h>

#ifdef STDC_HEADERS
# include <stdlib.h>
#endif


/*
** @LISTHASH_PREFIX@_hashptr_reset() - reset a hash pointer
*/
void
@LISTHASH_PREFIX@_hashptr_reset(@LISTHASH_PREFIX@_hashptr_t *hp)
{
  @LISTHASH_PREFIX@_listptr_reset(&(hp->node));
  hp->bucket = -1;
}


/*
** @LISTHASH_PREFIX@_hashptr_data() - retrieve the data being pointed to
*/
void *
@LISTHASH_PREFIX@_hashptr_data(@LISTHASH_PREFIX@_hashptr_t *hp)
{
  return @LISTHASH_PREFIX@_listptr_data(&(hp->node));
}


/*
** @LISTHASH_PREFIX@_str_hashfunc() - default hash function, optimized for
**              7-bit strings
*/
unsigned int
@LISTHASH_PREFIX@_str_hashfunc(char *key, unsigned int num_buckets)
{
#if 0
  register unsigned result = 0;
  register int i;

  if (key == NULL)
    return 0;

  for (i = 0; *key != '\0' && i < 32; i++)
    result = result * 33U + *key++;

  return (result % num_buckets);
#else
  if (key == NULL)
    return 0;

  return (key[0] % num_buckets);
#endif
}


/*
** @LISTHASH_PREFIX@_hash_nents() - return number of elements from hash
*/
unsigned int
@LISTHASH_PREFIX@_hash_nents(@LISTHASH_PREFIX@_hash_t *h)
{
  return h->nents;
}


/*
** @LISTHASH_PREFIX@_hash_new() - create a new hash
*/
@LISTHASH_PREFIX@_hash_t *
@LISTHASH_PREFIX@_hash_new(int num, @LISTHASH_PREFIX@_hashfunc_t hashfunc)
{
  @LISTHASH_PREFIX@_hash_t *hash;

  hash = (@LISTHASH_PREFIX@_hash_t *)calloc(1, sizeof(@LISTHASH_PREFIX@_hash_t));
  if (hash == NULL)
    return NULL;
  hash->numbuckets = num;
  if (hashfunc != NULL)
    hash->hashfunc = hashfunc;
  else
    hash->hashfunc = (@LISTHASH_PREFIX@_hashfunc_t)@LISTHASH_PREFIX@_str_hashfunc;

  hash->table = (@LISTHASH_PREFIX@_list_t **)calloc(num, sizeof(@LISTHASH_PREFIX@_list_t *));
  if (hash->table == NULL)
  {
    free(hash);
    return NULL;
  }

  return hash;
}


/*
** @LISTHASH_PREFIX@_hash_next() - get next element in hash
** returns:
**  1      data found
**  0      end of list
*/
int
@LISTHASH_PREFIX@_hash_next(@LISTHASH_PREFIX@_hash_t *h,
          @LISTHASH_PREFIX@_hashptr_t *hp)
{
#ifdef DS_DEBUG
  printf("==> @LISTHASH_PREFIX@_hash_next(h=0x%lx, hp={%d,0x%lx})\n",
         h, hp->bucket, hp->node);
#endif

  if (hp->bucket >= 0 && hp->node != NULL &&
      @LISTHASH_PREFIX@_list_next(h->table[hp->bucket], &(hp->node)) != 0)
  {
#ifdef DS_DEBUG
    printf("    @LISTHASH_PREFIX@_hash_next(): found additional "
           "data in current bucket (%d), returing 1\n",
           hp->bucket);
#endif
    return 1;
  }

#ifdef DS_DEBUG
  printf("    @LISTHASH_PREFIX@_hash_next(): done with bucket %d\n",
         hp->bucket);
#endif

  for (hp->bucket++; hp->bucket < h->numbuckets; hp->bucket++)
  {
#ifdef DS_DEBUG
    printf("    @LISTHASH_PREFIX@_hash_next(): "
           "checking bucket %d\n", hp->bucket);
#endif
    hp->node = NULL;
    if (h->table[hp->bucket] != NULL &&
        @LISTHASH_PREFIX@_list_next(h->table[hp->bucket],
                &(hp->node)) != 0)
    {
#ifdef DS_DEBUG
      printf("    @LISTHASH_PREFIX@_hash_next(): "
             "found data in bucket %d, returing 1\n",
             hp->bucket);
#endif
      return 1;
    }
  }

  if (hp->bucket == h->numbuckets)
  {
#ifdef DS_DEBUG
    printf("    @LISTHASH_PREFIX@_hash_next(): hash pointer "
           "wrapped to 0\n");
#endif
    hp->bucket = -1;
    hp->node = NULL;
  }

#ifdef DS_DEBUG
  printf("<== @LISTHASH_PREFIX@_hash_next(): no more data, "
         "returning 0\n");
#endif
  return 0;
}


/*
** @LISTHASH_PREFIX@_hash_del() - delete an entry from the hash
** returns:
**  0      success
**  -1 (and sets errno)  failure
*/
int
@LISTHASH_PREFIX@_hash_del(@LISTHASH_PREFIX@_hash_t *h,
         @LISTHASH_PREFIX@_hashptr_t *hp)
{
  if (hp->bucket < 0
      || hp->bucket >= h->numbuckets
      || h->table[hp->bucket] == NULL
      || hp->node == NULL)
  {
    errno = EINVAL;
    return -1;
  }

  @LISTHASH_PREFIX@_list_del(h->table[hp->bucket], &(hp->node));
  h->nents--;
  return 0;
}


/*
** @LISTHASH_PREFIX@_hash_empty() - empty the hash
*/
void
@LISTHASH_PREFIX@_hash_empty(@LISTHASH_PREFIX@_hash_t *h, @LISTHASH_PREFIX@_freefunc_t freefunc)
{
  int i;

  for (i = 0; i < h->numbuckets; i++)
    if (h->table[i] != NULL)
      @LISTHASH_PREFIX@_list_empty(h->table[i], freefunc);

  h->nents = 0;
}


/*
** @LISTHASH_PREFIX@_hash_free() - delete all of the nodes in the hash
*/
void
@LISTHASH_PREFIX@_hash_free(@LISTHASH_PREFIX@_hash_t *h, @LISTHASH_PREFIX@_freefunc_t freefunc)
{
  int i;

  for (i = 0; i < h->numbuckets; i++)
    if (h->table[i] != NULL)
      @LISTHASH_PREFIX@_list_free(h->table[i], freefunc);

  free(h->table);
  free(h);
}


/*
** @LISTHASH_PREFIX@_hash_search() - iterative search for an element in a hash
** returns:
**  1      match found
**  0      no match
*/
int
@LISTHASH_PREFIX@_hash_search(@LISTHASH_PREFIX@_hash_t *h,
            @LISTHASH_PREFIX@_hashptr_t *hp, void *data,
            @LISTHASH_PREFIX@_matchfunc_t matchfunc)
{
  while (@LISTHASH_PREFIX@_hash_next(h, hp) != 0)
    if ((*matchfunc)(data, @LISTHASH_PREFIX@_listptr_data(&(hp->node))) != 0)
      return 1;

  return 0;
}


/*
** @LISTHASH_PREFIX@_hash_getkey() - hash-based search for an element in a hash
** returns:
**  1      match found
**  0      no match
*/
int
@LISTHASH_PREFIX@_hash_getkey(@LISTHASH_PREFIX@_hash_t *h,
            @LISTHASH_PREFIX@_hashptr_t *hp, void *key,
            @LISTHASH_PREFIX@_matchfunc_t matchfunc)
{
#ifdef DS_DEBUG
  printf("==> @LISTHASH_PREFIX@_hash_getkey(h=0x%lx, hp={%d,0x%lx}, "
         "key=0x%lx, matchfunc=0x%lx)\n",
         h, hp->bucket, hp->node, key, matchfunc);
#endif

  if (hp->bucket == -1)
  {
    hp->bucket = (*(h->hashfunc))(key, h->numbuckets);
#ifdef DS_DEBUG
    printf("    @LISTHASH_PREFIX@_hash_getkey(): hp->bucket "
           "set to %d\n", hp->bucket);
#endif
  }

  if (h->table[hp->bucket] == NULL)
  {
#ifdef DS_DEBUG
    printf("    @LISTHASH_PREFIX@_hash_getkey(): no list "
           "for bucket %d, returning 0\n", hp->bucket);
#endif
    hp->bucket = -1;
    return 0;
  }

#ifdef DS_DEBUG
  printf("<== @LISTHASH_PREFIX@_hash_getkey(): "
         "returning @LISTHASH_PREFIX@_list_search()\n");
#endif
  return @LISTHASH_PREFIX@_list_search(h->table[hp->bucket], &(hp->node),
               key, matchfunc);
}


/*
** @LISTHASH_PREFIX@_hash_add() - add an element to the hash
** returns:
**  0      success
**  -1 (and sets errno)  failure
*/
int
@LISTHASH_PREFIX@_hash_add(@LISTHASH_PREFIX@_hash_t *h, void *data)
{
  int bucket, i;

#ifdef DS_DEBUG
  printf("==> @LISTHASH_PREFIX@_hash_add(h=0x%lx, data=0x%lx)\n",
         h, data);
#endif

  bucket = (*(h->hashfunc))(data, h->numbuckets);
#ifdef DS_DEBUG
  printf("    @LISTHASH_PREFIX@_hash_add(): inserting in bucket %d\n",
         bucket);
#endif
  if (h->table[bucket] == NULL)
  {
#ifdef DS_DEBUG
    printf("    @LISTHASH_PREFIX@_hash_add(): creating new list\n");
#endif
    h->table[bucket] = @LISTHASH_PREFIX@_list_new(LIST_QUEUE, NULL);
  }

#ifdef DS_DEBUG
  printf("<== @LISTHASH_PREFIX@_hash_add(): "
         "returning @LISTHASH_PREFIX@_list_add()\n");
#endif
  i = @LISTHASH_PREFIX@_list_add(h->table[bucket], data);
  if (i == 0)
    h->nents++;
  return i;
}