Update.
[glibc.git] / misc / hsearch_r.c
blob398f0b6dd9dcced1848c89d8eebf79ae26dc2b85
1 /* Copyright (C) 1993, 1995, 1996, 1997, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1993.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 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 reentrant 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 unsigned 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 (unsigned int number)
49 /* no even number will be passed */
50 unsigned int div = 3;
52 while (div * div < number && number % div != 0)
53 div += 2;
55 return number % div != 0;
59 /* Before using the hash table we must allocate memory for it.
60 Test for an existing table are done. We allocate one element
61 more as the found prime number says. This is done for more effective
62 indexing as explained in the comment for the hsearch function.
63 The contents of the table is zeroed, especially the field used
64 becomes zero. */
65 int
66 hcreate_r (nel, htab)
67 size_t nel;
68 struct hsearch_data *htab;
70 /* Test for correct arguments. */
71 if (htab == NULL)
73 __set_errno (EINVAL);
74 return 0;
77 /* There is still another table active. Return with error. */
78 if (htab->table != NULL)
79 return 0;
81 /* Change nel to the first prime number not smaller as nel. */
82 nel |= 1; /* make odd */
83 while (!isprime (nel))
84 nel += 2;
86 htab->size = nel;
87 htab->filled = 0;
89 /* allocate memory and zero out */
90 htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY));
91 if (htab->table == NULL)
92 return 0;
94 /* everything went alright */
95 return 1;
97 libc_hidden_def (hcreate_r)
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 __set_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;
120 libc_hidden_def (hdestroy_r)
123 /* This is the search function. It uses double hashing with open addressing.
124 The argument item.key has to be a pointer to an zero terminated, most
125 probably strings of chars. The function for generating a number of the
126 strings is simple but fast. It can be replaced by a more complex function
127 like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
129 We use an trick to speed up the lookup. The table is created by hcreate
130 with one more element available. This enables us to use the index zero
131 special. This index will never be used because we store the first hash
132 index in the field used where zero means not used. Every other value
133 means used. The used field can be used as a first fast comparison for
134 equality of the stored and the parameter value. This helps to prevent
135 unnecessary expensive calls of strcmp. */
137 hsearch_r (item, action, retval, htab)
138 ENTRY item;
139 ACTION action;
140 ENTRY **retval;
141 struct hsearch_data *htab;
143 unsigned int hval;
144 unsigned int count;
145 unsigned int len = strlen (item.key);
146 unsigned int idx;
148 /* Compute an value for the given string. Perhaps use a better method. */
149 hval = len;
150 count = len;
151 while (count-- > 0)
153 hval <<= 4;
154 hval += item.key[count];
157 /* First hash function: simply take the modul but prevent zero. */
158 hval %= htab->size;
159 if (hval == 0)
160 ++hval;
162 /* The first index tried. */
163 idx = hval;
165 if (htab->table[idx].used)
167 /* Further action might be required according to the action value. */
168 unsigned hval2;
170 if (htab->table[idx].used == hval
171 && strcmp (item.key, htab->table[idx].entry.key) == 0)
173 *retval = &htab->table[idx].entry;
174 return 1;
177 /* Second hash function, as suggested in [Knuth] */
178 hval2 = 1 + hval % (htab->size - 2);
182 /* Because SIZE is prime this guarantees to step through all
183 available indeces. */
184 if (idx <= hval2)
185 idx = htab->size + idx - hval2;
186 else
187 idx -= hval2;
189 /* If we visited all entries leave the loop unsuccessfully. */
190 if (idx == hval)
191 break;
193 /* If entry is found use it. */
194 if (htab->table[idx].used == hval
195 && strcmp (item.key, htab->table[idx].entry.key) == 0)
197 *retval = &htab->table[idx].entry;
198 return 1;
201 while (htab->table[idx].used);
204 /* An empty bucket has been found. */
205 if (action == ENTER)
207 /* If table is full and another entry should be entered return
208 with error. */
209 if (action == ENTER && htab->filled == htab->size)
211 __set_errno (ENOMEM);
212 *retval = NULL;
213 return 0;
216 htab->table[idx].used = hval;
217 htab->table[idx].entry = item;
219 ++htab->filled;
221 *retval = &htab->table[idx].entry;
222 return 1;
225 __set_errno (ESRCH);
226 *retval = NULL;
227 return 0;
229 libc_hidden_def (hsearch_r)