Debian Codegraph @

src/c/list.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * list.d - list manipulating routines
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <ecl/ecl-inl.h>
17 #include <ecl/internal.h>
18
19 struct cl_test {
20 bool (*test_c_function)(struct cl_test *, cl_object);
21 cl_object (*key_c_function)(struct cl_test *, cl_object);
22 cl_env_ptr env;
23 cl_object key_function;
24 cl_objectfn key_fn;
25 cl_object test_function;
26 cl_objectfn test_fn;
27 cl_object item_compared;
28 };
29
30 static cl_object subst(struct cl_test *t, cl_object new_obj, cl_object tree);
31 static cl_object nsubst(struct cl_test *t, cl_object new_obj, cl_object tree);
32 static cl_object sublis(struct cl_test *t, cl_object alist, cl_object tree);
33 static cl_object nsublis(struct cl_test *t, cl_object alist, cl_object tree);
34 static cl_object do_assoc(struct cl_test *t, cl_object alist);
35
36 #define TEST(t,k) ((t)->test_c_function)((t),(k))
37 #define KEY(t,x) ((t)->key_c_function)((t),(x))
38 #define close_test(t) (void)0
39
40 static bool
41 test_compare(struct cl_test *t, cl_object x)
42 {
43 x = KEY(t,x);
44 t->env->function = t->test_function;
45 return t->test_fn(2, t->item_compared, x) != ECL_NIL;
46 }
47
48 static bool
49 test_compare_not(struct cl_test *t, cl_object x)
50 {
51 x = KEY(t,x);
52 t->env->function = t->test_function;
53 return t->test_fn(2, t->item_compared, x) == ECL_NIL;
54 }
55
56 static bool
57 test_eq(struct cl_test *t, cl_object x)
58 {
59 return (t->item_compared == KEY(t,x));
60 }
61
62 static bool
63 test_eql(struct cl_test *t, cl_object x)
64 {
65 return ecl_eql(t->item_compared, KEY(t,x));
66 }
67
68 static bool
69 test_equal(struct cl_test *t, cl_object x)
70 {
71 return ecl_equal(t->item_compared, KEY(t,x));
72 }
73
74 static bool
75 test_equalp(struct cl_test *t, cl_object x)
76 {
77 return ecl_equalp(t->item_compared, KEY(t,x));
78 }
79
80 static cl_object
81 key_function(struct cl_test *t, cl_object x)
82 {
83 t->env->function = t->key_function;
84 return t->key_fn(1,x);
85 }
86
87 static cl_object
88 key_identity(struct cl_test *t, cl_object x)
89 {
90 return x;
91 }
92
93 static void
94 setup_test(struct cl_test *t, cl_object item, cl_object test,
95 cl_object test_not, cl_object key)
96 {
97 cl_env_ptr env = t->env = ecl_process_env();
98 t->item_compared = item;
99 if (test != ECL_NIL) {
100 if (test_not != ECL_NIL)
101 FEerror("Both :TEST and :TEST-NOT are specified.", 0);
102 t->test_function = test = si_coerce_to_function(test);
103 if (test == ECL_SYM_FUN(@'eq')) {
104 t->test_c_function = test_eq;
105 } else if (test == ECL_SYM_FUN(@'eql')) {
106 t->test_c_function = test_eql;
107 } else if (test == ECL_SYM_FUN(@'equal')) {
108 t->test_c_function = test_equal;
109 } else if (test == ECL_SYM_FUN(@'equalp')) {
110 t->test_c_function = test_equalp;
111 } else {
112 t->test_c_function = test_compare;
113 t->test_fn = ecl_function_dispatch(env, test);
114 t->test_function = env->function;
115 }
116 } else if (test_not != ECL_NIL) {
117 t->test_c_function = test_compare_not;
118 test_not = si_coerce_to_function(test_not);
119 t->test_fn = ecl_function_dispatch(env, test_not);
120 t->test_function = env->function;
121 } else {
122 t->test_c_function = test_eql;
123 }
124 if (key != ECL_NIL) {
125 key = si_coerce_to_function(key);
126 t->key_fn = ecl_function_dispatch(env, key);
127 t->key_function = env->function;
128 t->key_c_function = key_function;
129 } else {
130 t->key_c_function = key_identity;
131 }
132 }
133
134 @(defun list (&rest args)
135 cl_object head = ECL_NIL;
136 @
137 if (narg--) {
138 cl_object tail = head = ecl_list1(ecl_va_arg(args));
139 while (narg--) {
140 cl_object cons = ecl_list1(ecl_va_arg(args));
141 ECL_RPLACD(tail, cons);
142 tail = cons;
143 }
144 }
145 @(return head);
146 @)
147
148 @(defun list* (&rest args)
149 cl_object head;
150 @
151 if (narg == 0)
152 FEwrong_num_arguments(@[list*]);
153 head = ecl_va_arg(args);
154 if (--narg) {
155 cl_object tail = head = ecl_list1(head);
156 while (--narg) {
157 cl_object cons = ecl_list1(ecl_va_arg(args));
158 ECL_RPLACD(tail, cons);
159 tail = cons;
160 }
161 ECL_RPLACD(tail, ecl_va_arg(args));
162 }
163 @(return head);
164 @)
165
166 static cl_object *
167 append_into(cl_object head, cl_object *tail, cl_object l)
168 {
169 if (!Null(*tail)) {
170 /* (APPEND '(1 . 2) 3) */
171 FEtype_error_proper_list(head);
172 }
173 while (CONSP(l)) {
174 cl_object cons = ecl_list1(ECL_CONS_CAR(l));
175 *tail = cons;
176 tail = &ECL_CONS_CDR(cons);
177 l = ECL_CONS_CDR(l);
178 }
179 *tail = l;
180 return tail;
181 }
182
183 @(defun append (&rest rest)
184 cl_object head = ECL_NIL, *tail = &head;
185 @
186 for (; narg > 1; narg--) {
187 cl_object other = ecl_va_arg(rest);
188 tail = append_into(head, tail, other);
189 }
190 if (narg) {
191 if (!Null(*tail)) {
192 /* (APPEND '(1 . 2) 3) */
193 FEtype_error_proper_list(head);
194 }
195 *tail = ecl_va_arg(rest);
196 }
197 @(return head);
198 @)
199
200 cl_object
201 ecl_append(cl_object x, cl_object y)
202 {
203 cl_object head = ECL_NIL;
204 cl_object *tail = &head;
205 if (!Null(x)) {
206 tail = append_into(head, tail, x);
207 }
208 if (!Null(*tail)) {
209 /* (APPEND '(1 . 2) 3) */
210 FEtype_error_proper_list(head);
211 }
212 *tail = y;
213 return head;
214 }
215
216 #define LENTH(n) (cl_object x) { \
217 const cl_env_ptr the_env = ecl_process_env(); \
218 ecl_return1(the_env, ecl_nth(n, x)); \
219 }
220 cl_object @fifth LENTH(4)
221 cl_object @sixth LENTH(5)
222 cl_object @seventh LENTH(6)
223 cl_object @eighth LENTH(7)
224 cl_object @ninth LENTH(8)
225 cl_object @tenth LENTH(9)
226 #undef LENTH
227
228 static bool
229 tree_equal(struct cl_test *t, cl_object x, cl_object y)
230 {
231 BEGIN:
232 if (CONSP(x)) {
233 if (CONSP(y)) {
234 if (tree_equal(t, ECL_CONS_CAR(x), ECL_CONS_CAR(y))) {
235 x = ECL_CONS_CDR(x);
236 y = ECL_CONS_CDR(y);
237 goto BEGIN;
238 } else {
239 return(FALSE);
240 }
241 } else {
242 return(FALSE);
243 }
244 } else {
245 t->item_compared = x;
246 if (TEST(t, y))
247 return(TRUE);
248 else
249 return(FALSE);
250 }
251 }
252
253 @(defun tree_equal (x y &key test test_not)
254 struct cl_test t;
255 cl_object output;
256 @
257 setup_test(&t, ECL_NIL, test, test_not, ECL_NIL);
258 output = tree_equal(&t, x, y)? ECL_T : ECL_NIL;
259 close_test(&t);
260 @(return output);
261 @)
262
263 cl_object
264 cl_endp(cl_object x)
265 {
266 cl_object output = ECL_NIL;
267 if (Null(x)) {
268 output = ECL_T;
269 } else if (ecl_unlikely(!LISTP(x))) {
270 FEwrong_type_only_arg(@[endp], x, @[list]);
271 }
272 @(return output);
273 }
274
275 bool
276 ecl_endp(cl_object x)
277 {
278 if (Null(x)) {
279 return TRUE;
280 } else if (ecl_unlikely(!LISTP(x))) {
281 FEwrong_type_only_arg(@[endp], x, @[list]);
282 }
283 return FALSE;
284 }
285
286 cl_object
287 cl_list_length(cl_object x)
288 {
289 cl_fixnum n;
290 cl_object fast, slow;
291 /* INV: A list's length always fits in a fixnum */
292 fast = slow = x;
293 for (n = 0; !Null(fast); n++, fast = ECL_CONS_CDR(fast)) {
294 if (ecl_unlikely(!LISTP(fast))) {
295 FEtype_error_list(fast);
296 }
297 if (n & 1) {
298 /* Circular list? */
299 if (slow == fast) @(return ECL_NIL);
300 slow = ECL_CONS_CDR(slow);
301 }
302 }
303 @(return ecl_make_fixnum(n));
304 }
305
306 cl_object
307 si_proper_list_p(cl_object x)
308 {
309 cl_fixnum n;
310 cl_object fast, slow, test = ECL_T;
311 /* INV: A list's length always fits in a fixnum */
312 fast = slow = x;
313 for (n = 0; !Null(fast); n++, fast = ECL_CONS_CDR(fast)) {
314 if (!LISTP(fast)) {
315 test = ECL_NIL;
316 break;
317 }
318 if (n & 1) {
319 /* Circular list? */
320 if (slow == fast) {
321 test = ECL_NIL;
322 break;
323 }
324 slow = ECL_CONS_CDR(slow);
325 }
326 }
327 @(return test);
328 }
329
330 cl_object
331 cl_nth(cl_object n, cl_object x)
332 {
333 @(return ecl_nth(ecl_to_size(n), x));
334 }
335
336 cl_object
337 ecl_nth(cl_fixnum n, cl_object x)
338 {
339 if (n < 0)
340 FEtype_error_index(x, n);
341 /* INV: No need to check for circularity since we visit
342 at most `n' conses */
343 for (; n > 0 && CONSP(x); n--)
344 x = ECL_CONS_CDR(x);
345 if (Null(x))
346 return ECL_NIL;
347 if (!LISTP(x))
348 FEtype_error_list(x);
349 return ECL_CONS_CAR(x);
350 }
351
352 cl_object
353 cl_nthcdr(cl_object n, cl_object x)
354 {
355 @(return ecl_nthcdr(ecl_to_size(n), x));
356 }
357
358 cl_object
359 ecl_nthcdr(cl_fixnum n, cl_object x)
360 {
361 if (n < 0)
362 FEtype_error_index(x, n);
363 while (n-- > 0 && !Null(x)) {
364 if (LISTP(x)) {
365 x = ECL_CONS_CDR(x);
366 } else {
367 FEtype_error_list(x);
368 }
369 }
370 return x;
371 }
372
373 cl_object
374 ecl_last(cl_object l, cl_index n)
375 {
376 /* The algorithm is very simple. We run over the list with
377 * two pointers, "l" and "r". The separation between both
378 * must be "n", so that when "l" finds no more conses, "r"
379 * contains the output. */
380 cl_object r;
381 for (r = l; n && CONSP(r); n--, r = ECL_CONS_CDR(r))
382 ;
383 /* If "l" has not moved, we have to ensure that it is a list */
384 if (r == l) {
385 if (!LISTP(r)) FEtype_error_list(l);
386 while (CONSP(r)) {
387 r = ECL_CONS_CDR(r);
388 }
389 return r;
390 } else if (n == 0) {
391 while (CONSP(r)) {
392 r = ECL_CONS_CDR(r);
393 l = ECL_CONS_CDR(l);
394 }
395 return l;
396 } else {
397 return l;
398 }
399 }
400
401 @(defun last (l &optional (k ecl_make_fixnum(1)))
402 @
403 if (ecl_t_of(k) == t_bignum) {
404 @(return l);
405 }
406 @(return ecl_last(l, ecl_to_size(k)));
407 @)
408
409 @(defun make_list (size &key initial_element &aux x)
410 cl_fixnum i;
411 @
412 /* INV: ecl_to_size() signals a type-error if SIZE is not a integer >=0 */
413 i = ecl_to_size(size);
414 while (i-- > 0)
415 x = CONS(initial_element, x);
416 @(return x);
417 @)
418
419 cl_object
420 cl_copy_list(cl_object x)
421 {
422 cl_object copy;
423 if (ecl_unlikely(!LISTP(x))) {
424 FEwrong_type_only_arg(@[copy-list], x, @[list]);
425 }
426 copy = ECL_NIL;
427 if (!Null(x)) {
428 cl_object tail = copy = ecl_list1(CAR(x));
429 while (x = ECL_CONS_CDR(x), CONSP(x)) {
430 cl_object cons = ecl_list1(ECL_CONS_CAR(x));
431 ECL_RPLACD(tail, cons);
432 tail = cons;
433 }
434 ECL_RPLACD(tail, x);
435 }
436 @(return copy);
437 }
438
439 static cl_object
440 duplicate_pairs(cl_object x)
441 {
442 cl_object p = ECL_CONS_CAR(x);
443 if (CONSP(p))
444 p = CONS(ECL_CONS_CAR(p), ECL_CONS_CDR(p));
445 return ecl_list1(p);
446 }
447
448 cl_object
449 cl_copy_alist(cl_object x)
450 {
451 cl_object copy;
452 if (ecl_unlikely(!LISTP(x))) {
453 FEwrong_type_only_arg(@[copy-alist], x, @[list]);
454 }
455 copy = ECL_NIL;
456 if (!Null(x)) {
457 cl_object tail = copy = duplicate_pairs(x);
458 while (x = ECL_CONS_CDR(x), !Null(x)) {
459 if (!LISTP(x)) {
460 FEtype_error_list(x);
461 } else {
462 cl_object cons = duplicate_pairs(x);
463 tail = ECL_RPLACD(tail, cons);
464 tail = cons;
465 }
466 }
467 }
468 @(return copy);
469 }
470
471 static cl_object
472 do_copy_tree(cl_object x)
473 {
474 if (CONSP(x)) {
475 x = CONS(do_copy_tree(ECL_CONS_CAR(x)),
476 do_copy_tree(ECL_CONS_CDR(x)));
477 }
478 return x;
479 }
480
481 cl_object
482 cl_copy_tree(cl_object x)
483 {
484 @(return do_copy_tree(x));
485 }
486
487 cl_object
488 cl_revappend(cl_object x, cl_object y)
489 {
490 loop_for_in(x) {
491 y = CONS(ECL_CONS_CAR(x),y);
492 } end_loop_for_in;
493 @(return y);
494 }
495
496 @(defun nconc (&rest lists)
497 cl_object head = ECL_NIL, tail = ECL_NIL;
498 @
499 while (narg--) {
500 cl_object new_tail, other = ecl_va_arg(lists);
501 if (Null(other)) {
502 new_tail = tail;
503 } else if (CONSP(other)) {
504 new_tail = ecl_last(other, 1);
505 } else {
506 if (narg) FEtype_error_list(other);
507 new_tail = tail;
508 }
509 if (Null(head)) {
510 head = other;
511 } else {
512 ECL_RPLACD(tail, other);
513 }
514 tail = new_tail;
515 }
516 @(return head);
517 @)
518
519 cl_object
520 ecl_nconc(cl_object l, cl_object y)
521 {
522 if (Null(l)) {
523 return y;
524 } else {
525 ECL_RPLACD(ecl_last(l, 1), y);
526 return l;
527 }
528 }
529
530 cl_object
531 cl_nreconc(cl_object l, cl_object y)
532 {
533 cl_object x, z;
534 /* INV: when a circular list is "reconc'ed", the pointer ends
535 up at the beginning of the original list, hence we need no
536 slow pointer */
537 for (x = l; !Null(x); ) {
538 if (!LISTP(x)) FEtype_error_list(x);
539 z = x;
540 x = ECL_CONS_CDR(x);
541 if (x == l) FEcircular_list(l);
542 ECL_RPLACD(z, y);
543 y = z;
544 }
545 @(return y);
546 }
547
548 cl_object
549 ecl_butlast(cl_object l, cl_index n)
550 {
551 /* See LAST for details on this algorithm */
552 cl_object r;
553 for (r = l; n && CONSP(r); n--, r = ECL_CONS_CDR(r))
554 ;
555 if (Null(r)) {
556 return ECL_NIL;
557 } else if (!LISTP(r)) {
558 /* We reach here either because l is shorter than n conses,
559 * or because it is not a list */
560 if (r == l) FEtype_error_list(r);
561 return ECL_NIL;
562 } else {
563 /* We reach here because l has at least n conses and
564 * thus we can take CAR(l) */
565 cl_object head, tail;
566 head = tail = ecl_list1(CAR(l));
567 while (l = ECL_CONS_CDR(l), r = ECL_CONS_CDR(r), CONSP(r)) {
568 cl_object cons = ecl_list1(ECL_CONS_CAR(l));
569 ECL_RPLACD(tail, cons);
570 tail = cons;
571 }
572 return head;
573 }
574 }
575
576 @(defun butlast (lis &optional (nn ecl_make_fixnum(1)))
577 @
578 /* INV: No list has more than MOST_POSITIVE_FIXNUM elements */
579 if (ecl_t_of(nn) == t_bignum) {
580 @(return ECL_NIL);
581 }
582 /* INV: ecl_to_size() signals a type-error if NN is not an integer >=0 */
583 @(return ecl_butlast(lis, ecl_to_size(nn)));
584 @)
585
586 cl_object
587 ecl_nbutlast(cl_object l, cl_index n)
588 {
589 cl_object r;
590 if (ecl_unlikely(!LISTP(l)))
591 FEwrong_type_only_arg(@[nbutlast], l, @[list]);
592 for (n++, r = l; n && CONSP(r); n--, r = ECL_CONS_CDR(r))
593 ;
594 if (n == 0) {
595 cl_object tail = l;
596 while (CONSP(r)) {
597 tail = ECL_CONS_CDR(tail);
598 r = ECL_CONS_CDR(r);
599 }
600 ECL_RPLACD(tail, ECL_NIL);
601 return l;
602 }
603 return ECL_NIL;
604 }
605
606 @(defun nbutlast (lis &optional (nn ecl_make_fixnum(1)))
607 @
608 /* INV: No list has more than MOST_POSITIVE_FIXNUM elements */
609 if (ecl_t_of(nn) == t_bignum) {
610 @(return ECL_NIL);
611 }
612 /* INV: ecl_to_size() signas a type-error if NN is not an integer >=0 */
613 @(return ecl_nbutlast(lis, ecl_to_size(nn)));
614 @)
615
616 cl_object
617 cl_ldiff(cl_object x, cl_object y)
618 {
619 cl_object head = ECL_NIL;
620 if (ecl_unlikely(!LISTP(x))) {
621 FEwrong_type_only_arg(@[ldiff], x, @[list]);
622 }
623 /* Here we use that, if X or Y are CONS, then (EQL X Y)
624 * only when X == Y */
625 if (!Null(x) && (x != y)) {
626 cl_object tail = head = ecl_list1(ECL_CONS_CAR(x));
627 while (1) {
628 x = ECL_CONS_CDR(x);
629 if (!CONSP(x)) {
630 if (!ecl_eql(x, y)) {
631 ECL_RPLACD(tail, x);
632 }
633 break;
634 } else if (x == y) {
635 break;
636 } else {
637 cl_object cons = ecl_list1(ECL_CONS_CAR(x));
638 ECL_RPLACD(tail, cons);
639 tail = cons;
640 }
641 }
642 }
643 @(return head);
644 }
645
646 cl_object
647 cl_rplaca(cl_object x, cl_object v)
648 {
649 if (ecl_unlikely(!ECL_CONSP(x)))
650 FEwrong_type_nth_arg(@[rplaca], 1, x, @[cons]);
651 ECL_RPLACA(x, v);
652 @(return x);
653 }
654
655 cl_object
656 cl_rplacd(cl_object x, cl_object v)
657 {
658 if (ecl_unlikely(!ECL_CONSP(x)))
659 FEwrong_type_nth_arg(@[rplacd], 1, x, @[cons]);
660 ECL_RPLACD(x, v);
661 @(return x);
662 }
663
664 #ifdef ECL_THREADS
665 cl_object
666 mp_compare_and_swap_car(cl_object x, cl_object old, cl_object new)
667 {
668 if (ecl_unlikely(!ECL_CONSP(x)))
669 FEwrong_type_nth_arg(@[mp::compare-and-swap-car], 1, x, @[cons]);
670 return ecl_compare_and_swap(&ECL_CONS_CAR(x), old, new);
671 }
672
673 cl_object
674 mp_atomic_incf_car(cl_object x, cl_object increment)
675 {
676 if (ecl_unlikely(!ECL_CONSP(x)))
677 FEwrong_type_nth_arg(@[mp::atomic-incf-car], 1, x, @[cons]);
678 return ecl_atomic_incf(&ECL_CONS_CAR(x), increment);
679 }
680
681 cl_object
682 mp_compare_and_swap_cdr(cl_object x, cl_object old, cl_object new)
683 {
684 if (ecl_unlikely(!ECL_CONSP(x)))
685 FEwrong_type_nth_arg(@[mp::compare-and-swap-cdr], 1, x, @[cons]);
686 return ecl_compare_and_swap(&ECL_CONS_CDR(x), old, new);
687 }
688
689 cl_object
690 mp_atomic_incf_cdr(cl_object x, cl_object increment)
691 {
692 if (ecl_unlikely(!ECL_CONSP(x)))
693 FEwrong_type_nth_arg(@[mp::atomic-incf-cdr], 1, x, @[cons]);
694 return ecl_atomic_incf(&ECL_CONS_CDR(x), increment);
695 }
696 #endif /* ECL_THREADS */
697
698 @(defun subst (new_obj old_obj tree &key test test_not key)
699 struct cl_test t;
700 cl_object output;
701 @
702 setup_test(&t, old_obj, test, test_not, key);
703 output = subst(&t, new_obj, tree);
704 close_test(&t);
705 @(return output);
706 @)
707
708
709 static cl_object
710 subst(struct cl_test *t, cl_object new_obj, cl_object tree)
711 {
712 if (TEST(t, tree)) {
713 return new_obj;
714 } else if (ECL_ATOM(tree)) {
715 return tree;
716 } else {
717 cl_object head, tail = ECL_NIL;
718 do {
719 cl_object cons = subst(t, new_obj, ECL_CONS_CAR(tree));
720 cons = ecl_cons(cons, tree = ECL_CONS_CDR(tree));
721 if (Null(tail)) {
722 head = cons;
723 } else {
724 ECL_RPLACD(tail, cons);
725 }
726 tail = cons;
727 if (TEST(t, tree)) {
728 ECL_RPLACD(tail, new_obj);
729 return head;
730 }
731 } while (CONSP(tree));
732 return head;
733 }
734 }
735
736 @(defun nsubst (new_obj old_obj tree &key test test_not key)
737 struct cl_test t;
738 @
739 setup_test(&t, old_obj, test, test_not, key);
740 tree = nsubst(&t, new_obj, tree);
741 close_test(&t);
742 @(return tree);
743 @)
744
745 static cl_object
746 nsubst_cons(struct cl_test *t, cl_object new_obj, cl_object tree)
747 {
748 cl_object l = tree;
749 do {
750 cl_object o = ECL_CONS_CAR(l);
751 if (TEST(t, o)) {
752 ECL_RPLACA(l, new_obj);
753 } else if (CONSP(o)) {
754 nsubst_cons(t, new_obj, o);
755 }
756 o = ECL_CONS_CDR(l);
757 if (TEST(t, o)) {
758 ECL_RPLACD(l, new_obj);
759 return tree;
760 }
761 l = o;
762 } while (CONSP(l));
763 return tree;
764 }
765
766 static cl_object
767 nsubst(struct cl_test *t, cl_object new_obj, cl_object tree)
768 {
769 if (TEST(t, tree))
770 return new_obj;
771 if (CONSP(tree))
772 return nsubst_cons(t, new_obj, tree);
773 return tree;
774 }
775
776 @(defun sublis (alist tree &key test test_not key)
777 /* t[0] is the test for the objects in the tree, configured
778 with test, test_not and key. t[1] is the test for searching
779 in the association list.
780 */
781 struct cl_test t[2];
782 @
783 setup_test(t, ECL_NIL, ECL_NIL, ECL_NIL, key);
784 setup_test(t+1, ECL_NIL, test, test_not, ECL_NIL);
785 tree = sublis(t, alist, tree);
786 close_test(t+1);
787 close_test(t);
788 @(return tree);
789 @)
790
791 /*
792 Sublis(alist, tree) returns
793 result of substituting tree by alist.
794 */
795 static cl_object
796 sublis(struct cl_test *t, cl_object alist, cl_object tree)
797 {
798 cl_object node;
799 t[1].item_compared = KEY(t, tree);
800 node = do_assoc(t+1, alist);
801 if (!Null(node)) {
802 return ECL_CONS_CDR(node);
803 }
804 if (CONSP(tree)) {
805 tree = CONS(sublis(t, alist, ECL_CONS_CAR(tree)),
806 sublis(t, alist, ECL_CONS_CDR(tree)));
807 }
808 return tree;
809 }
810
811 @(defun nsublis (alist tree &key test test_not key)
812 /* t[0] is the test for the objects in the tree, configured
813 with test, test_not and key. t[1] is the test for searching
814 in the association list.
815 */
816 struct cl_test t[2];
817 @
818 setup_test(t, ECL_NIL, ECL_NIL, ECL_NIL, key);
819 setup_test(t+1, ECL_NIL, test, test_not, ECL_NIL);
820 tree = nsublis(t, alist, tree);
821 close_test(t+1);
822 close_test(t);
823 @(return tree);
824 @)
825
826 /*
827 Nsublis(alist, treep) stores
828 the result of substiting *treep by alist
829 to *treep.
830 */
831 static cl_object
832 nsublis(struct cl_test *t, cl_object alist, cl_object tree)
833 {
834 cl_object node;
835 t[1].item_compared = KEY(t, tree);
836 node = do_assoc(t+1, alist);
837 if (!Null(node)) {
838 return ECL_CONS_CDR(node);
839 }
840 if (CONSP(tree)) {
841 ECL_RPLACA(tree, nsublis(t, alist, ECL_CONS_CAR(tree)));
842 ECL_RPLACD(tree, nsublis(t, alist, ECL_CONS_CDR(tree)));
843 }
844 return tree;
845 }
846
847 @(defun member (item list &key test test_not key)
848 struct cl_test t;
849 @
850 setup_test(&t, item, test, test_not, key);
851 loop_for_in(list) {
852 if (TEST(&t, ECL_CONS_CAR(list)))
853 break;
854 } end_loop_for_in;
855 close_test(&t);
856 @(return list);
857 @)
858
859 bool
860 ecl_member_eq(cl_object x, cl_object l)
861 {
862 loop_for_in(l) {
863 if (x == ECL_CONS_CAR(l))
864 return(TRUE);
865 } end_loop_for_in;
866 return(FALSE);
867 }
868
869 cl_object
870 si_memq(cl_object x, cl_object l)
871 {
872 loop_for_in(l) {
873 if (x == ECL_CONS_CAR(l)) {
874 @(return l);
875 }
876 } end_loop_for_in;
877 @(return ECL_NIL);
878 }
879
880 /* Added for use by the compiler, instead of open coding them. Beppe */
881 cl_object
882 ecl_memql(cl_object x, cl_object l)
883 {
884 loop_for_in(l) {
885 if (ecl_eql(x, ECL_CONS_CAR(l)))
886 return(l);
887 } end_loop_for_in;
888 return(ECL_NIL);
889 }
890
891 cl_object
892 ecl_member(cl_object x, cl_object l)
893 {
894 loop_for_in(l) {
895 if (ecl_equal(x, ECL_CONS_CAR(l)))
896 return(l);
897 } end_loop_for_in;
898 return(ECL_NIL);
899 }
900 /* End of addition. Beppe */
901
902 cl_object
903 si_member1(cl_object item, cl_object list, cl_object test, cl_object test_not, cl_object key)
904 {
905 struct cl_test t;
906
907 if (key != ECL_NIL)
908 item = funcall(2, key, item);
909 setup_test(&t, item, test, test_not, key);
910 loop_for_in(list) {
911 if (TEST(&t, ECL_CONS_CAR(list)))
912 break;
913 } end_loop_for_in;
914 close_test(&t);
915 @(return list);
916 }
917
918 cl_object
919 cl_tailp(cl_object y, cl_object x)
920 {
921 loop_for_on(x) {
922 if (ecl_eql(x, y)) {
923 @(return ECL_T);
924 }
925 } end_loop_for_on(x);
926 return cl_eql(x, y);
927 }
928
929 @(defun adjoin (item list &key test test_not key)
930 cl_object output;
931 @
932 if (narg < 2)
933 FEwrong_num_arguments(@[adjoin]);
934 output = @si::member1(item, list, test, test_not, key);
935 if (Null(output))
936 output = CONS(item, list);
937 else
938 output = list;
939 @(return output);
940 @)
941
942 cl_object
943 cl_cons(cl_object x, cl_object y)
944 {
945 @(return CONS(x, y));
946 }
947
948 cl_object
949 cl_acons(cl_object x, cl_object y, cl_object z)
950 {
951 @(return CONS(CONS(x, y), z));
952 }
953
954 @(defun pairlis (keys data &optional a_list)
955 cl_object k, d;
956 @
957 k = keys;
958 d = data;
959 loop_for_in(k) {
960 if (ecl_endp(d))
961 goto error;
962 a_list = CONS(CONS(ECL_CONS_CAR(k), ECL_CONS_CAR(d)), a_list);
963 d = CDR(d);
964 } end_loop_for_in;
965 if (!ecl_endp(d))
966 error:
967 FEerror("The keys ~S and the data ~S are not of the same length",
968 2, keys, data);
969 @(return a_list);
970 @)
971
972
973 @(defun assoc (item a_list &key test test_not key)
974 struct cl_test t;
975 @
976 setup_test(&t, item, test, test_not, key);
977 a_list = do_assoc(&t, a_list);
978 close_test(&t);
979 @(return a_list);
980 @)
981
982 static cl_object
983 do_assoc(struct cl_test *t, cl_object a_list)
984 {
985 loop_for_in(a_list) {
986 cl_object pair = ECL_CONS_CAR(a_list);
987 if (!Null(pair)) {
988 if (!LISTP(pair))
989 FEtype_error_list(pair);
990 if (TEST(t, ECL_CONS_CAR(pair)))
991 return pair;
992 }
993 } end_loop_for_in;
994 return ECL_NIL;
995 }
996
997 @(defun rassoc (item a_list &key test test_not key)
998 struct cl_test t;
999 @
1000 setup_test(&t, item, test, test_not, key);
1001 loop_for_in(a_list) {
1002 cl_object pair = ECL_CONS_CAR(a_list);
1003 if (!Null(pair)) {
1004 if (!LISTP(pair))
1005 FEtype_error_list(pair);
1006 if (TEST(&t, ECL_CONS_CDR(pair))) {
1007 a_list = pair;
1008 break;
1009 }
1010 }
1011 } end_loop_for_in;
1012 close_test(&t);
1013 @(return a_list);
1014 @)
1015
1016 cl_object
1017 ecl_remove_eq(cl_object x, cl_object l)
1018 {
1019 cl_object head = ECL_NIL, tail = ECL_NIL;
1020 loop_for_on_unsafe(l) {
1021 if (ECL_CONS_CAR(l) != x) {
1022 cl_object cons = ecl_list1(ECL_CONS_CAR(l));
1023 if (Null(tail)) {
1024 head = tail = cons;
1025 } else {
1026 ECL_RPLACD(tail, cons);
1027 tail = cons;
1028 }
1029 }
1030 } end_loop_for_on_unsafe(l);
1031 return head;
1032 }
1033
1034 cl_object
1035 ecl_delete_eq(cl_object x, cl_object l)
1036 {
1037 cl_object head = l;
1038 cl_object *p = &head;
1039 while (!ECL_ATOM(l)) {
1040 if (ECL_CONS_CAR(l) == x) {
1041 *p = l = ECL_CONS_CDR(l);
1042 } else {
1043 p = &ECL_CONS_CDR(l);
1044 l = *p;
1045 }
1046 }
1047 return head;
1048 }
1049
1050 /* Added for use by the compiler, instead of open coding them. Beppe */
1051 cl_object
1052 ecl_assq(cl_object x, cl_object l)
1053 {
1054 loop_for_in(l) {
1055 cl_object pair = ECL_CONS_CAR(l);
1056 if (pair==ECL_NIL) continue;
1057 if (ecl_unlikely(ECL_ATOM(pair))) {
1058 FEtype_error_cons(pair);
1059 }
1060 if (x == CAR(pair))
1061 return pair;
1062 } end_loop_for_in;
1063 return(ECL_NIL);
1064 }
1065
1066 cl_object
1067 ecl_assql(cl_object x, cl_object l)
1068 {
1069 loop_for_in(l) {
1070 cl_object pair = ECL_CONS_CAR(l);
1071 if (pair==ECL_NIL) continue;
1072 if (ecl_unlikely(ECL_ATOM(pair))) {
1073 FEtype_error_cons(pair);
1074 }
1075 if (ecl_eql(x, CAR(pair)))
1076 return pair;
1077 } end_loop_for_in;
1078 return(ECL_NIL);
1079 }
1080
1081 cl_object
1082 ecl_assoc(cl_object x, cl_object l)
1083 {
1084 loop_for_in(l) {
1085 cl_object pair = ECL_CONS_CAR(l);
1086 if (pair==ECL_NIL) continue;
1087 if (ecl_unlikely(ECL_ATOM(pair))) {
1088 FEtype_error_cons(pair);
1089 }
1090 if (ecl_equal(x, CAR(pair)))
1091 return pair;
1092 } end_loop_for_in;
1093 return(ECL_NIL);
1094 }
1095
1096 cl_object
1097 ecl_assqlp(cl_object x, cl_object l)
1098 {
1099 loop_for_in(l) {
1100 cl_object pair = ECL_CONS_CAR(l);
1101 if (pair==ECL_NIL) continue;
1102 if (ecl_unlikely(ECL_ATOM(pair))) {
1103 FEtype_error_cons(pair);
1104 }
1105 if (ecl_equalp(x, CAR(pair)))
1106 return pair;
1107 } end_loop_for_in;
1108 return(ECL_NIL);
1109 }
1110 /* End of addition. Beppe */