Commit | Line | Data |
---|---|---|
58ade102 | 1 | /* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001 Free Software Foundation, Inc. |
0f2d19dd JB |
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 | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
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. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 | 41 | |
1bbd0b84 | 42 | |
0f2d19dd | 43 | \f |
a0599745 MD |
44 | #include "libguile/_scm.h" |
45 | #include "libguile/eq.h" | |
46 | #include "libguile/list.h" | |
c96d76b8 | 47 | #include "libguile/lang.h" |
20e6290e | 48 | |
a0599745 MD |
49 | #include "libguile/validate.h" |
50 | #include "libguile/alist.h" | |
0f2d19dd JB |
51 | |
52 | \f | |
53 | ||
3b3b36dd | 54 | SCM_DEFINE (scm_acons, "acons", 3, 0, 0, |
0b5f3f34 | 55 | (SCM key, SCM value, SCM alist), |
8f85c0c6 | 56 | "Add a new key-value pair to @var{alist}. A new pair is\n" |
b380b885 MD |
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.") | |
1bbd0b84 | 60 | #define FUNC_NAME s_scm_acons |
0f2d19dd | 61 | { |
16d4699b MV |
62 | return scm_alloc_cell (SCM_UNPACK (scm_alloc_cell (SCM_UNPACK (key), |
63 | SCM_UNPACK (value))), | |
64 | SCM_UNPACK (alist)); | |
0f2d19dd | 65 | } |
1bbd0b84 | 66 | #undef FUNC_NAME |
0f2d19dd JB |
67 | |
68 | \f | |
69 | ||
a1ec6916 | 70 | SCM_DEFINE (scm_sloppy_assq, "sloppy-assq", 2, 0, 0, |
0b5f3f34 | 71 | (SCM key, SCM alist), |
b380b885 MD |
72 | "Behaves like @code{assq} but does not do any error checking.\n" |
73 | "Recommended only for use in Guile internals.") | |
1bbd0b84 | 74 | #define FUNC_NAME s_scm_sloppy_assq |
0f2d19dd | 75 | { |
0c95b57d | 76 | for (; SCM_CONSP (alist); alist = SCM_CDR (alist)) |
0f2d19dd | 77 | { |
2de257bd | 78 | SCM tmp = SCM_CAR (alist); |
fbd485ba | 79 | if (SCM_CONSP (tmp) && SCM_EQ_P (SCM_CAR (tmp), key)) |
cf18adf0 | 80 | return tmp; |
0f2d19dd JB |
81 | } |
82 | return SCM_BOOL_F; | |
83 | } | |
1bbd0b84 | 84 | #undef FUNC_NAME |
0f2d19dd JB |
85 | |
86 | ||
87 | ||
a1ec6916 | 88 | SCM_DEFINE (scm_sloppy_assv, "sloppy-assv", 2, 0, 0, |
0b5f3f34 | 89 | (SCM key, SCM alist), |
b380b885 MD |
90 | "Behaves like @code{assv} but does not do any error checking.\n" |
91 | "Recommended only for use in Guile internals.") | |
1bbd0b84 | 92 | #define FUNC_NAME s_scm_sloppy_assv |
0f2d19dd | 93 | { |
0c95b57d | 94 | for (; SCM_CONSP (alist); alist = SCM_CDR (alist)) |
0f2d19dd | 95 | { |
2de257bd | 96 | SCM tmp = SCM_CAR (alist); |
0b5f3f34 GB |
97 | if (SCM_CONSP (tmp) |
98 | && SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), key))) | |
cf18adf0 | 99 | return tmp; |
0f2d19dd JB |
100 | } |
101 | return SCM_BOOL_F; | |
102 | } | |
1bbd0b84 | 103 | #undef FUNC_NAME |
0f2d19dd JB |
104 | |
105 | ||
a1ec6916 | 106 | SCM_DEFINE (scm_sloppy_assoc, "sloppy-assoc", 2, 0, 0, |
0b5f3f34 | 107 | (SCM key, SCM alist), |
b380b885 MD |
108 | "Behaves like @code{assoc} but does not do any error checking.\n" |
109 | "Recommended only for use in Guile internals.") | |
1bbd0b84 | 110 | #define FUNC_NAME s_scm_sloppy_assoc |
0f2d19dd | 111 | { |
0c95b57d | 112 | for (; SCM_CONSP (alist); alist = SCM_CDR (alist)) |
0f2d19dd | 113 | { |
2de257bd | 114 | SCM tmp = SCM_CAR (alist); |
0b5f3f34 GB |
115 | if (SCM_CONSP (tmp) |
116 | && SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), key))) | |
cf18adf0 | 117 | return tmp; |
0f2d19dd JB |
118 | } |
119 | return SCM_BOOL_F; | |
120 | } | |
1bbd0b84 | 121 | #undef FUNC_NAME |
0f2d19dd JB |
122 | |
123 | ||
124 | \f | |
125 | ||
3b3b36dd | 126 | SCM_DEFINE (scm_assq, "assq", 2, 0, 0, |
0b5f3f34 | 127 | (SCM key, SCM alist), |
8f85c0c6 NJ |
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" | |
b380b885 MD |
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" | |
8f85c0c6 | 135 | "predicate is in use), then return @code{#f}. These functions\n" |
b380b885 | 136 | "return the entire alist entry found (i.e. both the key and the value).") |
1bbd0b84 | 137 | #define FUNC_NAME s_scm_assq |
0f2d19dd | 138 | { |
1aa621a3 MD |
139 | SCM ls = alist; |
140 | for (; SCM_CONSP (ls); ls = SCM_CDR (ls)) | |
e1385ffc | 141 | { |
1aa621a3 MD |
142 | SCM tmp = SCM_CAR (ls); |
143 | SCM_ASSERT_TYPE (SCM_CONSP (tmp), alist, SCM_ARG2, FUNC_NAME, | |
144 | "association list"); | |
fbd485ba | 145 | if (SCM_EQ_P (SCM_CAR (tmp), key)) |
2de257bd | 146 | return tmp; |
e1385ffc | 147 | } |
c96d76b8 | 148 | SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME, |
1aa621a3 | 149 | "association list"); |
1bbd0b84 | 150 | return SCM_BOOL_F; |
0f2d19dd | 151 | } |
1bbd0b84 | 152 | #undef FUNC_NAME |
0f2d19dd JB |
153 | |
154 | ||
3b3b36dd | 155 | SCM_DEFINE (scm_assv, "assv", 2, 0, 0, |
0b5f3f34 | 156 | (SCM key, SCM alist), |
b380b885 | 157 | "Behaves like @code{assq} but uses @code{eqv?} for key comparison.") |
1bbd0b84 | 158 | #define FUNC_NAME s_scm_assv |
0f2d19dd | 159 | { |
1aa621a3 MD |
160 | SCM ls = alist; |
161 | for(; SCM_CONSP (ls); ls = SCM_CDR (ls)) | |
e1385ffc | 162 | { |
1aa621a3 MD |
163 | SCM tmp = SCM_CAR (ls); |
164 | SCM_ASSERT_TYPE (SCM_CONSP (tmp), alist, SCM_ARG2, FUNC_NAME, | |
165 | "association list"); | |
2de257bd MD |
166 | if (SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), key))) |
167 | return tmp; | |
e1385ffc | 168 | } |
c96d76b8 | 169 | SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME, |
1aa621a3 | 170 | "association list"); |
0f2d19dd JB |
171 | return SCM_BOOL_F; |
172 | } | |
1bbd0b84 | 173 | #undef FUNC_NAME |
0f2d19dd JB |
174 | |
175 | ||
3b3b36dd | 176 | SCM_DEFINE (scm_assoc, "assoc", 2, 0, 0, |
0b5f3f34 | 177 | (SCM key, SCM alist), |
b380b885 | 178 | "Behaves like @code{assq} but uses @code{equal?} for key comparison.") |
1bbd0b84 | 179 | #define FUNC_NAME s_scm_assoc |
0f2d19dd | 180 | { |
1aa621a3 MD |
181 | SCM ls = alist; |
182 | for(; SCM_CONSP (ls); ls = SCM_CDR (ls)) | |
e1385ffc | 183 | { |
1aa621a3 MD |
184 | SCM tmp = SCM_CAR (ls); |
185 | SCM_ASSERT_TYPE (SCM_CONSP (tmp), alist, SCM_ARG2, FUNC_NAME, | |
186 | "association list"); | |
2de257bd MD |
187 | if (SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), key))) |
188 | return tmp; | |
e1385ffc | 189 | } |
c96d76b8 | 190 | SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME, |
1aa621a3 | 191 | "association list"); |
1bbd0b84 | 192 | return SCM_BOOL_F; |
0f2d19dd | 193 | } |
1bbd0b84 | 194 | #undef FUNC_NAME |
0f2d19dd JB |
195 | |
196 | ||
197 | \f | |
198 | ||
d1ca2c64 DH |
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 | */ | |
a1ec6916 | 211 | SCM_DEFINE (scm_assq_ref, "assq-ref", 2, 0, 0, |
1bbd0b84 | 212 | (SCM alist, SCM key), |
8f85c0c6 NJ |
213 | "@deffnx {Scheme Procedure} assv-ref alist key\n" |
214 | "@deffnx {Scheme Procedure} assoc-ref alist key\n" | |
b380b885 MD |
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}.") | |
1bbd0b84 | 223 | #define FUNC_NAME s_scm_assq_ref |
0f2d19dd JB |
224 | { |
225 | SCM handle; | |
226 | ||
227 | handle = scm_sloppy_assq (key, alist); | |
0c95b57d | 228 | if (SCM_CONSP (handle)) |
0f2d19dd JB |
229 | { |
230 | return SCM_CDR (handle); | |
231 | } | |
232 | return SCM_BOOL_F; | |
233 | } | |
1bbd0b84 | 234 | #undef FUNC_NAME |
0f2d19dd JB |
235 | |
236 | ||
a1ec6916 | 237 | SCM_DEFINE (scm_assv_ref, "assv-ref", 2, 0, 0, |
1bbd0b84 | 238 | (SCM alist, SCM key), |
b380b885 | 239 | "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.") |
1bbd0b84 | 240 | #define FUNC_NAME s_scm_assv_ref |
0f2d19dd JB |
241 | { |
242 | SCM handle; | |
243 | ||
244 | handle = scm_sloppy_assv (key, alist); | |
0c95b57d | 245 | if (SCM_CONSP (handle)) |
0f2d19dd JB |
246 | { |
247 | return SCM_CDR (handle); | |
248 | } | |
249 | return SCM_BOOL_F; | |
250 | } | |
1bbd0b84 | 251 | #undef FUNC_NAME |
0f2d19dd JB |
252 | |
253 | ||
a1ec6916 | 254 | SCM_DEFINE (scm_assoc_ref, "assoc-ref", 2, 0, 0, |
1bbd0b84 | 255 | (SCM alist, SCM key), |
b380b885 | 256 | "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.") |
1bbd0b84 | 257 | #define FUNC_NAME s_scm_assoc_ref |
0f2d19dd JB |
258 | { |
259 | SCM handle; | |
260 | ||
261 | handle = scm_sloppy_assoc (key, alist); | |
0c95b57d | 262 | if (SCM_CONSP (handle)) |
0f2d19dd JB |
263 | { |
264 | return SCM_CDR (handle); | |
265 | } | |
266 | return SCM_BOOL_F; | |
267 | } | |
1bbd0b84 | 268 | #undef FUNC_NAME |
0f2d19dd JB |
269 | |
270 | ||
271 | ||
272 | \f | |
273 | ||
274 | ||
a1ec6916 | 275 | SCM_DEFINE (scm_assq_set_x, "assq-set!", 3, 0, 0, |
1bbd0b84 | 276 | (SCM alist, SCM key, SCM val), |
8f85c0c6 NJ |
277 | "@deffnx {Scheme Procedure} assv-set! alist key value\n" |
278 | "@deffnx {Scheme Procedure} assoc-set! alist key value\n" | |
b380b885 MD |
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.") | |
1bbd0b84 | 286 | #define FUNC_NAME s_scm_assq_set_x |
0f2d19dd JB |
287 | { |
288 | SCM handle; | |
289 | ||
290 | handle = scm_sloppy_assq (key, alist); | |
0c95b57d | 291 | if (SCM_CONSP (handle)) |
0f2d19dd JB |
292 | { |
293 | SCM_SETCDR (handle, val); | |
294 | return alist; | |
295 | } | |
296 | else | |
297 | return scm_acons (key, val, alist); | |
298 | } | |
1bbd0b84 | 299 | #undef FUNC_NAME |
0f2d19dd | 300 | |
a1ec6916 | 301 | SCM_DEFINE (scm_assv_set_x, "assv-set!", 3, 0, 0, |
1bbd0b84 | 302 | (SCM alist, SCM key, SCM val), |
b380b885 | 303 | "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.") |
1bbd0b84 | 304 | #define FUNC_NAME s_scm_assv_set_x |
0f2d19dd JB |
305 | { |
306 | SCM handle; | |
307 | ||
308 | handle = scm_sloppy_assv (key, alist); | |
0c95b57d | 309 | if (SCM_CONSP (handle)) |
0f2d19dd JB |
310 | { |
311 | SCM_SETCDR (handle, val); | |
312 | return alist; | |
313 | } | |
314 | else | |
315 | return scm_acons (key, val, alist); | |
316 | } | |
1bbd0b84 | 317 | #undef FUNC_NAME |
0f2d19dd | 318 | |
a1ec6916 | 319 | SCM_DEFINE (scm_assoc_set_x, "assoc-set!", 3, 0, 0, |
1bbd0b84 | 320 | (SCM alist, SCM key, SCM val), |
b380b885 | 321 | "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.") |
1bbd0b84 | 322 | #define FUNC_NAME s_scm_assoc_set_x |
0f2d19dd JB |
323 | { |
324 | SCM handle; | |
325 | ||
326 | handle = scm_sloppy_assoc (key, alist); | |
0c95b57d | 327 | if (SCM_CONSP (handle)) |
0f2d19dd JB |
328 | { |
329 | SCM_SETCDR (handle, val); | |
330 | return alist; | |
331 | } | |
332 | else | |
333 | return scm_acons (key, val, alist); | |
334 | } | |
1bbd0b84 | 335 | #undef FUNC_NAME |
0f2d19dd JB |
336 | |
337 | ||
338 | \f | |
339 | ||
a1ec6916 | 340 | SCM_DEFINE (scm_assq_remove_x, "assq-remove!", 2, 0, 0, |
1bbd0b84 | 341 | (SCM alist, SCM key), |
8f85c0c6 NJ |
342 | "@deffnx {Scheme Procedure} assv-remove! alist key\n" |
343 | "@deffnx {Scheme Procedure} assoc-remove! alist key\n" | |
623ada63 | 344 | "Delete the first entry in @var{alist} associated with @var{key}, and return\n" |
b380b885 | 345 | "the resulting alist.") |
1bbd0b84 | 346 | #define FUNC_NAME s_scm_assq_remove_x |
0f2d19dd JB |
347 | { |
348 | SCM handle; | |
349 | ||
350 | handle = scm_sloppy_assq (key, alist); | |
623ada63 | 351 | if (SCM_CONSP (handle)) |
60e61f0a | 352 | alist = scm_delq1_x (handle, alist); |
623ada63 | 353 | |
5d253852 | 354 | return alist; |
0f2d19dd | 355 | } |
1bbd0b84 | 356 | #undef FUNC_NAME |
0f2d19dd JB |
357 | |
358 | ||
a1ec6916 | 359 | SCM_DEFINE (scm_assv_remove_x, "assv-remove!", 2, 0, 0, |
1bbd0b84 | 360 | (SCM alist, SCM key), |
b380b885 | 361 | "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.") |
1bbd0b84 | 362 | #define FUNC_NAME s_scm_assv_remove_x |
0f2d19dd JB |
363 | { |
364 | SCM handle; | |
365 | ||
366 | handle = scm_sloppy_assv (key, alist); | |
623ada63 | 367 | if (SCM_CONSP (handle)) |
60e61f0a | 368 | alist = scm_delq1_x (handle, alist); |
623ada63 | 369 | |
5d253852 | 370 | return alist; |
0f2d19dd | 371 | } |
1bbd0b84 | 372 | #undef FUNC_NAME |
0f2d19dd JB |
373 | |
374 | ||
a1ec6916 | 375 | SCM_DEFINE (scm_assoc_remove_x, "assoc-remove!", 2, 0, 0, |
1bbd0b84 | 376 | (SCM alist, SCM key), |
b380b885 | 377 | "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.") |
1bbd0b84 | 378 | #define FUNC_NAME s_scm_assoc_remove_x |
0f2d19dd JB |
379 | { |
380 | SCM handle; | |
381 | ||
382 | handle = scm_sloppy_assoc (key, alist); | |
623ada63 | 383 | if (SCM_CONSP (handle)) |
60e61f0a | 384 | alist = scm_delq1_x (handle, alist); |
623ada63 | 385 | |
5d253852 | 386 | return alist; |
0f2d19dd | 387 | } |
1bbd0b84 | 388 | #undef FUNC_NAME |
0f2d19dd JB |
389 | |
390 | ||
391 | \f | |
392 | ||
393 | ||
1cc91f1b | 394 | |
0f2d19dd JB |
395 | void |
396 | scm_init_alist () | |
0f2d19dd | 397 | { |
8dc9439f | 398 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 399 | #include "libguile/alist.x" |
8dc9439f | 400 | #endif |
0f2d19dd JB |
401 | } |
402 | ||
89e00824 ML |
403 | |
404 | /* | |
405 | Local Variables: | |
406 | c-file-style: "gnu" | |
407 | End: | |
408 | */ |