hashtab.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /* Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2006,
  2. * 2008, 2009, 2010, 2011, 2012, 2013 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/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 ("#<hash-table ", port);
  142. scm_uintprint (SCM_UNPACK (exp), 16, port);
  143. scm_putc (' ', port);
  144. scm_uintprint (SCM_HASHTABLE_N_ITEMS (exp), 10, port);
  145. scm_putc ('/', port);
  146. scm_uintprint (SCM_SIMPLE_VECTOR_LENGTH (SCM_HASHTABLE_VECTOR (exp)),
  147. 10, port);
  148. scm_puts (">", port);
  149. }
  150. SCM
  151. scm_c_make_hash_table (unsigned long k)
  152. {
  153. return make_hash_table (k, "scm_c_make_hash_table");
  154. }
  155. SCM_DEFINE (scm_make_hash_table, "make-hash-table", 0, 1, 0,
  156. (SCM n),
  157. "Make a new abstract hash table object with minimum number of buckets @var{n}\n")
  158. #define FUNC_NAME s_scm_make_hash_table
  159. {
  160. return make_hash_table (SCM_UNBNDP (n) ? 0 : scm_to_ulong (n), FUNC_NAME);
  161. }
  162. #undef FUNC_NAME
  163. #define SCM_WEAK_TABLE_P(x) (scm_is_true (scm_weak_table_p (x)))
  164. SCM_DEFINE (scm_hash_table_p, "hash-table?", 1, 0, 0,
  165. (SCM obj),
  166. "Return @code{#t} if @var{obj} is an abstract hash table object.")
  167. #define FUNC_NAME s_scm_hash_table_p
  168. {
  169. return scm_from_bool (SCM_HASHTABLE_P (obj) || SCM_WEAK_TABLE_P (obj));
  170. }
  171. #undef FUNC_NAME
  172. /* Accessing hash table entries. */
  173. SCM
  174. scm_hash_fn_get_handle (SCM table, SCM obj,
  175. scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn,
  176. void * closure)
  177. #define FUNC_NAME "scm_hash_fn_get_handle"
  178. {
  179. unsigned long k;
  180. SCM buckets, h;
  181. SCM_VALIDATE_HASHTABLE (SCM_ARG1, table);
  182. buckets = SCM_HASHTABLE_VECTOR (table);
  183. if (SCM_SIMPLE_VECTOR_LENGTH (buckets) == 0)
  184. return SCM_BOOL_F;
  185. k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
  186. if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
  187. scm_out_of_range (FUNC_NAME, scm_from_ulong (k));
  188. h = assoc_fn (obj, SCM_SIMPLE_VECTOR_REF (buckets, k), closure);
  189. return h;
  190. }
  191. #undef FUNC_NAME
  192. SCM
  193. scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init,
  194. scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn,
  195. void * closure)
  196. #define FUNC_NAME "scm_hash_fn_create_handle_x"
  197. {
  198. unsigned long k;
  199. SCM buckets, it;
  200. SCM_VALIDATE_HASHTABLE (SCM_ARG1, table);
  201. buckets = SCM_HASHTABLE_VECTOR (table);
  202. if (SCM_SIMPLE_VECTOR_LENGTH (buckets) == 0)
  203. SCM_MISC_ERROR ("void hashtable", SCM_EOL);
  204. k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
  205. if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
  206. scm_out_of_range ("hash_fn_create_handle_x", scm_from_ulong (k));
  207. it = assoc_fn (obj, SCM_SIMPLE_VECTOR_REF (buckets, k), closure);
  208. if (scm_is_pair (it))
  209. return it;
  210. else if (scm_is_true (it))
  211. scm_wrong_type_arg_msg (NULL, 0, it, "a pair");
  212. else
  213. {
  214. SCM handle, new_bucket;
  215. handle = scm_cons (obj, init);
  216. new_bucket = scm_cons (handle, SCM_EOL);
  217. if (!scm_is_eq (SCM_HASHTABLE_VECTOR (table), buckets))
  218. {
  219. buckets = SCM_HASHTABLE_VECTOR (table);
  220. k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
  221. if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
  222. scm_out_of_range ("hash_fn_create_handle_x", scm_from_ulong (k));
  223. }
  224. SCM_SETCDR (new_bucket, SCM_SIMPLE_VECTOR_REF (buckets, k));
  225. SCM_SIMPLE_VECTOR_SET (buckets, k, new_bucket);
  226. SCM_HASHTABLE_INCREMENT (table);
  227. /* Maybe rehash the table. */
  228. if (SCM_HASHTABLE_N_ITEMS (table) < SCM_HASHTABLE_LOWER (table)
  229. || SCM_HASHTABLE_N_ITEMS (table) > SCM_HASHTABLE_UPPER (table))
  230. scm_i_rehash (table, hash_fn, closure, FUNC_NAME);
  231. return SCM_CAR (new_bucket);
  232. }
  233. }
  234. #undef FUNC_NAME
  235. SCM
  236. scm_hash_fn_ref (SCM table, SCM obj, SCM dflt,
  237. scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn,
  238. void *closure)
  239. {
  240. SCM it = scm_hash_fn_get_handle (table, obj, hash_fn, assoc_fn, closure);
  241. if (scm_is_pair (it))
  242. return SCM_CDR (it);
  243. else
  244. return dflt;
  245. }
  246. SCM
  247. scm_hash_fn_set_x (SCM table, SCM obj, SCM val,
  248. scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn,
  249. void *closure)
  250. {
  251. SCM pair;
  252. pair = scm_hash_fn_create_handle_x (table, obj, val,
  253. hash_fn, assoc_fn, closure);
  254. if (!scm_is_eq (SCM_CDR (pair), val))
  255. SCM_SETCDR (pair, val);
  256. return val;
  257. }
  258. SCM
  259. scm_hash_fn_remove_x (SCM table, SCM obj,
  260. scm_t_hash_fn hash_fn,
  261. scm_t_assoc_fn assoc_fn,
  262. void *closure)
  263. #define FUNC_NAME "hash_fn_remove_x"
  264. {
  265. unsigned long k;
  266. SCM buckets, h;
  267. SCM_VALIDATE_HASHTABLE (SCM_ARG1, table);
  268. buckets = SCM_HASHTABLE_VECTOR (table);
  269. if (SCM_SIMPLE_VECTOR_LENGTH (buckets) == 0)
  270. return SCM_EOL;
  271. k = hash_fn (obj, SCM_SIMPLE_VECTOR_LENGTH (buckets), closure);
  272. if (k >= SCM_SIMPLE_VECTOR_LENGTH (buckets))
  273. scm_out_of_range (FUNC_NAME, scm_from_ulong (k));
  274. h = assoc_fn (obj, SCM_SIMPLE_VECTOR_REF (buckets, k), closure);
  275. if (scm_is_true (h))
  276. {
  277. SCM_SIMPLE_VECTOR_SET
  278. (buckets, k, scm_delq_x (h, SCM_SIMPLE_VECTOR_REF (buckets, k)));
  279. SCM_HASHTABLE_DECREMENT (table);
  280. if (SCM_HASHTABLE_N_ITEMS (table) < SCM_HASHTABLE_LOWER (table))
  281. scm_i_rehash (table, hash_fn, closure, FUNC_NAME);
  282. }
  283. return h;
  284. }
  285. #undef FUNC_NAME
  286. SCM_DEFINE (scm_hash_clear_x, "hash-clear!", 1, 0, 0,
  287. (SCM table),
  288. "Remove all items from @var{table} (without triggering a resize).")
  289. #define FUNC_NAME s_scm_hash_clear_x
  290. {
  291. if (SCM_WEAK_TABLE_P (table))
  292. {
  293. scm_weak_table_clear_x (table);
  294. return SCM_UNSPECIFIED;
  295. }
  296. SCM_VALIDATE_HASHTABLE (SCM_ARG1, table);
  297. scm_vector_fill_x (SCM_HASHTABLE_VECTOR (table), SCM_EOL);
  298. SCM_SET_HASHTABLE_N_ITEMS (table, 0);
  299. return SCM_UNSPECIFIED;
  300. }
  301. #undef FUNC_NAME
  302. SCM_DEFINE (scm_hashq_get_handle, "hashq-get-handle", 2, 0, 0,
  303. (SCM table, SCM key),
  304. "This procedure returns the @code{(key . value)} pair from the\n"
  305. "hash table @var{table}. If @var{table} does not hold an\n"
  306. "associated value for @var{key}, @code{#f} is returned.\n"
  307. "Uses @code{eq?} for equality testing.")
  308. #define FUNC_NAME s_scm_hashq_get_handle
  309. {
  310. return scm_hash_fn_get_handle (table, key,
  311. (scm_t_hash_fn) scm_ihashq,
  312. (scm_t_assoc_fn) scm_sloppy_assq,
  313. 0);
  314. }
  315. #undef FUNC_NAME
  316. SCM_DEFINE (scm_hashq_create_handle_x, "hashq-create-handle!", 3, 0, 0,
  317. (SCM table, SCM key, SCM init),
  318. "This function looks up @var{key} in @var{table} and returns its handle.\n"
  319. "If @var{key} is not already present, a new handle is created which\n"
  320. "associates @var{key} with @var{init}.")
  321. #define FUNC_NAME s_scm_hashq_create_handle_x
  322. {
  323. return scm_hash_fn_create_handle_x (table, key, init,
  324. (scm_t_hash_fn) scm_ihashq,
  325. (scm_t_assoc_fn) scm_sloppy_assq,
  326. 0);
  327. }
  328. #undef FUNC_NAME
  329. SCM_DEFINE (scm_hashq_ref, "hashq-ref", 2, 1, 0,
  330. (SCM table, SCM key, SCM dflt),
  331. "Look up @var{key} in the hash table @var{table}, and return the\n"
  332. "value (if any) associated with it. If @var{key} is not found,\n"
  333. "return @var{dflt} (or @code{#f} if no @var{dflt} argument\n"
  334. "is supplied). Uses @code{eq?} for equality testing.")
  335. #define FUNC_NAME s_scm_hashq_ref
  336. {
  337. if (SCM_UNBNDP (dflt))
  338. dflt = SCM_BOOL_F;
  339. if (SCM_WEAK_TABLE_P (table))
  340. return scm_weak_table_refq (table, key, dflt);
  341. return scm_hash_fn_ref (table, key, dflt,
  342. (scm_t_hash_fn) scm_ihashq,
  343. (scm_t_assoc_fn) scm_sloppy_assq,
  344. 0);
  345. }
  346. #undef FUNC_NAME
  347. SCM_DEFINE (scm_hashq_set_x, "hashq-set!", 3, 0, 0,
  348. (SCM table, SCM key, SCM val),
  349. "Find the entry in @var{table} associated with @var{key}, and\n"
  350. "store @var{val} there. Uses @code{eq?} for equality testing.")
  351. #define FUNC_NAME s_scm_hashq_set_x
  352. {
  353. if (SCM_WEAK_TABLE_P (table))
  354. {
  355. scm_weak_table_putq_x (table, key, val);
  356. return val;
  357. }
  358. return scm_hash_fn_set_x (table, key, val,
  359. (scm_t_hash_fn) scm_ihashq,
  360. (scm_t_assoc_fn) scm_sloppy_assq,
  361. 0);
  362. }
  363. #undef FUNC_NAME
  364. SCM_DEFINE (scm_hashq_remove_x, "hashq-remove!", 2, 0, 0,
  365. (SCM table, SCM key),
  366. "Remove @var{key} (and any value associated with it) from\n"
  367. "@var{table}. Uses @code{eq?} for equality tests.")
  368. #define FUNC_NAME s_scm_hashq_remove_x
  369. {
  370. if (SCM_WEAK_TABLE_P (table))
  371. {
  372. scm_weak_table_remq_x (table, key);
  373. /* This return value is for historical compatibility with
  374. hash-remove!, which returns either the "handle" corresponding
  375. to the entry, or #f. Since weak tables don't have handles, we
  376. have to return #f. */
  377. return SCM_BOOL_F;
  378. }
  379. return scm_hash_fn_remove_x (table, key,
  380. (scm_t_hash_fn) scm_ihashq,
  381. (scm_t_assoc_fn) scm_sloppy_assq,
  382. 0);
  383. }
  384. #undef FUNC_NAME
  385. SCM_DEFINE (scm_hashv_get_handle, "hashv-get-handle", 2, 0, 0,
  386. (SCM table, SCM key),
  387. "This procedure returns the @code{(key . value)} pair from the\n"
  388. "hash table @var{table}. If @var{table} does not hold an\n"
  389. "associated value for @var{key}, @code{#f} is returned.\n"
  390. "Uses @code{eqv?} for equality testing.")
  391. #define FUNC_NAME s_scm_hashv_get_handle
  392. {
  393. return scm_hash_fn_get_handle (table, key,
  394. (scm_t_hash_fn) scm_ihashv,
  395. (scm_t_assoc_fn) scm_sloppy_assv,
  396. 0);
  397. }
  398. #undef FUNC_NAME
  399. SCM_DEFINE (scm_hashv_create_handle_x, "hashv-create-handle!", 3, 0, 0,
  400. (SCM table, SCM key, SCM init),
  401. "This function looks up @var{key} in @var{table} and returns its handle.\n"
  402. "If @var{key} is not already present, a new handle is created which\n"
  403. "associates @var{key} with @var{init}.")
  404. #define FUNC_NAME s_scm_hashv_create_handle_x
  405. {
  406. return scm_hash_fn_create_handle_x (table, key, init,
  407. (scm_t_hash_fn) scm_ihashv,
  408. (scm_t_assoc_fn) scm_sloppy_assv,
  409. 0);
  410. }
  411. #undef FUNC_NAME
  412. static int
  413. assv_predicate (SCM k, SCM v, void *closure)
  414. {
  415. return scm_is_true (scm_eqv_p (k, SCM_PACK_POINTER (closure)));
  416. }
  417. SCM_DEFINE (scm_hashv_ref, "hashv-ref", 2, 1, 0,
  418. (SCM table, SCM key, SCM dflt),
  419. "Look up @var{key} in the hash table @var{table}, and return the\n"
  420. "value (if any) associated with it. If @var{key} is not found,\n"
  421. "return @var{dflt} (or @code{#f} if no @var{dflt} argument\n"
  422. "is supplied). Uses @code{eqv?} for equality testing.")
  423. #define FUNC_NAME s_scm_hashv_ref
  424. {
  425. if (SCM_UNBNDP (dflt))
  426. dflt = SCM_BOOL_F;
  427. if (SCM_WEAK_TABLE_P (table))
  428. return scm_c_weak_table_ref (table, scm_ihashv (key, -1),
  429. assv_predicate,
  430. (void *) SCM_UNPACK (key), dflt);
  431. return scm_hash_fn_ref (table, key, dflt,
  432. (scm_t_hash_fn) scm_ihashv,
  433. (scm_t_assoc_fn) scm_sloppy_assv,
  434. 0);
  435. }
  436. #undef FUNC_NAME
  437. SCM_DEFINE (scm_hashv_set_x, "hashv-set!", 3, 0, 0,
  438. (SCM table, SCM key, SCM val),
  439. "Find the entry in @var{table} associated with @var{key}, and\n"
  440. "store @var{value} there. Uses @code{eqv?} for equality testing.")
  441. #define FUNC_NAME s_scm_hashv_set_x
  442. {
  443. if (SCM_WEAK_TABLE_P (table))
  444. {
  445. scm_c_weak_table_put_x (table, scm_ihashv (key, -1),
  446. assv_predicate, (void *) SCM_UNPACK (key),
  447. key, val);
  448. return val;
  449. }
  450. return scm_hash_fn_set_x (table, key, val,
  451. (scm_t_hash_fn) scm_ihashv,
  452. (scm_t_assoc_fn) scm_sloppy_assv,
  453. 0);
  454. }
  455. #undef FUNC_NAME
  456. SCM_DEFINE (scm_hashv_remove_x, "hashv-remove!", 2, 0, 0,
  457. (SCM table, SCM key),
  458. "Remove @var{key} (and any value associated with it) from\n"
  459. "@var{table}. Uses @code{eqv?} for equality tests.")
  460. #define FUNC_NAME s_scm_hashv_remove_x
  461. {
  462. if (SCM_WEAK_TABLE_P (table))
  463. {
  464. scm_c_weak_table_remove_x (table, scm_ihashv (key, -1),
  465. assv_predicate, (void *) SCM_UNPACK (key));
  466. /* See note in hashq-remove!. */
  467. return SCM_BOOL_F;
  468. }
  469. return scm_hash_fn_remove_x (table, key,
  470. (scm_t_hash_fn) scm_ihashv,
  471. (scm_t_assoc_fn) scm_sloppy_assv,
  472. 0);
  473. }
  474. #undef FUNC_NAME
  475. SCM_DEFINE (scm_hash_get_handle, "hash-get-handle", 2, 0, 0,
  476. (SCM table, SCM key),
  477. "This procedure returns the @code{(key . value)} pair from the\n"
  478. "hash table @var{table}. If @var{table} does not hold an\n"
  479. "associated value for @var{key}, @code{#f} is returned.\n"
  480. "Uses @code{equal?} for equality testing.")
  481. #define FUNC_NAME s_scm_hash_get_handle
  482. {
  483. return scm_hash_fn_get_handle (table, key,
  484. (scm_t_hash_fn) scm_ihash,
  485. (scm_t_assoc_fn) scm_sloppy_assoc,
  486. 0);
  487. }
  488. #undef FUNC_NAME
  489. SCM_DEFINE (scm_hash_create_handle_x, "hash-create-handle!", 3, 0, 0,
  490. (SCM table, SCM key, SCM init),
  491. "This function looks up @var{key} in @var{table} and returns its handle.\n"
  492. "If @var{key} is not already present, a new handle is created which\n"
  493. "associates @var{key} with @var{init}.")
  494. #define FUNC_NAME s_scm_hash_create_handle_x
  495. {
  496. return scm_hash_fn_create_handle_x (table, key, init,
  497. (scm_t_hash_fn) scm_ihash,
  498. (scm_t_assoc_fn) scm_sloppy_assoc,
  499. 0);
  500. }
  501. #undef FUNC_NAME
  502. static int
  503. assoc_predicate (SCM k, SCM v, void *closure)
  504. {
  505. return scm_is_true (scm_equal_p (k, SCM_PACK_POINTER (closure)));
  506. }
  507. SCM_DEFINE (scm_hash_ref, "hash-ref", 2, 1, 0,
  508. (SCM table, SCM key, SCM dflt),
  509. "Look up @var{key} in the hash table @var{table}, and return the\n"
  510. "value (if any) associated with it. If @var{key} is not found,\n"
  511. "return @var{dflt} (or @code{#f} if no @var{dflt} argument\n"
  512. "is supplied). Uses @code{equal?} for equality testing.")
  513. #define FUNC_NAME s_scm_hash_ref
  514. {
  515. if (SCM_UNBNDP (dflt))
  516. dflt = SCM_BOOL_F;
  517. if (SCM_WEAK_TABLE_P (table))
  518. return scm_c_weak_table_ref (table, scm_ihash (key, -1),
  519. assoc_predicate,
  520. (void *) SCM_UNPACK (key), dflt);
  521. return scm_hash_fn_ref (table, key, dflt,
  522. (scm_t_hash_fn) scm_ihash,
  523. (scm_t_assoc_fn) scm_sloppy_assoc,
  524. 0);
  525. }
  526. #undef FUNC_NAME
  527. SCM_DEFINE (scm_hash_set_x, "hash-set!", 3, 0, 0,
  528. (SCM table, SCM key, SCM val),
  529. "Find the entry in @var{table} associated with @var{key}, and\n"
  530. "store @var{val} there. Uses @code{equal?} for equality\n"
  531. "testing.")
  532. #define FUNC_NAME s_scm_hash_set_x
  533. {
  534. if (SCM_WEAK_TABLE_P (table))
  535. {
  536. scm_c_weak_table_put_x (table, scm_ihash (key, -1),
  537. assoc_predicate, (void *) SCM_UNPACK (key),
  538. key, val);
  539. return val;
  540. }
  541. return scm_hash_fn_set_x (table, key, val,
  542. (scm_t_hash_fn) scm_ihash,
  543. (scm_t_assoc_fn) scm_sloppy_assoc,
  544. 0);
  545. }
  546. #undef FUNC_NAME
  547. SCM_DEFINE (scm_hash_remove_x, "hash-remove!", 2, 0, 0,
  548. (SCM table, SCM key),
  549. "Remove @var{key} (and any value associated with it) from\n"
  550. "@var{table}. Uses @code{equal?} for equality tests.")
  551. #define FUNC_NAME s_scm_hash_remove_x
  552. {
  553. if (SCM_WEAK_TABLE_P (table))
  554. {
  555. scm_c_weak_table_remove_x (table, scm_ihash (key, -1),
  556. assoc_predicate, (void *) SCM_UNPACK (key));
  557. /* See note in hashq-remove!. */
  558. return SCM_BOOL_F;
  559. }
  560. return scm_hash_fn_remove_x (table, key,
  561. (scm_t_hash_fn) scm_ihash,
  562. (scm_t_assoc_fn) scm_sloppy_assoc,
  563. 0);
  564. }
  565. #undef FUNC_NAME
  566. typedef struct scm_t_ihashx_closure
  567. {
  568. SCM hash;
  569. SCM assoc;
  570. SCM key;
  571. } scm_t_ihashx_closure;
  572. static unsigned long
  573. scm_ihashx (SCM obj, unsigned long n, void *arg)
  574. {
  575. SCM answer;
  576. scm_t_ihashx_closure *closure = (scm_t_ihashx_closure *) arg;
  577. answer = scm_call_2 (closure->hash, obj, scm_from_ulong (n));
  578. return scm_to_ulong (answer);
  579. }
  580. static SCM
  581. scm_sloppy_assx (SCM obj, SCM alist, void *arg)
  582. {
  583. scm_t_ihashx_closure *closure = (scm_t_ihashx_closure *) arg;
  584. return scm_call_2 (closure->assoc, obj, alist);
  585. }
  586. static int
  587. assx_predicate (SCM k, SCM v, void *closure)
  588. {
  589. scm_t_ihashx_closure *c = (scm_t_ihashx_closure *) closure;
  590. /* FIXME: The hashx interface is crazy. Hash tables have nothing to
  591. do with alists in principle. Instead of getting an assoc proc,
  592. hashx functions should use an equality predicate. Perhaps we can
  593. change this before 2.2, but until then, add a terrible, terrible
  594. hack. */
  595. return scm_is_true (scm_call_2 (c->assoc, c->key, scm_acons (k, v, SCM_EOL)));
  596. }
  597. SCM_DEFINE (scm_hashx_get_handle, "hashx-get-handle", 4, 0, 0,
  598. (SCM hash, SCM assoc, SCM table, SCM key),
  599. "This behaves the same way as the corresponding\n"
  600. "@code{-get-handle} function, but uses @var{hash} as a hash\n"
  601. "function and @var{assoc} to compare keys. @code{hash} must be\n"
  602. "a function that takes two arguments, a key to be hashed and a\n"
  603. "table size. @code{assoc} must be an associator function, like\n"
  604. "@code{assoc}, @code{assq} or @code{assv}.")
  605. #define FUNC_NAME s_scm_hashx_get_handle
  606. {
  607. scm_t_ihashx_closure closure;
  608. closure.hash = hash;
  609. closure.assoc = assoc;
  610. closure.key = key;
  611. return scm_hash_fn_get_handle (table, key, scm_ihashx, scm_sloppy_assx,
  612. (void *) &closure);
  613. }
  614. #undef FUNC_NAME
  615. SCM_DEFINE (scm_hashx_create_handle_x, "hashx-create-handle!", 5, 0, 0,
  616. (SCM hash, SCM assoc, SCM table, SCM key, SCM init),
  617. "This behaves the same way as the corresponding\n"
  618. "@code{-create-handle} function, but uses @var{hash} as a hash\n"
  619. "function and @var{assoc} to compare keys. @code{hash} must be\n"
  620. "a function that takes two arguments, a key to be hashed and a\n"
  621. "table size. @code{assoc} must be an associator function, like\n"
  622. "@code{assoc}, @code{assq} or @code{assv}.")
  623. #define FUNC_NAME s_scm_hashx_create_handle_x
  624. {
  625. scm_t_ihashx_closure closure;
  626. closure.hash = hash;
  627. closure.assoc = assoc;
  628. closure.key = key;
  629. return scm_hash_fn_create_handle_x (table, key, init, scm_ihashx,
  630. scm_sloppy_assx, (void *)&closure);
  631. }
  632. #undef FUNC_NAME
  633. SCM_DEFINE (scm_hashx_ref, "hashx-ref", 4, 1, 0,
  634. (SCM hash, SCM assoc, SCM table, SCM key, SCM dflt),
  635. "This behaves the same way as the corresponding @code{ref}\n"
  636. "function, but uses @var{hash} as a hash function and\n"
  637. "@var{assoc} to compare keys. @code{hash} must be a function\n"
  638. "that takes two arguments, a key to be hashed and a table size.\n"
  639. "@code{assoc} must be an associator function, like @code{assoc},\n"
  640. "@code{assq} or @code{assv}.\n"
  641. "\n"
  642. "By way of illustration, @code{hashq-ref table key} is\n"
  643. "equivalent to @code{hashx-ref hashq assq table key}.")
  644. #define FUNC_NAME s_scm_hashx_ref
  645. {
  646. scm_t_ihashx_closure closure;
  647. if (SCM_UNBNDP (dflt))
  648. dflt = SCM_BOOL_F;
  649. closure.hash = hash;
  650. closure.assoc = assoc;
  651. closure.key = key;
  652. if (SCM_WEAK_TABLE_P (table))
  653. {
  654. unsigned long h = scm_to_ulong (scm_call_2 (hash, key,
  655. scm_from_ulong (-1)));
  656. return scm_c_weak_table_ref (table, h, assx_predicate, &closure, dflt);
  657. }
  658. return scm_hash_fn_ref (table, key, dflt, scm_ihashx, scm_sloppy_assx,
  659. (void *)&closure);
  660. }
  661. #undef FUNC_NAME
  662. SCM_DEFINE (scm_hashx_set_x, "hashx-set!", 5, 0, 0,
  663. (SCM hash, SCM assoc, SCM table, SCM key, SCM val),
  664. "This behaves the same way as the corresponding @code{set!}\n"
  665. "function, but uses @var{hash} as a hash function and\n"
  666. "@var{assoc} to compare keys. @code{hash} must be a function\n"
  667. "that takes two arguments, a key to be hashed and a table size.\n"
  668. "@code{assoc} must be an associator function, like @code{assoc},\n"
  669. "@code{assq} or @code{assv}.\n"
  670. "\n"
  671. " By way of illustration, @code{hashq-set! table key} is\n"
  672. "equivalent to @code{hashx-set! hashq assq table key}.")
  673. #define FUNC_NAME s_scm_hashx_set_x
  674. {
  675. scm_t_ihashx_closure closure;
  676. closure.hash = hash;
  677. closure.assoc = assoc;
  678. closure.key = key;
  679. if (SCM_WEAK_TABLE_P (table))
  680. {
  681. unsigned long h = scm_to_ulong (scm_call_2 (hash, key,
  682. scm_from_ulong (-1)));
  683. scm_c_weak_table_put_x (table, h, assx_predicate, &closure, key, val);
  684. return val;
  685. }
  686. return scm_hash_fn_set_x (table, key, val, scm_ihashx, scm_sloppy_assx,
  687. (void *)&closure);
  688. }
  689. #undef FUNC_NAME
  690. SCM_DEFINE (scm_hashx_remove_x, "hashx-remove!", 4, 0, 0,
  691. (SCM hash, SCM assoc, SCM table, SCM obj),
  692. "This behaves the same way as the corresponding @code{remove!}\n"
  693. "function, but uses @var{hash} as a hash function and\n"
  694. "@var{assoc} to compare keys. @code{hash} must be a function\n"
  695. "that takes two arguments, a key to be hashed and a table size.\n"
  696. "@code{assoc} must be an associator function, like @code{assoc},\n"
  697. "@code{assq} or @code{assv}.\n"
  698. "\n"
  699. " By way of illustration, @code{hashq-remove! table key} is\n"
  700. "equivalent to @code{hashx-remove! hashq assq #f table key}.")
  701. #define FUNC_NAME s_scm_hashx_remove_x
  702. {
  703. scm_t_ihashx_closure closure;
  704. closure.hash = hash;
  705. closure.assoc = assoc;
  706. closure.key = obj;
  707. if (SCM_WEAK_TABLE_P (table))
  708. {
  709. unsigned long h = scm_to_ulong (scm_call_2 (hash, obj,
  710. scm_from_ulong (-1)));
  711. scm_c_weak_table_remove_x (table, h, assx_predicate, &closure);
  712. /* See note in hashq-remove!. */
  713. return SCM_BOOL_F;
  714. }
  715. return scm_hash_fn_remove_x (table, obj, scm_ihashx, scm_sloppy_assx,
  716. (void *) &closure);
  717. }
  718. #undef FUNC_NAME
  719. /* Hash table iterators */
  720. SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0,
  721. (SCM proc, SCM init, SCM table),
  722. "An iterator over hash-table elements.\n"
  723. "Accumulates and returns a result by applying PROC successively.\n"
  724. "The arguments to PROC are \"(key value prior-result)\" where key\n"
  725. "and value are successive pairs from the hash table TABLE, and\n"
  726. "prior-result is either INIT (for the first application of PROC)\n"
  727. "or the return value of the previous application of PROC.\n"
  728. "For example, @code{(hash-fold acons '() tab)} will convert a hash\n"
  729. "table into an a-list of key-value pairs.")
  730. #define FUNC_NAME s_scm_hash_fold
  731. {
  732. SCM_VALIDATE_PROC (1, proc);
  733. if (SCM_WEAK_TABLE_P (table))
  734. return scm_weak_table_fold (proc, init, table);
  735. SCM_VALIDATE_HASHTABLE (3, table);
  736. return scm_internal_hash_fold ((scm_t_hash_fold_fn) scm_call_3,
  737. (void *) SCM_UNPACK (proc), init, table);
  738. }
  739. #undef FUNC_NAME
  740. static SCM
  741. for_each_proc (void *proc, SCM handle)
  742. {
  743. return scm_call_2 (SCM_PACK (proc), SCM_CAR (handle), SCM_CDR (handle));
  744. }
  745. SCM_DEFINE (scm_hash_for_each, "hash-for-each", 2, 0, 0,
  746. (SCM proc, SCM table),
  747. "An iterator over hash-table elements.\n"
  748. "Applies PROC successively on all hash table items.\n"
  749. "The arguments to PROC are \"(key value)\" where key\n"
  750. "and value are successive pairs from the hash table TABLE.")
  751. #define FUNC_NAME s_scm_hash_for_each
  752. {
  753. SCM_VALIDATE_PROC (1, proc);
  754. if (SCM_WEAK_TABLE_P (table))
  755. {
  756. scm_weak_table_for_each (proc, table);
  757. return SCM_UNSPECIFIED;
  758. }
  759. SCM_VALIDATE_HASHTABLE (2, table);
  760. scm_internal_hash_for_each_handle (for_each_proc,
  761. (void *) SCM_UNPACK (proc),
  762. table);
  763. return SCM_UNSPECIFIED;
  764. }
  765. #undef FUNC_NAME
  766. SCM_DEFINE (scm_hash_for_each_handle, "hash-for-each-handle", 2, 0, 0,
  767. (SCM proc, SCM table),
  768. "An iterator over hash-table elements.\n"
  769. "Applies PROC successively on all hash table handles.")
  770. #define FUNC_NAME s_scm_hash_for_each_handle
  771. {
  772. SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), proc, 1, FUNC_NAME);
  773. SCM_VALIDATE_HASHTABLE (2, table);
  774. scm_internal_hash_for_each_handle ((scm_t_hash_handle_fn) scm_call_1,
  775. (void *) SCM_UNPACK (proc),
  776. table);
  777. return SCM_UNSPECIFIED;
  778. }
  779. #undef FUNC_NAME
  780. static SCM
  781. map_proc (void *proc, SCM key, SCM data, SCM value)
  782. {
  783. return scm_cons (scm_call_2 (SCM_PACK (proc), key, data), value);
  784. }
  785. SCM_DEFINE (scm_hash_map_to_list, "hash-map->list", 2, 0, 0,
  786. (SCM proc, SCM table),
  787. "An iterator over hash-table elements.\n"
  788. "Accumulates and returns as a list the results of applying PROC successively.\n"
  789. "The arguments to PROC are \"(key value)\" where key\n"
  790. "and value are successive pairs from the hash table TABLE.")
  791. #define FUNC_NAME s_scm_hash_map_to_list
  792. {
  793. SCM_VALIDATE_PROC (1, proc);
  794. if (SCM_WEAK_TABLE_P (table))
  795. return scm_weak_table_map_to_list (proc, table);
  796. SCM_VALIDATE_HASHTABLE (2, table);
  797. return scm_internal_hash_fold (map_proc,
  798. (void *) SCM_UNPACK (proc),
  799. SCM_EOL,
  800. table);
  801. }
  802. #undef FUNC_NAME
  803. static SCM
  804. count_proc (void *pred, SCM key, SCM data, SCM value)
  805. {
  806. if (scm_is_false (scm_call_2 (SCM_PACK (pred), key, data)))
  807. return value;
  808. else
  809. return scm_oneplus(value);
  810. }
  811. SCM_DEFINE (scm_hash_count, "hash-count", 2, 0, 0,
  812. (SCM pred, SCM table),
  813. "Return the number of elements in the given hash TABLE that\n"
  814. "cause `(PRED KEY VALUE)' to return true. To quickly determine\n"
  815. "the total number of elements, use `(const #t)' for PRED.")
  816. #define FUNC_NAME s_scm_hash_count
  817. {
  818. SCM init;
  819. SCM_VALIDATE_PROC (1, pred);
  820. SCM_VALIDATE_HASHTABLE (2, table);
  821. init = scm_from_int (0);
  822. return scm_internal_hash_fold ((scm_t_hash_fold_fn) count_proc,
  823. (void *) SCM_UNPACK (pred), init, table);
  824. }
  825. #undef FUNC_NAME
  826. SCM
  827. scm_internal_hash_fold (scm_t_hash_fold_fn fn, void *closure,
  828. SCM init, SCM table)
  829. #define FUNC_NAME s_scm_hash_fold
  830. {
  831. long i, n;
  832. SCM buckets, result = init;
  833. if (SCM_WEAK_TABLE_P (table))
  834. return scm_c_weak_table_fold (fn, closure, init, table);
  835. SCM_VALIDATE_HASHTABLE (0, table);
  836. buckets = SCM_HASHTABLE_VECTOR (table);
  837. n = SCM_SIMPLE_VECTOR_LENGTH (buckets);
  838. for (i = 0; i < n; ++i)
  839. {
  840. SCM ls, handle;
  841. for (ls = SCM_SIMPLE_VECTOR_REF (buckets, i); !scm_is_null (ls);
  842. ls = SCM_CDR (ls))
  843. {
  844. handle = SCM_CAR (ls);
  845. result = fn (closure, SCM_CAR (handle), SCM_CDR (handle), result);
  846. }
  847. }
  848. return result;
  849. }
  850. #undef FUNC_NAME
  851. /* The following redundant code is here in order to be able to support
  852. hash-for-each-handle. An alternative would have been to replace
  853. this code and scm_internal_hash_fold above with a single
  854. scm_internal_hash_fold_handles, but we don't want to promote such
  855. an API. */
  856. void
  857. scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn, void *closure,
  858. SCM table)
  859. #define FUNC_NAME s_scm_hash_for_each
  860. {
  861. long i, n;
  862. SCM buckets;
  863. SCM_VALIDATE_HASHTABLE (0, table);
  864. buckets = SCM_HASHTABLE_VECTOR (table);
  865. n = SCM_SIMPLE_VECTOR_LENGTH (buckets);
  866. for (i = 0; i < n; ++i)
  867. {
  868. SCM ls = SCM_SIMPLE_VECTOR_REF (buckets, i), handle;
  869. while (!scm_is_null (ls))
  870. {
  871. if (!scm_is_pair (ls))
  872. SCM_WRONG_TYPE_ARG (SCM_ARG3, buckets);
  873. handle = SCM_CAR (ls);
  874. if (!scm_is_pair (handle))
  875. SCM_WRONG_TYPE_ARG (SCM_ARG3, buckets);
  876. fn (closure, handle);
  877. ls = SCM_CDR (ls);
  878. }
  879. }
  880. }
  881. #undef FUNC_NAME
  882. void
  883. scm_init_hashtab ()
  884. {
  885. #include "libguile/hashtab.x"
  886. }
  887. /*
  888. Local Variables:
  889. c-file-style: "gnu"
  890. End:
  891. */