Derive that m is integer after (integerp (* m m)).
[sbcl.git] / src / code / list.lisp
blob2f64b9d8fe929e184424a7506992c99b8b72187f
1 ;;;; functions to implement lists
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB-IMPL")
14 ;;; Limitation: no list might have more than INDEX conses.
16 ;;;; KLUDGE: comment from CMU CL, what does it mean?
17 ;;;; NSUBLIS, things at the beginning broken.
18 ;;;; -- WHN 20000127
20 (declaim (maybe-inline
21 tree-equal %setnth nthcdr nth
22 tailp
23 #| union nunion |# ; what the ????
24 intersection nintersection set-difference nset-difference
25 set-exclusive-or nset-exclusive-or subsetp acons
26 subst subst-if
27 ;; NSUBLIS is >400 lines of assembly. How is it helpful to inline?
28 subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
30 ;;; These functions perform basic list operations.
31 (defun car (list) "Return the 1st object in a list." (car list))
32 (defun cdr (list)
33 "Return all but the first object in a list."
34 (cdr list))
35 (defun cadr (list) "Return the 2nd object in a list." (cadr list))
36 (defun cdar (list) "Return the cdr of the 1st sublist." (cdar list))
37 (defun caar (list) "Return the car of the 1st sublist." (caar list))
38 (defun cddr (list)
39 "Return all but the 1st two objects of a list."
40 (cddr list))
41 (defun caddr (list)
42 "Return the 1st object in the cddr of a list."
43 (caddr list))
44 (defun caadr (list)
45 "Return the 1st object in the cadr of a list."
46 (caadr list))
47 (defun caaar (list)
48 "Return the 1st object in the caar of a list."
49 (caaar list))
50 (defun cdaar (list)
51 "Return the cdr of the caar of a list."
52 (cdaar list))
53 (defun cddar (list)
54 "Return the cdr of the cdar of a list."
55 (cddar list))
56 (defun cdddr (list)
57 "Return the cdr of the cddr of a list."
58 (cdddr list))
59 (defun cadar (list)
60 "Return the car of the cdar of a list."
61 (cadar list))
62 (defun cdadr (list)
63 "Return the cdr of the cadr of a list."
64 (cdadr list))
65 (defun caaaar (list)
66 "Return the car of the caaar of a list."
67 (caaaar list))
68 (defun caaadr (list)
69 "Return the car of the caadr of a list."
70 (caaadr list))
71 (defun caaddr (list)
72 "Return the car of the caddr of a list."
73 (caaddr list))
74 (defun cadddr (list)
75 "Return the car of the cdddr of a list."
76 (cadddr list))
77 (defun cddddr (list)
78 "Return the cdr of the cdddr of a list."
79 (cddddr list))
80 (defun cdaaar (list)
81 "Return the cdr of the caaar of a list."
82 (cdaaar list))
83 (defun cddaar (list)
84 "Return the cdr of the cdaar of a list."
85 (cddaar list))
86 (defun cdddar (list)
87 "Return the cdr of the cddar of a list."
88 (cdddar list))
89 (defun caadar (list)
90 "Return the car of the cadar of a list."
91 (caadar list))
92 (defun cadaar (list)
93 "Return the car of the cdaar of a list."
94 (cadaar list))
95 (defun cadadr (list)
96 "Return the car of the cdadr of a list."
97 (cadadr list))
98 (defun caddar (list)
99 "Return the car of the cddar of a list."
100 (caddar list))
101 (defun cdaadr (list)
102 "Return the cdr of the caadr of a list."
103 (cdaadr list))
104 (defun cdadar (list)
105 "Return the cdr of the cadar of a list."
106 (cdadar list))
107 (defun cdaddr (list)
108 "Return the cdr of the caddr of a list."
109 (cdaddr list))
110 (defun cddadr (list)
111 "Return the cdr of the cdadr of a list."
112 (cddadr list))
113 (defun cons (se1 se2)
114 "Return a list with SE1 as the CAR and SE2 as the CDR."
115 (cons se1 se2))
117 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
119 (defun tree-equal-test-not (x y test-not)
120 (declare (type function test-not))
121 (cond ((consp x)
122 (and (consp y)
123 (tree-equal-test-not (car x) (car y) test-not)
124 (tree-equal-test-not (cdr x) (cdr y) test-not)))
125 ((consp y) nil)
126 ((not (funcall test-not x y)) t)
127 (t ())))
129 (defun tree-equal-test (x y test)
130 (declare (type function test))
131 (cond ((consp x)
132 (and (consp y)
133 (tree-equal-test (car x) (car y) test)
134 (tree-equal-test (cdr x) (cdr y) test)))
135 ((consp y) nil)
136 ((funcall test x y) t)
137 (t ())))
139 (defun tree-equal-eql (x y)
140 (labels ((recurse (x y)
141 (if (eq x y)
143 (do ((x x (cdr x))
144 (y y (cdr y)))
145 ((or (atom x)
146 (atom y))
147 (or (eql x y)
148 (return-from tree-equal-eql)))
149 (cond ((consp (car x))
150 (when (atom (car y))
151 (return-from tree-equal-eql))
152 (recurse (car x) (car y)))
153 ((not (eql (car x) (car y)))
154 (return-from tree-equal-eql)))))))
155 (recurse x y)))
157 (defun tree-equal (x y &key (test nil testp) (test-not nil notp))
158 "Return T if X and Y are isomorphic trees with identical leaves."
159 (declare (explicit-check))
160 (declare (dynamic-extent test test-not))
161 (cond (notp
162 (when testp
163 (error ":TEST and :TEST-NOT were both supplied."))
164 (tree-equal-test-not x y (%coerce-callable-to-fun test-not)))
165 ((or (not test)
166 (eql test #'eql)
167 (eql test 'eql))
168 (tree-equal-eql x y))
170 (tree-equal-test x y (%coerce-callable-to-fun test)))))
172 (defun endp (object)
173 "This is the recommended way to test for the end of a proper list. It
174 returns true if OBJECT is NIL, false if OBJECT is a CONS, and an error
175 for any other type of OBJECT."
176 (endp object))
178 (defun list-length (list)
179 "Return the length of LIST, or NIL if LIST is circular."
180 (do ((n 0 (+ n 2))
181 (y list (cddr y))
182 (z list (cdr z)))
183 (())
184 (declare (type fixnum n)
185 (type list y z))
186 (when (endp y) (return n))
187 (when (endp (cdr y)) (return (+ n 1)))
188 (when (and (eq y z) (> n 0)) (return nil))))
190 (defun nth (n list)
191 "Return the nth object in a list where the car is the zero-th element."
192 (declare (explicit-check)
193 (optimize speed))
194 (typecase n
195 ((and fixnum unsigned-byte)
196 (block nil
197 (let ((i n)
198 (result list))
199 (tagbody
200 loop
201 (the list result)
202 (if (plusp i)
203 (psetq i (1- i)
204 result (cdr result))
205 (return (car result)))
206 (go loop)))))
208 (car (nthcdr n list)))))
210 (defun first (list)
211 "Return the 1st object in a list or NIL if the list is empty."
212 (car list))
213 (defun second (list)
214 "Return the 2nd object in a list or NIL if there is no 2nd object."
215 (cadr list))
216 (defun third (list)
217 "Return the 3rd object in a list or NIL if there is no 3rd object."
218 (caddr list))
219 (defun fourth (list)
220 "Return the 4th object in a list or NIL if there is no 4th object."
221 (cadddr list))
222 (defun fifth (list)
223 "Return the 5th object in a list or NIL if there is no 5th object."
224 (car (cddddr list)))
225 (defun sixth (list)
226 "Return the 6th object in a list or NIL if there is no 6th object."
227 (cadr (cddddr list)))
228 (defun seventh (list)
229 "Return the 7th object in a list or NIL if there is no 7th object."
230 (caddr (cddddr list)))
231 (defun eighth (list)
232 "Return the 8th object in a list or NIL if there is no 8th object."
233 (cadddr (cddddr list)))
234 (defun ninth (list)
235 "Return the 9th object in a list or NIL if there is no 9th object."
236 (car (cddddr (cddddr list))))
237 (defun tenth (list)
238 "Return the 10th object in a list or NIL if there is no 10th object."
239 (cadr (cddddr (cddddr list))))
240 (defun rest (list)
241 "Means the same as the cdr of a list."
242 (cdr list))
244 (defun nthcdr (n list)
245 "Performs the cdr function n times on a list."
246 (declare (explicit-check n)
247 (optimize speed))
248 (flet ((fast-nthcdr (n list)
249 (do ((i n (1- i))
250 (result list (cdr result)))
251 ((not (plusp i)) result))))
252 (typecase n
253 ((and fixnum unsigned-byte)
254 (fast-nthcdr n list))
255 ;; Such a large list can only be circular
257 (locally (declare (unsigned-byte n))
258 (do ((i 0 (1+ i))
259 (r-i list (cdr r-i))
260 (r-2i list (cddr r-2i)))
261 ((and (eq r-i r-2i) (not (zerop i)))
262 (fast-nthcdr (mod n i) r-i))
263 (declare (type fixnum i))))))))
265 ;;; For [n]butlast
266 (defun dotted-nthcdr (n list)
267 (declare (fixnum n))
268 (do ((i n (1- i))
269 (result list (cdr result)))
270 ((not (plusp i)) result)
271 (declare (type fixnum i))
272 (when (atom result)
273 (return))))
275 ;;; LAST
277 ;;; Transforms in src/compiler/srctran.lisp pick the most specific
278 ;;; version possible. %LAST/BIGNUM is admittedly somewhat academic...
279 (macrolet ((last0-macro ()
280 `(let ((rest list)
281 (list list))
282 (loop (unless (consp rest)
283 (return rest))
284 (shiftf list rest (cdr rest)))))
285 (last1-macro ()
286 `(let ((rest list)
287 (list list))
288 (loop (unless (consp rest)
289 (return list))
290 (shiftf list rest (cdr rest)))))
291 (lastn-macro (type)
292 `(let ((returned-list list)
293 (checked-list list)
294 (n (truly-the ,type n)))
295 (declare (,type n))
296 (tagbody
297 :scan
298 (pop checked-list)
299 (when (atom checked-list)
300 (go :done))
301 (if (zerop (truly-the ,type (decf n)))
302 (go :pop)
303 (go :scan))
304 :pop
305 (pop returned-list)
306 (pop checked-list)
307 (if (atom checked-list)
308 (go :done)
309 (go :pop))
310 :done)
311 returned-list)))
313 (defun %last0 (list)
314 (declare (optimize speed (sb-c:verify-arg-count 0)))
315 (last0-macro))
317 (defun %last1 (list)
318 (declare (optimize speed (sb-c:verify-arg-count 0)))
319 (last1-macro))
321 (defun %lastn/fixnum (list n)
322 (declare (optimize speed (sb-c:verify-arg-count 0))
323 (type (and unsigned-byte fixnum) n))
324 (case n
325 (1 (last1-macro))
326 (0 (last0-macro))
327 (t (lastn-macro fixnum))))
329 (defun %lastn/bignum (list n)
330 (declare (optimize speed (sb-c:verify-arg-count 0))
331 (type (and unsigned-byte bignum) n))
332 (lastn-macro unsigned-byte))
334 (defun last (list &optional (n 1))
335 "Return the last N conses (not the last element!) of a list."
336 (case n
337 (1 (last1-macro))
338 (0 (last0-macro))
340 (typecase n
341 (fixnum
342 (lastn-macro fixnum))
343 (bignum
344 (lastn-macro unsigned-byte)))))))
346 (define-compiler-macro last (&whole form list &optional (n 1) &environment env)
347 (if (constantp n env)
348 (case (constant-form-value n env)
349 (0 `(%last0 ,list))
350 (1 `(%last1 ,list))
351 (t form))
352 form))
354 (defun list (&rest args)
355 "Construct and return a list containing the objects ARGS."
356 args)
358 ;;; LIST* is done the same as LIST, except that the last cons is made
359 ;;; a dotted pair.
361 (defun list* (arg &rest others)
362 "Return a list of the arguments with last cons a dotted pair."
363 (let ((length (length others)))
364 (cond ((= length 0) arg)
365 ((= length 1)
366 (cons arg (fast-&rest-nth 0 others)))
368 (let* ((cons (list arg))
369 (result cons)
370 (index 0)
371 (1-length (1- length)))
372 (loop
373 (cond
374 ((< index 1-length)
375 (setf cons
376 (setf (cdr cons)
377 (list (fast-&rest-nth index others))))
378 (incf index))
379 (t (return nil))))
380 (setf (cdr cons) (fast-&rest-nth index others))
381 result)))))
383 (defun make-list (size &key initial-element)
384 "Construct and return a list with SIZE elements each set to INITIAL-ELEMENT."
385 (declare (explicit-check))
386 (%make-list size initial-element))
387 ;;; This entry point is to be preferred, irrespective of
388 ;;; whether or not the backend has vops for %MAKE-LIST.
389 (defun %make-list (size initial-element)
390 (declare (type index size))
391 (do ((count size (1- count))
392 (result '() (cons initial-element result)))
393 ((<= count 0) result)
394 (declare (type index count))))
396 (defun %sys-make-list (size initial-element)
397 (declare (type index size)
398 (sb-c::tlab :system))
399 (do ((count size (1- count))
400 (result '() (cons initial-element result)))
401 ((<= count 0) result)
402 (declare (type index count))))
404 (defun append (&rest lists)
405 "Construct and return a list by concatenating LISTS."
406 (let* ((result (unaligned-dx-cons nil))
407 (tail result)
408 (index 0)
409 (length (length lists))
410 (last (1- length)))
411 (declare (dynamic-extent result))
412 (loop
413 (cond
414 ((< (truly-the index index) last)
415 (let ((list (fast-&rest-nth (truly-the index index) lists)))
416 (dolist (elt list)
417 (setf (cdr (truly-the cons tail)) (list elt)
418 tail (cdr tail))))
419 (incf index))
420 (t (return nil))))
421 (cond
422 ((zerop length) nil)
423 ((null (cdr result))
424 (fast-&rest-nth (truly-the index last) lists))
426 (setf (cdr (truly-the cons tail))
427 (fast-&rest-nth (truly-the index last) lists))
428 (cdr result)))))
430 (defun append2 (x y)
431 (if (null x)
433 (let* ((result (cons (car x) y))
434 (tail result))
435 (do ((more (cdr x) (cdr more)))
436 ((null more)
437 result)
438 (rplacd (truly-the cons tail) (setf tail (cons (car more) y)))))))
441 ;;;; list copying functions
443 (defun copy-list (list)
444 "Return a new list which is EQUAL to LIST. LIST may be improper."
445 (copy-list-macro list))
447 (defun copy-list-to (list tail)
448 (declare (explicit-check))
449 (the list (cdr (truly-the cons tail)))
450 (do ((orig list (cdr orig))
451 (splice tail
452 (let ((cell (list (car orig))))
453 (rplacd (truly-the cons splice) cell)
454 cell)))
455 ((atom orig) (rplacd (truly-the cons splice) orig))))
457 (defun ensure-heap-list (list)
458 (declare (sb-c::tlab :system))
459 ;; If some cons is not on the heap then copy the whole list
460 (if (do ((cons list (cdr cons)))
461 ((null cons) nil)
462 (unless (dynamic-space-obj-p cons) (return t)))
463 (copy-list-macro list)
464 list))
466 (defun copy-alist (alist)
467 "Return a new association list which is EQUAL to ALIST."
468 (if (endp alist)
469 alist
470 (let ((result
471 (cons (if (atom (car alist))
472 (car alist)
473 (cons (caar alist) (cdar alist)))
474 nil)))
475 (do ((x (cdr alist) (cdr x))
476 (splice result
477 (cdr (rplacd splice
478 (cons
479 (if (atom (car x))
480 (car x)
481 (cons (caar x) (cdar x)))
482 nil)))))
483 ((endp x)))
484 result)))
486 (defun copy-tree (object)
487 "Recursively copy trees of conses."
488 (if (consp object)
489 (let ((result (list (if (consp (car object))
490 (copy-tree (car object))
491 (car object)))))
492 (loop for last-cons = result then new-cons
493 for cdr = (cdr object) then (cdr cdr)
494 for car = (if (consp cdr)
495 (car cdr)
496 (return (setf (cdr last-cons) cdr)))
497 for new-cons = (list (if (consp car)
498 (copy-tree car)
499 car))
500 do (setf (cdr last-cons) new-cons))
501 result)
502 object))
505 ;;;; more commonly-used list functions
507 (defun revappend (x y)
508 "Return (append (reverse x) y)."
509 (do ((top x (cdr top))
510 (result y (cons (car top) result)))
511 ((endp top) result)))
513 ;;; NCONC finds the first non-null list, so it can make splice point
514 ;;; to a cons. After finding the first cons element, it holds it in a
515 ;;; result variable while running down successive elements tacking
516 ;;; them together. While tacking lists together, if we encounter a
517 ;;; null list, we set the previous list's last cdr to nil just in case
518 ;;; it wasn't already nil, and it could have been dotted while the
519 ;;; null list was the last argument to NCONC. The manipulation of
520 ;;; splice (that is starting it out on a first cons, setting LAST of
521 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
522 ;;; and it avoids running down the last argument to NCONC which allows
523 ;;; the last argument to be circular.
524 (defun nconc (&rest lists)
525 "Concatenates the lists given as arguments (by changing them)"
526 (declare (optimize speed))
527 (flet ((fail (object)
528 (error 'type-error
529 :datum object
530 :expected-type 'list)))
531 (do-rest-arg ((result index) lists)
532 (typecase result
533 (cons
534 (let ((splice result))
535 (do-rest-arg ((ele index) lists (1+ index))
536 (typecase ele
537 (cons (rplacd (last splice) ele)
538 (setf splice ele))
539 (null (rplacd (last splice) nil))
540 (atom (if (< (1+ index) (length lists))
541 (fail ele)
542 (rplacd (last splice) ele)))))
543 (return result)))
544 (null)
545 (atom
546 (if (< (1+ index) (length lists))
547 (fail result)
548 (return result)))))))
550 (defun nreconc (x y)
551 "Return (NCONC (NREVERSE X) Y)."
552 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
553 (2nd x 1st) ;2nd follows first down the list.
554 (3rd y 2nd)) ;3rd follows 2nd down the list.
555 ((atom 2nd) 3rd)
556 (rplacd 2nd 3rd)))
559 (defun butlast (list &optional (n 1))
560 (declare (optimize speed)
561 (explicit-check n))
562 (cond ((not (typep n '(and fixnum unsigned-byte)))
563 (the unsigned-byte n)
564 nil)
565 ((zerop n)
566 (copy-list list))
568 (let ((head (dotted-nthcdr (1- n) list)))
569 (and (consp head) ; there are at least n
570 (collect ((copy)) ; conses; copy!
571 (do ((trail list (cdr trail))
572 (head head (cdr head)))
573 ;; HEAD is n-1 conses ahead of TRAIL;
574 ;; when HEAD is at the last cons, return
575 ;; the data copied so far.
576 ((atom (cdr head))
577 (copy))
578 (copy (car trail)))))))))
580 (defun nbutlast (list &optional (n 1))
581 (declare (optimize speed)
582 (explicit-check n))
583 (cond ((not (typep n '(and fixnum unsigned-byte)))
584 (the unsigned-byte n)
585 nil)
586 ((zerop n)
587 list)
589 (let ((head (dotted-nthcdr (1- n) list)))
590 (and (consp head) ; there are more than n
591 (consp (cdr head)) ; conses.
592 ;; TRAIL trails by n cons to be able to
593 ;; cut the list at the cons just before.
594 (do ((trail list (cdr trail))
595 (head (cdr head) (cdr head)))
596 ((atom (cdr head))
597 (setf (cdr trail) nil)
598 list)))))))
600 (defun ldiff (list object)
601 "Return a new list, whose elements are those of LIST that appear before
602 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
603 LIST must be a proper list or a dotted list."
604 (do* ((list list (cdr list))
605 (result (list ()))
606 (splice result))
607 ((atom list)
608 (if (eql list object)
609 (cdr result)
610 (progn (rplacd splice list) (cdr result))))
611 (if (eql list object)
612 (return (cdr result))
613 (setq splice (cdr (rplacd splice (list (car list))))))))
615 ;;;; functions to alter list structure
617 (defun rplaca (cons x)
618 "Change the CAR of CONS to X and return the CONS."
619 (rplaca cons x))
621 (defun rplacd (cons x)
622 "Change the CDR of CONS to X and return the CONS."
623 (rplacd cons x))
625 ;;; Set the Nth element of LIST to NEWVAL.
626 (defun %setnth (n list newval)
627 (typecase n
628 (index
629 (do ((count n (1- count))
630 (list list (cdr list)))
631 ((endp list)
632 (error "~S is too large an index for SETF of NTH." n))
633 (declare (type fixnum count))
634 (when (<= count 0)
635 (rplaca list newval)
636 (return newval))))
637 (t (let ((cons (nthcdr n list)))
638 (when (endp cons)
639 (error "~S is too large an index for SETF of NTH." n))
640 (rplaca cons newval)
641 newval))))
643 ;;;; :KEY arg optimization to save funcall of IDENTITY
645 ;;; APPLY-KEY saves us a function call sometimes.
646 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
647 ;;; because it's used in seq.lisp and sort.lisp.
648 (defmacro apply-key (key element)
649 `(if ,key
650 (funcall ,key ,element)
651 ,element))
653 (defmacro apply-key-function (key element)
654 `(if ,key
655 (funcall (truly-the function ,key) ,element)
656 ,element))
658 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
660 ;;; Use these with the following &KEY args:
661 (defmacro with-set-keys (funcall)
662 `(if notp
663 ,(append funcall '(:key key :test-not test-not))
664 ,(append funcall '(:key key :test test))))
666 (defmacro satisfies-the-test (item elt)
667 (let ((key-tmp (gensym)))
668 `(let ((,key-tmp (apply-key key ,elt)))
669 (cond (testp (funcall test ,item ,key-tmp))
670 (notp (not (funcall test-not ,item ,key-tmp)))
671 (t (funcall test ,item ,key-tmp))))))
673 ;;;; substitution of expressions
675 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
676 "Substitutes new for subtrees matching old."
677 (declare (dynamic-extent key test test-not))
678 (when (and testp notp)
679 (error ":TEST and :TEST-NOT were both supplied."))
680 (let ((key (and key (%coerce-callable-to-fun key)))
681 (test (if testp (%coerce-callable-to-fun test) test))
682 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
683 (declare (type function test test-not))
684 (labels ((s (subtree)
685 (cond ((satisfies-the-test old subtree) new)
686 ((atom subtree) subtree)
687 (t (let ((car (s (car subtree)))
688 (cdr (s (cdr subtree))))
689 (if (and (eq car (car subtree))
690 (eq cdr (cdr subtree)))
691 subtree
692 (cons car cdr)))))))
693 (s tree))))
695 (defun subst-if (new test tree &key key)
696 "Substitutes new for subtrees for which test is true."
697 (declare (dynamic-extent test key))
698 (let ((test (%coerce-callable-to-fun test))
699 (key (and key (%coerce-callable-to-fun key))))
700 (labels ((s (subtree)
701 (cond ((funcall test (apply-key key subtree)) new)
702 ((atom subtree) subtree)
703 (t (let ((car (s (car subtree)))
704 (cdr (s (cdr subtree))))
705 (if (and (eq car (car subtree))
706 (eq cdr (cdr subtree)))
707 subtree
708 (cons car cdr)))))))
709 (s tree))))
711 (defun subst-if-not (new test tree &key key)
712 "Substitutes new for subtrees for which test is false."
713 (declare (dynamic-extent test key))
714 (let ((test (%coerce-callable-to-fun test))
715 (key (and key (%coerce-callable-to-fun key))))
716 (labels ((s (subtree)
717 (cond ((not (funcall test (apply-key key subtree))) new)
718 ((atom subtree) subtree)
719 (t (let ((car (s (car subtree)))
720 (cdr (s (cdr subtree))))
721 (if (and (eq car (car subtree))
722 (eq cdr (cdr subtree)))
723 subtree
724 (cons car cdr)))))))
725 (s tree))))
727 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
728 "Substitute NEW for subtrees matching OLD."
729 (declare (dynamic-extent key test test-not))
730 (when (and testp notp)
731 (error ":TEST and :TEST-NOT were both supplied."))
732 (let ((key (and key (%coerce-callable-to-fun key)))
733 (test (if testp (%coerce-callable-to-fun test) test))
734 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
735 (declare (type function test test-not))
736 (labels ((s (subtree)
737 (cond ((satisfies-the-test old subtree) new)
738 ((atom subtree) subtree)
739 (t (do* ((last nil subtree)
740 (subtree subtree (cdr subtree)))
741 ((atom subtree)
742 (if (satisfies-the-test old subtree)
743 (setf (cdr last) new)))
744 (if (satisfies-the-test old subtree)
745 (return (setf (cdr last) new))
746 (setf (car subtree) (s (car subtree)))))
747 subtree))))
748 (s tree))))
750 (defun nsubst-if (new test tree &key key)
751 "Substitute NEW for subtrees of TREE for which TEST is true."
752 (declare (dynamic-extent test key))
753 (let ((test (%coerce-callable-to-fun test))
754 (key (and key (%coerce-callable-to-fun key))))
755 (labels ((s (subtree)
756 (cond ((funcall test (apply-key key subtree)) new)
757 ((atom subtree) subtree)
758 (t (do* ((last nil subtree)
759 (subtree subtree (cdr subtree)))
760 ((atom subtree)
761 (if (funcall test (apply-key key subtree))
762 (setf (cdr last) new)))
763 (if (funcall test (apply-key key subtree))
764 (return (setf (cdr last) new))
765 (setf (car subtree) (s (car subtree)))))
766 subtree))))
767 (s tree))))
769 (defun nsubst-if-not (new test tree &key key)
770 "Substitute NEW for subtrees of TREE for which TEST is false."
771 (declare (dynamic-extent test key))
772 (let ((test (%coerce-callable-to-fun test))
773 (key (and key (%coerce-callable-to-fun key))))
774 (labels ((s (subtree)
775 (cond ((not (funcall test (apply-key key subtree))) new)
776 ((atom subtree) subtree)
777 (t (do* ((last nil subtree)
778 (subtree subtree (cdr subtree)))
779 ((atom subtree)
780 (if (not (funcall test (apply-key key subtree)))
781 (setf (cdr last) new)))
782 (if (not (funcall test (apply-key key subtree)))
783 (return (setf (cdr last) new))
784 (setf (car subtree) (s (car subtree)))))
785 subtree))))
786 (s tree))))
788 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
789 "Substitute from ALIST into TREE nondestructively."
790 (declare (dynamic-extent key test test-not))
791 (when (and testp notp)
792 (error ":TEST and :TEST-NOT were both supplied."))
793 (let ((key (and key (%coerce-callable-to-fun key)))
794 (test (if testp (%coerce-callable-to-fun test) test))
795 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
796 (declare (type function test test-not))
797 (declare (inline assoc))
798 (labels ((s (subtree)
799 (let* ((key-val (apply-key key subtree))
800 (assoc (if notp
801 (assoc key-val alist :test-not test-not)
802 (assoc key-val alist :test test))))
803 (cond (assoc (cdr assoc))
804 ((atom subtree) subtree)
805 (t (let ((car (s (car subtree)))
806 (cdr (s (cdr subtree))))
807 (if (and (eq car (car subtree))
808 (eq cdr (cdr subtree)))
809 subtree
810 (cons car cdr))))))))
811 (s tree))))
813 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
814 ;;; because it can be referenced in inline expansions.
815 (defmacro nsublis-macro ()
816 (let ((key-tmp (gensym)))
817 `(let ((,key-tmp (apply-key key subtree)))
818 (if notp
819 (assoc ,key-tmp alist :test-not test-not)
820 (assoc ,key-tmp alist :test test)))))
822 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
823 "Substitute from ALIST into TREE destructively."
824 (declare (dynamic-extent key test test-not))
825 (when (and testp notp)
826 (error ":TEST and :TEST-NOT were both supplied."))
827 (let ((key (and key (%coerce-callable-to-fun key)))
828 (test (if testp (%coerce-callable-to-fun test) test))
829 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
830 (declare (type function test test-not)
831 (inline assoc))
832 (let (temp)
833 (labels ((s (subtree)
834 (cond ((setq temp (nsublis-macro))
835 (cdr temp))
836 ((atom subtree) subtree)
837 (t (do* ((last nil subtree)
838 (subtree subtree (cdr subtree)))
839 ((atom subtree)
840 (if (setq temp (nsublis-macro))
841 (setf (cdr last) (cdr temp))))
842 (if (setq temp (nsublis-macro))
843 (return (setf (cdr last) (cdr temp)))
844 (setf (car subtree) (s (car subtree)))))
845 subtree))))
846 (s tree)))))
848 ;;;; functions for using lists as sets
850 (defun member (item list &key key (test nil testp) (test-not nil notp))
851 "Return the tail of LIST beginning with first element satisfying EQLity,
852 :TEST, or :TEST-NOT with the given ITEM."
853 (declare (explicit-check))
854 (declare (dynamic-extent key test test-not))
855 (when (and testp notp)
856 (error ":TEST and :TEST-NOT were both supplied."))
857 (let ((key (and key (%coerce-callable-to-fun key)))
858 (test (and testp (%coerce-callable-to-fun test)))
859 (test-not (and notp (%coerce-callable-to-fun test-not))))
860 (cond (test
861 (if key
862 (%member-key-test item list key test)
863 (%member-test item list test)))
864 (test-not
865 (if key
866 (%member-key-test-not item list key test-not)
867 (%member-test-not item list test-not)))
869 (if key
870 (%member-key item list key)
871 (%member item list))))))
873 (defun member-if (test list &key key)
874 "Return tail of LIST beginning with first element satisfying TEST."
875 (declare (explicit-check))
876 (declare (dynamic-extent test key))
877 (let ((test (%coerce-callable-to-fun test))
878 (key (and key (%coerce-callable-to-fun key))))
879 (if key
880 (%member-if-key test list key)
881 (%member-if test list))))
883 (defun member-if-not (test list &key key)
884 "Return tail of LIST beginning with first element not satisfying TEST."
885 (declare (explicit-check))
886 (declare (dynamic-extent test key))
887 (let ((test (%coerce-callable-to-fun test))
888 (key (and key (%coerce-callable-to-fun key))))
889 (if key
890 (%member-if-not-key test list key)
891 (%member-if-not test list))))
893 (defun tailp (object list)
894 "Return true if OBJECT is the same as some tail of LIST, otherwise
895 returns false. LIST must be a proper list or a dotted list."
896 (do ((list list (cdr list)))
897 ((atom list) (eql list object))
898 (if (eql object list)
899 (return t))))
901 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
902 "Add ITEM to LIST unless it is already a member"
903 (declare (explicit-check))
904 (declare (dynamic-extent key test test-not))
905 (when (and testp notp)
906 (error ":TEST and :TEST-NOT were both supplied."))
907 (let ((key (and key (%coerce-callable-to-fun key)))
908 (test (and testp (%coerce-callable-to-fun test)))
909 (test-not (and notp (%coerce-callable-to-fun test-not))))
910 (cond (test
911 (if key
912 (%adjoin-key-test item list key test)
913 (%adjoin-test item list test)))
914 (test-not
915 (if key
916 (%adjoin-key-test-not item list key test-not)
917 (%adjoin-test-not item list test-not)))
919 (if key
920 (%adjoin-key item list key)
921 (%adjoin item list))))))
923 ;;; For cases where MEMBER is called in a loop this allows to perform
924 ;;; the dispatch that the MEMBER function does only once.
925 (defmacro with-member-test ((test-var &optional first-clause) &body body)
926 `(let* ((key (and key (%coerce-callable-to-fun key)))
927 (,test-var (cond ,@(and first-clause ; used by LIST-REMOVE-DUPLICATES*
928 `(,first-clause))
929 (notp
930 (if key
931 (lambda (x list2 key test)
932 (%member-key-test-not (funcall (truly-the function key) x)
933 list2 key test))
934 (lambda (x list2 key test)
935 (declare (ignore key))
936 (%member-test-not x list2 test))))
937 (testp
938 (if key
939 (lambda (x list2 key test)
940 (%member-key-test (funcall (truly-the function key) x)
941 list2 key test))
942 (lambda (x list2 key test)
943 (declare (ignore key))
944 (%member-test x list2 test))))
945 (key
946 (lambda (x list2 key test)
947 (declare (ignore test))
948 (%member-key (funcall (truly-the function key) x) list2 key)))
950 (lambda (x list2 key test)
951 (declare (ignore key test))
952 (%member x list2)))))
953 (test (cond (notp
954 (%coerce-callable-to-fun test-not))
955 (testp
956 (%coerce-callable-to-fun test)))))
958 ,@body))
960 (flet ((hashing-p (notp testp test n1 n2)
961 (declare (index n1 n2))
962 ;; If there is no TEST-NOT, and both lists are long enough, and the
963 ;; test function is that of a standard hash-table, then use a hash-table.
964 (and (not notp)
965 ;; no :TEST, or a standard hash-table test
966 (or (not testp)
967 (eq test #'eql)
968 (eq test #'eq)
969 (eq test #'equal)
970 (eq test #'equalp))
971 (or (and (> n1 20) (> n2 20)) ; both lists are non-short
972 ;; or one list is very long, and the other is not tiny
973 (and (>= n1 3) (>= n2 100))
974 (and (>= n2 3) (>= n1 100)))))
975 (unionize (testp test key n1 n2 set1 set2)
976 (let ((table (make-hash-table :test (if testp test #'eql)
977 :size (+ n1 n2))))
978 (dolist (elt set1)
979 (setf (gethash (apply-key key elt) table) elt))
980 (dolist (elt set2)
981 (setf (gethash (apply-key key elt) table) elt))
982 table)))
984 ;;; "If there is a duplication between list-1 and list-2, only one of the duplicate
985 ;;; instances will be in the result. If either list-1 or list-2 has duplicate entries
986 ;;; within it, the redundant entries might or might not appear in the result."
987 ;;; Our answer differs based on whether a hash-table is employed or not.
989 (defun union (list1 list2 &key key (test nil testp) (test-not nil notp))
990 "Return the union of LIST1 and LIST2."
991 (declare (explicit-check))
992 (declare (dynamic-extent key test test-not))
993 (when (and testp notp)
994 (error ":TEST and :TEST-NOT were both supplied."))
995 ;; "The result list may be eq to either list-1 or list-2 if appropriate."
996 ;; (and a 1000-element list unioned with NIL should not cons a hash-table)
997 (cond ((null list1) (return-from union list2))
998 ((null list2) (return-from union list1)))
999 (with-member-test (member-test)
1000 (let ((n1 (length list1))
1001 (n2 (length list2)))
1002 (if (hashing-p notp testp test n1 n2)
1003 ;; "The order of elements in the result do not have to reflect the ordering
1004 ;; of list-1 or list-2 in any way."
1005 (loop for k being the hash-values of (unionize testp test key n1 n2 list1 list2)
1006 collect k)
1007 ;; Start with the initial result being the shorter of the inputs.
1008 ;; Search for each element of the longer in the shorter, adding the missing ones.
1009 (multiple-value-bind (short long)
1010 (if (< n1 n2) (values list1 list2) (values list2 list1))
1011 (let ((result short))
1012 (dolist (elt long result)
1013 (unless (funcall member-test elt short key test)
1014 (push elt result)))))))))
1016 (defun nunion (list1 list2 &key key (test nil testp) (test-not nil notp))
1017 "Destructively return the union of LIST1 and LIST2."
1018 (declare (explicit-check))
1019 (declare (dynamic-extent key test test-not))
1020 (when (and testp notp)
1021 (error ":TEST and :TEST-NOT were both supplied."))
1022 (cond ((null list1) (return-from nunion list2))
1023 ((null list2) (return-from nunion list1)))
1024 (with-member-test (member-test)
1025 (binding* ((n1 (length list1))
1026 (n2 (length list2))
1027 ((short long) (if (< n1 n2) (values list1 list2) (values list2 list1))))
1028 (if (hashing-p notp testp test n1 n2)
1029 (let ((table (unionize testp test key n1 n2 short long))
1030 (union long)
1031 (head long))
1032 (maphash (lambda (k v)
1033 (declare (ignore k))
1034 (if head
1035 (setf (car head) v
1036 head (cdr head))
1037 (push v union))) ; easier than re-using cons cells of SHORT
1038 table)
1039 union)
1040 (do ((orig short)
1041 (elt (car long) (car long)))
1042 ((endp long) short)
1043 (if (funcall member-test elt orig key test)
1044 (pop long)
1045 (shiftf long (cdr long) short long))))))))
1047 (defun intersection (list1 list2
1048 &key key (test nil testp) (test-not nil notp))
1049 "Return the intersection of LIST1 and LIST2."
1050 (declare (explicit-check))
1051 (declare (dynamic-extent key test test-not))
1052 (when (and testp notp)
1053 (error ":TEST and :TEST-NOT were both supplied."))
1054 (when (and list1 list2)
1055 (with-member-test (member-test)
1056 (let ((res nil))
1057 (dolist (elt list1)
1058 (when (funcall member-test elt list2 key test)
1059 (push elt res)))
1060 res))))
1062 (defun nintersection (list1 list2
1063 &key key (test nil testp) (test-not nil notp))
1064 "Destructively return the intersection of LIST1 and LIST2."
1065 (declare (explicit-check))
1066 (declare (dynamic-extent key test test-not))
1067 (when (and testp notp)
1068 (error ":TEST and :TEST-NOT were both supplied."))
1069 (when (and list1 list2)
1070 (with-member-test (member-test)
1071 (let ((res nil)
1072 (list1 list1))
1073 (do () ((endp list1))
1074 (if (funcall member-test (car list1) list2 key test)
1075 (shiftf list1 (cdr list1) res list1)
1076 (setf list1 (cdr list1))))
1077 res))))
1079 (defun set-difference (list1 list2
1080 &key key (test nil testp) (test-not nil notp))
1081 "Return the elements of LIST1 which are not in LIST2."
1082 (declare (explicit-check))
1083 (declare (dynamic-extent key test test-not))
1084 (when (and testp notp)
1085 (error ":TEST and :TEST-NOT were both supplied."))
1086 (if list2
1087 (with-member-test (member-test)
1088 (let ((res nil))
1089 (dolist (elt list1)
1090 (unless (funcall member-test elt list2 key test)
1091 (push elt res)))
1092 res))
1093 list1))
1095 (defun nset-difference (list1 list2
1096 &key key (test nil testp) (test-not nil notp))
1097 "Destructively return the elements of LIST1 which are not in LIST2."
1098 (declare (explicit-check))
1099 (declare (dynamic-extent key test test-not))
1100 (when (and testp notp)
1101 (error ":TEST and :TEST-NOT were both supplied."))
1102 (if list2
1103 (with-member-test (member-test)
1104 (let ((res nil)
1105 (list1 list1))
1106 (do () ((endp list1))
1107 (if (funcall member-test (car list1) list2 key test)
1108 (setf list1 (cdr list1))
1109 (shiftf list1 (cdr list1) res list1)))
1110 res))
1111 list1))
1113 (defun set-exclusive-or (list1 list2
1114 &key key (test nil testp) (test-not nil notp))
1115 "Return new list of elements appearing exactly once in LIST1 and LIST2."
1116 (declare (explicit-check))
1117 (declare (dynamic-extent key test test-not))
1118 (when (and testp notp)
1119 (error ":TEST and :TEST-NOT were both supplied."))
1120 (let ((result nil))
1121 (with-member-test (member-test)
1122 (dolist (elt list1)
1123 (unless (funcall member-test elt list2 key test)
1124 (push elt result)))
1125 (dx-flet ((test (x y)
1126 ;; This local function is never called if TEST is NIL,
1127 ;; but the compiler doesn't know that, suppress the warning.
1128 (funcall (the* (function :truly t :silent-conflict t) test)
1129 y x)))
1130 (dolist (elt list2)
1131 (unless (funcall member-test elt list1 key #'test)
1132 (push elt result)))))
1133 result))
1135 (defun nset-exclusive-or (list1 list2
1136 &key key (test #'eql testp) (test-not #'eql notp))
1137 "Destructively return a list with elements which appear but once in LIST1
1138 and LIST2."
1139 (declare (explicit-check))
1140 (declare (dynamic-extent key test test-not))
1141 (when (and testp notp)
1142 (error ":TEST and :TEST-NOT were both supplied."))
1143 (let ((key (and key (%coerce-callable-to-fun key)))
1144 (test (if testp (%coerce-callable-to-fun test) test))
1145 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1146 (declare (type function test test-not))
1147 ;; The outer loop examines LIST1 while the inner loop examines
1148 ;; LIST2. If an element is found in LIST2 "equal" to the element
1149 ;; in LIST1, both are spliced out. When the end of LIST1 is
1150 ;; reached, what is left of LIST2 is tacked onto what is left of
1151 ;; LIST1. The splicing operation ensures that the correct
1152 ;; operation is performed depending on whether splice is at the
1153 ;; top of the list or not.
1154 (do ((list1 list1)
1155 (list2 list2)
1156 (x list1 (cdr x))
1157 (splicex ())
1158 (deleted-y ())
1159 ;; elements of LIST2, which are "equal" to some processed
1160 ;; earlier elements of LIST1
1162 ((endp x)
1163 (if (null splicex)
1164 (setq list1 list2)
1165 (rplacd splicex list2))
1166 list1)
1167 (let ((key-val-x (apply-key key (car x)))
1168 (found-duplicate nil))
1170 ;; Move all elements from LIST2, which are "equal" to (CAR X),
1171 ;; to DELETED-Y.
1172 (do* ((y list2 next-y)
1173 (next-y (cdr y) (cdr y))
1174 (splicey ()))
1175 ((endp y))
1176 (cond ((let ((key-val-y (apply-key key (car y))))
1177 (if notp
1178 (not (funcall test-not key-val-x key-val-y))
1179 (funcall test key-val-x key-val-y)))
1180 (if (null splicey)
1181 (setq list2 (cdr y))
1182 (rplacd splicey (cdr y)))
1183 (setq deleted-y (rplacd y deleted-y))
1184 (setq found-duplicate t))
1185 (t (setq splicey y))))
1187 (unless found-duplicate
1188 (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1190 (if found-duplicate
1191 (if (null splicex)
1192 (setq list1 (cdr x))
1193 (rplacd splicex (cdr x)))
1194 (setq splicex x))))))
1196 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1197 "Return T if every element in LIST1 is also in LIST2."
1198 (declare (explicit-check))
1199 (declare (dynamic-extent key test test-not))
1200 (when (and testp notp)
1201 (error ":TEST and :TEST-NOT were both supplied."))
1202 (with-member-test (member-test)
1203 (dolist (elt list1)
1204 (unless (funcall member-test elt list2 key test)
1205 (return-from subsetp nil)))
1208 ;;;; functions that operate on association lists
1210 (defun acons (key datum alist)
1211 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1212 ;; This function is maybe-inline, so can't use vop-existsp
1213 ;; which does not remain in the image post-build.
1214 #.(if (gethash 'acons sb-c::*backend-template-names*)
1215 '(acons key datum alist) ; vop translated
1216 '(cons (cons key datum) alist)))
1218 (defun pairlis (keys data &optional (alist '()))
1219 "Construct an association list from KEYS and DATA (adding to ALIST)."
1220 (do ((x keys (cdr x))
1221 (y data (cdr y)))
1222 ((and (endp x) (endp y)) alist)
1223 (if (or (endp x) (endp y))
1224 (error "The lists of keys and data are of unequal length."))
1225 (setq alist (acons (car x) (car y) alist))))
1227 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1228 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1229 the ITEM."
1230 (declare (explicit-check))
1231 (declare (dynamic-extent key test test-not))
1232 (when (and testp notp)
1233 (error ":TEST and :TEST-NOT were both supplied."))
1234 (let ((key (and key (%coerce-callable-to-fun key)))
1235 (test (and testp (%coerce-callable-to-fun test)))
1236 (test-not (and notp (%coerce-callable-to-fun test-not))))
1237 (cond (test
1238 (if key
1239 (%assoc-key-test item alist key test)
1240 (%assoc-test item alist test)))
1241 (test-not
1242 (if key
1243 (%assoc-key-test-not item alist key test-not)
1244 (%assoc-test-not item alist test-not)))
1246 (if key
1247 (%assoc-key item alist key)
1248 (%assoc item alist))))))
1250 (defun assoc-if (predicate alist &key key)
1251 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1252 KEY is supplied, apply it to the CAR of each cons before testing."
1253 (declare (explicit-check))
1254 (declare (dynamic-extent predicate key))
1255 (let ((predicate (%coerce-callable-to-fun predicate))
1256 (key (and key (%coerce-callable-to-fun key))))
1257 (if key
1258 (%assoc-if-key predicate alist key)
1259 (%assoc-if predicate alist))))
1261 (defun assoc-if-not (predicate alist &key key)
1262 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1263 If KEY is supplied, apply it to the CAR of each cons before testing."
1264 (declare (explicit-check))
1265 (declare (dynamic-extent predicate key))
1266 (let ((predicate (%coerce-callable-to-fun predicate))
1267 (key (and key (%coerce-callable-to-fun key))))
1268 (if key
1269 (%assoc-if-not-key predicate alist key)
1270 (%assoc-if-not predicate alist))))
1272 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1273 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1274 the ITEM."
1275 (declare (explicit-check))
1276 (declare (dynamic-extent key test test-not))
1277 (when (and testp notp)
1278 (error ":TEST and :TEST-NOT were both supplied."))
1279 (let ((key (and key (%coerce-callable-to-fun key)))
1280 (test (and testp (%coerce-callable-to-fun test)))
1281 (test-not (and notp (%coerce-callable-to-fun test-not))))
1282 (cond (test
1283 (if key
1284 (%rassoc-key-test item alist key test)
1285 (%rassoc-test item alist test)))
1286 (test-not
1287 (if key
1288 (%rassoc-key-test-not item alist key test-not)
1289 (%rassoc-test-not item alist test-not)))
1291 (if key
1292 (%rassoc-key item alist key)
1293 (%rassoc item alist))))))
1295 (defun rassoc-if (predicate alist &key key)
1296 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1297 is supplied, apply it to the CDR of each cons before testing."
1298 (declare (explicit-check))
1299 (declare (dynamic-extent predicate key))
1300 (let ((predicate (%coerce-callable-to-fun predicate))
1301 (key (and key (%coerce-callable-to-fun key))))
1302 (if key
1303 (%rassoc-if-key predicate alist key)
1304 (%rassoc-if predicate alist))))
1306 (defun rassoc-if-not (predicate alist &key key)
1307 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1308 If KEY is supplied, apply it to the CDR of each cons before testing."
1309 (declare (explicit-check))
1310 (declare (dynamic-extent predicate key))
1311 (let ((predicate (%coerce-callable-to-fun predicate))
1312 (key (and key (%coerce-callable-to-fun key))))
1313 (if key
1314 (%rassoc-if-not-key predicate alist key)
1315 (%rassoc-if-not predicate alist))))
1317 ;;;; mapping functions
1319 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1320 ;;; MAPL, MAPLIST, and MAPCON
1322 ;;; Map the designated function over the arglists in the appropriate
1323 ;;; way. It is done when any of the arglists runs out. Until then, it
1324 ;;; CDRs down the arglists calling the function and accumulating
1325 ;;; results as desired.
1326 (defun map1 (fun-designator arglists accumulate take-car)
1327 (declare (dynamic-extent fun-designator))
1328 (do* ((fun (%coerce-callable-to-fun fun-designator))
1329 (non-acc-result (car arglists))
1330 (ret-list (list nil))
1331 (temp ret-list)
1332 (res nil)
1333 (args (make-list (length arglists))))
1334 ((dolist (x arglists) (or x (return t)))
1335 (if accumulate
1336 (cdr ret-list)
1337 non-acc-result))
1338 (declare (dynamic-extent ret-list))
1339 (do ((l arglists (cdr l))
1340 (arg args (cdr arg)))
1341 ((null l))
1342 (setf (car arg) (if take-car (caar l) (car l)))
1343 (setf (car l) (cdar l)))
1344 (setq res (apply fun args))
1345 (case accumulate
1346 (:nconc
1347 (when res
1348 (psetf temp res
1349 (cdr (last temp)) res)))
1350 (:list (setf (cdr temp) (list res)
1351 temp (cdr temp))))))
1353 (macrolet ((define-list-map (name accumulate take-car
1354 return-value-description)
1355 (let ((documentation
1356 (format nil "Apply FUNCTION to successive tuples ~
1357 of ~A of LIST and MORE-LISTS.~%~
1358 Return ~A."
1359 (if take-car "elements" "CDRs")
1360 return-value-description)))
1361 `(defun ,name (function list &rest more-lists)
1362 ,documentation
1363 (declare (explicit-check))
1364 (declare (dynamic-extent function))
1365 (dx-let ((lists (list* list more-lists)))
1366 (map1 function lists ,accumulate ,take-car))))))
1367 (define-list-map mapc nil t "LIST")
1368 (define-list-map mapcar :list t "list of FUNCTION return values")
1369 (define-list-map mapcan :nconc t "NCONC of FUNCTION return values")
1370 (define-list-map mapl nil nil "LIST")
1371 (define-list-map maplist :list nil "list of results")
1372 (define-list-map mapcon :nconc nil "NCONC of results"))
1374 ;;;; Specialized versions
1376 ;;; %ADJOIN-*, %ASSOC-*, %MEMBER-*, and %RASSOC-* functions. Deftransforms
1377 ;;; delegate to TRANSFORM-LIST-PRED-SEEK and TRANSFORM-LIST-ITEM-SEEK which
1378 ;;; pick the appropriate versions. These win because they have only positional
1379 ;;; arguments, the TEST, TEST-NOT & KEY functions are known to exist (or not),
1380 ;;; and are known to be functions instead of function designators. We are also
1381 ;;; able to transform many common cases to -EQ versions, which are
1382 ;;; substantially faster then EQL using ones.
1383 (macrolet
1384 ((def (funs form &optional variant)
1385 (flet ((%def (name &optional conditional)
1386 (let* ((body-loop
1387 `(do ((list list (cdr list)))
1388 ((null list) nil)
1389 (let ((this (car list)))
1390 ,(let ((cxx (if (char= #\A (char (string name) 0))
1391 'car ; assoc, assoc-if, assoc-if-not
1392 'cdr))) ; rassoc, rassoc-if, rassoc-if-not
1393 (ecase name
1394 ((assoc rassoc)
1395 (if funs
1396 `(when this
1397 (let ((target (,cxx this)))
1398 (when ,form
1399 (return this))))
1400 ;; If there is no TEST/TEST-NOT or
1401 ;; KEY, do the EQ/EQL test first,
1402 ;; before checking for NIL.
1403 `(let ((target (,cxx this)))
1404 (when (and ,form this)
1405 (return this)))))
1406 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
1407 (aver (equal '(eql x) (subseq form 0 2)))
1408 `(when this
1409 (let ((target (,cxx this)))
1410 (,conditional (funcall ,@(cdr form))
1411 (return this)))))
1412 (member
1413 `(let ((target this))
1414 (when ,form
1415 (return list))))
1416 ((member-if member-if-not)
1417 (aver (equal '(eql x) (subseq form 0 2)))
1418 `(let ((target this))
1419 (,conditional (funcall ,@(cdr form))
1420 (return list))))
1421 (adjoin
1422 `(let ((target this))
1423 (when ,form
1424 (return t)))))))))
1425 (body (if (eq 'adjoin name)
1426 `(if (let ,(when (member 'key funs)
1427 `((x (funcall key x))))
1428 ,body-loop)
1429 list
1430 (cons x list))
1431 body-loop)))
1432 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1433 (x list ,@funs)
1434 ,@(when funs `((declare (function ,@funs)
1435 (dynamic-extent ,@funs))))
1436 ,@(unless (member name '(member assoc adjoin rassoc))
1437 `((declare (function x) (dynamic-extent x))))
1438 (declare (explicit-check))
1439 ,body))))
1440 `(progn
1441 ,(%def 'adjoin)
1442 ,(%def 'assoc)
1443 ,(%def 'member)
1444 ,(%def 'rassoc)
1445 ,@(when (and (not variant) (member funs '(() (key)) :test #'equal))
1446 (list (%def 'member-if 'when)
1447 (%def 'member-if-not 'unless)
1448 (%def 'assoc-if 'when)
1449 (%def 'assoc-if-not 'unless)
1450 (%def 'rassoc-if 'when)
1451 (%def 'rassoc-if-not 'unless)))))))
1452 (def ()
1453 (eql x target))
1454 (def ()
1455 (eq x target)
1457 (def (key)
1458 (eql x (funcall key target)))
1459 (def (key)
1460 (eq x (funcall key target))
1462 (def (key test)
1463 (funcall test x (funcall key target)))
1464 (def (key test-not)
1465 (not (funcall test-not x (funcall key target))))
1466 (def (test)
1467 (funcall test x target))
1468 (def (test-not)
1469 (not (funcall test-not x target))))
1471 (defun sys-tlab-adjoin-eq (item list)
1472 (declare (sb-c::tlab :system))
1473 (if (memq item list) list (cons item list)))
1475 (defun sys-tlab-append (a b)
1476 (declare (sb-c::tlab :system))
1477 (named-let recurse ((a a) (b b))
1478 (if a (cons (car a) (recurse (cdr a) b)) b)))