PageRenderTime 78ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/code/list.lisp

http://github.com/dmitryvk/sbcl-win32-threads
Lisp | 1388 lines | 1195 code | 88 blank | 105 comment | 30 complexity | 569ff72dff10725b7f69b8c8cd446d32 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file