_hsearch_r.c 6.2 KB

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