hash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. /* This hash function is originally from
  38. http://burtleburtle.net/bob/c/lookup3.c by Bob Jenkins, May 2006,
  39. Public Domain. No warranty. */
  40. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  41. #define mix(a,b,c) \
  42. { \
  43. a -= c; a ^= rot(c, 4); c += b; \
  44. b -= a; b ^= rot(a, 6); a += c; \
  45. c -= b; c ^= rot(b, 8); b += a; \
  46. a -= c; a ^= rot(c,16); c += b; \
  47. b -= a; b ^= rot(a,19); a += c; \
  48. c -= b; c ^= rot(b, 4); b += a; \
  49. }
  50. #define final(a,b,c) \
  51. { \
  52. c ^= b; c -= rot(b,14); \
  53. a ^= c; a -= rot(c,11); \
  54. b ^= a; b -= rot(a,25); \
  55. c ^= b; c -= rot(b,16); \
  56. a ^= c; a -= rot(c,4); \
  57. b ^= a; b -= rot(a,14); \
  58. c ^= b; c -= rot(b,24); \
  59. }
  60. #define JENKINS_LOOKUP3_HASHWORD2(k, length, ret) \
  61. do { \
  62. scm_t_uint32 a, b, c; \
  63. \
  64. /* Set up the internal state. */ \
  65. a = b = c = 0xdeadbeef + ((scm_t_uint32)(length<<2)) + 47; \
  66. \
  67. /* Handle most of the key. */ \
  68. while (length > 3) \
  69. { \
  70. a += k[0]; \
  71. b += k[1]; \
  72. c += k[2]; \
  73. mix (a, b, c); \
  74. length -= 3; \
  75. k += 3; \
  76. } \
  77. \
  78. /* Handle the last 3 elements. */ \
  79. switch(length) /* All the case statements fall through. */ \
  80. { \
  81. case 3 : c += k[2]; \
  82. case 2 : b += k[1]; \
  83. case 1 : a += k[0]; \
  84. final (a, b, c); \
  85. case 0: /* case 0: nothing left to add */ \
  86. break; \
  87. } \
  88. \
  89. if (sizeof (ret) == 8) \
  90. ret = (((unsigned long) c) << 32) | b; \
  91. else \
  92. ret = c; \
  93. } while (0)
  94. static unsigned long
  95. narrow_string_hash (const scm_t_uint8 *str, size_t len)
  96. {
  97. unsigned long ret;
  98. JENKINS_LOOKUP3_HASHWORD2 (str, len, ret);
  99. ret >>= 2; /* Ensure that it fits in a fixnum. */
  100. return ret;
  101. }
  102. static unsigned long
  103. wide_string_hash (const scm_t_wchar *str, size_t len)
  104. {
  105. unsigned long ret;
  106. JENKINS_LOOKUP3_HASHWORD2 (str, len, ret);
  107. ret >>= 2; /* Ensure that it fits in a fixnum. */
  108. return ret;
  109. }
  110. unsigned long
  111. scm_string_hash (const unsigned char *str, size_t len)
  112. {
  113. return narrow_string_hash (str, len);
  114. }
  115. unsigned long
  116. scm_i_string_hash (SCM str)
  117. {
  118. size_t len = scm_i_string_length (str);
  119. if (scm_i_is_narrow_string (str))
  120. return narrow_string_hash ((const scm_t_uint8 *) scm_i_string_chars (str),
  121. len);
  122. else
  123. return wide_string_hash (scm_i_string_wide_chars (str), len);
  124. }
  125. unsigned long
  126. scm_i_locale_string_hash (const char *str, size_t len)
  127. {
  128. return scm_i_string_hash (scm_from_locale_stringn (str, len));
  129. }
  130. unsigned long
  131. scm_i_latin1_string_hash (const char *str, size_t len)
  132. {
  133. if (len == (size_t) -1)
  134. len = strlen (str);
  135. return narrow_string_hash ((const scm_t_uint8 *) str, len);
  136. }
  137. /* A tricky optimization, but probably worth it. */
  138. unsigned long
  139. scm_i_utf8_string_hash (const char *str, size_t len)
  140. {
  141. const scm_t_uint8 *end, *ustr = (const scm_t_uint8 *) str;
  142. unsigned long ret;
  143. /* The length of the string in characters. This name corresponds to
  144. Jenkins' original name. */
  145. size_t length;
  146. scm_t_uint32 a, b, c, u32;
  147. if (len == (size_t) -1)
  148. len = strlen (str);
  149. end = ustr + len;
  150. if (u8_check (ustr, len) != NULL)
  151. /* Invalid UTF-8; punt. */
  152. return scm_i_string_hash (scm_from_utf8_stringn (str, len));
  153. length = u8_strnlen (ustr, len);
  154. /* Set up the internal state. */
  155. a = b = c = 0xdeadbeef + ((scm_t_uint32)(length<<2)) + 47;
  156. /* Handle most of the key. */
  157. while (length > 3)
  158. {
  159. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  160. a += u32;
  161. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  162. b += u32;
  163. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  164. c += u32;
  165. mix (a, b, c);
  166. length -= 3;
  167. }
  168. /* Handle the last 3 elements's. */
  169. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  170. a += u32;
  171. if (--length)
  172. {
  173. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  174. b += u32;
  175. if (--length)
  176. {
  177. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  178. c += u32;
  179. }
  180. }
  181. final (a, b, c);
  182. if (sizeof (unsigned long) == 8)
  183. ret = (((unsigned long) c) << 32) | b;
  184. else
  185. ret = c;
  186. ret >>= 2; /* Ensure that it fits in a fixnum. */
  187. return ret;
  188. }
  189. /* Thomas Wang's integer hasher, from
  190. http://www.cris.com/~Ttwang/tech/inthash.htm. */
  191. static unsigned long
  192. scm_raw_ihashq (scm_t_bits key)
  193. {
  194. if (sizeof (key) < 8)
  195. {
  196. key = (key ^ 61) ^ (key >> 16);
  197. key = key + (key << 3);
  198. key = key ^ (key >> 4);
  199. key = key * 0x27d4eb2d;
  200. key = key ^ (key >> 15);
  201. }
  202. else
  203. {
  204. key = (~key) + (key << 21); // key = (key << 21) - key - 1;
  205. key = key ^ (key >> 24);
  206. key = (key + (key << 3)) + (key << 8); // key * 265
  207. key = key ^ (key >> 14);
  208. key = (key + (key << 2)) + (key << 4); // key * 21
  209. key = key ^ (key >> 28);
  210. key = key + (key << 31);
  211. }
  212. key >>= 2; /* Ensure that it fits in a fixnum. */
  213. return key;
  214. }
  215. /* `depth' is used to limit recursion. */
  216. static unsigned long
  217. scm_raw_ihash (SCM obj, size_t depth)
  218. {
  219. if (SCM_IMP (obj))
  220. return scm_raw_ihashq (SCM_UNPACK (obj));
  221. switch (SCM_TYP7(obj))
  222. {
  223. /* FIXME: do better for structs, variables, ... Also the hashes
  224. are currently associative, which ain't the right thing. */
  225. case scm_tc7_smob:
  226. return scm_raw_ihashq (SCM_TYP16 (obj));
  227. case scm_tc7_number:
  228. if (scm_is_integer (obj))
  229. {
  230. SCM n = SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM);
  231. if (scm_is_inexact (obj))
  232. obj = scm_inexact_to_exact (obj);
  233. return scm_raw_ihashq (scm_to_ulong (scm_modulo (obj, n)));
  234. }
  235. else
  236. return scm_i_string_hash (scm_number_to_string (obj, scm_from_int (10)));
  237. case scm_tc7_string:
  238. return scm_i_string_hash (obj);
  239. case scm_tc7_symbol:
  240. return scm_i_symbol_hash (obj);
  241. case scm_tc7_pointer:
  242. return scm_raw_ihashq ((scm_t_uintptr) SCM_POINTER_VALUE (obj));
  243. case scm_tc7_wvect:
  244. case scm_tc7_vector:
  245. {
  246. size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj);
  247. size_t i = depth / 2;
  248. unsigned long h = scm_raw_ihashq (SCM_CELL_WORD_0 (obj));
  249. if (len)
  250. while (i--)
  251. h ^= scm_raw_ihash (scm_c_vector_ref (obj, h % len), i);
  252. return h;
  253. }
  254. case scm_tcs_cons_imcar:
  255. case scm_tcs_cons_nimcar:
  256. if (depth)
  257. return (scm_raw_ihash (SCM_CAR (obj), depth / 2)
  258. ^ scm_raw_ihash (SCM_CDR (obj), depth / 2));
  259. else
  260. return scm_raw_ihashq (scm_tc3_cons);
  261. default:
  262. return scm_raw_ihashq (SCM_CELL_WORD_0 (obj));
  263. }
  264. }
  265. unsigned long
  266. scm_ihashq (SCM obj, unsigned long n)
  267. {
  268. return scm_raw_ihashq (SCM_UNPACK (obj)) % n;
  269. }
  270. SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
  271. (SCM key, SCM size),
  272. "Determine a hash value for @var{key} that is suitable for\n"
  273. "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
  274. "used as the equality predicate. The function returns an\n"
  275. "integer in the range 0 to @var{size} - 1. Note that\n"
  276. "@code{hashq} may use internal addresses. Thus two calls to\n"
  277. "hashq where the keys are @code{eq?} are not guaranteed to\n"
  278. "deliver the same value if the key object gets garbage collected\n"
  279. "in between. This can happen, for example with symbols:\n"
  280. "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
  281. "different values, since @code{foo} will be garbage collected.")
  282. #define FUNC_NAME s_scm_hashq
  283. {
  284. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  285. return scm_from_ulong (scm_ihashq (key, sz));
  286. }
  287. #undef FUNC_NAME
  288. unsigned long
  289. scm_ihashv (SCM obj, unsigned long n)
  290. {
  291. if (SCM_NUMP(obj))
  292. return scm_raw_ihash (obj, 10) % n;
  293. else
  294. return scm_raw_ihashq (SCM_UNPACK (obj)) % n;
  295. }
  296. SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
  297. (SCM key, SCM size),
  298. "Determine a hash value for @var{key} that is suitable for\n"
  299. "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
  300. "used as the equality predicate. The function returns an\n"
  301. "integer in the range 0 to @var{size} - 1. Note that\n"
  302. "@code{(hashv key)} may use internal addresses. Thus two calls\n"
  303. "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
  304. "deliver the same value if the key object gets garbage collected\n"
  305. "in between. This can happen, for example with symbols:\n"
  306. "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
  307. "different values, since @code{foo} will be garbage collected.")
  308. #define FUNC_NAME s_scm_hashv
  309. {
  310. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  311. return scm_from_ulong (scm_ihashv (key, sz));
  312. }
  313. #undef FUNC_NAME
  314. unsigned long
  315. scm_ihash (SCM obj, unsigned long n)
  316. {
  317. return (unsigned long) scm_raw_ihash (obj, 10) % n;
  318. }
  319. SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
  320. (SCM key, SCM size),
  321. "Determine a hash value for @var{key} that is suitable for\n"
  322. "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
  323. "is used as the equality predicate. The function returns an\n"
  324. "integer in the range 0 to @var{size} - 1.")
  325. #define FUNC_NAME s_scm_hash
  326. {
  327. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  328. return scm_from_ulong (scm_ihash (key, sz));
  329. }
  330. #undef FUNC_NAME
  331. void
  332. scm_init_hash ()
  333. {
  334. #include "libguile/hash.x"
  335. }
  336. /*
  337. Local Variables:
  338. c-file-style: "gnu"
  339. End:
  340. */