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