1 ;;;; functions to implement lists
3 ;;;; This software is part of the SBCL system. See the README file for
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.
20 (declaim (maybe-inline
21 tree-equal %setnth nthcdr nth
23 #| union nunion |
# ; what the ????
24 intersection nintersection set-difference nset-difference
25 set-exclusive-or nset-exclusive-or subsetp acons
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
))
33 "Return all but the first object in a 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
))
39 "Return all but the 1st two objects of a list."
42 "Return the 1st object in the cddr of a list."
45 "Return the 1st object in the cadr of a list."
48 "Return the 1st object in the caar of a list."
51 "Return the cdr of the caar of a list."
54 "Return the cdr of the cdar of a list."
57 "Return the cdr of the cddr of a list."
60 "Return the car of the cdar of a list."
63 "Return the cdr of the cadr of a list."
66 "Return the car of the caaar of a list."
69 "Return the car of the caadr of a list."
72 "Return the car of the caddr of a list."
75 "Return the car of the cdddr of a list."
78 "Return the cdr of the cdddr of a list."
81 "Return the cdr of the caaar of a list."
84 "Return the cdr of the cdaar of a list."
87 "Return the cdr of the cddar of a list."
90 "Return the car of the cadar of a list."
93 "Return the car of the cdaar of a list."
96 "Return the car of the cdadr of a list."
99 "Return the car of the cddar of a list."
102 "Return the cdr of the caadr of a list."
105 "Return the cdr of the cadar of a list."
108 "Return the cdr of the caddr of a list."
111 "Return the cdr of the cdadr of a list."
113 (defun cons (se1 se2
)
114 "Return a list with SE1 as the CAR and SE2 as the CDR."
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
))
123 (tree-equal-test-not (car x
) (car y
) test-not
)
124 (tree-equal-test-not (cdr x
) (cdr y
) test-not
)))
126 ((not (funcall test-not x y
)) t
)
129 (defun tree-equal-test (x y test
)
130 (declare (type function test
))
133 (tree-equal-test (car x
) (car y
) test
)
134 (tree-equal-test (cdr x
) (cdr y
) test
)))
136 ((funcall test x y
) t
)
139 (defun tree-equal-eql (x y
)
140 (labels ((recurse (x y
)
148 (return-from tree-equal-eql
)))
149 (cond ((consp (car x
))
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
)))))))
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
))
163 (error ":TEST and :TEST-NOT were both supplied."))
164 (tree-equal-test-not x y
(%coerce-callable-to-fun test-not
)))
168 (tree-equal-eql x y
))
170 (tree-equal-test x y
(%coerce-callable-to-fun test
)))))
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."
178 (defun list-length (list)
179 "Return the length of LIST, or NIL if LIST is circular."
184 (declare (type fixnum n
)
186 (when (endp y
) (return n
))
187 (when (endp (cdr y
)) (return (+ n
1)))
188 (when (and (eq y z
) (> n
0)) (return nil
))))
191 "Return the nth object in a list where the car is the zero-th element."
192 (declare (explicit-check)
195 ((and fixnum unsigned-byte
)
205 (return (car result
)))
208 (car (nthcdr n list
)))))
211 "Return the 1st object in a list or NIL if the list is empty."
214 "Return the 2nd object in a list or NIL if there is no 2nd object."
217 "Return the 3rd object in a list or NIL if there is no 3rd object."
220 "Return the 4th object in a list or NIL if there is no 4th object."
223 "Return the 5th object in a list or NIL if there is no 5th object."
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
)))
232 "Return the 8th object in a list or NIL if there is no 8th object."
233 (cadddr (cddddr list
)))
235 "Return the 9th object in a list or NIL if there is no 9th object."
236 (car (cddddr (cddddr list
))))
238 "Return the 10th object in a list or NIL if there is no 10th object."
239 (cadr (cddddr (cddddr list
))))
241 "Means the same as the cdr of a list."
244 (defun nthcdr (n list
)
245 "Performs the cdr function n times on a list."
246 (declare (explicit-check n
)
248 (flet ((fast-nthcdr (n list
)
250 (result list
(cdr result
)))
251 ((not (plusp i
)) result
))))
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
))
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
))))))))
266 (defun dotted-nthcdr (n list
)
269 (result list
(cdr result
)))
270 ((not (plusp i
)) result
)
271 (declare (type fixnum i
))
277 ;;; Transforms in src/compiler/srctran.lisp pick the most specific
278 ;;; version possible. %LAST/BIGNUM is admittedly somewhat academic...
279 (macrolet ((last0-macro ()
282 (loop (unless (consp rest
)
284 (shiftf list rest
(cdr rest
)))))
288 (loop (unless (consp rest
)
290 (shiftf list rest
(cdr rest
)))))
292 `(let ((returned-list list
)
294 (n (truly-the ,type n
)))
299 (when (atom checked-list
)
301 (if (zerop (truly-the ,type
(decf n
)))
307 (if (atom checked-list
)
314 (declare (optimize speed
(sb-c:verify-arg-count
0)))
318 (declare (optimize speed
(sb-c:verify-arg-count
0)))
321 (defun %lastn
/fixnum
(list n
)
322 (declare (optimize speed
(sb-c:verify-arg-count
0))
323 (type (and unsigned-byte fixnum
) n
))
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."
342 (lastn-macro fixnum
))
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
)
354 (defun list (&rest args
)
355 "Construct and return a list containing the objects ARGS."
358 ;;; LIST* is done the same as LIST, except that the last cons is made
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
)
366 (cons arg
(fast-&rest-nth
0 others
)))
368 (let* ((cons (list arg
))
371 (1-length (1- length
)))
377 (list (fast-&rest-nth index others
))))
380 (setf (cdr cons
) (fast-&rest-nth index others
))
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
))
409 (length (length lists
))
411 (declare (dynamic-extent result
))
414 ((< (truly-the index index
) last
)
415 (let ((list (fast-&rest-nth
(truly-the index index
) lists
)))
417 (setf (cdr (truly-the cons tail
)) (list elt
)
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
))
433 (let* ((result (cons (car x
) y
))
435 (do ((more (cdr x
) (cdr more
)))
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
))
452 (let ((cell (list (car orig
))))
453 (rplacd (truly-the cons splice
) 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
)))
462 (unless (dynamic-space-obj-p cons
) (return t
)))
463 (copy-list-macro list
)
466 (defun copy-alist (alist)
467 "Return a new association list which is EQUAL to ALIST."
471 (cons (if (atom (car alist
))
473 (cons (caar alist
) (cdar alist
)))
475 (do ((x (cdr alist
) (cdr x
))
481 (cons (caar x
) (cdar x
)))
486 (defun copy-tree (object)
487 "Recursively copy trees of conses."
489 (let ((result (list (if (consp (car object
))
490 (copy-tree (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
)
496 (return (setf (cdr last-cons
) cdr
)))
497 for new-cons
= (list (if (consp car
)
500 do
(setf (cdr last-cons
) new-cons
))
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)
530 :expected-type
'list
)))
531 (do-rest-arg ((result index
) lists
)
534 (let ((splice result
))
535 (do-rest-arg ((ele index
) lists
(1+ index
))
537 (cons (rplacd (last splice
) ele
)
539 (null (rplacd (last splice
) nil
))
540 (atom (if (< (1+ index
) (length lists
))
542 (rplacd (last splice
) ele
)))))
546 (if (< (1+ index
) (length lists
))
548 (return result
)))))))
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.
559 (defun butlast (list &optional
(n 1))
560 (declare (optimize speed
)
562 (cond ((not (typep n
'(and fixnum unsigned-byte
)))
563 (the unsigned-byte n
)
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.
578 (copy (car trail
)))))))))
580 (defun nbutlast (list &optional
(n 1))
581 (declare (optimize speed
)
583 (cond ((not (typep n
'(and fixnum unsigned-byte
)))
584 (the unsigned-byte n
)
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
)))
597 (setf (cdr trail
) nil
)
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
))
608 (if (eql list object
)
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."
621 (defun rplacd (cons x
)
622 "Change the CDR of CONS to X and return the CONS."
625 ;;; Set the Nth element of LIST to NEWVAL.
626 (defun %setnth
(n list newval
)
629 (do ((count n
(1- count
))
630 (list list
(cdr list
)))
632 (error "~S is too large an index for SETF of NTH." n
))
633 (declare (type fixnum count
))
637 (t (let ((cons (nthcdr n list
)))
639 (error "~S is too large an index for SETF of NTH." n
))
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
)
650 (funcall ,key
,element
)
653 (defmacro apply-key-function
(key element
)
655 (funcall (truly-the function
,key
) ,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)
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
)))
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
)))
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
)))
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
)))
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
)))))
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
)))
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
)))))
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
)))
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
)))))
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
))
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
)))
810 (cons car cdr
))))))))
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
)))
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
)
833 (labels ((s (subtree)
834 (cond ((setq temp
(nsublis-macro))
836 ((atom subtree
) subtree
)
837 (t (do* ((last nil subtree
)
838 (subtree subtree
(cdr 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
)))))
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
))))
862 (%member-key-test item list key test
)
863 (%member-test item list test
)))
866 (%member-key-test-not item list key test-not
)
867 (%member-test-not item list test-not
)))
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
))))
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
))))
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
)
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
))))
912 (%adjoin-key-test item list key test
)
913 (%adjoin-test item list test
)))
916 (%adjoin-key-test-not item list key test-not
)
917 (%adjoin-test-not item list test-not
)))
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*
931 (lambda (x list2 key test
)
932 (%member-key-test-not
(funcall (truly-the function key
) x
)
934 (lambda (x list2 key test
)
935 (declare (ignore key
))
936 (%member-test-not x list2 test
))))
939 (lambda (x list2 key test
)
940 (%member-key-test
(funcall (truly-the function key
) x
)
942 (lambda (x list2 key test
)
943 (declare (ignore key
))
944 (%member-test x list2 test
))))
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
)))))
954 (%coerce-callable-to-fun test-not
))
956 (%coerce-callable-to-fun test
)))))
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.
965 ;; no :TEST, or a standard hash-table test
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
)
979 (setf (gethash (apply-key key elt
) table
) elt
))
981 (setf (gethash (apply-key key elt
) table
) elt
))
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
)
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
))
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
))
1032 (maphash (lambda (k v
)
1033 (declare (ignore k
))
1037 (push v union
))) ; easier than re-using cons cells of SHORT
1041 (elt (car long
) (car long
)))
1043 (if (funcall member-test elt orig key test
)
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)
1058 (when (funcall member-test elt list2 key test
)
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)
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
))))
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."))
1087 (with-member-test (member-test)
1090 (unless (funcall member-test elt list2 key test
)
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."))
1103 (with-member-test (member-test)
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
)))
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."))
1121 (with-member-test (member-test)
1123 (unless (funcall member-test elt list2 key test
)
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
)
1131 (unless (funcall member-test elt list1 key
#'test
)
1132 (push elt 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
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.
1159 ;; elements of LIST2, which are "equal" to some processed
1160 ;; earlier elements of LIST1
1165 (rplacd splicex list2
))
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),
1172 (do* ((y list2 next-y
)
1173 (next-y (cdr y
) (cdr y
))
1176 (cond ((let ((key-val-y (apply-key key
(car y
))))
1178 (not (funcall test-not key-val-x key-val-y
))
1179 (funcall test key-val-x key-val-y
)))
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
))))
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)
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
))
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
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
))))
1239 (%assoc-key-test item alist key test
)
1240 (%assoc-test item alist test
)))
1243 (%assoc-key-test-not item alist key test-not
)
1244 (%assoc-test-not item alist test-not
)))
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
))))
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
))))
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
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
))))
1284 (%rassoc-key-test item alist key test
)
1285 (%rassoc-test item alist test
)))
1288 (%rassoc-key-test-not item alist key test-not
)
1289 (%rassoc-test-not item alist test-not
)))
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
))))
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
))))
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
))
1333 (args (make-list (length arglists
))))
1334 ((dolist (x arglists
) (or x
(return t
)))
1338 (declare (dynamic-extent ret-list
))
1339 (do ((l arglists
(cdr l
))
1340 (arg args
(cdr arg
)))
1342 (setf (car arg
) (if take-car
(caar l
) (car l
)))
1343 (setf (car l
) (cdar l
)))
1344 (setq res
(apply fun args
))
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.~%~
1359 (if take-car
"elements" "CDRs")
1360 return-value-description
)))
1361 `(defun ,name
(function list
&rest more-lists
)
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.
1384 ((def (funs form
&optional variant
)
1385 (flet ((%def
(name &optional conditional
)
1387 `(do ((list list
(cdr list
)))
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
1397 (let ((target (,cxx 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
)
1406 ((assoc-if assoc-if-not rassoc-if rassoc-if-not
)
1407 (aver (equal '(eql x
) (subseq form
0 2)))
1409 (let ((target (,cxx this
)))
1410 (,conditional
(funcall ,@(cdr form
))
1413 `(let ((target this
))
1416 ((member-if member-if-not
)
1417 (aver (equal '(eql x
) (subseq form
0 2)))
1418 `(let ((target this
))
1419 (,conditional
(funcall ,@(cdr form
))
1422 `(let ((target this
))
1425 (body (if (eq 'adjoin name
)
1426 `(if (let ,(when (member 'key funs
)
1427 `((x (funcall key x
))))
1432 `(defun ,(intern (format nil
"%~A~{-~A~}~@[-~A~]" name funs variant
))
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))
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
)))))))
1458 (eql x
(funcall key target
)))
1460 (eq x
(funcall key target
))
1463 (funcall test x
(funcall key target
)))
1465 (not (funcall test-not x
(funcall key target
))))
1467 (funcall test x target
))
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
)))