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 nth %setnth nthcdr make-list
23 nunion intersection nintersection set-difference nset-difference
24 set-exclusive-or nset-exclusive-or subsetp acons
26 subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
28 ;;; These functions perform basic list operations.
29 (defun car (list) #!+sb-doc "Return the 1st object in a list." (car list))
31 #!+sb-doc "Return all but the first object in a list."
33 (defun cadr (list) #!+sb-doc "Return the 2nd object in a list." (cadr list))
34 (defun cdar (list) #!+sb-doc "Return the cdr of the 1st sublist." (cdar list))
35 (defun caar (list) #!+sb-doc "Return the car of the 1st sublist." (caar list))
37 #!+sb-doc "Return all but the 1st two objects of a list."
40 #!+sb-doc "Return the 1st object in the cddr of a list."
43 #!+sb-doc "Return the 1st object in the cadr of a list."
46 #!+sb-doc "Return the 1st object in the caar of a list."
49 #!+sb-doc "Return the cdr of the caar of a list."
52 #!+sb-doc "Return the cdr of the cdar of a list."
55 #!+sb-doc "Return the cdr of the cddr of a list."
58 #!+sb-doc "Return the car of the cdar of a list."
61 #!+sb-doc "Return the cdr of the cadr of a list."
64 #!+sb-doc "Return the car of the caaar of a list."
67 #!+sb-doc "Return the car of the caadr of a list."
70 #!+sb-doc "Return the car of the caddr of a list."
73 #!+sb-doc "Return the car of the cdddr of a list."
76 #!+sb-doc "Return the cdr of the cdddr of a list."
79 #!+sb-doc "Return the cdr of the caaar of a list."
82 #!+sb-doc "Return the cdr of the cdaar of a list."
85 #!+sb-doc "Return the cdr of the cddar of a list."
88 #!+sb-doc "Return the car of the cadar of a list."
91 #!+sb-doc "Return the car of the cdaar of a list."
94 #!+sb-doc "Return the car of the cdadr of a list."
97 #!+sb-doc "Return the car of the cddar of a list."
100 #!+sb-doc "Return the cdr of the caadr of a list."
103 #!+sb-doc "Return the cdr of the cadar of a list."
106 #!+sb-doc "Return the cdr of the caddr of a list."
109 #!+sb-doc "Return the cdr of the cdadr of a list."
111 (defun cons (se1 se2)
112 #!+sb-doc "Return a list with SE1 as the CAR and SE2 as the CDR."
115 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
117 (defun tree-equal-test-not (x y test-not)
118 (declare (type function test-not))
121 (tree-equal-test-not (car x) (car y) test-not)
122 (tree-equal-test-not (cdr x) (cdr y) test-not)))
124 ((not (funcall test-not x y)) t)
127 (defun tree-equal-test (x y test)
128 (declare (type function test))
131 (tree-equal-test (car x) (car y) test)
132 (tree-equal-test (cdr x) (cdr y) test)))
134 ((funcall test x y) t)
137 (defun tree-equal (x y &key (test #'eql testp) (test-not nil notp))
139 "Return T if X and Y are isomorphic trees with identical leaves."
140 (when (and testp notp)
141 (error ":TEST and :TEST-NOT were both supplied."))
143 (tree-equal-test-not x y (%coerce-callable-to-fun test-not))
144 (tree-equal-test x y (%coerce-callable-to-fun test))))
148 "This is the recommended way to test for the end of a proper list. It
149 returns true if OBJECT is NIL, false if OBJECT is a CONS, and an error
150 for any other type of OBJECT."
153 (defun list-length (list)
155 "Return the length of the given List, or Nil if the List is circular."
160 (declare (type fixnum n)
162 (when (endp y) (return n))
163 (when (endp (cdr y)) (return (+ n 1)))
164 (when (and (eq y z) (> n 0)) (return nil))))
168 "Return the nth object in a list where the car is the zero-th element."
169 (car (nthcdr n list)))
173 "Return the 1st object in a list or NIL if the list is empty."
176 "Return the 2nd object in a list or NIL if there is no 2nd object."
180 "Return the 3rd object in a list or NIL if there is no 3rd object."
184 "Return the 4th object in a list or NIL if there is no 4th object."
188 "Return the 5th object in a list or NIL if there is no 5th object."
192 "Return the 6th object in a list or NIL if there is no 6th object."
193 (cadr (cddddr list)))
194 (defun seventh (list)
196 "Return the 7th object in a list or NIL if there is no 7th object."
197 (caddr (cddddr list)))
200 "Return the 8th object in a list or NIL if there is no 8th object."
201 (cadddr (cddddr list)))
204 "Return the 9th object in a list or NIL if there is no 9th object."
205 (car (cddddr (cddddr list))))
208 "Return the 10th object in a list or NIL if there is no 10th object."
209 (cadr (cddddr (cddddr list))))
212 "Means the same as the cdr of a list."
215 (defun nthcdr (n list)
217 "Performs the cdr function n times on a list."
218 (flet ((fast-nthcdr (n list)
219 (declare (type index n))
221 (result list (cdr result)))
222 ((not (plusp i)) result)
223 (declare (type index i)))))
225 (index (fast-nthcdr n list))
228 (r-2i list (cddr r-2i)))
229 ((and (eq r-i r-2i) (not (zerop i)))
230 (fast-nthcdr (mod n i) r-i))
231 (declare (type index i)))))))
235 ;;; Transforms in src/compiler/srctran.lisp pick the most specific
236 ;;; version possible. %LAST/BIGNUM is admittedly somewhat academic...
237 (macrolet ((last0-macro ()
240 (loop (unless (consp rest)
242 (shiftf list rest (cdr rest)))))
246 (loop (unless (consp rest)
248 (shiftf list rest (cdr rest)))))
250 `(let ((returned-list list)
252 (n (truly-the ,type n)))
257 (when (atom checked-list)
259 (if (zerop (truly-the ,type (decf n)))
265 (if (atom checked-list)
272 (declare (optimize speed (sb!c::verify-arg-count 0)))
276 (declare (optimize speed (sb!c::verify-arg-count 0)))
279 (defun %lastn/fixnum (list n)
280 (declare (optimize speed (sb!c::verify-arg-count 0))
281 (type (and unsigned-byte fixnum) n))
285 (t (lastn-macro fixnum))))
287 (defun %lastn/bignum (list n)
288 (declare (optimize speed (sb!c::verify-arg-count 0))
289 (type (and unsigned-byte bignum) n))
290 (lastn-macro unsigned-byte))
292 (defun last (list &optional (n 1))
294 "Return the last N conses (not the last element!) of a list."
301 (lastn-macro fixnum))
303 (lastn-macro unsigned-byte)))))))
305 (define-compiler-macro last (&whole form list &optional (n 1) &environment env)
306 (if (sb!xc:constantp n env)
307 (case (constant-form-value n env)
313 (defun list (&rest args)
315 "Return constructs and returns a list of its arguments."
318 ;;; LIST* is done the same as LIST, except that the last cons is made
321 (defun list* (arg &rest others)
323 "Return a list of the arguments with last cons a dotted pair."
324 ;; We know the &REST is a proper list.
325 (declare (optimize (sb!c::type-check 0)))
326 (cond ((atom others) arg)
327 ((atom (cdr others)) (cons arg (car others)))
328 (t (do ((x others (cdr x)))
329 ((null (cddr x)) (rplacd x (cadr x))))
332 (defun make-list (size &key initial-element)
334 "Constructs a list with size elements each set to value"
335 (declare (type index size))
336 (do ((count size (1- count))
337 (result '() (cons initial-element result)))
338 ((<= count 0) result)
339 (declare (type index count))))
341 (defun append (&rest lists)
343 "Construct a new list by concatenating the list arguments"
344 (declare (truly-dynamic-extent lists) (optimize speed))
345 (labels ((fail (object)
348 :expected-type 'list))
349 (append-into (last-cons current rest)
350 ;; Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST).
351 (declare (cons last-cons rest))
354 ;; normal case, cdr down the list
355 (append-into (setf (cdr last-cons) (list (car current)))
359 (let ((more (cdr rest)))
361 (setf (cdr last-cons) (car rest))
362 (append-into last-cons (car rest) more))))
365 (let ((current (car lists))
370 (let ((result (truly-the cons (list (car current)))))
382 (declare (optimize speed (sb!c::verify-arg-count 0)))
385 (let ((result (list (car x))))
386 (do ((more (cdr x) (cdr more))
387 (tail result (cdr tail)))
391 (rplacd tail (list (car more)))))))
393 (define-compiler-macro append (&whole form &rest lists)
397 (2 `(append2 ,@lists))
400 ;;;; list copying functions
402 (eval-when (:compile-toplevel :load-toplevel :execute)
403 (sb!xc:defmacro !copy-list-macro (list &key check-proper-list)
404 ;; Unless CHECK-PROPER-LIST is true, the list is copied correctly
405 ;; even if the list is not terminated by NIL. The new list is built
406 ;; by CDR'ing SPLICE which is always at the tail of the new list.
408 (let ((copy (list (car ,list))))
409 (do ((orig (cdr ,list) (cdr orig))
410 (splice copy (cdr (rplacd splice (cons (car orig) nil)))))
411 (,@(if check-proper-list
415 (rplacd splice orig))))
418 (defun copy-list (list)
420 "Return a new list which is EQUAL to LIST. LIST may be improper."
421 (!copy-list-macro list))
423 (defun copy-alist (alist)
425 "Return a new association list which is EQUAL to ALIST."
429 (cons (if (atom (car alist))
431 (cons (caar alist) (cdar alist)))
433 (do ((x (cdr alist) (cdr x))
439 (cons (caar x) (cdar x)))
444 (defun copy-tree (object)
446 "Recursively copy trees of conses."
448 (cons (copy-tree (car object)) (copy-tree (cdr object)))
451 ;;;; more commonly-used list functions
453 (defun revappend (x y)
455 "Return (append (reverse x) y)."
456 (do ((top x (cdr top))
457 (result y (cons (car top) result)))
458 ((endp top) result)))
460 ;;; NCONC finds the first non-null list, so it can make splice point
461 ;;; to a cons. After finding the first cons element, it holds it in a
462 ;;; result variable while running down successive elements tacking
463 ;;; them together. While tacking lists together, if we encounter a
464 ;;; null list, we set the previous list's last cdr to nil just in case
465 ;;; it wasn't already nil, and it could have been dotted while the
466 ;;; null list was the last argument to NCONC. The manipulation of
467 ;;; splice (that is starting it out on a first cons, setting LAST of
468 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
469 ;;; and it avoids running down the last argument to NCONC which allows
470 ;;; the last argument to be circular.
471 (defun nconc (&rest lists)
473 "Concatenates the lists given as arguments (by changing them)"
474 (declare (truly-dynamic-extent lists) (optimize speed))
475 (flet ((fail (object)
478 :expected-type 'list)))
479 (do ((top lists (cdr top)))
481 (let ((top-of-top (car top)))
484 (let* ((result top-of-top)
486 (do ((elements (cdr top) (cdr elements)))
488 (let ((ele (car elements)))
490 (cons (rplacd (last splice) ele)
492 (null (rplacd (last splice) nil))
493 (atom (if (cdr elements)
495 (rplacd (last splice) ele))))))
501 (return top-of-top))))))))
505 "Return (NCONC (NREVERSE X) Y)."
506 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
507 (2nd x 1st) ;2nd follows first down the list.
508 (3rd y 2nd)) ;3rd follows 2nd down the list.
512 (flet (;; Return the number of conses at the head of the
513 ;; possibly-improper list LIST. (Or if LIST is circular, you
516 (do ((in-list list (cdr in-list))
517 (result 0 (1+ result)))
520 (declare (type index result)))))
521 (declare (ftype (function (t) index) count-conses))
522 (defun butlast (list &optional (n 1))
524 (let ((n-conses-in-list (count-conses list)))
526 ;; (We can't use SUBSEQ in this case because LIST isn't
527 ;; necessarily a proper list, but SUBSEQ expects a
528 ;; proper sequence. COPY-LIST isn't so fussy.)
530 ((>= n n-conses-in-list)
533 ;; (LIST isn't necessarily a proper list in this case
534 ;; either, and technically SUBSEQ wants a proper
535 ;; sequence, but no reasonable implementation of SUBSEQ
536 ;; will actually walk down to the end of the list to
537 ;; check, and since we're calling our own implementation
538 ;; we know it's reasonable, so it's OK.)
539 (subseq list 0 (- n-conses-in-list n)))))
541 (defun nbutlast (list &optional (n 1))
544 ((not (typep n 'index))
546 (t (let ((n-conses-in-list (count-conses list)))
547 (unless (<= n-conses-in-list n)
548 (setf (cdr (nthcdr (- n-conses-in-list n 1) list))
552 (defun ldiff (list object)
553 "Return a new list, whose elements are those of LIST that appear before
554 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
555 LIST must be a proper list or a dotted list."
556 (do* ((list list (cdr list))
560 (if (eql list object)
562 (progn (rplacd splice list) (cdr result))))
563 (if (eql list object)
564 (return (cdr result))
565 (setq splice (cdr (rplacd splice (list (car list))))))))
567 ;;;; functions to alter list structure
569 (defun rplaca (cons x)
571 "Change the CAR of CONS to X and return the CONS."
574 (defun rplacd (cons x)
576 "Change the CDR of CONS to X and return the CONS."
579 ;;; The following are for use by SETF.
581 (defun %rplaca (x val) (rplaca x val) val)
583 (defun %rplacd (x val) (rplacd x val) val)
585 ;;; Set the Nth element of LIST to NEWVAL.
586 (defun %setnth (n list newval)
589 (do ((count n (1- count))
590 (list list (cdr list)))
592 (error "~S is too large an index for SETF of NTH." n))
593 (declare (type fixnum count))
597 (t (let ((cons (nthcdr n list)))
599 (error "~S is too large an index for SETF of NTH." n))
603 ;;;; :KEY arg optimization to save funcall of IDENTITY
605 ;;; APPLY-KEY saves us a function call sometimes.
606 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
607 ;;; because it's used in seq.lisp and sort.lisp.
608 (defmacro apply-key (key element)
610 (funcall ,key ,element)
613 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
615 ;;; Use these with the following &KEY args:
616 (defmacro with-set-keys (funcall)
618 ,(append funcall '(:key key :test-not test-not))
619 ,(append funcall '(:key key :test test))))
621 (defmacro satisfies-the-test (item elt)
622 (let ((key-tmp (gensym)))
623 `(let ((,key-tmp (apply-key key ,elt)))
624 (cond (testp (funcall test ,item ,key-tmp))
625 (notp (not (funcall test-not ,item ,key-tmp)))
626 (t (funcall test ,item ,key-tmp))))))
628 ;;;; substitution of expressions
630 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
632 "Substitutes new for subtrees matching old."
633 (when (and testp notp)
634 (error ":TEST and :TEST-NOT were both supplied."))
635 (let ((key (and key (%coerce-callable-to-fun key)))
636 (test (if testp (%coerce-callable-to-fun test) test))
637 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
638 (declare (type function test test-not))
639 (labels ((s (subtree)
640 (cond ((satisfies-the-test old subtree) new)
641 ((atom subtree) subtree)
642 (t (let ((car (s (car subtree)))
643 (cdr (s (cdr subtree))))
644 (if (and (eq car (car subtree))
645 (eq cdr (cdr subtree)))
650 (defun subst-if (new test tree &key key)
652 "Substitutes new for subtrees for which test is true."
653 (let ((test (%coerce-callable-to-fun test))
654 (key (and key (%coerce-callable-to-fun key))))
655 (labels ((s (subtree)
656 (cond ((funcall test (apply-key key subtree)) new)
657 ((atom subtree) subtree)
658 (t (let ((car (s (car subtree)))
659 (cdr (s (cdr subtree))))
660 (if (and (eq car (car subtree))
661 (eq cdr (cdr subtree)))
666 (defun subst-if-not (new test tree &key key)
668 "Substitutes new for subtrees for which test is false."
669 (let ((test (%coerce-callable-to-fun test))
670 (key (and key (%coerce-callable-to-fun key))))
671 (labels ((s (subtree)
672 (cond ((not (funcall test (apply-key key subtree))) new)
673 ((atom subtree) subtree)
674 (t (let ((car (s (car subtree)))
675 (cdr (s (cdr subtree))))
676 (if (and (eq car (car subtree))
677 (eq cdr (cdr subtree)))
682 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
684 "Substitute NEW for subtrees matching OLD."
685 (when (and testp notp)
686 (error ":TEST and :TEST-NOT were both supplied."))
687 (let ((key (and key (%coerce-callable-to-fun key)))
688 (test (if testp (%coerce-callable-to-fun test) test))
689 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
690 (declare (type function test test-not))
691 (labels ((s (subtree)
692 (cond ((satisfies-the-test old subtree) new)
693 ((atom subtree) subtree)
694 (t (do* ((last nil subtree)
695 (subtree subtree (cdr subtree)))
697 (if (satisfies-the-test old subtree)
698 (setf (cdr last) new)))
699 (if (satisfies-the-test old subtree)
700 (return (setf (cdr last) new))
701 (setf (car subtree) (s (car subtree)))))
705 (defun nsubst-if (new test tree &key key)
707 "Substitute NEW for subtrees of TREE for which TEST is true."
708 (let ((test (%coerce-callable-to-fun test))
709 (key (and key (%coerce-callable-to-fun key))))
710 (labels ((s (subtree)
711 (cond ((funcall test (apply-key key subtree)) new)
712 ((atom subtree) subtree)
713 (t (do* ((last nil subtree)
714 (subtree subtree (cdr subtree)))
716 (if (funcall test (apply-key key subtree))
717 (setf (cdr last) new)))
718 (if (funcall test (apply-key key subtree))
719 (return (setf (cdr last) new))
720 (setf (car subtree) (s (car subtree)))))
724 (defun nsubst-if-not (new test tree &key key)
726 "Substitute NEW for subtrees of TREE for which TEST is false."
727 (let ((test (%coerce-callable-to-fun test))
728 (key (and key (%coerce-callable-to-fun key))))
729 (labels ((s (subtree)
730 (cond ((not (funcall test (apply-key key subtree))) new)
731 ((atom subtree) subtree)
732 (t (do* ((last nil subtree)
733 (subtree subtree (cdr subtree)))
735 (if (not (funcall test (apply-key key subtree)))
736 (setf (cdr last) new)))
737 (if (not (funcall test (apply-key key subtree)))
738 (return (setf (cdr last) new))
739 (setf (car subtree) (s (car subtree)))))
743 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
745 "Substitute from ALIST into TREE nondestructively."
746 (when (and testp notp)
747 (error ":TEST and :TEST-NOT were both supplied."))
748 (let ((key (and key (%coerce-callable-to-fun key)))
749 (test (if testp (%coerce-callable-to-fun test) test))
750 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
751 (declare (type function test test-not))
752 (declare (inline assoc))
753 (labels ((s (subtree)
754 (let* ((key-val (apply-key key subtree))
756 (assoc key-val alist :test-not test-not)
757 (assoc key-val alist :test test))))
758 (cond (assoc (cdr assoc))
759 ((atom subtree) subtree)
760 (t (let ((car (s (car subtree)))
761 (cdr (s (cdr subtree))))
762 (if (and (eq car (car subtree))
763 (eq cdr (cdr subtree)))
765 (cons car cdr))))))))
768 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
769 ;;; because it can be referenced in inline expansions.
770 (defmacro nsublis-macro ()
771 (let ((key-tmp (gensym)))
772 `(let ((,key-tmp (apply-key key subtree)))
774 (assoc ,key-tmp alist :test-not test-not)
775 (assoc ,key-tmp alist :test test)))))
777 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
779 "Substitute from ALIST into TRUE destructively."
780 (when (and testp notp)
781 (error ":TEST and :TEST-NOT were both supplied."))
782 (let ((key (and key (%coerce-callable-to-fun key)))
783 (test (if testp (%coerce-callable-to-fun test) test))
784 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
785 (declare (inline assoc))
787 (labels ((s (subtree)
788 (cond ((setq temp (nsublis-macro))
790 ((atom subtree) subtree)
791 (t (do* ((last nil subtree)
792 (subtree subtree (cdr subtree)))
794 (if (setq temp (nsublis-macro))
795 (setf (cdr last) (cdr temp))))
796 (if (setq temp (nsublis-macro))
797 (return (setf (cdr last) (cdr temp)))
798 (setf (car subtree) (s (car subtree)))))
802 ;;;; functions for using lists as sets
804 (defun member (item list &key key (test nil testp) (test-not nil notp))
806 "Return the tail of LIST beginning with first element satisfying EQLity,
807 :TEST, or :TEST-NOT with the given ITEM."
808 (when (and testp notp)
809 (error ":TEST and :TEST-NOT were both supplied."))
810 (let ((key (and key (%coerce-callable-to-fun key)))
811 (test (and testp (%coerce-callable-to-fun test)))
812 (test-not (and notp (%coerce-callable-to-fun test-not))))
815 (%member-key-test item list key test)
816 (%member-test item list test)))
819 (%member-key-test-not item list key test-not)
820 (%member-test-not item list test-not)))
823 (%member-key item list key)
824 (%member item list))))))
826 (defun member-if (test list &key key)
828 "Return tail of LIST beginning with first element satisfying TEST."
829 (let ((test (%coerce-callable-to-fun test))
830 (key (and key (%coerce-callable-to-fun key))))
832 (%member-if-key test list key)
833 (%member-if test list))))
835 (defun member-if-not (test list &key key)
837 "Return tail of LIST beginning with first element not satisfying TEST."
838 (let ((test (%coerce-callable-to-fun test))
839 (key (and key (%coerce-callable-to-fun key))))
841 (%member-if-not-key test list key)
842 (%member-if-not test list))))
844 (defun tailp (object list)
846 "Return true if OBJECT is the same as some tail of LIST, otherwise
847 returns false. LIST must be a proper list or a dotted list."
848 (do ((list list (cdr list)))
849 ((atom list) (eql list object))
850 (if (eql object list)
853 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
855 "Add ITEM to LIST unless it is already a member"
856 (when (and testp notp)
857 (error ":TEST and :TEST-NOT were both supplied."))
858 (let ((key (and key (%coerce-callable-to-fun key)))
859 (test (and testp (%coerce-callable-to-fun test)))
860 (test-not (and notp (%coerce-callable-to-fun test-not))))
863 (%adjoin-key-test item list key test)
864 (%adjoin-test item list test)))
867 (%adjoin-key-test-not item list key test-not)
868 (%adjoin-test-not item list test-not)))
871 (%adjoin-key item list key)
872 (%adjoin item list))))))
874 (defconstant +list-based-union-limit+ 80)
876 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
878 "Return the union of LIST1 and LIST2."
879 (declare (inline member))
880 (when (and testp notp)
881 (error ":TEST and :TEST-NOT were both supplied."))
882 ;; We have to possibilities here: for shortish lists we pick up the
883 ;; shorter one as the result, and add the other one to it. For long
884 ;; lists we use a hash-table when possible.
885 (let ((n1 (length list1))
887 (key (and key (%coerce-callable-to-fun key)))
889 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
890 (lambda (x y) (not (funcall test-not-fun x y))))
891 (%coerce-callable-to-fun test))))
892 (multiple-value-bind (short long n-short)
894 (values list1 list2 n1)
895 (values list2 list1 n2))
896 (if (or (< n-short +list-based-union-limit+)
897 (not (member test (list #'eq #'eql #'equal #'equalp))))
900 (unless (member (apply-key key elt) orig :key key :test test)
903 (let ((table (make-hash-table :test test :size (+ n1 n2)))
906 (setf (gethash (apply-key key elt) table) elt))
908 (setf (gethash (apply-key key elt) table) elt))
909 (maphash (lambda (k v)
915 ;;; Destination and source are SETF-able and many-evaluable. Set the
916 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
918 ;;; FIXME: needs a more mnemonic name
919 (defmacro steve-splice (source destination)
920 `(let ((temp ,source))
921 (setf ,source (cdr ,source)
922 (cdr temp) ,destination
925 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
927 "Destructively return the union of LIST1 and LIST2."
928 (declare (inline member))
929 (when (and testp notp)
930 (error ":TEST and :TEST-NOT were both supplied."))
931 ;; We have to possibilities here: for shortish lists we pick up the
932 ;; shorter one as the result, and add the other one to it. For long
933 ;; lists we use a hash-table when possible.
934 (let ((n1 (length list1))
936 (key (and key (%coerce-callable-to-fun key)))
938 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
939 (lambda (x y) (not (funcall test-not-fun x y))))
940 (%coerce-callable-to-fun test))))
941 (multiple-value-bind (short long n-short)
943 (values list1 list2 n1)
944 (values list2 list1 n2))
945 (if (or (< n-short +list-based-union-limit+)
946 (not (member test (list #'eq #'eql #'equal #'equalp))))
948 (do ((elt (car long) (car long)))
950 (if (not (member (apply-key key elt) orig :key key :test test))
951 (steve-splice long short)
952 (setf long (cdr long))))
954 (let ((table (make-hash-table :test test :size (+ n1 n2))))
956 (setf (gethash (apply-key key elt) table) elt))
958 (setf (gethash (apply-key key elt) table) elt))
961 (maphash (lambda (k v)
970 (defun intersection (list1 list2
971 &key key (test #'eql testp) (test-not nil notp))
973 "Return the intersection of LIST1 and LIST2."
974 (declare (inline member))
975 (when (and testp notp)
976 (error ":TEST and :TEST-NOT were both supplied."))
977 (let ((key (and key (%coerce-callable-to-fun key))))
980 (if (with-set-keys (member (apply-key key elt) list2))
984 (defun nintersection (list1 list2
985 &key key (test #'eql testp) (test-not nil notp))
987 "Destructively return the intersection of LIST1 and LIST2."
988 (declare (inline member))
989 (when (and testp notp)
990 (error ":TEST and :TEST-NOT were both supplied."))
991 (let ((key (and key (%coerce-callable-to-fun key))))
994 (do () ((endp list1))
995 (if (with-set-keys (member (apply-key key (car list1)) list2))
996 (steve-splice list1 res)
997 (setq list1 (cdr list1))))
1000 (defun set-difference (list1 list2
1001 &key key (test #'eql testp) (test-not nil notp))
1003 "Return the elements of LIST1 which are not in LIST2."
1004 (declare (inline member))
1005 (when (and testp notp)
1006 (error ":TEST and :TEST-NOT were both supplied."))
1007 (let ((key (and key (%coerce-callable-to-fun key))))
1012 (if (not (with-set-keys (member (apply-key key elt) list2)))
1016 (defun nset-difference (list1 list2
1017 &key key (test #'eql testp) (test-not nil notp))
1019 "Destructively return the elements of LIST1 which are not in LIST2."
1020 (declare (inline member))
1021 (when (and testp notp)
1022 (error ":TEST and :TEST-NOT were both supplied."))
1023 (let ((key (and key (%coerce-callable-to-fun key))))
1026 (do () ((endp list1))
1027 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
1028 (steve-splice list1 res)
1029 (setq list1 (cdr list1))))
1032 (defun set-exclusive-or (list1 list2
1033 &key key (test #'eql testp) (test-not #'eql notp))
1035 "Return new list of elements appearing exactly once in LIST1 and LIST2."
1036 (declare (inline member))
1037 (when (and testp notp)
1038 (error ":TEST and :TEST-NOT were both supplied."))
1040 (key (and key (%coerce-callable-to-fun key)))
1041 (test (if testp (%coerce-callable-to-fun test) test))
1042 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1043 (declare (type function test test-not))
1045 (unless (with-set-keys (member (apply-key key elt) list2))
1046 (setq result (cons elt result))))
1047 (let ((test (if testp
1048 (lambda (x y) (funcall test y x))
1051 (lambda (x y) (funcall test-not y x))
1054 (unless (with-set-keys (member (apply-key key elt) list1))
1055 (setq result (cons elt result)))))
1058 (defun nset-exclusive-or (list1 list2
1059 &key key (test #'eql testp) (test-not #'eql notp))
1061 "Destructively return a list with elements which appear but once in LIST1
1063 (when (and testp notp)
1064 (error ":TEST and :TEST-NOT were both supplied."))
1065 (let ((key (and key (%coerce-callable-to-fun key)))
1066 (test (if testp (%coerce-callable-to-fun test) test))
1067 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1068 (declare (type function test test-not))
1069 ;; The outer loop examines LIST1 while the inner loop examines
1070 ;; LIST2. If an element is found in LIST2 "equal" to the element
1071 ;; in LIST1, both are spliced out. When the end of LIST1 is
1072 ;; reached, what is left of LIST2 is tacked onto what is left of
1073 ;; LIST1. The splicing operation ensures that the correct
1074 ;; operation is performed depending on whether splice is at the
1075 ;; top of the list or not.
1081 ;; elements of LIST2, which are "equal" to some processed
1082 ;; earlier elements of LIST1
1087 (rplacd splicex list2))
1089 (let ((key-val-x (apply-key key (car x)))
1090 (found-duplicate nil))
1092 ;; Move all elements from LIST2, which are "equal" to (CAR X),
1094 (do* ((y list2 next-y)
1095 (next-y (cdr y) (cdr y))
1098 (cond ((let ((key-val-y (apply-key key (car y))))
1100 (not (funcall test-not key-val-x key-val-y))
1101 (funcall test key-val-x key-val-y)))
1103 (setq list2 (cdr y))
1104 (rplacd splicey (cdr y)))
1105 (setq deleted-y (rplacd y deleted-y))
1106 (setq found-duplicate t))
1107 (t (setq splicey y))))
1109 (unless found-duplicate
1110 (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1114 (setq list1 (cdr x))
1115 (rplacd splicex (cdr x)))
1116 (setq splicex x))))))
1118 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1120 "Return T if every element in LIST1 is also in LIST2."
1121 (declare (inline member))
1122 (when (and testp notp)
1123 (error ":TEST and :TEST-NOT were both supplied."))
1124 (let ((key (and key (%coerce-callable-to-fun key))))
1126 (unless (with-set-keys (member (apply-key key elt) list2))
1127 (return-from subsetp nil)))
1130 ;;;; functions that operate on association lists
1132 (defun acons (key datum alist)
1134 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1135 (cons (cons key datum) alist))
1137 (defun pairlis (keys data &optional (alist '()))
1139 "Construct an association list from KEYS and DATA (adding to ALIST)."
1140 (do ((x keys (cdr x))
1142 ((and (endp x) (endp y)) alist)
1143 (if (or (endp x) (endp y))
1144 (error "The lists of keys and data are of unequal length."))
1145 (setq alist (acons (car x) (car y) alist))))
1147 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1149 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1151 (when (and testp notp)
1152 (error ":TEST and :TEST-NOT were both supplied."))
1153 (let ((key (and key (%coerce-callable-to-fun key)))
1154 (test (and testp (%coerce-callable-to-fun test)))
1155 (test-not (and notp (%coerce-callable-to-fun test-not))))
1158 (%assoc-key-test item alist key test)
1159 (%assoc-test item alist test)))
1162 (%assoc-key-test-not item alist key test-not)
1163 (%assoc-test-not item alist test-not)))
1166 (%assoc-key item alist key)
1167 (%assoc item alist))))))
1169 (defun assoc-if (predicate alist &key key)
1171 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1172 KEY is supplied, apply it to the CAR of each cons before testing."
1173 (let ((predicate (%coerce-callable-to-fun predicate))
1174 (key (and key (%coerce-callable-to-fun key))))
1176 (%assoc-if-key predicate alist key)
1177 (%assoc-if predicate alist))))
1179 (defun assoc-if-not (predicate alist &key key)
1181 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1182 If KEY is supplied, apply it to the CAR of each cons before testing."
1183 (let ((predicate (%coerce-callable-to-fun predicate))
1184 (key (and key (%coerce-callable-to-fun key))))
1186 (%assoc-if-not-key predicate alist key)
1187 (%assoc-if-not predicate alist))))
1189 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1190 (declare (list alist))
1192 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1194 (when (and testp notp)
1195 (error ":TEST and :TEST-NOT were both supplied."))
1196 (let ((key (and key (%coerce-callable-to-fun key)))
1197 (test (and testp (%coerce-callable-to-fun test)))
1198 (test-not (and notp (%coerce-callable-to-fun test-not))))
1201 (%rassoc-key-test item alist key test)
1202 (%rassoc-test item alist test)))
1205 (%rassoc-key-test-not item alist key test-not)
1206 (%rassoc-test-not item alist test-not)))
1209 (%rassoc-key item alist key)
1210 (%rassoc item alist))))))
1212 (defun rassoc-if (predicate alist &key key)
1214 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1215 is supplied, apply it to the CDR of each cons before testing."
1216 (let ((predicate (%coerce-callable-to-fun predicate))
1217 (key (and key (%coerce-callable-to-fun key))))
1219 (%rassoc-if-key predicate alist key)
1220 (%rassoc-if predicate alist))))
1222 (defun rassoc-if-not (predicate alist &key key)
1224 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1225 If KEY is supplied, apply it to the CDR of each cons before testing."
1226 (let ((predicate (%coerce-callable-to-fun predicate))
1227 (key (and key (%coerce-callable-to-fun key))))
1229 (%rassoc-if-not-key predicate alist key)
1230 (%rassoc-if-not predicate alist))))
1232 ;;;; mapping functions
1234 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1235 ;;; MAPL, MAPLIST, and MAPCON
1237 ;;; Map the designated function over the arglists in the appropriate
1238 ;;; way. It is done when any of the arglists runs out. Until then, it
1239 ;;; CDRs down the arglists calling the function and accumulating
1240 ;;; results as desired.
1241 (defun map1 (fun-designator original-arglists accumulate take-car)
1242 (let ((fun (%coerce-callable-to-fun fun-designator)))
1243 (let* ((arglists (copy-list original-arglists))
1244 (ret-list (list nil))
1248 ((dolist (x arglists nil) (if (null x) (return t)))
1251 (car original-arglists)))
1252 (do ((l arglists (cdr l)))
1254 (push (if take-car (caar l) (car l)) args)
1255 (setf (car l) (cdar l)))
1256 (setq res (apply fun (nreverse args)))
1258 (:nconc (setq temp (last (nconc temp res))))
1259 (:list (rplacd temp (list res))
1260 (setq temp (cdr temp))))))))
1262 (defun mapc (function list &rest more-lists)
1264 "Apply FUNCTION to successive elements of lists. Return the second argument."
1265 (map1 function (cons list more-lists) nil t))
1267 (defun mapcar (function list &rest more-lists)
1269 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1271 (map1 function (cons list more-lists) :list t))
1273 (defun mapcan (function list &rest more-lists)
1275 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1277 (map1 function (cons list more-lists) :nconc t))
1279 (defun mapl (function list &rest more-lists)
1281 "Apply FUNCTION to successive CDRs of list. Return NIL."
1282 (map1 function (cons list more-lists) nil nil))
1284 (defun maplist (function list &rest more-lists)
1286 "Apply FUNCTION to successive CDRs of list. Return list of results."
1287 (map1 function (cons list more-lists) :list nil))
1289 (defun mapcon (function list &rest more-lists)
1291 "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1292 (map1 function (cons list more-lists) :nconc nil))
1294 ;;;; Specialized versions
1296 ;;; %ADJOIN-*, %ASSOC-*, %MEMBER-*, and %RASSOC-* functions. Deftransforms
1297 ;;; delegate to TRANSFORM-LIST-PRED-SEEK and TRANSFORM-LIST-ITEM-SEEK which
1298 ;;; pick the appropriate versions. These win because they have only positional
1299 ;;; arguments, the TEST, TEST-NOT & KEY functions are known to exist (or not),
1300 ;;; and are known to be functions instead of function designators. We are also
1301 ;;; able to transform many common cases to -EQ versions, which are
1302 ;;; substantially faster then EQL using ones.
1304 ((def (funs form &optional variant)
1305 (flet ((%def (name &optional conditional)
1307 `(do ((list list (cdr list)))
1309 (declare (list list))
1310 (let ((this (car list)))
1311 ,(let ((cxx (if (char= #\A (char (string name) 0))
1312 'car ; assoc, assoc-if, assoc-if-not
1313 'cdr))) ; rassoc, rassoc-if, rassoc-if-not
1318 (let ((target (,cxx this)))
1321 ;; If there is no TEST/TEST-NOT or
1322 ;; KEY, do the EQ/EQL test first,
1323 ;; before checking for NIL.
1324 `(let ((target (,cxx this)))
1325 (when (and ,form this)
1327 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
1328 (aver (equal '(eql x) (subseq form 0 2)))
1330 (let ((target (,cxx this)))
1331 (,conditional (funcall ,@(cdr form))
1334 `(let ((target this))
1337 ((member-if member-if-not)
1338 (aver (equal '(eql x) (subseq form 0 2)))
1339 `(let ((target this))
1340 (,conditional (funcall ,@(cdr form))
1343 `(let ((target this))
1346 (body (if (eq 'adjoin name)
1347 `(if (let ,(when (member 'key funs)
1348 `((x (funcall key x))))
1353 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1355 (declare (optimize speed (sb!c::verify-arg-count 0)))
1356 ,@(when funs `((declare (function ,@funs))))
1357 ,@(unless (member name '(member assoc adjoin rassoc)) `((declare (function x))))
1364 ,@(when (and (not variant) (member funs '(() (key)) :test #'equal))
1365 (list (%def 'member-if 'when)
1366 (%def 'member-if-not 'unless)
1367 (%def 'assoc-if 'when)
1368 (%def 'assoc-if-not 'unless)
1369 (%def 'rassoc-if 'when)
1370 (%def 'rassoc-if-not 'unless)))))))
1377 (eql x (funcall key target)))
1379 (eq x (funcall key target))
1382 (funcall test x (funcall key target)))
1384 (not (funcall test-not x (funcall key target))))
1386 (funcall test x target))
1388 (not (funcall test-not x target))))