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