Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / misc / hsearch_r.c
blob5ea1d5e6c8d6c22c98076c8b239b29df21a27dfd
1 /* Copyright (C) 1993, 1995 Free Software Foundation, Inc.
2 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <malloc.h>
22 #include <string.h>
24 #include <search.h>
26 /* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
27 [Knuth] The Art of Computer Programming, part 3 (6.4) */
30 /* The reentrent version has no static variables to maintain the state.
31 Instead the interface of all functions is extended to take an argument
32 which describes the current status. */
33 typedef struct _ENTRY
35 int used;
36 ENTRY entry;
38 _ENTRY;
41 /* For the used double hash method the table size has to be a prime. To
42 correct the user given table size we need a prime test. This trivial
43 algorithm is adequate because
44 a) the code is (most probably) called a few times per program run and
45 b) the number is small because the table must fit in the core */
46 static int
47 isprime (number)
48 unsigned int number;
50 /* no even number will be passed */
51 unsigned int div = 3;
53 while (div * div < number && number % div != 0)
54 div += 2;
56 return number % div != 0;
60 /* Before using the hash table we must allocate memory for it.
61 Test for an existing table are done. We allocate one element
62 more as the found prime number says. This is done for more effective
63 indexing as explained in the comment for the hsearch function.
64 The contents of the table is zeroed, especially the field used
65 becomes zero. */
66 int
67 hcreate_r (nel, htab)
68 unsigned int nel;
69 struct hsearch_data *htab;
71 /* Test for correct arguments. */
72 if (htab == NULL)
74 errno = EINVAL;
75 return 0;
78 /* There is still another table active. Return with error. */
79 if (htab->table != NULL)
80 return 0;
82 /* Change nel to the first prime number not smaller as nel. */
83 nel |= 1; /* make odd */
84 while (!isprime (nel))
85 nel += 2;
87 htab->size = nel;
88 htab->filled = 0;
90 /* allocate memory and zero out */
91 htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY));
92 if (htab->table == NULL)
93 return 0;
95 /* everything went alright */
96 return 1;
100 /* After using the hash table it has to be destroyed. The used memory can
101 be freed and the local static variable can be marked as not used. */
102 void
103 hdestroy_r (htab)
104 struct hsearch_data *htab;
106 /* Test for correct arguments. */
107 if (htab == NULL)
109 errno = EINVAL;
110 return;
113 if (htab->table != NULL)
114 /* free used memory */
115 free (htab->table);
117 /* the sign for an existing table is an value != NULL in htable */
118 htab->table = NULL;
122 /* This is the search function. It uses double hashing with open adressing.
123 The argument item.key has to be a pointer to an zero terminated, most
124 probably strings of chars. The function for generating a number of the
125 strings is simple but fast. It can be replaced by a more complex function
126 like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
128 We use an trick to speed up the lookup. The table is created by hcreate
129 with one more element available. This enables us to use the index zero
130 special. This index will never be used because we store the first hash
131 index in the field used where zero means not used. Every other value
132 means used. The used field can be used as a first fast comparison for
133 equality of the stored and the parameter value. This helps to prevent
134 unnecessary expensive calls of strcmp. */
136 hsearch_r (item, action, retval, htab)
137 ENTRY item;
138 ACTION action;
139 ENTRY **retval;
140 struct hsearch_data *htab;
142 unsigned int hval;
143 unsigned int count;
144 unsigned int len = strlen (item.key);
145 unsigned int idx;
147 /* If table is full and another entry should be entered return with
148 error. */
149 if (action == ENTER && htab->filled == htab->size)
151 errno = ENOMEM;
152 *retval = NULL;
153 return 0;
156 /* Compute an value for the given string. Perhaps use a better method. */
157 hval = len;
158 count = len;
159 while (count-- > 0)
161 hval <<= 4;
162 hval += item.key[count];
165 /* First hash function: simply take the modul but prevent zero. */
166 hval %= htab->size;
167 if (hval == 0)
168 ++hval;
170 /* The first index tried. */
171 idx = hval;
173 if (htab->table[idx].used)
175 /* Further action might be required according to the action value. */
176 unsigned hval2;
178 if (htab->table[idx].used == hval
179 && strcmp (item.key, htab->table[idx].entry.key) == 0)
181 if (action == ENTER)
182 htab->table[idx].entry.data = item.data;
184 *retval = &htab->table[idx].entry;
185 return 1;
188 /* Second hash function, as suggested in [Knuth] */
189 hval2 = 1 + hval % (htab->size - 2);
193 /* Because SIZE is prime this guarantees to step through all
194 available indeces. */
195 if (idx <= hval2)
196 idx = htab->size + idx - hval2;
197 else
198 idx -= hval2;
200 /* If entry is found use it. */
201 if (htab->table[idx].used == hval
202 && strcmp (item.key, htab->table[idx].entry.key) == 0)
204 if (action == ENTER)
205 htab->table[idx].entry.data = item.data;
207 *retval = &htab->table[idx].entry;
208 return 1;
211 while (htab->table[idx].used);
214 /* An empty bucket has been found. */
215 if (action == ENTER)
217 htab->table[idx].used = hval;
218 htab->table[idx].entry = item;
220 ++htab->filled;
222 *retval = &htab->table[idx].entry;
223 return 1;
226 errno = ESRCH;
227 *retval = NULL;
228 return 0;