hsearch_r.c 6.4 KB

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