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