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