Remove single use function, revise comment, fix inlining failure
[sbcl.git] / src / code / list.lisp
blobc6d693198d56d33723fc9f694aeead8f61ef07e7
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 %setnth nthcdr
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 ;; NSUBLIS is >400 lines of assembly. How is it helpful to inline?
27 subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
29 ;;; These functions perform basic list operations.
30 (defun car (list) "Return the 1st object in a list." (car list))
31 (defun cdr (list)
32 "Return all but the first object in a list."
33 (cdr list))
34 (defun cadr (list) "Return the 2nd object in a list." (cadr list))
35 (defun cdar (list) "Return the cdr of the 1st sublist." (cdar list))
36 (defun caar (list) "Return the car of the 1st sublist." (caar list))
37 (defun cddr (list)
38 "Return all but the 1st two objects of a list."
39 (cddr list))
40 (defun caddr (list)
41 "Return the 1st object in the cddr of a list."
42 (caddr list))
43 (defun caadr (list)
44 "Return the 1st object in the cadr of a list."
45 (caadr list))
46 (defun caaar (list)
47 "Return the 1st object in the caar of a list."
48 (caaar list))
49 (defun cdaar (list)
50 "Return the cdr of the caar of a list."
51 (cdaar list))
52 (defun cddar (list)
53 "Return the cdr of the cdar of a list."
54 (cddar list))
55 (defun cdddr (list)
56 "Return the cdr of the cddr of a list."
57 (cdddr list))
58 (defun cadar (list)
59 "Return the car of the cdar of a list."
60 (cadar list))
61 (defun cdadr (list)
62 "Return the cdr of the cadr of a list."
63 (cdadr list))
64 (defun caaaar (list)
65 "Return the car of the caaar of a list."
66 (caaaar list))
67 (defun caaadr (list)
68 "Return the car of the caadr of a list."
69 (caaadr list))
70 (defun caaddr (list)
71 "Return the car of the caddr of a list."
72 (caaddr list))
73 (defun cadddr (list)
74 "Return the car of the cdddr of a list."
75 (cadddr list))
76 (defun cddddr (list)
77 "Return the cdr of the cdddr of a list."
78 (cddddr list))
79 (defun cdaaar (list)
80 "Return the cdr of the caaar of a list."
81 (cdaaar list))
82 (defun cddaar (list)
83 "Return the cdr of the cdaar of a list."
84 (cddaar list))
85 (defun cdddar (list)
86 "Return the cdr of the cddar of a list."
87 (cdddar list))
88 (defun caadar (list)
89 "Return the car of the cadar of a list."
90 (caadar list))
91 (defun cadaar (list)
92 "Return the car of the cdaar of a list."
93 (cadaar list))
94 (defun cadadr (list)
95 "Return the car of the cdadr of a list."
96 (cadadr list))
97 (defun caddar (list)
98 "Return the car of the cddar of a list."
99 (caddar list))
100 (defun cdaadr (list)
101 "Return the cdr of the caadr of a list."
102 (cdaadr list))
103 (defun cdadar (list)
104 "Return the cdr of the cadar of a list."
105 (cdadar list))
106 (defun cdaddr (list)
107 "Return the cdr of the caddr of a list."
108 (cdaddr list))
109 (defun cddadr (list)
110 "Return the cdr of the cdadr of a list."
111 (cddadr list))
112 (defun cons (se1 se2)
113 "Return a list with SE1 as the CAR and SE2 as the CDR."
114 (cons se1 se2))
116 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
118 (defun tree-equal-test-not (x y test-not)
119 (declare (type function test-not))
120 (cond ((consp x)
121 (and (consp y)
122 (tree-equal-test-not (car x) (car y) test-not)
123 (tree-equal-test-not (cdr x) (cdr y) test-not)))
124 ((consp y) nil)
125 ((not (funcall test-not x y)) t)
126 (t ())))
128 (defun tree-equal-test (x y test)
129 (declare (type function test))
130 (cond ((consp x)
131 (and (consp y)
132 (tree-equal-test (car x) (car y) test)
133 (tree-equal-test (cdr x) (cdr y) test)))
134 ((consp y) nil)
135 ((funcall test x y) t)
136 (t ())))
138 (defun tree-equal-eql (x y)
139 (labels ((recurse (x y)
140 (if (eq x y)
142 (do ((x x (cdr x))
143 (y y (cdr y)))
144 ((or (atom x)
145 (atom y))
146 (eql x y))
147 (cond ((consp (car x))
148 (when (atom (car y))
149 (return-from tree-equal-eql))
150 (recurse (car x) (car y)))
151 ((not (eql (car x) (car y)))
152 (return-from tree-equal-eql)))))))
153 (recurse x y)))
155 (defun tree-equal (x y &key (test nil testp) (test-not nil notp))
156 "Return T if X and Y are isomorphic trees with identical leaves."
157 (declare (explicit-check))
158 (cond (notp
159 (when testp
160 (error ":TEST and :TEST-NOT were both supplied."))
161 (tree-equal-test-not x y (%coerce-callable-to-fun test-not)))
162 ((or (not test)
163 (eql test #'eql)
164 (eql test 'eql))
165 (tree-equal-eql x y))
167 (tree-equal-test x y (%coerce-callable-to-fun test)))))
169 (defun endp (object)
170 "This is the recommended way to test for the end of a proper list. It
171 returns true if OBJECT is NIL, false if OBJECT is a CONS, and an error
172 for any other type of OBJECT."
173 (endp object))
175 (defun list-length (list)
176 "Return the length of the given List, or Nil if the List is circular."
177 (do ((n 0 (+ n 2))
178 (y list (cddr y))
179 (z list (cdr z)))
180 (())
181 (declare (type fixnum n)
182 (type list y z))
183 (when (endp y) (return n))
184 (when (endp (cdr y)) (return (+ n 1)))
185 (when (and (eq y z) (> n 0)) (return nil))))
187 (defun nth (n list)
188 "Return the nth object in a list where the car is the zero-th element."
189 (declare (explicit-check))
190 (car (nthcdr n list)))
192 (defun first (list)
193 "Return the 1st object in a list or NIL if the list is empty."
194 (car list))
195 (defun second (list)
196 "Return the 2nd object in a list or NIL if there is no 2nd object."
197 (cadr list))
198 (defun third (list)
199 "Return the 3rd object in a list or NIL if there is no 3rd object."
200 (caddr list))
201 (defun fourth (list)
202 "Return the 4th object in a list or NIL if there is no 4th object."
203 (cadddr list))
204 (defun fifth (list)
205 "Return the 5th object in a list or NIL if there is no 5th object."
206 (car (cddddr list)))
207 (defun sixth (list)
208 "Return the 6th object in a list or NIL if there is no 6th object."
209 (cadr (cddddr list)))
210 (defun seventh (list)
211 "Return the 7th object in a list or NIL if there is no 7th object."
212 (caddr (cddddr list)))
213 (defun eighth (list)
214 "Return the 8th object in a list or NIL if there is no 8th object."
215 (cadddr (cddddr list)))
216 (defun ninth (list)
217 "Return the 9th object in a list or NIL if there is no 9th object."
218 (car (cddddr (cddddr list))))
219 (defun tenth (list)
220 "Return the 10th object in a list or NIL if there is no 10th object."
221 (cadr (cddddr (cddddr list))))
222 (defun rest (list)
223 "Means the same as the cdr of a list."
224 (cdr list))
226 (defun nthcdr (n list)
227 "Performs the cdr function n times on a list."
228 (flet ((fast-nthcdr (n list)
229 (declare (type index n))
230 (do ((i n (1- i))
231 (result list (cdr result)))
232 ((not (plusp i)) result)
233 (declare (type index i)))))
234 (typecase n
235 (index (fast-nthcdr n list))
236 ;; Such a large list can only be circular
237 (t (do ((i 0 (1+ i))
238 (r-i list (cdr r-i))
239 (r-2i list (cddr r-2i)))
240 ((and (eq r-i r-2i) (not (zerop i)))
241 (fast-nthcdr (mod n i) r-i))
242 (declare (type index i)))))))
244 ;;; For [n]butlast
245 (defun dotted-nthcdr (n list)
246 (flet ((fast-nthcdr (n list)
247 (declare (type index n))
248 (do ((i n (1- i))
249 (result list (cdr result)))
250 ((not (plusp i)) result)
251 (declare (type index i))
252 (when (atom result)
253 (return)))))
254 (typecase n
255 (index (fast-nthcdr n list))
256 ;; Such a large list can only be circular
257 (t (do ((i 0 (1+ i))
258 (r-i list (cdr r-i))
259 (r-2i list (cddr r-2i)))
260 ((and (eq r-i r-2i) (not (zerop i)))
261 (fast-nthcdr (mod n i) r-i))
262 (declare (type index i)))))))
264 ;;; LAST
266 ;;; Transforms in src/compiler/srctran.lisp pick the most specific
267 ;;; version possible. %LAST/BIGNUM is admittedly somewhat academic...
268 (macrolet ((last0-macro ()
269 `(let ((rest list)
270 (list list))
271 (loop (unless (consp rest)
272 (return rest))
273 (shiftf list rest (cdr rest)))))
274 (last1-macro ()
275 `(let ((rest list)
276 (list list))
277 (loop (unless (consp rest)
278 (return list))
279 (shiftf list rest (cdr rest)))))
280 (lastn-macro (type)
281 `(let ((returned-list list)
282 (checked-list list)
283 (n (truly-the ,type n)))
284 (declare (,type n))
285 (tagbody
286 :scan
287 (pop checked-list)
288 (when (atom checked-list)
289 (go :done))
290 (if (zerop (truly-the ,type (decf n)))
291 (go :pop)
292 (go :scan))
293 :pop
294 (pop returned-list)
295 (pop checked-list)
296 (if (atom checked-list)
297 (go :done)
298 (go :pop))
299 :done)
300 returned-list)))
302 (defun %last0 (list)
303 (declare (optimize speed (sb!c::verify-arg-count 0)))
304 (last0-macro))
306 (defun %last1 (list)
307 (declare (optimize speed (sb!c::verify-arg-count 0)))
308 (last1-macro))
310 (defun %lastn/fixnum (list n)
311 (declare (optimize speed (sb!c::verify-arg-count 0))
312 (type (and unsigned-byte fixnum) n))
313 (case n
314 (1 (last1-macro))
315 (0 (last0-macro))
316 (t (lastn-macro fixnum))))
318 (defun %lastn/bignum (list n)
319 (declare (optimize speed (sb!c::verify-arg-count 0))
320 (type (and unsigned-byte bignum) n))
321 (lastn-macro unsigned-byte))
323 (defun last (list &optional (n 1))
324 "Return the last N conses (not the last element!) of a list."
325 (case n
326 (1 (last1-macro))
327 (0 (last0-macro))
329 (typecase n
330 (fixnum
331 (lastn-macro fixnum))
332 (bignum
333 (lastn-macro unsigned-byte)))))))
335 (define-compiler-macro last (&whole form list &optional (n 1) &environment env)
336 (if (sb!xc:constantp n env)
337 (case (constant-form-value n env)
338 (0 `(%last0 ,list))
339 (1 `(%last1 ,list))
340 (t form))
341 form))
343 (defun list (&rest args)
344 "Return constructs and returns a list of its arguments."
345 args)
347 ;;; LIST* is done the same as LIST, except that the last cons is made
348 ;;; a dotted pair.
350 (defun list* (arg &rest others)
351 "Return a list of the arguments with last cons a dotted pair."
352 (let ((length (length others)))
353 (cond ((= length 0) arg)
354 ((= length 1)
355 (cons arg (fast-&rest-nth 0 others)))
357 (let* ((cons (list arg))
358 (result cons)
359 (index 0)
360 (1-length (1- length)))
361 (loop
362 (cond
363 ((< index 1-length)
364 (setf cons
365 (setf (cdr cons)
366 (list (fast-&rest-nth index others))))
367 (incf index))
368 (t (return nil))))
369 (setf (cdr cons) (fast-&rest-nth index others))
370 result)))))
372 (defun make-list (size &key initial-element)
373 "Constructs a list with size elements each set to value"
374 (declare (explicit-check))
375 (%make-list size initial-element))
376 ;;; This entry point is to be preferred, irrespective of
377 ;;; whether or not the backend has vops for %MAKE-LIST.
378 (defun %make-list (size initial-element)
379 (declare (type index size))
380 (do ((count size (1- count))
381 (result '() (cons initial-element result)))
382 ((<= count 0) result)
383 (declare (type index count))))
385 (defun append (&rest lists)
386 "Construct a new list by concatenating the list arguments"
387 (let* ((result (list nil))
388 (tail result)
389 (index 0)
390 (length (length lists))
391 (last (1- length)))
392 (declare (truly-dynamic-extent result))
393 (loop
394 (cond
395 ((< (truly-the index index) last)
396 (let ((list (fast-&rest-nth (truly-the index index) lists)))
397 (dolist (elt list)
398 (setf (cdr (truly-the cons tail)) (list elt)
399 tail (cdr tail))))
400 (incf index))
401 (t (return nil))))
402 (cond
403 ((zerop length) nil)
404 ((null (cdr result))
405 (fast-&rest-nth (truly-the index last) lists))
407 (setf (cdr (truly-the cons tail))
408 (fast-&rest-nth (truly-the index last) lists))
409 (cdr result)))))
411 (defun append2 (x y)
412 (declare (optimize (sb!c::verify-arg-count 0)))
413 (if (null x)
415 (let ((result (list (car x))))
416 (do ((more (cdr x) (cdr more))
417 (tail result (cdr tail)))
418 ((null more)
419 (rplacd (truly-the cons tail) y)
420 result)
421 (rplacd (truly-the cons tail) (list (car more)))))))
424 ;;;; list copying functions
426 (eval-when (:compile-toplevel :load-toplevel :execute)
427 (sb!xc:defmacro !copy-list-macro (list &key check-proper-list)
428 ;; Unless CHECK-PROPER-LIST is true, the list is copied correctly
429 ;; even if the list is not terminated by NIL. The new list is built
430 ;; by CDR'ing SPLICE which is always at the tail of the new list.
431 `(when ,list
432 (let ((copy (list (car ,list))))
433 (do ((orig (cdr ,list) (cdr orig))
434 (splice copy (cdr (rplacd splice (cons (car orig) nil)))))
435 (,@(if check-proper-list
436 '((endp orig))
437 '((atom orig)
438 (unless (null orig)
439 (rplacd splice orig))))
440 copy))))))
442 (defun copy-list (list)
443 "Return a new list which is EQUAL to LIST. LIST may be improper."
444 (!copy-list-macro list))
446 (defun copy-alist (alist)
447 "Return a new association list which is EQUAL to ALIST."
448 (if (endp alist)
449 alist
450 (let ((result
451 (cons (if (atom (car alist))
452 (car alist)
453 (cons (caar alist) (cdar alist)))
454 nil)))
455 (do ((x (cdr alist) (cdr x))
456 (splice result
457 (cdr (rplacd splice
458 (cons
459 (if (atom (car x))
460 (car x)
461 (cons (caar x) (cdar x)))
462 nil)))))
463 ((endp x)))
464 result)))
466 (defun copy-tree (object)
467 "Recursively copy trees of conses."
468 (if (consp object)
469 (let ((result (list (if (consp (car object))
470 (copy-tree (car object))
471 (car object)))))
472 (loop for last-cons = result then new-cons
473 for cdr = (cdr object) then (cdr cdr)
474 for car = (if (consp cdr)
475 (car cdr)
476 (return (setf (cdr last-cons) cdr)))
477 for new-cons = (list (if (consp car)
478 (copy-tree car)
479 car))
480 do (setf (cdr last-cons) new-cons))
481 result)
482 object))
485 ;;;; more commonly-used list functions
487 (defun revappend (x y)
488 "Return (append (reverse x) y)."
489 (do ((top x (cdr top))
490 (result y (cons (car top) result)))
491 ((endp top) result)))
493 ;;; NCONC finds the first non-null list, so it can make splice point
494 ;;; to a cons. After finding the first cons element, it holds it in a
495 ;;; result variable while running down successive elements tacking
496 ;;; them together. While tacking lists together, if we encounter a
497 ;;; null list, we set the previous list's last cdr to nil just in case
498 ;;; it wasn't already nil, and it could have been dotted while the
499 ;;; null list was the last argument to NCONC. The manipulation of
500 ;;; splice (that is starting it out on a first cons, setting LAST of
501 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
502 ;;; and it avoids running down the last argument to NCONC which allows
503 ;;; the last argument to be circular.
504 (defun nconc (&rest lists)
505 "Concatenates the lists given as arguments (by changing them)"
506 (declare (optimize speed))
507 (flet ((fail (object)
508 (error 'type-error
509 :datum object
510 :expected-type 'list)))
511 (do-rest-arg ((result index) lists)
512 (typecase result
513 (cons
514 (let ((splice result))
515 (do-rest-arg ((ele index) lists (1+ index))
516 (typecase ele
517 (cons (rplacd (last splice) ele)
518 (setf splice ele))
519 (null (rplacd (last splice) nil))
520 (atom (if (< (1+ index) (length lists))
521 (fail ele)
522 (rplacd (last splice) ele)))))
523 (return result)))
524 (null)
525 (atom
526 (if (< (1+ index) (length lists))
527 (fail result)
528 (return result)))))))
530 (defun nreconc (x y)
531 "Return (NCONC (NREVERSE X) Y)."
532 (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
533 (2nd x 1st) ;2nd follows first down the list.
534 (3rd y 2nd)) ;3rd follows 2nd down the list.
535 ((atom 2nd) 3rd)
536 (rplacd 2nd 3rd)))
539 (defun butlast (list &optional (n 1))
540 (cond ((zerop n)
541 (copy-list list))
542 ((not (typep n 'index))
543 nil)
545 (let ((head (dotted-nthcdr (1- n) list)))
546 (and (consp head) ; there are at least n
547 (collect ((copy)) ; conses; copy!
548 (do ((trail list (cdr trail))
549 (head head (cdr head)))
550 ;; HEAD is n-1 conses ahead of TRAIL;
551 ;; when HEAD is at the last cons, return
552 ;; the data copied so far.
553 ((atom (cdr head))
554 (copy))
555 (copy (car trail)))))))))
557 (defun nbutlast (list &optional (n 1))
558 (cond ((zerop n)
559 list)
560 ((not (typep n 'index))
561 nil)
563 (let ((head (dotted-nthcdr (1- n) list)))
564 (and (consp head) ; there are more than n
565 (consp (cdr head)) ; conses.
566 ;; TRAIL trails by n cons to be able to
567 ;; cut the list at the cons just before.
568 (do ((trail list (cdr trail))
569 (head (cdr head) (cdr head)))
570 ((atom (cdr head))
571 (setf (cdr trail) nil)
572 list)))))))
574 (defun ldiff (list object)
575 "Return a new list, whose elements are those of LIST that appear before
576 OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
577 LIST must be a proper list or a dotted list."
578 (do* ((list list (cdr list))
579 (result (list ()))
580 (splice result))
581 ((atom list)
582 (if (eql list object)
583 (cdr result)
584 (progn (rplacd splice list) (cdr result))))
585 (if (eql list object)
586 (return (cdr result))
587 (setq splice (cdr (rplacd splice (list (car list))))))))
589 ;;;; functions to alter list structure
591 (defun rplaca (cons x)
592 "Change the CAR of CONS to X and return the CONS."
593 (rplaca cons x))
595 (defun rplacd (cons x)
596 "Change the CDR of CONS to X and return the CONS."
597 (rplacd cons x))
599 ;;; The following are for use by SETF.
601 (defun %rplaca (x val) (rplaca x val) val)
603 (defun %rplacd (x val) (rplacd x val) val)
605 ;;; Set the Nth element of LIST to NEWVAL.
606 (defun %setnth (n list newval)
607 (typecase n
608 (index
609 (do ((count n (1- count))
610 (list list (cdr list)))
611 ((endp list)
612 (error "~S is too large an index for SETF of NTH." n))
613 (declare (type fixnum count))
614 (when (<= count 0)
615 (rplaca list newval)
616 (return newval))))
617 (t (let ((cons (nthcdr n list)))
618 (when (endp cons)
619 (error "~S is too large an index for SETF of NTH." n))
620 (rplaca cons newval)
621 newval))))
623 ;;;; :KEY arg optimization to save funcall of IDENTITY
625 ;;; APPLY-KEY saves us a function call sometimes.
626 ;;; This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
627 ;;; because it's used in seq.lisp and sort.lisp.
628 (defmacro apply-key (key element)
629 `(if ,key
630 (funcall ,key ,element)
631 ,element))
633 (defmacro apply-key-function (key element)
634 `(if ,key
635 (funcall (truly-the function ,key) ,element)
636 ,element))
638 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
640 ;;; Use these with the following &KEY args:
641 (defmacro with-set-keys (funcall)
642 `(if notp
643 ,(append funcall '(:key key :test-not test-not))
644 ,(append funcall '(:key key :test test))))
646 (defmacro satisfies-the-test (item elt)
647 (let ((key-tmp (gensym)))
648 `(let ((,key-tmp (apply-key key ,elt)))
649 (cond (testp (funcall test ,item ,key-tmp))
650 (notp (not (funcall test-not ,item ,key-tmp)))
651 (t (funcall test ,item ,key-tmp))))))
653 ;;;; substitution of expressions
655 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
656 "Substitutes new for subtrees matching old."
657 (when (and testp notp)
658 (error ":TEST and :TEST-NOT were both supplied."))
659 (let ((key (and key (%coerce-callable-to-fun key)))
660 (test (if testp (%coerce-callable-to-fun test) test))
661 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
662 (declare (type function test test-not))
663 (labels ((s (subtree)
664 (cond ((satisfies-the-test old subtree) new)
665 ((atom subtree) subtree)
666 (t (let ((car (s (car subtree)))
667 (cdr (s (cdr subtree))))
668 (if (and (eq car (car subtree))
669 (eq cdr (cdr subtree)))
670 subtree
671 (cons car cdr)))))))
672 (s tree))))
674 (defun subst-if (new test tree &key key)
675 "Substitutes new for subtrees for which test is true."
676 (let ((test (%coerce-callable-to-fun test))
677 (key (and key (%coerce-callable-to-fun key))))
678 (labels ((s (subtree)
679 (cond ((funcall test (apply-key key subtree)) new)
680 ((atom subtree) subtree)
681 (t (let ((car (s (car subtree)))
682 (cdr (s (cdr subtree))))
683 (if (and (eq car (car subtree))
684 (eq cdr (cdr subtree)))
685 subtree
686 (cons car cdr)))))))
687 (s tree))))
689 (defun subst-if-not (new test tree &key key)
690 "Substitutes new for subtrees for which test is false."
691 (let ((test (%coerce-callable-to-fun test))
692 (key (and key (%coerce-callable-to-fun key))))
693 (labels ((s (subtree)
694 (cond ((not (funcall test (apply-key key subtree))) new)
695 ((atom subtree) subtree)
696 (t (let ((car (s (car subtree)))
697 (cdr (s (cdr subtree))))
698 (if (and (eq car (car subtree))
699 (eq cdr (cdr subtree)))
700 subtree
701 (cons car cdr)))))))
702 (s tree))))
704 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
705 "Substitute NEW for subtrees matching OLD."
706 (when (and testp notp)
707 (error ":TEST and :TEST-NOT were both supplied."))
708 (let ((key (and key (%coerce-callable-to-fun key)))
709 (test (if testp (%coerce-callable-to-fun test) test))
710 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
711 (declare (type function test test-not))
712 (labels ((s (subtree)
713 (cond ((satisfies-the-test old subtree) new)
714 ((atom subtree) subtree)
715 (t (do* ((last nil subtree)
716 (subtree subtree (cdr subtree)))
717 ((atom subtree)
718 (if (satisfies-the-test old subtree)
719 (setf (cdr last) new)))
720 (if (satisfies-the-test old subtree)
721 (return (setf (cdr last) new))
722 (setf (car subtree) (s (car subtree)))))
723 subtree))))
724 (s tree))))
726 (defun nsubst-if (new test tree &key key)
727 "Substitute NEW for subtrees of TREE for which TEST is true."
728 (let ((test (%coerce-callable-to-fun test))
729 (key (and key (%coerce-callable-to-fun key))))
730 (labels ((s (subtree)
731 (cond ((funcall test (apply-key key subtree)) new)
732 ((atom subtree) subtree)
733 (t (do* ((last nil subtree)
734 (subtree subtree (cdr subtree)))
735 ((atom subtree)
736 (if (funcall test (apply-key key subtree))
737 (setf (cdr last) new)))
738 (if (funcall test (apply-key key subtree))
739 (return (setf (cdr last) new))
740 (setf (car subtree) (s (car subtree)))))
741 subtree))))
742 (s tree))))
744 (defun nsubst-if-not (new test tree &key key)
745 "Substitute NEW for subtrees of TREE for which TEST is false."
746 (let ((test (%coerce-callable-to-fun test))
747 (key (and key (%coerce-callable-to-fun key))))
748 (labels ((s (subtree)
749 (cond ((not (funcall test (apply-key key subtree))) new)
750 ((atom subtree) subtree)
751 (t (do* ((last nil subtree)
752 (subtree subtree (cdr subtree)))
753 ((atom subtree)
754 (if (not (funcall test (apply-key key subtree)))
755 (setf (cdr last) new)))
756 (if (not (funcall test (apply-key key subtree)))
757 (return (setf (cdr last) new))
758 (setf (car subtree) (s (car subtree)))))
759 subtree))))
760 (s tree))))
762 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
763 "Substitute from ALIST into TREE nondestructively."
764 (when (and testp notp)
765 (error ":TEST and :TEST-NOT were both supplied."))
766 (let ((key (and key (%coerce-callable-to-fun key)))
767 (test (if testp (%coerce-callable-to-fun test) test))
768 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
769 (declare (type function test test-not))
770 (declare (inline assoc))
771 (labels ((s (subtree)
772 (let* ((key-val (apply-key key subtree))
773 (assoc (if notp
774 (assoc key-val alist :test-not test-not)
775 (assoc key-val alist :test test))))
776 (cond (assoc (cdr assoc))
777 ((atom subtree) subtree)
778 (t (let ((car (s (car subtree)))
779 (cdr (s (cdr subtree))))
780 (if (and (eq car (car subtree))
781 (eq cdr (cdr subtree)))
782 subtree
783 (cons car cdr))))))))
784 (s tree))))
786 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
787 ;;; because it can be referenced in inline expansions.
788 (defmacro nsublis-macro ()
789 (let ((key-tmp (gensym)))
790 `(let ((,key-tmp (apply-key key subtree)))
791 (if notp
792 (assoc ,key-tmp alist :test-not test-not)
793 (assoc ,key-tmp alist :test test)))))
795 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
796 "Substitute from ALIST into TREE destructively."
797 (when (and testp notp)
798 (error ":TEST and :TEST-NOT were both supplied."))
799 (let ((key (and key (%coerce-callable-to-fun key)))
800 (test (if testp (%coerce-callable-to-fun test) test))
801 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
802 (declare (type function test test-not)
803 (inline assoc))
804 (let (temp)
805 (labels ((s (subtree)
806 (cond ((setq temp (nsublis-macro))
807 (cdr temp))
808 ((atom subtree) subtree)
809 (t (do* ((last nil subtree)
810 (subtree subtree (cdr subtree)))
811 ((atom subtree)
812 (if (setq temp (nsublis-macro))
813 (setf (cdr last) (cdr temp))))
814 (if (setq temp (nsublis-macro))
815 (return (setf (cdr last) (cdr temp)))
816 (setf (car subtree) (s (car subtree)))))
817 subtree))))
818 (s tree)))))
820 ;;;; functions for using lists as sets
822 (defun member (item list &key key (test nil testp) (test-not nil notp))
823 "Return the tail of LIST beginning with first element satisfying EQLity,
824 :TEST, or :TEST-NOT with the given ITEM."
825 (declare (explicit-check))
826 (when (and testp notp)
827 (error ":TEST and :TEST-NOT were both supplied."))
828 (let ((key (and key (%coerce-callable-to-fun key)))
829 (test (and testp (%coerce-callable-to-fun test)))
830 (test-not (and notp (%coerce-callable-to-fun test-not))))
831 (cond (test
832 (if key
833 (%member-key-test item list key test)
834 (%member-test item list test)))
835 (test-not
836 (if key
837 (%member-key-test-not item list key test-not)
838 (%member-test-not item list test-not)))
840 (if key
841 (%member-key item list key)
842 (%member item list))))))
844 (defun member-if (test list &key key)
845 "Return tail of LIST beginning with first element satisfying TEST."
846 (declare (explicit-check))
847 (let ((test (%coerce-callable-to-fun test))
848 (key (and key (%coerce-callable-to-fun key))))
849 (if key
850 (%member-if-key test list key)
851 (%member-if test list))))
853 (defun member-if-not (test list &key key)
854 "Return tail of LIST beginning with first element not satisfying TEST."
855 (declare (explicit-check))
856 (let ((test (%coerce-callable-to-fun test))
857 (key (and key (%coerce-callable-to-fun key))))
858 (if key
859 (%member-if-not-key test list key)
860 (%member-if-not test list))))
862 (defun tailp (object list)
863 "Return true if OBJECT is the same as some tail of LIST, otherwise
864 returns false. LIST must be a proper list or a dotted list."
865 (do ((list list (cdr list)))
866 ((atom list) (eql list object))
867 (if (eql object list)
868 (return t))))
870 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
871 "Add ITEM to LIST unless it is already a member"
872 (declare (explicit-check))
873 (when (and testp notp)
874 (error ":TEST and :TEST-NOT were both supplied."))
875 (let ((key (and key (%coerce-callable-to-fun key)))
876 (test (and testp (%coerce-callable-to-fun test)))
877 (test-not (and notp (%coerce-callable-to-fun test-not))))
878 (cond (test
879 (if key
880 (%adjoin-key-test item list key test)
881 (%adjoin-test item list test)))
882 (test-not
883 (if key
884 (%adjoin-key-test-not item list key test-not)
885 (%adjoin-test-not item list test-not)))
887 (if key
888 (%adjoin-key item list key)
889 (%adjoin item list))))))
891 ;;; For cases where MEMBER is called in a loop this allows to perform
892 ;;; the dispatch that the MEMBER function does only once.
893 (defmacro with-member-test ((test-var &optional first-clause) &body body)
894 `(let* ((key (and key (%coerce-callable-to-fun key)))
895 (,test-var (cond ,@(and first-clause ; used by LIST-REMOVE-DUPLICATES*
896 `(,first-clause))
897 (notp
898 (if key
899 (lambda (x list2 key test)
900 (%member-key-test-not (funcall (truly-the function key) x)
901 list2 key test))
902 (lambda (x list2 key test)
903 (declare (ignore key))
904 (%member-test-not x list2 test))))
905 (testp
906 (if key
907 (lambda (x list2 key test)
908 (%member-key-test (funcall (truly-the function key) x)
909 list2 key test))
910 (lambda (x list2 key test)
911 (declare (ignore key))
912 (%member-test x list2 test))))
913 (key
914 (lambda (x list2 key test)
915 (declare (ignore test))
916 (%member-key (funcall (truly-the function key) x) list2 key)))
918 (lambda (x list2 key test)
919 (declare (ignore key test))
920 (%member x list2)))))
921 (test (cond (notp
922 (%coerce-callable-to-fun test-not))
923 (testp
924 (%coerce-callable-to-fun test)))))
926 ,@body))
928 (defconstant +list-based-union-limit+ 80)
930 (defun hash-table-test-p (fun)
931 (or (eq fun #'eq)
932 (eq fun #'eql)
933 (eq fun #'equal)
934 (eq fun #'equalp)
935 (eq fun 'eq)
936 (eq fun 'eql)
937 (eq fun 'equal)
938 (eq fun 'equalp)))
940 (defun union (list1 list2 &key key (test nil testp) (test-not nil notp))
941 "Return the union of LIST1 and LIST2."
942 (declare (explicit-check))
943 (when (and testp notp)
944 (error ":TEST and :TEST-NOT were both supplied."))
945 ;; We have two possibilities here: for shortish lists we pick up the
946 ;; shorter one as the result, and add the other one to it. For long
947 ;; lists we use a hash-table when possible.
948 (let ((n1 (length list1))
949 (n2 (length list2)))
950 (multiple-value-bind (short long n-short)
951 (if (< n1 n2)
952 (values list1 list2 n1)
953 (values list2 list1 n2))
954 (if (or (< n-short +list-based-union-limit+)
955 notp
956 (and testp
957 (not (hash-table-test-p test))))
958 (with-member-test (member-test)
959 (let ((orig short))
960 (dolist (elt long)
961 (unless (funcall member-test elt orig key test)
962 (push elt short)))
963 short))
964 (let ((table (make-hash-table :test (if testp
965 test
966 #'eql) :size (+ n1 n2)))
967 (key (and key (%coerce-callable-to-fun key)))
968 (union nil))
969 (dolist (elt long)
970 (setf (gethash (apply-key key elt) table) elt))
971 (dolist (elt short)
972 (setf (gethash (apply-key key elt) table) elt))
973 (maphash (lambda (k v)
974 (declare (ignore k))
975 (push v union))
976 table)
977 union)))))
979 (defun nunion (list1 list2 &key key (test nil testp) (test-not nil notp))
980 "Destructively return the union of LIST1 and LIST2."
981 (declare (explicit-check))
982 (when (and testp notp)
983 (error ":TEST and :TEST-NOT were both supplied."))
984 ;; We have two possibilities here: for shortish lists we pick up the
985 ;; shorter one as the result, and add the other one to it. For long
986 ;; lists we use a hash-table when possible.
987 (let ((n1 (length list1))
988 (n2 (length list2)))
989 (multiple-value-bind (short long n-short)
990 (if (< n1 n2)
991 (values list1 list2 n1)
992 (values list2 list1 n2))
993 (if (or (< n-short +list-based-union-limit+)
994 notp
995 (and testp
996 (not (hash-table-test-p test))))
997 (with-member-test (member-test)
998 (do ((orig short)
999 (elt (car long) (car long)))
1000 ((endp long))
1001 (if (funcall member-test elt orig key test)
1002 (pop long)
1003 (shiftf long (cdr long) short long)))
1004 short)
1005 (let ((table (make-hash-table :test (if testp
1006 test
1007 #'eql) :size (+ n1 n2)))
1008 (key (and key (%coerce-callable-to-fun key))))
1009 (dolist (elt long)
1010 (setf (gethash (apply-key key elt) table) elt))
1011 (dolist (elt short)
1012 (setf (gethash (apply-key key elt) table) elt))
1013 (let ((union long)
1014 (head long))
1015 (maphash (lambda (k v)
1016 (declare (ignore k))
1017 (if head
1018 (setf (car head) v
1019 head (cdr head))
1020 (push v union)))
1021 table)
1022 union))))))
1024 (defun intersection (list1 list2
1025 &key key (test nil testp) (test-not nil notp))
1026 "Return the intersection of LIST1 and LIST2."
1027 (declare (explicit-check))
1028 (when (and testp notp)
1029 (error ":TEST and :TEST-NOT were both supplied."))
1030 (when (and list1 list2)
1031 (with-member-test (member-test)
1032 (let ((res nil))
1033 (dolist (elt list1)
1034 (when (funcall member-test elt list2 key test)
1035 (push elt res)))
1036 res))))
1038 (defun nintersection (list1 list2
1039 &key key (test nil testp) (test-not nil notp))
1040 "Destructively return the intersection of LIST1 and LIST2."
1041 (declare (explicit-check))
1042 (when (and testp notp)
1043 (error ":TEST and :TEST-NOT were both supplied."))
1044 (when (and list1 list2)
1045 (with-member-test (member-test)
1046 (let ((res nil)
1047 (list1 list1))
1048 (do () ((endp list1))
1049 (if (funcall member-test (car list1) list2 key test)
1050 (shiftf list1 (cdr list1) res list1)
1051 (setf list1 (cdr list1))))
1052 res))))
1054 (defun set-difference (list1 list2
1055 &key key (test nil testp) (test-not nil notp))
1056 "Return the elements of LIST1 which are not in LIST2."
1057 (declare (explicit-check))
1058 (when (and testp notp)
1059 (error ":TEST and :TEST-NOT were both supplied."))
1060 (if list2
1061 (with-member-test (member-test)
1062 (let ((res nil))
1063 (dolist (elt list1)
1064 (unless (funcall member-test elt list2 key test)
1065 (push elt res)))
1066 res))
1067 list1))
1069 (defun nset-difference (list1 list2
1070 &key key (test nil testp) (test-not nil notp))
1071 "Destructively return the elements of LIST1 which are not in LIST2."
1072 (declare (explicit-check))
1073 (when (and testp notp)
1074 (error ":TEST and :TEST-NOT were both supplied."))
1075 (if list2
1076 (with-member-test (member-test)
1077 (let ((res nil)
1078 (list1 list1))
1079 (do () ((endp list1))
1080 (if (funcall member-test (car list1) list2 key test)
1081 (setf list1 (cdr list1))
1082 (shiftf list1 (cdr list1) res list1)))
1083 res))
1084 list1))
1086 (defun set-exclusive-or (list1 list2
1087 &key key (test nil testp) (test-not nil notp))
1088 "Return new list of elements appearing exactly once in LIST1 and LIST2."
1089 (declare (explicit-check))
1090 (when (and testp notp)
1091 (error ":TEST and :TEST-NOT were both supplied."))
1092 (let ((result nil))
1093 (with-member-test (member-test)
1094 (dolist (elt list1)
1095 (unless (funcall member-test elt list2 key test)
1096 (push elt result)))
1097 (dx-flet ((test (x y) (funcall (truly-the function test) y x)))
1098 (dolist (elt list2)
1099 (unless (funcall member-test elt list1 key #'test)
1100 (push elt result)))))
1101 result))
1103 (defun nset-exclusive-or (list1 list2
1104 &key key (test #'eql testp) (test-not #'eql notp))
1105 "Destructively return a list with elements which appear but once in LIST1
1106 and LIST2."
1107 (declare (explicit-check))
1108 (when (and testp notp)
1109 (error ":TEST and :TEST-NOT were both supplied."))
1110 (let ((key (and key (%coerce-callable-to-fun key)))
1111 (test (if testp (%coerce-callable-to-fun test) test))
1112 (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
1113 (declare (type function test test-not))
1114 ;; The outer loop examines LIST1 while the inner loop examines
1115 ;; LIST2. If an element is found in LIST2 "equal" to the element
1116 ;; in LIST1, both are spliced out. When the end of LIST1 is
1117 ;; reached, what is left of LIST2 is tacked onto what is left of
1118 ;; LIST1. The splicing operation ensures that the correct
1119 ;; operation is performed depending on whether splice is at the
1120 ;; top of the list or not.
1121 (do ((list1 list1)
1122 (list2 list2)
1123 (x list1 (cdr x))
1124 (splicex ())
1125 (deleted-y ())
1126 ;; elements of LIST2, which are "equal" to some processed
1127 ;; earlier elements of LIST1
1129 ((endp x)
1130 (if (null splicex)
1131 (setq list1 list2)
1132 (rplacd splicex list2))
1133 list1)
1134 (let ((key-val-x (apply-key key (car x)))
1135 (found-duplicate nil))
1137 ;; Move all elements from LIST2, which are "equal" to (CAR X),
1138 ;; to DELETED-Y.
1139 (do* ((y list2 next-y)
1140 (next-y (cdr y) (cdr y))
1141 (splicey ()))
1142 ((endp y))
1143 (cond ((let ((key-val-y (apply-key key (car y))))
1144 (if notp
1145 (not (funcall test-not key-val-x key-val-y))
1146 (funcall test key-val-x key-val-y)))
1147 (if (null splicey)
1148 (setq list2 (cdr y))
1149 (rplacd splicey (cdr y)))
1150 (setq deleted-y (rplacd y deleted-y))
1151 (setq found-duplicate t))
1152 (t (setq splicey y))))
1154 (unless found-duplicate
1155 (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1157 (if found-duplicate
1158 (if (null splicex)
1159 (setq list1 (cdr x))
1160 (rplacd splicex (cdr x)))
1161 (setq splicex x))))))
1163 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1164 "Return T if every element in LIST1 is also in LIST2."
1165 (declare (explicit-check))
1166 (when (and testp notp)
1167 (error ":TEST and :TEST-NOT were both supplied."))
1168 (with-member-test (member-test)
1169 (dolist (elt list1)
1170 (unless (funcall member-test elt list2 key test)
1171 (return-from subsetp nil)))
1174 ;;;; functions that operate on association lists
1176 (defun acons (key datum alist)
1177 "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1178 (cons (cons key datum) alist))
1180 (defun pairlis (keys data &optional (alist '()))
1181 "Construct an association list from KEYS and DATA (adding to ALIST)."
1182 (do ((x keys (cdr x))
1183 (y data (cdr y)))
1184 ((and (endp x) (endp y)) alist)
1185 (if (or (endp x) (endp y))
1186 (error "The lists of keys and data are of unequal length."))
1187 (setq alist (acons (car x) (car y) alist))))
1189 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1190 "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1191 the ITEM."
1192 (declare (explicit-check))
1193 (when (and testp notp)
1194 (error ":TEST and :TEST-NOT were both supplied."))
1195 (let ((key (and key (%coerce-callable-to-fun key)))
1196 (test (and testp (%coerce-callable-to-fun test)))
1197 (test-not (and notp (%coerce-callable-to-fun test-not))))
1198 (cond (test
1199 (if key
1200 (%assoc-key-test item alist key test)
1201 (%assoc-test item alist test)))
1202 (test-not
1203 (if key
1204 (%assoc-key-test-not item alist key test-not)
1205 (%assoc-test-not item alist test-not)))
1207 (if key
1208 (%assoc-key item alist key)
1209 (%assoc item alist))))))
1211 (defun assoc-if (predicate alist &key key)
1212 "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1213 KEY is supplied, apply it to the CAR of each cons before testing."
1214 (declare (explicit-check))
1215 (let ((predicate (%coerce-callable-to-fun predicate))
1216 (key (and key (%coerce-callable-to-fun key))))
1217 (if key
1218 (%assoc-if-key predicate alist key)
1219 (%assoc-if predicate alist))))
1221 (defun assoc-if-not (predicate alist &key key)
1222 "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1223 If KEY is supplied, apply it to the CAR of each cons before testing."
1224 (declare (explicit-check))
1225 (let ((predicate (%coerce-callable-to-fun predicate))
1226 (key (and key (%coerce-callable-to-fun key))))
1227 (if key
1228 (%assoc-if-not-key predicate alist key)
1229 (%assoc-if-not predicate alist))))
1231 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1232 "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1233 the ITEM."
1234 (declare (explicit-check))
1235 (when (and testp notp)
1236 (error ":TEST and :TEST-NOT were both supplied."))
1237 (let ((key (and key (%coerce-callable-to-fun key)))
1238 (test (and testp (%coerce-callable-to-fun test)))
1239 (test-not (and notp (%coerce-callable-to-fun test-not))))
1240 (cond (test
1241 (if key
1242 (%rassoc-key-test item alist key test)
1243 (%rassoc-test item alist test)))
1244 (test-not
1245 (if key
1246 (%rassoc-key-test-not item alist key test-not)
1247 (%rassoc-test-not item alist test-not)))
1249 (if key
1250 (%rassoc-key item alist key)
1251 (%rassoc item alist))))))
1253 (defun rassoc-if (predicate alist &key key)
1254 "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1255 is supplied, apply it to the CDR of each cons before testing."
1256 (declare (explicit-check))
1257 (let ((predicate (%coerce-callable-to-fun predicate))
1258 (key (and key (%coerce-callable-to-fun key))))
1259 (if key
1260 (%rassoc-if-key predicate alist key)
1261 (%rassoc-if predicate alist))))
1263 (defun rassoc-if-not (predicate alist &key key)
1264 "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1265 If KEY is supplied, apply it to the CDR of each cons before testing."
1266 (declare (explicit-check))
1267 (let ((predicate (%coerce-callable-to-fun predicate))
1268 (key (and key (%coerce-callable-to-fun key))))
1269 (if key
1270 (%rassoc-if-not-key predicate alist key)
1271 (%rassoc-if-not predicate alist))))
1273 ;;;; mapping functions
1275 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1276 ;;; MAPL, MAPLIST, and MAPCON
1278 ;;; Map the designated function over the arglists in the appropriate
1279 ;;; way. It is done when any of the arglists runs out. Until then, it
1280 ;;; CDRs down the arglists calling the function and accumulating
1281 ;;; results as desired.
1282 (defun map1 (fun-designator arglists accumulate take-car)
1283 (do* ((fun (%coerce-callable-to-fun fun-designator))
1284 (non-acc-result (car arglists))
1285 (ret-list (list nil))
1286 (temp ret-list)
1287 (res nil)
1288 (args (make-list (length arglists))))
1289 ((dolist (x arglists) (or x (return t)))
1290 (if accumulate
1291 (cdr ret-list)
1292 non-acc-result))
1293 (do ((l arglists (cdr l))
1294 (arg args (cdr arg)))
1295 ((null l))
1296 (setf (car arg) (if take-car (caar l) (car l)))
1297 (setf (car l) (cdar l)))
1298 (setq res (apply fun args))
1299 (case accumulate
1300 (:nconc
1301 (when res
1302 (setf (cdr temp) res)
1303 ;; KLUDGE: it is said that MAPCON is equivalent to
1304 ;; (apply #'nconc (maplist ...)) which means (nconc 1) would
1305 ;; return 1, but (nconc 1 1) should signal an error.
1306 ;; The transformed MAP code returns the last result, do that
1307 ;; here as well for consistency and simplicity.
1308 (when (consp res)
1309 (setf temp (last res)))))
1310 (:list (setf (cdr temp) (list res)
1311 temp (cdr temp))))))
1313 (macrolet ((define-list-map (name accumulate take-car
1314 return-value-description)
1315 (let ((documentation
1316 (format nil "Apply FUNCTION to successive tuples ~
1317 of ~A of LIST and MORE-LISTS.~%~
1318 Return ~A."
1319 (if take-car "elements" "CDRs")
1320 return-value-description)))
1321 `(defun ,name (function list &rest more-lists)
1322 ,documentation
1323 (declare (explicit-check))
1324 (dx-let ((lists (list* list more-lists)))
1325 (map1 function lists ,accumulate ,take-car))))))
1326 (define-list-map mapc nil t "LIST")
1327 (define-list-map mapcar :list t "list of FUNCTION return values")
1328 (define-list-map mapcan :nconc t "NCONC of FUNCTION return values")
1329 (define-list-map mapl nil nil "LIST")
1330 (define-list-map maplist :list nil "list of results")
1331 (define-list-map mapcon :nconc nil "NCONC of results"))
1333 ;;;; Specialized versions
1335 ;;; %ADJOIN-*, %ASSOC-*, %MEMBER-*, and %RASSOC-* functions. Deftransforms
1336 ;;; delegate to TRANSFORM-LIST-PRED-SEEK and TRANSFORM-LIST-ITEM-SEEK which
1337 ;;; pick the appropriate versions. These win because they have only positional
1338 ;;; arguments, the TEST, TEST-NOT & KEY functions are known to exist (or not),
1339 ;;; and are known to be functions instead of function designators. We are also
1340 ;;; able to transform many common cases to -EQ versions, which are
1341 ;;; substantially faster then EQL using ones.
1342 (macrolet
1343 ((def (funs form &optional variant)
1344 (flet ((%def (name &optional conditional)
1345 (let* ((body-loop
1346 `(do ((list list (cdr list)))
1347 ((null list) nil)
1348 (declare (list list))
1349 (let ((this (car list)))
1350 ,(let ((cxx (if (char= #\A (char (string name) 0))
1351 'car ; assoc, assoc-if, assoc-if-not
1352 'cdr))) ; rassoc, rassoc-if, rassoc-if-not
1353 (ecase name
1354 ((assoc rassoc)
1355 (if funs
1356 `(when this
1357 (let ((target (,cxx this)))
1358 (when ,form
1359 (return this))))
1360 ;; If there is no TEST/TEST-NOT or
1361 ;; KEY, do the EQ/EQL test first,
1362 ;; before checking for NIL.
1363 `(let ((target (,cxx this)))
1364 (when (and ,form this)
1365 (return this)))))
1366 ((assoc-if assoc-if-not rassoc-if rassoc-if-not)
1367 (aver (equal '(eql x) (subseq form 0 2)))
1368 `(when this
1369 (let ((target (,cxx this)))
1370 (,conditional (funcall ,@(cdr form))
1371 (return this)))))
1372 (member
1373 `(let ((target this))
1374 (when ,form
1375 (return list))))
1376 ((member-if member-if-not)
1377 (aver (equal '(eql x) (subseq form 0 2)))
1378 `(let ((target this))
1379 (,conditional (funcall ,@(cdr form))
1380 (return list))))
1381 (adjoin
1382 `(let ((target this))
1383 (when ,form
1384 (return t)))))))))
1385 (body (if (eq 'adjoin name)
1386 `(if (let ,(when (member 'key funs)
1387 `((x (funcall key x))))
1388 ,body-loop)
1389 list
1390 (cons x list))
1391 body-loop)))
1392 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1393 (x list ,@funs)
1394 (declare (optimize speed (sb!c::verify-arg-count 0)))
1395 ,@(when funs `((declare (function ,@funs))))
1396 ,@(unless (member name '(member assoc adjoin rassoc)) `((declare (function x))))
1397 (declare (explicit-check))
1398 ,body))))
1399 `(progn
1400 ,(%def 'adjoin)
1401 ,(%def 'assoc)
1402 ,(%def 'member)
1403 ,(%def 'rassoc)
1404 ,@(when (and (not variant) (member funs '(() (key)) :test #'equal))
1405 (list (%def 'member-if 'when)
1406 (%def 'member-if-not 'unless)
1407 (%def 'assoc-if 'when)
1408 (%def 'assoc-if-not 'unless)
1409 (%def 'rassoc-if 'when)
1410 (%def 'rassoc-if-not 'unless)))))))
1411 (def ()
1412 (eql x target))
1413 (def ()
1414 (eq x target)
1416 (def (key)
1417 (eql x (funcall key target)))
1418 (def (key)
1419 (eq x (funcall key target))
1421 (def (key test)
1422 (funcall test x (funcall key target)))
1423 (def (key test-not)
1424 (not (funcall test-not x (funcall key target))))
1425 (def (test)
1426 (funcall test x target))
1427 (def (test-not)
1428 (not (funcall test-not x target))))