880f7fd6edfb19ac755710a64f0d119c3dec5c8a
[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 sequencep (thing)
19   (or (listp thing) (vectorp thing)))
20
21 (defun not-seq-error (thing)
22   (error "`~S' is not of type SEQUENCE" thing))
23
24 (defmacro do-sequence ((elt seq &optional (index (gensym "i") index-p)) &body body)
25   (let ((nseq (gensym "seq")))
26     (unless (symbolp elt)
27       (error "`~S' must be a symbol." elt))
28     `(let ((,nseq ,seq))
29        (if (listp ,nseq)
30            ,(if index-p
31                 `(let ((,index -1))
32                    (dolist (,elt ,nseq)
33                      (incf ,index)
34                      ,@body))
35                 `(dolist (,elt ,nseq)
36                    ,@body))
37            (dotimes (,index (length ,nseq))
38              (let ((,elt (aref ,nseq ,index)))
39                ,@body))))))
40
41 (defun find (item seq &key key (test #'eql testp) (test-not #'eql test-not-p))
42   (do-sequence (x seq)
43     (when (satisfies-test-p item x :key key :test test :testp testp
44                             :test-not test-not :test-not-p test-not-p)
45       (return x))))
46
47 (defun find-if (predicate sequence &key key)
48   (if key
49       (do-sequence (x sequence)
50         (when (funcall predicate (funcall key x))
51           (return x)))
52       (do-sequence (x sequence)
53         (when (funcall predicate x)
54           (return x)))))
55
56 (defun position (elt sequence
57                  &key key (test #'eql testp)
58                    (test-not #'eql test-not-p)
59                    (start 0) end)
60   ;; TODO: Implement START and END efficiently for all the sequence
61   ;; functions.
62   (let ((end (or end (length sequence))))
63     (do-sequence (x sequence index)
64       (when (and (<= start index)
65                  (< index end)
66                  (satisfies-test-p elt x
67                                    :key key :test test :testp testp
68                                    :test-not test-not :test-not-p test-not-p))
69         (return index)))))
70
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
75   ;; functions.
76   (let ((end (or end (length sequence))))
77     (do-sequence (x sequence index)
78       (when (and (<= start index)
79                  (< index end)
80                  (funcall predicate (if key (funcall key x) x)))
81         (return index)))))
82
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))
86
87 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
88   (cond
89     ((null seq)
90      nil)
91     ((listp seq)
92      (let* ((head (cons nil nil))
93             (tail head))
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)))
98              (rplacd tail new)
99              (setq tail new))))
100        (cdr head)))
101     (t
102      (let (vector)
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.
108              (unless vector
109                (setq vector (make-array 0))
110                (dotimes (i index)
111                  (vector-push-extend (aref seq i) vector)))
112              (when vector
113                (vector-push-extend elt vector))))
114        (or vector seq)))))
115
116
117 (defun some (function seq)
118   (do-sequence (elt seq)
119     (when (funcall function elt)
120       (return-from some t))))
121
122 (defun every (function seq)
123   (do-sequence (elt seq)
124     (unless (funcall function elt)
125       (return-from every nil)))
126   t)
127
128 (defun remove-if (func seq)
129   (cond
130     ((listp  seq) (list-remove-if   func seq nil))
131     ((arrayp seq) (vector-remove-if func seq nil))
132     (t (not-seq-error seq))))
133
134 (defun remove-if-not (func seq)
135   (cond
136     ((listp  seq) (list-remove-if   func seq t))
137     ((arrayp seq) (vector-remove-if func seq t))
138     (t (not-seq-error seq))))
139
140 (defun list-remove-if (func list negate)
141   (if (endp list)
142     ()
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))))))
147
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))))
154     out-vector))
155
156 (defun subseq (seq a &optional b)
157   (cond
158     ((listp seq)
159      (if b
160        (let ((diff (- b a)))
161          (cond
162            ((zerop  diff) ())
163            ((minusp diff)
164             (error "Start index must be smaller than end index"))
165            (t
166             (let* ((drop-a (copy-list (nthcdr a seq)))
167                    (pointer drop-a))
168               (dotimes (_ (1- diff))
169                 (setq pointer (cdr pointer))
170                 (when (null pointer)
171                   (error "Ending index larger than length of list")))
172               (rplacd pointer ())
173               drop-a))))
174        (copy-list (nthcdr a seq))))
175     ((vectorp seq)
176      (let* ((b (or b (length seq)))
177             (size (- b a))
178             (new (make-array size :element-type (array-element-type seq))))
179        (do ((i 0 (1+ i))
180             (j a (1+ j)))
181            ((= j b) new)
182          (aset new i (aref seq j)))))
183     (t (not-seq-error seq))))
184
185 (defun copy-seq (sequence)
186   (subseq sequence 0))
187
188
189 ;;; Reduce (based on SBCL's version)
190
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))))
194     (if (= end start)
195         (if ivp initial-value (funcall function))
196         (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end)
197                      `(let ((sequence
198                              ,(if from-end
199                                   `(reverse (nthcdr ,start ,sequence))
200                                   `(nthcdr ,start ,sequence))))
201                         (do ((count (if ,ivp ,start (1+ ,start))
202                                     (1+ count))
203                              (sequence (if ,ivp sequence (cdr sequence))
204                                        (cdr sequence))
205                              (value (if ,ivp ,initial-value (funcall ,key (car sequence)))
206                                     ,(if from-end
207                                          `(funcall ,function (funcall ,key (car sequence)) value)
208                                          `(funcall ,function value (funcall ,key (car sequence))))))
209                             ((>= count ,end) value)))))
210           (if from-end
211               (reduce-list function sequence key start end initial-value ivp t)
212               (reduce-list function sequence key start end initial-value ivp nil))))))
213
214 (defun elt (sequence index)
215   (when (< index 0)
216     (error "The index ~D is below zero." index))
217   (etypecase sequence
218     (list
219      (let ((i 0))
220        (dolist (elt sequence)
221          (when (eql index i)
222            (return-from elt elt))
223          (incf i))
224        (error "The index ~D is too large for ~A of length ~D." index 'list i)))
225     (array
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)))))
230
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)
235         (index2 start2))
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))
243       (incf index1)
244       (incf index2))))
245
246 (defun list-search (sequence1 list2 args)
247   (let ((length1 (length sequence1))
248         (position 0))
249     (while list2
250       (let ((mismatch (apply #'mismatch sequence1 list2 args)))
251         (when (or (not mismatch) (>= mismatch length1))
252           (return-from list-search position)))
253       (pop list2)
254       (incf position))))
255
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))))))
262
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))
269   (funcall
270    (typecase sequence2
271      (list #'list-search)
272      (array #'vector-search)
273      (t (not-seq-error sequence2)))
274    sequence1 sequence2 args))