hashtab.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2006,
  2. * 2008, 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. #include <alloca.h>
  23. #include <stdio.h>
  24. #include <assert.h>
  25. #include "libguile/_scm.h"
  26. #include "libguile/alist.h"
  27. #include "libguile/hash.h"
  28. #include "libguile/eval.h"
  29. #include "libguile/root.h"
  30. #include "libguile/vectors.h"
  31. #include "libguile/ports.h"
  32. #include "libguile/bdw-gc.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/hashtab.h"
  35. /* A hash table is a cell containing a vector of association lists.
  36. *
  37. * Growing or shrinking, with following rehashing, is triggered when
  38. * the load factor
  39. *
  40. * L = N / S (N: number of items in table, S: bucket vector length)
  41. *
  42. * passes an upper limit of 0.9 or a lower limit of 0.25.
  43. *
  44. * The implementation stores the upper and lower number of items which
  45. * trigger a resize in the hashtable object.
  46. *
  47. * Possible hash table sizes (primes) are stored in the array
  48. * hashtable_size.
  49. */
  50. static unsigned long hashtable_size[] = {
  51. 31, 61, 113, 223, 443, 883, 1759, 3517, 7027, 14051, 28099, 56197, 112363,
  52. 224717, 449419, 898823, 1797641, 3595271, 7190537, 14381041
  53. #if SIZEOF_SCM_T_BITS > 4
  54. /* vector lengths are stored in the first word of vectors, shifted by
  55. 8 bits for the tc8, so for 32-bit we only get 2^24-1 = 16777215
  56. elements. But we allow a few more sizes for 64-bit. */
  57. , 28762081, 57524111, 115048217, 230096423, 460192829
  58. #endif
  59. };
  60. #define HASHTABLE_SIZE_N (sizeof(hashtable_size)/sizeof(unsigned long))
  61. static char *s_hashtable = "hashtable";
  62. static SCM
  63. make_hash_table (unsigned long k, const char *func_name)
  64. {
  65. SCM vector;
  66. scm_t_hashtable *t;
  67. int i = 0, n = k ? k : 31;
  68. while (i + 1 < HASHTABLE_SIZE_N && n > hashtable_size[i])
  69. ++i;
  70. n = hashtable_size[i];
  71. vector = scm_c_make_vector (n, SCM_EOL);
  72. t = scm_gc_malloc_pointerless (sizeof (*t), s_hashtable);
  73. t->min_size_index = t->size_index = i;
  74. t->n_items = 0;
  75. t->lower = 0;
  76. t->upper = 9 * n / 10;
  77. /* FIXME: we just need two words of storage, not three */
  78. return scm_double_cell (scm_tc7_hashtable, SCM_UNPACK (vector),
  79. (scm_t_bits)t, 0);
  80. }
  81. void
  82. scm_i_rehash (SCM table,
  83. scm_t_hash_fn hash_fn,
  84. void *closure,
  85. const char* func_name)
  86. {
  87. SCM buckets, new_buckets;
  88. int i;
  89. unsigned long old_size;
  90. unsigned long new_size;
  91. if (SCM_HASHTABLE_N_ITEMS (table) < SCM_HASHTABLE_LOWER (table))
  92. {
  93. /* rehashing is not triggered when i <= min_size */
  94. i = SCM_HASHTABLE (table)->size_index;
  95. do
  96. --i;
  97. while (i > SCM_HASHTABLE (table)->min_size_index
  98. && SCM_HASHTABLE_N_ITEMS (table) < hashtable_size[i] / 4);
  99. }
  100. else
  101. {
  102. i = SCM_HASHTABLE (table)->size_index + 1;
  103. if (i >= HASHTABLE_SIZE_N)
  104. /* don't rehash */
  105. return;
  106. }
  107. SCM_HASHTABLE (table)->size_index = i;
  108. new_size = hashtable_size[i];
  109. if (i <= SCM_HASHTABLE (table)->min_size_index)
  110. SCM_HASHTABLE (table)->lower = 0;
  111. else
  112. SCM_HASHTABLE (table)->lower = new_size / 4;
  113. SCM_HASHTABLE (table)->upper = 9 * new_size / 10;
  114. buckets = SCM_HASHTABLE_VECTOR (table);
  115. new_buckets = scm_c_make_vector (new_size, SCM_EOL);
  116. SCM_SET_HASHTABLE_VECTOR (table, new_buckets);
  117. SCM_SET_HASHTABLE_N_ITEMS (table, 0);
  118. old_size = SCM_SIMPLE_VECTOR_LENGTH (buckets);
  119. for (i = 0; i < old_size; ++i)
  120. {
  121. SCM ls, cell, handle;
  122. ls = SCM_SIMPLE_VECTOR_REF (buckets, i);
  123. SCM_SIMPLE_VECTOR_SET (buckets, i, SCM_EOL);
  124. while (scm_is_pair (ls))
  125. {
  126. unsigned long h;
  127. cell = ls;
  128. handle = SCM_CAR (cell);
  129. ls = SCM_CDR (ls);
  130. h = hash_fn (SCM_CAR (handle), new_size, closure);
  131. if (h >= new_size)
  132. scm_out_of_range (func_name, scm_from_ulong (h));
  133. SCM_SETCDR (cell, SCM_SIMPLE_VECTOR_REF (new_buckets, h));
  134. SCM_SIMPLE_VECTOR_SET (new_buckets, h, cell);
  135. SCM_HASHTABLE_INCREMENT (table);
  136. }
  137. }
  138. }
  139. void
  140. scm_i_hashtable_print (SCM exp, SCM port, scm_print_state *pstate)
  141. {
  142. scm_puts_unlocked ("#<hash-table ", port);
  143. scm_uintprint (SCM_UNPACK (exp), 16, port);
  144. scm_putc (' ', port);
  145. scm_uintprint (SCM_HASHTABLE_N_ITEMS (exp), 10, port);
  146. scm_putc_unlocked ('/', port);
  147. scm_uintprint (SCM_SIMPLE_VECTOR_LENGTH (SCM_HASHTABLE_VECTOR (exp)),
  148. 10, port);
  149. scm_puts_unlocked (">", port);
  150. }
  151. SCM
  152. scm_c_make_hash_table (unsigned long k)
  153. {
  154. return make_hash_table (k, "scm_c_make_hash_table");
  155. }
  156. SCM_DEFINE (scm_make_hash_table, "make-hash-table", 0, 1, 0,
  157. (SCM n),
  158. "Make a new abstract hash table object with minimum number of buckets @var{n}\n")
  159. #define FUNC_NAME s_scm_make_hash_table
  160. {
  161. return make_hash_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n), FUNC_NAME);
  162. }
  163. #undef FUNC_NAME
  164. #define SCM_WEAK_TABLE_P(x) (scm_is_true (scm_weak_table_p (x)))
  165. SCM_DEFINE (scm_hash_table_p, "hash-table?", 1, 0, 0,
  166. (SCM obj),
  167. "Return @code{#t} if @var{obj} is an abstract hash table object.")
  168. #define FUNC_NAME s_scm_hash_table_p
  169. {
  170. return scm_from_bool (SCM_HASHTABLE_P (obj) || SCM_WEAK_TABLE_P (obj));
  171. }
  172. #undef FUNC_NAME
  173. /* Accessing hash table entries. */
  174. SCM
  175. scm_hash_fn_get_handle (SCM table, SCM obj,
  176. scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn,
  177. void * closure)
  178. #define FUNC_NAME "scm_hash_fn_get_handle"
  179. {
  180. unsigned long k;
  181. SCM buckets, h;
  182. SCM_VALIDATE_HASHTABLE (SCM_ARG1, table);
  183. buckets = SCM_HASHTABLE_VECTOR (table);
  184. if (SCM_SIMPLE_VECTOR_LENGTH (buckets) == 0)
  185. return SCM_BOOL_F;
  186. k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
  187. if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
  188. scm_out_of_range (FUNC_NAME, scm_from_ulong (k));
  189. h = assoc_fn (obj, SCM_SIMPLE_VECTOR_REF (buckets, k), closure);
  190. return h;
  191. }
  192. #undef FUNC_NAME
  193. SCM
  194. scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init,
  195. scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn,
  196. void * closure)
  197. #define FUNC_NAME "scm_hash_fn_create_handle_x"
  198. {
  199. unsigned long k;
  200. SCM buckets, it;
  201. SCM_VALIDATE_HASHTABLE (SCM_ARG1, table);
  202. buckets = SCM_HASHTABLE_VECTOR (table);
  203. if (SCM_SIMPLE_VECTOR_LENGTH (buckets) == 0)
  204. SCM_MISC_ERROR ("void hashtable", SCM_EOL);
  205. k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
  206. if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
  207. scm_out_of_range ("hash_fn_create_handle_x", scm_from_ulong (k));
  208. it = assoc_fn (obj, SCM_SIMPLE_VECTOR_REF (buckets, k), closure);
  209. if (scm_is_pair (it))
  210. return it;
  211. else if (scm_is_true (it))
  212. scm_wrong_type_arg_msg (NULL, 0, it, "a pair");
  213. else
  214. {
  215. SCM handle, new_bucket;
  216. handle = scm_cons (obj, init);
  217. new_bucket = scm_cons (handle, SCM_EOL);
  218. if (!scm_is_eq (SCM_HASHTABLE_VECTOR (table), buckets))
  219. {
  220. buckets = SCM_HASHTABLE_VECTOR (table);
  221. k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
  222. if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
  223. scm_out_of_range ("hash_fn_create_handle_x", scm_from_ulong (k));
  224. }
  225. SCM_SETCDR (new_bucket, SCM_SIMPLE_VECTOR_REF (buckets, k));
  226. SCM_SIMPLE_VECTOR_SET (buckets, k, new_bucket);
  227. SCM_HASHTABLE_INCREMENT (table);
  228. /* Maybe rehash the table. */
  229. if (SCM_HASHTABLE_N_ITEMS (table) < SCM_HASHTABLE_LOWER (table)
  230. || SCM_HASHTABLE_N_ITEMS (table) > SCM_HASHTABLE_UPPER (table))
  231. scm_i_rehash (table, hash_fn, closure, FUNC_NAME);
  232. return SCM_CAR (new_bucket);
  233. }
  234. }
  235. #undef FUNC_NAME
  236. SCM
  237. scm_hash_fn_ref (SCM table, SCM obj, SCM dflt,
  238. scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn,
  239. void *closure)
  240. {
  241. SCM it = scm_hash_fn_get_handle (table, obj, hash_fn, assoc_fn, closure);
  242. if (scm_is_pair (it))
  243. return SCM_CDR (it);
  244. else
  245. return dflt;
  246. }
  247. SCM
  248. scm_hash_fn_set_x (SCM table, SCM obj, SCM val,
  249. scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn,
  250. void *closure)
  251. {
  252. SCM pair;
  253. pair = scm_hash_fn_create_handle_x (table, obj, val,
  254. hash_fn, assoc_fn, closure);
  255. if (!scm_is_eq (SCM_CDR (pair), val))
  256. SCM_SETCDR (pair, val);
  257. return val;
  258. }
  259. SCM
  260. scm_hash_fn_remove_x (SCM table, SCM obj,
  261. scm_t_hash_fn hash_fn,
  262. scm_t_assoc_fn assoc_fn,
  263. void *closure)
  264. #define FUNC_NAME "hash_fn_remove_x"
  265. {
  266. unsigned long k;
  267. SCM buckets, h;
  268. SCM_VALIDATE_HASHTABLE (SCM_ARG1, table);
  269. buckets = SCM_HASHTABLE_VECTOR (table);
  270. if (SCM_SIMPLE_VECTOR_LENGTH (buckets) == 0)
  271. return SCM_EOL;
  272. k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
  273. if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
  274. scm_out_of_range (FUNC_NAME, scm_from_ulong (k));
  275. h = assoc_fn (obj, SCM_SIMPLE_VECTOR_REF (buckets, k), closure);
  276. if (scm_is_true (h))
  277. {
  278. SCM_SIMPLE_VECTOR_SET
  279. (buckets, k, scm_delq_x (h, SCM_SIMPLE_VECTOR_REF (buckets, k)));
  280. SCM_HASHTABLE_DECREMENT (table);
  281. if (SCM_HASHTABLE_N_ITEMS (table) < SCM_HASHTABLE_LOWER (table))
  282. scm_i_rehash (table, hash_fn, closure, FUNC_NAME);
  283. }
  284. return h;
  285. }
  286. #undef FUNC_NAME
  287. SCM_DEFINE (scm_hash_clear_x, "hash-clear!", 1, 0, 0,
  288. (SCM table),
  289. "Remove all items from @var{table} (without triggering a resize).")
  290. #define FUNC_NAME s_scm_hash_clear_x
  291. {
  292. if (SCM_WEAK_TABLE_P (table))
  293. return scm_weak_table_clear_x (table);
  294. SCM_VALIDATE_HASHTABLE (SCM_ARG1, table);
  295. scm_vector_fill_x (SCM_HASHTABLE_VECTOR (table), SCM_EOL);
  296. SCM_SET_HASHTABLE_N_ITEMS (table, 0);
  297. return SCM_UNSPECIFIED;
  298. }
  299. #undef FUNC_NAME
  300. SCM_DEFINE (scm_hashq_get_handle, "hashq-get-handle", 2, 0, 0,
  301. (SCM table, SCM key),
  302. "This procedure returns the @code{(key . value)} pair from the\n"
  303. "hash table @var{table}. If @var{table} does not hold an\n"
  304. "associated value for @var{key}, @code{#f} is returned.\n"
  305. "Uses @code{eq?} for equality testing.")
  306. #define FUNC_NAME s_scm_hashq_get_handle
  307. {
  308. return scm_hash_fn_get_handle (table, key,
  309. (scm_t_hash_fn) scm_ihashq,
  310. (scm_t_assoc_fn) scm_sloppy_assq,
  311. 0);
  312. }
  313. #undef FUNC_NAME
  314. SCM_DEFINE (scm_hashq_create_handle_x, "hashq-create-handle!", 3, 0, 0,
  315. (SCM table, SCM key, SCM init),
  316. "This function looks up @var{key} in @var{table} and returns its handle.\n"
  317. "If @var{key} is not already present, a new handle is created which\n"
  318. "associates @var{key} with @var{init}.")
  319. #define FUNC_NAME s_scm_hashq_create_handle_x
  320. {
  321. return scm_hash_fn_create_handle_x (table, key, init,
  322. (scm_t_hash_fn) scm_ihashq,
  323. (scm_t_assoc_fn) scm_sloppy_assq,
  324. 0);
  325. }
  326. #undef FUNC_NAME
  327. SCM_DEFINE (scm_hashq_ref, "hashq-ref", 2, 1, 0,
  328. (SCM table, SCM key, SCM dflt),
  329. "Look up @var{key} in the hash table @var{table}, and return the\n"
  330. "value (if any) associated with it. If @var{key} is not found,\n"
  331. "return @var{dflt} (or @code{#f} if no @var{dflt} argument\n"
  332. "is supplied). Uses @code{eq?} for equality testing.")
  333. #define FUNC_NAME s_scm_hashq_ref
  334. {
  335. if (SCM_UNBNDP (dflt))
  336. dflt = SCM_BOOL_F;
  337. if (SCM_WEAK_TABLE_P (table))
  338. return scm_weak_table_refq (table, key, dflt);
  339. return scm_hash_fn_ref (table, key, dflt,
  340. (scm_t_hash_fn) scm_ihashq,
  341. (scm_t_assoc_fn) scm_sloppy_assq,
  342. 0);
  343. }
  344. #undef FUNC_NAME
  345. SCM_DEFINE (scm_hashq_set_x, "hashq-set!", 3, 0, 0,
  346. (SCM table, SCM key, SCM val),
  347. "Find the entry in @var{table} associated with @var{key}, and\n"
  348. "store @var{val} there. Uses @code{eq?} for equality testing.")
  349. #define FUNC_NAME s_scm_hashq_set_x
  350. {
  351. if (SCM_WEAK_TABLE_P (table))
  352. return scm_weak_table_putq_x (table, key, val);
  353. return scm_hash_fn_set_x (table, key, val,
  354. (scm_t_hash_fn) scm_ihashq,
  355. (scm_t_assoc_fn) scm_sloppy_assq,
  356. 0);
  357. }
  358. #undef FUNC_NAME
  359. SCM_DEFINE (scm_hashq_remove_x, "hashq-remove!", 2, 0, 0,
  360. (SCM table, SCM key),
  361. "Remove @var{key} (and any value associated with it) from\n"
  362. "@var{table}. Uses @code{eq?} for equality tests.")
  363. #define FUNC_NAME s_scm_hashq_remove_x
  364. {
  365. if (SCM_WEAK_TABLE_P (table))
  366. return scm_weak_table_remq_x (table, key);
  367. return scm_hash_fn_remove_x (table, key,
  368. (scm_t_hash_fn) scm_ihashq,
  369. (scm_t_assoc_fn) scm_sloppy_assq,
  370. 0);
  371. }
  372. #undef FUNC_NAME
  373. SCM_DEFINE (scm_hashv_get_handle, "hashv-get-handle", 2, 0, 0,
  374. (SCM table, SCM key),
  375. "This procedure returns the @code{(key . value)} pair from the\n"
  376. "hash table @var{table}. If @var{table} does not hold an\n"
  377. "associated value for @var{key}, @code{#f} is returned.\n"
  378. "Uses @code{eqv?} for equality testing.")
  379. #define FUNC_NAME s_scm_hashv_get_handle
  380. {
  381. return scm_hash_fn_get_handle (table, key,
  382. (scm_t_hash_fn) scm_ihashv,
  383. (scm_t_assoc_fn) scm_sloppy_assv,
  384. 0);
  385. }
  386. #undef FUNC_NAME
  387. SCM_DEFINE (scm_hashv_create_handle_x, "hashv-create-handle!", 3, 0, 0,
  388. (SCM table, SCM key, SCM init),
  389. "This function looks up @var{key} in @var{table} and returns its handle.\n"
  390. "If @var{key} is not already present, a new handle is created which\n"
  391. "associates @var{key} with @var{init}.")
  392. #define FUNC_NAME s_scm_hashv_create_handle_x
  393. {
  394. return scm_hash_fn_create_handle_x (table, key, init,
  395. (scm_t_hash_fn) scm_ihashv,
  396. (scm_t_assoc_fn) scm_sloppy_assv,
  397. 0);
  398. }
  399. #undef FUNC_NAME
  400. static int
  401. assv_predicate (SCM k, SCM v, void *closure)
  402. {
  403. return scm_is_true (scm_eqv_p (k, SCM_PACK_POINTER (closure)));
  404. }
  405. SCM_DEFINE (scm_hashv_ref, "hashv-ref", 2, 1, 0,
  406. (SCM table, SCM key, SCM dflt),
  407. "Look up @var{key} in the hash table @var{table}, and return the\n"
  408. "value (if any) associated with it. If @var{key} is not found,\n"
  409. "return @var{dflt} (or @code{#f} if no @var{dflt} argument\n"
  410. "is supplied). Uses @code{eqv?} for equality testing.")
  411. #define FUNC_NAME s_scm_hashv_ref
  412. {
  413. if (SCM_UNBNDP (dflt))
  414. dflt = SCM_BOOL_F;
  415. if (SCM_WEAK_TABLE_P (table))
  416. return scm_c_weak_table_ref (table, scm_ihashv (key, -1),
  417. assv_predicate, SCM_PACK (key), dflt);
  418. return scm_hash_fn_ref (table, key, dflt,
  419. (scm_t_hash_fn) scm_ihashv,
  420. (scm_t_assoc_fn) scm_sloppy_assv,
  421. 0);
  422. }
  423. #undef FUNC_NAME
  424. SCM_DEFINE (scm_hashv_set_x, "hashv-set!", 3, 0, 0,
  425. (SCM table, SCM key, SCM val),
  426. "Find the entry in @var{table} associated with @var{key}, and\n"
  427. "store @var{value} there. Uses @code{eqv?} for equality testing.")
  428. #define FUNC_NAME s_scm_hashv_set_x
  429. {
  430. if (SCM_WEAK_TABLE_P (table))
  431. {
  432. scm_c_weak_table_put_x (table, scm_ihashv (key, -1),
  433. assv_predicate, SCM_PACK (key),
  434. key, val);
  435. return SCM_UNSPECIFIED;
  436. }
  437. return scm_hash_fn_set_x (table, key, val,
  438. (scm_t_hash_fn) scm_ihashv,
  439. (scm_t_assoc_fn) scm_sloppy_assv,
  440. 0);
  441. }
  442. #undef FUNC_NAME
  443. SCM_DEFINE (scm_hashv_remove_x, "hashv-remove!", 2, 0, 0,
  444. (SCM table, SCM key),
  445. "Remove @var{key} (and any value associated with it) from\n"
  446. "@var{table}. Uses @code{eqv?} for equality tests.")
  447. #define FUNC_NAME s_scm_hashv_remove_x
  448. {
  449. if (SCM_WEAK_TABLE_P (table))
  450. {
  451. scm_c_weak_table_remove_x (table, scm_ihashv (key, -1),
  452. assv_predicate, SCM_PACK (key));
  453. return SCM_UNSPECIFIED;
  454. }
  455. return scm_hash_fn_remove_x (table, key,
  456. (scm_t_hash_fn) scm_ihashv,
  457. (scm_t_assoc_fn) scm_sloppy_assv,
  458. 0);
  459. }
  460. #undef FUNC_NAME
  461. SCM_DEFINE (scm_hash_get_handle, "hash-get-handle", 2, 0, 0,
  462. (SCM table, SCM key),
  463. "This procedure returns the @code{(key . value)} pair from the\n"
  464. "hash table @var{table}. If @var{table} does not hold an\n"
  465. "associated value for @var{key}, @code{#f} is returned.\n"
  466. "Uses @code{equal?} for equality testing.")
  467. #define FUNC_NAME s_scm_hash_get_handle
  468. {
  469. return scm_hash_fn_get_handle (table, key,
  470. (scm_t_hash_fn) scm_ihash,
  471. (scm_t_assoc_fn) scm_sloppy_assoc,
  472. 0);
  473. }
  474. #undef FUNC_NAME
  475. SCM_DEFINE (scm_hash_create_handle_x, "hash-create-handle!", 3, 0, 0,
  476. (SCM table, SCM key, SCM init),
  477. "This function looks up @var{key} in @var{table} and returns its handle.\n"
  478. "If @var{key} is not already present, a new handle is created which\n"
  479. "associates @var{key} with @var{init}.")
  480. #define FUNC_NAME s_scm_hash_create_handle_x
  481. {
  482. return scm_hash_fn_create_handle_x (table, key, init,
  483. (scm_t_hash_fn) scm_ihash,
  484. (scm_t_assoc_fn) scm_sloppy_assoc,
  485. 0);
  486. }
  487. #undef FUNC_NAME
  488. static int
  489. assoc_predicate (SCM k, SCM v, void *closure)
  490. {
  491. return scm_is_true (scm_equal_p (k, SCM_PACK_POINTER (closure)));
  492. }
  493. SCM_DEFINE (scm_hash_ref, "hash-ref", 2, 1, 0,
  494. (SCM table, SCM key, SCM dflt),
  495. "Look up @var{key} in the hash table @var{table}, and return the\n"
  496. "value (if any) associated with it. If @var{key} is not found,\n"
  497. "return @var{dflt} (or @code{#f} if no @var{dflt} argument\n"
  498. "is supplied). Uses @code{equal?} for equality testing.")
  499. #define FUNC_NAME s_scm_hash_ref
  500. {
  501. if (SCM_UNBNDP (dflt))
  502. dflt = SCM_BOOL_F;
  503. if (SCM_WEAK_TABLE_P (table))
  504. return scm_c_weak_table_ref (table, scm_ihash (key, -1),
  505. assoc_predicate, SCM_PACK (key), dflt);
  506. return scm_hash_fn_ref (table, key, dflt,
  507. (scm_t_hash_fn) scm_ihash,
  508. (scm_t_assoc_fn) scm_sloppy_assoc,
  509. 0);
  510. }
  511. #undef FUNC_NAME
  512. SCM_DEFINE (scm_hash_set_x, "hash-set!", 3, 0, 0,
  513. (SCM table, SCM key, SCM val),
  514. "Find the entry in @var{table} associated with @var{key}, and\n"
  515. "store @var{val} there. Uses @code{equal?} for equality\n"
  516. "testing.")
  517. #define FUNC_NAME s_scm_hash_set_x
  518. {
  519. if (SCM_WEAK_TABLE_P (table))
  520. {
  521. scm_c_weak_table_put_x (table, scm_ihash (key, -1),
  522. assoc_predicate, SCM_PACK (key),
  523. key, val);
  524. return SCM_UNSPECIFIED;
  525. }
  526. return scm_hash_fn_set_x (table, key, val,
  527. (scm_t_hash_fn) scm_ihash,
  528. (scm_t_assoc_fn) scm_sloppy_assoc,
  529. 0);
  530. }
  531. #undef FUNC_NAME
  532. SCM_DEFINE (scm_hash_remove_x, "hash-remove!", 2, 0, 0,
  533. (SCM table, SCM key),
  534. "Remove @var{key} (and any value associated with it) from\n"
  535. "@var{table}. Uses @code{equal?} for equality tests.")
  536. #define FUNC_NAME s_scm_hash_remove_x
  537. {
  538. if (SCM_WEAK_TABLE_P (table))
  539. {
  540. scm_c_weak_table_remove_x (table, scm_ihash (key, -1),
  541. assoc_predicate, SCM_PACK (key));
  542. return SCM_UNSPECIFIED;
  543. }
  544. return scm_hash_fn_remove_x (table, key,
  545. (scm_t_hash_fn) scm_ihash,
  546. (scm_t_assoc_fn) scm_sloppy_assoc,
  547. 0);
  548. }
  549. #undef FUNC_NAME
  550. typedef struct scm_t_ihashx_closure
  551. {
  552. SCM hash;
  553. SCM assoc;
  554. SCM key;
  555. } scm_t_ihashx_closure;
  556. static unsigned long
  557. scm_ihashx (SCM obj, unsigned long n, void *arg)
  558. {
  559. SCM answer;
  560. scm_t_ihashx_closure *closure = (scm_t_ihashx_closure *) arg;
  561. answer = scm_call_2 (closure->hash, obj, scm_from_ulong (n));
  562. return scm_to_ulong (answer);
  563. }
  564. static SCM
  565. scm_sloppy_assx (SCM obj, SCM alist, void *arg)
  566. {
  567. scm_t_ihashx_closure *closure = (scm_t_ihashx_closure *) arg;
  568. return scm_call_2 (closure->assoc, obj, alist);
  569. }
  570. static int
  571. assx_predicate (SCM k, SCM v, void *closure)
  572. {
  573. scm_t_ihashx_closure *c = (scm_t_ihashx_closure *) closure;
  574. /* FIXME: The hashx interface is crazy. Hash tables have nothing to
  575. do with alists in principle. Instead of getting an assoc proc,
  576. hashx functions should use an equality predicate. Perhaps we can
  577. change this before 2.2, but until then, add a terrible, terrible
  578. hack. */
  579. return scm_is_true (scm_call_2 (c->assoc, c->key, scm_acons (k, v, SCM_EOL)));
  580. }
  581. SCM_DEFINE (scm_hashx_get_handle, "hashx-get-handle", 4, 0, 0,
  582. (SCM hash, SCM assoc, SCM table, SCM key),
  583. "This behaves the same way as the corresponding\n"
  584. "@code{-get-handle} function, but uses @var{hash} as a hash\n"
  585. "function and @var{assoc} to compare keys. @code{hash} must be\n"
  586. "a function that takes two arguments, a key to be hashed and a\n"
  587. "table size. @code{assoc} must be an associator function, like\n"
  588. "@code{assoc}, @code{assq} or @code{assv}.")
  589. #define FUNC_NAME s_scm_hashx_get_handle
  590. {
  591. scm_t_ihashx_closure closure;
  592. closure.hash = hash;
  593. closure.assoc = assoc;
  594. closure.key = key;
  595. return scm_hash_fn_get_handle (table, key, scm_ihashx, scm_sloppy_assx,
  596. (void *) &closure);
  597. }
  598. #undef FUNC_NAME
  599. SCM_DEFINE (scm_hashx_create_handle_x, "hashx-create-handle!", 5, 0, 0,
  600. (SCM hash, SCM assoc, SCM table, SCM key, SCM init),
  601. "This behaves the same way as the corresponding\n"
  602. "@code{-create-handle} function, but uses @var{hash} as a hash\n"
  603. "function and @var{assoc} to compare keys. @code{hash} must be\n"
  604. "a function that takes two arguments, a key to be hashed and a\n"
  605. "table size. @code{assoc} must be an associator function, like\n"
  606. "@code{assoc}, @code{assq} or @code{assv}.")
  607. #define FUNC_NAME s_scm_hashx_create_handle_x
  608. {
  609. scm_t_ihashx_closure closure;
  610. closure.hash = hash;
  611. closure.assoc = assoc;
  612. closure.key = key;
  613. return scm_hash_fn_create_handle_x (table, key, init, scm_ihashx,
  614. scm_sloppy_assx, (void *)&closure);
  615. }
  616. #undef FUNC_NAME
  617. SCM_DEFINE (scm_hashx_ref, "hashx-ref", 4, 1, 0,
  618. (SCM hash, SCM assoc, SCM table, SCM key, SCM dflt),
  619. "This behaves the same way as the corresponding @code{ref}\n"
  620. "function, but uses @var{hash} as a hash function and\n"
  621. "@var{assoc} to compare keys. @code{hash} must be a function\n"
  622. "that takes two arguments, a key to be hashed and a table size.\n"
  623. "@code{assoc} must be an associator function, like @code{assoc},\n"
  624. "@code{assq} or @code{assv}.\n"
  625. "\n"
  626. "By way of illustration, @code{hashq-ref table key} is\n"
  627. "equivalent to @code{hashx-ref hashq assq table key}.")
  628. #define FUNC_NAME s_scm_hashx_ref
  629. {
  630. scm_t_ihashx_closure closure;
  631. if (SCM_UNBNDP (dflt))
  632. dflt = SCM_BOOL_F;
  633. closure.hash = hash;
  634. closure.assoc = assoc;
  635. closure.key = key;
  636. if (SCM_WEAK_TABLE_P (table))
  637. {
  638. unsigned long h = scm_to_ulong (scm_call_2 (hash, key,
  639. scm_from_ulong (-1)));
  640. return scm_c_weak_table_ref (table, h, assx_predicate, &closure, dflt);
  641. }
  642. return scm_hash_fn_ref (table, key, dflt, scm_ihashx, scm_sloppy_assx,
  643. (void *)&closure);
  644. }
  645. #undef FUNC_NAME
  646. SCM_DEFINE (scm_hashx_set_x, "hashx-set!", 5, 0, 0,
  647. (SCM hash, SCM assoc, SCM table, SCM key, SCM val),
  648. "This behaves the same way as the corresponding @code{set!}\n"
  649. "function, but uses @var{hash} as a hash function and\n"
  650. "@var{assoc} to compare keys. @code{hash} must be a function\n"
  651. "that takes two arguments, a key to be hashed and a table size.\n"
  652. "@code{assoc} must be an associator function, like @code{assoc},\n"
  653. "@code{assq} or @code{assv}.\n"
  654. "\n"
  655. " By way of illustration, @code{hashq-set! table key} is\n"
  656. "equivalent to @code{hashx-set! hashq assq table key}.")
  657. #define FUNC_NAME s_scm_hashx_set_x
  658. {
  659. scm_t_ihashx_closure closure;
  660. closure.hash = hash;
  661. closure.assoc = assoc;
  662. closure.key = key;
  663. if (SCM_WEAK_TABLE_P (table))
  664. {
  665. unsigned long h = scm_to_ulong (scm_call_2 (hash, key,
  666. scm_from_ulong (-1)));
  667. scm_c_weak_table_put_x (table, h, assx_predicate, &closure, key, val);
  668. return SCM_UNSPECIFIED;
  669. }
  670. return scm_hash_fn_set_x (table, key, val, scm_ihashx, scm_sloppy_assx,
  671. (void *)&closure);
  672. }
  673. #undef FUNC_NAME
  674. SCM_DEFINE (scm_hashx_remove_x, "hashx-remove!", 4, 0, 0,
  675. (SCM hash, SCM assoc, SCM table, SCM obj),
  676. "This behaves the same way as the corresponding @code{remove!}\n"
  677. "function, but uses @var{hash} as a hash function and\n"
  678. "@var{assoc} to compare keys. @code{hash} must be a function\n"
  679. "that takes two arguments, a key to be hashed and a table size.\n"
  680. "@code{assoc} must be an associator function, like @code{assoc},\n"
  681. "@code{assq} or @code{assv}.\n"
  682. "\n"
  683. " By way of illustration, @code{hashq-remove! table key} is\n"
  684. "equivalent to @code{hashx-remove! hashq assq #f table key}.")
  685. #define FUNC_NAME s_scm_hashx_remove_x
  686. {
  687. scm_t_ihashx_closure closure;
  688. closure.hash = hash;
  689. closure.assoc = assoc;
  690. closure.key = obj;
  691. if (SCM_WEAK_TABLE_P (table))
  692. {
  693. unsigned long h = scm_to_ulong (scm_call_2 (hash, obj,
  694. scm_from_ulong (-1)));
  695. scm_c_weak_table_remove_x (table, h, assx_predicate, &closure);
  696. return SCM_UNSPECIFIED;
  697. }
  698. return scm_hash_fn_remove_x (table, obj, scm_ihashx, scm_sloppy_assx,
  699. (void *) &closure);
  700. }
  701. #undef FUNC_NAME
  702. /* Hash table iterators */
  703. SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0,
  704. (SCM proc, SCM init, SCM table),
  705. "An iterator over hash-table elements.\n"
  706. "Accumulates and returns a result by applying PROC successively.\n"
  707. "The arguments to PROC are \"(key value prior-result)\" where key\n"
  708. "and value are successive pairs from the hash table TABLE, and\n"
  709. "prior-result is either INIT (for the first application of PROC)\n"
  710. "or the return value of the previous application of PROC.\n"
  711. "For example, @code{(hash-fold acons '() tab)} will convert a hash\n"
  712. "table into an a-list of key-value pairs.")
  713. #define FUNC_NAME s_scm_hash_fold
  714. {
  715. SCM_VALIDATE_PROC (1, proc);
  716. if (SCM_WEAK_TABLE_P (table))
  717. return scm_weak_table_fold (proc, init, table);
  718. SCM_VALIDATE_HASHTABLE (3, table);
  719. return scm_internal_hash_fold ((scm_t_hash_fold_fn) scm_call_3,
  720. (void *) SCM_UNPACK (proc), init, table);
  721. }
  722. #undef FUNC_NAME
  723. static SCM
  724. for_each_proc (void *proc, SCM handle)
  725. {
  726. return scm_call_2 (SCM_PACK (proc), SCM_CAR (handle), SCM_CDR (handle));
  727. }
  728. SCM_DEFINE (scm_hash_for_each, "hash-for-each", 2, 0, 0,
  729. (SCM proc, SCM table),
  730. "An iterator over hash-table elements.\n"
  731. "Applies PROC successively on all hash table items.\n"
  732. "The arguments to PROC are \"(key value)\" where key\n"
  733. "and value are successive pairs from the hash table TABLE.")
  734. #define FUNC_NAME s_scm_hash_for_each
  735. {
  736. SCM_VALIDATE_PROC (1, proc);
  737. if (SCM_WEAK_TABLE_P (table))
  738. return scm_weak_table_for_each (proc, table);
  739. SCM_VALIDATE_HASHTABLE (2, table);
  740. scm_internal_hash_for_each_handle (for_each_proc,
  741. (void *) SCM_UNPACK (proc),
  742. table);
  743. return SCM_UNSPECIFIED;
  744. }
  745. #undef FUNC_NAME
  746. SCM_DEFINE (scm_hash_for_each_handle, "hash-for-each-handle", 2, 0, 0,
  747. (SCM proc, SCM table),
  748. "An iterator over hash-table elements.\n"
  749. "Applies PROC successively on all hash table handles.")
  750. #define FUNC_NAME s_scm_hash_for_each_handle
  751. {
  752. SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), proc, 1, FUNC_NAME);
  753. SCM_VALIDATE_HASHTABLE (2, table);
  754. scm_internal_hash_for_each_handle ((scm_t_hash_handle_fn) scm_call_1,
  755. (void *) SCM_UNPACK (proc),
  756. table);
  757. return SCM_UNSPECIFIED;
  758. }
  759. #undef FUNC_NAME
  760. static SCM
  761. map_proc (void *proc, SCM key, SCM data, SCM value)
  762. {
  763. return scm_cons (scm_call_2 (SCM_PACK (proc), key, data), value);
  764. }
  765. SCM_DEFINE (scm_hash_map_to_list, "hash-map->list", 2, 0, 0,
  766. (SCM proc, SCM table),
  767. "An iterator over hash-table elements.\n"
  768. "Accumulates and returns as a list the results of applying PROC successively.\n"
  769. "The arguments to PROC are \"(key value)\" where key\n"
  770. "and value are successive pairs from the hash table TABLE.")
  771. #define FUNC_NAME s_scm_hash_map_to_list
  772. {
  773. SCM_VALIDATE_PROC (1, proc);
  774. if (SCM_WEAK_TABLE_P (table))
  775. return scm_weak_table_map_to_list (proc, table);
  776. SCM_VALIDATE_HASHTABLE (2, table);
  777. return scm_internal_hash_fold (map_proc,
  778. (void *) SCM_UNPACK (proc),
  779. SCM_EOL,
  780. table);
  781. }
  782. #undef FUNC_NAME
  783. SCM
  784. scm_internal_hash_fold (scm_t_hash_fold_fn fn, void *closure,
  785. SCM init, SCM table)
  786. #define FUNC_NAME s_scm_hash_fold
  787. {
  788. long i, n;
  789. SCM buckets, result = init;
  790. if (SCM_WEAK_TABLE_P (table))
  791. return scm_c_weak_table_fold (fn, closure, init, table);
  792. SCM_VALIDATE_HASHTABLE (0, table);
  793. buckets = SCM_HASHTABLE_VECTOR (table);
  794. n = SCM_SIMPLE_VECTOR_LENGTH (buckets);
  795. for (i = 0; i < n; ++i)
  796. {
  797. SCM ls, handle;
  798. for (ls = SCM_SIMPLE_VECTOR_REF (buckets, i); !scm_is_null (ls);
  799. ls = SCM_CDR (ls))
  800. {
  801. handle = SCM_CAR (ls);
  802. result = fn (closure, SCM_CAR (handle), SCM_CDR (handle), result);
  803. }
  804. }
  805. return result;
  806. }
  807. #undef FUNC_NAME
  808. /* The following redundant code is here in order to be able to support
  809. hash-for-each-handle. An alternative would have been to replace
  810. this code and scm_internal_hash_fold above with a single
  811. scm_internal_hash_fold_handles, but we don't want to promote such
  812. an API. */
  813. void
  814. scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn, void *closure,
  815. SCM table)
  816. #define FUNC_NAME s_scm_hash_for_each
  817. {
  818. long i, n;
  819. SCM buckets;
  820. SCM_VALIDATE_HASHTABLE (0, table);
  821. buckets = SCM_HASHTABLE_VECTOR (table);
  822. n = SCM_SIMPLE_VECTOR_LENGTH (buckets);
  823. for (i = 0; i < n; ++i)
  824. {
  825. SCM ls = SCM_SIMPLE_VECTOR_REF (buckets, i), handle;
  826. while (!scm_is_null (ls))
  827. {
  828. if (!scm_is_pair (ls))
  829. SCM_WRONG_TYPE_ARG (SCM_ARG3, buckets);
  830. handle = SCM_CAR (ls);
  831. if (!scm_is_pair (handle))
  832. SCM_WRONG_TYPE_ARG (SCM_ARG3, buckets);
  833. fn (closure, handle);
  834. ls = SCM_CDR (ls);
  835. }
  836. }
  837. }
  838. #undef FUNC_NAME
  839. void
  840. scm_init_hashtab ()
  841. {
  842. #include "libguile/hashtab.x"
  843. }
  844. /*
  845. Local Variables:
  846. c-file-style: "gnu"
  847. End:
  848. */