hashtab.c 30 KB

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