_hsearch_r.c 6.3 KB

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