summaryrefslogtreecommitdiff
path: root/revnetgroup/hash.c
blob: bd38f2bd814df9425c0fc4b578ba0f858e571f96 (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
/*
** hash.c - functions for a hash table.
**
** Copyright (c) 1996, 1997, 1999 Thorsten Kukuk
**
** This file is part of the NYS YP Server.
**
** The YP Server is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** version 2 as published by the Free Software Foundation.
**
** The NYS YP Server 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 the NYS YP Server; see the file COPYING.  If
** not, write to the Free Software Foundation, Inc., 51 Franklin Street,
** Suite 500, Boston, MA 02110-1335, USA.
**
** Author: Thorsten Kukuk <kukuk@suse.de>
*/


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include <assert.h>

#define TABLESIZE 997		/*Should be a prime */

/*
 * hash_malloc(void)
 *
 *   Initialize a new hash table.
 */
hash_t **
hash_malloc (void)
{
  hash_t **work = NULL;
  int i = 0;

  work = malloc (sizeof (hash_t *) * TABLESIZE);
  if (work == NULL)
    {
      fprintf (stderr, "Out of memory.\n");
      exit (1);
    }

  for (i = 0; i < TABLESIZE; i++)
    work[i] = NULL;

  return work;
}

/*
 * hash_calc_key(const char* key)
 *
 *   Calculates the key, returns it.
 */
static inline long
hash_calc_key (const char *key)
{
  long hkey = 0;
  int length = strlen (key);
  int i = -1;

  for (i = 0; i < length; i++)
    hkey = (256 * hkey + key[i]) % TABLESIZE;

  assert (hkey < TABLESIZE);
  return hkey;
}


/*
 * hash_insert(hash_t **table, const char*, const char*)
 *
 *   Complete re-write, to insert item into head of list
 *   at it's entry in the table.
 */
int
hash_insert (hash_t **table, const char *key, const char *val)
{
  long hkey = -1;
  hash_t *work = NULL;

  assert (table != NULL);
  assert (key != NULL);
  assert (val != NULL);

  hkey = hash_calc_key (key);

  /* look for the item */
  work = table[hkey];
  while (work != NULL)
    {
      if (strcmp (work->key, key) == 0)
	{
	  return -1;
	}
      work = work->next;
    }

  /* insert into head of list */
  work = malloc (sizeof (hash_t));
  if (work == NULL)
    {
      fprintf (stderr, "Out of Memory.\n");
      exit (1);
    }

  /* setup the new node */
  work->key = strdup (key);
  work->val = strdup (val);
  work->next = NULL;

  if (table[hkey] != NULL)
    {
      work->next = table[hkey];
    }

  table[hkey] = work;

  return 0;
}


/*
 * hash_free(hash_t**)
 *
 *   Deallocates all the structures.
 *
 */
int
hash_free (hash_t **table UNUSED)
{
  /* Not implementet yet! */

  return 0;
}


/*
 * hash_search(hash_t**, const char*)
 *
 *   Looks for specified key, returns value if found,
 *   and NULL if not.
 *
 */
char *
hash_search (hash_t **table, const char *key)
{
  hash_t *work = NULL;
  long hkey = -1;

  assert (table != NULL);
  assert (key != NULL);

  hkey = hash_calc_key (key);

  /* look for the key in the list */
  work = table[hkey];
  while (work != NULL)
    {
      if (strcmp (work->key, key) == 0)
	return work->val;

      work = work->next;
    }

  return NULL;
}


/*
 * hash_delkey(hash_t**, const char* )
 *
 *   Delete the item from the table.
 */
int
hash_delkey (hash_t **table, const char *key)
{
  hash_t *work = NULL;
  hash_t *prev = NULL;
  long hkey = -1;

  assert (table != NULL);
  assert (key != NULL);

  hkey = hash_calc_key (key);

  work = table[hkey];
  prev = table[hkey];

  while (work != NULL)
    {
      if (strcmp (work->key, key) == 0)
	{

	  /* delete this node, and return? */
	  if (work == table[hkey])
	    table[hkey] = work->next;
	  else
	    prev->next = work->next;

	  free (work->key);
	  free (work->val);
	  free (work);
	  break;
	}

      prev = work;
      work = work->next;
    }

  return 0;
}


/*
 * hash_first(hash_t**)
 *
 *   Returns the first item in the hash table.
 */
hash_t *
hash_first (hash_t **table)
{
  unsigned long i = 0;

  for (i = 0; i < TABLESIZE; i++)
    {
      if (table[i] != NULL)
	return table[i];
    }

  return NULL;
}

/*
 * hash_next(hash_t**, const char*)
 *
 *   Returns the next item in the cache.
 */
hash_t *
hash_next (hash_t **table, const char *key)
{
  hash_t *work = NULL;
  long hkey = -1;

  assert (table != NULL);
  assert (key != NULL);

  hkey = hash_calc_key (key);

  /* look for the item */
  work = table[hkey];
  while (work != NULL)
    {
      if (strcmp (work->key, key) == 0)
	{
	  work = work->next;
	  break;
	}
      work = work->next;
    }

  /* at this point, we have seen the key:
   * starting from here, return the first
   * valid pointer we find
   */
  if (work != NULL)
    return work;

  /* work is NULL, increment to next list. */
  hkey++;
  while (hkey < TABLESIZE)
    {
      if (table[hkey] != NULL)
	return table[hkey];

      hkey++;
    }

  return NULL;
}