alist.c 10 KB

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