* First batch of libguile changes for Elisp support.
[bpt/guile.git] / libguile / alist.c
1 /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001 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
42
43 \f
44 #include "libguile/_scm.h"
45 #include "libguile/eq.h"
46 #include "libguile/list.h"
47 #include "libguile/lang.h"
48
49 #include "libguile/validate.h"
50 #include "libguile/alist.h"
51
52 \f
53
54 SCM_DEFINE (scm_acons, "acons", 3, 0, 0,
55 (SCM key, SCM value, SCM alist),
56 "Add a new key-value pair to @var{alist}. A new pair is\n"
57 "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
58 "pair is consed onto @var{alist}, and the new list is returned. This\n"
59 "function is @emph{not} destructive; @var{alist} is not modified.")
60 #define FUNC_NAME s_scm_acons
61 {
62 return scm_alloc_cell (SCM_UNPACK (scm_alloc_cell (SCM_UNPACK (key),
63 SCM_UNPACK (value))),
64 SCM_UNPACK (alist));
65 }
66 #undef FUNC_NAME
67
68 \f
69
70 SCM_DEFINE (scm_sloppy_assq, "sloppy-assq", 2, 0, 0,
71 (SCM key, SCM alist),
72 "Behaves like @code{assq} but does not do any error checking.\n"
73 "Recommended only for use in Guile internals.")
74 #define FUNC_NAME s_scm_sloppy_assq
75 {
76 for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
77 {
78 SCM tmp = SCM_CAR (alist);
79 if (SCM_CONSP (tmp) && SCM_EQ_P (SCM_CAR (tmp), key))
80 return tmp;
81 }
82 return SCM_BOOL_F;
83 }
84 #undef FUNC_NAME
85
86
87
88 SCM_DEFINE (scm_sloppy_assv, "sloppy-assv", 2, 0, 0,
89 (SCM key, SCM alist),
90 "Behaves like @code{assv} but does not do any error checking.\n"
91 "Recommended only for use in Guile internals.")
92 #define FUNC_NAME s_scm_sloppy_assv
93 {
94 for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
95 {
96 SCM tmp = SCM_CAR (alist);
97 if (SCM_CONSP (tmp)
98 && SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), key)))
99 return tmp;
100 }
101 return SCM_BOOL_F;
102 }
103 #undef FUNC_NAME
104
105
106 SCM_DEFINE (scm_sloppy_assoc, "sloppy-assoc", 2, 0, 0,
107 (SCM key, SCM alist),
108 "Behaves like @code{assoc} but does not do any error checking.\n"
109 "Recommended only for use in Guile internals.")
110 #define FUNC_NAME s_scm_sloppy_assoc
111 {
112 for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
113 {
114 SCM tmp = SCM_CAR (alist);
115 if (SCM_CONSP (tmp)
116 && SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), key)))
117 return tmp;
118 }
119 return SCM_BOOL_F;
120 }
121 #undef FUNC_NAME
122
123
124 \f
125
126 SCM_DEFINE (scm_assq, "assq", 2, 0, 0,
127 (SCM key, SCM alist),
128 "@deffnx {Scheme Procedure} assv key alist\n"
129 "@deffnx {Scheme Procedure} assoc key alist\n"
130 "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
131 "decide whether the argument @var{key} matches a particular entry in\n"
132 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
133 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
134 "cannot be found in @var{alist} (according to whichever equality\n"
135 "predicate is in use), then return @code{#f}. These functions\n"
136 "return the entire alist entry found (i.e. both the key and the value).")
137 #define FUNC_NAME s_scm_assq
138 {
139 SCM ls = alist;
140 for (; SCM_CONSP (ls); ls = SCM_CDR (ls))
141 {
142 SCM tmp = SCM_CAR (ls);
143 SCM_ASSERT_TYPE (SCM_CONSP (tmp), alist, SCM_ARG2, FUNC_NAME,
144 "association list");
145 if (SCM_EQ_P (SCM_CAR (tmp), key))
146 return tmp;
147 }
148 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
149 "association list");
150 return SCM_BOOL_F;
151 }
152 #undef FUNC_NAME
153
154
155 SCM_DEFINE (scm_assv, "assv", 2, 0, 0,
156 (SCM key, SCM alist),
157 "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
158 #define FUNC_NAME s_scm_assv
159 {
160 SCM ls = alist;
161 for(; SCM_CONSP (ls); ls = SCM_CDR (ls))
162 {
163 SCM tmp = SCM_CAR (ls);
164 SCM_ASSERT_TYPE (SCM_CONSP (tmp), alist, SCM_ARG2, FUNC_NAME,
165 "association list");
166 if (SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), key)))
167 return tmp;
168 }
169 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
170 "association list");
171 return SCM_BOOL_F;
172 }
173 #undef FUNC_NAME
174
175
176 SCM_DEFINE (scm_assoc, "assoc", 2, 0, 0,
177 (SCM key, SCM alist),
178 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
179 #define FUNC_NAME s_scm_assoc
180 {
181 SCM ls = alist;
182 for(; SCM_CONSP (ls); ls = SCM_CDR (ls))
183 {
184 SCM tmp = SCM_CAR (ls);
185 SCM_ASSERT_TYPE (SCM_CONSP (tmp), alist, SCM_ARG2, FUNC_NAME,
186 "association list");
187 if (SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), key)))
188 return tmp;
189 }
190 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
191 "association list");
192 return SCM_BOOL_F;
193 }
194 #undef FUNC_NAME
195
196
197 \f
198
199 /* Dirk:API2.0:: We should not return #f if the key was not found. In the
200 * current solution we can not distinguish between finding a (key . #f) pair
201 * and not finding the key at all.
202 *
203 * Possible alternative solutions:
204 * 1) Remove assq-ref from the API: assq is sufficient.
205 * 2) Signal an error (what error type?) if the key is not found.
206 * 3) provide an additional 'default' parameter.
207 * 3.1) The default parameter is mandatory.
208 * 3.2) The default parameter is optional, but if no default is given and
209 * the key is not found, signal an error (what error type?).
210 */
211 SCM_DEFINE (scm_assq_ref, "assq-ref", 2, 0, 0,
212 (SCM alist, SCM key),
213 "@deffnx {Scheme Procedure} assv-ref alist key\n"
214 "@deffnx {Scheme Procedure} assoc-ref alist key\n"
215 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
216 "value associated with @var{key} in @var{alist} is returned. These\n"
217 "functions are equivalent to\n\n"
218 "@lisp\n"
219 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
220 " (and ent (cdr ent)))\n"
221 "@end lisp\n\n"
222 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
223 #define FUNC_NAME s_scm_assq_ref
224 {
225 SCM handle;
226
227 handle = scm_sloppy_assq (key, alist);
228 if (SCM_CONSP (handle))
229 {
230 return SCM_CDR (handle);
231 }
232 return SCM_BOOL_F;
233 }
234 #undef FUNC_NAME
235
236
237 SCM_DEFINE (scm_assv_ref, "assv-ref", 2, 0, 0,
238 (SCM alist, SCM key),
239 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
240 #define FUNC_NAME s_scm_assv_ref
241 {
242 SCM handle;
243
244 handle = scm_sloppy_assv (key, alist);
245 if (SCM_CONSP (handle))
246 {
247 return SCM_CDR (handle);
248 }
249 return SCM_BOOL_F;
250 }
251 #undef FUNC_NAME
252
253
254 SCM_DEFINE (scm_assoc_ref, "assoc-ref", 2, 0, 0,
255 (SCM alist, SCM key),
256 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
257 #define FUNC_NAME s_scm_assoc_ref
258 {
259 SCM handle;
260
261 handle = scm_sloppy_assoc (key, alist);
262 if (SCM_CONSP (handle))
263 {
264 return SCM_CDR (handle);
265 }
266 return SCM_BOOL_F;
267 }
268 #undef FUNC_NAME
269
270
271
272 \f
273
274
275 SCM_DEFINE (scm_assq_set_x, "assq-set!", 3, 0, 0,
276 (SCM alist, SCM key, SCM val),
277 "@deffnx {Scheme Procedure} assv-set! alist key value\n"
278 "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
279 "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
280 "@var{alist} entry for @var{key} and associate it with the new\n"
281 "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
282 "add a new one. Return the (possibly new) alist.\n\n"
283 "These functions do not attempt to verify the structure of @var{alist},\n"
284 "and so may cause unusual results if passed an object that is not an\n"
285 "association list.")
286 #define FUNC_NAME s_scm_assq_set_x
287 {
288 SCM handle;
289
290 handle = scm_sloppy_assq (key, alist);
291 if (SCM_CONSP (handle))
292 {
293 SCM_SETCDR (handle, val);
294 return alist;
295 }
296 else
297 return scm_acons (key, val, alist);
298 }
299 #undef FUNC_NAME
300
301 SCM_DEFINE (scm_assv_set_x, "assv-set!", 3, 0, 0,
302 (SCM alist, SCM key, SCM val),
303 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
304 #define FUNC_NAME s_scm_assv_set_x
305 {
306 SCM handle;
307
308 handle = scm_sloppy_assv (key, alist);
309 if (SCM_CONSP (handle))
310 {
311 SCM_SETCDR (handle, val);
312 return alist;
313 }
314 else
315 return scm_acons (key, val, alist);
316 }
317 #undef FUNC_NAME
318
319 SCM_DEFINE (scm_assoc_set_x, "assoc-set!", 3, 0, 0,
320 (SCM alist, SCM key, SCM val),
321 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
322 #define FUNC_NAME s_scm_assoc_set_x
323 {
324 SCM handle;
325
326 handle = scm_sloppy_assoc (key, alist);
327 if (SCM_CONSP (handle))
328 {
329 SCM_SETCDR (handle, val);
330 return alist;
331 }
332 else
333 return scm_acons (key, val, alist);
334 }
335 #undef FUNC_NAME
336
337
338 \f
339
340 SCM_DEFINE (scm_assq_remove_x, "assq-remove!", 2, 0, 0,
341 (SCM alist, SCM key),
342 "@deffnx {Scheme Procedure} assv-remove! alist key\n"
343 "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
344 "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
345 "the resulting alist.")
346 #define FUNC_NAME s_scm_assq_remove_x
347 {
348 SCM handle;
349
350 handle = scm_sloppy_assq (key, alist);
351 if (SCM_CONSP (handle))
352 alist = scm_delq1_x (handle, alist);
353
354 return alist;
355 }
356 #undef FUNC_NAME
357
358
359 SCM_DEFINE (scm_assv_remove_x, "assv-remove!", 2, 0, 0,
360 (SCM alist, SCM key),
361 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
362 #define FUNC_NAME s_scm_assv_remove_x
363 {
364 SCM handle;
365
366 handle = scm_sloppy_assv (key, alist);
367 if (SCM_CONSP (handle))
368 alist = scm_delq1_x (handle, alist);
369
370 return alist;
371 }
372 #undef FUNC_NAME
373
374
375 SCM_DEFINE (scm_assoc_remove_x, "assoc-remove!", 2, 0, 0,
376 (SCM alist, SCM key),
377 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
378 #define FUNC_NAME s_scm_assoc_remove_x
379 {
380 SCM handle;
381
382 handle = scm_sloppy_assoc (key, alist);
383 if (SCM_CONSP (handle))
384 alist = scm_delq1_x (handle, alist);
385
386 return alist;
387 }
388 #undef FUNC_NAME
389
390
391 \f
392
393
394
395 void
396 scm_init_alist ()
397 {
398 #ifndef SCM_MAGIC_SNARFER
399 #include "libguile/alist.x"
400 #endif
401 }
402
403
404 /*
405 Local Variables:
406 c-file-style: "gnu"
407 End:
408 */