cl-seq.el 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. ;;; cl-seq.el --- Common Lisp features, part 3 -*- lexical-binding: t -*-
  2. ;; Copyright (C) 1993, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Dave Gillespie <daveg@synaptics.com>
  4. ;; Old-Version: 2.02
  5. ;; Keywords: extensions
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; These are extensions to Emacs Lisp that provide a degree of
  20. ;; Common Lisp compatibility, beyond what is already built-in
  21. ;; in Emacs Lisp.
  22. ;;
  23. ;; This package was written by Dave Gillespie; it is a complete
  24. ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
  25. ;;
  26. ;; Bug reports, comments, and suggestions are welcome!
  27. ;; This file contains the Common Lisp sequence and list functions
  28. ;; which take keyword arguments.
  29. ;; See cl.el for Change Log.
  30. ;;; Code:
  31. (require 'cl-lib)
  32. ;; Keyword parsing.
  33. ;; This is special-cased here so that we can compile
  34. ;; this file independent from cl-macs.
  35. (defmacro cl--parsing-keywords (kwords other-keys &rest body)
  36. (declare (indent 2) (debug (sexp sexp &rest form)))
  37. `(let* ,(mapcar
  38. (lambda (x)
  39. (let* ((var (if (consp x) (car x) x))
  40. (mem `(car (cdr (memq ',var cl-keys)))))
  41. (if (eq var :test-not)
  42. (setq mem `(and ,mem (setq cl-test ,mem) t)))
  43. (if (eq var :if-not)
  44. (setq mem `(and ,mem (setq cl-if ,mem) t)))
  45. (list (intern
  46. (format "cl-%s" (substring (symbol-name var) 1)))
  47. (if (consp x) `(or ,mem ,(car (cdr x))) mem))))
  48. kwords)
  49. ,@(append
  50. (and (not (eq other-keys t))
  51. (list
  52. (list 'let '((cl-keys-temp cl-keys))
  53. (list 'while 'cl-keys-temp
  54. (list 'or (list 'memq '(car cl-keys-temp)
  55. (list 'quote
  56. (mapcar
  57. (function
  58. (lambda (x)
  59. (if (consp x)
  60. (car x) x)))
  61. (append kwords
  62. other-keys))))
  63. '(car (cdr (memq (quote :allow-other-keys)
  64. cl-keys)))
  65. '(error "Bad keyword argument %s"
  66. (car cl-keys-temp)))
  67. '(setq cl-keys-temp (cdr (cdr cl-keys-temp)))))))
  68. body)))
  69. (defmacro cl--check-key (x) ;Expects `cl-key' in context of generated code.
  70. (declare (debug edebug-forms))
  71. `(if cl-key (funcall cl-key ,x) ,x))
  72. (defmacro cl--check-test-nokey (item x) ;cl-test cl-if cl-test-not cl-if-not.
  73. (declare (debug edebug-forms))
  74. `(cond
  75. (cl-test (eq (not (funcall cl-test ,item ,x))
  76. cl-test-not))
  77. (cl-if (eq (not (funcall cl-if ,x)) cl-if-not))
  78. (t (eql ,item ,x))))
  79. (defmacro cl--check-test (item x) ;all of the above.
  80. (declare (debug edebug-forms))
  81. `(cl--check-test-nokey ,item (cl--check-key ,x)))
  82. (defmacro cl--check-match (x y) ;cl-key cl-test cl-test-not
  83. (declare (debug edebug-forms))
  84. (setq x `(cl--check-key ,x) y `(cl--check-key ,y))
  85. `(if cl-test
  86. (eq (not (funcall cl-test ,x ,y)) cl-test-not)
  87. (eql ,x ,y)))
  88. ;; Yuck! These vars are set/bound by cl--parsing-keywords to match :if :test
  89. ;; and :key keyword args, and they are also accessed (sometimes) via dynamic
  90. ;; scoping (and some of those accesses are from macro-expanded code).
  91. (defvar cl-test) (defvar cl-test-not)
  92. (defvar cl-if) (defvar cl-if-not)
  93. (defvar cl-key)
  94. ;;;###autoload
  95. (defun cl-reduce (cl-func cl-seq &rest cl-keys)
  96. "Reduce two-argument FUNCTION across SEQ.
  97. \nKeywords supported: :start :end :from-end :initial-value :key
  98. \n(fn FUNCTION SEQ [KEYWORD VALUE]...)"
  99. (cl--parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
  100. (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
  101. (setq cl-seq (cl-subseq cl-seq cl-start cl-end))
  102. (if cl-from-end (setq cl-seq (nreverse cl-seq)))
  103. (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
  104. (cl-seq (cl--check-key (pop cl-seq)))
  105. (t (funcall cl-func)))))
  106. (if cl-from-end
  107. (while cl-seq
  108. (setq cl-accum (funcall cl-func (cl--check-key (pop cl-seq))
  109. cl-accum)))
  110. (while cl-seq
  111. (setq cl-accum (funcall cl-func cl-accum
  112. (cl--check-key (pop cl-seq))))))
  113. cl-accum)))
  114. ;;;###autoload
  115. (defun cl-fill (seq item &rest cl-keys)
  116. "Fill the elements of SEQ with ITEM.
  117. \nKeywords supported: :start :end
  118. \n(fn SEQ ITEM [KEYWORD VALUE]...)"
  119. (cl--parsing-keywords ((:start 0) :end) ()
  120. (if (listp seq)
  121. (let ((p (nthcdr cl-start seq))
  122. (n (if cl-end (- cl-end cl-start) 8000000)))
  123. (while (and p (>= (setq n (1- n)) 0))
  124. (setcar p item)
  125. (setq p (cdr p))))
  126. (or cl-end (setq cl-end (length seq)))
  127. (if (and (= cl-start 0) (= cl-end (length seq)))
  128. (fillarray seq item)
  129. (while (< cl-start cl-end)
  130. (aset seq cl-start item)
  131. (setq cl-start (1+ cl-start)))))
  132. seq))
  133. ;;;###autoload
  134. (defun cl-replace (cl-seq1 cl-seq2 &rest cl-keys)
  135. "Replace the elements of SEQ1 with the elements of SEQ2.
  136. SEQ1 is destructively modified, then returned.
  137. \nKeywords supported: :start1 :end1 :start2 :end2
  138. \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
  139. (cl--parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) ()
  140. (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1))
  141. (or (= cl-start1 cl-start2)
  142. (let* ((cl-len (length cl-seq1))
  143. (cl-n (min (- (or cl-end1 cl-len) cl-start1)
  144. (- (or cl-end2 cl-len) cl-start2))))
  145. (while (>= (setq cl-n (1- cl-n)) 0)
  146. (setf (elt cl-seq1 (+ cl-start1 cl-n))
  147. (elt cl-seq2 (+ cl-start2 cl-n))))))
  148. (if (listp cl-seq1)
  149. (let ((cl-p1 (nthcdr cl-start1 cl-seq1))
  150. (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000)))
  151. (if (listp cl-seq2)
  152. (let ((cl-p2 (nthcdr cl-start2 cl-seq2))
  153. (cl-n (min cl-n1
  154. (if cl-end2 (- cl-end2 cl-start2) 4000000))))
  155. (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0))
  156. (setcar cl-p1 (car cl-p2))
  157. (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))))
  158. (setq cl-end2 (min (or cl-end2 (length cl-seq2))
  159. (+ cl-start2 cl-n1)))
  160. (while (and cl-p1 (< cl-start2 cl-end2))
  161. (setcar cl-p1 (aref cl-seq2 cl-start2))
  162. (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2)))))
  163. (setq cl-end1 (min (or cl-end1 (length cl-seq1))
  164. (+ cl-start1 (- (or cl-end2 (length cl-seq2))
  165. cl-start2))))
  166. (if (listp cl-seq2)
  167. (let ((cl-p2 (nthcdr cl-start2 cl-seq2)))
  168. (while (< cl-start1 cl-end1)
  169. (aset cl-seq1 cl-start1 (car cl-p2))
  170. (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1))))
  171. (while (< cl-start1 cl-end1)
  172. (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2))
  173. (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1))))))
  174. cl-seq1))
  175. ;;;###autoload
  176. (defun cl-remove (cl-item cl-seq &rest cl-keys)
  177. "Remove all occurrences of ITEM in SEQ.
  178. This is a non-destructive function; it makes a copy of SEQ if necessary
  179. to avoid corrupting the original SEQ.
  180. \nKeywords supported: :test :test-not :key :count :start :end :from-end
  181. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  182. (cl--parsing-keywords (:test :test-not :key :if :if-not :count :from-end
  183. (:start 0) :end) ()
  184. (if (<= (or cl-count (setq cl-count 8000000)) 0)
  185. cl-seq
  186. (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000)))
  187. (let ((cl-i (cl--position cl-item cl-seq cl-start cl-end
  188. cl-from-end)))
  189. (if cl-i
  190. (let ((cl-res (apply 'cl-delete cl-item (append cl-seq nil)
  191. (append (if cl-from-end
  192. (list :end (1+ cl-i))
  193. (list :start cl-i))
  194. cl-keys))))
  195. (if (listp cl-seq) cl-res
  196. (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))
  197. cl-seq))
  198. (setq cl-end (- (or cl-end 8000000) cl-start))
  199. (if (= cl-start 0)
  200. (while (and cl-seq (> cl-end 0)
  201. (cl--check-test cl-item (car cl-seq))
  202. (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
  203. (> (setq cl-count (1- cl-count)) 0))))
  204. (if (and (> cl-count 0) (> cl-end 0))
  205. (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq)
  206. (setq cl-end (1- cl-end)) (cdr cl-seq))))
  207. (while (and cl-p (> cl-end 0)
  208. (not (cl--check-test cl-item (car cl-p))))
  209. (setq cl-p (cdr cl-p) cl-end (1- cl-end)))
  210. (if (and cl-p (> cl-end 0))
  211. (nconc (cl-ldiff cl-seq cl-p)
  212. (if (= cl-count 1) (cdr cl-p)
  213. (and (cdr cl-p)
  214. (apply 'cl-delete cl-item
  215. (copy-sequence (cdr cl-p))
  216. :start 0 :end (1- cl-end)
  217. :count (1- cl-count) cl-keys))))
  218. cl-seq))
  219. cl-seq)))))
  220. ;;;###autoload
  221. (defun cl-remove-if (cl-pred cl-list &rest cl-keys)
  222. "Remove all items satisfying PREDICATE in SEQ.
  223. This is a non-destructive function; it makes a copy of SEQ if necessary
  224. to avoid corrupting the original SEQ.
  225. \nKeywords supported: :key :count :start :end :from-end
  226. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  227. (apply 'cl-remove nil cl-list :if cl-pred cl-keys))
  228. ;;;###autoload
  229. (defun cl-remove-if-not (cl-pred cl-list &rest cl-keys)
  230. "Remove all items not satisfying PREDICATE in SEQ.
  231. This is a non-destructive function; it makes a copy of SEQ if necessary
  232. to avoid corrupting the original SEQ.
  233. \nKeywords supported: :key :count :start :end :from-end
  234. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  235. (apply 'cl-remove nil cl-list :if-not cl-pred cl-keys))
  236. ;;;###autoload
  237. (defun cl-delete (cl-item cl-seq &rest cl-keys)
  238. "Remove all occurrences of ITEM in SEQ.
  239. This is a destructive function; it reuses the storage of SEQ whenever possible.
  240. \nKeywords supported: :test :test-not :key :count :start :end :from-end
  241. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  242. (cl--parsing-keywords (:test :test-not :key :if :if-not :count :from-end
  243. (:start 0) :end) ()
  244. (if (<= (or cl-count (setq cl-count 8000000)) 0)
  245. cl-seq
  246. (if (listp cl-seq)
  247. (if (and cl-from-end (< cl-count 4000000))
  248. (let (cl-i)
  249. (while (and (>= (setq cl-count (1- cl-count)) 0)
  250. (setq cl-i (cl--position cl-item cl-seq cl-start
  251. cl-end cl-from-end)))
  252. (if (= cl-i 0) (setq cl-seq (cdr cl-seq))
  253. (let ((cl-tail (nthcdr (1- cl-i) cl-seq)))
  254. (setcdr cl-tail (cdr (cdr cl-tail)))))
  255. (setq cl-end cl-i))
  256. cl-seq)
  257. (setq cl-end (- (or cl-end 8000000) cl-start))
  258. (if (= cl-start 0)
  259. (progn
  260. (while (and cl-seq
  261. (> cl-end 0)
  262. (cl--check-test cl-item (car cl-seq))
  263. (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
  264. (> (setq cl-count (1- cl-count)) 0)))
  265. (setq cl-end (1- cl-end)))
  266. (setq cl-start (1- cl-start)))
  267. (if (and (> cl-count 0) (> cl-end 0))
  268. (let ((cl-p (nthcdr cl-start cl-seq)))
  269. (while (and (cdr cl-p) (> cl-end 0))
  270. (if (cl--check-test cl-item (car (cdr cl-p)))
  271. (progn
  272. (setcdr cl-p (cdr (cdr cl-p)))
  273. (if (= (setq cl-count (1- cl-count)) 0)
  274. (setq cl-end 1)))
  275. (setq cl-p (cdr cl-p)))
  276. (setq cl-end (1- cl-end)))))
  277. cl-seq)
  278. (apply 'cl-remove cl-item cl-seq cl-keys)))))
  279. ;;;###autoload
  280. (defun cl-delete-if (cl-pred cl-list &rest cl-keys)
  281. "Remove all items satisfying PREDICATE in SEQ.
  282. This is a destructive function; it reuses the storage of SEQ whenever possible.
  283. \nKeywords supported: :key :count :start :end :from-end
  284. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  285. (apply 'cl-delete nil cl-list :if cl-pred cl-keys))
  286. ;;;###autoload
  287. (defun cl-delete-if-not (cl-pred cl-list &rest cl-keys)
  288. "Remove all items not satisfying PREDICATE in SEQ.
  289. This is a destructive function; it reuses the storage of SEQ whenever possible.
  290. \nKeywords supported: :key :count :start :end :from-end
  291. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  292. (apply 'cl-delete nil cl-list :if-not cl-pred cl-keys))
  293. ;;;###autoload
  294. (defun cl-remove-duplicates (cl-seq &rest cl-keys)
  295. "Return a copy of SEQ with all duplicate elements removed.
  296. \nKeywords supported: :test :test-not :key :start :end :from-end
  297. \n(fn SEQ [KEYWORD VALUE]...)"
  298. (cl--delete-duplicates cl-seq cl-keys t))
  299. ;;;###autoload
  300. (defun cl-delete-duplicates (cl-seq &rest cl-keys)
  301. "Remove all duplicate elements from SEQ (destructively).
  302. \nKeywords supported: :test :test-not :key :start :end :from-end
  303. \n(fn SEQ [KEYWORD VALUE]...)"
  304. (cl--delete-duplicates cl-seq cl-keys nil))
  305. (defun cl--delete-duplicates (cl-seq cl-keys cl-copy)
  306. (if (listp cl-seq)
  307. (cl--parsing-keywords
  308. (:test :test-not :key (:start 0) :end :from-end :if)
  309. ()
  310. (if cl-from-end
  311. (let ((cl-p (nthcdr cl-start cl-seq)) cl-i)
  312. (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
  313. (while (> cl-end 1)
  314. (setq cl-i 0)
  315. (while (setq cl-i (cl--position (cl--check-key (car cl-p))
  316. (cdr cl-p) cl-i (1- cl-end)))
  317. (if cl-copy (setq cl-seq (copy-sequence cl-seq)
  318. cl-p (nthcdr cl-start cl-seq) cl-copy nil))
  319. (let ((cl-tail (nthcdr cl-i cl-p)))
  320. (setcdr cl-tail (cdr (cdr cl-tail))))
  321. (setq cl-end (1- cl-end)))
  322. (setq cl-p (cdr cl-p) cl-end (1- cl-end)
  323. cl-start (1+ cl-start)))
  324. cl-seq)
  325. (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
  326. (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1)
  327. (cl--position (cl--check-key (car cl-seq))
  328. (cdr cl-seq) 0 (1- cl-end)))
  329. (setq cl-seq (cdr cl-seq) cl-end (1- cl-end)))
  330. (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq)
  331. (setq cl-end (1- cl-end) cl-start 1) cl-seq)))
  332. (while (and (cdr (cdr cl-p)) (> cl-end 1))
  333. (if (cl--position (cl--check-key (car (cdr cl-p)))
  334. (cdr (cdr cl-p)) 0 (1- cl-end))
  335. (progn
  336. (if cl-copy (setq cl-seq (copy-sequence cl-seq)
  337. cl-p (nthcdr (1- cl-start) cl-seq)
  338. cl-copy nil))
  339. (setcdr cl-p (cdr (cdr cl-p))))
  340. (setq cl-p (cdr cl-p)))
  341. (setq cl-end (1- cl-end) cl-start (1+ cl-start)))
  342. cl-seq)))
  343. (let ((cl-res (cl--delete-duplicates (append cl-seq nil) cl-keys nil)))
  344. (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))))
  345. ;;;###autoload
  346. (defun cl-substitute (cl-new cl-old cl-seq &rest cl-keys)
  347. "Substitute NEW for OLD in SEQ.
  348. This is a non-destructive function; it makes a copy of SEQ if necessary
  349. to avoid corrupting the original SEQ.
  350. \nKeywords supported: :test :test-not :key :count :start :end :from-end
  351. \n(fn NEW OLD SEQ [KEYWORD VALUE]...)"
  352. (cl--parsing-keywords (:test :test-not :key :if :if-not :count
  353. (:start 0) :end :from-end) ()
  354. (if (or (eq cl-old cl-new)
  355. (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0))
  356. cl-seq
  357. (let ((cl-i (cl--position cl-old cl-seq cl-start cl-end)))
  358. (if (not cl-i)
  359. cl-seq
  360. (setq cl-seq (copy-sequence cl-seq))
  361. (or cl-from-end
  362. (progn (setf (elt cl-seq cl-i) cl-new)
  363. (setq cl-i (1+ cl-i) cl-count (1- cl-count))))
  364. (apply 'cl-nsubstitute cl-new cl-old cl-seq :count cl-count
  365. :start cl-i cl-keys))))))
  366. ;;;###autoload
  367. (defun cl-substitute-if (cl-new cl-pred cl-list &rest cl-keys)
  368. "Substitute NEW for all items satisfying PREDICATE in SEQ.
  369. This is a non-destructive function; it makes a copy of SEQ if necessary
  370. to avoid corrupting the original SEQ.
  371. \nKeywords supported: :key :count :start :end :from-end
  372. \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
  373. (apply 'cl-substitute cl-new nil cl-list :if cl-pred cl-keys))
  374. ;;;###autoload
  375. (defun cl-substitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
  376. "Substitute NEW for all items not satisfying PREDICATE in SEQ.
  377. This is a non-destructive function; it makes a copy of SEQ if necessary
  378. to avoid corrupting the original SEQ.
  379. \nKeywords supported: :key :count :start :end :from-end
  380. \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
  381. (apply 'cl-substitute cl-new nil cl-list :if-not cl-pred cl-keys))
  382. ;;;###autoload
  383. (defun cl-nsubstitute (cl-new cl-old cl-seq &rest cl-keys)
  384. "Substitute NEW for OLD in SEQ.
  385. This is a destructive function; it reuses the storage of SEQ whenever possible.
  386. \nKeywords supported: :test :test-not :key :count :start :end :from-end
  387. \n(fn NEW OLD SEQ [KEYWORD VALUE]...)"
  388. (cl--parsing-keywords (:test :test-not :key :if :if-not :count
  389. (:start 0) :end :from-end) ()
  390. (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0)
  391. (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000)))
  392. (let ((cl-p (nthcdr cl-start cl-seq)))
  393. (setq cl-end (- (or cl-end 8000000) cl-start))
  394. (while (and cl-p (> cl-end 0) (> cl-count 0))
  395. (if (cl--check-test cl-old (car cl-p))
  396. (progn
  397. (setcar cl-p cl-new)
  398. (setq cl-count (1- cl-count))))
  399. (setq cl-p (cdr cl-p) cl-end (1- cl-end))))
  400. (or cl-end (setq cl-end (length cl-seq)))
  401. (if cl-from-end
  402. (while (and (< cl-start cl-end) (> cl-count 0))
  403. (setq cl-end (1- cl-end))
  404. (if (cl--check-test cl-old (elt cl-seq cl-end))
  405. (progn
  406. (setf (elt cl-seq cl-end) cl-new)
  407. (setq cl-count (1- cl-count)))))
  408. (while (and (< cl-start cl-end) (> cl-count 0))
  409. (if (cl--check-test cl-old (aref cl-seq cl-start))
  410. (progn
  411. (aset cl-seq cl-start cl-new)
  412. (setq cl-count (1- cl-count))))
  413. (setq cl-start (1+ cl-start))))))
  414. cl-seq))
  415. ;;;###autoload
  416. (defun cl-nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys)
  417. "Substitute NEW for all items satisfying PREDICATE in SEQ.
  418. This is a destructive function; it reuses the storage of SEQ whenever possible.
  419. \nKeywords supported: :key :count :start :end :from-end
  420. \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
  421. (apply 'cl-nsubstitute cl-new nil cl-list :if cl-pred cl-keys))
  422. ;;;###autoload
  423. (defun cl-nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
  424. "Substitute NEW for all items not satisfying PREDICATE in SEQ.
  425. This is a destructive function; it reuses the storage of SEQ whenever possible.
  426. \nKeywords supported: :key :count :start :end :from-end
  427. \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
  428. (apply 'cl-nsubstitute cl-new nil cl-list :if-not cl-pred cl-keys))
  429. ;;;###autoload
  430. (defun cl-find (cl-item cl-seq &rest cl-keys)
  431. "Find the first occurrence of ITEM in SEQ.
  432. Return the matching ITEM, or nil if not found.
  433. \nKeywords supported: :test :test-not :key :start :end :from-end
  434. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  435. (let ((cl-pos (apply 'cl-position cl-item cl-seq cl-keys)))
  436. (and cl-pos (elt cl-seq cl-pos))))
  437. ;;;###autoload
  438. (defun cl-find-if (cl-pred cl-list &rest cl-keys)
  439. "Find the first item satisfying PREDICATE in SEQ.
  440. Return the matching item, or nil if not found.
  441. \nKeywords supported: :key :start :end :from-end
  442. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  443. (apply 'cl-find nil cl-list :if cl-pred cl-keys))
  444. ;;;###autoload
  445. (defun cl-find-if-not (cl-pred cl-list &rest cl-keys)
  446. "Find the first item not satisfying PREDICATE in SEQ.
  447. Return the matching item, or nil if not found.
  448. \nKeywords supported: :key :start :end :from-end
  449. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  450. (apply 'cl-find nil cl-list :if-not cl-pred cl-keys))
  451. ;;;###autoload
  452. (defun cl-position (cl-item cl-seq &rest cl-keys)
  453. "Find the first occurrence of ITEM in SEQ.
  454. Return the index of the matching item, or nil if not found.
  455. \nKeywords supported: :test :test-not :key :start :end :from-end
  456. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  457. (cl--parsing-keywords (:test :test-not :key :if :if-not
  458. (:start 0) :end :from-end) ()
  459. (cl--position cl-item cl-seq cl-start cl-end cl-from-end)))
  460. (defun cl--position (cl-item cl-seq cl-start &optional cl-end cl-from-end)
  461. (if (listp cl-seq)
  462. (let ((cl-p (nthcdr cl-start cl-seq)))
  463. (or cl-end (setq cl-end 8000000))
  464. (let ((cl-res nil))
  465. (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end))
  466. (if (cl--check-test cl-item (car cl-p))
  467. (setq cl-res cl-start))
  468. (setq cl-p (cdr cl-p) cl-start (1+ cl-start)))
  469. cl-res))
  470. (or cl-end (setq cl-end (length cl-seq)))
  471. (if cl-from-end
  472. (progn
  473. (while (and (>= (setq cl-end (1- cl-end)) cl-start)
  474. (not (cl--check-test cl-item (aref cl-seq cl-end)))))
  475. (and (>= cl-end cl-start) cl-end))
  476. (while (and (< cl-start cl-end)
  477. (not (cl--check-test cl-item (aref cl-seq cl-start))))
  478. (setq cl-start (1+ cl-start)))
  479. (and (< cl-start cl-end) cl-start))))
  480. ;;;###autoload
  481. (defun cl-position-if (cl-pred cl-list &rest cl-keys)
  482. "Find the first item satisfying PREDICATE in SEQ.
  483. Return the index of the matching item, or nil if not found.
  484. \nKeywords supported: :key :start :end :from-end
  485. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  486. (apply 'cl-position nil cl-list :if cl-pred cl-keys))
  487. ;;;###autoload
  488. (defun cl-position-if-not (cl-pred cl-list &rest cl-keys)
  489. "Find the first item not satisfying PREDICATE in SEQ.
  490. Return the index of the matching item, or nil if not found.
  491. \nKeywords supported: :key :start :end :from-end
  492. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  493. (apply 'cl-position nil cl-list :if-not cl-pred cl-keys))
  494. ;;;###autoload
  495. (defun cl-count (cl-item cl-seq &rest cl-keys)
  496. "Count the number of occurrences of ITEM in SEQ.
  497. \nKeywords supported: :test :test-not :key :start :end
  498. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  499. (cl--parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) ()
  500. (let ((cl-count 0) cl-x)
  501. (or cl-end (setq cl-end (length cl-seq)))
  502. (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  503. (while (< cl-start cl-end)
  504. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  505. (if (cl--check-test cl-item cl-x) (setq cl-count (1+ cl-count)))
  506. (setq cl-start (1+ cl-start)))
  507. cl-count)))
  508. ;;;###autoload
  509. (defun cl-count-if (cl-pred cl-list &rest cl-keys)
  510. "Count the number of items satisfying PREDICATE in SEQ.
  511. \nKeywords supported: :key :start :end
  512. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  513. (apply 'cl-count nil cl-list :if cl-pred cl-keys))
  514. ;;;###autoload
  515. (defun cl-count-if-not (cl-pred cl-list &rest cl-keys)
  516. "Count the number of items not satisfying PREDICATE in SEQ.
  517. \nKeywords supported: :key :start :end
  518. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  519. (apply 'cl-count nil cl-list :if-not cl-pred cl-keys))
  520. ;;;###autoload
  521. (defun cl-mismatch (cl-seq1 cl-seq2 &rest cl-keys)
  522. "Compare SEQ1 with SEQ2, return index of first mismatching element.
  523. Return nil if the sequences match. If one sequence is a prefix of the
  524. other, the return value indicates the end of the shorter sequence.
  525. \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end
  526. \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
  527. (cl--parsing-keywords (:test :test-not :key :from-end
  528. (:start1 0) :end1 (:start2 0) :end2) ()
  529. (or cl-end1 (setq cl-end1 (length cl-seq1)))
  530. (or cl-end2 (setq cl-end2 (length cl-seq2)))
  531. (if cl-from-end
  532. (progn
  533. (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
  534. (cl--check-match (elt cl-seq1 (1- cl-end1))
  535. (elt cl-seq2 (1- cl-end2))))
  536. (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2)))
  537. (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
  538. (1- cl-end1)))
  539. (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1)))
  540. (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2))))
  541. (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
  542. (cl--check-match (if cl-p1 (car cl-p1)
  543. (aref cl-seq1 cl-start1))
  544. (if cl-p2 (car cl-p2)
  545. (aref cl-seq2 cl-start2))))
  546. (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)
  547. cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2)))
  548. (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
  549. cl-start1)))))
  550. ;;;###autoload
  551. (defun cl-search (cl-seq1 cl-seq2 &rest cl-keys)
  552. "Search for SEQ1 as a subsequence of SEQ2.
  553. Return the index of the leftmost element of the first match found;
  554. return nil if there are no matches.
  555. \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end
  556. \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
  557. (cl--parsing-keywords (:test :test-not :key :from-end
  558. (:start1 0) :end1 (:start2 0) :end2) ()
  559. (or cl-end1 (setq cl-end1 (length cl-seq1)))
  560. (or cl-end2 (setq cl-end2 (length cl-seq2)))
  561. (if (>= cl-start1 cl-end1)
  562. (if cl-from-end cl-end2 cl-start2)
  563. (let* ((cl-len (- cl-end1 cl-start1))
  564. (cl-first (cl--check-key (elt cl-seq1 cl-start1)))
  565. (cl-if nil) cl-pos)
  566. (setq cl-end2 (- cl-end2 (1- cl-len)))
  567. (while (and (< cl-start2 cl-end2)
  568. (setq cl-pos (cl--position cl-first cl-seq2
  569. cl-start2 cl-end2 cl-from-end))
  570. (apply 'cl-mismatch cl-seq1 cl-seq2
  571. :start1 (1+ cl-start1) :end1 cl-end1
  572. :start2 (1+ cl-pos) :end2 (+ cl-pos cl-len)
  573. :from-end nil cl-keys))
  574. (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos))))
  575. (and (< cl-start2 cl-end2) cl-pos)))))
  576. ;;;###autoload
  577. (defun cl-sort (cl-seq cl-pred &rest cl-keys)
  578. "Sort the argument SEQ according to PREDICATE.
  579. This is a destructive function; it reuses the storage of SEQ if possible.
  580. \nKeywords supported: :key
  581. \n(fn SEQ PREDICATE [KEYWORD VALUE]...)"
  582. (if (nlistp cl-seq)
  583. (cl-replace cl-seq (apply 'cl-sort (append cl-seq nil) cl-pred cl-keys))
  584. (cl--parsing-keywords (:key) ()
  585. (if (memq cl-key '(nil identity))
  586. (sort cl-seq cl-pred)
  587. (sort cl-seq (function (lambda (cl-x cl-y)
  588. (funcall cl-pred (funcall cl-key cl-x)
  589. (funcall cl-key cl-y)))))))))
  590. ;;;###autoload
  591. (defun cl-stable-sort (cl-seq cl-pred &rest cl-keys)
  592. "Sort the argument SEQ stably according to PREDICATE.
  593. This is a destructive function; it reuses the storage of SEQ if possible.
  594. \nKeywords supported: :key
  595. \n(fn SEQ PREDICATE [KEYWORD VALUE]...)"
  596. (apply 'cl-sort cl-seq cl-pred cl-keys))
  597. ;;;###autoload
  598. (defun cl-merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys)
  599. "Destructively merge the two sequences to produce a new sequence.
  600. TYPE is the sequence type to return, SEQ1 and SEQ2 are the two argument
  601. sequences, and PREDICATE is a `less-than' predicate on the elements.
  602. \nKeywords supported: :key
  603. \n(fn TYPE SEQ1 SEQ2 PREDICATE [KEYWORD VALUE]...)"
  604. (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil)))
  605. (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil)))
  606. (cl--parsing-keywords (:key) ()
  607. (let ((cl-res nil))
  608. (while (and cl-seq1 cl-seq2)
  609. (if (funcall cl-pred (cl--check-key (car cl-seq2))
  610. (cl--check-key (car cl-seq1)))
  611. (push (pop cl-seq2) cl-res)
  612. (push (pop cl-seq1) cl-res)))
  613. (cl-coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type))))
  614. ;;;###autoload
  615. (defun cl-member (cl-item cl-list &rest cl-keys)
  616. "Find the first occurrence of ITEM in LIST.
  617. Return the sublist of LIST whose car is ITEM.
  618. \nKeywords supported: :test :test-not :key
  619. \n(fn ITEM LIST [KEYWORD VALUE]...)"
  620. (declare (compiler-macro cl--compiler-macro-member))
  621. (if cl-keys
  622. (cl--parsing-keywords (:test :test-not :key :if :if-not) ()
  623. (while (and cl-list (not (cl--check-test cl-item (car cl-list))))
  624. (setq cl-list (cdr cl-list)))
  625. cl-list)
  626. (if (and (numberp cl-item) (not (integerp cl-item)))
  627. (member cl-item cl-list)
  628. (memq cl-item cl-list))))
  629. (autoload 'cl--compiler-macro-member "cl-macs")
  630. ;;;###autoload
  631. (defun cl-member-if (cl-pred cl-list &rest cl-keys)
  632. "Find the first item satisfying PREDICATE in LIST.
  633. Return the sublist of LIST whose car matches.
  634. \nKeywords supported: :key
  635. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  636. (apply 'cl-member nil cl-list :if cl-pred cl-keys))
  637. ;;;###autoload
  638. (defun cl-member-if-not (cl-pred cl-list &rest cl-keys)
  639. "Find the first item not satisfying PREDICATE in LIST.
  640. Return the sublist of LIST whose car matches.
  641. \nKeywords supported: :key
  642. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  643. (apply 'cl-member nil cl-list :if-not cl-pred cl-keys))
  644. ;;;###autoload
  645. (defun cl--adjoin (cl-item cl-list &rest cl-keys)
  646. (if (cl--parsing-keywords (:key) t
  647. (apply 'cl-member (cl--check-key cl-item) cl-list cl-keys))
  648. cl-list
  649. (cons cl-item cl-list)))
  650. ;;;###autoload
  651. (defun cl-assoc (cl-item cl-alist &rest cl-keys)
  652. "Find the first item whose car matches ITEM in LIST.
  653. \nKeywords supported: :test :test-not :key
  654. \n(fn ITEM LIST [KEYWORD VALUE]...)"
  655. (declare (compiler-macro cl--compiler-macro-assoc))
  656. (if cl-keys
  657. (cl--parsing-keywords (:test :test-not :key :if :if-not) ()
  658. (while (and cl-alist
  659. (or (not (consp (car cl-alist)))
  660. (not (cl--check-test cl-item (car (car cl-alist))))))
  661. (setq cl-alist (cdr cl-alist)))
  662. (and cl-alist (car cl-alist)))
  663. (if (and (numberp cl-item) (not (integerp cl-item)))
  664. (assoc cl-item cl-alist)
  665. (assq cl-item cl-alist))))
  666. (autoload 'cl--compiler-macro-assoc "cl-macs")
  667. ;;;###autoload
  668. (defun cl-assoc-if (cl-pred cl-list &rest cl-keys)
  669. "Find the first item whose car satisfies PREDICATE in LIST.
  670. \nKeywords supported: :key
  671. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  672. (apply 'cl-assoc nil cl-list :if cl-pred cl-keys))
  673. ;;;###autoload
  674. (defun cl-assoc-if-not (cl-pred cl-list &rest cl-keys)
  675. "Find the first item whose car does not satisfy PREDICATE in LIST.
  676. \nKeywords supported: :key
  677. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  678. (apply 'cl-assoc nil cl-list :if-not cl-pred cl-keys))
  679. ;;;###autoload
  680. (defun cl-rassoc (cl-item cl-alist &rest cl-keys)
  681. "Find the first item whose cdr matches ITEM in LIST.
  682. \nKeywords supported: :test :test-not :key
  683. \n(fn ITEM LIST [KEYWORD VALUE]...)"
  684. (if (or cl-keys (numberp cl-item))
  685. (cl--parsing-keywords (:test :test-not :key :if :if-not) ()
  686. (while (and cl-alist
  687. (or (not (consp (car cl-alist)))
  688. (not (cl--check-test cl-item (cdr (car cl-alist))))))
  689. (setq cl-alist (cdr cl-alist)))
  690. (and cl-alist (car cl-alist)))
  691. (rassq cl-item cl-alist)))
  692. ;;;###autoload
  693. (defun cl-rassoc-if (cl-pred cl-list &rest cl-keys)
  694. "Find the first item whose cdr satisfies PREDICATE in LIST.
  695. \nKeywords supported: :key
  696. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  697. (apply 'cl-rassoc nil cl-list :if cl-pred cl-keys))
  698. ;;;###autoload
  699. (defun cl-rassoc-if-not (cl-pred cl-list &rest cl-keys)
  700. "Find the first item whose cdr does not satisfy PREDICATE in LIST.
  701. \nKeywords supported: :key
  702. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  703. (apply 'cl-rassoc nil cl-list :if-not cl-pred cl-keys))
  704. ;;;###autoload
  705. (defun cl-union (cl-list1 cl-list2 &rest cl-keys)
  706. "Combine LIST1 and LIST2 using a set-union operation.
  707. The resulting list contains all items that appear in either LIST1 or LIST2.
  708. This is a non-destructive function; it makes a copy of the data if necessary
  709. to avoid corrupting the original LIST1 and LIST2.
  710. \nKeywords supported: :test :test-not :key
  711. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  712. (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
  713. ((equal cl-list1 cl-list2) cl-list1)
  714. (t
  715. (or (>= (length cl-list1) (length cl-list2))
  716. (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
  717. (while cl-list2
  718. (if (or cl-keys (numberp (car cl-list2)))
  719. (setq cl-list1
  720. (apply 'cl-adjoin (car cl-list2) cl-list1 cl-keys))
  721. (or (memq (car cl-list2) cl-list1)
  722. (push (car cl-list2) cl-list1)))
  723. (pop cl-list2))
  724. cl-list1)))
  725. ;;;###autoload
  726. (defun cl-nunion (cl-list1 cl-list2 &rest cl-keys)
  727. "Combine LIST1 and LIST2 using a set-union operation.
  728. The resulting list contains all items that appear in either LIST1 or LIST2.
  729. This is a destructive function; it reuses the storage of LIST1 and LIST2
  730. whenever possible.
  731. \nKeywords supported: :test :test-not :key
  732. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  733. (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
  734. (t (apply 'cl-union cl-list1 cl-list2 cl-keys))))
  735. ;;;###autoload
  736. (defun cl-intersection (cl-list1 cl-list2 &rest cl-keys)
  737. "Combine LIST1 and LIST2 using a set-intersection operation.
  738. The resulting list contains all items that appear in both LIST1 and LIST2.
  739. This is a non-destructive function; it makes a copy of the data if necessary
  740. to avoid corrupting the original LIST1 and LIST2.
  741. \nKeywords supported: :test :test-not :key
  742. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  743. (and cl-list1 cl-list2
  744. (if (equal cl-list1 cl-list2) cl-list1
  745. (cl--parsing-keywords (:key) (:test :test-not)
  746. (let ((cl-res nil))
  747. (or (>= (length cl-list1) (length cl-list2))
  748. (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
  749. (while cl-list2
  750. (if (if (or cl-keys (numberp (car cl-list2)))
  751. (apply 'cl-member (cl--check-key (car cl-list2))
  752. cl-list1 cl-keys)
  753. (memq (car cl-list2) cl-list1))
  754. (push (car cl-list2) cl-res))
  755. (pop cl-list2))
  756. cl-res)))))
  757. ;;;###autoload
  758. (defun cl-nintersection (cl-list1 cl-list2 &rest cl-keys)
  759. "Combine LIST1 and LIST2 using a set-intersection operation.
  760. The resulting list contains all items that appear in both LIST1 and LIST2.
  761. This is a destructive function; it reuses the storage of LIST1 and LIST2
  762. whenever possible.
  763. \nKeywords supported: :test :test-not :key
  764. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  765. (and cl-list1 cl-list2 (apply 'cl-intersection cl-list1 cl-list2 cl-keys)))
  766. ;;;###autoload
  767. (defun cl-set-difference (cl-list1 cl-list2 &rest cl-keys)
  768. "Combine LIST1 and LIST2 using a set-difference operation.
  769. The resulting list contains all items that appear in LIST1 but not LIST2.
  770. This is a non-destructive function; it makes a copy of the data if necessary
  771. to avoid corrupting the original LIST1 and LIST2.
  772. \nKeywords supported: :test :test-not :key
  773. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  774. (if (or (null cl-list1) (null cl-list2)) cl-list1
  775. (cl--parsing-keywords (:key) (:test :test-not)
  776. (let ((cl-res nil))
  777. (while cl-list1
  778. (or (if (or cl-keys (numberp (car cl-list1)))
  779. (apply 'cl-member (cl--check-key (car cl-list1))
  780. cl-list2 cl-keys)
  781. (memq (car cl-list1) cl-list2))
  782. (push (car cl-list1) cl-res))
  783. (pop cl-list1))
  784. cl-res))))
  785. ;;;###autoload
  786. (defun cl-nset-difference (cl-list1 cl-list2 &rest cl-keys)
  787. "Combine LIST1 and LIST2 using a set-difference operation.
  788. The resulting list contains all items that appear in LIST1 but not LIST2.
  789. This is a destructive function; it reuses the storage of LIST1 and LIST2
  790. whenever possible.
  791. \nKeywords supported: :test :test-not :key
  792. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  793. (if (or (null cl-list1) (null cl-list2)) cl-list1
  794. (apply 'cl-set-difference cl-list1 cl-list2 cl-keys)))
  795. ;;;###autoload
  796. (defun cl-set-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
  797. "Combine LIST1 and LIST2 using a set-exclusive-or operation.
  798. The resulting list contains all items appearing in exactly one of LIST1, LIST2.
  799. This is a non-destructive function; it makes a copy of the data if necessary
  800. to avoid corrupting the original LIST1 and LIST2.
  801. \nKeywords supported: :test :test-not :key
  802. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  803. (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
  804. ((equal cl-list1 cl-list2) nil)
  805. (t (append (apply 'cl-set-difference cl-list1 cl-list2 cl-keys)
  806. (apply 'cl-set-difference cl-list2 cl-list1 cl-keys)))))
  807. ;;;###autoload
  808. (defun cl-nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys)
  809. "Combine LIST1 and LIST2 using a set-exclusive-or operation.
  810. The resulting list contains all items appearing in exactly one of LIST1, LIST2.
  811. This is a destructive function; it reuses the storage of LIST1 and LIST2
  812. whenever possible.
  813. \nKeywords supported: :test :test-not :key
  814. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  815. (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
  816. ((equal cl-list1 cl-list2) nil)
  817. (t (nconc (apply 'cl-nset-difference cl-list1 cl-list2 cl-keys)
  818. (apply 'cl-nset-difference cl-list2 cl-list1 cl-keys)))))
  819. ;;;###autoload
  820. (defun cl-subsetp (cl-list1 cl-list2 &rest cl-keys)
  821. "Return true if LIST1 is a subset of LIST2.
  822. I.e., if every element of LIST1 also appears in LIST2.
  823. \nKeywords supported: :test :test-not :key
  824. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  825. (cond ((null cl-list1) t) ((null cl-list2) nil)
  826. ((equal cl-list1 cl-list2) t)
  827. (t (cl--parsing-keywords (:key) (:test :test-not)
  828. (while (and cl-list1
  829. (apply 'cl-member (cl--check-key (car cl-list1))
  830. cl-list2 cl-keys))
  831. (pop cl-list1))
  832. (null cl-list1)))))
  833. ;;;###autoload
  834. (defun cl-subst-if (cl-new cl-pred cl-tree &rest cl-keys)
  835. "Substitute NEW for elements matching PREDICATE in TREE (non-destructively).
  836. Return a copy of TREE with all matching elements replaced by NEW.
  837. \nKeywords supported: :key
  838. \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)"
  839. (apply 'cl-sublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
  840. ;;;###autoload
  841. (defun cl-subst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
  842. "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively).
  843. Return a copy of TREE with all non-matching elements replaced by NEW.
  844. \nKeywords supported: :key
  845. \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)"
  846. (apply 'cl-sublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
  847. ;;;###autoload
  848. (defun cl-nsubst (cl-new cl-old cl-tree &rest cl-keys)
  849. "Substitute NEW for OLD everywhere in TREE (destructively).
  850. Any element of TREE which is `eql' to OLD is changed to NEW (via a call
  851. to `setcar').
  852. \nKeywords supported: :test :test-not :key
  853. \n(fn NEW OLD TREE [KEYWORD VALUE]...)"
  854. (apply 'cl-nsublis (list (cons cl-old cl-new)) cl-tree cl-keys))
  855. ;;;###autoload
  856. (defun cl-nsubst-if (cl-new cl-pred cl-tree &rest cl-keys)
  857. "Substitute NEW for elements matching PREDICATE in TREE (destructively).
  858. Any element of TREE which matches is changed to NEW (via a call to `setcar').
  859. \nKeywords supported: :key
  860. \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)"
  861. (apply 'cl-nsublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
  862. ;;;###autoload
  863. (defun cl-nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys)
  864. "Substitute NEW for elements not matching PREDICATE in TREE (destructively).
  865. Any element of TREE which matches is changed to NEW (via a call to `setcar').
  866. \nKeywords supported: :key
  867. \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)"
  868. (apply 'cl-nsublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
  869. (defvar cl--alist)
  870. ;;;###autoload
  871. (defun cl-sublis (cl-alist cl-tree &rest cl-keys)
  872. "Perform substitutions indicated by ALIST in TREE (non-destructively).
  873. Return a copy of TREE with all matching elements replaced.
  874. \nKeywords supported: :test :test-not :key
  875. \n(fn ALIST TREE [KEYWORD VALUE]...)"
  876. (cl--parsing-keywords (:test :test-not :key :if :if-not) ()
  877. (let ((cl--alist cl-alist))
  878. (cl--sublis-rec cl-tree))))
  879. (defun cl--sublis-rec (cl-tree) ;Uses cl--alist cl-key/test*/if*.
  880. (let ((cl-temp (cl--check-key cl-tree)) (cl-p cl--alist))
  881. (while (and cl-p (not (cl--check-test-nokey (car (car cl-p)) cl-temp)))
  882. (setq cl-p (cdr cl-p)))
  883. (if cl-p (cdr (car cl-p))
  884. (if (consp cl-tree)
  885. (let ((cl-a (cl--sublis-rec (car cl-tree)))
  886. (cl-d (cl--sublis-rec (cdr cl-tree))))
  887. (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree)))
  888. cl-tree
  889. (cons cl-a cl-d)))
  890. cl-tree))))
  891. ;;;###autoload
  892. (defun cl-nsublis (cl-alist cl-tree &rest cl-keys)
  893. "Perform substitutions indicated by ALIST in TREE (destructively).
  894. Any matching element of TREE is changed via a call to `setcar'.
  895. \nKeywords supported: :test :test-not :key
  896. \n(fn ALIST TREE [KEYWORD VALUE]...)"
  897. (cl--parsing-keywords (:test :test-not :key :if :if-not) ()
  898. (let ((cl-hold (list cl-tree))
  899. (cl--alist cl-alist))
  900. (cl--nsublis-rec cl-hold)
  901. (car cl-hold))))
  902. (defun cl--nsublis-rec (cl-tree) ;Uses cl--alist cl-key/test*/if*.
  903. (while (consp cl-tree)
  904. (let ((cl-temp (cl--check-key (car cl-tree))) (cl-p cl--alist))
  905. (while (and cl-p (not (cl--check-test-nokey (car (car cl-p)) cl-temp)))
  906. (setq cl-p (cdr cl-p)))
  907. (if cl-p (setcar cl-tree (cdr (car cl-p)))
  908. (if (consp (car cl-tree)) (cl--nsublis-rec (car cl-tree))))
  909. (setq cl-temp (cl--check-key (cdr cl-tree)) cl-p cl--alist)
  910. (while (and cl-p (not (cl--check-test-nokey (car (car cl-p)) cl-temp)))
  911. (setq cl-p (cdr cl-p)))
  912. (if cl-p
  913. (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil))
  914. (setq cl-tree (cdr cl-tree))))))
  915. ;;;###autoload
  916. (defun cl-tree-equal (cl-x cl-y &rest cl-keys)
  917. "Return t if trees TREE1 and TREE2 have `eql' leaves.
  918. Atoms are compared by `eql'; cons cells are compared recursively.
  919. \nKeywords supported: :test :test-not :key
  920. \n(fn TREE1 TREE2 [KEYWORD VALUE]...)"
  921. (cl--parsing-keywords (:test :test-not :key) ()
  922. (cl--tree-equal-rec cl-x cl-y)))
  923. (defun cl--tree-equal-rec (cl-x cl-y) ;Uses cl-key/test*.
  924. (while (and (consp cl-x) (consp cl-y)
  925. (cl--tree-equal-rec (car cl-x) (car cl-y)))
  926. (setq cl-x (cdr cl-x) cl-y (cdr cl-y)))
  927. (and (not (consp cl-x)) (not (consp cl-y)) (cl--check-match cl-x cl-y)))
  928. (run-hooks 'cl-seq-load-hook)
  929. ;; Local variables:
  930. ;; byte-compile-dynamic: t
  931. ;; generated-autoload-file: "cl-loaddefs.el"
  932. ;; End:
  933. (provide 'cl-seq)
  934. ;;; cl-seq.el ends here