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
23 nunion intersection nintersection set-difference nset-difference
24 set-exclusive-or nset-exclusive-or subsetp acons
26 ;; NSUBLIS is >400 lines of assembly. How is it helpful to inline?
27 subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis
))
29 ;;; These functions perform basic list operations.
30 (defun car (list) #!+sb-doc
"Return the 1st object in a list." (car list
))
32 #!+sb-doc
"Return all but the first object in a list."
34 (defun cadr (list) #!+sb-doc
"Return the 2nd object in a list." (cadr list
))
35 (defun cdar (list) #!+sb-doc
"Return the cdr of the 1st sublist." (cdar list
))
36 (defun caar (list) #!+sb-doc
"Return the car of the 1st sublist." (caar list
))
38 #!+sb-doc
"Return all but the 1st two objects of a list."
41 #!+sb-doc
"Return the 1st object in the cddr of a list."
44 #!+sb-doc
"Return the 1st object in the cadr of a list."
47 #!+sb-doc
"Return the 1st object in the caar of a list."
50 #!+sb-doc
"Return the cdr of the caar of a list."
53 #!+sb-doc
"Return the cdr of the cdar of a list."
56 #!+sb-doc
"Return the cdr of the cddr of a list."
59 #!+sb-doc
"Return the car of the cdar of a list."
62 #!+sb-doc
"Return the cdr of the cadr of a list."
65 #!+sb-doc
"Return the car of the caaar of a list."
68 #!+sb-doc
"Return the car of the caadr of a list."
71 #!+sb-doc
"Return the car of the caddr of a list."
74 #!+sb-doc
"Return the car of the cdddr of a list."
77 #!+sb-doc
"Return the cdr of the cdddr of a list."
80 #!+sb-doc
"Return the cdr of the caaar of a list."
83 #!+sb-doc
"Return the cdr of the cdaar of a list."
86 #!+sb-doc
"Return the cdr of the cddar of a list."
89 #!+sb-doc
"Return the car of the cadar of a list."
92 #!+sb-doc
"Return the car of the cdaar of a list."
95 #!+sb-doc
"Return the car of the cdadr of a list."
98 #!+sb-doc
"Return the car of the cddar of a list."
101 #!+sb-doc
"Return the cdr of the caadr of a list."
104 #!+sb-doc
"Return the cdr of the cadar of a list."
107 #!+sb-doc
"Return the cdr of the caddr of a list."
110 #!+sb-doc
"Return the cdr of the cdadr of a list."
112 (defun cons (se1 se2
)
113 #!+sb-doc
"Return a list with SE1 as the CAR and SE2 as the CDR."
116 (declaim (maybe-inline tree-equal-test tree-equal-test-not
))
118 (defun tree-equal-test-not (x y test-not
)
119 (declare (type function test-not
))
122 (tree-equal-test-not (car x
) (car y
) test-not
)
123 (tree-equal-test-not (cdr x
) (cdr y
) test-not
)))
125 ((not (funcall test-not x y
)) t
)
128 (defun tree-equal-test (x y test
)
129 (declare (type function test
))
132 (tree-equal-test (car x
) (car y
) test
)
133 (tree-equal-test (cdr x
) (cdr y
) test
)))
135 ((funcall test x y
) t
)
138 (defun tree-equal-eql (x y
)
139 (labels ((recurse (x y
)
147 (cond ((consp (car x
))
149 (return-from tree-equal-eql
))
150 (recurse (car x
) (car y
)))
151 ((not (eql (car x
) (car y
)))
152 (return-from tree-equal-eql
)))))))
155 (defun tree-equal (x y
&key
(test nil testp
) (test-not nil notp
))
157 "Return T if X and Y are isomorphic trees with identical leaves."
158 (declare (explicit-check))
161 (error ":TEST and :TEST-NOT were both supplied."))
162 (tree-equal-test-not x y
(%coerce-callable-to-fun test-not
)))
166 (tree-equal-eql x y
))
168 (tree-equal-test x y
(%coerce-callable-to-fun test
)))))
172 "This is the recommended way to test for the end of a proper list. It
173 returns true if OBJECT is NIL, false if OBJECT is a CONS, and an error
174 for any other type of OBJECT."
177 (defun list-length (list)
179 "Return the length of the given List, or Nil if the 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
))))
192 "Return the nth object in a list where the car is the zero-th element."
193 (declare (explicit-check))
194 (car (nthcdr n list
)))
198 "Return the 1st object in a list or NIL if the list is empty."
202 "Return the 2nd object in a list or NIL if there is no 2nd object."
206 "Return the 3rd object in a list or NIL if there is no 3rd object."
210 "Return the 4th object in a list or NIL if there is no 4th object."
214 "Return the 5th object in a list or NIL if there is no 5th object."
218 "Return the 6th object in a list or NIL if there is no 6th object."
219 (cadr (cddddr list
)))
220 (defun seventh (list)
222 "Return the 7th object in a list or NIL if there is no 7th object."
223 (caddr (cddddr list
)))
226 "Return the 8th object in a list or NIL if there is no 8th object."
227 (cadddr (cddddr list
)))
230 "Return the 9th object in a list or NIL if there is no 9th object."
231 (car (cddddr (cddddr list
))))
234 "Return the 10th object in a list or NIL if there is no 10th object."
235 (cadr (cddddr (cddddr list
))))
238 "Means the same as the cdr of a list."
241 (defun nthcdr (n list
)
243 "Performs the cdr function n times on a list."
244 (flet ((fast-nthcdr (n list
)
245 (declare (type index n
))
247 (result list
(cdr result
)))
248 ((not (plusp i
)) result
)
249 (declare (type index i
)))))
251 (index (fast-nthcdr n list
))
254 (r-2i list
(cddr r-2i
)))
255 ((and (eq r-i r-2i
) (not (zerop i
)))
256 (fast-nthcdr (mod n i
) r-i
))
257 (declare (type index i
)))))))
261 ;;; Transforms in src/compiler/srctran.lisp pick the most specific
262 ;;; version possible. %LAST/BIGNUM is admittedly somewhat academic...
263 (macrolet ((last0-macro ()
266 (loop (unless (consp rest
)
268 (shiftf list rest
(cdr rest
)))))
272 (loop (unless (consp rest
)
274 (shiftf list rest
(cdr rest
)))))
276 `(let ((returned-list list
)
278 (n (truly-the ,type n
)))
283 (when (atom checked-list
)
285 (if (zerop (truly-the ,type
(decf n
)))
291 (if (atom checked-list
)
298 (declare (optimize speed
(sb!c
::verify-arg-count
0)))
302 (declare (optimize speed
(sb!c
::verify-arg-count
0)))
305 (defun %lastn
/fixnum
(list n
)
306 (declare (optimize speed
(sb!c
::verify-arg-count
0))
307 (type (and unsigned-byte fixnum
) n
))
311 (t (lastn-macro fixnum
))))
313 (defun %lastn
/bignum
(list n
)
314 (declare (optimize speed
(sb!c
::verify-arg-count
0))
315 (type (and unsigned-byte bignum
) n
))
316 (lastn-macro unsigned-byte
))
318 (defun last (list &optional
(n 1))
320 "Return the last N conses (not the last element!) of a list."
327 (lastn-macro fixnum
))
329 (lastn-macro unsigned-byte
)))))))
331 (define-compiler-macro last
(&whole form list
&optional
(n 1) &environment env
)
332 (if (sb!xc
:constantp n env
)
333 (case (constant-form-value n env
)
339 (defun list (&rest args
)
341 "Return constructs and returns a list of its arguments."
344 ;;; LIST* is done the same as LIST, except that the last cons is made
347 (defun list* (arg &rest others
)
349 "Return a list of the arguments with last cons a dotted pair."
350 (let ((length (length others
)))
351 (cond ((= length
0) arg
)
353 (cons arg
(fast-&rest-nth
0 others
)))
355 (let* ((cons (list arg
))
358 (1-length (1- length
)))
364 (list (fast-&rest-nth index others
))))
367 (setf (cdr cons
) (fast-&rest-nth index others
))
370 (defun make-list (size &key initial-element
)
372 "Constructs a list with size elements each set to value"
373 (declare (explicit-check))
374 (%make-list size initial-element
))
375 ;;; This entry point is to be preferred, irrespective of
376 ;;; whether or not the backend has vops for %MAKE-LIST.
377 (defun %make-list
(size initial-element
)
378 (declare (type index size
))
379 (do ((count size
(1- count
))
380 (result '() (cons initial-element result
)))
381 ((<= count
0) result
)
382 (declare (type index count
))))
384 (defun append (&rest lists
)
386 "Construct a new list by concatenating the list arguments"
387 (let* ((result (list nil
))
390 (length (length lists
))
392 (declare (truly-dynamic-extent result
))
395 ((< (truly-the index index
) last
)
396 (let ((list (fast-&rest-nth
(truly-the index index
) lists
)))
398 (setf (cdr (truly-the cons tail
)) (list elt
)
405 (fast-&rest-nth
(truly-the index last
) lists
))
407 (setf (cdr (truly-the cons tail
))
408 (fast-&rest-nth
(truly-the index last
) lists
))
412 (declare (optimize (sb!c
::verify-arg-count
0)))
415 (let ((result (list (car x
))))
416 (do ((more (cdr x
) (cdr more
))
417 (tail result
(cdr tail
)))
419 (rplacd (truly-the cons tail
) y
)
421 (rplacd (truly-the cons tail
) (list (car more
)))))))
424 ;;;; list copying functions
426 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
427 (sb!xc
:defmacro
!copy-list-macro
(list &key check-proper-list
)
428 ;; Unless CHECK-PROPER-LIST is true, the list is copied correctly
429 ;; even if the list is not terminated by NIL. The new list is built
430 ;; by CDR'ing SPLICE which is always at the tail of the new list.
432 (let ((copy (list (car ,list
))))
433 (do ((orig (cdr ,list
) (cdr orig
))
434 (splice copy
(cdr (rplacd splice
(cons (car orig
) nil
)))))
435 (,@(if check-proper-list
439 (rplacd splice orig
))))
442 (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-alist (alist)
449 "Return a new association list which is EQUAL to ALIST."
453 (cons (if (atom (car alist
))
455 (cons (caar alist
) (cdar alist
)))
457 (do ((x (cdr alist
) (cdr x
))
463 (cons (caar x
) (cdar x
)))
468 (defun copy-tree (object)
470 "Recursively copy trees of conses."
472 (let ((result (list (if (consp (car object
))
473 (copy-tree (car object
))
475 (loop for last-cons
= result then new-cons
476 for cdr
= (cdr object
) then
(cdr cdr
)
477 for car
= (if (consp cdr
)
479 (return (setf (cdr last-cons
) cdr
)))
480 for new-cons
= (list (if (consp car
)
483 do
(setf (cdr last-cons
) new-cons
))
488 ;;;; more commonly-used list functions
490 (defun revappend (x y
)
492 "Return (append (reverse x) y)."
493 (do ((top x
(cdr top
))
494 (result y
(cons (car top
) result
)))
495 ((endp top
) result
)))
497 ;;; NCONC finds the first non-null list, so it can make splice point
498 ;;; to a cons. After finding the first cons element, it holds it in a
499 ;;; result variable while running down successive elements tacking
500 ;;; them together. While tacking lists together, if we encounter a
501 ;;; null list, we set the previous list's last cdr to nil just in case
502 ;;; it wasn't already nil, and it could have been dotted while the
503 ;;; null list was the last argument to NCONC. The manipulation of
504 ;;; splice (that is starting it out on a first cons, setting LAST of
505 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
506 ;;; and it avoids running down the last argument to NCONC which allows
507 ;;; the last argument to be circular.
508 (defun nconc (&rest lists
)
510 "Concatenates the lists given as arguments (by changing them)"
511 (declare (optimize speed
))
512 (flet ((fail (object)
515 :expected-type
'list
)))
516 (do-rest-arg ((result index
) lists
)
519 (let ((splice result
))
520 (do-rest-arg ((ele index
) lists
(1+ index
))
522 (cons (rplacd (last splice
) ele
)
524 (null (rplacd (last splice
) nil
))
525 (atom (if (< (1+ index
) (length lists
))
527 (rplacd (last splice
) ele
)))))
531 (if (< (1+ index
) (length lists
))
533 (return result
)))))))
537 "Return (NCONC (NREVERSE X) Y)."
538 (do ((1st (cdr x
) (if (endp 1st
) 1st
(cdr 1st
)))
539 (2nd x
1st
) ;2nd follows first down the list.
540 (3rd y
2nd
)) ;3rd follows 2nd down the list.
544 (defun butlast (list &optional
(n 1))
547 ((not (typep n
'index
))
550 (let ((head (nthcdr (1- n
) list
)))
551 (and (consp head
) ; there are at least n
552 (collect ((copy)) ; conses; copy!
553 (do ((trail list
(cdr trail
))
554 (head head
(cdr head
)))
555 ;; HEAD is n-1 conses ahead of TRAIL;
556 ;; when HEAD is at the last cons, return
557 ;; the data copied so far.
560 (copy (car trail
)))))))))
562 (defun nbutlast (list &optional
(n 1))
565 ((not (typep n
'index
))
568 (let ((head (nthcdr (1- n
) list
)))
569 (and (consp head
) ; there are more than n
570 (consp (cdr head
)) ; conses.
571 ;; TRAIL trails by n cons to be able to
572 ;; cut the list at the cons just before.
573 (do ((trail list
(cdr trail
))
574 (head (cdr head
) (cdr head
)))
576 (setf (cdr trail
) nil
)
579 (defun ldiff (list object
)
581 "Return a new list, whose elements are those of LIST that appear before
582 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
583 LIST must be a proper list or a dotted list."
584 (do* ((list list
(cdr list
))
588 (if (eql list object
)
590 (progn (rplacd splice list
) (cdr result
))))
591 (if (eql list object
)
592 (return (cdr result
))
593 (setq splice
(cdr (rplacd splice
(list (car list
))))))))
595 ;;;; functions to alter list structure
597 (defun rplaca (cons x
)
599 "Change the CAR of CONS to X and return the CONS."
602 (defun rplacd (cons x
)
604 "Change the CDR of CONS to X and return the CONS."
607 ;;; The following are for use by SETF.
609 (defun %rplaca
(x val
) (rplaca x val
) val
)
611 (defun %rplacd
(x val
) (rplacd x val
) val
)
613 ;;; Set the Nth element of LIST to NEWVAL.
614 (defun %setnth
(n list newval
)
617 (do ((count n
(1- count
))
618 (list list
(cdr list
)))
620 (error "~S is too large an index for SETF of NTH." n
))
621 (declare (type fixnum count
))
625 (t (let ((cons (nthcdr n list
)))
627 (error "~S is too large an index for SETF of NTH." n
))
631 ;;;; :KEY arg optimization to save funcall of IDENTITY
633 ;;; APPLY-KEY saves us a function call sometimes.
634 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
635 ;;; because it's used in seq.lisp and sort.lisp.
636 (defmacro apply-key
(key element
)
638 (funcall ,key
,element
)
641 (defmacro apply-key-function
(key element
)
643 (funcall (truly-the function
,key
) ,element
)
646 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
648 ;;; Use these with the following &KEY args:
649 (defmacro with-set-keys
(funcall)
651 ,(append funcall
'(:key key
:test-not test-not
))
652 ,(append funcall
'(:key key
:test test
))))
654 (defmacro satisfies-the-test
(item elt
)
655 (let ((key-tmp (gensym)))
656 `(let ((,key-tmp
(apply-key key
,elt
)))
657 (cond (testp (funcall test
,item
,key-tmp
))
658 (notp (not (funcall test-not
,item
,key-tmp
)))
659 (t (funcall test
,item
,key-tmp
))))))
661 ;;;; substitution of expressions
663 (defun subst (new old tree
&key key
(test #'eql testp
) (test-not #'eql notp
))
665 "Substitutes new for subtrees matching old."
666 (when (and testp notp
)
667 (error ":TEST and :TEST-NOT were both supplied."))
668 (let ((key (and key
(%coerce-callable-to-fun key
)))
669 (test (if testp
(%coerce-callable-to-fun test
) test
))
670 (test-not (if notp
(%coerce-callable-to-fun test-not
) test-not
)))
671 (declare (type function test test-not
))
672 (labels ((s (subtree)
673 (cond ((satisfies-the-test old subtree
) new
)
674 ((atom subtree
) subtree
)
675 (t (let ((car (s (car subtree
)))
676 (cdr (s (cdr subtree
))))
677 (if (and (eq car
(car subtree
))
678 (eq cdr
(cdr subtree
)))
683 (defun subst-if (new test tree
&key key
)
685 "Substitutes new for subtrees for which test is true."
686 (let ((test (%coerce-callable-to-fun test
))
687 (key (and key
(%coerce-callable-to-fun key
))))
688 (labels ((s (subtree)
689 (cond ((funcall test
(apply-key key subtree
)) new
)
690 ((atom subtree
) subtree
)
691 (t (let ((car (s (car subtree
)))
692 (cdr (s (cdr subtree
))))
693 (if (and (eq car
(car subtree
))
694 (eq cdr
(cdr subtree
)))
699 (defun subst-if-not (new test tree
&key key
)
701 "Substitutes new for subtrees for which test is false."
702 (let ((test (%coerce-callable-to-fun test
))
703 (key (and key
(%coerce-callable-to-fun key
))))
704 (labels ((s (subtree)
705 (cond ((not (funcall test
(apply-key key subtree
))) new
)
706 ((atom subtree
) subtree
)
707 (t (let ((car (s (car subtree
)))
708 (cdr (s (cdr subtree
))))
709 (if (and (eq car
(car subtree
))
710 (eq cdr
(cdr subtree
)))
715 (defun nsubst (new old tree
&key key
(test #'eql testp
) (test-not #'eql notp
))
717 "Substitute NEW for subtrees matching OLD."
718 (when (and testp notp
)
719 (error ":TEST and :TEST-NOT were both supplied."))
720 (let ((key (and key
(%coerce-callable-to-fun key
)))
721 (test (if testp
(%coerce-callable-to-fun test
) test
))
722 (test-not (if notp
(%coerce-callable-to-fun test-not
) test-not
)))
723 (declare (type function test test-not
))
724 (labels ((s (subtree)
725 (cond ((satisfies-the-test old subtree
) new
)
726 ((atom subtree
) subtree
)
727 (t (do* ((last nil subtree
)
728 (subtree subtree
(cdr subtree
)))
730 (if (satisfies-the-test old subtree
)
731 (setf (cdr last
) new
)))
732 (if (satisfies-the-test old subtree
)
733 (return (setf (cdr last
) new
))
734 (setf (car subtree
) (s (car subtree
)))))
738 (defun nsubst-if (new test tree
&key key
)
740 "Substitute NEW for subtrees of TREE for which TEST is true."
741 (let ((test (%coerce-callable-to-fun test
))
742 (key (and key
(%coerce-callable-to-fun key
))))
743 (labels ((s (subtree)
744 (cond ((funcall test
(apply-key key subtree
)) new
)
745 ((atom subtree
) subtree
)
746 (t (do* ((last nil subtree
)
747 (subtree subtree
(cdr subtree
)))
749 (if (funcall test
(apply-key key subtree
))
750 (setf (cdr last
) new
)))
751 (if (funcall test
(apply-key key subtree
))
752 (return (setf (cdr last
) new
))
753 (setf (car subtree
) (s (car subtree
)))))
757 (defun nsubst-if-not (new test tree
&key key
)
759 "Substitute NEW for subtrees of TREE for which TEST is false."
760 (let ((test (%coerce-callable-to-fun test
))
761 (key (and key
(%coerce-callable-to-fun key
))))
762 (labels ((s (subtree)
763 (cond ((not (funcall test
(apply-key key subtree
))) new
)
764 ((atom subtree
) subtree
)
765 (t (do* ((last nil subtree
)
766 (subtree subtree
(cdr subtree
)))
768 (if (not (funcall test
(apply-key key subtree
)))
769 (setf (cdr last
) new
)))
770 (if (not (funcall test
(apply-key key subtree
)))
771 (return (setf (cdr last
) new
))
772 (setf (car subtree
) (s (car subtree
)))))
776 (defun sublis (alist tree
&key key
(test #'eql testp
) (test-not #'eql notp
))
778 "Substitute from ALIST into TREE nondestructively."
779 (when (and testp notp
)
780 (error ":TEST and :TEST-NOT were both supplied."))
781 (let ((key (and key
(%coerce-callable-to-fun key
)))
782 (test (if testp
(%coerce-callable-to-fun test
) test
))
783 (test-not (if notp
(%coerce-callable-to-fun test-not
) test-not
)))
784 (declare (type function test test-not
))
785 (declare (inline assoc
))
786 (labels ((s (subtree)
787 (let* ((key-val (apply-key key subtree
))
789 (assoc key-val alist
:test-not test-not
)
790 (assoc key-val alist
:test test
))))
791 (cond (assoc (cdr assoc
))
792 ((atom subtree
) subtree
)
793 (t (let ((car (s (car subtree
)))
794 (cdr (s (cdr subtree
))))
795 (if (and (eq car
(car subtree
))
796 (eq cdr
(cdr subtree
)))
798 (cons car cdr
))))))))
801 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
802 ;;; because it can be referenced in inline expansions.
803 (defmacro nsublis-macro
()
804 (let ((key-tmp (gensym)))
805 `(let ((,key-tmp
(apply-key key subtree
)))
807 (assoc ,key-tmp alist
:test-not test-not
)
808 (assoc ,key-tmp alist
:test test
)))))
810 (defun nsublis (alist tree
&key key
(test #'eql testp
) (test-not #'eql notp
))
812 "Substitute from ALIST into TRUE destructively."
813 (when (and testp notp
)
814 (error ":TEST and :TEST-NOT were both supplied."))
815 (let ((key (and key
(%coerce-callable-to-fun key
)))
816 (test (if testp
(%coerce-callable-to-fun test
) test
))
817 (test-not (if notp
(%coerce-callable-to-fun test-not
) test-not
)))
818 (declare (inline assoc
))
820 (labels ((s (subtree)
821 (cond ((setq temp
(nsublis-macro))
823 ((atom subtree
) subtree
)
824 (t (do* ((last nil subtree
)
825 (subtree subtree
(cdr subtree
)))
827 (if (setq temp
(nsublis-macro))
828 (setf (cdr last
) (cdr temp
))))
829 (if (setq temp
(nsublis-macro))
830 (return (setf (cdr last
) (cdr temp
)))
831 (setf (car subtree
) (s (car subtree
)))))
835 ;;;; functions for using lists as sets
837 (defun member (item list
&key key
(test nil testp
) (test-not nil notp
))
839 "Return the tail of LIST beginning with first element satisfying EQLity,
840 :TEST, or :TEST-NOT with the given ITEM."
841 (declare (explicit-check))
842 (when (and testp notp
)
843 (error ":TEST and :TEST-NOT were both supplied."))
844 (let ((key (and key
(%coerce-callable-to-fun key
)))
845 (test (and testp
(%coerce-callable-to-fun test
)))
846 (test-not (and notp
(%coerce-callable-to-fun test-not
))))
849 (%member-key-test item list key test
)
850 (%member-test item list test
)))
853 (%member-key-test-not item list key test-not
)
854 (%member-test-not item list test-not
)))
857 (%member-key item list key
)
858 (%member item list
))))))
860 (defun member-if (test list
&key key
)
862 "Return tail of LIST beginning with first element satisfying TEST."
863 (declare (explicit-check))
864 (let ((test (%coerce-callable-to-fun test
))
865 (key (and key
(%coerce-callable-to-fun key
))))
867 (%member-if-key test list key
)
868 (%member-if test list
))))
870 (defun member-if-not (test list
&key key
)
872 "Return tail of LIST beginning with first element not satisfying TEST."
873 (declare (explicit-check))
874 (let ((test (%coerce-callable-to-fun test
))
875 (key (and key
(%coerce-callable-to-fun key
))))
877 (%member-if-not-key test list key
)
878 (%member-if-not test list
))))
880 (defun tailp (object list
)
882 "Return true if OBJECT is the same as some tail of LIST, otherwise
883 returns false. LIST must be a proper list or a dotted list."
884 (do ((list list
(cdr list
)))
885 ((atom list
) (eql list object
))
886 (if (eql object list
)
889 (defun adjoin (item list
&key key
(test #'eql testp
) (test-not nil notp
))
891 "Add ITEM to LIST unless it is already a member"
892 (declare (explicit-check))
893 (when (and testp notp
)
894 (error ":TEST and :TEST-NOT were both supplied."))
895 (let ((key (and key
(%coerce-callable-to-fun key
)))
896 (test (and testp
(%coerce-callable-to-fun test
)))
897 (test-not (and notp
(%coerce-callable-to-fun test-not
))))
900 (%adjoin-key-test item list key test
)
901 (%adjoin-test item list test
)))
904 (%adjoin-key-test-not item list key test-not
)
905 (%adjoin-test-not item list test-not
)))
908 (%adjoin-key item list key
)
909 (%adjoin item list
))))))
911 ;;; For cases where MEMBER is called in a loop this allows to perform
912 ;;; the dispatch that the MEMBER function does only once.
913 (defmacro with-member-test
((test-var &optional first-clause
) &body body
)
914 `(let* ((key (and key
(%coerce-callable-to-fun key
)))
915 (,test-var
(cond ,@(and first-clause
; used by LIST-REMOVE-DUPLICATES*
919 (lambda (x list2 key test
)
920 (%member-key-test-not
(funcall (truly-the function key
) x
)
922 (lambda (x list2 key test
)
923 (declare (ignore key
))
924 (%member-test-not x list2 test
))))
927 (lambda (x list2 key test
)
928 (%member-key-test
(funcall (truly-the function key
) x
)
930 (lambda (x list2 key test
)
931 (declare (ignore key
))
932 (%member-test x list2 test
))))
934 (lambda (x list2 key test
)
935 (declare (ignore test
))
936 (%member-key
(funcall (truly-the function key
) x
) list2 key
)))
938 (lambda (x list2 key test
)
939 (declare (ignore key test
))
940 (%member x list2
)))))
942 (%coerce-callable-to-fun test-not
))
944 (%coerce-callable-to-fun test
)))))
948 (defconstant +list-based-union-limit
+ 80)
950 (defun hash-table-test-p (fun)
960 (defun union (list1 list2
&key key
(test nil testp
) (test-not nil notp
))
962 "Return the union of LIST1 and LIST2."
963 (declare (explicit-check))
964 (when (and testp notp
)
965 (error ":TEST and :TEST-NOT were both supplied."))
966 ;; We have two possibilities here: for shortish lists we pick up the
967 ;; shorter one as the result, and add the other one to it. For long
968 ;; lists we use a hash-table when possible.
969 (let ((n1 (length list1
))
971 (multiple-value-bind (short long n-short
)
973 (values list1 list2 n1
)
974 (values list2 list1 n2
))
975 (if (or (< n-short
+list-based-union-limit
+)
978 (not (hash-table-test-p test
))))
979 (with-member-test (member-test)
982 (unless (funcall member-test elt orig key test
)
985 (let ((table (make-hash-table :test
(if testp
987 #'eql
) :size
(+ n1 n2
)))
988 (key (and key
(%coerce-callable-to-fun key
)))
991 (setf (gethash (apply-key key elt
) table
) elt
))
993 (setf (gethash (apply-key key elt
) table
) elt
))
994 (maphash (lambda (k v
)
1000 (defun nunion (list1 list2
&key key
(test nil testp
) (test-not nil notp
))
1002 "Destructively return the union of LIST1 and LIST2."
1003 (declare (explicit-check))
1004 (when (and testp notp
)
1005 (error ":TEST and :TEST-NOT were both supplied."))
1006 ;; We have two possibilities here: for shortish lists we pick up the
1007 ;; shorter one as the result, and add the other one to it. For long
1008 ;; lists we use a hash-table when possible.
1009 (let ((n1 (length list1
))
1010 (n2 (length list2
)))
1011 (multiple-value-bind (short long n-short
)
1013 (values list1 list2 n1
)
1014 (values list2 list1 n2
))
1015 (if (or (< n-short
+list-based-union-limit
+)
1018 (not (hash-table-test-p test
))))
1019 (with-member-test (member-test)
1021 (elt (car long
) (car long
)))
1023 (if (funcall member-test elt orig key test
)
1025 (shiftf long
(cdr long
) short long
)))
1027 (let ((table (make-hash-table :test
(if testp
1029 #'eql
) :size
(+ n1 n2
)))
1030 (key (and key
(%coerce-callable-to-fun key
))))
1032 (setf (gethash (apply-key key elt
) table
) elt
))
1034 (setf (gethash (apply-key key elt
) table
) elt
))
1037 (maphash (lambda (k v
)
1038 (declare (ignore k
))
1046 (defun intersection (list1 list2
1047 &key key
(test nil testp
) (test-not nil notp
))
1049 "Return the intersection of LIST1 and LIST2."
1050 (declare (explicit-check))
1051 (when (and testp notp
)
1052 (error ":TEST and :TEST-NOT were both supplied."))
1053 (when (and list1 list2
)
1054 (with-member-test (member-test)
1057 (when (funcall member-test elt list2 key test
)
1061 (defun nintersection (list1 list2
1062 &key key
(test nil testp
) (test-not nil notp
))
1064 "Destructively return the intersection of LIST1 and LIST2."
1065 (declare (explicit-check))
1066 (when (and testp notp
)
1067 (error ":TEST and :TEST-NOT were both supplied."))
1068 (when (and list1 list2
)
1069 (with-member-test (member-test)
1072 (do () ((endp list1
))
1073 (if (funcall member-test
(car list1
) list2 key test
)
1074 (shiftf list1
(cdr list1
) res list1
)
1075 (setf list1
(cdr list1
))))
1078 (defun set-difference (list1 list2
1079 &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 (when (and testp notp
)
1084 (error ":TEST and :TEST-NOT were both supplied."))
1086 (with-member-test (member-test)
1089 (unless (funcall member-test elt list2 key test
)
1094 (defun nset-difference (list1 list2
1095 &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 (when (and testp notp
)
1100 (error ":TEST and :TEST-NOT were both supplied."))
1102 (with-member-test (member-test)
1105 (do () ((endp list1
))
1106 (if (funcall member-test
(car list1
) list2 key test
)
1107 (setf list1
(cdr list1
))
1108 (shiftf list1
(cdr list1
) res list1
)))
1112 (defun set-exclusive-or (list1 list2
1113 &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 (when (and testp notp
)
1118 (error ":TEST and :TEST-NOT were both supplied."))
1120 (with-member-test (member-test)
1122 (unless (funcall member-test elt list2 key test
)
1124 (dx-flet ((test (x y
) (funcall (truly-the function test
) y x
)))
1126 (unless (funcall member-test elt list1 key
#'test
)
1127 (push elt result
)))))
1130 (defun nset-exclusive-or (list1 list2
1131 &key key
(test #'eql testp
) (test-not #'eql notp
))
1133 "Destructively return a list with elements which appear but once in LIST1
1135 (declare (explicit-check))
1136 (when (and testp notp
)
1137 (error ":TEST and :TEST-NOT were both supplied."))
1138 (let ((key (and key
(%coerce-callable-to-fun key
)))
1139 (test (if testp
(%coerce-callable-to-fun test
) test
))
1140 (test-not (if notp
(%coerce-callable-to-fun test-not
) test-not
)))
1141 (declare (type function test test-not
))
1142 ;; The outer loop examines LIST1 while the inner loop examines
1143 ;; LIST2. If an element is found in LIST2 "equal" to the element
1144 ;; in LIST1, both are spliced out. When the end of LIST1 is
1145 ;; reached, what is left of LIST2 is tacked onto what is left of
1146 ;; LIST1. The splicing operation ensures that the correct
1147 ;; operation is performed depending on whether splice is at the
1148 ;; top of the list or not.
1154 ;; elements of LIST2, which are "equal" to some processed
1155 ;; earlier elements of LIST1
1160 (rplacd splicex list2
))
1162 (let ((key-val-x (apply-key key
(car x
)))
1163 (found-duplicate nil
))
1165 ;; Move all elements from LIST2, which are "equal" to (CAR X),
1167 (do* ((y list2 next-y
)
1168 (next-y (cdr y
) (cdr y
))
1171 (cond ((let ((key-val-y (apply-key key
(car y
))))
1173 (not (funcall test-not key-val-x key-val-y
))
1174 (funcall test key-val-x key-val-y
)))
1176 (setq list2
(cdr y
))
1177 (rplacd splicey
(cdr y
)))
1178 (setq deleted-y
(rplacd y deleted-y
))
1179 (setq found-duplicate t
))
1180 (t (setq splicey y
))))
1182 (unless found-duplicate
1183 (setq found-duplicate
(with-set-keys (member key-val-x deleted-y
))))
1187 (setq list1
(cdr x
))
1188 (rplacd splicex
(cdr x
)))
1189 (setq splicex x
))))))
1191 (defun subsetp (list1 list2
&key key
(test #'eql testp
) (test-not nil notp
))
1193 "Return T if every element in LIST1 is also in LIST2."
1194 (declare (explicit-check))
1195 (when (and testp notp
)
1196 (error ":TEST and :TEST-NOT were both supplied."))
1197 (with-member-test (member-test)
1199 (unless (funcall member-test elt list2 key test
)
1200 (return-from subsetp nil
)))
1203 ;;;; functions that operate on association lists
1205 (defun acons (key datum alist
)
1207 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1208 (cons (cons key datum
) alist
))
1210 (defun pairlis (keys data
&optional
(alist '()))
1212 "Construct an association list from KEYS and DATA (adding to ALIST)."
1213 (do ((x keys
(cdr x
))
1215 ((and (endp x
) (endp y
)) alist
)
1216 (if (or (endp x
) (endp y
))
1217 (error "The lists of keys and data are of unequal length."))
1218 (setq alist
(acons (car x
) (car y
) alist
))))
1220 (defun assoc (item alist
&key key
(test nil testp
) (test-not nil notp
))
1222 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1224 (declare (explicit-check))
1225 (when (and testp notp
)
1226 (error ":TEST and :TEST-NOT were both supplied."))
1227 (let ((key (and key
(%coerce-callable-to-fun key
)))
1228 (test (and testp
(%coerce-callable-to-fun test
)))
1229 (test-not (and notp
(%coerce-callable-to-fun test-not
))))
1232 (%assoc-key-test item alist key test
)
1233 (%assoc-test item alist test
)))
1236 (%assoc-key-test-not item alist key test-not
)
1237 (%assoc-test-not item alist test-not
)))
1240 (%assoc-key item alist key
)
1241 (%assoc item alist
))))))
1243 (defun assoc-if (predicate alist
&key key
)
1245 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1246 KEY is supplied, apply it to the CAR of each cons before testing."
1247 (declare (explicit-check))
1248 (let ((predicate (%coerce-callable-to-fun predicate
))
1249 (key (and key
(%coerce-callable-to-fun key
))))
1251 (%assoc-if-key predicate alist key
)
1252 (%assoc-if predicate alist
))))
1254 (defun assoc-if-not (predicate alist
&key key
)
1256 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1257 If KEY is supplied, apply it to the CAR of each cons before testing."
1258 (declare (explicit-check))
1259 (let ((predicate (%coerce-callable-to-fun predicate
))
1260 (key (and key
(%coerce-callable-to-fun key
))))
1262 (%assoc-if-not-key predicate alist key
)
1263 (%assoc-if-not predicate alist
))))
1265 (defun rassoc (item alist
&key key
(test nil testp
) (test-not nil notp
))
1267 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1269 (declare (explicit-check))
1270 (when (and testp notp
)
1271 (error ":TEST and :TEST-NOT were both supplied."))
1272 (let ((key (and key
(%coerce-callable-to-fun key
)))
1273 (test (and testp
(%coerce-callable-to-fun test
)))
1274 (test-not (and notp
(%coerce-callable-to-fun test-not
))))
1277 (%rassoc-key-test item alist key test
)
1278 (%rassoc-test item alist test
)))
1281 (%rassoc-key-test-not item alist key test-not
)
1282 (%rassoc-test-not item alist test-not
)))
1285 (%rassoc-key item alist key
)
1286 (%rassoc item alist
))))))
1288 (defun rassoc-if (predicate alist
&key key
)
1290 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1291 is supplied, apply it to the CDR of each cons before testing."
1292 (declare (explicit-check))
1293 (let ((predicate (%coerce-callable-to-fun predicate
))
1294 (key (and key
(%coerce-callable-to-fun key
))))
1296 (%rassoc-if-key predicate alist key
)
1297 (%rassoc-if predicate alist
))))
1299 (defun rassoc-if-not (predicate alist
&key key
)
1301 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1302 If KEY is supplied, apply it to the CDR of each cons before testing."
1303 (declare (explicit-check))
1304 (let ((predicate (%coerce-callable-to-fun predicate
))
1305 (key (and key
(%coerce-callable-to-fun key
))))
1307 (%rassoc-if-not-key predicate alist key
)
1308 (%rassoc-if-not predicate alist
))))
1310 ;;;; mapping functions
1312 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1313 ;;; MAPL, MAPLIST, and MAPCON
1315 ;;; Map the designated function over the arglists in the appropriate
1316 ;;; way. It is done when any of the arglists runs out. Until then, it
1317 ;;; CDRs down the arglists calling the function and accumulating
1318 ;;; results as desired.
1319 (defun map1 (fun-designator arglists accumulate take-car
)
1320 (do* ((fun (%coerce-callable-to-fun fun-designator
))
1321 (non-acc-result (car arglists
))
1322 (ret-list (list nil
))
1325 (args (make-list (length arglists
))))
1326 ((dolist (x arglists
) (or x
(return t
)))
1330 (do ((l arglists
(cdr l
))
1331 (arg args
(cdr arg
)))
1333 (setf (car arg
) (if take-car
(caar l
) (car l
)))
1334 (setf (car l
) (cdar l
)))
1335 (setq res
(apply fun args
))
1339 (setf (cdr temp
) res
)
1340 ;; KLUDGE: it is said that MAPCON is equivalent to
1341 ;; (apply #'nconc (maplist ...)) which means (nconc 1) would
1342 ;; return 1, but (nconc 1 1) should signal an error.
1343 ;; The transformed MAP code returns the last result, do that
1344 ;; here as well for consistency and simplicity.
1346 (setf temp
(last res
)))))
1347 (:list
(setf (cdr temp
) (list res
)
1348 temp
(cdr temp
))))))
1350 (defun mapc (function list
&rest more-lists
)
1352 "Apply FUNCTION to successive elements of lists. Return the second argument."
1353 (declare (explicit-check))
1354 (map1 function
(cons list more-lists
) nil t
))
1356 (defun mapcar (function list
&rest more-lists
)
1358 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1360 (declare (explicit-check))
1361 (map1 function
(cons list more-lists
) :list t
))
1363 (defun mapcan (function list
&rest more-lists
)
1365 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1367 (declare (explicit-check))
1368 (map1 function
(cons list more-lists
) :nconc t
))
1370 (defun mapl (function list
&rest more-lists
)
1372 "Apply FUNCTION to successive CDRs of list. Return NIL."
1373 (declare (explicit-check))
1374 (map1 function
(cons list more-lists
) nil nil
))
1376 (defun maplist (function list
&rest more-lists
)
1378 "Apply FUNCTION to successive CDRs of list. Return list of results."
1379 (declare (explicit-check))
1380 (map1 function
(cons list more-lists
) :list nil
))
1382 (defun mapcon (function list
&rest more-lists
)
1384 "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1385 (declare (explicit-check))
1386 (map1 function
(cons list more-lists
) :nconc nil
))
1388 ;;;; Specialized versions
1390 ;;; %ADJOIN-*, %ASSOC-*, %MEMBER-*, and %RASSOC-* functions. Deftransforms
1391 ;;; delegate to TRANSFORM-LIST-PRED-SEEK and TRANSFORM-LIST-ITEM-SEEK which
1392 ;;; pick the appropriate versions. These win because they have only positional
1393 ;;; arguments, the TEST, TEST-NOT & KEY functions are known to exist (or not),
1394 ;;; and are known to be functions instead of function designators. We are also
1395 ;;; able to transform many common cases to -EQ versions, which are
1396 ;;; substantially faster then EQL using ones.
1398 ((def (funs form
&optional variant
)
1399 (flet ((%def
(name &optional conditional
)
1401 `(do ((list list
(cdr list
)))
1403 (declare (list list
))
1404 (let ((this (car list
)))
1405 ,(let ((cxx (if (char= #\A
(char (string name
) 0))
1406 'car
; assoc, assoc-if, assoc-if-not
1407 'cdr
))) ; rassoc, rassoc-if, rassoc-if-not
1412 (let ((target (,cxx this
)))
1415 ;; If there is no TEST/TEST-NOT or
1416 ;; KEY, do the EQ/EQL test first,
1417 ;; before checking for NIL.
1418 `(let ((target (,cxx this
)))
1419 (when (and ,form this
)
1421 ((assoc-if assoc-if-not rassoc-if rassoc-if-not
)
1422 (aver (equal '(eql x
) (subseq form
0 2)))
1424 (let ((target (,cxx this
)))
1425 (,conditional
(funcall ,@(cdr form
))
1428 `(let ((target this
))
1431 ((member-if member-if-not
)
1432 (aver (equal '(eql x
) (subseq form
0 2)))
1433 `(let ((target this
))
1434 (,conditional
(funcall ,@(cdr form
))
1437 `(let ((target this
))
1440 (body (if (eq 'adjoin name
)
1441 `(if (let ,(when (member 'key funs
)
1442 `((x (funcall key x
))))
1447 `(defun ,(intern (format nil
"%~A~{-~A~}~@[-~A~]" name funs variant
))
1449 (declare (optimize speed
(sb!c
::verify-arg-count
0)))
1450 ,@(when funs
`((declare (function ,@funs
))))
1451 ,@(unless (member name
'(member assoc adjoin rassoc
)) `((declare (function x
))))
1452 (declare (explicit-check))
1459 ,@(when (and (not variant
) (member funs
'(() (key)) :test
#'equal
))
1460 (list (%def
'member-if
'when
)
1461 (%def
'member-if-not
'unless
)
1462 (%def
'assoc-if
'when
)
1463 (%def
'assoc-if-not
'unless
)
1464 (%def
'rassoc-if
'when
)
1465 (%def
'rassoc-if-not
'unless
)))))))
1472 (eql x
(funcall key target
)))
1474 (eq x
(funcall key target
))
1477 (funcall test x
(funcall key target
)))
1479 (not (funcall test-not x
(funcall key target
))))
1481 (funcall test x target
))
1483 (not (funcall test-not x target
))))