Fix comment
[jscl.git] / src / sequence.lisp
1 ;;; sequence.lisp
2
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.
7 ;;
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.
12 ;;
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/>.
15
16 (/debug "loading sequence.lisp!")
17
18 (defun length (seq)
19   (cond
20     ((stringp seq)
21      (string-length seq))
22     ((arrayp seq)
23      (oget seq "length"))
24     ((listp seq)
25      (list-length seq))))
26
27 (defun sequencep (thing)
28   (or (listp thing) (vectorp thing)))
29
30 (defun not-seq-error (thing)
31   (error "`~S' is not of type SEQUENCE" thing))
32
33 (defmacro do-sequence ((elt seq &optional (index (gensym "i") index-p)) &body body)
34   (let ((nseq (gensym "seq")))
35     (unless (symbolp elt)
36       (error "`~S' must be a symbol." elt))
37     `(let ((,nseq ,seq))
38        (if (listp ,nseq)
39            ,(if index-p
40                 `(let ((,index -1))
41                    (dolist (,elt ,nseq)
42                      (incf ,index)
43                      ,@body))
44                 `(dolist (,elt ,nseq)
45                    ,@body))
46            (dotimes (,index (length ,nseq))
47              (let ((,elt (aref ,nseq ,index)))
48                ,@body))))))
49
50 (defun find (item seq &key key (test #'eql testp) (test-not #'eql test-not-p))
51   (do-sequence (x seq)
52     (when (satisfies-test-p item x :key key :test test :testp testp
53                             :test-not test-not :test-not-p test-not-p)
54       (return x))))
55
56 (defun find-if (predicate sequence &key key)
57   (if key
58       (do-sequence (x sequence)
59         (when (funcall predicate (funcall key x))
60           (return x)))
61       (do-sequence (x sequence)
62         (when (funcall predicate x)
63           (return x)))))
64
65 (defun position (elt sequence
66                  &key key (test #'eql testp)
67                    (test-not #'eql test-not-p)
68                    (start 0) end)
69   ;; TODO: Implement START and END efficiently for all the sequence
70   ;; functions.
71   (let ((end (or end (length sequence))))
72     (do-sequence (x sequence index)
73       (when (and (<= start index)
74                  (< index end)
75                  (satisfies-test-p elt x
76                                    :key key :test test :testp testp
77                                    :test-not test-not :test-not-p test-not-p))
78         (return index)))))
79
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
84   ;; functions.
85   (let ((end (or end (length sequence))))
86     (do-sequence (x sequence index)
87       (when (and (<= start index)
88                  (< index end)
89                  (funcall predicate (if key (funcall key x) x)))
90         (return index)))))
91
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))
95
96 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
97   (cond
98     ((null seq)
99      nil)
100     ((listp seq)
101      (let* ((head (cons nil nil))
102             (tail head))
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)))
107              (rplacd tail new)
108              (setq tail new))))
109        (cdr head)))
110     (t
111      (let (vector)
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.
117              (unless vector
118                (setq vector (make-array 0))
119                (dotimes (i index)
120                  (vector-push-extend (aref seq i) vector)))
121              (when vector
122                (vector-push-extend elt vector))))
123        (or vector seq)))))
124
125
126 (defun some (function seq)
127   (do-sequence (elt seq)
128     (when (funcall function elt)
129       (return-from some t))))
130
131 (defun every (function seq)
132   (do-sequence (elt seq)
133     (unless (funcall function elt)
134       (return-from every nil)))
135   t)
136
137 (defun remove-if (func seq)
138   (cond
139     ((listp  seq) (list-remove-if   func seq nil))
140     ((arrayp seq) (vector-remove-if func seq nil))
141     (t (not-seq-error seq))))
142
143 (defun remove-if-not (func seq)
144   (cond
145     ((listp  seq) (list-remove-if   func seq t))
146     ((arrayp seq) (vector-remove-if func seq t))
147     (t (not-seq-error seq))))
148
149 (defun list-remove-if (func list negate)
150   (if (endp list)
151     ()
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))))))
156
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))))
163     out-vector))
164
165 (defun subseq (seq a &optional b)
166   (cond
167     ((listp seq)
168      (if b
169        (let ((diff (- b a)))
170          (cond
171            ((zerop  diff) ())
172            ((minusp diff)
173             (error "Start index must be smaller than end index"))
174            (t
175             (let* ((drop-a (copy-list (nthcdr a seq)))
176                    (pointer drop-a))
177               (dotimes (_ (1- diff))
178                 (setq pointer (cdr pointer))
179                 (when (null pointer)
180                   (error "Ending index larger than length of list")))
181               (rplacd pointer ())
182               drop-a))))
183        (copy-list (nthcdr a seq))))
184     ((vectorp seq)
185      (let* ((b (or b (length seq)))
186             (size (- b a))
187             (new (make-array size :element-type (array-element-type seq))))
188        (do ((i 0 (1+ i))
189             (j a (1+ j)))
190            ((= j b) new)
191          (aset new i (aref seq j)))))
192     (t (not-seq-error seq))))
193
194 (defun copy-seq (sequence)
195   (subseq sequence 0))
196
197
198 ;;; Reduce (based on SBCL's version)
199
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))))
203     (if (= end start)
204         (if ivp initial-value (funcall function))
205         (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end)
206                      `(let ((sequence
207                              ,(if from-end
208                                   `(reverse (nthcdr ,start ,sequence))
209                                   `(nthcdr ,start ,sequence))))
210                         (do ((count (if ,ivp ,start (1+ ,start))
211                                     (1+ count))
212                              (sequence (if ,ivp sequence (cdr sequence))
213                                        (cdr sequence))
214                              (value (if ,ivp ,initial-value (funcall ,key (car sequence)))
215                                     ,(if from-end
216                                          `(funcall ,function (funcall ,key (car sequence)) value)
217                                          `(funcall ,function value (funcall ,key (car sequence))))))
218                             ((>= count ,end) value)))))
219           (if from-end
220               (reduce-list function sequence key start end initial-value ivp t)
221               (reduce-list function sequence key start end initial-value ivp nil))))))
222
223 (defun elt (sequence index)
224   (when (< index 0)
225     (error "The index ~D is below zero." index))
226   (etypecase sequence
227     (list
228      (let ((i 0))
229        (dolist (elt sequence)
230          (when (eql index i)
231            (return-from elt elt))
232          (incf i))
233        (error "The index ~D is too large for ~A of length ~D." index 'list i)))
234     (array
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)))))
239
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)
244         (index2 start2))
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))
252       (incf index1)
253       (incf index2))))
254
255 (defun list-search (sequence1 list2 args)
256   (let ((length1 (length sequence1))
257         (position 0))
258     (while list2
259       (let ((mismatch (apply #'mismatch sequence1 list2 args)))
260         (when (or (not mismatch) (>= mismatch length1))
261           (return-from list-search position)))
262       (pop list2)
263       (incf position))))
264
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))))))
271
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))
278   (funcall
279    (typecase sequence2
280      (list #'list-search)
281      (array #'vector-search)
282      (t (not-seq-error sequence2)))
283    sequence1 sequence2 args))