8f6322684e7246c86413d17723c9ebddb3820b7d
[sbcl.git] / src / code / list.lisp
1 ;;;; functions to implement lists
2
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.
11
12 (in-package "SB!IMPL")
13
14 ;;; Limitation: no list might have more than INDEX conses.
15
16 ;;;; KLUDGE: comment from CMU CL, what does it mean?
17 ;;;;   NSUBLIS, things at the beginning broken.
18 ;;;; -- WHN 20000127
19
20 (declaim (maybe-inline
21           tree-equal nth %setnth nthcdr last last1 make-list append
22           nconc nconc2 member-if member-if-not tailp union
23           nunion intersection nintersection set-difference nset-difference
24           set-exclusive-or nset-exclusive-or subsetp acons
25           assoc-if assoc-if-not rassoc rassoc-if rassoc-if-not subst subst-if
26           subst-if-not nsubst nsubst-if nsubst-if-not sublis nsublis))
27
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))
114 \f
115 (declaim (maybe-inline tree-equal-test tree-equal-test-not))
116
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 ())))
126
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 ())))
136
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))))
145
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))
152
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))))
165
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)))
170
171 (defun first (list)
172   #!+sb-doc
173   "Return the 1st object in a list or NIL if the list is empty."
174   (car list))
175 (defun second (list)
176   "Return the 2nd object in a list or NIL if there is no 2nd object."
177   (cadr list))
178 (defun third (list)
179   #!+sb-doc
180   "Return the 3rd object in a list or NIL if there is no 3rd object."
181   (caddr list))
182 (defun fourth (list)
183   #!+sb-doc
184   "Return the 4th object in a list or NIL if there is no 4th object."
185   (cadddr list))
186 (defun fifth (list)
187   #!+sb-doc
188   "Return the 5th object in a list or NIL if there is no 5th object."
189   (car (cddddr list)))
190 (defun sixth (list)
191   #!+sb-doc
192   "Return the 6th object in a list or NIL if there is no 6th object."
193   (cadr (cddddr list)))
194 (defun seventh (list)
195   #!+sb-doc
196   "Return the 7th object in a list or NIL if there is no 7th object."
197   (caddr (cddddr list)))
198 (defun eighth (list)
199   #!+sb-doc
200   "Return the 8th object in a list or NIL if there is no 8th object."
201   (cadddr (cddddr list)))
202 (defun ninth (list)
203   #!+sb-doc
204   "Return the 9th object in a list or NIL if there is no 9th object."
205   (car (cddddr (cddddr list))))
206 (defun tenth (list)
207   #!+sb-doc
208   "Return the 10th object in a list or NIL if there is no 10th object."
209   (cadr (cddddr (cddddr list))))
210 (defun rest (list)
211   #!+sb-doc
212   "Means the same as the cdr of a list."
213   (cdr list))
214
215 (defun nthcdr (n list)
216   #!+sb-doc
217   "Performs the cdr function n times on a list."
218   (flet ((fast-nthcdr (n list)
219            (declare (type index n))
220            (do ((i n (1- i))
221                 (result list (cdr result)))
222                ((not (plusp i)) result)
223              (declare (type index i)))))
224     (typecase n
225       (index (fast-nthcdr n list))
226       (t (do ((i 0 (1+ i))
227               (r-i list (cdr r-i))
228               (r-2i list (cddr r-2i)))
229              ((and (eq r-i r-2i) (not (zerop i)))
230               (fast-nthcdr (mod n i) r-i))
231            (declare (type index i)))))))
232
233 (defun last1 (list)
234   #!+sb-doc
235   "Return the last cons (not the last element) of a list"
236   (let ((rest list)
237         (list list))
238     (loop (unless (consp rest) (return list))
239           (shiftf list rest (cdr rest)))))
240
241 (defun last (list &optional (n 1))
242   #!+sb-doc
243   "Return the last N conses (not the last element!) of a list."
244   (if (eql n 1)
245       (last1 list)
246     (if (typep n 'index)
247         (do ((checked-list list (cdr checked-list))
248              (returned-list list)
249              (index 0 (1+ index)))
250             ((atom checked-list) returned-list)
251           (declare (type index index))
252           (if (>= index n)
253               (pop returned-list)))
254       list)))
255
256 (defun list (&rest args)
257   #!+sb-doc
258   "Return constructs and returns a list of its arguments."
259   args)
260
261 ;;; LIST* is done the same as LIST, except that the last cons is made
262 ;;; a dotted pair.
263
264 (defun list* (arg &rest others)
265   #!+sb-doc
266   "Return a list of the arguments with last cons a dotted pair"
267   (cond ((atom others) arg)
268         ((atom (cdr others)) (cons arg (car others)))
269         (t (do ((x others (cdr x)))
270                ((null (cddr x)) (rplacd x (cadr x))))
271            (cons arg others))))
272
273 (defun make-list (size &key initial-element)
274   #!+sb-doc
275   "Constructs a list with size elements each set to value"
276   (declare (type index size))
277   (do ((count size (1- count))
278        (result '() (cons initial-element result)))
279       ((<= count 0) result)
280     (declare (type index count))))
281 \f
282 (defun append (&rest lists)
283   #!+sb-doc
284   "Construct a new list by concatenating the list arguments"
285   (labels ((fail (object)
286              (error 'type-error
287                     :datum object
288                     :expected-type 'list))
289            (append-into (last-cons current rest)
290              "Set (CDR LAST-CONS) to (APPLY #'APPEND CURRENT REST)."
291              (declare (cons last-cons rest))
292              (cond ((consp current)
293                     (append-into (setf (cdr last-cons) (list (car current)))
294                                 (cdr current)
295                                 rest))
296                    ((not (null current)) (fail current))
297                    ((null (cdr rest)) (setf (cdr last-cons) (car rest)))
298                    (t (append-into last-cons (car rest) (cdr rest)))))
299            (append1 (lists)
300              (let ((current (car lists))
301                    (rest (cdr lists)))
302                (cond ((null rest) current)
303                      ((consp current)
304                       (let ((result (truly-the cons (list (car current)))))
305                         (append-into result
306                                     (cdr current)
307                                     rest)
308                         result))
309                      ((null current) (append1 rest))
310                      (t (fail current))))))
311     (append1 lists)))
312 \f
313 ;;;; list copying functions
314
315 (eval-when (:compile-toplevel :load-toplevel :execute)
316   (sb!xc:defmacro !copy-list-macro (list &key check-proper-list)
317     ;; Unless CHECK-PROPER-LIST is true, the list is copied correctly
318     ;; even if the list is not terminated by NIL. The new list is built
319     ;; by CDR'ing SPLICE which is always at the tail of the new list.
320     `(when ,list
321        (let ((copy (list (car ,list))))
322          (do ((orig (cdr ,list) (cdr orig))
323               (splice copy (cdr (rplacd splice (cons (car orig) nil)))))
324              (,@(if check-proper-list
325                     '((endp orig))
326                     '((atom orig)
327                       (unless (null orig)
328                         (rplacd splice orig))))
329               copy))))))
330
331 (defun copy-list (list)
332   #!+sb-doc
333   "Return a new list which is EQUAL to LIST. LIST may be improper."
334   (!copy-list-macro list))
335
336 (defun copy-alist (alist)
337   #!+sb-doc
338   "Return a new association list which is EQUAL to ALIST."
339   (if (endp alist)
340       alist
341       (let ((result
342              (cons (if (atom (car alist))
343                        (car alist)
344                        (cons (caar alist) (cdar alist)))
345                    nil)))
346         (do ((x (cdr alist) (cdr x))
347              (splice result
348                      (cdr (rplacd splice
349                                   (cons
350                                    (if (atom (car x))
351                                        (car x)
352                                        (cons (caar x) (cdar x)))
353                                    nil)))))
354             ((endp x)))
355         result)))
356
357 (defun copy-tree (object)
358   #!+sb-doc
359   "Recursively copy trees of conses."
360   (if (consp object)
361       (cons (copy-tree (car object)) (copy-tree (cdr object)))
362       object))
363 \f
364 ;;;; more commonly-used list functions
365
366 (defun revappend (x y)
367   #!+sb-doc
368   "Return (append (reverse x) y)."
369   (do ((top x (cdr top))
370        (result y (cons (car top) result)))
371       ((endp top) result)))
372
373 ;;; NCONC finds the first non-null list, so it can make splice point
374 ;;; to a cons. After finding the first cons element, it holds it in a
375 ;;; result variable while running down successive elements tacking
376 ;;; them together. While tacking lists together, if we encounter a
377 ;;; null list, we set the previous list's last cdr to nil just in case
378 ;;; it wasn't already nil, and it could have been dotted while the
379 ;;; null list was the last argument to NCONC. The manipulation of
380 ;;; splice (that is starting it out on a first cons, setting LAST of
381 ;;; splice, and setting splice to ele) inherently handles (nconc x x),
382 ;;; and it avoids running down the last argument to NCONC which allows
383 ;;; the last argument to be circular.
384 (defun nconc (&rest lists)
385   #!+sb-doc
386   "Concatenates the lists given as arguments (by changing them)"
387   (flet ((fail (object)
388            (error 'type-error
389                   :datum object
390                   :expected-type 'list)))
391     (do ((top lists (cdr top)))
392         ((null top) nil)
393       (let ((top-of-top (car top)))
394         (typecase top-of-top
395           (cons
396            (let* ((result top-of-top)
397                   (splice result))
398              (do ((elements (cdr top) (cdr elements)))
399                  ((endp elements))
400                (let ((ele (car elements)))
401                  (typecase ele
402                    (cons (rplacd (last splice) ele)
403                          (setf splice ele))
404                    (null (rplacd (last splice) nil))
405                    (atom (if (cdr elements)
406                              (fail ele)
407                              (rplacd (last splice) ele)))
408                    (t (fail ele)))))
409              (return result)))
410           (null)
411           (atom
412            (if (cdr top)
413                (fail top-of-top)
414                (return top-of-top)))
415           (t (fail top-of-top)))))))
416
417 (defun nconc2 (x y)
418   (if (null x) y
419     (let ((z x)
420           (rest (cdr x)))
421       (loop
422        (unless (consp rest)
423          (rplacd z y)
424          (return x))
425        (shiftf z rest (cdr rest))))))
426
427 (defun nreconc (x y)
428   #!+sb-doc
429   "Return (NCONC (NREVERSE X) Y)."
430   (do ((1st (cdr x) (if (endp 1st) 1st (cdr 1st)))
431        (2nd x 1st)              ;2nd follows first down the list.
432        (3rd y 2nd))             ;3rd follows 2nd down the list.
433       ((atom 2nd) 3rd)
434     (rplacd 2nd 3rd)))
435 \f
436 (flet (;; Return the number of conses at the head of the
437        ;; possibly-improper list LIST. (Or if LIST is circular, you
438        ;; lose.)
439        (count-conses (list)
440          (do ((in-list list (cdr in-list))
441               (result 0 (1+ result)))
442              ((atom in-list)
443               result)
444            (declare (type index result)))))
445   (declare (ftype (function (t) index) count-conses))
446   (defun butlast (list &optional (n 1))
447     (if (typep n 'index)
448         (let ((n-conses-in-list (count-conses list)))
449           (cond ((zerop n)
450                  ;; (We can't use SUBSEQ in this case because LIST isn't
451                  ;; necessarily a proper list, but SUBSEQ expects a
452                  ;; proper sequence. COPY-LIST isn't so fussy.)
453                  (copy-list list))
454                 ((>= n n-conses-in-list)
455                  nil)
456                 (t
457                  ;; (LIST isn't necessarily a proper list in this case
458                  ;; either, and technically SUBSEQ wants a proper
459                  ;; sequence, but no reasonable implementation of SUBSEQ
460                  ;; will actually walk down to the end of the list to
461                  ;; check, and since we're calling our own implementation
462                  ;; we know it's reasonable, so it's OK.)
463                  (subseq list 0 (- n-conses-in-list n)))))
464         nil))
465   (defun nbutlast (list &optional (n 1))
466     (cond ((zerop n)
467            list)
468           ((not (typep n 'index))
469            nil)
470           (t (let ((n-conses-in-list (count-conses list)))
471                (unless (<= n-conses-in-list n)
472                  (setf (cdr (nthcdr (- n-conses-in-list n 1) list))
473                        nil)
474                  list))))))
475
476 (defun ldiff (list object)
477   "Return a new list, whose elements are those of LIST that appear before
478    OBJECT. If OBJECT is not a tail of LIST, a copy of LIST is returned.
479    LIST must be a proper list or a dotted list."
480   (do* ((list list (cdr list))
481         (result (list ()))
482         (splice result))
483        ((atom list)
484         (if (eql list object)
485             (cdr result)
486             (progn (rplacd splice list) (cdr result))))
487     (if (eql list object)
488         (return (cdr result))
489         (setq splice (cdr (rplacd splice (list (car list))))))))
490 \f
491 ;;;; functions to alter list structure
492
493 (defun rplaca (cons x)
494   #!+sb-doc
495   "Change the CAR of CONS to X and return the CONS."
496   (rplaca cons x))
497
498 (defun rplacd (cons x)
499   #!+sb-doc
500   "Change the CDR of CONS to X and return the CONS."
501   (rplacd cons x))
502
503 ;;; The following are for use by SETF.
504
505 (defun %rplaca (x val) (rplaca x val) val)
506
507 (defun %rplacd (x val) (rplacd x val) val)
508
509 ;;; Set the Nth element of LIST to NEWVAL.
510 (defun %setnth (n list newval)
511   (typecase n
512     (index
513      (do ((count n (1- count))
514           (list list (cdr list)))
515          ((endp list)
516           (error "~S is too large an index for SETF of NTH." n))
517        (declare (type fixnum count))
518        (when (<= count 0)
519          (rplaca list newval)
520          (return newval))))
521     (t (let ((cons (nthcdr n list)))
522          (when (endp cons)
523            (error "~S is too large an index for SETF of NTH." n))
524          (rplaca cons newval)
525          newval))))
526 \f
527 ;;;; :KEY arg optimization to save funcall of IDENTITY
528
529 ;;; APPLY-KEY saves us a function call sometimes.
530 ;;;    This isn't wrapped in an (EVAL-WHEN (COMPILE EVAL) ..)
531 ;;;    because it's used in seq.lisp and sort.lisp.
532 (defmacro apply-key (key element)
533   `(if ,key
534        (funcall ,key ,element)
535        ,element))
536 \f
537 ;;;; macros for (&KEY (KEY #'IDENTITY) (TEST #'EQL TESTP) (TEST-NOT NIL NOTP))
538
539 ;;; Use these with the following &KEY args:
540 (defmacro with-set-keys (funcall)
541   `(if notp
542        ,(append funcall '(:key key :test-not test-not))
543        ,(append funcall '(:key key :test test))))
544
545 (defmacro satisfies-the-test (item elt)
546   (let ((key-tmp (gensym)))
547     `(let ((,key-tmp (apply-key key ,elt)))
548       (cond (testp (funcall test ,item ,key-tmp))
549             (notp (not (funcall test-not ,item ,key-tmp)))
550             (t (funcall test ,item ,key-tmp))))))
551 \f
552 ;;;; substitution of expressions
553
554 (defun subst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
555   #!+sb-doc
556   "Substitutes new for subtrees matching old."
557   (when (and testp notp)
558     (error ":TEST and :TEST-NOT were both supplied."))
559   (let ((key (and key (%coerce-callable-to-fun key)))
560         (test (if testp (%coerce-callable-to-fun test) test))
561         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
562     (declare (type function test test-not))
563     (labels ((s (subtree)
564                (cond ((satisfies-the-test old subtree) new)
565                      ((atom subtree) subtree)
566                      (t (let ((car (s (car subtree)))
567                               (cdr (s (cdr subtree))))
568                           (if (and (eq car (car subtree))
569                                    (eq cdr (cdr subtree)))
570                               subtree
571                               (cons car cdr)))))))
572       (s tree))))
573
574 (defun subst-if (new test tree &key key)
575   #!+sb-doc
576   "Substitutes new for subtrees for which test is true."
577   (let ((test (%coerce-callable-to-fun test))
578         (key (and key (%coerce-callable-to-fun key))))
579     (labels ((s (subtree)
580                (cond ((funcall test (apply-key key subtree)) new)
581                      ((atom subtree) subtree)
582                      (t (let ((car (s (car subtree)))
583                               (cdr (s (cdr subtree))))
584                           (if (and (eq car (car subtree))
585                                    (eq cdr (cdr subtree)))
586                               subtree
587                               (cons car cdr)))))))
588       (s tree))))
589
590 (defun subst-if-not (new test tree &key key)
591   #!+sb-doc
592   "Substitutes new for subtrees for which test is false."
593   (let ((test (%coerce-callable-to-fun test))
594         (key (and key (%coerce-callable-to-fun key))))
595     (labels ((s (subtree)
596                (cond ((not (funcall test (apply-key key subtree))) new)
597                      ((atom subtree) subtree)
598                      (t (let ((car (s (car subtree)))
599                               (cdr (s (cdr subtree))))
600                           (if (and (eq car (car subtree))
601                                    (eq cdr (cdr subtree)))
602                               subtree
603                               (cons car cdr)))))))
604       (s tree))))
605
606 (defun nsubst (new old tree &key key (test #'eql testp) (test-not #'eql notp))
607   #!+sb-doc
608   "Substitute NEW for subtrees matching OLD."
609   (when (and testp notp)
610     (error ":TEST and :TEST-NOT were both supplied."))
611   (let ((key (and key (%coerce-callable-to-fun key)))
612         (test (if testp (%coerce-callable-to-fun test) test))
613         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
614     (declare (type function test test-not))
615     (labels ((s (subtree)
616                (cond ((satisfies-the-test old subtree) new)
617                      ((atom subtree) subtree)
618                      (t (do* ((last nil subtree)
619                               (subtree subtree (cdr subtree)))
620                              ((atom subtree)
621                               (if (satisfies-the-test old subtree)
622                                   (setf (cdr last) new)))
623                           (if (satisfies-the-test old subtree)
624                               (return (setf (cdr last) new))
625                               (setf (car subtree) (s (car subtree)))))
626                         subtree))))
627       (s tree))))
628
629 (defun nsubst-if (new test tree &key key)
630   #!+sb-doc
631   "Substitute NEW for subtrees of TREE for which TEST is true."
632   (let ((test (%coerce-callable-to-fun test))
633         (key (and key (%coerce-callable-to-fun key))))
634     (labels ((s (subtree)
635                (cond ((funcall test (apply-key key subtree)) new)
636                      ((atom subtree) subtree)
637                      (t (do* ((last nil subtree)
638                               (subtree subtree (cdr subtree)))
639                              ((atom subtree)
640                               (if (funcall test (apply-key key subtree))
641                                   (setf (cdr last) new)))
642                           (if (funcall test (apply-key key subtree))
643                               (return (setf (cdr last) new))
644                               (setf (car subtree) (s (car subtree)))))
645                         subtree))))
646       (s tree))))
647
648 (defun nsubst-if-not (new test tree &key key)
649   #!+sb-doc
650   "Substitute NEW for subtrees of TREE for which TEST is false."
651   (let ((test (%coerce-callable-to-fun test))
652         (key (and key (%coerce-callable-to-fun key))))
653     (labels ((s (subtree)
654                (cond ((not (funcall test (apply-key key subtree))) new)
655                      ((atom subtree) subtree)
656                      (t (do* ((last nil subtree)
657                               (subtree subtree (cdr subtree)))
658                              ((atom subtree)
659                               (if (not (funcall test (apply-key key subtree)))
660                                   (setf (cdr last) new)))
661                           (if (not (funcall test (apply-key key subtree)))
662                               (return (setf (cdr last) new))
663                               (setf (car subtree) (s (car subtree)))))
664                         subtree))))
665       (s tree))))
666 \f
667 (defun sublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
668   #!+sb-doc
669   "Substitute from ALIST into TREE nondestructively."
670   (when (and testp notp)
671     (error ":TEST and :TEST-NOT were both supplied."))
672   (let ((key (and key (%coerce-callable-to-fun key)))
673         (test (if testp (%coerce-callable-to-fun test) test))
674         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
675     (declare (type function test test-not))
676     (declare (inline assoc))
677     (labels ((s (subtree)
678                (let* ((key-val (apply-key key subtree))
679                       (assoc (if notp
680                                  (assoc key-val alist :test-not test-not)
681                                  (assoc key-val alist :test test))))
682                  (cond (assoc (cdr assoc))
683                        ((atom subtree) subtree)
684                        (t (let ((car (s (car subtree)))
685                                 (cdr (s (cdr subtree))))
686                             (if (and (eq car (car subtree))
687                                      (eq cdr (cdr subtree)))
688                                 subtree
689                                 (cons car cdr))))))))
690       (s tree))))
691
692 ;;; This is in run-time env (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL))
693 ;;; because it can be referenced in inline expansions.
694 (defmacro nsublis-macro ()
695   (let ((key-tmp (gensym)))
696     `(let ((,key-tmp (apply-key key subtree)))
697       (if notp
698           (assoc ,key-tmp alist :test-not test-not)
699           (assoc ,key-tmp alist :test test)))))
700
701 (defun nsublis (alist tree &key key (test #'eql testp) (test-not #'eql notp))
702   #!+sb-doc
703   "Substitute from ALIST into TRUE destructively."
704   (when (and testp notp)
705     (error ":TEST and :TEST-NOT were both supplied."))
706   (let ((key (and key (%coerce-callable-to-fun key)))
707         (test (if testp (%coerce-callable-to-fun test) test))
708         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
709     (declare (inline assoc))
710     (let (temp)
711       (labels ((s (subtree)
712                  (cond ((setq temp (nsublis-macro))
713                         (cdr temp))
714                        ((atom subtree) subtree)
715                        (t (do* ((last nil subtree)
716                                 (subtree subtree (cdr subtree)))
717                                ((atom subtree)
718                                 (if (setq temp (nsublis-macro))
719                                     (setf (cdr last) (cdr temp))))
720                             (if (setq temp (nsublis-macro))
721                                 (return (setf (cdr last) (cdr temp)))
722                                 (setf (car subtree) (s (car subtree)))))
723                           subtree))))
724         (s tree)))))
725 \f
726 ;;;; functions for using lists as sets
727
728 (defun member (item list &key key (test #'eql testp) (test-not #'eql notp))
729   #!+sb-doc
730   "Return the tail of LIST beginning with first element satisfying EQLity,
731    :TEST, or :TEST-NOT with the given ITEM."
732   (when (and testp notp)
733     (error ":TEST and :TEST-NOT were both supplied."))
734   (let ((key (and key (%coerce-callable-to-fun key)))
735         (test (if testp (%coerce-callable-to-fun test) test))
736         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
737     (declare (type function test test-not))
738     (do ((list list (cdr list)))
739         ((null list) nil)
740       (let ((car (car list)))
741         (when (satisfies-the-test item car)
742           (return list))))))
743
744 (defun member-if (test list &key key)
745   #!+sb-doc
746   "Return tail of LIST beginning with first element satisfying TEST."
747   (let ((test (%coerce-callable-to-fun test))
748         (key (and key (%coerce-callable-to-fun key))))
749     (do ((list list (cdr list)))
750         ((endp list) nil)
751       (if (funcall test (apply-key key (car list)))
752           (return list)))))
753
754 (defun member-if-not (test list &key key)
755   #!+sb-doc
756   "Return tail of LIST beginning with first element not satisfying TEST."
757   (let ((test (%coerce-callable-to-fun test))
758         (key (and key (%coerce-callable-to-fun key))))
759     (do ((list list (cdr list)))
760         ((endp list) ())
761       (if (not (funcall test (apply-key key (car list))))
762           (return list)))))
763
764 (defun tailp (object list)
765   #!+sb-doc
766   "Return true if OBJECT is the same as some tail of LIST, otherwise
767    returns false. LIST must be a proper list or a dotted list."
768   (do ((list list (cdr list)))
769       ((atom list) (eql list object))
770     (if (eql object list)
771         (return t))))
772
773 (defun adjoin (item list &key key (test #'eql testp) (test-not nil notp))
774   #!+sb-doc
775   "Add ITEM to LIST unless it is already a member"
776   (when (and testp notp)
777     (error ":TEST and :TEST-NOT were both supplied."))
778   (let ((key (and key (%coerce-callable-to-fun key))))
779     (if (let ((key-val (apply-key key item)))
780           (if notp
781               (member key-val list :test-not test-not :key key)
782               (member key-val list :test test :key key)))
783         list
784         (cons item list))))
785
786 (define-compiler-macro adjoin (item list &rest keys)
787   (with-unique-names (n-item n-list)
788     `(let ((,n-item ,item)
789            (,n-list ,list))
790        (if (member ,n-item ,n-list ,@keys)
791            ,n-list
792            (cons ,n-item ,n-list)))))
793
794 (defconstant +list-based-union-limit+ 80)
795
796 (defun union (list1 list2 &key key (test #'eql testp) (test-not nil notp))
797   #!+sb-doc
798   "Return the union of LIST1 and LIST2."
799   (declare (inline member))
800   (when (and testp notp)
801     (error ":TEST and :TEST-NOT were both supplied."))
802   ;; We have to possibilities here: for shortish lists we pick up the
803   ;; shorter one as the result, and add the other one to it. For long
804   ;; lists we use a hash-table when possible.
805   (let ((n1 (length list1))
806         (n2 (length list2))
807         (key (and key (%coerce-callable-to-fun key)))
808         (test (if notp
809                   (let ((test-not-fun (%coerce-callable-to-fun test-not)))
810                     (lambda (x) (not (funcall test-not-fun x))))
811                   (%coerce-callable-to-fun test))))
812     (multiple-value-bind (short long n-short)
813         (if (< n1 n2)
814             (values list1 list2 n1)
815             (values list2 list1 n2))
816       (if (or (< n-short +list-based-union-limit+)
817               (not (member test (list #'eq #'eql #'equal #'equalp))))
818           (let ((orig short))
819             (dolist (elt long)
820               (unless (member (apply-key key elt) orig :key key :test test)
821                 (push elt short)))
822             short)
823           (let ((table (make-hash-table :test test :size (+ n1 n2)))
824                 (union nil))
825             (dolist (elt long)
826               (setf (gethash (apply-key key elt) table) elt))
827             (dolist (elt short)
828               (setf (gethash (apply-key key elt) table) elt))
829             (maphash (lambda (k v)
830                        (declare (ignore k))
831                        (push v union))
832                      table)
833             union)))))
834
835 ;;; Destination and source are SETF-able and many-evaluable. Set the
836 ;;; SOURCE to the CDR, and "cons" the 1st elt of source to DESTINATION.
837 ;;;
838 ;;; FIXME: needs a more mnemonic name
839 (defmacro steve-splice (source destination)
840   `(let ((temp ,source))
841      (setf ,source (cdr ,source)
842            (cdr temp) ,destination
843            ,destination temp)))
844
845 (defun nunion (list1 list2 &key key (test #'eql testp) (test-not nil notp))
846   #!+sb-doc
847   "Destructively return the union of LIST1 and LIST2."
848   (declare (inline member))
849   (when (and testp notp)
850     (error ":TEST and :TEST-NOT were both supplied."))
851   ;; We have to possibilities here: for shortish lists we pick up the
852   ;; shorter one as the result, and add the other one to it. For long
853   ;; lists we use a hash-table when possible.
854   (let ((n1 (length list1))
855         (n2 (length list2))
856         (key (and key (%coerce-callable-to-fun key)))
857         (test (if notp
858                   (let ((test-not-fun (%coerce-callable-to-fun test-not)))
859                     (lambda (x) (not (funcall test-not-fun x))))
860                   (%coerce-callable-to-fun test))))
861     (multiple-value-bind (short long n-short)
862         (if (< n1 n2)
863             (values list1 list2 n1)
864             (values list2 list1 n2))
865       (if (or (< n-short +list-based-union-limit+)
866               (not (member test (list #'eq #'eql #'equal #'equalp))))
867           (let ((orig short))
868             (do ((elt (car long) (car long)))
869                 ((endp long))
870               (if (not (member (apply-key key elt) orig :key key :test test))
871                   (steve-splice long short)
872                   (setf long (cdr long))))
873             short)
874           (let ((table (make-hash-table :test test :size (+ n1 n2))))
875             (dolist (elt long)
876               (setf (gethash (apply-key key elt) table) elt))
877             (dolist (elt short)
878               (setf (gethash (apply-key key elt) table) elt))
879             (let ((union long)
880                   (head long))
881               (maphash (lambda (k v)
882                          (declare (ignore k))
883                          (if head
884                              (setf (car head) v
885                                    head (cdr head))
886                              (push v union)))
887                       table)
888               union))))))
889
890 (defun intersection (list1 list2
891                      &key key (test #'eql testp) (test-not nil notp))
892   #!+sb-doc
893   "Return the intersection of LIST1 and LIST2."
894   (declare (inline member))
895   (when (and testp notp)
896     (error ":TEST and :TEST-NOT were both supplied."))
897   (let ((key (and key (%coerce-callable-to-fun key))))
898     (let ((res nil))
899       (dolist (elt list1)
900         (if (with-set-keys (member (apply-key key elt) list2))
901             (push elt res)))
902       res)))
903
904 (defun nintersection (list1 list2
905                       &key key (test #'eql testp) (test-not nil notp))
906   #!+sb-doc
907   "Destructively return the intersection of LIST1 and LIST2."
908   (declare (inline member))
909   (when (and testp notp)
910     (error ":TEST and :TEST-NOT were both supplied."))
911   (let ((key (and key (%coerce-callable-to-fun key))))
912     (let ((res nil)
913           (list1 list1))
914       (do () ((endp list1))
915         (if (with-set-keys (member (apply-key key (car list1)) list2))
916             (steve-splice list1 res)
917             (setq list1 (cdr list1))))
918       res)))
919
920 (defun set-difference (list1 list2
921                        &key key (test #'eql testp) (test-not nil notp))
922   #!+sb-doc
923   "Return the elements of LIST1 which are not in LIST2."
924   (declare (inline member))
925   (when (and testp notp)
926     (error ":TEST and :TEST-NOT were both supplied."))
927   (let ((key (and key (%coerce-callable-to-fun key))))
928     (if (null list2)
929         list1
930         (let ((res nil))
931           (dolist (elt list1)
932             (if (not (with-set-keys (member (apply-key key elt) list2)))
933                 (push elt res)))
934           res))))
935
936 (defun nset-difference (list1 list2
937                         &key key (test #'eql testp) (test-not nil notp))
938   #!+sb-doc
939   "Destructively return the elements of LIST1 which are not in LIST2."
940   (declare (inline member))
941   (when (and testp notp)
942     (error ":TEST and :TEST-NOT were both supplied."))
943   (let ((key (and key (%coerce-callable-to-fun key))))
944     (let ((res nil)
945           (list1 list1))
946       (do () ((endp list1))
947         (if (not (with-set-keys (member (apply-key key (car list1)) list2)))
948             (steve-splice list1 res)
949             (setq list1 (cdr list1))))
950       res)))
951
952 (defun set-exclusive-or (list1 list2
953                          &key key (test #'eql testp) (test-not #'eql notp))
954   #!+sb-doc
955   "Return new list of elements appearing exactly once in LIST1 and LIST2."
956   (declare (inline member))
957   (when (and testp notp)
958     (error ":TEST and :TEST-NOT were both supplied."))
959   (let ((result nil)
960         (key (and key (%coerce-callable-to-fun key)))
961         (test (if testp (%coerce-callable-to-fun test) test))
962         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
963     (declare (type function test test-not))
964     (dolist (elt list1)
965       (unless (with-set-keys (member (apply-key key elt) list2))
966         (setq result (cons elt result))))
967     (let ((test (if testp
968                     (lambda (x y) (funcall test y x))
969                     test))
970           (test-not (if notp
971                         (lambda (x y) (funcall test-not y x))
972                         test-not)))
973       (dolist (elt list2)
974         (unless (with-set-keys (member (apply-key key elt) list1))
975           (setq result (cons elt result)))))
976     result))
977
978 (defun nset-exclusive-or (list1 list2
979                           &key key (test #'eql testp) (test-not #'eql notp))
980   #!+sb-doc
981   "Destructively return a list with elements which appear but once in LIST1
982    and LIST2."
983   (when (and testp notp)
984     (error ":TEST and :TEST-NOT were both supplied."))
985   (let ((key (and key (%coerce-callable-to-fun key)))
986         (test (if testp (%coerce-callable-to-fun test) test))
987         (test-not (if notp (%coerce-callable-to-fun test-not) test-not)))
988     (declare (type function test test-not))
989     ;; The outer loop examines LIST1 while the inner loop examines
990     ;; LIST2. If an element is found in LIST2 "equal" to the element
991     ;; in LIST1, both are spliced out. When the end of LIST1 is
992     ;; reached, what is left of LIST2 is tacked onto what is left of
993     ;; LIST1. The splicing operation ensures that the correct
994     ;; operation is performed depending on whether splice is at the
995     ;; top of the list or not.
996     (do ((list1 list1)
997          (list2 list2)
998          (x list1 (cdr x))
999          (splicex ())
1000          (deleted-y ())
1001          ;; elements of LIST2, which are "equal" to some processed
1002          ;; earlier elements of LIST1
1003          )
1004         ((endp x)
1005          (if (null splicex)
1006              (setq list1 list2)
1007              (rplacd splicex list2))
1008          list1)
1009       (let ((key-val-x (apply-key key (car x)))
1010             (found-duplicate nil))
1011
1012         ;; Move all elements from LIST2, which are "equal" to (CAR X),
1013         ;; to DELETED-Y.
1014         (do* ((y list2 next-y)
1015               (next-y (cdr y) (cdr y))
1016               (splicey ()))
1017              ((endp y))
1018           (cond ((let ((key-val-y (apply-key key (car y))))
1019                    (if notp
1020                        (not (funcall test-not key-val-x key-val-y))
1021                        (funcall test key-val-x key-val-y)))
1022                  (if (null splicey)
1023                      (setq list2 (cdr y))
1024                      (rplacd splicey (cdr y)))
1025                  (setq deleted-y (rplacd y deleted-y))
1026                  (setq found-duplicate t))
1027                 (t (setq splicey y))))
1028
1029         (unless found-duplicate
1030           (setq found-duplicate (with-set-keys (member key-val-x deleted-y))))
1031
1032         (if found-duplicate
1033             (if (null splicex)
1034                 (setq list1 (cdr x))
1035                 (rplacd splicex (cdr x)))
1036             (setq splicex x))))))
1037
1038 (defun subsetp (list1 list2 &key key (test #'eql testp) (test-not nil notp))
1039   #!+sb-doc
1040   "Return T if every element in LIST1 is also in LIST2."
1041   (declare (inline member))
1042   (when (and testp notp)
1043     (error ":TEST and :TEST-NOT were both supplied."))
1044   (let ((key (and key (%coerce-callable-to-fun key))))
1045     (dolist (elt list1)
1046       (unless (with-set-keys (member (apply-key key elt) list2))
1047         (return-from subsetp nil)))
1048     t))
1049 \f
1050 ;;;; functions that operate on association lists
1051
1052 (defun acons (key datum alist)
1053   #!+sb-doc
1054   "Construct a new alist by adding the pair (KEY . DATUM) to ALIST."
1055   (cons (cons key datum) alist))
1056
1057 (defun pairlis (keys data &optional (alist '()))
1058   #!+sb-doc
1059   "Construct an association list from KEYS and DATA (adding to ALIST)."
1060   (do ((x keys (cdr x))
1061        (y data (cdr y)))
1062       ((and (endp x) (endp y)) alist)
1063     (if (or (endp x) (endp y))
1064         (error "The lists of keys and data are of unequal length."))
1065     (setq alist (acons (car x) (car y) alist))))
1066
1067 ;;; This is defined in the run-time environment, not just the compile-time
1068 ;;; environment (i.e. not wrapped in EVAL-WHEN (COMPILE EVAL)) because it
1069 ;;; can appear in inline expansions.
1070 (defmacro assoc-guts (test-expr)
1071   `(do ((alist alist (cdr alist)))
1072        ((endp alist))
1073     (when (and (car alist) ,test-expr)
1074       (return (car alist)))))
1075
1076 (defun assoc (item alist &key key (test nil testp) (test-not nil notp))
1077   #!+sb-doc
1078   "Return the cons in ALIST whose car is equal (by a given test or EQL) to
1079    the ITEM."
1080   (when (and testp notp)
1081     (error ":TEST and :TEST-NOT were both supplied."))
1082   (let ((key (and key (%coerce-callable-to-fun key)))
1083         (test (and testp (%coerce-callable-to-fun test)))
1084         (test-not (and notp (%coerce-callable-to-fun test-not))))
1085     (cond (test
1086            (if key
1087                (assoc-guts (funcall test item (funcall key (caar alist))))
1088                (assoc-guts (funcall test item (caar alist)))))
1089           (test-not
1090            (if key
1091                (assoc-guts (not (funcall test-not item
1092                                          (funcall key (caar alist)))))
1093                (assoc-guts (not (funcall test-not item (caar alist))))))
1094           (t
1095            (if key
1096                (assoc-guts (eql item (funcall key (caar alist))))
1097                (assoc-guts (eql item (caar alist))))))))
1098
1099 (defun assoc-if (predicate alist &key key)
1100   #!+sb-doc
1101   "Return the first cons in ALIST whose CAR satisfies PREDICATE. If
1102    KEY is supplied, apply it to the CAR of each cons before testing."
1103   (let ((predicate (%coerce-callable-to-fun predicate))
1104         (key (and key (%coerce-callable-to-fun key))))
1105     (if key
1106         (assoc-guts (funcall predicate (funcall key (caar alist))))
1107         (assoc-guts (funcall predicate (caar alist))))))
1108
1109 (defun assoc-if-not (predicate alist &key key)
1110   #!+sb-doc
1111   "Return the first cons in ALIST whose CAR does not satisfy PREDICATE.
1112   If KEY is supplied, apply it to the CAR of each cons before testing."
1113   (let ((predicate (%coerce-callable-to-fun predicate))
1114         (key (and key (%coerce-callable-to-fun key))))
1115     (if key
1116         (assoc-guts (not (funcall predicate (funcall key (caar alist)))))
1117         (assoc-guts (not (funcall predicate (caar alist)))))))
1118
1119 (defun rassoc (item alist &key key (test nil testp) (test-not nil notp))
1120   (declare (list alist))
1121   #!+sb-doc
1122   "Return the cons in ALIST whose CDR is equal (by a given test or EQL) to
1123    the ITEM."
1124   (when (and testp notp)
1125     (error ":TEST and :TEST-NOT were both supplied."))
1126   (let ((key (and key (%coerce-callable-to-fun key)))
1127         (test (and testp (%coerce-callable-to-fun test)))
1128         (test-not (and notp (%coerce-callable-to-fun test-not))))
1129     (cond (test
1130            (if key
1131                (assoc-guts (funcall test item (funcall key (cdar alist))))
1132                (assoc-guts (funcall test item (cdar alist)))))
1133           (test-not
1134            (if key
1135                (assoc-guts (not (funcall test-not item
1136                                          (funcall key (cdar alist)))))
1137                (assoc-guts (not (funcall test-not item (cdar alist))))))
1138           (t
1139            (if key
1140                (assoc-guts (eql item (funcall key (cdar alist))))
1141                (assoc-guts (eql item (cdar alist))))))))
1142
1143 (defun rassoc-if (predicate alist &key key)
1144   #!+sb-doc
1145   "Return the first cons in ALIST whose CDR satisfies PREDICATE. If KEY
1146   is supplied, apply it to the CDR of each cons before testing."
1147   (let ((predicate (%coerce-callable-to-fun predicate))
1148         (key (and key (%coerce-callable-to-fun key))))
1149     (if key
1150         (assoc-guts (funcall predicate (funcall key (cdar alist))))
1151         (assoc-guts (funcall predicate (cdar alist))))))
1152
1153 (defun rassoc-if-not (predicate alist &key key)
1154   #!+sb-doc
1155   "Return the first cons in ALIST whose CDR does not satisfy PREDICATE.
1156   If KEY is supplied, apply it to the CDR of each cons before testing."
1157   (let ((predicate (%coerce-callable-to-fun predicate))
1158         (key (and key (%coerce-callable-to-fun key))))
1159     (if key
1160         (assoc-guts (not (funcall predicate (funcall key (cdar alist)))))
1161         (assoc-guts (not (funcall predicate (cdar alist)))))))
1162 \f
1163 ;;;; mapping functions
1164
1165 ;;; a helper function for implementation of MAPC, MAPCAR, MAPCAN,
1166 ;;; MAPL, MAPLIST, and MAPCON
1167 ;;;
1168 ;;; Map the designated function over the arglists in the appropriate
1169 ;;; way. It is done when any of the arglists runs out. Until then, it
1170 ;;; CDRs down the arglists calling the function and accumulating
1171 ;;; results as desired.
1172 (defun map1 (fun-designator original-arglists accumulate take-car)
1173   (let ((fun (%coerce-callable-to-fun fun-designator)))
1174     (let* ((arglists (copy-list original-arglists))
1175            (ret-list (list nil))
1176            (temp ret-list))
1177       (do ((res nil)
1178            (args '() '()))
1179           ((dolist (x arglists nil) (if (null x) (return t)))
1180            (if accumulate
1181                (cdr ret-list)
1182                (car original-arglists)))
1183         (do ((l arglists (cdr l)))
1184             ((null l))
1185           (push (if take-car (caar l) (car l)) args)
1186           (setf (car l) (cdar l)))
1187         (setq res (apply fun (nreverse args)))
1188         (case accumulate
1189           (:nconc (setq temp (last (nconc temp res))))
1190           (:list (rplacd temp (list res))
1191                  (setq temp (cdr temp))))))))
1192
1193 (defun mapc (function list &rest more-lists)
1194   #!+sb-doc
1195   "Apply FUNCTION to successive elements of lists. Return the second argument."
1196   (map1 function (cons list more-lists) nil t))
1197
1198 (defun mapcar (function list &rest more-lists)
1199   #!+sb-doc
1200   "Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
1201    return values."
1202   (map1 function (cons list more-lists) :list t))
1203
1204 (defun mapcan (function list &rest more-lists)
1205   #!+sb-doc
1206   "Apply FUNCTION to successive elements of LIST. Return NCONC of FUNCTION
1207    results."
1208   (map1 function (cons list more-lists) :nconc t))
1209
1210 (defun mapl (function list &rest more-lists)
1211   #!+sb-doc
1212   "Apply FUNCTION to successive CDRs of list. Return NIL."
1213   (map1 function (cons list more-lists) nil nil))
1214
1215 (defun maplist (function list &rest more-lists)
1216   #!+sb-doc
1217   "Apply FUNCTION to successive CDRs of list. Return list of results."
1218   (map1 function (cons list more-lists) :list nil))
1219
1220 (defun mapcon (function list &rest more-lists)
1221   #!+sb-doc
1222   "Apply FUNCTION to successive CDRs of lists. Return NCONC of results."
1223   (map1 function (cons list more-lists) :nconc nil))
1224
1225 ;;;; Specialized versions
1226
1227 ;;; %MEMBER-* and %ASSOC-* functions. The transforms for MEMBER and
1228 ;;; ASSOC pick the appropriate version. These win because they have
1229 ;;; only positional arguments, the TEST, TEST-NOT & KEY functions are
1230 ;;; known to exist (or not), and are known to be functions instead of
1231 ;;; function designators. We are also able to transform many common
1232 ;;; cases to -EQ versions, which are substantially faster then EQL
1233 ;;; using ones.
1234 (macrolet
1235     ((def (funs form &optional variant)
1236        (flet ((%def (name)
1237                 `(defun ,(intern (format nil "%~A~{-~A~}~@[-~A~]" name funs variant))
1238                      (item list ,@funs)
1239                    (declare (optimize speed))
1240                    ,@(when funs `((declare (function ,@funs))))
1241                    (do ((list list (cdr list)))
1242                        ((null list) nil)
1243                      (declare (list list))
1244                      (let ((this (car list)))
1245                        ,(ecase name
1246                                (assoc
1247                                 (if funs
1248                                     `(when this
1249                                        (let ((target (car this)))
1250                                          (when ,form
1251                                            (return this))))
1252                                     ;; If there is no TEST/TEST-NOT or
1253                                     ;; KEY, do the EQ/EQL test first,
1254                                     ;; before checking for NIL.
1255                                     `(let ((target (car this)))
1256                                        (when (and ,form this)
1257                                          (return this)))))
1258                                (member
1259                                 `(let ((target this))
1260                                    (when ,form
1261                                      (return list))))))))))
1262          `(progn
1263             ,(%def 'member)
1264             ,(%def 'assoc)))))
1265   (def ()
1266       (eql item target))
1267   (def ()
1268       (eq item target)
1269     eq)
1270   (def (key)
1271       (eql item (funcall key target)))
1272   (def (key)
1273       (eq item (funcall key target))
1274     eq)
1275   (def (key test)
1276       (funcall test item (funcall key target)))
1277   (def (key test-not)
1278       (not (funcall test-not item (funcall key target))))
1279   (def (test)
1280       (funcall test item target))
1281   (def (test-not)
1282     (not (funcall test-not item target))))