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