hash.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* Copyright (C) 1995, 1996, 1997, 2000, 2001, 2003, 2004, 2006, 2008,
  2. * 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #ifdef HAVE_WCHAR_H
  23. #include <wchar.h>
  24. #endif
  25. #include <math.h>
  26. #include <unistr.h>
  27. #include "libguile/_scm.h"
  28. #include "libguile/chars.h"
  29. #include "libguile/ports.h"
  30. #include "libguile/strings.h"
  31. #include "libguile/symbols.h"
  32. #include "libguile/vectors.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/hash.h"
  35. #ifndef floor
  36. extern double floor();
  37. #endif
  38. /* This hash function is originally from
  39. http://burtleburtle.net/bob/c/lookup3.c by Bob Jenkins, May 2006,
  40. Public Domain. No warranty. */
  41. #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
  42. #define mix(a,b,c) \
  43. { \
  44. a -= c; a ^= rot(c, 4); c += b; \
  45. b -= a; b ^= rot(a, 6); a += c; \
  46. c -= b; c ^= rot(b, 8); b += a; \
  47. a -= c; a ^= rot(c,16); c += b; \
  48. b -= a; b ^= rot(a,19); a += c; \
  49. c -= b; c ^= rot(b, 4); b += a; \
  50. }
  51. #define final(a,b,c) \
  52. { \
  53. c ^= b; c -= rot(b,14); \
  54. a ^= c; a -= rot(c,11); \
  55. b ^= a; b -= rot(a,25); \
  56. c ^= b; c -= rot(b,16); \
  57. a ^= c; a -= rot(c,4); \
  58. b ^= a; b -= rot(a,14); \
  59. c ^= b; c -= rot(b,24); \
  60. }
  61. #define JENKINS_LOOKUP3_HASHWORD2(k, length, ret) \
  62. do { \
  63. scm_t_uint32 a, b, c; \
  64. \
  65. /* Set up the internal state. */ \
  66. a = b = c = 0xdeadbeef + ((scm_t_uint32)(length<<2)) + 47; \
  67. \
  68. /* Handle most of the key. */ \
  69. while (length > 3) \
  70. { \
  71. a += k[0]; \
  72. b += k[1]; \
  73. c += k[2]; \
  74. mix (a, b, c); \
  75. length -= 3; \
  76. k += 3; \
  77. } \
  78. \
  79. /* Handle the last 3 elements. */ \
  80. switch(length) /* All the case statements fall through. */ \
  81. { \
  82. case 3 : c += k[2]; \
  83. case 2 : b += k[1]; \
  84. case 1 : a += k[0]; \
  85. final (a, b, c); \
  86. case 0: /* case 0: nothing left to add */ \
  87. break; \
  88. } \
  89. \
  90. if (sizeof (ret) == 8) \
  91. ret = (((unsigned long) c) << 32) | b; \
  92. else \
  93. ret = c; \
  94. } while (0)
  95. static unsigned long
  96. narrow_string_hash (const scm_t_uint8 *str, size_t len)
  97. {
  98. unsigned long ret;
  99. JENKINS_LOOKUP3_HASHWORD2 (str, len, ret);
  100. ret >>= 2; /* Ensure that it fits in a fixnum. */
  101. return ret;
  102. }
  103. static unsigned long
  104. wide_string_hash (const scm_t_wchar *str, size_t len)
  105. {
  106. unsigned long ret;
  107. JENKINS_LOOKUP3_HASHWORD2 (str, len, ret);
  108. ret >>= 2; /* Ensure that it fits in a fixnum. */
  109. return ret;
  110. }
  111. unsigned long
  112. scm_string_hash (const unsigned char *str, size_t len)
  113. {
  114. return narrow_string_hash (str, len);
  115. }
  116. unsigned long
  117. scm_i_string_hash (SCM str)
  118. {
  119. size_t len = scm_i_string_length (str);
  120. if (scm_i_is_narrow_string (str))
  121. return narrow_string_hash ((const scm_t_uint8 *) scm_i_string_chars (str),
  122. len);
  123. else
  124. return wide_string_hash (scm_i_string_wide_chars (str), len);
  125. }
  126. unsigned long
  127. scm_i_locale_string_hash (const char *str, size_t len)
  128. {
  129. return scm_i_string_hash (scm_from_locale_stringn (str, len));
  130. }
  131. unsigned long
  132. scm_i_latin1_string_hash (const char *str, size_t len)
  133. {
  134. if (len == (size_t) -1)
  135. len = strlen (str);
  136. return narrow_string_hash ((const scm_t_uint8 *) str, len);
  137. }
  138. /* A tricky optimization, but probably worth it. */
  139. unsigned long
  140. scm_i_utf8_string_hash (const char *str, size_t len)
  141. {
  142. const scm_t_uint8 *end, *ustr = (const scm_t_uint8 *) str;
  143. unsigned long ret;
  144. /* The length of the string in characters. This name corresponds to
  145. Jenkins' original name. */
  146. size_t length;
  147. scm_t_uint32 a, b, c, u32;
  148. if (len == (size_t) -1)
  149. len = strlen (str);
  150. end = ustr + len;
  151. if (u8_check (ustr, len) != NULL)
  152. /* Invalid UTF-8; punt. */
  153. return scm_i_string_hash (scm_from_utf8_stringn (str, len));
  154. length = u8_strnlen (ustr, len);
  155. /* Set up the internal state. */
  156. a = b = c = 0xdeadbeef + ((scm_t_uint32)(length<<2)) + 47;
  157. /* Handle most of the key. */
  158. while (length > 3)
  159. {
  160. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  161. a += u32;
  162. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  163. b += u32;
  164. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  165. c += u32;
  166. mix (a, b, c);
  167. length -= 3;
  168. }
  169. /* Handle the last 3 elements's. */
  170. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  171. a += u32;
  172. if (--length)
  173. {
  174. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  175. b += u32;
  176. if (--length)
  177. {
  178. ustr += u8_mbtouc_unsafe (&u32, ustr, end - ustr);
  179. c += u32;
  180. }
  181. }
  182. final (a, b, c);
  183. if (sizeof (unsigned long) == 8)
  184. ret = (((unsigned long) c) << 32) | b;
  185. else
  186. ret = c;
  187. ret >>= 2; /* Ensure that it fits in a fixnum. */
  188. return ret;
  189. }
  190. static unsigned long scm_raw_ihashq (scm_t_bits key);
  191. static unsigned long scm_raw_ihash (SCM obj, size_t depth);
  192. /* Return the hash of struct OBJ. Traverse OBJ's fields to compute the
  193. result, unless DEPTH is zero. Assumes that OBJ is a struct. */
  194. static unsigned long
  195. scm_i_struct_hash (SCM obj, size_t depth)
  196. {
  197. SCM layout;
  198. scm_t_bits *data;
  199. size_t struct_size, field_num;
  200. unsigned long hash;
  201. layout = SCM_STRUCT_LAYOUT (obj);
  202. struct_size = scm_i_symbol_length (layout) / 2;
  203. data = SCM_STRUCT_DATA (obj);
  204. hash = scm_raw_ihashq (SCM_UNPACK (SCM_STRUCT_VTABLE (obj)));
  205. if (depth > 0)
  206. for (field_num = 0; field_num < struct_size; field_num++)
  207. {
  208. int protection;
  209. protection = scm_i_symbol_ref (layout, field_num * 2 + 1);
  210. if (protection != 'h' && protection != 'o')
  211. {
  212. int type;
  213. type = scm_i_symbol_ref (layout, field_num * 2);
  214. switch (type)
  215. {
  216. case 'p':
  217. hash ^= scm_raw_ihash (SCM_PACK (data[field_num]),
  218. depth / 2);
  219. break;
  220. case 'u':
  221. hash ^= scm_raw_ihashq (data[field_num]);
  222. break;
  223. default:
  224. /* Ignore 's' fields. */;
  225. }
  226. }
  227. }
  228. /* FIXME: Tail elements should be taken into account. */
  229. return hash;
  230. }
  231. /* Thomas Wang's integer hasher, from
  232. http://www.cris.com/~Ttwang/tech/inthash.htm. */
  233. static unsigned long
  234. scm_raw_ihashq (scm_t_bits key)
  235. {
  236. if (sizeof (key) < 8)
  237. {
  238. key = (key ^ 61) ^ (key >> 16);
  239. key = key + (key << 3);
  240. key = key ^ (key >> 4);
  241. key = key * 0x27d4eb2d;
  242. key = key ^ (key >> 15);
  243. }
  244. else
  245. {
  246. key = (~key) + (key << 21); // key = (key << 21) - key - 1;
  247. key = key ^ (key >> 24);
  248. key = (key + (key << 3)) + (key << 8); // key * 265
  249. key = key ^ (key >> 14);
  250. key = (key + (key << 2)) + (key << 4); // key * 21
  251. key = key ^ (key >> 28);
  252. key = key + (key << 31);
  253. }
  254. key >>= 2; /* Ensure that it fits in a fixnum. */
  255. return key;
  256. }
  257. /* `depth' is used to limit recursion. */
  258. static unsigned long
  259. scm_raw_ihash (SCM obj, size_t depth)
  260. {
  261. if (SCM_IMP (obj))
  262. return scm_raw_ihashq (SCM_UNPACK (obj));
  263. switch (SCM_TYP7(obj))
  264. {
  265. /* FIXME: do better for structs, variables, ... Also the hashes
  266. are currently associative, which ain't the right thing. */
  267. case scm_tc7_smob:
  268. return scm_raw_ihashq (SCM_TYP16 (obj));
  269. case scm_tc7_number:
  270. if (scm_is_integer (obj))
  271. {
  272. SCM n = SCM_I_MAKINUM (SCM_MOST_POSITIVE_FIXNUM);
  273. if (scm_is_inexact (obj))
  274. obj = scm_inexact_to_exact (obj);
  275. return scm_raw_ihashq (scm_to_ulong (scm_modulo (obj, n)));
  276. }
  277. else
  278. return scm_i_string_hash (scm_number_to_string (obj, scm_from_int (10)));
  279. case scm_tc7_string:
  280. return scm_i_string_hash (obj);
  281. case scm_tc7_symbol:
  282. return scm_i_symbol_hash (obj);
  283. case scm_tc7_pointer:
  284. return scm_raw_ihashq ((scm_t_uintptr) SCM_POINTER_VALUE (obj));
  285. case scm_tc7_wvect:
  286. case scm_tc7_vector:
  287. {
  288. size_t len = SCM_SIMPLE_VECTOR_LENGTH (obj);
  289. size_t i = depth / 2;
  290. unsigned long h = scm_raw_ihashq (SCM_CELL_WORD_0 (obj));
  291. if (len)
  292. while (i--)
  293. h ^= scm_raw_ihash (scm_c_vector_ref (obj, h % len), i);
  294. return h;
  295. }
  296. case scm_tcs_cons_imcar:
  297. case scm_tcs_cons_nimcar:
  298. if (depth)
  299. return (scm_raw_ihash (SCM_CAR (obj), depth / 2)
  300. ^ scm_raw_ihash (SCM_CDR (obj), depth / 2));
  301. else
  302. return scm_raw_ihashq (scm_tc3_cons);
  303. case scm_tcs_struct:
  304. return scm_i_struct_hash (obj, depth);
  305. default:
  306. return scm_raw_ihashq (SCM_CELL_WORD_0 (obj));
  307. }
  308. }
  309. unsigned long
  310. scm_ihashq (SCM obj, unsigned long n)
  311. {
  312. return scm_raw_ihashq (SCM_UNPACK (obj)) % n;
  313. }
  314. SCM_DEFINE (scm_hashq, "hashq", 2, 0, 0,
  315. (SCM key, SCM size),
  316. "Determine a hash value for @var{key} that is suitable for\n"
  317. "lookups in a hashtable of size @var{size}, where @code{eq?} is\n"
  318. "used as the equality predicate. The function returns an\n"
  319. "integer in the range 0 to @var{size} - 1. Note that\n"
  320. "@code{hashq} may use internal addresses. Thus two calls to\n"
  321. "hashq where the keys are @code{eq?} are not guaranteed to\n"
  322. "deliver the same value if the key object gets garbage collected\n"
  323. "in between. This can happen, for example with symbols:\n"
  324. "@code{(hashq 'foo n) (gc) (hashq 'foo n)} may produce two\n"
  325. "different values, since @code{foo} will be garbage collected.")
  326. #define FUNC_NAME s_scm_hashq
  327. {
  328. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  329. return scm_from_ulong (scm_ihashq (key, sz));
  330. }
  331. #undef FUNC_NAME
  332. unsigned long
  333. scm_ihashv (SCM obj, unsigned long n)
  334. {
  335. if (SCM_NUMP(obj))
  336. return scm_raw_ihash (obj, 10) % n;
  337. else
  338. return scm_raw_ihashq (SCM_UNPACK (obj)) % n;
  339. }
  340. SCM_DEFINE (scm_hashv, "hashv", 2, 0, 0,
  341. (SCM key, SCM size),
  342. "Determine a hash value for @var{key} that is suitable for\n"
  343. "lookups in a hashtable of size @var{size}, where @code{eqv?} is\n"
  344. "used as the equality predicate. The function returns an\n"
  345. "integer in the range 0 to @var{size} - 1. Note that\n"
  346. "@code{(hashv key)} may use internal addresses. Thus two calls\n"
  347. "to hashv where the keys are @code{eqv?} are not guaranteed to\n"
  348. "deliver the same value if the key object gets garbage collected\n"
  349. "in between. This can happen, for example with symbols:\n"
  350. "@code{(hashv 'foo n) (gc) (hashv 'foo n)} may produce two\n"
  351. "different values, since @code{foo} will be garbage collected.")
  352. #define FUNC_NAME s_scm_hashv
  353. {
  354. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  355. return scm_from_ulong (scm_ihashv (key, sz));
  356. }
  357. #undef FUNC_NAME
  358. unsigned long
  359. scm_ihash (SCM obj, unsigned long n)
  360. {
  361. return (unsigned long) scm_raw_ihash (obj, 10) % n;
  362. }
  363. SCM_DEFINE (scm_hash, "hash", 2, 0, 0,
  364. (SCM key, SCM size),
  365. "Determine a hash value for @var{key} that is suitable for\n"
  366. "lookups in a hashtable of size @var{size}, where @code{equal?}\n"
  367. "is used as the equality predicate. The function returns an\n"
  368. "integer in the range 0 to @var{size} - 1.")
  369. #define FUNC_NAME s_scm_hash
  370. {
  371. unsigned long sz = scm_to_unsigned_integer (size, 1, ULONG_MAX);
  372. return scm_from_ulong (scm_ihash (key, sz));
  373. }
  374. #undef FUNC_NAME
  375. void
  376. scm_init_hash ()
  377. {
  378. #include "libguile/hash.x"
  379. }
  380. /*
  381. Local Variables:
  382. c-file-style: "gnu"
  383. End:
  384. */