alist.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001, 2004, 2006, 2008 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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but 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 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/eq.h"
  22. #include "libguile/list.h"
  23. #include "libguile/lang.h"
  24. #include "libguile/validate.h"
  25. #include "libguile/pairs.h"
  26. #include "libguile/alist.h"
  27. SCM_DEFINE (scm_acons, "acons", 3, 0, 0,
  28. (SCM key, SCM value, SCM alist),
  29. "Add a new key-value pair to @var{alist}. A new pair is\n"
  30. "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
  31. "pair is consed onto @var{alist}, and the new list is returned. This\n"
  32. "function is @emph{not} destructive; @var{alist} is not modified.")
  33. #define FUNC_NAME s_scm_acons
  34. {
  35. return scm_cell (SCM_UNPACK (scm_cell (SCM_UNPACK (key),
  36. SCM_UNPACK (value))),
  37. SCM_UNPACK (alist));
  38. }
  39. #undef FUNC_NAME
  40. SCM_DEFINE (scm_sloppy_assq, "sloppy-assq", 2, 0, 0,
  41. (SCM key, SCM alist),
  42. "Behaves like @code{assq} but does not do any error checking.\n"
  43. "Recommended only for use in Guile internals.")
  44. #define FUNC_NAME s_scm_sloppy_assq
  45. {
  46. for (; scm_is_pair (alist); alist = SCM_CDR (alist))
  47. {
  48. SCM tmp = SCM_CAR (alist);
  49. if (scm_is_pair (tmp) && scm_is_eq (SCM_CAR (tmp), key))
  50. return tmp;
  51. }
  52. return SCM_BOOL_F;
  53. }
  54. #undef FUNC_NAME
  55. SCM_DEFINE (scm_sloppy_assv, "sloppy-assv", 2, 0, 0,
  56. (SCM key, SCM alist),
  57. "Behaves like @code{assv} but does not do any error checking.\n"
  58. "Recommended only for use in Guile internals.")
  59. #define FUNC_NAME s_scm_sloppy_assv
  60. {
  61. for (; scm_is_pair (alist); alist = SCM_CDR (alist))
  62. {
  63. SCM tmp = SCM_CAR (alist);
  64. if (scm_is_pair (tmp)
  65. && scm_is_true (scm_eqv_p (SCM_CAR (tmp), key)))
  66. return tmp;
  67. }
  68. return SCM_BOOL_F;
  69. }
  70. #undef FUNC_NAME
  71. SCM_DEFINE (scm_sloppy_assoc, "sloppy-assoc", 2, 0, 0,
  72. (SCM key, SCM alist),
  73. "Behaves like @code{assoc} but does not do any error checking.\n"
  74. "Recommended only for use in Guile internals.")
  75. #define FUNC_NAME s_scm_sloppy_assoc
  76. {
  77. for (; scm_is_pair (alist); alist = SCM_CDR (alist))
  78. {
  79. SCM tmp = SCM_CAR (alist);
  80. if (scm_is_pair (tmp)
  81. && scm_is_true (scm_equal_p (SCM_CAR (tmp), key)))
  82. return tmp;
  83. }
  84. return SCM_BOOL_F;
  85. }
  86. #undef FUNC_NAME
  87. SCM_DEFINE (scm_assq, "assq", 2, 0, 0,
  88. (SCM key, SCM alist),
  89. "@deffnx {Scheme Procedure} assv key alist\n"
  90. "@deffnx {Scheme Procedure} assoc key alist\n"
  91. "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
  92. "decide whether the argument @var{key} matches a particular entry in\n"
  93. "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
  94. "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
  95. "cannot be found in @var{alist} (according to whichever equality\n"
  96. "predicate is in use), then return @code{#f}. These functions\n"
  97. "return the entire alist entry found (i.e. both the key and the value).")
  98. #define FUNC_NAME s_scm_assq
  99. {
  100. SCM ls = alist;
  101. for(; scm_is_pair (ls); ls = SCM_CDR (ls))
  102. {
  103. SCM tmp = SCM_CAR (ls);
  104. SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
  105. "association list");
  106. if (scm_is_eq (SCM_CAR (tmp), key))
  107. return tmp;
  108. }
  109. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
  110. "association list");
  111. return SCM_BOOL_F;
  112. }
  113. #undef FUNC_NAME
  114. SCM_DEFINE (scm_assv, "assv", 2, 0, 0,
  115. (SCM key, SCM alist),
  116. "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
  117. #define FUNC_NAME s_scm_assv
  118. {
  119. SCM ls = alist;
  120. for(; scm_is_pair (ls); ls = SCM_CDR (ls))
  121. {
  122. SCM tmp = SCM_CAR (ls);
  123. SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
  124. "association list");
  125. if (scm_is_true (scm_eqv_p (SCM_CAR (tmp), key)))
  126. return tmp;
  127. }
  128. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
  129. "association list");
  130. return SCM_BOOL_F;
  131. }
  132. #undef FUNC_NAME
  133. SCM_DEFINE (scm_assoc, "assoc", 2, 0, 0,
  134. (SCM key, SCM alist),
  135. "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
  136. #define FUNC_NAME s_scm_assoc
  137. {
  138. SCM ls = alist;
  139. for(; scm_is_pair (ls); ls = SCM_CDR (ls))
  140. {
  141. SCM tmp = SCM_CAR (ls);
  142. SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
  143. "association list");
  144. if (scm_is_true (scm_equal_p (SCM_CAR (tmp), key)))
  145. return tmp;
  146. }
  147. SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
  148. "association list");
  149. return SCM_BOOL_F;
  150. }
  151. #undef FUNC_NAME
  152. /* Dirk:API2.0:: We should not return #f if the key was not found. In the
  153. * current solution we can not distinguish between finding a (key . #f) pair
  154. * and not finding the key at all.
  155. *
  156. * Possible alternative solutions:
  157. * 1) Remove assq-ref from the API: assq is sufficient.
  158. * 2) Signal an error (what error type?) if the key is not found.
  159. * 3) provide an additional 'default' parameter.
  160. * 3.1) The default parameter is mandatory.
  161. * 3.2) The default parameter is optional, but if no default is given and
  162. * the key is not found, signal an error (what error type?).
  163. */
  164. SCM_DEFINE (scm_assq_ref, "assq-ref", 2, 0, 0,
  165. (SCM alist, SCM key),
  166. "@deffnx {Scheme Procedure} assv-ref alist key\n"
  167. "@deffnx {Scheme Procedure} assoc-ref alist key\n"
  168. "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
  169. "value associated with @var{key} in @var{alist} is returned. These\n"
  170. "functions are equivalent to\n\n"
  171. "@lisp\n"
  172. "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
  173. " (and ent (cdr ent)))\n"
  174. "@end lisp\n\n"
  175. "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
  176. #define FUNC_NAME s_scm_assq_ref
  177. {
  178. SCM handle;
  179. handle = scm_sloppy_assq (key, alist);
  180. if (scm_is_pair (handle))
  181. {
  182. return SCM_CDR (handle);
  183. }
  184. return SCM_BOOL_F;
  185. }
  186. #undef FUNC_NAME
  187. SCM_DEFINE (scm_assv_ref, "assv-ref", 2, 0, 0,
  188. (SCM alist, SCM key),
  189. "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
  190. #define FUNC_NAME s_scm_assv_ref
  191. {
  192. SCM handle;
  193. handle = scm_sloppy_assv (key, alist);
  194. if (scm_is_pair (handle))
  195. {
  196. return SCM_CDR (handle);
  197. }
  198. return SCM_BOOL_F;
  199. }
  200. #undef FUNC_NAME
  201. SCM_DEFINE (scm_assoc_ref, "assoc-ref", 2, 0, 0,
  202. (SCM alist, SCM key),
  203. "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
  204. #define FUNC_NAME s_scm_assoc_ref
  205. {
  206. SCM handle;
  207. handle = scm_sloppy_assoc (key, alist);
  208. if (scm_is_pair (handle))
  209. {
  210. return SCM_CDR (handle);
  211. }
  212. return SCM_BOOL_F;
  213. }
  214. #undef FUNC_NAME
  215. SCM_DEFINE (scm_assq_set_x, "assq-set!", 3, 0, 0,
  216. (SCM alist, SCM key, SCM val),
  217. "@deffnx {Scheme Procedure} assv-set! alist key value\n"
  218. "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
  219. "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
  220. "@var{alist} entry for @var{key} and associate it with the new\n"
  221. "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
  222. "add a new one. Return the (possibly new) alist.\n\n"
  223. "These functions do not attempt to verify the structure of @var{alist},\n"
  224. "and so may cause unusual results if passed an object that is not an\n"
  225. "association list.")
  226. #define FUNC_NAME s_scm_assq_set_x
  227. {
  228. SCM handle;
  229. handle = scm_sloppy_assq (key, alist);
  230. if (scm_is_pair (handle))
  231. {
  232. SCM_SETCDR (handle, val);
  233. return alist;
  234. }
  235. else
  236. return scm_acons (key, val, alist);
  237. }
  238. #undef FUNC_NAME
  239. SCM_DEFINE (scm_assv_set_x, "assv-set!", 3, 0, 0,
  240. (SCM alist, SCM key, SCM val),
  241. "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
  242. #define FUNC_NAME s_scm_assv_set_x
  243. {
  244. SCM handle;
  245. handle = scm_sloppy_assv (key, alist);
  246. if (scm_is_pair (handle))
  247. {
  248. SCM_SETCDR (handle, val);
  249. return alist;
  250. }
  251. else
  252. return scm_acons (key, val, alist);
  253. }
  254. #undef FUNC_NAME
  255. SCM_DEFINE (scm_assoc_set_x, "assoc-set!", 3, 0, 0,
  256. (SCM alist, SCM key, SCM val),
  257. "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
  258. #define FUNC_NAME s_scm_assoc_set_x
  259. {
  260. SCM handle;
  261. handle = scm_sloppy_assoc (key, alist);
  262. if (scm_is_pair (handle))
  263. {
  264. SCM_SETCDR (handle, val);
  265. return alist;
  266. }
  267. else
  268. return scm_acons (key, val, alist);
  269. }
  270. #undef FUNC_NAME
  271. SCM_DEFINE (scm_assq_remove_x, "assq-remove!", 2, 0, 0,
  272. (SCM alist, SCM key),
  273. "@deffnx {Scheme Procedure} assv-remove! alist key\n"
  274. "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
  275. "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
  276. "the resulting alist.")
  277. #define FUNC_NAME s_scm_assq_remove_x
  278. {
  279. SCM handle;
  280. handle = scm_sloppy_assq (key, alist);
  281. if (scm_is_pair (handle))
  282. alist = scm_delq1_x (handle, alist);
  283. return alist;
  284. }
  285. #undef FUNC_NAME
  286. SCM_DEFINE (scm_assv_remove_x, "assv-remove!", 2, 0, 0,
  287. (SCM alist, SCM key),
  288. "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
  289. #define FUNC_NAME s_scm_assv_remove_x
  290. {
  291. SCM handle;
  292. handle = scm_sloppy_assv (key, alist);
  293. if (scm_is_pair (handle))
  294. alist = scm_delq1_x (handle, alist);
  295. return alist;
  296. }
  297. #undef FUNC_NAME
  298. SCM_DEFINE (scm_assoc_remove_x, "assoc-remove!", 2, 0, 0,
  299. (SCM alist, SCM key),
  300. "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
  301. #define FUNC_NAME s_scm_assoc_remove_x
  302. {
  303. SCM handle;
  304. handle = scm_sloppy_assoc (key, alist);
  305. if (scm_is_pair (handle))
  306. alist = scm_delq1_x (handle, alist);
  307. return alist;
  308. }
  309. #undef FUNC_NAME
  310. void
  311. scm_init_alist ()
  312. {
  313. #include "libguile/alist.x"
  314. }
  315. /*
  316. Local Variables:
  317. c-file-style: "gnu"
  318. End:
  319. */