Replace FSF snail mail address with URLs.
[glibc.git] / misc / hsearch_r.c
blob9aeddac84da071e38141a5b15fa480ef91a847c2
1 /* Copyright (C) 1993,1995-1997,2002,2005,2007,2008,2009
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1993.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
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 /* We need a size of at least 3. Otherwise the hash functions we
82 use will not work. */
83 if (nel < 3)
84 nel = 3;
85 /* Change nel to the first prime number not smaller as nel. */
86 nel |= 1; /* make odd */
87 while (!isprime (nel))
88 nel += 2;
90 htab->size = nel;
91 htab->filled = 0;
93 /* allocate memory and zero out */
94 htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY));
95 if (htab->table == NULL)
96 return 0;
98 /* everything went alright */
99 return 1;
101 libc_hidden_def (hcreate_r)
104 /* After using the hash table it has to be destroyed. The used memory can
105 be freed and the local static variable can be marked as not used. */
106 void
107 hdestroy_r (htab)
108 struct hsearch_data *htab;
110 /* Test for correct arguments. */
111 if (htab == NULL)
113 __set_errno (EINVAL);
114 return;
117 /* Free used memory. */
118 free (htab->table);
120 /* the sign for an existing table is an value != NULL in htable */
121 htab->table = NULL;
123 libc_hidden_def (hdestroy_r)
126 /* This is the search function. It uses double hashing with open addressing.
127 The argument item.key has to be a pointer to an zero terminated, most
128 probably strings of chars. The function for generating a number of the
129 strings is simple but fast. It can be replaced by a more complex function
130 like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
132 We use an trick to speed up the lookup. The table is created by hcreate
133 with one more element available. This enables us to use the index zero
134 special. This index will never be used because we store the first hash
135 index in the field used where zero means not used. Every other value
136 means used. The used field can be used as a first fast comparison for
137 equality of the stored and the parameter value. This helps to prevent
138 unnecessary expensive calls of strcmp. */
140 hsearch_r (item, action, retval, htab)
141 ENTRY item;
142 ACTION action;
143 ENTRY **retval;
144 struct hsearch_data *htab;
146 unsigned int hval;
147 unsigned int count;
148 unsigned int len = strlen (item.key);
149 unsigned int idx;
151 /* Compute an value for the given string. Perhaps use a better method. */
152 hval = len;
153 count = len;
154 while (count-- > 0)
156 hval <<= 4;
157 hval += item.key[count];
159 if (hval == 0)
160 ++hval;
162 /* First hash function: simply take the modul but prevent zero. */
163 idx = hval % htab->size + 1;
165 if (htab->table[idx].used)
167 /* Further action might be required according to the action value. */
168 if (htab->table[idx].used == hval
169 && strcmp (item.key, htab->table[idx].entry.key) == 0)
171 *retval = &htab->table[idx].entry;
172 return 1;
175 /* Second hash function, as suggested in [Knuth] */
176 unsigned int hval2 = 1 + hval % (htab->size - 2);
177 unsigned int first_idx = idx;
181 /* Because SIZE is prime this guarantees to step through all
182 available indeces. */
183 if (idx <= hval2)
184 idx = htab->size + idx - hval2;
185 else
186 idx -= hval2;
188 /* If we visited all entries leave the loop unsuccessfully. */
189 if (idx == first_idx)
190 break;
192 /* If entry is found use it. */
193 if (htab->table[idx].used == hval
194 && strcmp (item.key, htab->table[idx].entry.key) == 0)
196 *retval = &htab->table[idx].entry;
197 return 1;
200 while (htab->table[idx].used);
203 /* An empty bucket has been found. */
204 if (action == ENTER)
206 /* If table is full and another entry should be entered return
207 with error. */
208 if (htab->filled == htab->size)
210 __set_errno (ENOMEM);
211 *retval = NULL;
212 return 0;
215 htab->table[idx].used = hval;
216 htab->table[idx].entry = item;
218 ++htab->filled;
220 *retval = &htab->table[idx].entry;
221 return 1;
224 __set_errno (ESRCH);
225 *retval = NULL;
226 return 0;
228 libc_hidden_def (hsearch_r)