hash.c 14 KB

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