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