hash.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Copyright (C) 1995,1996,1997, 2000, 2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This 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. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/chars.h"
  22. #include "libguile/ports.h"
  23. #include "libguile/strings.h"
  24. #include "libguile/symbols.h"
  25. #include "libguile/vectors.h"
  26. #include "libguile/validate.h"
  27. #include "libguile/hash.h"
  28. #ifndef floor
  29. extern double floor();
  30. #endif
  31. unsigned long
  32. scm_string_hash (const unsigned char *str, size_t len)
  33. {
  34. /* from suggestion at: */
  35. /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
  36. unsigned long h = 0;
  37. while (len-- > 0)
  38. h = *str++ + h*37;
  39. return h;
  40. }
  41. /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
  42. /* Dirk:FIXME:: scm_hasher could be made static. */
  43. unsigned long
  44. scm_hasher(SCM obj, unsigned long n, size_t d)
  45. {
  46. switch (SCM_ITAG3 (obj)) {
  47. case scm_tc3_int_1:
  48. case scm_tc3_int_2:
  49. return SCM_I_INUM(obj) % n; /* SCM_INUMP(obj) */
  50. case scm_tc3_imm24:
  51. if (SCM_CHARP(obj))
  52. return (unsigned)(scm_c_downcase(SCM_CHAR(obj))) % n;
  53. switch (SCM_UNPACK (obj)) {
  54. #ifndef SICP
  55. case SCM_UNPACK(SCM_EOL):
  56. d = 256;
  57. break;
  58. #endif
  59. case SCM_UNPACK(SCM_BOOL_T):
  60. d = 257;
  61. break;
  62. case SCM_UNPACK(SCM_BOOL_F):
  63. d = 258;
  64. break;
  65. case SCM_UNPACK(SCM_EOF_VAL):
  66. d = 259;
  67. break;
  68. default:
  69. d = 263; /* perhaps should be error */
  70. }
  71. return d % n;
  72. default:
  73. return 263 % n; /* perhaps should be error */
  74. case scm_tc3_cons:
  75. switch SCM_TYP7(obj) {
  76. default:
  77. return 263 % n;
  78. case scm_tc7_smob:
  79. return 263 % n;
  80. case scm_tc7_number:
  81. switch SCM_TYP16 (obj) {
  82. case scm_tc16_big:
  83. return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
  84. case scm_tc16_real:
  85. {
  86. double r = SCM_REAL_VALUE (obj);
  87. if (floor (r) == r)
  88. {
  89. obj = scm_inexact_to_exact (obj);
  90. return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
  91. }
  92. }
  93. /* Fall through */
  94. case scm_tc16_complex:
  95. case scm_tc16_fraction:
  96. obj = scm_number_to_string (obj, scm_from_int (10));
  97. /* Fall through */
  98. }
  99. /* Fall through */
  100. case scm_tc7_string:
  101. {
  102. unsigned long hash =
  103. scm_string_hash ((const unsigned char *) scm_i_string_chars (obj),
  104. scm_i_string_length (obj)) % n;
  105. scm_remember_upto_here_1 (obj);
  106. return hash;
  107. }
  108. case scm_tc7_symbol:
  109. return scm_i_symbol_hash (obj) % n;
  110. case scm_tc7_wvect:
  111. case scm_tc7_vector:
  112. {
  113. size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj);
  114. if (len > 5)
  115. {
  116. size_t i = d/2;
  117. unsigned long h = 1;
  118. while (i--)
  119. {
  120. SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
  121. h = ((h << 8) + (scm_hasher (elt, n, 2))) % n;
  122. }
  123. return h;
  124. }
  125. else
  126. {
  127. size_t i = len;
  128. unsigned long h = (n)-1;
  129. while (i--)
  130. {
  131. SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
  132. h = ((h << 8) + (scm_hasher (elt, n, d/len))) % n;
  133. }
  134. return h;
  135. }
  136. }
  137. case scm_tcs_cons_imcar:
  138. case scm_tcs_cons_nimcar:
  139. if (d) return (scm_hasher (SCM_CAR (obj), n, d/2)
  140. + scm_hasher (SCM_CDR (obj), n, d/2)) % n;
  141. else return 1;
  142. case scm_tc7_port:
  143. return ((SCM_RDNG & SCM_CELL_WORD_0 (obj)) ? 260 : 261) % n;
  144. case scm_tcs_closures:
  145. case scm_tcs_subrs:
  146. return 262 % n;
  147. }
  148. }
  149. }
  150. unsigned long
  151. scm_ihashq (SCM obj, unsigned long n)
  152. {
  153. return (SCM_UNPACK (obj) >> 1) % n;
  154. }
  155. SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
  156. (SCM key, SCM size),
  157. "Determine a hash value for @var{key} that is suitable for\n"
  158. "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
  159. "used as the equality predicate. The function returns an\n"
  160. "integer in the range 0 to @var{size} - 1. Note that\n"
  161. "@code{hashq} may use internal addresses. Thus two calls to\n"
  162. "hashq where the keys are @code{eq?} are not guaranteed to\n"
  163. "deliver the same value if the key object gets garbage collected\n"
  164. "in between. This can happen, for example with symbols:\n"
  165. "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
  166. "different values, since @code{foo} will be garbage collected.")
  167. #define FUNC_NAME s_scm_hashq
  168. {
  169. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  170. return scm_from_ulong (scm_ihashq (key, sz));
  171. }
  172. #undef FUNC_NAME
  173. unsigned long
  174. scm_ihashv (SCM obj, unsigned long n)
  175. {
  176. if (SCM_CHARP(obj))
  177. return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */
  178. if (SCM_NUMP(obj))
  179. return (unsigned long) scm_hasher(obj, n, 10);
  180. else
  181. return SCM_UNPACK (obj) % n;
  182. }
  183. SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
  184. (SCM key, SCM size),
  185. "Determine a hash value for @var{key} that is suitable for\n"
  186. "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
  187. "used as the equality predicate. The function returns an\n"
  188. "integer in the range 0 to @var{size} - 1. Note that\n"
  189. "@code{(hashv key)} may use internal addresses. Thus two calls\n"
  190. "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
  191. "deliver the same value if the key object gets garbage collected\n"
  192. "in between. This can happen, for example with symbols:\n"
  193. "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
  194. "different values, since @code{foo} will be garbage collected.")
  195. #define FUNC_NAME s_scm_hashv
  196. {
  197. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  198. return scm_from_ulong (scm_ihashv (key, sz));
  199. }
  200. #undef FUNC_NAME
  201. unsigned long
  202. scm_ihash (SCM obj, unsigned long n)
  203. {
  204. return (unsigned long) scm_hasher (obj, n, 10);
  205. }
  206. SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
  207. (SCM key, SCM size),
  208. "Determine a hash value for @var{key} that is suitable for\n"
  209. "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
  210. "is used as the equality predicate. The function returns an\n"
  211. "integer in the range 0 to @var{size} - 1.")
  212. #define FUNC_NAME s_scm_hash
  213. {
  214. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  215. return scm_from_ulong (scm_ihash (key, sz));
  216. }
  217. #undef FUNC_NAME
  218. void
  219. scm_init_hash ()
  220. {
  221. #include "libguile/hash.x"
  222. }
  223. /*
  224. Local Variables:
  225. c-file-style: "gnu"
  226. End:
  227. */