2.9
[glibc/nacl-glibc.git] / misc / hsearch_r.c
blobc855a418464425d36eb04eb952a51dd7b8ba4237
1 /* Copyright (C) 1993,1995-1997,2002,2005,2007,2008
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <malloc.h>
23 #include <string.h>
25 #include <search.h>
27 /* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
28 [Knuth] The Art of Computer Programming, part 3 (6.4) */
31 /* The reentrant version has no static variables to maintain the state.
32 Instead the interface of all functions is extended to take an argument
33 which describes the current status. */
34 typedef struct _ENTRY
36 unsigned int used;
37 ENTRY entry;
39 _ENTRY;
42 /* For the used double hash method the table size has to be a prime. To
43 correct the user given table size we need a prime test. This trivial
44 algorithm is adequate because
45 a) the code is (most probably) called a few times per program run and
46 b) the number is small because the table must fit in the core */
47 static int
48 isprime (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 size_t nel;
69 struct hsearch_data *htab;
71 /* Test for correct arguments. */
72 if (htab == NULL)
74 __set_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;
98 libc_hidden_def (hcreate_r)
101 /* After using the hash table it has to be destroyed. The used memory can
102 be freed and the local static variable can be marked as not used. */
103 void
104 hdestroy_r (htab)
105 struct hsearch_data *htab;
107 /* Test for correct arguments. */
108 if (htab == NULL)
110 __set_errno (EINVAL);
111 return;
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 idx = hval % htab->size + 1;
160 if (htab->table[idx].used)
162 /* Further action might be required according to the action value. */
163 if (htab->table[idx].used == hval
164 && strcmp (item.key, htab->table[idx].entry.key) == 0)
166 *retval = &htab->table[idx].entry;
167 return 1;
170 /* Second hash function, as suggested in [Knuth] */
171 unsigned int hval2 = 1 + hval % (htab->size - 2);
172 unsigned int first_idx = idx;
176 /* Because SIZE is prime this guarantees to step through all
177 available indeces. */
178 if (idx <= hval2)
179 idx = htab->size + idx - hval2;
180 else
181 idx -= hval2;
183 /* If we visited all entries leave the loop unsuccessfully. */
184 if (idx == first_idx)
185 break;
187 /* If entry is found use it. */
188 if (htab->table[idx].used == hval
189 && strcmp (item.key, htab->table[idx].entry.key) == 0)
191 *retval = &htab->table[idx].entry;
192 return 1;
195 while (htab->table[idx].used);
198 /* An empty bucket has been found. */
199 if (action == ENTER)
201 /* If table is full and another entry should be entered return
202 with error. */
203 if (htab->filled == htab->size)
205 __set_errno (ENOMEM);
206 *retval = NULL;
207 return 0;
210 htab->table[idx].used = hval;
211 htab->table[idx].entry = item;
213 ++htab->filled;
215 *retval = &htab->table[idx].entry;
216 return 1;
219 __set_errno (ESRCH);
220 *retval = NULL;
221 return 0;
223 libc_hidden_def (hsearch_r)