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