hash.c 13 KB

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