hsearch_r.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _GNU_SOURCE
  17. #define _GNU_SOURCE
  18. #endif
  19. #include <errno.h>
  20. #include <malloc.h>
  21. #include <string.h>
  22. #include <search.h>
  23. libc_hidden_proto(hcreate_r)
  24. libc_hidden_proto(hdestroy_r)
  25. libc_hidden_proto(hsearch_r)
  26. /* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
  27. [Knuth] The Art of Computer Programming, part 3 (6.4) */
  28. /* The reentrant version has no static variables to maintain the state.
  29. Instead the interface of all functions is extended to take an argument
  30. which describes the current status. */
  31. typedef struct _ENTRY
  32. {
  33. unsigned int used;
  34. ENTRY entry;
  35. }
  36. _ENTRY;
  37. #ifdef L_hcreate_r
  38. /* For the used double hash method the table size has to be a prime. To
  39. correct the user given table size we need a prime test. This trivial
  40. algorithm is adequate because
  41. a) the code is (most probably) called a few times per program run and
  42. b) the number is small because the table must fit in the core */
  43. static int isprime (unsigned int number)
  44. {
  45. /* no even number will be passed */
  46. unsigned int div = 3;
  47. while (div * div < number && number % div != 0)
  48. div += 2;
  49. return number % div != 0;
  50. }
  51. /* Before using the hash table we must allocate memory for it.
  52. Test for an existing table are done. We allocate one element
  53. more as the found prime number says. This is done for more effective
  54. indexing as explained in the comment for the hsearch function.
  55. The contents of the table is zeroed, especially the field used
  56. becomes zero. */
  57. int hcreate_r (size_t nel, struct hsearch_data *htab)
  58. {
  59. /* Test for correct arguments. */
  60. if (htab == NULL)
  61. {
  62. __set_errno (EINVAL);
  63. return 0;
  64. }
  65. /* There is still another table active. Return with error. */
  66. if (htab->table != NULL)
  67. return 0;
  68. /* Change nel to the first prime number not smaller as nel. */
  69. nel |= 1; /* make odd */
  70. while (!isprime (nel))
  71. nel += 2;
  72. htab->size = nel;
  73. htab->filled = 0;
  74. /* allocate memory and zero out */
  75. htab->table = (_ENTRY *) calloc (htab->size + 1, sizeof (_ENTRY));
  76. if (htab->table == NULL)
  77. return 0;
  78. /* everything went alright */
  79. return 1;
  80. }
  81. libc_hidden_def(hcreate_r)
  82. #endif
  83. #ifdef L_hdestroy_r
  84. /* After using the hash table it has to be destroyed. The used memory can
  85. be freed and the local static variable can be marked as not used. */
  86. void hdestroy_r (struct hsearch_data *htab)
  87. {
  88. /* Test for correct arguments. */
  89. if (htab == NULL)
  90. {
  91. __set_errno (EINVAL);
  92. return;
  93. }
  94. if (htab->table != NULL)
  95. /* free used memory */
  96. free (htab->table);
  97. /* the sign for an existing table is an value != NULL in htable */
  98. htab->table = NULL;
  99. }
  100. libc_hidden_def(hdestroy_r)
  101. #endif
  102. #ifdef L_hsearch_r
  103. /* This is the search function. It uses double hashing with open addressing.
  104. The argument item.key has to be a pointer to an zero terminated, most
  105. probably strings of chars. The function for generating a number of the
  106. strings is simple but fast. It can be replaced by a more complex function
  107. like ajw (see [Aho,Sethi,Ullman]) if the needs are shown.
  108. We use an trick to speed up the lookup. The table is created by hcreate
  109. with one more element available. This enables us to use the index zero
  110. special. This index will never be used because we store the first hash
  111. index in the field used where zero means not used. Every other value
  112. means used. The used field can be used as a first fast comparison for
  113. equality of the stored and the parameter value. This helps to prevent
  114. unnecessary expensive calls of strcmp. */
  115. libc_hidden_proto(strcmp)
  116. libc_hidden_proto(strlen)
  117. int hsearch_r (ENTRY item, ACTION action, ENTRY **retval,
  118. struct hsearch_data *htab)
  119. {
  120. unsigned int hval;
  121. unsigned int count;
  122. unsigned int len = strlen (item.key);
  123. unsigned int idx;
  124. /* Compute an value for the given string. Perhaps use a better method. */
  125. hval = len;
  126. count = len;
  127. while (count-- > 0)
  128. {
  129. hval <<= 4;
  130. hval += item.key[count];
  131. }
  132. /* First hash function: simply take the modul but prevent zero. */
  133. hval %= htab->size;
  134. if (hval == 0)
  135. ++hval;
  136. /* The first index tried. */
  137. idx = hval;
  138. if (htab->table[idx].used)
  139. {
  140. /* Further action might be required according to the action value. */
  141. unsigned hval2;
  142. if (htab->table[idx].used == hval
  143. && strcmp (item.key, htab->table[idx].entry.key) == 0)
  144. {
  145. *retval = &htab->table[idx].entry;
  146. return 1;
  147. }
  148. /* Second hash function, as suggested in [Knuth] */
  149. hval2 = 1 + hval % (htab->size - 2);
  150. do
  151. {
  152. /* Because SIZE is prime this guarantees to step through all
  153. available indeces. */
  154. if (idx <= hval2)
  155. idx = htab->size + idx - hval2;
  156. else
  157. idx -= hval2;
  158. /* If we visited all entries leave the loop unsuccessfully. */
  159. if (idx == hval)
  160. break;
  161. /* If entry is found use it. */
  162. if (htab->table[idx].used == hval
  163. && strcmp (item.key, htab->table[idx].entry.key) == 0)
  164. {
  165. *retval = &htab->table[idx].entry;
  166. return 1;
  167. }
  168. }
  169. while (htab->table[idx].used);
  170. }
  171. /* An empty bucket has been found. */
  172. if (action == ENTER)
  173. {
  174. /* If table is full and another entry should be entered return
  175. with error. */
  176. if (action == ENTER && htab->filled == htab->size)
  177. {
  178. __set_errno (ENOMEM);
  179. *retval = NULL;
  180. return 0;
  181. }
  182. htab->table[idx].used = hval;
  183. htab->table[idx].entry = item;
  184. ++htab->filled;
  185. *retval = &htab->table[idx].entry;
  186. return 1;
  187. }
  188. __set_errno (ESRCH);
  189. *retval = NULL;
  190. return 0;
  191. }
  192. libc_hidden_def(hsearch_r)
  193. #endif