Add more tests for ceil, floor, round, trunc.
[glibc.git] / misc / hsearch_r.c
blob9d6cd81c95e2559ac73b03c7708271c122d98bf0
1 /* Copyright (C) 1993-2015 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <malloc.h>
21 #include <string.h>
22 #include <stdint.h>
23 #include <search.h>
25 /* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
26 [Knuth] The Art of Computer Programming, part 3 (6.4) */
29 /* The reentrant version has no static variables to maintain the state.
30 Instead the interface of all functions is extended to take an argument
31 which describes the current status. */
32 typedef struct _ENTRY
34 unsigned int used;
35 ENTRY entry;
37 _ENTRY;
40 /* For the used double hash method the table size has to be a prime. To
41 correct the user given table size we need a prime test. This trivial
42 algorithm is adequate because
43 a) the code is (most probably) called a few times per program run and
44 b) the number is small because the table must fit in the core */
45 static int
46 isprime (unsigned int number)
48 /* no even number will be passed */
49 unsigned int div = 3;
51 while (div * div < number && number % div != 0)
52 div += 2;
54 return number % div != 0;
58 /* Before using the hash table we must allocate memory for it.
59 Test for an existing table are done. We allocate one element
60 more as the found prime number says. This is done for more effective
61 indexing as explained in the comment for the hsearch function.
62 The contents of the table is zeroed, especially the field used
63 becomes zero. */
64 int
65 __hcreate_r (size_t nel, struct hsearch_data *htab)
67 /* Test for correct arguments. */
68 if (htab == NULL)
70 __set_errno (EINVAL);
71 return 0;
74 if (nel >= SIZE_MAX / sizeof (_ENTRY))
76 __set_errno (ENOMEM);
77 return 0;
81 /* There is still another table active. Return with error. */
82 if (htab->table != NULL)
83 return 0;
85 /* We need a size of at least 3. Otherwise the hash functions we
86 use will not work. */
87 if (nel < 3)
88 nel = 3;
89 /* Change nel to the first prime number not smaller as nel. */
90 nel |= 1; /* make odd */
91 while (!isprime (nel))
92 nel += 2;
94 htab->size = nel;
95 htab->filled = 0;
97 /* allocate memory and zero out */
98 htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY));
99 if (htab->table == NULL)
100 return 0;
102 /* everything went alright */
103 return 1;
105 libc_hidden_def (__hcreate_r)
106 weak_alias (__hcreate_r, hcreate_r)
109 /* After using the hash table it has to be destroyed. The used memory can
110 be freed and the local static variable can be marked as not used. */
111 void
112 __hdestroy_r (struct hsearch_data *htab)
114 /* Test for correct arguments. */
115 if (htab == NULL)
117 __set_errno (EINVAL);
118 return;
121 /* Free used memory. */
122 free (htab->table);
124 /* the sign for an existing table is an value != NULL in htable */
125 htab->table = NULL;
127 libc_hidden_def (__hdestroy_r)
128 weak_alias (__hdestroy_r, hdestroy_r)
131 /* This is the search function. It uses double hashing with open addressing.
132 The argument item.key has to be a pointer to an zero terminated, most
133 probably strings of chars. The function for generating a number of the
134 strings is simple but fast. It can be replaced by a more complex function
135 like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
137 We use an trick to speed up the lookup. The table is created by hcreate
138 with one more element available. This enables us to use the index zero
139 special. This index will never be used because we store the first hash
140 index in the field used where zero means not used. Every other value
141 means used. The used field can be used as a first fast comparison for
142 equality of the stored and the parameter value. This helps to prevent
143 unnecessary expensive calls of strcmp. */
145 __hsearch_r (item, action, retval, htab)
146 ENTRY item;
147 ACTION action;
148 ENTRY **retval;
149 struct hsearch_data *htab;
151 unsigned int hval;
152 unsigned int count;
153 unsigned int len = strlen (item.key);
154 unsigned int idx;
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];
164 if (hval == 0)
165 ++hval;
167 /* First hash function: simply take the modul but prevent zero. */
168 idx = hval % htab->size + 1;
170 if (htab->table[idx].used)
172 /* Further action might be required according to the action value. */
173 if (htab->table[idx].used == hval
174 && strcmp (item.key, htab->table[idx].entry.key) == 0)
176 *retval = &htab->table[idx].entry;
177 return 1;
180 /* Second hash function, as suggested in [Knuth] */
181 unsigned int hval2 = 1 + hval % (htab->size - 2);
182 unsigned int first_idx = idx;
186 /* Because SIZE is prime this guarantees to step through all
187 available indeces. */
188 if (idx <= hval2)
189 idx = htab->size + idx - hval2;
190 else
191 idx -= hval2;
193 /* If we visited all entries leave the loop unsuccessfully. */
194 if (idx == first_idx)
195 break;
197 /* If entry is found use it. */
198 if (htab->table[idx].used == hval
199 && strcmp (item.key, htab->table[idx].entry.key) == 0)
201 *retval = &htab->table[idx].entry;
202 return 1;
205 while (htab->table[idx].used);
208 /* An empty bucket has been found. */
209 if (action == ENTER)
211 /* If table is full and another entry should be entered return
212 with error. */
213 if (htab->filled == htab->size)
215 __set_errno (ENOMEM);
216 *retval = NULL;
217 return 0;
220 htab->table[idx].used = hval;
221 htab->table[idx].entry = item;
223 ++htab->filled;
225 *retval = &htab->table[idx].entry;
226 return 1;
229 __set_errno (ESRCH);
230 *retval = NULL;
231 return 0;
233 libc_hidden_def (__hsearch_r)
234 weak_alias (__hsearch_r, hsearch_r)