1.0.21.31: fix bad PROGV and RESTRICT-COMPILER-POLICY interaction
[sbcl/tcr.git] / src / code / list.lisp
blob39af79ec1333c0b1f5dd186a9e6b1ceabdd8a9fb
1 ;;;; functions to implement lists
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 ;;; Limitation: no list might have more than INDEX conses.
16 ;;;; KLUDGE: comment from CMU CL, what does it mean?
17 ;;;; NSUBLIS, things at the beginning broken.
18 ;;;; -- WHN 20000127
20 (declaim (maybe-inline
21 tree-equal nth %setnth nthcdr make-list
22 tailp union
23 nunion intersection nintersection set-difference nset-difference
24 set-exclusive-or nset-exclusive-or subsetp acons
25 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))
30 (defun cdr (list)
31 #!+sb-doc "Return all but the first object in a list."
32 (cdr 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))
36 (defun cddr (list)
37 #!+sb-doc "Return all but the 1st two objects of a list."
38 (cddr list))
39 (defun caddr (list)
40 #!+sb-doc "Return the 1st object in the cddr of a list."
41 (caddr list))
42 (defun caadr (list)
43 #!+sb-doc "Return the 1st object in the cadr of a list."
44 (caadr list))
45 (defun caaar (list)
46 #!+sb-doc "Return the 1st object in the caar of a list."
47 (caaar list))
48 (defun cdaar (list)
49 #!+sb-doc "Return the cdr of the caar of a list."
50 (cdaar list))
51 (defun cddar (list)
52 #!+sb-doc "Return the cdr of the cdar of a list."
53 (cddar list))
54 (defun cdddr (list)
55 #!+sb-doc "Return the cdr of the cddr of a list."
56 (cdddr list))
57 (defun cadar (list)
58 #!+sb-doc "Return the car of the cdar of a list."
59 (cadar list))
60 (defun cdadr (list)
61 #!+sb-doc "Return the cdr of the cadr of a list."
62 (cdadr list))
63 (defun caaaar (list)
64 #!+sb-doc "Return the car of the caaar of a list."
65 (caaaar list))
66 (defun caaadr (list)
67 #!+sb-doc "Return the car of the caadr of a list."
68 (caaadr list))
69 (defun caaddr (list)
70 #!+sb-doc "Return the car of the caddr of a list."
71 (caaddr list))
72 (defun cadddr (list)
73 #!+sb-doc "Return the car of the cdddr of a list."
74 (cadddr list))
75 (defun cddddr (list)
76 #!+sb-doc "Return the cdr of the cdddr of a list."
77 (cddddr list))
78 (defun cdaaar (list)
79 #!+sb-doc "Return the cdr of the caaar of a list."
80 (cdaaar list))
81 (defun cddaar (list)
82 #!+sb-doc "Return the cdr of the cdaar of a list."
83 (cddaar list))
84 (defun cdddar (list)
85 #!+sb-doc "Return the cdr of the cddar of a list."
86 (cdddar list))
87 (defun caadar (list)
88 #!+sb-doc "Return the car of the cadar of a list."
89 (caadar list))
90 (defun cadaar (list)
91 #!+sb-doc "Return the car of the cdaar of a list."
92 (cadaar list))
93 (defun cadadr (list)
94 #!+sb-doc "Return the car of the cdadr of a list."
95 (cadadr list))
96 (defun caddar (list)
97 #!+sb-doc "Return the car of the cddar of a list."
98 (caddar list))
99 (defun cdaadr (list)
100 #!+sb-doc "Return the cdr of the caadr of a list."
101 (cdaadr list))
102 (defun cdadar (list)
103 #!+sb-doc "Return the cdr of the cadar of a list."
104 (cdadar list))
105 (defun cdaddr (list)
106 #!+sb-doc "Return the cdr of the caddr of a list."
107 (cdaddr list))
108 (defun cddadr (list)
109 #!+sb-doc "Return the cdr of the cdadr of a list."
110 (cddadr list))
111 (defun cons (se1 se2)
112 #!+sb-doc "Return a list with SE1 as the CAR and SE2 as the CDR."
113 (cons se1 se2))
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))
119 (cond ((consp x)
120 (and (consp y)
121 (tree-equal-test-not (car x) (car y) test-not)
122 (tree-equal-test-not (cdr x) (cdr y) test-not)))
123 ((consp y) nil)
124 ((not (funcall test-not x y)) t)
125 (t ())))
127 (defun tree-equal-test (x y test)
128 (declare (type function test))
129 (cond ((consp x)
130 (and (consp y)
131 (tree-equal-test (car x) (car y) test)
132 (tree-equal-test (cdr x) (cdr y) test)))
133 ((consp y) nil)
134 ((funcall test x y) t)
135 (t ())))
137 (defun tree-equal (x y &key (test #'eql testp) (test-not nil notp))
138 #!+sb-doc
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."))
142 (if test-not
143 (tree-equal-test-not x y (%coerce-callable-to-fun test-not))
144 (tree-equal-test x y (%coerce-callable-to-fun test))))
146 (defun endp (object)
147 #!+sb-doc
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."
151 (endp object))
153 (defun list-length (list)
154 #!+sb-doc
155 "Return the length of the given List, or Nil if the List is circular."
156 (do ((n 0 (+ n 2))
157 (y list (cddr y))
158 (z list (cdr z)))
159 (())
160 (declare (type fixnum n)
161 (type list y z))
162 (when (endp y) (return n))
163 (when (endp (cdr y)) (return (+ n 1)))
164 (when (and (eq y z) (> n 0)) (return nil))))
166 (defun nth (n list)
167 #!+sb-doc
168 "Return the nth object in a list where the car is the zero-th element."
169 (car (nthcdr n list)))
171 (defun first (list)
172 #!+sb-doc
173 "Return the 1st object in a list or NIL if the list is empty."
174 (car list))
175 (defun second (list)
176 "Return the 2nd object in a list or NIL if there is no 2nd object."
177 (cadr list))
178 (defun third (list)
179 #!+sb-doc
180 "Return the 3rd object in a list or NIL if there is no 3rd object."
181 (caddr list))
182 (defun fourth (list)
183 #!+sb-doc
184 "Return the 4th object in a list or NIL if there is no 4th object."
185 (cadddr list))
186 (defun fifth (list)
187 #!+sb-doc
188 "Return the 5th object in a list or NIL if there is no 5th object."
189 (car (cddddr list)))
190 (defun sixth (list)
191 #!+sb-doc
192 "Return the 6th object in a list or NIL if there is no 6th object."
193 (cadr (cddddr list)))
194 (defun seventh (list)
195 #!+sb-doc
196 "Return the 7th object in a list or NIL if there is no 7th object."
197 (caddr (cddddr list)))
198 (defun eighth (list)
199 #!+sb-doc
200 "Return the 8th object in a list or NIL if there is no 8th object."
201 (cadddr (cddddr list)))
202 (defun ninth (list)
203 #!+sb-doc
204 "Return the 9th object in a list or NIL if there is no 9th object."
205 (car (cddddr (cddddr list))))
206 (defun tenth (list)
207 #!+sb-doc
208 "Return the 10th object in a list or NIL if there is no 10th object."
209 (cadr (cddddr (cddddr list))))
210 (defun rest (list)
211 #!+sb-doc
212 "Means the same as the cdr of a list."
213 (cdr list))
215 (defun nthcdr (n list)
216 #!+sb-doc
217 "Performs the cdr function n times on a list."
218 (flet ((fast-nthcdr (n list)
219 (declare (type index n))
220 (do ((i n (1- i))
221 (result list (cdr result)))
222 ((not (plusp i)) result)
223 (declare (type index i)))))
224 (typecase n
225 (index (fast-nthcdr n list))
226 (t (do ((i 0 (1+ i))
227 (r-i list (cdr r-i))
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)))))))
233 ;;; LAST
235 ;;; Transforms in src/compiler/srctran.lisp pick the most specific
236 ;;; version possible. %LAST/BIGNUM is admittedly somewhat academic...
237 (macrolet ((last0-macro ()
238 `(let ((rest list)
239 (list list))
240 (loop (unless (consp rest)
241 (return rest))
242 (shiftf list rest (cdr rest)))))
243 (last1-macro ()
244 `(let ((rest list)
245 (list list))
246 (loop (unless (consp rest)
247 (return list))
248 (shiftf list rest (cdr rest)))))
249 (lastn-macro (type)
250 `(let ((returned-list list)
251 (checked-list list)
252 (n (truly-the ,type n)))
253 (declare (,type n))
254 (tagbody
255 :scan
256 (pop checked-list)
257 (when (atom checked-list)
258 (go :done))
259 (if (zerop (truly-the ,type (decf n)))
260 (go :pop)
261 (go :scan))
262 :pop
263 (pop returned-list)
264 (pop checked-list)
265 (if (atom checked-list)
266 (go :done)
267 (go :pop))
268 :done)
269 returned-list)))
271 (defun %last0 (list)
272 (declare (optimize speed (sb!c::verify-arg-count 0)))
273 (last0-macro))
275 (defun %last1 (list)
276 (declare (optimize speed (sb!c::verify-arg-count 0)))
277 (last1-macro))
279 (defun %lastn/fixnum (list n)
280 (declare (optimize speed (sb!c::verify-arg-count 0))
281 (type (and unsigned-byte fixnum) n))
282 (case n
283 (1 (last1-macro))
284 (0 (last0-macro))
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))
293 #!+sb-doc
294 "Return the last N conses (not the last element!) of a list."
295 (case n
296 (1 (last1-macro))
297 (0 (last0-macro))
299 (typecase n
300 (fixnum
301 (lastn-macro fixnum))
302 (bignum
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)
308 (0 `(%last0 ,list))
309 (1 `(%last1 ,list))
310 (t form))
311 form))
313 (defun list (&rest args)
314 #!+sb-doc
315 "Return constructs and returns a list of its arguments."
316 args)
318 ;;; LIST* is done the same as LIST, except that the last cons is made
319 ;;; a dotted pair.
321 (defun list* (arg &rest others)
322 #!+sb-doc
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))))
330 (cons arg others))))
332 (defun make-list (size &key initial-element)
333 #!+sb-doc
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)
342 #!+sb-doc
343 "Construct a new list by concatenating the list arguments"
344 (declare (truly-dynamic-extent lists) (optimize speed))
345 (labels ((fail (object)
346 (error 'type-error
347 :datum 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))
352 (if (listp current)
353 (if (consp current)
354 ;; normal case, cdr down the list
355 (append-into (setf (cdr last-cons) (list (car current)))
356 (cdr current)
357 rest)
358 ;; empty list
359 (let ((more (cdr rest)))
360 (if (null more)
361 (setf (cdr last-cons) (car rest))
362 (append-into last-cons (car rest) more))))
363 (fail current)))
364 (append1 (lists)
365 (let ((current (car lists))
366 (rest (cdr lists)))
367 (cond ((null rest)
368 current)
369 ((consp current)
370 (let ((result (truly-the cons (list (car current)))))
371 (append-into result
372 (cdr current)
373 rest)
374 result))
375 ((null current)
376 (append1 rest))
378 (fail current))))))
379 (append1 lists)))
381 (defun append2 (x y)
382 (declare (optimize speed (sb!c::verify-arg-count 0)))
383 (if (null x)
385 (let ((result (list (car x))))
386 (do ((more (cdr x) (cdr more))
387 (tail result (cdr tail)))
388 ((null more)
389 (rplacd tail y)
390 result)
391 (rplacd tail (list (car more)))))))
393 (define-compiler-macro append (&whole form &rest lists)
394 (case (length lists)
395 (0 nil)
396 (1 (car lists))
397 (2 `(append2 ,@lists))
398 (t form)))
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.
407 `(when ,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
412 '((endp orig))
413 '((atom orig)
414 (unless (null orig)
415 (rplacd splice orig))))
416 copy))))))
418 (defun copy-list (list)
419 #!+sb-doc
420 "Return a new list which is EQUAL to LIST. LIST may be improper."
421 (!copy-list-macro list))
423 (defun copy-alist (alist)
424 #!+sb-doc
425 "Return a new association list which is EQUAL to ALIST."
426 (if (endp alist)
427 alist
428 (let ((result
429 (cons (if (atom (car alist))
430 (car alist)
431 (cons (caar alist) (cdar alist)))
432 nil)))
433 (do ((x (cdr alist) (cdr x))
434 (splice result
435 (cdr (rplacd splice
436 (cons
437 (if (atom (car x))
438 (car x)
439 (cons (caar x) (cdar x)))
440 nil)))))
441 ((endp x)))
442 result)))
444 (defun copy-tree (object)
445 #!+sb-doc
446 "Recursively copy trees of conses."
447 (if (consp object)
448 (cons (copy-tree (car object)) (copy-tree (cdr object)))
449 object))
451 ;;;; more commonly-used list functions
453 (defun revappend (x y)
454 #!+sb-doc
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)
472 #!+sb-doc
473 "Concatenates the lists given as arguments (by changing them)"
474 (declare (truly-dynamic-extent lists) (optimize speed))
475 (flet ((fail (object)
476 (error 'type-error
477 :datum object
478 :expected-type 'list)))
479 (do ((top lists (cdr top)))
480 ((null top) nil)
481 (let ((top-of-top (car top)))
482 (typecase top-of-top
483 (cons
484 (let* ((result top-of-top)
485 (splice result))
486 (do ((elements (cdr top) (cdr elements)))
487 ((endp elements))
488 (let ((ele (car elements)))
489 (typecase ele
490 (cons (rplacd (last splice) ele)
491 (setf splice ele))
492 (null (rplacd (last splice) nil))
493 (atom (if (cdr elements)
494 (fail ele)
495 (rplacd (last splice) ele))))))
496 (return result)))
497 (null)
498 (atom
499 (if (cdr top)
500 (fail top-of-top)
501 (return top-of-top))))))))
503 (defun nreconc (x y)
504 #!+sb-doc
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.
509 ((atom 2nd) 3rd)
510 (rplacd 2nd 3rd)))
512 (flet (;; Return the number of conses at the head of the
513 ;; possibly-improper list LIST. (Or if LIST is circular, you
514 ;; lose.)
515 (count-conses (list)
516 (do ((in-list list (cdr in-list))
517 (result 0 (1+ result)))
518 ((atom in-list)
519 result)
520 (declare (type index result)))))
521 (declare (ftype (function (t) index) count-conses))
522 (defun butlast (list &optional (n 1))
523 (if (typep n 'index)
524 (let ((n-conses-in-list (count-conses list)))
525 (cond ((zerop n)
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.)
529 (copy-list list))
530 ((>= n n-conses-in-list)
531 nil)
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)))))
540 nil))
541 (defun nbutlast (list &optional (n 1))
542 (cond ((zerop n)
543 list)
544 ((not (typep n 'index))
545 nil)
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))
549 nil)
550 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))
557 (result (list ()))
558 (splice result))
559 ((atom list)
560 (if (eql list object)
561 (cdr result)
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)
570 #!+sb-doc
571 "Change the CAR of CONS to X and return the CONS."
572 (rplaca cons x))
574 (defun rplacd (cons x)
575 #!+sb-doc
576 "Change the CDR of CONS to X and return the CONS."
577 (rplacd cons x))
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)
587 (typecase n
588 (index
589 (do ((count n (1- count))
590 (list list (cdr list)))
591 ((endp list)
592 (error "~S is too large an index for SETF of NTH." n))
593 (declare (type fixnum count))
594 (when (<= count 0)
595 (rplaca list newval)
596 (return newval))))
597 (t (let ((cons (nthcdr n list)))
598 (when (endp cons)
599 (error "~S is too large an index for SETF of NTH." n))
600 (rplaca cons newval)
601 newval))))
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)
609 `(if ,key
610 (funcall ,key ,element)
611 ,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)
617 `(if notp
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))
631 #!+sb-doc
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)))
646 subtree
647 (cons car cdr)))))))
648 (s tree))))
650 (defun subst-if (new test tree &key key)
651 #!+sb-doc
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)))
662 subtree
663 (cons car cdr)))))))
664 (s tree))))
666 (defun subst-if-not (new test tree &key key)
667 #!+sb-doc
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)))
678 subtree
679 (cons car cdr)))))))
680 (s tree))))
682 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
683 #!+sb-doc
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)))
696 ((atom 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)))))
702 subtree))))
703 (s tree))))
705 (defun nsubst-if (new test tree &key key)
706 #!+sb-doc
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)))
715 ((atom 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)))))
721 subtree))))
722 (s tree))))
724 (defun nsubst-if-not (new test tree &key key)
725 #!+sb-doc
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)))
734 ((atom 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)))))
740 subtree))))
741 (s tree))))
743 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
744 #!+sb-doc
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))
755 (assoc (if notp
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)))
764 subtree
765 (cons car cdr))))))))
766 (s tree))))
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)))
773 (if notp
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))
778 #!+sb-doc
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))
786 (let (temp)
787 (labels ((s (subtree)
788 (cond ((setq temp (nsublis-macro))
789 (cdr temp))
790 ((atom subtree) subtree)
791 (t (do* ((last nil subtree)
792 (subtree subtree (cdr subtree)))
793 ((atom 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)))))
799 subtree))))
800 (s tree)))))
802 ;;;; functions for using lists as sets
804 (defun member (item list &key key (test nil testp) (test-not nil notp))
805 #!+sb-doc
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))))
813 (cond (test
814 (if key
815 (%member-key-test item list key test)
816 (%member-test item list test)))
817 (test-not
818 (if key
819 (%member-key-test-not item list key test-not)
820 (%member-test-not item list test-not)))
822 (if key
823 (%member-key item list key)
824 (%member item list))))))
826 (defun member-if (test list &key key)
827 #!+sb-doc
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))))
831 (if key
832 (%member-if-key test list key)
833 (%member-if test list))))
835 (defun member-if-not (test list &key key)
836 #!+sb-doc
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))))
840 (if key
841 (%member-if-not-key test list key)
842 (%member-if-not test list))))
844 (defun tailp (object list)
845 #!+sb-doc
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)
851 (return t))))
853 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
854 #!+sb-doc
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))))
861 (cond (test
862 (if key
863 (%adjoin-key-test item list key test)
864 (%adjoin-test item list test)))
865 (test-not
866 (if key
867 (%adjoin-key-test-not item list key test-not)
868 (%adjoin-test-not item list test-not)))
870 (if key
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))
877 #!+sb-doc
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))
886 (n2 (length list2))
887 (key (and key (%coerce-callable-to-fun key)))
888 (test (if notp
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)
893 (if (< n1 n2)
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))))
898 (let ((orig short))
899 (dolist (elt long)
900 (unless (member (apply-key key elt) orig :key key :test test)
901 (push elt short)))
902 short)
903 (let ((table (make-hash-table :test test :size (+ n1 n2)))
904 (union nil))
905 (dolist (elt long)
906 (setf (gethash (apply-key key elt) table) elt))
907 (dolist (elt short)
908 (setf (gethash (apply-key key elt) table) elt))
909 (maphash (lambda (k v)
910 (declare (ignore k))
911 (push v union))
912 table)
913 union)))))
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
923 ,destination temp)))
925 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
926 #!+sb-doc
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))
935 (n2 (length list2))
936 (key (and key (%coerce-callable-to-fun key)))
937 (test (if notp
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)
942 (if (< n1 n2)
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))))
947 (let ((orig short))
948 (do ((elt (car long) (car long)))
949 ((endp long))
950 (if (not (member (apply-key key elt) orig :key key :test test))
951 (steve-splice long short)
952 (setf long (cdr long))))
953 short)
954 (let ((table (make-hash-table :test test :size (+ n1 n2))))
955 (dolist (elt long)
956 (setf (gethash (apply-key key elt) table) elt))
957 (dolist (elt short)
958 (setf (gethash (apply-key key elt) table) elt))
959 (let ((union long)
960 (head long))
961 (maphash (lambda (k v)
962 (declare (ignore k))
963 (if head
964 (setf (car head) v
965 head (cdr head))
966 (push v union)))
967 table)
968 union))))))
970 (defun intersection (list1 list2
971 &key key (test #'eql testp) (test-not nil notp))
972 #!+sb-doc
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))))
978 (let ((res nil))
979 (dolist (elt list1)
980 (if (with-set-keys (member (apply-key key elt) list2))
981 (push elt res)))
982 res)))
984 (defun nintersection (list1 list2
985 &key key (test #'eql testp) (test-not nil notp))
986 #!+sb-doc
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))))
992 (let ((res nil)
993 (list1 list1))
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))))
998 res)))
1000 (defun set-difference (list1 list2
1001 &key key (test #'eql testp) (test-not nil notp))
1002 #!+sb-doc
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))))
1008 (if (null list2)
1009 list1
1010 (let ((res nil))
1011 (dolist (elt list1)
1012 (if (not (with-set-keys (member (apply-key key elt) list2)))
1013 (push elt res)))
1014 res))))
1016 (defun nset-difference (list1 list2
1017 &key key (test #'eql testp) (test-not nil notp))
1018 #!+sb-doc
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))))
1024 (let ((res nil)
1025 (list1 list1))
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))))
1030 res)))
1032 (defun set-exclusive-or (list1 list2
1033 &key key (test #'eql testp) (test-not #'eql notp))
1034 #!+sb-doc
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."))
1039 (let ((result nil)
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))
1044 (dolist (elt list1)
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))
1049 test))
1050 (test-not (if notp
1051 (lambda (x y) (funcall test-not y x))
1052 test-not)))
1053 (dolist (elt list2)
1054 (unless (with-set-keys (member (apply-key key elt) list1))
1055 (setq result (cons elt result)))))
1056 result))
1058 (defun nset-exclusive-or (list1 list2
1059 &key key (test #'eql testp) (test-not #'eql notp))
1060 #!+sb-doc
1061 "Destructively return a list with elements which appear but once in LIST1
1062 and LIST2."
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.
1076 (do ((list1 list1)
1077 (list2 list2)
1078 (x list1 (cdr x))
1079 (splicex ())
1080 (deleted-y ())
1081 ;; elements of LIST2, which are "equal" to some processed
1082 ;; earlier elements of LIST1
1084 ((endp x)
1085 (if (null splicex)
1086 (setq list1 list2)
1087 (rplacd splicex list2))
1088 list1)
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),
1093 ;; to DELETED-Y.
1094 (do* ((y list2 next-y)
1095 (next-y (cdr y) (cdr y))
1096 (splicey ()))
1097 ((endp y))
1098 (cond ((let ((key-val-y (apply-key key (car y))))
1099 (if notp
1100 (not (funcall test-not key-val-x key-val-y))
1101 (funcall test key-val-x key-val-y)))
1102 (if (null splicey)
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))))
1112 (if found-duplicate
1113 (if (null splicex)
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))
1119 #!+sb-doc
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))))
1125 (dolist (elt list1)
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)
1133 #!+sb-doc
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 '()))
1138 #!+sb-doc
1139 "Construct an association list from KEYS and DATA (adding to ALIST)."
1140 (do ((x keys (cdr x))
1141 (y data (cdr y)))
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))
1148 #!+sb-doc
1149 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1150 the ITEM."
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))))
1156 (cond (test
1157 (if key
1158 (%assoc-key-test item alist key test)
1159 (%assoc-test item alist test)))
1160 (test-not
1161 (if key
1162 (%assoc-key-test-not item alist key test-not)
1163 (%assoc-test-not item alist test-not)))
1165 (if key
1166 (%assoc-key item alist key)
1167 (%assoc item alist))))))
1169 (defun assoc-if (predicate alist &key key)
1170 #!+sb-doc
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))))
1175 (if key
1176 (%assoc-if-key predicate alist key)
1177 (%assoc-if predicate alist))))
1179 (defun assoc-if-not (predicate alist &key key)
1180 #!+sb-doc
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))))
1185 (if 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))
1191 #!+sb-doc
1192 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1193 the ITEM."
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))))
1199 (cond (test
1200 (if key
1201 (%rassoc-key-test item alist key test)
1202 (%rassoc-test item alist test)))
1203 (test-not
1204 (if key
1205 (%rassoc-key-test-not item alist key test-not)
1206 (%rassoc-test-not item alist test-not)))
1208 (if key
1209 (%rassoc-key item alist key)
1210 (%rassoc item alist))))))
1212 (defun rassoc-if (predicate alist &key key)
1213 #!+sb-doc
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))))
1218 (if key
1219 (%rassoc-if-key predicate alist key)
1220 (%rassoc-if predicate alist))))
1222 (defun rassoc-if-not (predicate alist &key key)
1223 #!+sb-doc
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))))
1228 (if 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))
1245 (temp ret-list))
1246 (do ((res nil)
1247 (args '() '()))
1248 ((dolist (x arglists nil) (if (null x) (return t)))
1249 (if accumulate
1250 (cdr ret-list)
1251 (car original-arglists)))
1252 (do ((l arglists (cdr l)))
1253 ((null l))
1254 (push (if take-car (caar l) (car l)) args)
1255 (setf (car l) (cdar l)))
1256 (setq res (apply fun (nreverse args)))
1257 (case accumulate
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)
1263 #!+sb-doc
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)
1268 #!+sb-doc
1269 "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1270 return values."
1271 (map1 function (cons list more-lists) :list t))
1273 (defun mapcan (function list &rest more-lists)
1274 #!+sb-doc
1275 "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1276 results."
1277 (map1 function (cons list more-lists) :nconc t))
1279 (defun mapl (function list &rest more-lists)
1280 #!+sb-doc
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)
1285 #!+sb-doc
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)
1290 #!+sb-doc
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.
1303 (macrolet
1304 ((def (funs form &optional variant)
1305 (flet ((%def (name &optional conditional)
1306 (let* ((body-loop
1307 `(do ((list list (cdr list)))
1308 ((null list) nil)
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
1314 (ecase name
1315 ((assoc rassoc)
1316 (if funs
1317 `(when this
1318 (let ((target (,cxx this)))
1319 (when ,form
1320 (return 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)
1326 (return this)))))
1327 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
1328 (aver (equal '(eql x) (subseq form 0 2)))
1329 `(when this
1330 (let ((target (,cxx this)))
1331 (,conditional (funcall ,@(cdr form))
1332 (return this)))))
1333 (member
1334 `(let ((target this))
1335 (when ,form
1336 (return list))))
1337 ((member-if member-if-not)
1338 (aver (equal '(eql x) (subseq form 0 2)))
1339 `(let ((target this))
1340 (,conditional (funcall ,@(cdr form))
1341 (return list))))
1342 (adjoin
1343 `(let ((target this))
1344 (when ,form
1345 (return t)))))))))
1346 (body (if (eq 'adjoin name)
1347 `(if (let ,(when (member 'key funs)
1348 `((x (funcall key x))))
1349 ,body-loop)
1350 list
1351 (cons x list))
1352 body-loop)))
1353 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1354 (x list ,@funs)
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))))
1358 ,body))))
1359 `(progn
1360 ,(%def 'adjoin)
1361 ,(%def 'assoc)
1362 ,(%def 'member)
1363 ,(%def 'rassoc)
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)))))))
1371 (def ()
1372 (eql x target))
1373 (def ()
1374 (eq x target)
1376 (def (key)
1377 (eql x (funcall key target)))
1378 (def (key)
1379 (eq x (funcall key target))
1381 (def (key test)
1382 (funcall test x (funcall key target)))
1383 (def (key test-not)
1384 (not (funcall test-not x (funcall key target))))
1385 (def (test)
1386 (funcall test x target))
1387 (def (test-not)
1388 (not (funcall test-not x target))))