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