3 ;; JSCL is free software: you can redistribute it and/or
4 ;; modify it under the terms of the GNU General Public License as
5 ;; published by the Free Software Foundation, either version 3 of the
6 ;; License, or (at your option) any later version.
8 ;; JSCL is distributed in the hope that it will be useful, but
9 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;; General Public License for more details.
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
16 (/debug "loading sequence.lisp!")
27 (defun sequencep (thing)
28 (or (listp thing) (vectorp thing)))
30 (defun not-seq-error (thing)
31 (error "`~S' is not of type SEQUENCE" thing))
33 (defmacro do-sequence ((elt seq &optional (index (gensym "i") index-p)) &body body)
34 (let ((nseq (gensym "seq")))
36 (error "`~S' must be a symbol." elt))
46 (dotimes (,index (length ,nseq))
47 (let ((,elt (aref ,nseq ,index)))
50 (defun find (item seq &key key (test #'eql testp) (test-not #'eql test-not-p))
52 (when (satisfies-test-p item x :key key :test test :testp testp
53 :test-not test-not :test-not-p test-not-p)
56 (defun find-if (predicate sequence &key key)
58 (do-sequence (x sequence)
59 (when (funcall predicate (funcall key x))
61 (do-sequence (x sequence)
62 (when (funcall predicate x)
65 (defun position (elt sequence
66 &key key (test #'eql testp)
67 (test-not #'eql test-not-p)
69 ;; TODO: Implement START and END efficiently for all the sequence
71 (let ((end (or end (length sequence))))
72 (do-sequence (x sequence index)
73 (when (and (<= start index)
75 (satisfies-test-p elt x
76 :key key :test test :testp testp
77 :test-not test-not :test-not-p test-not-p))
80 ;; TODO: need to support &key from-end
81 (defun position-if (predicate sequence
82 &key key (start 0) end)
83 ;; TODO: Implement START and END efficiently for all the sequence
85 (let ((end (or end (length sequence))))
86 (do-sequence (x sequence index)
87 (when (and (<= start index)
89 (funcall predicate (if key (funcall key x) x)))
92 (defun position-if-not (predicate sequence
93 &key key (start 0) end)
94 (position-if (complement predicate) sequence :key key :start start :end end))
96 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
101 (let* ((head (cons nil nil))
103 (do-sequence (elt seq)
104 (unless (satisfies-test-p x elt :key key :test test :testp testp
105 :test-not test-not :test-not-p test-not-p)
106 (let ((new (list elt)))
112 (do-sequence (elt seq index)
113 (if (satisfies-test-p x elt :key key :test test :testp testp
114 :test-not test-not :test-not-p test-not-p)
115 ;; Copy the beginning of the vector only when we find an element
116 ;; that does not match.
118 (setq vector (make-array 0))
120 (vector-push-extend (aref seq i) vector)))
122 (vector-push-extend elt vector))))
126 (defun some (function seq)
127 (do-sequence (elt seq)
128 (when (funcall function elt)
129 (return-from some t))))
131 (defun every (function seq)
132 (do-sequence (elt seq)
133 (unless (funcall function elt)
134 (return-from every nil)))
137 (defun remove-if (func seq)
139 ((listp seq) (list-remove-if func seq nil))
140 ((arrayp seq) (vector-remove-if func seq nil))
141 (t (not-seq-error seq))))
143 (defun remove-if-not (func seq)
145 ((listp seq) (list-remove-if func seq t))
146 ((arrayp seq) (vector-remove-if func seq t))
147 (t (not-seq-error seq))))
149 (defun list-remove-if (func list negate)
152 (let ((test (funcall func (car list))))
153 (if (if negate (not test) test)
154 (list-remove-if func (cdr list) negate)
155 (cons (car list) (list-remove-if func (cdr list) negate))))))
157 (defun vector-remove-if (func vector negate)
158 (let ((out-vector (make-array 0)))
159 (do-sequence (element vector i)
160 (let ((test (funcall func element)))
161 (when (if negate test (not test))
162 (vector-push-extend element out-vector))))
165 (defun subseq (seq a &optional b)
169 (let ((diff (- b a)))
173 (error "Start index must be smaller than end index"))
175 (let* ((drop-a (copy-list (nthcdr a seq)))
177 (dotimes (_ (1- diff))
178 (setq pointer (cdr pointer))
180 (error "Ending index larger than length of list")))
183 (copy-list (nthcdr a seq))))
185 (let* ((b (or b (length seq)))
187 (new (make-array size :element-type (array-element-type seq))))
191 (aset new i (aref seq j)))))
192 (t (not-seq-error seq))))
194 (defun copy-seq (sequence)
198 ;;; Reduce (based on SBCL's version)
200 (defun reduce (function sequence &key key from-end (start 0) end (initial-value nil ivp))
201 (let ((key (or key #'identity))
202 (end (or end (length sequence))))
204 (if ivp initial-value (funcall function))
205 (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end)
208 `(reverse (nthcdr ,start ,sequence))
209 `(nthcdr ,start ,sequence))))
210 (do ((count (if ,ivp ,start (1+ ,start))
212 (sequence (if ,ivp sequence (cdr sequence))
214 (value (if ,ivp ,initial-value (funcall ,key (car sequence)))
216 `(funcall ,function (funcall ,key (car sequence)) value)
217 `(funcall ,function value (funcall ,key (car sequence))))))
218 ((>= count ,end) value)))))
220 (reduce-list function sequence key start end initial-value ivp t)
221 (reduce-list function sequence key start end initial-value ivp nil))))))
223 (defun elt (sequence index)
225 (error "The index ~D is below zero." index))
229 (dolist (elt sequence)
231 (return-from elt elt))
233 (error "The index ~D is too large for ~A of length ~D." index 'list i)))
235 (let ((length (length sequence)))
236 (when (>= index length)
237 (error "The index ~D is too large for ~A of length ~D." index 'vector length))
238 (aref sequence index)))))
240 (defun mismatch (sequence1 sequence2 &key key (test #'eql testp) (test-not nil test-not-p)
241 (start1 0) (end1 (length sequence1))
242 (start2 0) (end2 (length sequence2)))
243 (let ((index1 start1)
245 (while (and (<= index1 end1) (<= index2 end2))
246 (when (or (eql index1 end1) (eql index2 end2))
247 (return-from mismatch (if (eql end1 end2) NIL index1)))
248 (unless (satisfies-test-p (elt sequence1 index1) (elt sequence2 index2)
249 :key key :test test :testp testp
250 :test-not test-not :test-not-p test-not-p)
251 (return-from mismatch index1))
255 (defun list-search (sequence1 list2 args)
256 (let ((length1 (length sequence1))
259 (let ((mismatch (apply #'mismatch sequence1 list2 args)))
260 (when (or (not mismatch) (>= mismatch length1))
261 (return-from list-search position)))
265 (defun vector-search (sequence1 vector2 args)
266 (let ((length1 (length sequence1)))
267 (dotimes (position (length vector2))
268 (let ((mismatch (apply #'mismatch sequence1 (subseq vector2 position) args)))
269 (when (or (not mismatch) (>= mismatch length1))
270 (return-from vector-search position))))))
272 (defun search (sequence1 sequence2 &rest args &key key test test-not)
273 (unless (sequencep sequence1)
274 (not-seq-error sequence1))
275 (when (or (and (listp sequence1) (null sequence1))
276 (and (vectorp sequence1) (zerop (length sequence1))))
277 (return-from search 0))
281 (array #'vector-search)
282 (t (not-seq-error sequence2)))
283 sequence1 sequence2 args))