* __scm.h, alist.c, async.c, async.h, backtrace.h, chars.c,
[bpt/guile.git] / libguile / alist.c
CommitLineData
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. */
0f2d19dd
JB
41\f
42#include <stdio.h>
43#include "_scm.h"
20e6290e
JB
44#include "eq.h"
45#include "list.h"
46
47#include "alist.h"
0f2d19dd
JB
48
49\f
50
51SCM_PROC(s_acons, "acons", 3, 0, 0, scm_acons);
1cc91f1b 52
0f2d19dd
JB
53SCM
54scm_acons (w, x, y)
55 SCM w;
56 SCM x;
57 SCM y;
0f2d19dd
JB
58{
59 register SCM z;
60 SCM_NEWCELL (z);
a6c64c3c
MD
61 SCM_SETCAR (z, w);
62 SCM_SETCDR (z, x);
0f2d19dd
JB
63 x = z;
64 SCM_NEWCELL (z);
a6c64c3c
MD
65 SCM_SETCAR (z, x);
66 SCM_SETCDR (z, y);
0f2d19dd
JB
67 return z;
68}
69
70\f
71
72SCM_PROC (s_sloppy_assq, "sloppy-assq", 2, 0, 0, scm_sloppy_assq);
1cc91f1b 73
0f2d19dd
JB
74SCM
75scm_sloppy_assq(x, alist)
76 SCM x;
77 SCM alist;
0f2d19dd 78{
cf18adf0
JB
79
80 for (; SCM_NIMP (alist) && SCM_CONSP (alist); alist = SCM_CDR (alist))
0f2d19dd 81 {
cf18adf0
JB
82 SCM tmp = SCM_CAR(alist);
83 if (SCM_NIMP (tmp) && SCM_CONSP (tmp) && (SCM_CAR (tmp)==x))
84 return tmp;
0f2d19dd
JB
85 }
86 return SCM_BOOL_F;
87}
88
89
90
91SCM_PROC (s_sloppy_assv, "sloppy-assv", 2, 0, 0, scm_sloppy_assv);
1cc91f1b 92
0f2d19dd
JB
93SCM
94scm_sloppy_assv(x, alist)
95 SCM x;
96 SCM alist;
0f2d19dd 97{
cf18adf0 98 for (; SCM_NIMP (alist) && SCM_CONSP (alist); alist = SCM_CDR (alist))
0f2d19dd 99 {
cf18adf0
JB
100 SCM tmp = SCM_CAR(alist);
101 if (SCM_NIMP (tmp)
102 && SCM_CONSP (tmp)
103 && SCM_NFALSEP (scm_eqv_p (SCM_CAR (tmp), x)))
104 return tmp;
0f2d19dd
JB
105 }
106 return SCM_BOOL_F;
107}
108
109
110SCM_PROC (s_sloppy_assoc, "sloppy-assoc", 2, 0, 0, scm_sloppy_assoc);
1cc91f1b 111
0f2d19dd
JB
112SCM
113scm_sloppy_assoc(x, alist)
114 SCM x;
115 SCM alist;
0f2d19dd 116{
cf18adf0 117 for (; SCM_NIMP (alist) && SCM_CONSP (alist); alist = SCM_CDR (alist))
0f2d19dd 118 {
cf18adf0
JB
119 SCM tmp = SCM_CAR(alist);
120 if (SCM_NIMP (tmp)
121 && SCM_CONSP (tmp)
122 && SCM_NFALSEP (scm_equal_p (SCM_CAR (tmp), x)))
123 return tmp;
0f2d19dd
JB
124 }
125 return SCM_BOOL_F;
126}
127
128
129\f
130
131SCM_PROC(s_assq, "assq", 2, 0, 0, scm_assq);
1cc91f1b 132
0f2d19dd
JB
133SCM
134scm_assq(x, alist)
135 SCM x;
136 SCM alist;
0f2d19dd
JB
137{
138 SCM tmp;
139 for(;SCM_NIMP(alist);alist = SCM_CDR(alist)) {
140 SCM_ASSERT(SCM_CONSP(alist), alist, SCM_ARG2, s_assq);
141 tmp = SCM_CAR(alist);
142 SCM_ASSERT(SCM_NIMP(tmp) && SCM_CONSP(tmp), alist, SCM_ARG2, s_assq);
143 if (SCM_CAR(tmp)==x) return tmp;
144 }
145 SCM_ASSERT(SCM_NULLP(alist), alist, SCM_ARG2, s_assq);
146 return SCM_BOOL_F;
147}
148
149
150SCM_PROC(s_assv, "assv", 2, 0, 0, scm_assv);
1cc91f1b 151
0f2d19dd
JB
152SCM
153scm_assv(x, alist)
154 SCM x;
155 SCM alist;
0f2d19dd
JB
156{
157 SCM tmp;
158 for(;SCM_NIMP(alist);alist = SCM_CDR(alist)) {
159 SCM_ASRTGO(SCM_CONSP(alist), badlst);
160 tmp = SCM_CAR(alist);
161 SCM_ASRTGO(SCM_NIMP(tmp) && SCM_CONSP(tmp), badlst);
162 if SCM_NFALSEP(scm_eqv_p(SCM_CAR(tmp), x)) return tmp;
163 }
cf7c17e9 164# ifndef SCM_RECKLESS
0f2d19dd
JB
165 if (!(SCM_NULLP(alist)))
166 badlst: scm_wta(alist, (char *)SCM_ARG2, s_assv);
167# endif
168 return SCM_BOOL_F;
169}
170
171
172SCM_PROC(s_assoc, "assoc", 2, 0, 0, scm_assoc);
1cc91f1b 173
0f2d19dd
JB
174SCM
175scm_assoc(x, alist)
176 SCM x;
177 SCM alist;
0f2d19dd
JB
178{
179 SCM tmp;
180 for(;SCM_NIMP(alist);alist = SCM_CDR(alist)) {
181 SCM_ASSERT(SCM_CONSP(alist), alist, SCM_ARG2, s_assoc);
182 tmp = SCM_CAR(alist);
183 SCM_ASSERT(SCM_NIMP(tmp) && SCM_CONSP(tmp), alist, SCM_ARG2, s_assoc);
184 if SCM_NFALSEP(scm_equal_p(SCM_CAR(tmp), x)) return tmp;
185 }
186 SCM_ASSERT(SCM_NULLP(alist), alist, SCM_ARG2, s_assoc);
187 return SCM_BOOL_F;
188}
189
190
191\f
192
193SCM_PROC (s_assq_ref, "assq-ref", 2, 0, 0, scm_assq_ref);
1cc91f1b 194
0f2d19dd
JB
195SCM
196scm_assq_ref (alist, key)
197 SCM alist;
198 SCM key;
0f2d19dd
JB
199{
200 SCM handle;
201
202 handle = scm_sloppy_assq (key, alist);
203 if (SCM_NIMP (handle) && SCM_CONSP (handle))
204 {
205 return SCM_CDR (handle);
206 }
207 return SCM_BOOL_F;
208}
209
210
211SCM_PROC (s_assv_ref, "assv-ref", 2, 0, 0, scm_assv_ref);
1cc91f1b 212
0f2d19dd
JB
213SCM
214scm_assv_ref (alist, key)
215 SCM alist;
216 SCM key;
0f2d19dd
JB
217{
218 SCM handle;
219
220 handle = scm_sloppy_assv (key, alist);
221 if (SCM_NIMP (handle) && SCM_CONSP (handle))
222 {
223 return SCM_CDR (handle);
224 }
225 return SCM_BOOL_F;
226}
227
228
229SCM_PROC (s_assoc_ref, "assoc-ref", 2, 0, 0, scm_assoc_ref);
1cc91f1b 230
0f2d19dd
JB
231SCM
232scm_assoc_ref (alist, key)
233 SCM alist;
234 SCM key;
0f2d19dd
JB
235{
236 SCM handle;
237
238 handle = scm_sloppy_assoc (key, alist);
239 if (SCM_NIMP (handle) && SCM_CONSP (handle))
240 {
241 return SCM_CDR (handle);
242 }
243 return SCM_BOOL_F;
244}
245
246
247
248\f
249
250
251SCM_PROC (s_assq_set_x, "assq-set!", 3, 0, 0, scm_assq_set_x);
1cc91f1b 252
0f2d19dd
JB
253SCM
254scm_assq_set_x (alist, key, val)
255 SCM alist;
256 SCM key;
257 SCM val;
0f2d19dd
JB
258{
259 SCM handle;
260
261 handle = scm_sloppy_assq (key, alist);
262 if (SCM_NIMP (handle) && SCM_CONSP (handle))
263 {
264 SCM_SETCDR (handle, val);
265 return alist;
266 }
267 else
268 return scm_acons (key, val, alist);
269}
270
271SCM_PROC (s_assv_set_x, "assv-set!", 3, 0, 0, scm_assv_set_x);
1cc91f1b 272
0f2d19dd
JB
273SCM
274scm_assv_set_x (alist, key, val)
275 SCM alist;
276 SCM key;
277 SCM val;
0f2d19dd
JB
278{
279 SCM handle;
280
281 handle = scm_sloppy_assv (key, alist);
282 if (SCM_NIMP (handle) && SCM_CONSP (handle))
283 {
284 SCM_SETCDR (handle, val);
285 return alist;
286 }
287 else
288 return scm_acons (key, val, alist);
289}
290
291SCM_PROC (s_assoc_set_x, "assoc-set!", 3, 0, 0, scm_assoc_set_x);
1cc91f1b 292
0f2d19dd
JB
293SCM
294scm_assoc_set_x (alist, key, val)
295 SCM alist;
296 SCM key;
297 SCM val;
0f2d19dd
JB
298{
299 SCM handle;
300
301 handle = scm_sloppy_assoc (key, alist);
302 if (SCM_NIMP (handle) && SCM_CONSP (handle))
303 {
304 SCM_SETCDR (handle, val);
305 return alist;
306 }
307 else
308 return scm_acons (key, val, alist);
309}
310
311
312\f
313
314SCM_PROC (s_assq_remove_x, "assq-remove!", 2, 0, 0, scm_assq_remove_x);
1cc91f1b 315
0f2d19dd
JB
316SCM
317scm_assq_remove_x (alist, key)
318 SCM alist;
319 SCM key;
0f2d19dd
JB
320{
321 SCM handle;
322
323 handle = scm_sloppy_assq (key, alist);
324 if (SCM_NIMP (handle) && SCM_CONSP (handle))
325 {
326 return scm_delq_x (handle, alist);
327 }
328 else
329 return alist;
330}
331
332
333SCM_PROC (s_assv_remove_x, "assv-remove!", 2, 0, 0, scm_assv_remove_x);
1cc91f1b 334
0f2d19dd
JB
335SCM
336scm_assv_remove_x (alist, key)
337 SCM alist;
338 SCM key;
0f2d19dd
JB
339{
340 SCM handle;
341
342 handle = scm_sloppy_assv (key, alist);
343 if (SCM_NIMP (handle) && SCM_CONSP (handle))
344 {
345 return scm_delv_x (handle, alist);
346 }
347 else
348 return alist;
349}
350
351
352SCM_PROC (s_assoc_remove_x, "assoc-remove!", 2, 0, 0, scm_assoc_remove_x);
1cc91f1b 353
0f2d19dd
JB
354SCM
355scm_assoc_remove_x (alist, key)
356 SCM alist;
357 SCM key;
0f2d19dd
JB
358{
359 SCM handle;
360
361 handle = scm_sloppy_assoc (key, alist);
362 if (SCM_NIMP (handle) && SCM_CONSP (handle))
363 {
364 return scm_delete_x (handle, alist);
365 }
366 else
367 return alist;
368}
369
370
371\f
372
373
1cc91f1b 374
0f2d19dd
JB
375void
376scm_init_alist ()
0f2d19dd
JB
377{
378#include "alist.x"
379}
380