alist.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/eq.h"
  44. #include "libguile/list.h"
  45. #include "libguile/validate.h"
  46. #include "libguile/alist.h"
  47. SCM_DEFINE (scm_acons, "acons", 3, 0, 0,
  48. (SCM key, SCM value, SCM alist),
  49. "Adds a new key-value pair to @var{alist}. A new pair is\n"
  50. "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
  51. "pair is consed onto @var{alist}, and the new list is returned. This\n"
  52. "function is @emph{not} destructive; @var{alist} is not modified.")
  53. #define FUNC_NAME s_scm_acons
  54. {
  55. SCM pair;
  56. SCM head;
  57. SCM_NEWCELL (pair);
  58. SCM_SET_CELL_OBJECT_0 (pair, key);
  59. SCM_SET_CELL_OBJECT_1 (pair, value);
  60. SCM_NEWCELL (head);
  61. SCM_SET_CELL_OBJECT_0 (head, pair);
  62. SCM_SET_CELL_OBJECT_1 (head, alist);
  63. return head;
  64. }
  65. #undef FUNC_NAME
  66. SCM_DEFINE (scm_sloppy_assq, "sloppy-assq", 2, 0, 0,
  67. (SCM key, SCM alist),
  68. "Behaves like @code{assq} but does not do any error checking.\n"
  69. "Recommended only for use in Guile internals.")
  70. #define FUNC_NAME s_scm_sloppy_assq
  71. {
  72. for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
  73. {
  74. SCM tmp = SCM_CAR (alist);
  75. if (SCM_CONSP (tmp) && SCM_EQ_P (SCM_CAR (tmp), key))
  76. return tmp;
  77. }
  78. return SCM_BOOL_F;
  79. }
  80. #undef FUNC_NAME
  81. SCM_DEFINE (scm_sloppy_assv, "sloppy-assv", 2, 0, 0,
  82. (SCM key, SCM alist),
  83. "Behaves like @code{assv} but does not do any error checking.\n"
  84. "Recommended only for use in Guile internals.")
  85. #define FUNC_NAME s_scm_sloppy_assv
  86. {
  87. for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
  88. {
  89. SCM tmp = SCM_CAR (alist);
  90. if (SCM_CONSP (tmp)
  91. && SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), key)))
  92. return tmp;
  93. }
  94. return SCM_BOOL_F;
  95. }
  96. #undef FUNC_NAME
  97. SCM_DEFINE (scm_sloppy_assoc, "sloppy-assoc", 2, 0, 0,
  98. (SCM key, SCM alist),
  99. "Behaves like @code{assoc} but does not do any error checking.\n"
  100. "Recommended only for use in Guile internals.")
  101. #define FUNC_NAME s_scm_sloppy_assoc
  102. {
  103. for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
  104. {
  105. SCM tmp = SCM_CAR (alist);
  106. if (SCM_CONSP (tmp)
  107. && SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), key)))
  108. return tmp;
  109. }
  110. return SCM_BOOL_F;
  111. }
  112. #undef FUNC_NAME
  113. SCM_DEFINE (scm_assq, "assq", 2, 0, 0,
  114. (SCM key, SCM alist),
  115. "@deffnx primitive assv key alist\n"
  116. "@deffnx primitive assoc key alist\n"
  117. "Fetches the entry in @var{alist} that is associated with @var{key}. To\n"
  118. "decide whether the argument @var{key} matches a particular entry in\n"
  119. "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
  120. "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
  121. "cannot be found in @var{alist} (according to whichever equality\n"
  122. "predicate is in use), then @code{#f} is returned. These functions\n"
  123. "return the entire alist entry found (i.e. both the key and the value).")
  124. #define FUNC_NAME s_scm_assq
  125. {
  126. for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
  127. {
  128. SCM tmp = SCM_CAR (alist);
  129. SCM_VALIDATE_CONS (SCM_ARG2, tmp);
  130. if (SCM_EQ_P (SCM_CAR (tmp), key))
  131. return tmp;
  132. }
  133. SCM_VALIDATE_NULL (2, alist);
  134. return SCM_BOOL_F;
  135. }
  136. #undef FUNC_NAME
  137. SCM_DEFINE (scm_assv, "assv", 2, 0, 0,
  138. (SCM key, SCM alist),
  139. "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
  140. #define FUNC_NAME s_scm_assv
  141. {
  142. for(; SCM_CONSP (alist); alist = SCM_CDR (alist))
  143. {
  144. SCM tmp = SCM_CAR (alist);
  145. SCM_VALIDATE_CONS (SCM_ARG2, tmp);
  146. if (SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), key)))
  147. return tmp;
  148. }
  149. SCM_VALIDATE_NULL (2, alist);
  150. return SCM_BOOL_F;
  151. }
  152. #undef FUNC_NAME
  153. SCM_DEFINE (scm_assoc, "assoc", 2, 0, 0,
  154. (SCM key, SCM alist),
  155. "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
  156. #define FUNC_NAME s_scm_assoc
  157. {
  158. for(; SCM_CONSP (alist); alist = SCM_CDR (alist))
  159. {
  160. SCM tmp = SCM_CAR (alist);
  161. SCM_VALIDATE_CONS (SCM_ARG2, tmp);
  162. if (SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), key)))
  163. return tmp;
  164. }
  165. SCM_VALIDATE_NULL (2, alist);
  166. return SCM_BOOL_F;
  167. }
  168. #undef FUNC_NAME
  169. SCM_DEFINE (scm_assq_ref, "assq-ref", 2, 0, 0,
  170. (SCM alist, SCM key),
  171. "@deffnx primitive assv-ref alist key\n"
  172. "@deffnx primitive assoc-ref alist key\n"
  173. "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
  174. "value associated with @var{key} in @var{alist} is returned. These\n"
  175. "functions are equivalent to\n\n"
  176. "@lisp\n"
  177. "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
  178. " (and ent (cdr ent)))\n"
  179. "@end lisp\n\n"
  180. "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
  181. #define FUNC_NAME s_scm_assq_ref
  182. {
  183. SCM handle;
  184. handle = scm_sloppy_assq (key, alist);
  185. if (SCM_CONSP (handle))
  186. {
  187. return SCM_CDR (handle);
  188. }
  189. return SCM_BOOL_F;
  190. }
  191. #undef FUNC_NAME
  192. SCM_DEFINE (scm_assv_ref, "assv-ref", 2, 0, 0,
  193. (SCM alist, SCM key),
  194. "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
  195. #define FUNC_NAME s_scm_assv_ref
  196. {
  197. SCM handle;
  198. handle = scm_sloppy_assv (key, alist);
  199. if (SCM_CONSP (handle))
  200. {
  201. return SCM_CDR (handle);
  202. }
  203. return SCM_BOOL_F;
  204. }
  205. #undef FUNC_NAME
  206. SCM_DEFINE (scm_assoc_ref, "assoc-ref", 2, 0, 0,
  207. (SCM alist, SCM key),
  208. "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
  209. #define FUNC_NAME s_scm_assoc_ref
  210. {
  211. SCM handle;
  212. handle = scm_sloppy_assoc (key, alist);
  213. if (SCM_CONSP (handle))
  214. {
  215. return SCM_CDR (handle);
  216. }
  217. return SCM_BOOL_F;
  218. }
  219. #undef FUNC_NAME
  220. SCM_DEFINE (scm_assq_set_x, "assq-set!", 3, 0, 0,
  221. (SCM alist, SCM key, SCM val),
  222. "@deffnx primitive assv-set! alist key value\n"
  223. "@deffnx primitive assoc-set! alist key value\n"
  224. "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
  225. "@var{alist} entry for @var{key} and associate it with the new\n"
  226. "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
  227. "add a new one. Return the (possibly new) alist.\n\n"
  228. "These functions do not attempt to verify the structure of @var{alist},\n"
  229. "and so may cause unusual results if passed an object that is not an\n"
  230. "association list.")
  231. #define FUNC_NAME s_scm_assq_set_x
  232. {
  233. SCM handle;
  234. handle = scm_sloppy_assq (key, alist);
  235. if (SCM_CONSP (handle))
  236. {
  237. SCM_SETCDR (handle, val);
  238. return alist;
  239. }
  240. else
  241. return scm_acons (key, val, alist);
  242. }
  243. #undef FUNC_NAME
  244. SCM_DEFINE (scm_assv_set_x, "assv-set!", 3, 0, 0,
  245. (SCM alist, SCM key, SCM val),
  246. "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
  247. #define FUNC_NAME s_scm_assv_set_x
  248. {
  249. SCM handle;
  250. handle = scm_sloppy_assv (key, alist);
  251. if (SCM_CONSP (handle))
  252. {
  253. SCM_SETCDR (handle, val);
  254. return alist;
  255. }
  256. else
  257. return scm_acons (key, val, alist);
  258. }
  259. #undef FUNC_NAME
  260. SCM_DEFINE (scm_assoc_set_x, "assoc-set!", 3, 0, 0,
  261. (SCM alist, SCM key, SCM val),
  262. "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
  263. #define FUNC_NAME s_scm_assoc_set_x
  264. {
  265. SCM handle;
  266. handle = scm_sloppy_assoc (key, alist);
  267. if (SCM_CONSP (handle))
  268. {
  269. SCM_SETCDR (handle, val);
  270. return alist;
  271. }
  272. else
  273. return scm_acons (key, val, alist);
  274. }
  275. #undef FUNC_NAME
  276. SCM_DEFINE (scm_assq_remove_x, "assq-remove!", 2, 0, 0,
  277. (SCM alist, SCM key),
  278. "@deffnx primitive assv-remove! alist key\n"
  279. "@deffnx primitive assoc-remove! alist key\n"
  280. "Delete any entry in @var{alist} associated with @var{key}, and return\n"
  281. "the resulting alist.")
  282. #define FUNC_NAME s_scm_assq_remove_x
  283. {
  284. SCM handle;
  285. handle = scm_sloppy_assq (key, alist);
  286. if (SCM_CONSP (handle))
  287. {
  288. return scm_delq_x (handle, alist);
  289. }
  290. else
  291. return alist;
  292. }
  293. #undef FUNC_NAME
  294. SCM_DEFINE (scm_assv_remove_x, "assv-remove!", 2, 0, 0,
  295. (SCM alist, SCM key),
  296. "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
  297. #define FUNC_NAME s_scm_assv_remove_x
  298. {
  299. SCM handle;
  300. handle = scm_sloppy_assv (key, alist);
  301. if (SCM_CONSP (handle))
  302. {
  303. return scm_delv_x (handle, alist);
  304. }
  305. else
  306. return alist;
  307. }
  308. #undef FUNC_NAME
  309. SCM_DEFINE (scm_assoc_remove_x, "assoc-remove!", 2, 0, 0,
  310. (SCM alist, SCM key),
  311. "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
  312. #define FUNC_NAME s_scm_assoc_remove_x
  313. {
  314. SCM handle;
  315. handle = scm_sloppy_assoc (key, alist);
  316. if (SCM_CONSP (handle))
  317. {
  318. return scm_delete_x (handle, alist);
  319. }
  320. else
  321. return alist;
  322. }
  323. #undef FUNC_NAME
  324. void
  325. scm_init_alist ()
  326. {
  327. #include "libguile/alist.x"
  328. }
  329. /*
  330. Local Variables:
  331. c-file-style: "gnu"
  332. End:
  333. */