* Makefile.am (DEFS): Added. automake adds -I options to DEFS,
[bpt/guile.git] / libguile / alist.c
1 /* Copyright (C) 1995, 96, 97, 98, 99, 2000 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 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46 #include <stdio.h>
47 #include "libguile/_scm.h"
48 #include "libguile/eq.h"
49 #include "libguile/list.h"
50
51 #include "libguile/validate.h"
52 #include "libguile/alist.h"
53
54 \f
55
56 SCM_DEFINE (scm_acons, "acons", 3, 0, 0,
57 (SCM key, SCM value, SCM alist),
58 "Adds a new key-value pair to @var{alist}. A new pair is\n"
59 "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
60 "pair is consed onto @var{alist}, and the new list is returned. This\n"
61 "function is @emph{not} destructive; @var{alist} is not modified.")
62 #define FUNC_NAME s_scm_acons
63 {
64 SCM pair;
65 SCM head;
66
67 SCM_NEWCELL (pair);
68 SCM_SETCAR (pair, key);
69 SCM_SETCDR (pair, value);
70
71 SCM_NEWCELL (head);
72 SCM_SETCAR (head, pair);
73 SCM_SETCDR (head, alist);
74
75 return head;
76 }
77 #undef FUNC_NAME
78
79 \f
80
81 SCM_DEFINE (scm_sloppy_assq, "sloppy-assq", 2, 0, 0,
82 (SCM key, SCM alist),
83 "Behaves like @code{assq} but does not do any error checking.\n"
84 "Recommended only for use in Guile internals.")
85 #define FUNC_NAME s_scm_sloppy_assq
86 {
87 for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
88 {
89 SCM tmp = SCM_CAR (alist);
90 if (SCM_CONSP (tmp) && SCM_EQ_P (SCM_CAR (tmp), key))
91 return tmp;
92 }
93 return SCM_BOOL_F;
94 }
95 #undef FUNC_NAME
96
97
98
99 SCM_DEFINE (scm_sloppy_assv, "sloppy-assv", 2, 0, 0,
100 (SCM key, SCM alist),
101 "Behaves like @code{assv} but does not do any error checking.\n"
102 "Recommended only for use in Guile internals.")
103 #define FUNC_NAME s_scm_sloppy_assv
104 {
105 for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
106 {
107 SCM tmp = SCM_CAR (alist);
108 if (SCM_CONSP (tmp)
109 && SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), key)))
110 return tmp;
111 }
112 return SCM_BOOL_F;
113 }
114 #undef FUNC_NAME
115
116
117 SCM_DEFINE (scm_sloppy_assoc, "sloppy-assoc", 2, 0, 0,
118 (SCM key, SCM alist),
119 "Behaves like @code{assoc} but does not do any error checking.\n"
120 "Recommended only for use in Guile internals.")
121 #define FUNC_NAME s_scm_sloppy_assoc
122 {
123 for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
124 {
125 SCM tmp = SCM_CAR (alist);
126 if (SCM_CONSP (tmp)
127 && SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), key)))
128 return tmp;
129 }
130 return SCM_BOOL_F;
131 }
132 #undef FUNC_NAME
133
134
135 \f
136
137 SCM_DEFINE (scm_assq, "assq", 2, 0, 0,
138 (SCM key, SCM alist),
139 "@deffnx primitive assv key alist\n"
140 "@deffnx primitive assoc key alist\n"
141 "Fetches the entry in @var{alist} that is associated with @var{key}. To\n"
142 "decide whether the argument @var{key} matches a particular entry in\n"
143 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
144 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
145 "cannot be found in @var{alist} (according to whichever equality\n"
146 "predicate is in use), then @code{#f} is returned. These functions\n"
147 "return the entire alist entry found (i.e. both the key and the value).")
148 #define FUNC_NAME s_scm_assq
149 {
150 for (; SCM_CONSP (alist); alist = SCM_CDR (alist))
151 {
152 SCM tmp = SCM_CAR (alist);
153 SCM_VALIDATE_CONS (SCM_ARG2, tmp);
154 if (SCM_EQ_P (SCM_CAR (tmp), key))
155 return tmp;
156 }
157 SCM_VALIDATE_NULL (2, alist);
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 for(; SCM_CONSP (alist); alist = SCM_CDR (alist))
169 {
170 SCM tmp = SCM_CAR (alist);
171 SCM_VALIDATE_CONS (SCM_ARG2, tmp);
172 if (SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), key)))
173 return tmp;
174 }
175 SCM_VALIDATE_NULL (2, alist);
176 return SCM_BOOL_F;
177 }
178 #undef FUNC_NAME
179
180
181 SCM_DEFINE (scm_assoc, "assoc", 2, 0, 0,
182 (SCM key, SCM alist),
183 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
184 #define FUNC_NAME s_scm_assoc
185 {
186 for(; SCM_CONSP (alist); alist = SCM_CDR (alist))
187 {
188 SCM tmp = SCM_CAR (alist);
189 SCM_VALIDATE_CONS (SCM_ARG2, tmp);
190 if (SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), key)))
191 return tmp;
192 }
193 SCM_VALIDATE_NULL (2, alist);
194 return SCM_BOOL_F;
195 }
196 #undef FUNC_NAME
197
198
199 \f
200
201 SCM_DEFINE (scm_assq_ref, "assq-ref", 2, 0, 0,
202 (SCM alist, SCM key),
203 "@deffnx primitive assv-ref alist key\n"
204 "@deffnx primitive assoc-ref alist key\n"
205 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
206 "value associated with @var{key} in @var{alist} is returned. These\n"
207 "functions are equivalent to\n\n"
208 "@lisp\n"
209 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
210 " (and ent (cdr ent)))\n"
211 "@end lisp\n\n"
212 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
213 #define FUNC_NAME s_scm_assq_ref
214 {
215 SCM handle;
216
217 handle = scm_sloppy_assq (key, alist);
218 if (SCM_CONSP (handle))
219 {
220 return SCM_CDR (handle);
221 }
222 return SCM_BOOL_F;
223 }
224 #undef FUNC_NAME
225
226
227 SCM_DEFINE (scm_assv_ref, "assv-ref", 2, 0, 0,
228 (SCM alist, SCM key),
229 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
230 #define FUNC_NAME s_scm_assv_ref
231 {
232 SCM handle;
233
234 handle = scm_sloppy_assv (key, alist);
235 if (SCM_CONSP (handle))
236 {
237 return SCM_CDR (handle);
238 }
239 return SCM_BOOL_F;
240 }
241 #undef FUNC_NAME
242
243
244 SCM_DEFINE (scm_assoc_ref, "assoc-ref", 2, 0, 0,
245 (SCM alist, SCM key),
246 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
247 #define FUNC_NAME s_scm_assoc_ref
248 {
249 SCM handle;
250
251 handle = scm_sloppy_assoc (key, alist);
252 if (SCM_CONSP (handle))
253 {
254 return SCM_CDR (handle);
255 }
256 return SCM_BOOL_F;
257 }
258 #undef FUNC_NAME
259
260
261
262 \f
263
264
265 SCM_DEFINE (scm_assq_set_x, "assq-set!", 3, 0, 0,
266 (SCM alist, SCM key, SCM val),
267 "@deffnx primitive assv-set! alist key value\n"
268 "@deffnx primitive assoc-set! alist key value\n"
269 "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
270 "@var{alist} entry for @var{key} and associate it with the new\n"
271 "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
272 "add a new one. Return the (possibly new) alist.\n\n"
273 "These functions do not attempt to verify the structure of @var{alist},\n"
274 "and so may cause unusual results if passed an object that is not an\n"
275 "association list.")
276 #define FUNC_NAME s_scm_assq_set_x
277 {
278 SCM handle;
279
280 handle = scm_sloppy_assq (key, alist);
281 if (SCM_CONSP (handle))
282 {
283 SCM_SETCDR (handle, val);
284 return alist;
285 }
286 else
287 return scm_acons (key, val, alist);
288 }
289 #undef FUNC_NAME
290
291 SCM_DEFINE (scm_assv_set_x, "assv-set!", 3, 0, 0,
292 (SCM alist, SCM key, SCM val),
293 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
294 #define FUNC_NAME s_scm_assv_set_x
295 {
296 SCM handle;
297
298 handle = scm_sloppy_assv (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_assoc_set_x, "assoc-set!", 3, 0, 0,
310 (SCM alist, SCM key, SCM val),
311 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
312 #define FUNC_NAME s_scm_assoc_set_x
313 {
314 SCM handle;
315
316 handle = scm_sloppy_assoc (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
328 \f
329
330 SCM_DEFINE (scm_assq_remove_x, "assq-remove!", 2, 0, 0,
331 (SCM alist, SCM key),
332 "@deffnx primitive assv-remove! alist key\n"
333 "@deffnx primitive assoc-remove! alist key\n"
334 "Delete any entry in @var{alist} associated with @var{key}, and return\n"
335 "the resulting alist.")
336 #define FUNC_NAME s_scm_assq_remove_x
337 {
338 SCM handle;
339
340 handle = scm_sloppy_assq (key, alist);
341 if (SCM_CONSP (handle))
342 {
343 return scm_delq_x (handle, alist);
344 }
345 else
346 return alist;
347 }
348 #undef FUNC_NAME
349
350
351 SCM_DEFINE (scm_assv_remove_x, "assv-remove!", 2, 0, 0,
352 (SCM alist, SCM key),
353 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
354 #define FUNC_NAME s_scm_assv_remove_x
355 {
356 SCM handle;
357
358 handle = scm_sloppy_assv (key, alist);
359 if (SCM_CONSP (handle))
360 {
361 return scm_delv_x (handle, alist);
362 }
363 else
364 return alist;
365 }
366 #undef FUNC_NAME
367
368
369 SCM_DEFINE (scm_assoc_remove_x, "assoc-remove!", 2, 0, 0,
370 (SCM alist, SCM key),
371 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
372 #define FUNC_NAME s_scm_assoc_remove_x
373 {
374 SCM handle;
375
376 handle = scm_sloppy_assoc (key, alist);
377 if (SCM_CONSP (handle))
378 {
379 return scm_delete_x (handle, alist);
380 }
381 else
382 return alist;
383 }
384 #undef FUNC_NAME
385
386
387 \f
388
389
390
391 void
392 scm_init_alist ()
393 {
394 #include "libguile/alist.x"
395 }
396
397
398 /*
399 Local Variables:
400 c-file-style: "gnu"
401 End:
402 */