hash.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Copyright (C) 1995,1996,1997, 2000, 2001, 2003, 2004, 2006, 2008, 2009, 2010, 2011 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 License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #ifdef HAVE_WCHAR_H
  22. #include <wchar.h>
  23. #endif
  24. #include <math.h>
  25. #include <unistr.h>
  26. #include "libguile/_scm.h"
  27. #include "libguile/chars.h"
  28. #include "libguile/ports.h"
  29. #include "libguile/strings.h"
  30. #include "libguile/symbols.h"
  31. #include "libguile/vectors.h"
  32. #include "libguile/validate.h"
  33. #include "libguile/hash.h"
  34. #ifndef floor
  35. extern double floor();
  36. #endif
  37. unsigned long
  38. scm_string_hash (const unsigned char *str, size_t len)
  39. {
  40. /* from suggestion at: */
  41. /* http://srfi.schemers.org/srfi-13/mail-archive/msg00112.html */
  42. unsigned long h = 0;
  43. while (len-- > 0)
  44. h = *str++ + h*37;
  45. return h;
  46. }
  47. unsigned long
  48. scm_i_string_hash (SCM str)
  49. {
  50. size_t len = scm_i_string_length (str);
  51. size_t i = 0;
  52. unsigned long h = 0;
  53. while (len-- > 0)
  54. h = (unsigned long) scm_i_string_ref (str, i++) + h * 37;
  55. scm_remember_upto_here_1 (str);
  56. return h;
  57. }
  58. unsigned long
  59. scm_i_locale_string_hash (const char *str, size_t len)
  60. {
  61. #ifdef HAVE_WCHAR_H
  62. mbstate_t state;
  63. wchar_t c;
  64. size_t byte_idx = 0, nbytes;
  65. unsigned long h = 0;
  66. if (len == (size_t) -1)
  67. len = strlen (str);
  68. while ((nbytes = mbrtowc (&c, str + byte_idx, len - byte_idx, &state)) > 0)
  69. {
  70. if (nbytes >= (size_t) -2)
  71. /* Invalid input string; punt. */
  72. return scm_i_string_hash (scm_from_locale_stringn (str, len));
  73. h = (unsigned long) c + h * 37;
  74. byte_idx += nbytes;
  75. }
  76. return h;
  77. #else
  78. return scm_i_string_hash (scm_from_locale_stringn (str, len));
  79. #endif
  80. }
  81. unsigned long
  82. scm_i_latin1_string_hash (const char *str, size_t len)
  83. {
  84. const scm_t_uint8 *ustr = (const scm_t_uint8 *) str;
  85. size_t i = 0;
  86. unsigned long h = 0;
  87. if (len == (size_t) -1)
  88. len = strlen (str);
  89. for (; i < len; i++)
  90. h = (unsigned long) ustr[i] + h * 37;
  91. return h;
  92. }
  93. unsigned long
  94. scm_i_utf8_string_hash (const char *str, size_t len)
  95. {
  96. const scm_t_uint8 *ustr = (const scm_t_uint8 *) str;
  97. size_t byte_idx = 0;
  98. unsigned long h = 0;
  99. if (len == (size_t) -1)
  100. len = strlen (str);
  101. while (byte_idx < len)
  102. {
  103. ucs4_t c;
  104. int nbytes;
  105. nbytes = u8_mbtouc (&c, ustr + byte_idx, len - byte_idx);
  106. if (nbytes == 0)
  107. break;
  108. else if (nbytes < 0)
  109. /* Bad UTF-8; punt. */
  110. return scm_i_string_hash (scm_from_utf8_stringn (str, len));
  111. h = (unsigned long) c + h * 37;
  112. byte_idx += nbytes;
  113. }
  114. return h;
  115. }
  116. /* Dirk:FIXME:: why downcase for characters? (2x: scm_hasher, scm_ihashv) */
  117. /* Dirk:FIXME:: scm_hasher could be made static. */
  118. unsigned long
  119. scm_hasher(SCM obj, unsigned long n, size_t d)
  120. {
  121. switch (SCM_ITAG3 (obj)) {
  122. case scm_tc3_int_1:
  123. case scm_tc3_int_2:
  124. return SCM_I_INUM(obj) % n; /* SCM_INUMP(obj) */
  125. case scm_tc3_imm24:
  126. if (SCM_CHARP(obj))
  127. return (unsigned)(scm_c_downcase(SCM_CHAR(obj))) % n;
  128. switch (SCM_UNPACK (obj)) {
  129. case SCM_EOL_BITS:
  130. d = 256;
  131. break;
  132. case SCM_BOOL_T_BITS:
  133. d = 257;
  134. break;
  135. case SCM_BOOL_F_BITS:
  136. d = 258;
  137. break;
  138. case SCM_EOF_VAL_BITS:
  139. d = 259;
  140. break;
  141. default:
  142. d = 263; /* perhaps should be error */
  143. }
  144. return d % n;
  145. default:
  146. return 263 % n; /* perhaps should be error */
  147. case scm_tc3_cons:
  148. switch SCM_TYP7(obj) {
  149. default:
  150. return 263 % n;
  151. case scm_tc7_smob:
  152. return 263 % n;
  153. case scm_tc7_number:
  154. switch SCM_TYP16 (obj) {
  155. case scm_tc16_big:
  156. return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
  157. case scm_tc16_real:
  158. {
  159. double r = SCM_REAL_VALUE (obj);
  160. if (floor (r) == r && !isinf (r) && !isnan (r))
  161. {
  162. obj = scm_inexact_to_exact (obj);
  163. return scm_to_ulong (scm_modulo (obj, scm_from_ulong (n)));
  164. }
  165. }
  166. /* Fall through */
  167. case scm_tc16_complex:
  168. case scm_tc16_fraction:
  169. obj = scm_number_to_string (obj, scm_from_int (10));
  170. /* Fall through */
  171. }
  172. /* Fall through */
  173. case scm_tc7_string:
  174. {
  175. unsigned long hash =
  176. scm_i_string_hash (obj) % n;
  177. return hash;
  178. }
  179. case scm_tc7_symbol:
  180. return scm_i_symbol_hash (obj) % n;
  181. case scm_tc7_pointer:
  182. {
  183. /* Pointer objects are typically used to store addresses of heap
  184. objects. On most platforms, these are at least 3-byte
  185. aligned (on x86_64-*-gnu, `malloc' returns 4-byte aligned
  186. addresses), so get rid of the least significant bits. */
  187. scm_t_uintptr significant_bits;
  188. significant_bits = (scm_t_uintptr) SCM_POINTER_VALUE (obj) >> 4UL;
  189. return (size_t) significant_bits % n;
  190. }
  191. case scm_tc7_wvect:
  192. case scm_tc7_vector:
  193. {
  194. size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj);
  195. if (len > 5)
  196. {
  197. size_t i = d/2;
  198. unsigned long h = 1;
  199. while (i--)
  200. {
  201. SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
  202. h = ((h << 8) + (scm_hasher (elt, n, 2))) % n;
  203. }
  204. return h;
  205. }
  206. else
  207. {
  208. size_t i = len;
  209. unsigned long h = (n)-1;
  210. while (i--)
  211. {
  212. SCM elt = SCM_SIMPLE_VECTOR_REF (obj, h % len);
  213. h = ((h << 8) + (scm_hasher (elt, n, d/len))) % n;
  214. }
  215. return h;
  216. }
  217. }
  218. case scm_tcs_cons_imcar:
  219. case scm_tcs_cons_nimcar:
  220. if (d) return (scm_hasher (SCM_CAR (obj), n, d/2)
  221. + scm_hasher (SCM_CDR (obj), n, d/2)) % n;
  222. else return 1;
  223. case scm_tc7_port:
  224. return ((SCM_RDNG & SCM_CELL_WORD_0 (obj)) ? 260 : 261) % n;
  225. case scm_tc7_program:
  226. return 262 % n;
  227. }
  228. }
  229. }
  230. unsigned long
  231. scm_ihashq (SCM obj, unsigned long n)
  232. {
  233. return (SCM_UNPACK (obj) >> 1) % n;
  234. }
  235. SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
  236. (SCM key, SCM size),
  237. "Determine a hash value for @var{key} that is suitable for\n"
  238. "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
  239. "used as the equality predicate. The function returns an\n"
  240. "integer in the range 0 to @var{size} - 1. Note that\n"
  241. "@code{hashq} may use internal addresses. Thus two calls to\n"
  242. "hashq where the keys are @code{eq?} are not guaranteed to\n"
  243. "deliver the same value if the key object gets garbage collected\n"
  244. "in between. This can happen, for example with symbols:\n"
  245. "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
  246. "different values, since @code{foo} will be garbage collected.")
  247. #define FUNC_NAME s_scm_hashq
  248. {
  249. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  250. return scm_from_ulong (scm_ihashq (key, sz));
  251. }
  252. #undef FUNC_NAME
  253. unsigned long
  254. scm_ihashv (SCM obj, unsigned long n)
  255. {
  256. if (SCM_CHARP(obj))
  257. return ((unsigned long) (scm_c_downcase (SCM_CHAR (obj)))) % n; /* downcase!?!! */
  258. if (SCM_NUMP(obj))
  259. return (unsigned long) scm_hasher(obj, n, 10);
  260. else
  261. return SCM_UNPACK (obj) % n;
  262. }
  263. SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
  264. (SCM key, SCM size),
  265. "Determine a hash value for @var{key} that is suitable for\n"
  266. "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
  267. "used as the equality predicate. The function returns an\n"
  268. "integer in the range 0 to @var{size} - 1. Note that\n"
  269. "@code{(hashv key)} may use internal addresses. Thus two calls\n"
  270. "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
  271. "deliver the same value if the key object gets garbage collected\n"
  272. "in between. This can happen, for example with symbols:\n"
  273. "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
  274. "different values, since @code{foo} will be garbage collected.")
  275. #define FUNC_NAME s_scm_hashv
  276. {
  277. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  278. return scm_from_ulong (scm_ihashv (key, sz));
  279. }
  280. #undef FUNC_NAME
  281. unsigned long
  282. scm_ihash (SCM obj, unsigned long n)
  283. {
  284. return (unsigned long) scm_hasher (obj, n, 10);
  285. }
  286. SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
  287. (SCM key, SCM size),
  288. "Determine a hash value for @var{key} that is suitable for\n"
  289. "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
  290. "is used as the equality predicate. The function returns an\n"
  291. "integer in the range 0 to @var{size} - 1.")
  292. #define FUNC_NAME s_scm_hash
  293. {
  294. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  295. return scm_from_ulong (scm_ihash (key, sz));
  296. }
  297. #undef FUNC_NAME
  298. void
  299. scm_init_hash ()
  300. {
  301. #include "libguile/hash.x"
  302. }
  303. /*
  304. Local Variables:
  305. c-file-style: "gnu"
  306. End:
  307. */