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