maintainer changed: was lord, now jimb; first import
[bpt/guile.git] / libguile / alist.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42#include <stdio.h>
43#include "_scm.h"
44
45\f
46
47SCM_PROC(s_acons, "acons", 3, 0, 0, scm_acons);
48#ifdef __STDC__
49SCM
50scm_acons (SCM w, SCM x, SCM y)
51#else
52SCM
53scm_acons (w, x, y)
54 SCM w;
55 SCM x;
56 SCM y;
57#endif
58{
59 register SCM z;
60 SCM_NEWCELL (z);
61 SCM_CAR (z) = w;
62 SCM_CDR (z) = x;
63 x = z;
64 SCM_NEWCELL (z);
65 SCM_CAR (z) = x;
66 SCM_CDR (z) = y;
67 return z;
68}
69
70\f
71
72SCM_PROC (s_sloppy_assq, "sloppy-assq", 2, 0, 0, scm_sloppy_assq);
73#ifdef __STDC__
74SCM
75scm_sloppy_assq(SCM x, SCM alist)
76#else
77SCM
78scm_sloppy_assq(x, alist)
79 SCM x;
80 SCM alist;
81#endif
82{
83 SCM tmp;
84 for(;SCM_NIMP(alist);alist = SCM_CDR(alist))
85 {
86 if (SCM_CONSP(alist))
87 {
88 tmp = SCM_CAR(alist);
89 if (SCM_NIMP (tmp) && SCM_CONSP (tmp) && (SCM_CAR (tmp)==x))
90 return tmp;
91 }
92 }
93 return SCM_BOOL_F;
94}
95
96
97
98SCM_PROC (s_sloppy_assv, "sloppy-assv", 2, 0, 0, scm_sloppy_assv);
99#ifdef __STDC__
100SCM
101scm_sloppy_assv(SCM x, SCM alist)
102#else
103SCM
104scm_sloppy_assv(x, alist)
105 SCM x;
106 SCM alist;
107#endif
108{
109 SCM tmp;
110 for(;SCM_NIMP(alist);alist = SCM_CDR(alist))
111 {
112 if (SCM_CONSP(alist))
113 {
114 tmp = SCM_CAR(alist);
115 if ( SCM_NIMP (tmp)
116 && SCM_CONSP (tmp)
117 && SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), x)))
118 return tmp;
119 }
120 }
121 return SCM_BOOL_F;
122}
123
124
125SCM_PROC (s_sloppy_assoc, "sloppy-assoc", 2, 0, 0, scm_sloppy_assoc);
126#ifdef __STDC__
127SCM
128scm_sloppy_assoc(SCM x, SCM alist)
129#else
130SCM
131scm_sloppy_assoc(x, alist)
132 SCM x;
133 SCM alist;
134#endif
135{
136 SCM tmp;
137 for(;SCM_NIMP(alist);alist = SCM_CDR(alist))
138 {
139 if (SCM_CONSP(alist))
140 {
141 tmp = SCM_CAR(alist);
142 if ( SCM_NIMP (tmp)
143 && SCM_CONSP (tmp)
144 && SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), x)))
145 return tmp;
146 }
147 }
148 return SCM_BOOL_F;
149}
150
151
152\f
153
154SCM_PROC(s_assq, "assq", 2, 0, 0, scm_assq);
155#ifdef __STDC__
156SCM
157scm_assq(SCM x, SCM alist)
158#else
159SCM
160scm_assq(x, alist)
161 SCM x;
162 SCM alist;
163#endif
164{
165 SCM tmp;
166 for(;SCM_NIMP(alist);alist = SCM_CDR(alist)) {
167 SCM_ASSERT(SCM_CONSP(alist), alist, SCM_ARG2, s_assq);
168 tmp = SCM_CAR(alist);
169 SCM_ASSERT(SCM_NIMP(tmp) && SCM_CONSP(tmp), alist, SCM_ARG2, s_assq);
170 if (SCM_CAR(tmp)==x) return tmp;
171 }
172 SCM_ASSERT(SCM_NULLP(alist), alist, SCM_ARG2, s_assq);
173 return SCM_BOOL_F;
174}
175
176
177SCM_PROC(s_assv, "assv", 2, 0, 0, scm_assv);
178#ifdef __STDC__
179SCM
180scm_assv(SCM x, SCM alist)
181#else
182SCM
183scm_assv(x, alist)
184 SCM x;
185 SCM alist;
186#endif
187{
188 SCM tmp;
189 for(;SCM_NIMP(alist);alist = SCM_CDR(alist)) {
190 SCM_ASRTGO(SCM_CONSP(alist), badlst);
191 tmp = SCM_CAR(alist);
192 SCM_ASRTGO(SCM_NIMP(tmp) && SCM_CONSP(tmp), badlst);
193 if SCM_NFALSEP(scm_eqv_p(SCM_CAR(tmp), x)) return tmp;
194 }
195# ifndef RECKLESS
196 if (!(SCM_NULLP(alist)))
197 badlst: scm_wta(alist, (char *)SCM_ARG2, s_assv);
198# endif
199 return SCM_BOOL_F;
200}
201
202
203SCM_PROC(s_assoc, "assoc", 2, 0, 0, scm_assoc);
204#ifdef __STDC__
205SCM
206scm_assoc(SCM x, SCM alist)
207#else
208SCM
209scm_assoc(x, alist)
210 SCM x;
211 SCM alist;
212#endif
213{
214 SCM tmp;
215 for(;SCM_NIMP(alist);alist = SCM_CDR(alist)) {
216 SCM_ASSERT(SCM_CONSP(alist), alist, SCM_ARG2, s_assoc);
217 tmp = SCM_CAR(alist);
218 SCM_ASSERT(SCM_NIMP(tmp) && SCM_CONSP(tmp), alist, SCM_ARG2, s_assoc);
219 if SCM_NFALSEP(scm_equal_p(SCM_CAR(tmp), x)) return tmp;
220 }
221 SCM_ASSERT(SCM_NULLP(alist), alist, SCM_ARG2, s_assoc);
222 return SCM_BOOL_F;
223}
224
225
226\f
227
228SCM_PROC (s_assq_ref, "assq-ref", 2, 0, 0, scm_assq_ref);
229#ifdef __STDC__
230SCM
231scm_assq_ref (SCM alist, SCM key)
232#else
233SCM
234scm_assq_ref (alist, key)
235 SCM alist;
236 SCM key;
237#endif
238{
239 SCM handle;
240
241 handle = scm_sloppy_assq (key, alist);
242 if (SCM_NIMP (handle) && SCM_CONSP (handle))
243 {
244 return SCM_CDR (handle);
245 }
246 return SCM_BOOL_F;
247}
248
249
250SCM_PROC (s_assv_ref, "assv-ref", 2, 0, 0, scm_assv_ref);
251#ifdef __STDC__
252SCM
253scm_assv_ref (SCM alist, SCM key)
254#else
255SCM
256scm_assv_ref (alist, key)
257 SCM alist;
258 SCM key;
259#endif
260{
261 SCM handle;
262
263 handle = scm_sloppy_assv (key, alist);
264 if (SCM_NIMP (handle) && SCM_CONSP (handle))
265 {
266 return SCM_CDR (handle);
267 }
268 return SCM_BOOL_F;
269}
270
271
272SCM_PROC (s_assoc_ref, "assoc-ref", 2, 0, 0, scm_assoc_ref);
273#ifdef __STDC__
274SCM
275scm_assoc_ref (SCM alist, SCM key)
276#else
277SCM
278scm_assoc_ref (alist, key)
279 SCM alist;
280 SCM key;
281#endif
282{
283 SCM handle;
284
285 handle = scm_sloppy_assoc (key, alist);
286 if (SCM_NIMP (handle) && SCM_CONSP (handle))
287 {
288 return SCM_CDR (handle);
289 }
290 return SCM_BOOL_F;
291}
292
293
294
295\f
296
297
298SCM_PROC (s_assq_set_x, "assq-set!", 3, 0, 0, scm_assq_set_x);
299#ifdef __STDC__
300SCM
301scm_assq_set_x (SCM alist, SCM key, SCM val)
302#else
303SCM
304scm_assq_set_x (alist, key, val)
305 SCM alist;
306 SCM key;
307 SCM val;
308#endif
309{
310 SCM handle;
311
312 handle = scm_sloppy_assq (key, alist);
313 if (SCM_NIMP (handle) && SCM_CONSP (handle))
314 {
315 SCM_SETCDR (handle, val);
316 return alist;
317 }
318 else
319 return scm_acons (key, val, alist);
320}
321
322SCM_PROC (s_assv_set_x, "assv-set!", 3, 0, 0, scm_assv_set_x);
323#ifdef __STDC__
324SCM
325scm_assv_set_x (SCM alist, SCM key, SCM val)
326#else
327SCM
328scm_assv_set_x (alist, key, val)
329 SCM alist;
330 SCM key;
331 SCM val;
332#endif
333{
334 SCM handle;
335
336 handle = scm_sloppy_assv (key, alist);
337 if (SCM_NIMP (handle) && SCM_CONSP (handle))
338 {
339 SCM_SETCDR (handle, val);
340 return alist;
341 }
342 else
343 return scm_acons (key, val, alist);
344}
345
346SCM_PROC (s_assoc_set_x, "assoc-set!", 3, 0, 0, scm_assoc_set_x);
347#ifdef __STDC__
348SCM
349scm_assoc_set_x (SCM alist, SCM key, SCM val)
350#else
351SCM
352scm_assoc_set_x (alist, key, val)
353 SCM alist;
354 SCM key;
355 SCM val;
356#endif
357{
358 SCM handle;
359
360 handle = scm_sloppy_assoc (key, alist);
361 if (SCM_NIMP (handle) && SCM_CONSP (handle))
362 {
363 SCM_SETCDR (handle, val);
364 return alist;
365 }
366 else
367 return scm_acons (key, val, alist);
368}
369
370
371\f
372
373SCM_PROC (s_assq_remove_x, "assq-remove!", 2, 0, 0, scm_assq_remove_x);
374#ifdef __STDC__
375SCM
376scm_assq_remove_x (SCM alist, SCM key)
377#else
378SCM
379scm_assq_remove_x (alist, key)
380 SCM alist;
381 SCM key;
382#endif
383{
384 SCM handle;
385
386 handle = scm_sloppy_assq (key, alist);
387 if (SCM_NIMP (handle) && SCM_CONSP (handle))
388 {
389 return scm_delq_x (handle, alist);
390 }
391 else
392 return alist;
393}
394
395
396SCM_PROC (s_assv_remove_x, "assv-remove!", 2, 0, 0, scm_assv_remove_x);
397#ifdef __STDC__
398SCM
399scm_assv_remove_x (SCM alist, SCM key)
400#else
401SCM
402scm_assv_remove_x (alist, key)
403 SCM alist;
404 SCM key;
405#endif
406{
407 SCM handle;
408
409 handle = scm_sloppy_assv (key, alist);
410 if (SCM_NIMP (handle) && SCM_CONSP (handle))
411 {
412 return scm_delv_x (handle, alist);
413 }
414 else
415 return alist;
416}
417
418
419SCM_PROC (s_assoc_remove_x, "assoc-remove!", 2, 0, 0, scm_assoc_remove_x);
420#ifdef __STDC__
421SCM
422scm_assoc_remove_x (SCM alist, SCM key)
423#else
424SCM
425scm_assoc_remove_x (alist, key)
426 SCM alist;
427 SCM key;
428#endif
429{
430 SCM handle;
431
432 handle = scm_sloppy_assoc (key, alist);
433 if (SCM_NIMP (handle) && SCM_CONSP (handle))
434 {
435 return scm_delete_x (handle, alist);
436 }
437 else
438 return alist;
439}
440
441
442\f
443
444
445#ifdef __STDC__
446void
447scm_init_alist (void)
448#else
449void
450scm_init_alist ()
451#endif
452{
453#include "alist.x"
454}
455