Add `ChangeLog-2008' files to the distribution.
[bpt/guile.git] / libguile / alist.c
CommitLineData
2b829bbb 1/* Copyright (C) 1995, 96, 97, 98, 99, 2000, 2001, 2004, 2006 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e
MV
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
0f2d19dd 7 *
73be1d9e
MV
8 * This library 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 GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
92205699 15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
73be1d9e 16 */
1bbd0b84 17
1bbd0b84 18
0f2d19dd 19\f
a0599745
MD
20#include "libguile/_scm.h"
21#include "libguile/eq.h"
22#include "libguile/list.h"
c96d76b8 23#include "libguile/lang.h"
20e6290e 24
a0599745 25#include "libguile/validate.h"
d2e53ed6 26#include "libguile/pairs.h"
a0599745 27#include "libguile/alist.h"
0f2d19dd
JB
28
29\f
30
3b3b36dd 31SCM_DEFINE (scm_acons, "acons", 3, 0, 0,
0b5f3f34 32 (SCM key, SCM value, SCM alist),
8f85c0c6 33 "Add a new key-value pair to @var{alist}. A new pair is\n"
b380b885
MD
34 "created whose car is @var{key} and whose cdr is @var{value}, and the\n"
35 "pair is consed onto @var{alist}, and the new list is returned. This\n"
36 "function is @emph{not} destructive; @var{alist} is not modified.")
1bbd0b84 37#define FUNC_NAME s_scm_acons
0f2d19dd 38{
228a24ef
DH
39 return scm_cell (SCM_UNPACK (scm_cell (SCM_UNPACK (key),
40 SCM_UNPACK (value))),
41 SCM_UNPACK (alist));
0f2d19dd 42}
1bbd0b84 43#undef FUNC_NAME
0f2d19dd
JB
44
45\f
46
a1ec6916 47SCM_DEFINE (scm_sloppy_assq, "sloppy-assq", 2, 0, 0,
0b5f3f34 48 (SCM key, SCM alist),
b380b885
MD
49 "Behaves like @code{assq} but does not do any error checking.\n"
50 "Recommended only for use in Guile internals.")
1bbd0b84 51#define FUNC_NAME s_scm_sloppy_assq
0f2d19dd 52{
d2e53ed6 53 for (; scm_is_pair (alist); alist = SCM_CDR (alist))
0f2d19dd 54 {
2de257bd 55 SCM tmp = SCM_CAR (alist);
d2e53ed6 56 if (scm_is_pair (tmp) && scm_is_eq (SCM_CAR (tmp), key))
cf18adf0 57 return tmp;
0f2d19dd
JB
58 }
59 return SCM_BOOL_F;
60}
1bbd0b84 61#undef FUNC_NAME
0f2d19dd
JB
62
63
64
a1ec6916 65SCM_DEFINE (scm_sloppy_assv, "sloppy-assv", 2, 0, 0,
0b5f3f34 66 (SCM key, SCM alist),
b380b885
MD
67 "Behaves like @code{assv} but does not do any error checking.\n"
68 "Recommended only for use in Guile internals.")
1bbd0b84 69#define FUNC_NAME s_scm_sloppy_assv
0f2d19dd 70{
d2e53ed6 71 for (; scm_is_pair (alist); alist = SCM_CDR (alist))
0f2d19dd 72 {
2de257bd 73 SCM tmp = SCM_CAR (alist);
d2e53ed6 74 if (scm_is_pair (tmp)
7888309b 75 && scm_is_true (scm_eqv_p (SCM_CAR (tmp), key)))
cf18adf0 76 return tmp;
0f2d19dd
JB
77 }
78 return SCM_BOOL_F;
79}
1bbd0b84 80#undef FUNC_NAME
0f2d19dd
JB
81
82
a1ec6916 83SCM_DEFINE (scm_sloppy_assoc, "sloppy-assoc", 2, 0, 0,
0b5f3f34 84 (SCM key, SCM alist),
b380b885
MD
85 "Behaves like @code{assoc} but does not do any error checking.\n"
86 "Recommended only for use in Guile internals.")
1bbd0b84 87#define FUNC_NAME s_scm_sloppy_assoc
0f2d19dd 88{
d2e53ed6 89 for (; scm_is_pair (alist); alist = SCM_CDR (alist))
0f2d19dd 90 {
2de257bd 91 SCM tmp = SCM_CAR (alist);
d2e53ed6 92 if (scm_is_pair (tmp)
7888309b 93 && scm_is_true (scm_equal_p (SCM_CAR (tmp), key)))
cf18adf0 94 return tmp;
0f2d19dd
JB
95 }
96 return SCM_BOOL_F;
97}
1bbd0b84 98#undef FUNC_NAME
0f2d19dd
JB
99
100
101\f
102
3b3b36dd 103SCM_DEFINE (scm_assq, "assq", 2, 0, 0,
0b5f3f34 104 (SCM key, SCM alist),
8f85c0c6
NJ
105 "@deffnx {Scheme Procedure} assv key alist\n"
106 "@deffnx {Scheme Procedure} assoc key alist\n"
107 "Fetch the entry in @var{alist} that is associated with @var{key}. To\n"
b380b885
MD
108 "decide whether the argument @var{key} matches a particular entry in\n"
109 "@var{alist}, @code{assq} compares keys with @code{eq?}, @code{assv}\n"
110 "uses @code{eqv?} and @code{assoc} uses @code{equal?}. If @var{key}\n"
111 "cannot be found in @var{alist} (according to whichever equality\n"
8f85c0c6 112 "predicate is in use), then return @code{#f}. These functions\n"
b380b885 113 "return the entire alist entry found (i.e. both the key and the value).")
1bbd0b84 114#define FUNC_NAME s_scm_assq
0f2d19dd 115{
1aa621a3 116 SCM ls = alist;
d2e53ed6 117 for(; scm_is_pair (ls); ls = SCM_CDR (ls))
e1385ffc 118 {
1aa621a3 119 SCM tmp = SCM_CAR (ls);
d2e53ed6 120 SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
1aa621a3 121 "association list");
bc36d050 122 if (scm_is_eq (SCM_CAR (tmp), key))
2de257bd 123 return tmp;
e1385ffc 124 }
c96d76b8 125 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
1aa621a3 126 "association list");
1bbd0b84 127 return SCM_BOOL_F;
0f2d19dd 128}
1bbd0b84 129#undef FUNC_NAME
0f2d19dd
JB
130
131
3b3b36dd 132SCM_DEFINE (scm_assv, "assv", 2, 0, 0,
0b5f3f34 133 (SCM key, SCM alist),
b380b885 134 "Behaves like @code{assq} but uses @code{eqv?} for key comparison.")
1bbd0b84 135#define FUNC_NAME s_scm_assv
0f2d19dd 136{
1aa621a3 137 SCM ls = alist;
d2e53ed6 138 for(; scm_is_pair (ls); ls = SCM_CDR (ls))
e1385ffc 139 {
1aa621a3 140 SCM tmp = SCM_CAR (ls);
d2e53ed6 141 SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
1aa621a3 142 "association list");
7888309b 143 if (scm_is_true (scm_eqv_p (SCM_CAR (tmp), key)))
2de257bd 144 return tmp;
e1385ffc 145 }
c96d76b8 146 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
1aa621a3 147 "association list");
0f2d19dd
JB
148 return SCM_BOOL_F;
149}
1bbd0b84 150#undef FUNC_NAME
0f2d19dd
JB
151
152
3b3b36dd 153SCM_DEFINE (scm_assoc, "assoc", 2, 0, 0,
0b5f3f34 154 (SCM key, SCM alist),
b380b885 155 "Behaves like @code{assq} but uses @code{equal?} for key comparison.")
1bbd0b84 156#define FUNC_NAME s_scm_assoc
0f2d19dd 157{
1aa621a3 158 SCM ls = alist;
d2e53ed6 159 for(; scm_is_pair (ls); ls = SCM_CDR (ls))
e1385ffc 160 {
1aa621a3 161 SCM tmp = SCM_CAR (ls);
d2e53ed6 162 SCM_ASSERT_TYPE (scm_is_pair (tmp), alist, SCM_ARG2, FUNC_NAME,
1aa621a3 163 "association list");
7888309b 164 if (scm_is_true (scm_equal_p (SCM_CAR (tmp), key)))
2de257bd 165 return tmp;
e1385ffc 166 }
c96d76b8 167 SCM_ASSERT_TYPE (SCM_NULL_OR_NIL_P (ls), alist, SCM_ARG2, FUNC_NAME,
1aa621a3 168 "association list");
1bbd0b84 169 return SCM_BOOL_F;
0f2d19dd 170}
1bbd0b84 171#undef FUNC_NAME
0f2d19dd
JB
172
173
174\f
175
d1ca2c64
DH
176/* Dirk:API2.0:: We should not return #f if the key was not found. In the
177 * current solution we can not distinguish between finding a (key . #f) pair
178 * and not finding the key at all.
179 *
180 * Possible alternative solutions:
181 * 1) Remove assq-ref from the API: assq is sufficient.
182 * 2) Signal an error (what error type?) if the key is not found.
183 * 3) provide an additional 'default' parameter.
184 * 3.1) The default parameter is mandatory.
185 * 3.2) The default parameter is optional, but if no default is given and
186 * the key is not found, signal an error (what error type?).
187 */
a1ec6916 188SCM_DEFINE (scm_assq_ref, "assq-ref", 2, 0, 0,
1bbd0b84 189 (SCM alist, SCM key),
8f85c0c6
NJ
190 "@deffnx {Scheme Procedure} assv-ref alist key\n"
191 "@deffnx {Scheme Procedure} assoc-ref alist key\n"
b380b885
MD
192 "Like @code{assq}, @code{assv} and @code{assoc}, except that only the\n"
193 "value associated with @var{key} in @var{alist} is returned. These\n"
194 "functions are equivalent to\n\n"
195 "@lisp\n"
196 "(let ((ent (@var{associator} @var{key} @var{alist})))\n"
197 " (and ent (cdr ent)))\n"
198 "@end lisp\n\n"
199 "where @var{associator} is one of @code{assq}, @code{assv} or @code{assoc}.")
1bbd0b84 200#define FUNC_NAME s_scm_assq_ref
0f2d19dd
JB
201{
202 SCM handle;
203
204 handle = scm_sloppy_assq (key, alist);
d2e53ed6 205 if (scm_is_pair (handle))
0f2d19dd
JB
206 {
207 return SCM_CDR (handle);
208 }
209 return SCM_BOOL_F;
210}
1bbd0b84 211#undef FUNC_NAME
0f2d19dd
JB
212
213
a1ec6916 214SCM_DEFINE (scm_assv_ref, "assv-ref", 2, 0, 0,
1bbd0b84 215 (SCM alist, SCM key),
b380b885 216 "Behaves like @code{assq-ref} but uses @code{eqv?} for key comparison.")
1bbd0b84 217#define FUNC_NAME s_scm_assv_ref
0f2d19dd
JB
218{
219 SCM handle;
220
221 handle = scm_sloppy_assv (key, alist);
d2e53ed6 222 if (scm_is_pair (handle))
0f2d19dd
JB
223 {
224 return SCM_CDR (handle);
225 }
226 return SCM_BOOL_F;
227}
1bbd0b84 228#undef FUNC_NAME
0f2d19dd
JB
229
230
a1ec6916 231SCM_DEFINE (scm_assoc_ref, "assoc-ref", 2, 0, 0,
1bbd0b84 232 (SCM alist, SCM key),
b380b885 233 "Behaves like @code{assq-ref} but uses @code{equal?} for key comparison.")
1bbd0b84 234#define FUNC_NAME s_scm_assoc_ref
0f2d19dd
JB
235{
236 SCM handle;
237
238 handle = scm_sloppy_assoc (key, alist);
d2e53ed6 239 if (scm_is_pair (handle))
0f2d19dd
JB
240 {
241 return SCM_CDR (handle);
242 }
243 return SCM_BOOL_F;
244}
1bbd0b84 245#undef FUNC_NAME
0f2d19dd
JB
246
247
248
249\f
250
251
a1ec6916 252SCM_DEFINE (scm_assq_set_x, "assq-set!", 3, 0, 0,
1bbd0b84 253 (SCM alist, SCM key, SCM val),
8f85c0c6
NJ
254 "@deffnx {Scheme Procedure} assv-set! alist key value\n"
255 "@deffnx {Scheme Procedure} assoc-set! alist key value\n"
b380b885
MD
256 "Reassociate @var{key} in @var{alist} with @var{value}: find any existing\n"
257 "@var{alist} entry for @var{key} and associate it with the new\n"
258 "@var{value}. If @var{alist} does not contain an entry for @var{key},\n"
259 "add a new one. Return the (possibly new) alist.\n\n"
260 "These functions do not attempt to verify the structure of @var{alist},\n"
261 "and so may cause unusual results if passed an object that is not an\n"
262 "association list.")
1bbd0b84 263#define FUNC_NAME s_scm_assq_set_x
0f2d19dd
JB
264{
265 SCM handle;
266
267 handle = scm_sloppy_assq (key, alist);
d2e53ed6 268 if (scm_is_pair (handle))
0f2d19dd
JB
269 {
270 SCM_SETCDR (handle, val);
271 return alist;
272 }
273 else
274 return scm_acons (key, val, alist);
275}
1bbd0b84 276#undef FUNC_NAME
0f2d19dd 277
a1ec6916 278SCM_DEFINE (scm_assv_set_x, "assv-set!", 3, 0, 0,
1bbd0b84 279 (SCM alist, SCM key, SCM val),
b380b885 280 "Behaves like @code{assq-set!} but uses @code{eqv?} for key comparison.")
1bbd0b84 281#define FUNC_NAME s_scm_assv_set_x
0f2d19dd
JB
282{
283 SCM handle;
284
285 handle = scm_sloppy_assv (key, alist);
d2e53ed6 286 if (scm_is_pair (handle))
0f2d19dd
JB
287 {
288 SCM_SETCDR (handle, val);
289 return alist;
290 }
291 else
292 return scm_acons (key, val, alist);
293}
1bbd0b84 294#undef FUNC_NAME
0f2d19dd 295
a1ec6916 296SCM_DEFINE (scm_assoc_set_x, "assoc-set!", 3, 0, 0,
1bbd0b84 297 (SCM alist, SCM key, SCM val),
b380b885 298 "Behaves like @code{assq-set!} but uses @code{equal?} for key comparison.")
1bbd0b84 299#define FUNC_NAME s_scm_assoc_set_x
0f2d19dd
JB
300{
301 SCM handle;
302
303 handle = scm_sloppy_assoc (key, alist);
d2e53ed6 304 if (scm_is_pair (handle))
0f2d19dd
JB
305 {
306 SCM_SETCDR (handle, val);
307 return alist;
308 }
309 else
310 return scm_acons (key, val, alist);
311}
1bbd0b84 312#undef FUNC_NAME
0f2d19dd
JB
313
314
315\f
316
a1ec6916 317SCM_DEFINE (scm_assq_remove_x, "assq-remove!", 2, 0, 0,
1bbd0b84 318 (SCM alist, SCM key),
8f85c0c6
NJ
319 "@deffnx {Scheme Procedure} assv-remove! alist key\n"
320 "@deffnx {Scheme Procedure} assoc-remove! alist key\n"
623ada63 321 "Delete the first entry in @var{alist} associated with @var{key}, and return\n"
b380b885 322 "the resulting alist.")
1bbd0b84 323#define FUNC_NAME s_scm_assq_remove_x
0f2d19dd
JB
324{
325 SCM handle;
326
327 handle = scm_sloppy_assq (key, alist);
d2e53ed6 328 if (scm_is_pair (handle))
60e61f0a 329 alist = scm_delq1_x (handle, alist);
623ada63 330
5d253852 331 return alist;
0f2d19dd 332}
1bbd0b84 333#undef FUNC_NAME
0f2d19dd
JB
334
335
a1ec6916 336SCM_DEFINE (scm_assv_remove_x, "assv-remove!", 2, 0, 0,
1bbd0b84 337 (SCM alist, SCM key),
b380b885 338 "Behaves like @code{assq-remove!} but uses @code{eqv?} for key comparison.")
1bbd0b84 339#define FUNC_NAME s_scm_assv_remove_x
0f2d19dd
JB
340{
341 SCM handle;
342
343 handle = scm_sloppy_assv (key, alist);
d2e53ed6 344 if (scm_is_pair (handle))
60e61f0a 345 alist = scm_delq1_x (handle, alist);
623ada63 346
5d253852 347 return alist;
0f2d19dd 348}
1bbd0b84 349#undef FUNC_NAME
0f2d19dd
JB
350
351
a1ec6916 352SCM_DEFINE (scm_assoc_remove_x, "assoc-remove!", 2, 0, 0,
1bbd0b84 353 (SCM alist, SCM key),
b380b885 354 "Behaves like @code{assq-remove!} but uses @code{equal?} for key comparison.")
1bbd0b84 355#define FUNC_NAME s_scm_assoc_remove_x
0f2d19dd
JB
356{
357 SCM handle;
358
359 handle = scm_sloppy_assoc (key, alist);
d2e53ed6 360 if (scm_is_pair (handle))
60e61f0a 361 alist = scm_delq1_x (handle, alist);
623ada63 362
5d253852 363 return alist;
0f2d19dd 364}
1bbd0b84 365#undef FUNC_NAME
0f2d19dd
JB
366
367
368\f
369
370
1cc91f1b 371
0f2d19dd
JB
372void
373scm_init_alist ()
0f2d19dd 374{
a0599745 375#include "libguile/alist.x"
0f2d19dd
JB
376}
377
89e00824
ML
378
379/*
380 Local Variables:
381 c-file-style: "gnu"
382 End:
383*/