1 /* Copyright (C) 1993-2014 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/>. */
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. */
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 */
46 isprime (unsigned int number
)
48 /* no even number will be passed */
51 while (div
* div
< number
&& number
% div
!= 0)
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
67 struct hsearch_data
*htab
;
69 /* Test for correct arguments. */
76 /* There is still another table active. Return with error. */
77 if (htab
->table
!= NULL
)
80 /* We need a size of at least 3. Otherwise the hash functions we
84 /* Change nel to the first prime number not smaller as nel. */
85 nel
|= 1; /* make odd */
86 while (!isprime (nel
))
92 /* allocate memory and zero out */
93 htab
->table
= (_ENTRY
*) calloc (htab
->size
+ 1, sizeof (_ENTRY
));
94 if (htab
->table
== NULL
)
97 /* everything went alright */
100 libc_hidden_def (hcreate_r
)
103 /* After using the hash table it has to be destroyed. The used memory can
104 be freed and the local static variable can be marked as not used. */
107 struct hsearch_data
*htab
;
109 /* Test for correct arguments. */
112 __set_errno (EINVAL
);
116 /* Free used memory. */
119 /* the sign for an existing table is an value != NULL in htable */
122 libc_hidden_def (hdestroy_r
)
125 /* This is the search function. It uses double hashing with open addressing.
126 The argument item.key has to be a pointer to an zero terminated, most
127 probably strings of chars. The function for generating a number of the
128 strings is simple but fast. It can be replaced by a more complex function
129 like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
131 We use an trick to speed up the lookup. The table is created by hcreate
132 with one more element available. This enables us to use the index zero
133 special. This index will never be used because we store the first hash
134 index in the field used where zero means not used. Every other value
135 means used. The used field can be used as a first fast comparison for
136 equality of the stored and the parameter value. This helps to prevent
137 unnecessary expensive calls of strcmp. */
139 hsearch_r (item
, action
, retval
, htab
)
143 struct hsearch_data
*htab
;
147 unsigned int len
= strlen (item
.key
);
150 /* Compute an value for the given string. Perhaps use a better method. */
156 hval
+= item
.key
[count
];
161 /* First hash function: simply take the modul but prevent zero. */
162 idx
= hval
% htab
->size
+ 1;
164 if (htab
->table
[idx
].used
)
166 /* Further action might be required according to the action value. */
167 if (htab
->table
[idx
].used
== hval
168 && strcmp (item
.key
, htab
->table
[idx
].entry
.key
) == 0)
170 *retval
= &htab
->table
[idx
].entry
;
174 /* Second hash function, as suggested in [Knuth] */
175 unsigned int hval2
= 1 + hval
% (htab
->size
- 2);
176 unsigned int first_idx
= idx
;
180 /* Because SIZE is prime this guarantees to step through all
181 available indeces. */
183 idx
= htab
->size
+ idx
- hval2
;
187 /* If we visited all entries leave the loop unsuccessfully. */
188 if (idx
== first_idx
)
191 /* If entry is found use it. */
192 if (htab
->table
[idx
].used
== hval
193 && strcmp (item
.key
, htab
->table
[idx
].entry
.key
) == 0)
195 *retval
= &htab
->table
[idx
].entry
;
199 while (htab
->table
[idx
].used
);
202 /* An empty bucket has been found. */
205 /* If table is full and another entry should be entered return
207 if (htab
->filled
== htab
->size
)
209 __set_errno (ENOMEM
);
214 htab
->table
[idx
].used
= hval
;
215 htab
->table
[idx
].entry
= item
;
219 *retval
= &htab
->table
[idx
].entry
;
227 libc_hidden_def (hsearch_r
)