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