cl-seq.el 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. ;;; cl-seq.el --- Common Lisp features, part 3
  2. ;; Copyright (C) 1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Gillespie <daveg@synaptics.com>
  4. ;; 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)
  32. ;;; Keyword parsing. This is special-cased here so that we can compile
  33. ;;; this file independent from cl-macs.
  34. (defmacro cl-parsing-keywords (kwords other-keys &rest body)
  35. (declare (indent 2) (debug (sexp sexp &rest form)))
  36. (cons
  37. 'let*
  38. (cons (mapcar
  39. (function
  40. (lambda (x)
  41. (let* ((var (if (consp x) (car x) x))
  42. (mem (list 'car (list 'cdr (list 'memq (list 'quote var)
  43. 'cl-keys)))))
  44. (if (eq var :test-not)
  45. (setq mem (list 'and mem (list 'setq 'cl-test mem) t)))
  46. (if (eq var :if-not)
  47. (setq mem (list 'and mem (list 'setq 'cl-if mem) t)))
  48. (list (intern
  49. (format "cl-%s" (substring (symbol-name var) 1)))
  50. (if (consp x) (list 'or mem (car (cdr x))) mem)))))
  51. kwords)
  52. (append
  53. (and (not (eq other-keys t))
  54. (list
  55. (list 'let '((cl-keys-temp cl-keys))
  56. (list 'while 'cl-keys-temp
  57. (list 'or (list 'memq '(car cl-keys-temp)
  58. (list 'quote
  59. (mapcar
  60. (function
  61. (lambda (x)
  62. (if (consp x)
  63. (car x) x)))
  64. (append kwords
  65. other-keys))))
  66. '(car (cdr (memq (quote :allow-other-keys)
  67. cl-keys)))
  68. '(error "Bad keyword argument %s"
  69. (car cl-keys-temp)))
  70. '(setq cl-keys-temp (cdr (cdr cl-keys-temp)))))))
  71. body))))
  72. (defmacro cl-check-key (x)
  73. (declare (debug edebug-forms))
  74. (list 'if 'cl-key (list 'funcall 'cl-key x) x))
  75. (defmacro cl-check-test-nokey (item x)
  76. (declare (debug edebug-forms))
  77. (list 'cond
  78. (list 'cl-test
  79. (list 'eq (list 'not (list 'funcall 'cl-test item x))
  80. 'cl-test-not))
  81. (list 'cl-if
  82. (list 'eq (list 'not (list 'funcall 'cl-if x)) 'cl-if-not))
  83. (list 't (list 'if (list 'numberp item)
  84. (list 'equal item x) (list 'eq item x)))))
  85. (defmacro cl-check-test (item x)
  86. (declare (debug edebug-forms))
  87. (list 'cl-check-test-nokey item (list 'cl-check-key x)))
  88. (defmacro cl-check-match (x y)
  89. (declare (debug edebug-forms))
  90. (setq x (list 'cl-check-key x) y (list 'cl-check-key y))
  91. (list 'if 'cl-test
  92. (list 'eq (list 'not (list 'funcall 'cl-test x y)) 'cl-test-not)
  93. (list 'if (list 'numberp x)
  94. (list 'equal x y) (list 'eq x y))))
  95. (defvar cl-test) (defvar cl-test-not)
  96. (defvar cl-if) (defvar cl-if-not)
  97. (defvar cl-key)
  98. ;;;###autoload
  99. (defun reduce (cl-func cl-seq &rest cl-keys)
  100. "Reduce two-argument FUNCTION across SEQ.
  101. \nKeywords supported: :start :end :from-end :initial-value :key
  102. \n(fn FUNCTION SEQ [KEYWORD VALUE]...)"
  103. (cl-parsing-keywords (:from-end (:start 0) :end :initial-value :key) ()
  104. (or (listp cl-seq) (setq cl-seq (append cl-seq nil)))
  105. (setq cl-seq (subseq cl-seq cl-start cl-end))
  106. (if cl-from-end (setq cl-seq (nreverse cl-seq)))
  107. (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value)
  108. (cl-seq (cl-check-key (pop cl-seq)))
  109. (t (funcall cl-func)))))
  110. (if cl-from-end
  111. (while cl-seq
  112. (setq cl-accum (funcall cl-func (cl-check-key (pop cl-seq))
  113. cl-accum)))
  114. (while cl-seq
  115. (setq cl-accum (funcall cl-func cl-accum
  116. (cl-check-key (pop cl-seq))))))
  117. cl-accum)))
  118. ;;;###autoload
  119. (defun fill (seq item &rest cl-keys)
  120. "Fill the elements of SEQ with ITEM.
  121. \nKeywords supported: :start :end
  122. \n(fn SEQ ITEM [KEYWORD VALUE]...)"
  123. (cl-parsing-keywords ((:start 0) :end) ()
  124. (if (listp seq)
  125. (let ((p (nthcdr cl-start seq))
  126. (n (if cl-end (- cl-end cl-start) 8000000)))
  127. (while (and p (>= (setq n (1- n)) 0))
  128. (setcar p item)
  129. (setq p (cdr p))))
  130. (or cl-end (setq cl-end (length seq)))
  131. (if (and (= cl-start 0) (= cl-end (length seq)))
  132. (fillarray seq item)
  133. (while (< cl-start cl-end)
  134. (aset seq cl-start item)
  135. (setq cl-start (1+ cl-start)))))
  136. seq))
  137. ;;;###autoload
  138. (defun replace (cl-seq1 cl-seq2 &rest cl-keys)
  139. "Replace the elements of SEQ1 with the elements of SEQ2.
  140. SEQ1 is destructively modified, then returned.
  141. \nKeywords supported: :start1 :end1 :start2 :end2
  142. \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
  143. (cl-parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) ()
  144. (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1))
  145. (or (= cl-start1 cl-start2)
  146. (let* ((cl-len (length cl-seq1))
  147. (cl-n (min (- (or cl-end1 cl-len) cl-start1)
  148. (- (or cl-end2 cl-len) cl-start2))))
  149. (while (>= (setq cl-n (1- cl-n)) 0)
  150. (cl-set-elt cl-seq1 (+ cl-start1 cl-n)
  151. (elt cl-seq2 (+ cl-start2 cl-n))))))
  152. (if (listp cl-seq1)
  153. (let ((cl-p1 (nthcdr cl-start1 cl-seq1))
  154. (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000)))
  155. (if (listp cl-seq2)
  156. (let ((cl-p2 (nthcdr cl-start2 cl-seq2))
  157. (cl-n (min cl-n1
  158. (if cl-end2 (- cl-end2 cl-start2) 4000000))))
  159. (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0))
  160. (setcar cl-p1 (car cl-p2))
  161. (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2))))
  162. (setq cl-end2 (min (or cl-end2 (length cl-seq2))
  163. (+ cl-start2 cl-n1)))
  164. (while (and cl-p1 (< cl-start2 cl-end2))
  165. (setcar cl-p1 (aref cl-seq2 cl-start2))
  166. (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2)))))
  167. (setq cl-end1 (min (or cl-end1 (length cl-seq1))
  168. (+ cl-start1 (- (or cl-end2 (length cl-seq2))
  169. cl-start2))))
  170. (if (listp cl-seq2)
  171. (let ((cl-p2 (nthcdr cl-start2 cl-seq2)))
  172. (while (< cl-start1 cl-end1)
  173. (aset cl-seq1 cl-start1 (car cl-p2))
  174. (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1))))
  175. (while (< cl-start1 cl-end1)
  176. (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2))
  177. (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1))))))
  178. cl-seq1))
  179. ;;;###autoload
  180. (defun remove* (cl-item cl-seq &rest cl-keys)
  181. "Remove all occurrences of ITEM in SEQ.
  182. This is a non-destructive function; it makes a copy of SEQ if necessary
  183. to avoid corrupting the original SEQ.
  184. \nKeywords supported: :test :test-not :key :count :start :end :from-end
  185. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  186. (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
  187. (:start 0) :end) ()
  188. (if (<= (or cl-count (setq cl-count 8000000)) 0)
  189. cl-seq
  190. (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000)))
  191. (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end
  192. cl-from-end)))
  193. (if cl-i
  194. (let ((cl-res (apply 'delete* cl-item (append cl-seq nil)
  195. (append (if cl-from-end
  196. (list :end (1+ cl-i))
  197. (list :start cl-i))
  198. cl-keys))))
  199. (if (listp cl-seq) cl-res
  200. (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))
  201. cl-seq))
  202. (setq cl-end (- (or cl-end 8000000) cl-start))
  203. (if (= cl-start 0)
  204. (while (and cl-seq (> cl-end 0)
  205. (cl-check-test cl-item (car cl-seq))
  206. (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
  207. (> (setq cl-count (1- cl-count)) 0))))
  208. (if (and (> cl-count 0) (> cl-end 0))
  209. (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq)
  210. (setq cl-end (1- cl-end)) (cdr cl-seq))))
  211. (while (and cl-p (> cl-end 0)
  212. (not (cl-check-test cl-item (car cl-p))))
  213. (setq cl-p (cdr cl-p) cl-end (1- cl-end)))
  214. (if (and cl-p (> cl-end 0))
  215. (nconc (ldiff cl-seq cl-p)
  216. (if (= cl-count 1) (cdr cl-p)
  217. (and (cdr cl-p)
  218. (apply 'delete* cl-item
  219. (copy-sequence (cdr cl-p))
  220. :start 0 :end (1- cl-end)
  221. :count (1- cl-count) cl-keys))))
  222. cl-seq))
  223. cl-seq)))))
  224. ;;;###autoload
  225. (defun remove-if (cl-pred cl-list &rest cl-keys)
  226. "Remove all items satisfying PREDICATE in SEQ.
  227. This is a non-destructive function; it makes a copy of SEQ if necessary
  228. to avoid corrupting the original SEQ.
  229. \nKeywords supported: :key :count :start :end :from-end
  230. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  231. (apply 'remove* nil cl-list :if cl-pred cl-keys))
  232. ;;;###autoload
  233. (defun remove-if-not (cl-pred cl-list &rest cl-keys)
  234. "Remove all items not satisfying PREDICATE in SEQ.
  235. This is a non-destructive function; it makes a copy of SEQ if necessary
  236. to avoid corrupting the original SEQ.
  237. \nKeywords supported: :key :count :start :end :from-end
  238. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  239. (apply 'remove* nil cl-list :if-not cl-pred cl-keys))
  240. ;;;###autoload
  241. (defun delete* (cl-item cl-seq &rest cl-keys)
  242. "Remove all occurrences of ITEM in SEQ.
  243. This is a destructive function; it reuses the storage of SEQ whenever possible.
  244. \nKeywords supported: :test :test-not :key :count :start :end :from-end
  245. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  246. (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end
  247. (:start 0) :end) ()
  248. (if (<= (or cl-count (setq cl-count 8000000)) 0)
  249. cl-seq
  250. (if (listp cl-seq)
  251. (if (and cl-from-end (< cl-count 4000000))
  252. (let (cl-i)
  253. (while (and (>= (setq cl-count (1- cl-count)) 0)
  254. (setq cl-i (cl-position cl-item cl-seq cl-start
  255. cl-end cl-from-end)))
  256. (if (= cl-i 0) (setq cl-seq (cdr cl-seq))
  257. (let ((cl-tail (nthcdr (1- cl-i) cl-seq)))
  258. (setcdr cl-tail (cdr (cdr cl-tail)))))
  259. (setq cl-end cl-i))
  260. cl-seq)
  261. (setq cl-end (- (or cl-end 8000000) cl-start))
  262. (if (= cl-start 0)
  263. (progn
  264. (while (and cl-seq
  265. (> cl-end 0)
  266. (cl-check-test cl-item (car cl-seq))
  267. (setq cl-end (1- cl-end) cl-seq (cdr cl-seq))
  268. (> (setq cl-count (1- cl-count)) 0)))
  269. (setq cl-end (1- cl-end)))
  270. (setq cl-start (1- cl-start)))
  271. (if (and (> cl-count 0) (> cl-end 0))
  272. (let ((cl-p (nthcdr cl-start cl-seq)))
  273. (while (and (cdr cl-p) (> cl-end 0))
  274. (if (cl-check-test cl-item (car (cdr cl-p)))
  275. (progn
  276. (setcdr cl-p (cdr (cdr cl-p)))
  277. (if (= (setq cl-count (1- cl-count)) 0)
  278. (setq cl-end 1)))
  279. (setq cl-p (cdr cl-p)))
  280. (setq cl-end (1- cl-end)))))
  281. cl-seq)
  282. (apply 'remove* cl-item cl-seq cl-keys)))))
  283. ;;;###autoload
  284. (defun delete-if (cl-pred cl-list &rest cl-keys)
  285. "Remove all items satisfying PREDICATE in SEQ.
  286. This is a destructive function; it reuses the storage of SEQ whenever possible.
  287. \nKeywords supported: :key :count :start :end :from-end
  288. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  289. (apply 'delete* nil cl-list :if cl-pred cl-keys))
  290. ;;;###autoload
  291. (defun delete-if-not (cl-pred cl-list &rest cl-keys)
  292. "Remove all items not satisfying PREDICATE in SEQ.
  293. This is a destructive function; it reuses the storage of SEQ whenever possible.
  294. \nKeywords supported: :key :count :start :end :from-end
  295. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  296. (apply 'delete* nil cl-list :if-not cl-pred cl-keys))
  297. ;;;###autoload
  298. (defun remove-duplicates (cl-seq &rest cl-keys)
  299. "Return a copy of SEQ with all duplicate elements removed.
  300. \nKeywords supported: :test :test-not :key :start :end :from-end
  301. \n(fn SEQ [KEYWORD VALUE]...)"
  302. (cl-delete-duplicates cl-seq cl-keys t))
  303. ;;;###autoload
  304. (defun delete-duplicates (cl-seq &rest cl-keys)
  305. "Remove all duplicate elements from SEQ (destructively).
  306. \nKeywords supported: :test :test-not :key :start :end :from-end
  307. \n(fn SEQ [KEYWORD VALUE]...)"
  308. (cl-delete-duplicates cl-seq cl-keys nil))
  309. (defun cl-delete-duplicates (cl-seq cl-keys cl-copy)
  310. (if (listp cl-seq)
  311. (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if)
  312. ()
  313. (if cl-from-end
  314. (let ((cl-p (nthcdr cl-start cl-seq)) cl-i)
  315. (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
  316. (while (> cl-end 1)
  317. (setq cl-i 0)
  318. (while (setq cl-i (cl-position (cl-check-key (car cl-p))
  319. (cdr cl-p) cl-i (1- cl-end)))
  320. (if cl-copy (setq cl-seq (copy-sequence cl-seq)
  321. cl-p (nthcdr cl-start cl-seq) cl-copy nil))
  322. (let ((cl-tail (nthcdr cl-i cl-p)))
  323. (setcdr cl-tail (cdr (cdr cl-tail))))
  324. (setq cl-end (1- cl-end)))
  325. (setq cl-p (cdr cl-p) cl-end (1- cl-end)
  326. cl-start (1+ cl-start)))
  327. cl-seq)
  328. (setq cl-end (- (or cl-end (length cl-seq)) cl-start))
  329. (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1)
  330. (cl-position (cl-check-key (car cl-seq))
  331. (cdr cl-seq) 0 (1- cl-end)))
  332. (setq cl-seq (cdr cl-seq) cl-end (1- cl-end)))
  333. (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq)
  334. (setq cl-end (1- cl-end) cl-start 1) cl-seq)))
  335. (while (and (cdr (cdr cl-p)) (> cl-end 1))
  336. (if (cl-position (cl-check-key (car (cdr cl-p)))
  337. (cdr (cdr cl-p)) 0 (1- cl-end))
  338. (progn
  339. (if cl-copy (setq cl-seq (copy-sequence cl-seq)
  340. cl-p (nthcdr (1- cl-start) cl-seq)
  341. cl-copy nil))
  342. (setcdr cl-p (cdr (cdr cl-p))))
  343. (setq cl-p (cdr cl-p)))
  344. (setq cl-end (1- cl-end) cl-start (1+ cl-start)))
  345. cl-seq)))
  346. (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil)))
  347. (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))))
  348. ;;;###autoload
  349. (defun substitute (cl-new cl-old cl-seq &rest cl-keys)
  350. "Substitute NEW for OLD in SEQ.
  351. This is a non-destructive function; it makes a copy of SEQ if necessary
  352. to avoid corrupting the original SEQ.
  353. \nKeywords supported: :test :test-not :key :count :start :end :from-end
  354. \n(fn NEW OLD SEQ [KEYWORD VALUE]...)"
  355. (cl-parsing-keywords (:test :test-not :key :if :if-not :count
  356. (:start 0) :end :from-end) ()
  357. (if (or (eq cl-old cl-new)
  358. (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0))
  359. cl-seq
  360. (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end)))
  361. (if (not cl-i)
  362. cl-seq
  363. (setq cl-seq (copy-sequence cl-seq))
  364. (or cl-from-end
  365. (progn (cl-set-elt cl-seq cl-i cl-new)
  366. (setq cl-i (1+ cl-i) cl-count (1- cl-count))))
  367. (apply 'nsubstitute cl-new cl-old cl-seq :count cl-count
  368. :start cl-i cl-keys))))))
  369. ;;;###autoload
  370. (defun substitute-if (cl-new cl-pred cl-list &rest cl-keys)
  371. "Substitute NEW for all items satisfying PREDICATE in SEQ.
  372. This is a non-destructive function; it makes a copy of SEQ if necessary
  373. to avoid corrupting the original SEQ.
  374. \nKeywords supported: :key :count :start :end :from-end
  375. \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
  376. (apply 'substitute cl-new nil cl-list :if cl-pred cl-keys))
  377. ;;;###autoload
  378. (defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
  379. "Substitute NEW for all items not satisfying PREDICATE in SEQ.
  380. This is a non-destructive function; it makes a copy of SEQ if necessary
  381. to avoid corrupting the original SEQ.
  382. \nKeywords supported: :key :count :start :end :from-end
  383. \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
  384. (apply 'substitute cl-new nil cl-list :if-not cl-pred cl-keys))
  385. ;;;###autoload
  386. (defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys)
  387. "Substitute NEW for OLD in SEQ.
  388. This is a destructive function; it reuses the storage of SEQ whenever possible.
  389. \nKeywords supported: :test :test-not :key :count :start :end :from-end
  390. \n(fn NEW OLD SEQ [KEYWORD VALUE]...)"
  391. (cl-parsing-keywords (:test :test-not :key :if :if-not :count
  392. (:start 0) :end :from-end) ()
  393. (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0)
  394. (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000)))
  395. (let ((cl-p (nthcdr cl-start cl-seq)))
  396. (setq cl-end (- (or cl-end 8000000) cl-start))
  397. (while (and cl-p (> cl-end 0) (> cl-count 0))
  398. (if (cl-check-test cl-old (car cl-p))
  399. (progn
  400. (setcar cl-p cl-new)
  401. (setq cl-count (1- cl-count))))
  402. (setq cl-p (cdr cl-p) cl-end (1- cl-end))))
  403. (or cl-end (setq cl-end (length cl-seq)))
  404. (if cl-from-end
  405. (while (and (< cl-start cl-end) (> cl-count 0))
  406. (setq cl-end (1- cl-end))
  407. (if (cl-check-test cl-old (elt cl-seq cl-end))
  408. (progn
  409. (cl-set-elt cl-seq cl-end cl-new)
  410. (setq cl-count (1- cl-count)))))
  411. (while (and (< cl-start cl-end) (> cl-count 0))
  412. (if (cl-check-test cl-old (aref cl-seq cl-start))
  413. (progn
  414. (aset cl-seq cl-start cl-new)
  415. (setq cl-count (1- cl-count))))
  416. (setq cl-start (1+ cl-start))))))
  417. cl-seq))
  418. ;;;###autoload
  419. (defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys)
  420. "Substitute NEW for all items satisfying PREDICATE in SEQ.
  421. This is a destructive function; it reuses the storage of SEQ whenever possible.
  422. \nKeywords supported: :key :count :start :end :from-end
  423. \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
  424. (apply 'nsubstitute cl-new nil cl-list :if cl-pred cl-keys))
  425. ;;;###autoload
  426. (defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys)
  427. "Substitute NEW for all items not satisfying PREDICATE in SEQ.
  428. This is a destructive function; it reuses the storage of SEQ whenever possible.
  429. \nKeywords supported: :key :count :start :end :from-end
  430. \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)"
  431. (apply 'nsubstitute cl-new nil cl-list :if-not cl-pred cl-keys))
  432. ;;;###autoload
  433. (defun find (cl-item cl-seq &rest cl-keys)
  434. "Find the first occurrence of ITEM in SEQ.
  435. Return the matching ITEM, or nil if not found.
  436. \nKeywords supported: :test :test-not :key :start :end :from-end
  437. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  438. (let ((cl-pos (apply 'position cl-item cl-seq cl-keys)))
  439. (and cl-pos (elt cl-seq cl-pos))))
  440. ;;;###autoload
  441. (defun find-if (cl-pred cl-list &rest cl-keys)
  442. "Find the first item satisfying PREDICATE in SEQ.
  443. Return the matching item, or nil if not found.
  444. \nKeywords supported: :key :start :end :from-end
  445. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  446. (apply 'find nil cl-list :if cl-pred cl-keys))
  447. ;;;###autoload
  448. (defun find-if-not (cl-pred cl-list &rest cl-keys)
  449. "Find the first item not satisfying PREDICATE in SEQ.
  450. Return the matching item, or nil if not found.
  451. \nKeywords supported: :key :start :end :from-end
  452. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  453. (apply 'find nil cl-list :if-not cl-pred cl-keys))
  454. ;;;###autoload
  455. (defun position (cl-item cl-seq &rest cl-keys)
  456. "Find the first occurrence of ITEM in SEQ.
  457. Return the index of the matching item, or nil if not found.
  458. \nKeywords supported: :test :test-not :key :start :end :from-end
  459. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  460. (cl-parsing-keywords (:test :test-not :key :if :if-not
  461. (:start 0) :end :from-end) ()
  462. (cl-position cl-item cl-seq cl-start cl-end cl-from-end)))
  463. (defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end)
  464. (if (listp cl-seq)
  465. (let ((cl-p (nthcdr cl-start cl-seq)))
  466. (or cl-end (setq cl-end 8000000))
  467. (let ((cl-res nil))
  468. (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end))
  469. (if (cl-check-test cl-item (car cl-p))
  470. (setq cl-res cl-start))
  471. (setq cl-p (cdr cl-p) cl-start (1+ cl-start)))
  472. cl-res))
  473. (or cl-end (setq cl-end (length cl-seq)))
  474. (if cl-from-end
  475. (progn
  476. (while (and (>= (setq cl-end (1- cl-end)) cl-start)
  477. (not (cl-check-test cl-item (aref cl-seq cl-end)))))
  478. (and (>= cl-end cl-start) cl-end))
  479. (while (and (< cl-start cl-end)
  480. (not (cl-check-test cl-item (aref cl-seq cl-start))))
  481. (setq cl-start (1+ cl-start)))
  482. (and (< cl-start cl-end) cl-start))))
  483. ;;;###autoload
  484. (defun position-if (cl-pred cl-list &rest cl-keys)
  485. "Find the first item satisfying PREDICATE in SEQ.
  486. Return the index of the matching item, or nil if not found.
  487. \nKeywords supported: :key :start :end :from-end
  488. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  489. (apply 'position nil cl-list :if cl-pred cl-keys))
  490. ;;;###autoload
  491. (defun position-if-not (cl-pred cl-list &rest cl-keys)
  492. "Find the first item not satisfying PREDICATE in SEQ.
  493. Return the index of the matching item, or nil if not found.
  494. \nKeywords supported: :key :start :end :from-end
  495. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  496. (apply 'position nil cl-list :if-not cl-pred cl-keys))
  497. ;;;###autoload
  498. (defun count (cl-item cl-seq &rest cl-keys)
  499. "Count the number of occurrences of ITEM in SEQ.
  500. \nKeywords supported: :test :test-not :key :start :end
  501. \n(fn ITEM SEQ [KEYWORD VALUE]...)"
  502. (cl-parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) ()
  503. (let ((cl-count 0) cl-x)
  504. (or cl-end (setq cl-end (length cl-seq)))
  505. (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  506. (while (< cl-start cl-end)
  507. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  508. (if (cl-check-test cl-item cl-x) (setq cl-count (1+ cl-count)))
  509. (setq cl-start (1+ cl-start)))
  510. cl-count)))
  511. ;;;###autoload
  512. (defun count-if (cl-pred cl-list &rest cl-keys)
  513. "Count the number of items satisfying PREDICATE in SEQ.
  514. \nKeywords supported: :key :start :end
  515. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  516. (apply 'count nil cl-list :if cl-pred cl-keys))
  517. ;;;###autoload
  518. (defun count-if-not (cl-pred cl-list &rest cl-keys)
  519. "Count the number of items not satisfying PREDICATE in SEQ.
  520. \nKeywords supported: :key :start :end
  521. \n(fn PREDICATE SEQ [KEYWORD VALUE]...)"
  522. (apply 'count nil cl-list :if-not cl-pred cl-keys))
  523. ;;;###autoload
  524. (defun mismatch (cl-seq1 cl-seq2 &rest cl-keys)
  525. "Compare SEQ1 with SEQ2, return index of first mismatching element.
  526. Return nil if the sequences match. If one sequence is a prefix of the
  527. other, the return value indicates the end of the shorter sequence.
  528. \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end
  529. \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
  530. (cl-parsing-keywords (:test :test-not :key :from-end
  531. (:start1 0) :end1 (:start2 0) :end2) ()
  532. (or cl-end1 (setq cl-end1 (length cl-seq1)))
  533. (or cl-end2 (setq cl-end2 (length cl-seq2)))
  534. (if cl-from-end
  535. (progn
  536. (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
  537. (cl-check-match (elt cl-seq1 (1- cl-end1))
  538. (elt cl-seq2 (1- cl-end2))))
  539. (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2)))
  540. (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
  541. (1- cl-end1)))
  542. (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1)))
  543. (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2))))
  544. (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2)
  545. (cl-check-match (if cl-p1 (car cl-p1)
  546. (aref cl-seq1 cl-start1))
  547. (if cl-p2 (car cl-p2)
  548. (aref cl-seq2 cl-start2))))
  549. (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)
  550. cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2)))
  551. (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2))
  552. cl-start1)))))
  553. ;;;###autoload
  554. (defun search (cl-seq1 cl-seq2 &rest cl-keys)
  555. "Search for SEQ1 as a subsequence of SEQ2.
  556. Return the index of the leftmost element of the first match found;
  557. return nil if there are no matches.
  558. \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end
  559. \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)"
  560. (cl-parsing-keywords (:test :test-not :key :from-end
  561. (:start1 0) :end1 (:start2 0) :end2) ()
  562. (or cl-end1 (setq cl-end1 (length cl-seq1)))
  563. (or cl-end2 (setq cl-end2 (length cl-seq2)))
  564. (if (>= cl-start1 cl-end1)
  565. (if cl-from-end cl-end2 cl-start2)
  566. (let* ((cl-len (- cl-end1 cl-start1))
  567. (cl-first (cl-check-key (elt cl-seq1 cl-start1)))
  568. (cl-if nil) cl-pos)
  569. (setq cl-end2 (- cl-end2 (1- cl-len)))
  570. (while (and (< cl-start2 cl-end2)
  571. (setq cl-pos (cl-position cl-first cl-seq2
  572. cl-start2 cl-end2 cl-from-end))
  573. (apply 'mismatch cl-seq1 cl-seq2
  574. :start1 (1+ cl-start1) :end1 cl-end1
  575. :start2 (1+ cl-pos) :end2 (+ cl-pos cl-len)
  576. :from-end nil cl-keys))
  577. (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos))))
  578. (and (< cl-start2 cl-end2) cl-pos)))))
  579. ;;;###autoload
  580. (defun sort* (cl-seq cl-pred &rest cl-keys)
  581. "Sort the argument SEQ according to PREDICATE.
  582. This is a destructive function; it reuses the storage of SEQ if possible.
  583. \nKeywords supported: :key
  584. \n(fn SEQ PREDICATE [KEYWORD VALUE]...)"
  585. (if (nlistp cl-seq)
  586. (replace cl-seq (apply 'sort* (append cl-seq nil) cl-pred cl-keys))
  587. (cl-parsing-keywords (:key) ()
  588. (if (memq cl-key '(nil identity))
  589. (sort cl-seq cl-pred)
  590. (sort cl-seq (function (lambda (cl-x cl-y)
  591. (funcall cl-pred (funcall cl-key cl-x)
  592. (funcall cl-key cl-y)))))))))
  593. ;;;###autoload
  594. (defun stable-sort (cl-seq cl-pred &rest cl-keys)
  595. "Sort the argument SEQ stably according to PREDICATE.
  596. This is a destructive function; it reuses the storage of SEQ if possible.
  597. \nKeywords supported: :key
  598. \n(fn SEQ PREDICATE [KEYWORD VALUE]...)"
  599. (apply 'sort* cl-seq cl-pred cl-keys))
  600. ;;;###autoload
  601. (defun merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys)
  602. "Destructively merge the two sequences to produce a new sequence.
  603. TYPE is the sequence type to return, SEQ1 and SEQ2 are the two argument
  604. sequences, and PREDICATE is a `less-than' predicate on the elements.
  605. \nKeywords supported: :key
  606. \n(fn TYPE SEQ1 SEQ2 PREDICATE [KEYWORD VALUE]...)"
  607. (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil)))
  608. (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil)))
  609. (cl-parsing-keywords (:key) ()
  610. (let ((cl-res nil))
  611. (while (and cl-seq1 cl-seq2)
  612. (if (funcall cl-pred (cl-check-key (car cl-seq2))
  613. (cl-check-key (car cl-seq1)))
  614. (push (pop cl-seq2) cl-res)
  615. (push (pop cl-seq1) cl-res)))
  616. (coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type))))
  617. ;;; See compiler macro in cl-macs.el
  618. ;;;###autoload
  619. (defun member* (cl-item cl-list &rest cl-keys)
  620. "Find the first occurrence of ITEM in LIST.
  621. Return the sublist of LIST whose car is ITEM.
  622. \nKeywords supported: :test :test-not :key
  623. \n(fn ITEM LIST [KEYWORD VALUE]...)"
  624. (if cl-keys
  625. (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
  626. (while (and cl-list (not (cl-check-test cl-item (car cl-list))))
  627. (setq cl-list (cdr cl-list)))
  628. cl-list)
  629. (if (and (numberp cl-item) (not (integerp cl-item)))
  630. (member cl-item cl-list)
  631. (memq cl-item cl-list))))
  632. ;;;###autoload
  633. (defun member-if (cl-pred cl-list &rest cl-keys)
  634. "Find the first item satisfying PREDICATE in LIST.
  635. Return the sublist of LIST whose car matches.
  636. \nKeywords supported: :key
  637. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  638. (apply 'member* nil cl-list :if cl-pred cl-keys))
  639. ;;;###autoload
  640. (defun member-if-not (cl-pred cl-list &rest cl-keys)
  641. "Find the first item not satisfying PREDICATE in LIST.
  642. Return the sublist of LIST whose car matches.
  643. \nKeywords supported: :key
  644. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  645. (apply 'member* nil cl-list :if-not cl-pred cl-keys))
  646. ;;;###autoload
  647. (defun cl-adjoin (cl-item cl-list &rest cl-keys)
  648. (if (cl-parsing-keywords (:key) t
  649. (apply 'member* (cl-check-key cl-item) cl-list cl-keys))
  650. cl-list
  651. (cons cl-item cl-list)))
  652. ;;; See compiler macro in cl-macs.el
  653. ;;;###autoload
  654. (defun assoc* (cl-item cl-alist &rest cl-keys)
  655. "Find the first item whose car matches ITEM in LIST.
  656. \nKeywords supported: :test :test-not :key
  657. \n(fn ITEM LIST [KEYWORD VALUE]...)"
  658. (if cl-keys
  659. (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
  660. (while (and cl-alist
  661. (or (not (consp (car cl-alist)))
  662. (not (cl-check-test cl-item (car (car cl-alist))))))
  663. (setq cl-alist (cdr cl-alist)))
  664. (and cl-alist (car cl-alist)))
  665. (if (and (numberp cl-item) (not (integerp cl-item)))
  666. (assoc cl-item cl-alist)
  667. (assq cl-item cl-alist))))
  668. ;;;###autoload
  669. (defun assoc-if (cl-pred cl-list &rest cl-keys)
  670. "Find the first item whose car satisfies PREDICATE in LIST.
  671. \nKeywords supported: :key
  672. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  673. (apply 'assoc* nil cl-list :if cl-pred cl-keys))
  674. ;;;###autoload
  675. (defun assoc-if-not (cl-pred cl-list &rest cl-keys)
  676. "Find the first item whose car does not satisfy PREDICATE in LIST.
  677. \nKeywords supported: :key
  678. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  679. (apply 'assoc* nil cl-list :if-not cl-pred cl-keys))
  680. ;;;###autoload
  681. (defun rassoc* (cl-item cl-alist &rest cl-keys)
  682. "Find the first item whose cdr matches ITEM in LIST.
  683. \nKeywords supported: :test :test-not :key
  684. \n(fn ITEM LIST [KEYWORD VALUE]...)"
  685. (if (or cl-keys (numberp cl-item))
  686. (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
  687. (while (and cl-alist
  688. (or (not (consp (car cl-alist)))
  689. (not (cl-check-test cl-item (cdr (car cl-alist))))))
  690. (setq cl-alist (cdr cl-alist)))
  691. (and cl-alist (car cl-alist)))
  692. (rassq cl-item cl-alist)))
  693. ;;;###autoload
  694. (defun rassoc-if (cl-pred cl-list &rest cl-keys)
  695. "Find the first item whose cdr satisfies PREDICATE in LIST.
  696. \nKeywords supported: :key
  697. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  698. (apply 'rassoc* nil cl-list :if cl-pred cl-keys))
  699. ;;;###autoload
  700. (defun rassoc-if-not (cl-pred cl-list &rest cl-keys)
  701. "Find the first item whose cdr does not satisfy PREDICATE in LIST.
  702. \nKeywords supported: :key
  703. \n(fn PREDICATE LIST [KEYWORD VALUE]...)"
  704. (apply 'rassoc* nil cl-list :if-not cl-pred cl-keys))
  705. ;;;###autoload
  706. (defun union (cl-list1 cl-list2 &rest cl-keys)
  707. "Combine LIST1 and LIST2 using a set-union operation.
  708. The resulting list contains all items that appear in either LIST1 or LIST2.
  709. This is a non-destructive function; it makes a copy of the data if necessary
  710. to avoid corrupting the original LIST1 and LIST2.
  711. \nKeywords supported: :test :test-not :key
  712. \n(fn LIST1 LIST2 [KEYWORD VALUE]...)"
  713. (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1)
  714. ((equal cl-list1 cl-list2) cl-list1)
  715. (t
  716. (or (>= (length cl-list1) (length cl-list2))
  717. (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1))))
  718. (while cl-list2
  719. (if (or cl-keys (numberp (car cl-list2)))
  720. (setq cl-list1 (apply '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 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 'union cl-list1 cl-list2 cl-keys))))
  735. ;;;###autoload
  736. (defun 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 '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 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 'intersection cl-list1 cl-list2 cl-keys)))
  766. ;;;###autoload
  767. (defun 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 '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 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 'set-difference cl-list1 cl-list2 cl-keys)))
  795. ;;;###autoload
  796. (defun 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 'set-difference cl-list1 cl-list2 cl-keys)
  806. (apply 'set-difference cl-list2 cl-list1 cl-keys)))))
  807. ;;;###autoload
  808. (defun 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 'nset-difference cl-list1 cl-list2 cl-keys)
  818. (apply 'nset-difference cl-list2 cl-list1 cl-keys)))))
  819. ;;;###autoload
  820. (defun 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 'member* (cl-check-key (car cl-list1))
  830. cl-list2 cl-keys))
  831. (pop cl-list1))
  832. (null cl-list1)))))
  833. ;;;###autoload
  834. (defun 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 'sublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
  840. ;;;###autoload
  841. (defun 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 'sublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
  847. ;;;###autoload
  848. (defun 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 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys))
  855. ;;;###autoload
  856. (defun 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 'nsublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys))
  862. ;;;###autoload
  863. (defun 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 'nsublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys))
  869. ;;;###autoload
  870. (defun sublis (cl-alist cl-tree &rest cl-keys)
  871. "Perform substitutions indicated by ALIST in TREE (non-destructively).
  872. Return a copy of TREE with all matching elements replaced.
  873. \nKeywords supported: :test :test-not :key
  874. \n(fn ALIST TREE [KEYWORD VALUE]...)"
  875. (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
  876. (cl-sublis-rec cl-tree)))
  877. (defvar cl-alist)
  878. (defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if*
  879. (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist))
  880. (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
  881. (setq cl-p (cdr cl-p)))
  882. (if cl-p (cdr (car cl-p))
  883. (if (consp cl-tree)
  884. (let ((cl-a (cl-sublis-rec (car cl-tree)))
  885. (cl-d (cl-sublis-rec (cdr cl-tree))))
  886. (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree)))
  887. cl-tree
  888. (cons cl-a cl-d)))
  889. cl-tree))))
  890. ;;;###autoload
  891. (defun nsublis (cl-alist cl-tree &rest cl-keys)
  892. "Perform substitutions indicated by ALIST in TREE (destructively).
  893. Any matching element of TREE is changed via a call to `setcar'.
  894. \nKeywords supported: :test :test-not :key
  895. \n(fn ALIST TREE [KEYWORD VALUE]...)"
  896. (cl-parsing-keywords (:test :test-not :key :if :if-not) ()
  897. (let ((cl-hold (list cl-tree)))
  898. (cl-nsublis-rec cl-hold)
  899. (car cl-hold))))
  900. (defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if*
  901. (while (consp cl-tree)
  902. (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist))
  903. (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
  904. (setq cl-p (cdr cl-p)))
  905. (if cl-p (setcar cl-tree (cdr (car cl-p)))
  906. (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree))))
  907. (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist)
  908. (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp)))
  909. (setq cl-p (cdr cl-p)))
  910. (if cl-p
  911. (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil))
  912. (setq cl-tree (cdr cl-tree))))))
  913. ;;;###autoload
  914. (defun tree-equal (cl-x cl-y &rest cl-keys)
  915. "Return t if trees TREE1 and TREE2 have `eql' leaves.
  916. Atoms are compared by `eql'; cons cells are compared recursively.
  917. \nKeywords supported: :test :test-not :key
  918. \n(fn TREE1 TREE2 [KEYWORD VALUE]...)"
  919. (cl-parsing-keywords (:test :test-not :key) ()
  920. (cl-tree-equal-rec cl-x cl-y)))
  921. (defun cl-tree-equal-rec (cl-x cl-y)
  922. (while (and (consp cl-x) (consp cl-y)
  923. (cl-tree-equal-rec (car cl-x) (car cl-y)))
  924. (setq cl-x (cdr cl-x) cl-y (cdr cl-y)))
  925. (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y)))
  926. (run-hooks 'cl-seq-load-hook)
  927. ;; Local variables:
  928. ;; byte-compile-dynamic: t
  929. ;; byte-compile-warnings: (not cl-functions)
  930. ;; generated-autoload-file: "cl-loaddefs.el"
  931. ;; End:
  932. ;;; cl-seq.el ends here