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!")
18 (defun sequencep (thing)
19 (or (listp thing) (vectorp thing)))
21 (defun not-seq-error (thing)
22 (error "`~S' is not of type SEQUENCE" thing))
24 (defmacro do-sequence ((elt seq &optional (index (gensym "i") index-p)) &body body)
25 (let ((nseq (gensym "seq")))
27 (error "`~S' must be a symbol." elt))
37 (dotimes (,index (length ,nseq))
38 (let ((,elt (aref ,nseq ,index)))
41 (defun find (item seq &key key (test #'eql testp) (test-not #'eql test-not-p))
43 (when (satisfies-test-p item x :key key :test test :testp testp
44 :test-not test-not :test-not-p test-not-p)
47 (defun find-if (predicate sequence &key key)
49 (do-sequence (x sequence)
50 (when (funcall predicate (funcall key x))
52 (do-sequence (x sequence)
53 (when (funcall predicate x)
56 (defun position (elt sequence
57 &key key (test #'eql testp)
58 (test-not #'eql test-not-p)
60 ;; TODO: Implement START and END efficiently for all the sequence
62 (let ((end (or end (length sequence))))
63 (do-sequence (x sequence index)
64 (when (and (<= start index)
66 (satisfies-test-p elt x
67 :key key :test test :testp testp
68 :test-not test-not :test-not-p test-not-p))
71 ;; TODO: need to support &key from-end
72 (defun position-if (predicate sequence
73 &key key (start 0) end)
74 ;; TODO: Implement START and END efficiently for all the sequence
76 (let ((end (or end (length sequence))))
77 (do-sequence (x sequence index)
78 (when (and (<= start index)
80 (funcall predicate (if key (funcall key x) x)))
83 (defun position-if-not (predicate sequence
84 &key key (start 0) end)
85 (position-if (complement predicate) sequence :key key :start start :end end))
87 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
92 (let* ((head (cons nil nil))
94 (do-sequence (elt seq)
95 (unless (satisfies-test-p x elt :key key :test test :testp testp
96 :test-not test-not :test-not-p test-not-p)
97 (let ((new (list elt)))
103 (do-sequence (elt seq index)
104 (if (satisfies-test-p x elt :key key :test test :testp testp
105 :test-not test-not :test-not-p test-not-p)
106 ;; Copy the beginning of the vector only when we find an element
107 ;; that does not match.
109 (setq vector (make-array 0))
111 (vector-push-extend (aref seq i) vector)))
113 (vector-push-extend elt vector))))
117 (defun some (function seq)
118 (do-sequence (elt seq)
119 (when (funcall function elt)
120 (return-from some t))))
122 (defun every (function seq)
123 (do-sequence (elt seq)
124 (unless (funcall function elt)
125 (return-from every nil)))
128 (defun remove-if (func seq)
130 ((listp seq) (list-remove-if func seq nil))
131 ((arrayp seq) (vector-remove-if func seq nil))
132 (t (not-seq-error seq))))
134 (defun remove-if-not (func seq)
136 ((listp seq) (list-remove-if func seq t))
137 ((arrayp seq) (vector-remove-if func seq t))
138 (t (not-seq-error seq))))
140 (defun list-remove-if (func list negate)
143 (let ((test (funcall func (car list))))
144 (if (if negate (not test) test)
145 (list-remove-if func (cdr list) negate)
146 (cons (car list) (list-remove-if func (cdr list) negate))))))
148 (defun vector-remove-if (func vector negate)
149 (let ((out-vector (make-array 0)))
150 (do-sequence (element vector i)
151 (let ((test (funcall func element)))
152 (when (if negate test (not test))
153 (vector-push-extend element out-vector))))
156 (defun subseq (seq a &optional b)
160 (let ((diff (- b a)))
164 (error "Start index must be smaller than end index"))
166 (let* ((drop-a (copy-list (nthcdr a seq)))
168 (dotimes (_ (1- diff))
169 (setq pointer (cdr pointer))
171 (error "Ending index larger than length of list")))
174 (copy-list (nthcdr a seq))))
176 (let* ((b (or b (length seq)))
178 (new (make-array size :element-type (array-element-type seq))))
182 (aset new i (aref seq j)))))
183 (t (not-seq-error seq))))
185 (defun copy-seq (sequence)
189 ;;; Reduce (based on SBCL's version)
191 (defun reduce (function sequence &key key from-end (start 0) end (initial-value nil ivp))
192 (let ((key (or key #'identity))
193 (end (or end (length sequence))))
195 (if ivp initial-value (funcall function))
196 (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end)
199 `(reverse (nthcdr ,start ,sequence))
200 `(nthcdr ,start ,sequence))))
201 (do ((count (if ,ivp ,start (1+ ,start))
203 (sequence (if ,ivp sequence (cdr sequence))
205 (value (if ,ivp ,initial-value (funcall ,key (car sequence)))
207 `(funcall ,function (funcall ,key (car sequence)) value)
208 `(funcall ,function value (funcall ,key (car sequence))))))
209 ((>= count ,end) value)))))
211 (reduce-list function sequence key start end initial-value ivp t)
212 (reduce-list function sequence key start end initial-value ivp nil))))))
214 (defun elt (sequence index)
216 (error "The index ~D is below zero." index))
220 (dolist (elt sequence)
222 (return-from elt elt))
224 (error "The index ~D is too large for ~A of length ~D." index 'list i)))
226 (let ((length (length sequence)))
227 (when (>= index length)
228 (error "The index ~D is too large for ~A of length ~D." index 'vector length))
229 (aref sequence index)))))
231 (defun mismatch (sequence1 sequence2 &key key (test #'eql testp) (test-not nil test-not-p)
232 (start1 0) (end1 (length sequence1))
233 (start2 0) (end2 (length sequence2)))
234 (let ((index1 start1)
236 (while (and (<= index1 end1) (<= index2 end2))
237 (when (or (eql index1 end1) (eql index2 end2))
238 (return-from mismatch (if (eql end1 end2) NIL index1)))
239 (unless (satisfies-test-p (elt sequence1 index1) (elt sequence2 index2)
240 :key key :test test :testp testp
241 :test-not test-not :test-not-p test-not-p)
242 (return-from mismatch index1))
246 (defun list-search (sequence1 list2 args)
247 (let ((length1 (length sequence1))
250 (let ((mismatch (apply #'mismatch sequence1 list2 args)))
251 (when (or (not mismatch) (>= mismatch length1))
252 (return-from list-search position)))
256 (defun vector-search (sequence1 vector2 args)
257 (let ((length1 (length sequence1)))
258 (dotimes (position (length vector2))
259 (let ((mismatch (apply #'mismatch sequence1 (subseq vector2 position) args)))
260 (when (or (not mismatch) (>= mismatch length1))
261 (return-from vector-search position))))))
263 (defun search (sequence1 sequence2 &rest args &key key test test-not)
264 (unless (sequencep sequence1)
265 (not-seq-error sequence1))
266 (when (or (and (listp sequence1) (null sequence1))
267 (and (vectorp sequence1) (zerop (length sequence1))))
268 (return-from search 0))
272 (array #'vector-search)
273 (t (not-seq-error sequence2)))
274 sequence1 sequence2 args))