OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / misc / search / hsearch_r.c
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.
4
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.
9
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.
14
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.  */
19
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE
22 #endif
23
24 #include <errno.h>
25 #include <malloc.h>
26 #include <string.h>
27
28 #include <search.h>
29
30 /* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
31    [Knuth]            The Art of Computer Programming, part 3 (6.4)  */
32
33
34 /* The reentrant version has no static variables to maintain the state.
35    Instead the interface of all functions is extended to take an argument
36    which describes the current status.  */
37 typedef struct _ENTRY
38 {
39   unsigned int used;
40   ENTRY entry;
41 }
42 _ENTRY;
43
44
45 #ifdef L_hcreate_r
46
47 /* For the used double hash method the table size has to be a prime. To
48    correct the user given table size we need a prime test.  This trivial
49    algorithm is adequate because
50    a)  the code is (most probably) called a few times per program run and
51    b)  the number is small because the table must fit in the core  */
52 static int isprime (unsigned int number)
53 {
54   /* no even number will be passed */
55   unsigned int div = 3;
56
57   while (div * div < number && number % div != 0)
58     div += 2;
59
60   return number % div != 0;
61 }
62
63
64 /* Before using the hash table we must allocate memory for it.
65    Test for an existing table are done. We allocate one element
66    more as the found prime number says. This is done for more effective
67    indexing as explained in the comment for the hsearch function.
68    The contents of the table is zeroed, especially the field used
69    becomes zero.  */
70 int hcreate_r (size_t nel, struct hsearch_data *htab)
71 {
72   /* Test for correct arguments.  */
73   if (htab == NULL)
74     {
75       __set_errno (EINVAL);
76       return 0;
77     }
78
79   /* There is still another table active. Return with error. */
80   if (htab->table != NULL)
81     return 0;
82
83   /* Change nel to the first prime number not smaller as nel. */
84   nel |= 1;      /* make odd */
85   while (!isprime (nel))
86     nel += 2;
87
88   htab->size = nel;
89   htab->filled = 0;
90
91   /* allocate memory and zero out */
92   htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY));
93   if (htab->table == NULL)
94     return 0;
95
96   /* everything went alright */
97   return 1;
98 }
99 libc_hidden_proto(hcreate_r)
100 libc_hidden_def(hcreate_r)
101 #endif
102
103 #ifdef L_hdestroy_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 hdestroy_r (struct hsearch_data *htab)
107 {
108   /* Test for correct arguments.  */
109   if (htab == NULL)
110     {
111       __set_errno (EINVAL);
112       return;
113     }
114
115   if (htab->table != NULL)
116     /* free used memory */
117     free (htab->table);
118
119   /* the sign for an existing table is an value != NULL in htable */
120   htab->table = NULL;
121 }
122 libc_hidden_proto(hdestroy_r)
123 libc_hidden_def(hdestroy_r)
124 #endif
125
126 #ifdef L_hsearch_r
127 /* This is the search function. It uses double hashing with open addressing.
128    The argument item.key has to be a pointer to an zero terminated, most
129    probably strings of chars. The function for generating a number of the
130    strings is simple but fast. It can be replaced by a more complex function
131    like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
132
133    We use an trick to speed up the lookup. The table is created by hcreate
134    with one more element available. This enables us to use the index zero
135    special. This index will never be used because we store the first hash
136    index in the field used where zero means not used. Every other value
137    means used. The used field can be used as a first fast comparison for
138    equality of the stored and the parameter value. This helps to prevent
139    unnecessary expensive calls of strcmp.  */
140
141 libc_hidden_proto(strcmp)
142 libc_hidden_proto(strlen)
143
144 int hsearch_r (ENTRY item, ACTION action, ENTRY **retval,
145                struct hsearch_data *htab)
146 {
147   unsigned int hval;
148   unsigned int count;
149   unsigned int len = strlen (item.key);
150   unsigned int idx;
151
152   /* Compute an value for the given string. Perhaps use a better method. */
153   hval = len;
154   count = len;
155   while (count-- > 0)
156     {
157       hval <<= 4;
158       hval += item.key[count];
159     }
160
161   /* First hash function: simply take the modul but prevent zero. */
162   hval %= htab->size;
163   if (hval == 0)
164     ++hval;
165
166   /* The first index tried. */
167   idx = hval;
168
169   if (htab->table[idx].used)
170     {
171       /* Further action might be required according to the action value. */
172       unsigned hval2;
173
174       if (htab->table[idx].used == hval
175           && strcmp (item.key, htab->table[idx].entry.key) == 0)
176         {
177           *retval = &htab->table[idx].entry;
178           return 1;
179         }
180
181       /* Second hash function, as suggested in [Knuth] */
182       hval2 = 1 + hval % (htab->size - 2);
183
184       do
185         {
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;
192
193           /* If we visited all entries leave the loop unsuccessfully.  */
194           if (idx == hval)
195             break;
196
197             /* If entry is found use it. */
198           if (htab->table[idx].used == hval
199               && strcmp (item.key, htab->table[idx].entry.key) == 0)
200             {
201               *retval = &htab->table[idx].entry;
202               return 1;
203             }
204         }
205       while (htab->table[idx].used);
206     }
207
208   /* An empty bucket has been found. */
209   if (action == ENTER)
210     {
211       /* If table is full and another entry should be entered return
212          with error.  */
213       if (action == ENTER && htab->filled == htab->size)
214         {
215           __set_errno (ENOMEM);
216           *retval = NULL;
217           return 0;
218         }
219
220       htab->table[idx].used  = hval;
221       htab->table[idx].entry = item;
222
223       ++htab->filled;
224
225       *retval = &htab->table[idx].entry;
226       return 1;
227     }
228
229   __set_errno (ESRCH);
230   *retval = NULL;
231   return 0;
232 }
233 libc_hidden_proto(hsearch_r)
234 libc_hidden_def(hsearch_r)
235 #endif