_hsearch_r.c 6.4 KB

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