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 last last1 make-list append
22 nconc nconc2 member member-if member-if-not tailp adjoin union
23 nunion intersection nintersection set-difference nset-difference
24 set-exclusive-or nset-exclusive-or subsetp acons assoc
25 assoc-if assoc-if-not rassoc rassoc-if rassoc-if-not subst subst-if
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 "Return the last cons (not the last element) of a list"
238 (loop (unless (consp rest) (return list))
239 (shiftf list rest (cdr rest)))))
241 (defun last (list &optional (n 1))
243 "Return the last N conses (not the last element!) of a list."
247 (do ((checked-list list (cdr checked-list))
249 (index 0 (1+ index)))
250 ((atom checked-list) returned-list)
251 (declare (type index index))
253 (pop returned-list)))
256 (defun list (&rest args)
258 "Return constructs and returns a list of its arguments."
261 ;;; LIST* is done the same as LIST, except that the last cons is made
264 (defun list* (arg &rest others)
266 "Return a list of the arguments with last cons a dotted pair"
267 (cond ((atom others) arg)
268 ((atom (cdr others)) (cons arg (car others)))
269 (t (do ((x others (cdr x)))
270 ((null (cddr x)) (rplacd x (cadr x))))
273 (defun make-list (size &key initial-element)
275 "Constructs a list with size elements each set to value"
276 (declare (type index size))
277 (do ((count size (1- count))
278 (result '() (cons initial-element result)))
279 ((<= count 0) result)
280 (declare (type index count))))
282 (defun append (&rest lists)
284 "Construct a new list by concatenating the list arguments"
285 (labels ((fail (object)
288 :expected-type 'list))
289 (append-into (last-cons current rest)
290 "Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST)."
291 (declare (cons last-cons rest))
292 (cond ((consp current)
293 (append-into (setf (cdr last-cons) (list (car current)))
296 ((not (null current)) (fail current))
297 ((null (cdr rest)) (setf (cdr last-cons) (car rest)))
298 (t (append-into last-cons (car rest) (cdr rest)))))
300 (let ((current (car lists))
302 (cond ((null rest) current)
304 (let ((result (truly-the cons (list (car current)))))
309 ((null current) (append1 rest))
310 (t (fail current))))))
313 ;;;; list copying functions
315 (defun copy-list (list)
317 "Return a new list which is EQUAL to LIST."
318 ;; The list is copied correctly even if the list is not terminated
319 ;; by NIL. The new list is built by CDR'ing SPLICE which is always
320 ;; at the tail of the new list.
323 (let ((result (list (car list))))
324 (do ((x (cdr list) (cdr x))
326 (cdr (rplacd splice (cons (car x) '())))))
332 (defun copy-alist (alist)
334 "Return a new association list which is EQUAL to ALIST."
338 (cons (if (atom (car alist))
340 (cons (caar alist) (cdar alist)))
342 (do ((x (cdr alist) (cdr x))
348 (cons (caar x) (cdar x)))
353 (defun copy-tree (object)
355 "Recursively copy trees of conses."
357 (cons (copy-tree (car object)) (copy-tree (cdr object)))
360 ;;;; more commonly-used list functions
362 (defun revappend (x y)
364 "Return (append (reverse x) y)."
365 (do ((top x (cdr top))
366 (result y (cons (car top) result)))
367 ((endp top) result)))
369 ;;; NCONC finds the first non-null list, so it can make splice point
370 ;;; to a cons. After finding the first cons element, it holds it in a
371 ;;; result variable while running down successive elements tacking
372 ;;; them together. While tacking lists together, if we encounter a
373 ;;; null list, we set the previous list's last cdr to nil just in case
374 ;;; it wasn't already nil, and it could have been dotted while the
375 ;;; null list was the last argument to NCONC. The manipulation of
376 ;;; splice (that is starting it out on a first cons, setting LAST of
377 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
378 ;;; and it avoids running down the last argument to NCONC which allows
379 ;;; the last argument to be circular.
380 (defun nconc (&rest lists)
382 "Concatenates the lists given as arguments (by changing them)"
383 (flet ((fail (object)
386 :expected-type 'list)))
387 (do ((top lists (cdr top)))
389 (let ((top-of-top (car top)))
392 (let* ((result top-of-top)
394 (do ((elements (cdr top) (cdr elements)))
396 (let ((ele (car elements)))
398 (cons (rplacd (last splice) ele)
400 (null (rplacd (last splice) nil))
401 (atom (if (cdr elements)
403 (rplacd (last splice) ele)))
410 (return top-of-top)))
411 (t (fail top-of-top)))))))
421 (shiftf z rest (cdr rest))))))
425 "Return (NCONC (NREVERSE X) Y)."
426 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
427 (2nd x 1st) ;2nd follows first down the list.
428 (3rd y 2nd)) ;3rd follows 2nd down the list.
432 (flet (;; Return the number of conses at the head of the
433 ;; possibly-improper list LIST. (Or if LIST is circular, you
436 (do ((in-list list (cdr in-list))
437 (result 0 (1+ result)))
440 (declare (type index result)))))
441 (declare (ftype (function (t) index) count-conses))
442 (defun butlast (list &optional (n 1))
444 (let ((n-conses-in-list (count-conses list)))
446 ;; (We can't use SUBSEQ in this case because LIST isn't
447 ;; necessarily a proper list, but SUBSEQ expects a
448 ;; proper sequence. COPY-LIST isn't so fussy.)
450 ((>= n n-conses-in-list)
453 ;; (LIST isn't necessarily a proper list in this case
454 ;; either, and technically SUBSEQ wants a proper
455 ;; sequence, but no reasonable implementation of SUBSEQ
456 ;; will actually walk down to the end of the list to
457 ;; check, and since we're calling our own implementation
458 ;; we know it's reasonable, so it's OK.)
459 (subseq list 0 (- n-conses-in-list n)))))
461 (defun nbutlast (list &optional (n 1))
464 ((not (typep n 'index))
466 (t (let ((n-conses-in-list (count-conses list)))
467 (unless (<= n-conses-in-list n)
468 (setf (cdr (nthcdr (- n-conses-in-list n 1) list))
472 (defun ldiff (list object)
473 "Return a new list, whose elements are those of LIST that appear before
474 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
475 LIST must be a proper list or a dotted list."
476 (do* ((list list (cdr list))
480 (if (eql list object)
482 (progn (rplacd splice list) (cdr result))))
483 (if (eql list object)
484 (return (cdr result))
485 (setq splice (cdr (rplacd splice (list (car list))))))))
487 ;;;; functions to alter list structure
491 "Change the CAR of X to Y and return the new X."
496 "Change the CDR of X to Y and return the new X."
499 ;;; The following are for use by SETF.
501 (defun %rplaca (x val) (rplaca x val) val)
503 (defun %rplacd (x val) (rplacd x val) val)
505 ;;; Set the Nth element of LIST to NEWVAL.
506 (defun %setnth (n list newval)
509 (do ((count n (1- count))
510 (list list (cdr list)))
512 (error "~S is too large an index for SETF of NTH." n))
513 (declare (type fixnum count))
517 (t (let ((cons (nthcdr n list)))
519 (error "~S is too large an index for SETF of NTH." n))
523 ;;;; :KEY arg optimization to save funcall of IDENTITY
525 ;;; APPLY-KEY saves us a function call sometimes.
526 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
527 ;;; because it's used in seq.lisp and sort.lisp.
528 (defmacro apply-key (key element)
530 (funcall ,key ,element)
533 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
535 ;;; Use these with the following &KEY args:
536 (defmacro with-set-keys (funcall)
538 ,(append funcall '(:key key :test-not test-not))
539 ,(append funcall '(:key key :test test))))
541 (defmacro satisfies-the-test (item elt)
542 (let ((key-tmp (gensym)))
543 `(let ((,key-tmp (apply-key key ,elt)))
544 (cond (testp (funcall test ,item ,key-tmp))
545 (notp (not (funcall test-not ,item ,key-tmp)))
546 (t (funcall test ,item ,key-tmp))))))
548 ;;;; substitution of expressions
550 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
552 "Substitutes new for subtrees matching old."
553 (when (and testp notp)
554 (error ":TEST and :TEST-NOT were both supplied."))
555 (let ((key (and key (%coerce-callable-to-fun key)))
556 (test (if testp (%coerce-callable-to-fun test) test))
557 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
558 (declare (type function test test-not))
559 (labels ((s (subtree)
560 (cond ((satisfies-the-test old subtree) new)
561 ((atom subtree) subtree)
562 (t (let ((car (s (car subtree)))
563 (cdr (s (cdr subtree))))
564 (if (and (eq car (car subtree))
565 (eq cdr (cdr subtree)))
570 (defun subst-if (new test tree &key key)
572 "Substitutes new for subtrees for which test is true."
573 (let ((test (%coerce-callable-to-fun test))
574 (key (and key (%coerce-callable-to-fun key))))
575 (labels ((s (subtree)
576 (cond ((funcall test (apply-key key subtree)) new)
577 ((atom subtree) subtree)
578 (t (let ((car (s (car subtree)))
579 (cdr (s (cdr subtree))))
580 (if (and (eq car (car subtree))
581 (eq cdr (cdr subtree)))
586 (defun subst-if-not (new test tree &key key)
588 "Substitutes new for subtrees for which test is false."
589 (let ((test (%coerce-callable-to-fun test))
590 (key (and key (%coerce-callable-to-fun key))))
591 (labels ((s (subtree)
592 (cond ((not (funcall test (apply-key key subtree))) new)
593 ((atom subtree) subtree)
594 (t (let ((car (s (car subtree)))
595 (cdr (s (cdr subtree))))
596 (if (and (eq car (car subtree))
597 (eq cdr (cdr subtree)))
602 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
604 "Substitute NEW for subtrees matching OLD."
605 (when (and testp notp)
606 (error ":TEST and :TEST-NOT were both supplied."))
607 (let ((key (and key (%coerce-callable-to-fun key)))
608 (test (if testp (%coerce-callable-to-fun test) test))
609 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
610 (declare (type function test test-not))
611 (labels ((s (subtree)
612 (cond ((satisfies-the-test old subtree) new)
613 ((atom subtree) subtree)
614 (t (do* ((last nil subtree)
615 (subtree subtree (cdr subtree)))
617 (if (satisfies-the-test old subtree)
618 (setf (cdr last) new)))
619 (if (satisfies-the-test old subtree)
620 (return (setf (cdr last) new))
621 (setf (car subtree) (s (car subtree)))))
625 (defun nsubst-if (new test tree &key key)
627 "Substitute NEW for subtrees of TREE for which TEST is true."
628 (let ((test (%coerce-callable-to-fun test))
629 (key (and key (%coerce-callable-to-fun key))))
630 (labels ((s (subtree)
631 (cond ((funcall test (apply-key key subtree)) new)
632 ((atom subtree) subtree)
633 (t (do* ((last nil subtree)
634 (subtree subtree (cdr subtree)))
636 (if (funcall test (apply-key key subtree))
637 (setf (cdr last) new)))
638 (if (funcall test (apply-key key subtree))
639 (return (setf (cdr last) new))
640 (setf (car subtree) (s (car subtree)))))
644 (defun nsubst-if-not (new test tree &key key)
646 "Substitute NEW for subtrees of TREE for which TEST is false."
647 (let ((test (%coerce-callable-to-fun test))
648 (key (and key (%coerce-callable-to-fun key))))
649 (labels ((s (subtree)
650 (cond ((not (funcall test (apply-key key subtree))) new)
651 ((atom subtree) subtree)
652 (t (do* ((last nil subtree)
653 (subtree subtree (cdr subtree)))
655 (if (not (funcall test (apply-key key subtree)))
656 (setf (cdr last) new)))
657 (if (not (funcall test (apply-key key subtree)))
658 (return (setf (cdr last) new))
659 (setf (car subtree) (s (car subtree)))))
663 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
665 "Substitute from ALIST into TREE nondestructively."
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 (declare (inline assoc))
673 (labels ((s (subtree)
674 (let* ((key-val (apply-key key subtree))
676 (assoc key-val alist :test-not test-not)
677 (assoc key-val alist :test test))))
678 (cond (assoc (cdr assoc))
679 ((atom subtree) subtree)
680 (t (let ((car (s (car subtree)))
681 (cdr (s (cdr subtree))))
682 (if (and (eq car (car subtree))
683 (eq cdr (cdr subtree)))
685 (cons car cdr))))))))
688 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
689 ;;; because it can be referenced in inline expansions.
690 (defmacro nsublis-macro ()
691 (let ((key-tmp (gensym)))
692 `(let ((,key-tmp (apply-key key subtree)))
694 (assoc ,key-tmp alist :test-not test-not)
695 (assoc ,key-tmp alist :test test)))))
697 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
699 "Substitute from ALIST into TRUE destructively."
700 (when (and testp notp)
701 (error ":TEST and :TEST-NOT were both supplied."))
702 (let ((key (and key (%coerce-callable-to-fun key)))
703 (test (if testp (%coerce-callable-to-fun test) test))
704 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
705 (declare (inline assoc))
707 (labels ((s (subtree)
708 (cond ((setq temp (nsublis-macro))
710 ((atom subtree) subtree)
711 (t (do* ((last nil subtree)
712 (subtree subtree (cdr subtree)))
714 (if (setq temp (nsublis-macro))
715 (setf (cdr last) (cdr temp))))
716 (if (setq temp (nsublis-macro))
717 (return (setf (cdr last) (cdr temp)))
718 (setf (car subtree) (s (car subtree)))))
722 ;;;; functions for using lists as sets
724 (defun member (item list &key key (test #'eql testp) (test-not #'eql notp))
726 "Return the tail of LIST beginning with first element satisfying EQLity,
727 :TEST, or :TEST-NOT with the given ITEM."
728 (when (and testp notp)
729 (error ":TEST and :TEST-NOT were both supplied."))
730 (let ((key (and key (%coerce-callable-to-fun key)))
731 (test (if testp (%coerce-callable-to-fun test) test))
732 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
733 (declare (type function test test-not))
734 (do ((list list (cdr list)))
736 (let ((car (car list)))
737 (when (satisfies-the-test item car)
740 (defun member-if (test list &key key)
742 "Return tail of LIST beginning with first element satisfying TEST."
743 (let ((test (%coerce-callable-to-fun test))
744 (key (and key (%coerce-callable-to-fun key))))
745 (do ((list list (cdr list)))
747 (if (funcall test (apply-key key (car list)))
750 (defun member-if-not (test list &key key)
752 "Return tail of LIST beginning with first element not satisfying TEST."
753 (let ((test (%coerce-callable-to-fun test))
754 (key (and key (%coerce-callable-to-fun key))))
755 (do ((list list (cdr list)))
757 (if (not (funcall test (apply-key key (car list))))
760 (defun tailp (object list)
762 "Return true if OBJECT is the same as some tail of LIST, otherwise
763 returns false. LIST must be a proper list or a dotted list."
764 (do ((list list (cdr list)))
765 ((atom list) (eql list object))
766 (if (eql object list)
769 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
771 "Add ITEM to LIST unless it is already a member"
772 (when (and testp notp)
773 (error ":TEST and :TEST-NOT were both supplied."))
774 (let ((key (and key (%coerce-callable-to-fun key))))
775 (declare (inline member))
776 (if (let ((key-val (apply-key key item)))
778 (member key-val list :test-not test-not :key key)
779 (member key-val list :test test :key key)))
783 (defconstant +list-based-union-limit+ 80)
785 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
787 "Return the union of LIST1 and LIST2."
788 (declare (inline member))
789 (when (and testp notp)
790 (error ":TEST and :TEST-NOT were both supplied."))
791 ;; We have to possibilities here: for shortish lists we pick up the
792 ;; shorter one as the result, and add the other one to it. For long
793 ;; lists we use a hash-table when possible.
794 (let ((n1 (length list1))
796 (key (and key (%coerce-callable-to-fun key)))
798 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
799 (lambda (x) (not (funcall test-not-fun x))))
800 (%coerce-callable-to-fun test))))
801 (multiple-value-bind (short long n-short)
803 (values list1 list2 n1)
804 (values list2 list1 n2))
805 (if (or (< n-short +list-based-union-limit+)
806 (not (member test (list #'eq #'eql #'equal #'equalp))))
809 (unless (member (apply-key key elt) orig :key key :test test)
812 (let ((table (make-hash-table :test test :size (+ n1 n2)))
815 (setf (gethash (apply-key key elt) table) elt))
817 (setf (gethash (apply-key key elt) table) elt))
818 (maphash (lambda (k v)
824 ;;; Destination and source are SETF-able and many-evaluable. Set the
825 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
827 ;;; FIXME: needs a more mnemonic name
828 (defmacro steve-splice (source destination)
829 `(let ((temp ,source))
830 (setf ,source (cdr ,source)
831 (cdr temp) ,destination
834 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
836 "Destructively return the union of LIST1 and LIST2."
837 (declare (inline member))
838 (when (and testp notp)
839 (error ":TEST and :TEST-NOT were both supplied."))
840 ;; We have to possibilities here: for shortish lists we pick up the
841 ;; shorter one as the result, and add the other one to it. For long
842 ;; lists we use a hash-table when possible.
843 (let ((n1 (length list1))
845 (key (and key (%coerce-callable-to-fun key)))
847 (let ((test-not-fun (%coerce-callable-to-fun test-not)))
848 (lambda (x) (not (funcall test-not-fun x))))
849 (%coerce-callable-to-fun test))))
850 (multiple-value-bind (short long n-short)
852 (values list1 list2 n1)
853 (values list2 list1 n2))
854 (if (or (< n-short +list-based-union-limit+)
855 (not (member test (list #'eq #'eql #'equal #'equalp))))
857 (do ((elt (car long) (car long)))
859 (if (not (member (apply-key key elt) orig :key key :test test))
860 (steve-splice long short)
861 (setf long (cdr long))))
863 (let ((table (make-hash-table :test test :size (+ n1 n2))))
865 (setf (gethash (apply-key key elt) table) elt))
867 (setf (gethash (apply-key key elt) table) elt))
870 (maphash (lambda (k v)
879 (defun intersection (list1 list2
880 &key key (test #'eql testp) (test-not nil notp))
882 "Return the intersection of LIST1 and LIST2."
883 (declare (inline member))
884 (when (and testp notp)
885 (error ":TEST and :TEST-NOT were both supplied."))
886 (let ((key (and key (%coerce-callable-to-fun key))))
889 (if (with-set-keys (member (apply-key key elt) list2))
893 (defun nintersection (list1 list2
894 &key key (test #'eql testp) (test-not nil notp))
896 "Destructively return the intersection of LIST1 and LIST2."
897 (declare (inline member))
898 (when (and testp notp)
899 (error ":TEST and :TEST-NOT were both supplied."))
900 (let ((key (and key (%coerce-callable-to-fun key))))
903 (do () ((endp list1))
904 (if (with-set-keys (member (apply-key key (car list1)) list2))
905 (steve-splice list1 res)
906 (setq list1 (cdr list1))))
909 (defun set-difference (list1 list2
910 &key key (test #'eql testp) (test-not nil notp))
912 "Return the elements of LIST1 which are not in LIST2."
913 (declare (inline member))
914 (when (and testp notp)
915 (error ":TEST and :TEST-NOT were both supplied."))
916 (let ((key (and key (%coerce-callable-to-fun key))))
921 (if (not (with-set-keys (member (apply-key key elt) list2)))
925 (defun nset-difference (list1 list2
926 &key key (test #'eql testp) (test-not nil notp))
928 "Destructively return the elements of LIST1 which are not in LIST2."
929 (declare (inline member))
930 (when (and testp notp)
931 (error ":TEST and :TEST-NOT were both supplied."))
932 (let ((key (and key (%coerce-callable-to-fun key))))
935 (do () ((endp list1))
936 (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
937 (steve-splice list1 res)
938 (setq list1 (cdr list1))))
941 (defun set-exclusive-or (list1 list2
942 &key key (test #'eql testp) (test-not #'eql notp))
944 "Return new list of elements appearing exactly once in LIST1 and LIST2."
945 (declare (inline member))
946 (when (and testp notp)
947 (error ":TEST and :TEST-NOT were both supplied."))
949 (key (and key (%coerce-callable-to-fun key)))
950 (test (if testp (%coerce-callable-to-fun test) test))
951 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
952 (declare (type function test test-not))
954 (unless (with-set-keys (member (apply-key key elt) list2))
955 (setq result (cons elt result))))
956 (let ((test (if testp
957 (lambda (x y) (funcall test y x))
960 (lambda (x y) (funcall test-not y x))
963 (unless (with-set-keys (member (apply-key key elt) list1))
964 (setq result (cons elt result)))))
967 (defun nset-exclusive-or (list1 list2
968 &key key (test #'eql testp) (test-not #'eql notp))
970 "Destructively return a list with elements which appear but once in LIST1
972 (when (and testp notp)
973 (error ":TEST and :TEST-NOT were both supplied."))
974 (let ((key (and key (%coerce-callable-to-fun key)))
975 (test (if testp (%coerce-callable-to-fun test) test))
976 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
977 (declare (type function test test-not))
978 ;; The outer loop examines LIST1 while the inner loop examines
979 ;; LIST2. If an element is found in LIST2 "equal" to the element
980 ;; in LIST1, both are spliced out. When the end of LIST1 is
981 ;; reached, what is left of LIST2 is tacked onto what is left of
982 ;; LIST1. The splicing operation ensures that the correct
983 ;; operation is performed depending on whether splice is at the
984 ;; top of the list or not.
990 ;; elements of LIST2, which are "equal" to some processed
991 ;; earlier elements of LIST1
996 (rplacd splicex list2))
998 (let ((key-val-x (apply-key key (car x)))
999 (found-duplicate nil))
1001 ;; Move all elements from LIST2, which are "equal" to (CAR X),
1003 (do* ((y list2 next-y)
1004 (next-y (cdr y) (cdr y))
1007 (cond ((let ((key-val-y (apply-key key (car y))))
1009 (not (funcall test-not key-val-x key-val-y))
1010 (funcall test key-val-x key-val-y)))
1012 (setq list2 (cdr y))
1013 (rplacd splicey (cdr y)))
1014 (setq deleted-y (rplacd y deleted-y))
1015 (setq found-duplicate t))
1016 (t (setq splicey y))))
1018 (unless found-duplicate
1019 (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1023 (setq list1 (cdr x))
1024 (rplacd splicex (cdr x)))
1025 (setq splicex x))))))
1027 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1029 "Return T if every element in LIST1 is also in LIST2."
1030 (declare (inline member))
1031 (when (and testp notp)
1032 (error ":TEST and :TEST-NOT were both supplied."))
1033 (let ((key (and key (%coerce-callable-to-fun key))))
1035 (unless (with-set-keys (member (apply-key key elt) list2))
1036 (return-from subsetp nil)))
1039 ;;;; functions that operate on association lists
1041 (defun acons (key datum alist)
1043 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1044 (cons (cons key datum) alist))
1046 (defun pairlis (keys data &optional (alist '()))
1048 "Construct an association list from KEYS and DATA (adding to ALIST)."
1049 (do ((x keys (cdr x))
1051 ((and (endp x) (endp y)) alist)
1052 (if (or (endp x) (endp y))
1053 (error "The lists of keys and data are of unequal length."))
1054 (setq alist (acons (car x) (car y) alist))))
1056 ;;; This is defined in the run-time environment, not just the compile-time
1057 ;;; environment (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL)) because it
1058 ;;; can appear in inline expansions.
1059 (defmacro assoc-guts (test-expr)
1060 `(do ((alist alist (cdr alist)))
1062 (when (and (car alist) ,test-expr)
1063 (return (car alist)))))
1065 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1067 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1069 (when (and testp notp)
1070 (error ":TEST and :TEST-NOT were both supplied."))
1071 (let ((key (and key (%coerce-callable-to-fun key)))
1072 (test (and testp (%coerce-callable-to-fun test)))
1073 (test-not (and notp (%coerce-callable-to-fun test-not))))
1076 (assoc-guts (funcall test item (funcall key (caar alist))))
1077 (assoc-guts (funcall test item (caar alist)))))
1080 (assoc-guts (not (funcall test-not item
1081 (funcall key (caar alist)))))
1082 (assoc-guts (not (funcall test-not item (caar alist))))))
1085 (assoc-guts (eql item (funcall key (caar alist))))
1086 (assoc-guts (eql item (caar alist))))))))
1088 (defun assoc-if (predicate alist &key key)
1090 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1091 KEY is supplied, apply it to the CAR of each cons before testing."
1092 (let ((predicate (%coerce-callable-to-fun predicate))
1093 (key (and key (%coerce-callable-to-fun key))))
1095 (assoc-guts (funcall predicate (funcall key (caar alist))))
1096 (assoc-guts (funcall predicate (caar alist))))))
1098 (defun assoc-if-not (predicate alist &key key)
1100 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1101 If KEY is supplied, apply it to the CAR of each cons before testing."
1102 (let ((predicate (%coerce-callable-to-fun predicate))
1103 (key (and key (%coerce-callable-to-fun key))))
1105 (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
1106 (assoc-guts (not (funcall predicate (caar alist)))))))
1108 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1109 (declare (list alist))
1111 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1113 (when (and testp notp)
1114 (error ":TEST and :TEST-NOT were both supplied."))
1115 (let ((key (and key (%coerce-callable-to-fun key)))
1116 (test (and testp (%coerce-callable-to-fun test)))
1117 (test-not (and notp (%coerce-callable-to-fun test-not))))
1120 (assoc-guts (funcall test item (funcall key (cdar alist))))
1121 (assoc-guts (funcall test item (cdar alist)))))
1124 (assoc-guts (not (funcall test-not item
1125 (funcall key (cdar alist)))))
1126 (assoc-guts (not (funcall test-not item (cdar alist))))))
1129 (assoc-guts (eql item (funcall key (cdar alist))))
1130 (assoc-guts (eql item (cdar alist))))))))
1132 (defun rassoc-if (predicate alist &key key)
1134 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1135 is supplied, apply it to the CDR of each cons before testing."
1136 (let ((predicate (%coerce-callable-to-fun predicate))
1137 (key (and key (%coerce-callable-to-fun key))))
1139 (assoc-guts (funcall predicate (funcall key (cdar alist))))
1140 (assoc-guts (funcall predicate (cdar alist))))))
1142 (defun rassoc-if-not (predicate alist &key key)
1144 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1145 If KEY is supplied, apply it to the CDR of each cons before testing."
1146 (let ((predicate (%coerce-callable-to-fun predicate))
1147 (key (and key (%coerce-callable-to-fun key))))
1149 (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
1150 (assoc-guts (not (funcall predicate (cdar alist)))))))
1152 ;;;; mapping functions
1154 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1155 ;;; MAPL, MAPLIST, and MAPCON
1157 ;;; Map the designated function over the arglists in the appropriate
1158 ;;; way. It is done when any of the arglists runs out. Until then, it
1159 ;;; CDRs down the arglists calling the function and accumulating
1160 ;;; results as desired.
1161 (defun map1 (fun-designator original-arglists accumulate take-car)
1162 (let ((fun (%coerce-callable-to-fun fun-designator)))
1163 (let* ((arglists (copy-list original-arglists))
1164 (ret-list (list nil))
1168 ((dolist (x arglists nil) (if (null x) (return t)))
1171 (car original-arglists)))
1172 (do ((l arglists (cdr l)))
1174 (push (if take-car (caar l) (car l)) args)
1175 (setf (car l) (cdar l)))
1176 (setq res (apply fun (nreverse args)))
1178 (:nconc (setq temp (last (nconc temp res))))
1179 (:list (rplacd temp (list res))
1180 (setq temp (cdr temp))))))))
1182 (defun mapc (function list &rest more-lists)
1184 "Apply FUNCTION to successive elements of lists. Return the second argument."
1185 (map1 function (cons list more-lists) nil t))
1187 (defun mapcar (function list &rest more-lists)
1189 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1191 (map1 function (cons list more-lists) :list t))
1193 (defun mapcan (function list &rest more-lists)
1195 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1197 (map1 function (cons list more-lists) :nconc t))
1199 (defun mapl (function list &rest more-lists)
1201 "Apply FUNCTION to successive CDRs of list. Return NIL."
1202 (map1 function (cons list more-lists) nil nil))
1204 (defun maplist (function list &rest more-lists)
1206 "Apply FUNCTION to successive CDRs of list. Return list of results."
1207 (map1 function (cons list more-lists) :list nil))
1209 (defun mapcon (function list &rest more-lists)
1211 "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1212 (map1 function (cons list more-lists) :nconc nil))
1214 ;;;; Specialized versions
1216 ;;; %MEMBER-* and %ASSOC-* function. The transforms for %MEMBER and %ASSOC pick
1217 ;;; the appropriate version. These win because they have only positional arguments,
1218 ;;; the TEST & KEY functions are known to exist (or not), and are known to be
1219 ;;; functions, not function designators.
1220 (macrolet ((def (funs form)
1222 `(defun ,(intern (format nil "%~A~{-~A~}" name funs))
1224 ,@(when funs `((declare (function ,@funs))))
1225 (do ((list list (cdr list)))
1227 (let ((this (car list)))
1231 (let ((target (car this)))
1232 (when (and this ,form)
1235 `(let ((target this))
1237 (return list))))))))))
1244 (eql item (funcall key target)))
1246 (funcall test item (funcall key target)))
1248 (not (funcall test-not item (funcall key target))))
1250 (funcall test item target))
1252 (not (funcall test-not item target))))