be36e994fedeef7904f48a9c61b48c6b91dcb71f
[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 not-seq-error (thing)
19   (error "`~S' is not of type SEQUENCE" thing))
20
21 (defmacro do-sequence ((elt seq &optional (index (gensym "i") index-p)) &body body)
22   (let ((nseq (gensym "seq")))
23     (unless (symbolp elt)
24       (error "`~S' must be a symbol." elt))
25     `(let ((,nseq ,seq))
26        (if (listp ,nseq)
27            ,(if index-p
28                 `(let ((,index -1))
29                    (dolist (,elt ,nseq)
30                      (incf ,index)
31                      ,@body))
32                 `(dolist (,elt ,nseq)
33                    ,@body))
34            (dotimes (,index (length ,nseq))
35              (let ((,elt (aref ,nseq ,index)))
36                ,@body))))))
37
38 (defun find (item seq &key key (test #'eql testp) (test-not #'eql test-not-p))
39   (do-sequence (x seq)
40     (when (satisfies-test-p item x :key key :test test :testp testp
41                             :test-not test-not :test-not-p test-not-p)
42       (return x))))
43
44 (defun find-if (predicate sequence &key key)
45   (if key
46       (do-sequence (x sequence)
47         (when (funcall predicate (funcall key x))
48           (return x)))
49       (do-sequence (x sequence)
50         (when (funcall predicate x)
51           (return x)))))
52
53 (defun position (elt sequence
54                  &key key (test #'eql testp)
55                    (test-not #'eql test-not-p)
56                    (start 0) end)
57   ;; TODO: Implement START and END efficiently for all the sequence
58   ;; functions.
59   (let ((end (or end (length sequence))))
60     (do-sequence (x sequence index)
61       (when (and (<= start index)
62                  (< index end)
63                  (satisfies-test-p elt x
64                                    :key key :test test :testp testp
65                                    :test-not test-not :test-not-p test-not-p))
66         (return index)))))
67
68 ;; TODO: need to support &key from-end
69 (defun position-if (predicate sequence
70                  &key key (start 0) end)
71   ;; TODO: Implement START and END efficiently for all the sequence
72   ;; functions.
73   (let ((end (or end (length sequence))))
74     (do-sequence (x sequence index)
75       (when (and (<= start index)
76                  (< index end)
77                  (funcall predicate (if key (funcall key x) x)))
78         (return index)))))
79
80 (defun position-if-not (predicate sequence
81                  &key key (start 0) end)
82   (position-if (complement predicate) sequence :key key :start start :end end))
83
84 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
85   (cond
86     ((null seq)
87      nil)
88     ((listp seq)
89      (let* ((head (cons nil nil))
90             (tail head))
91        (do-sequence (elt seq)
92          (unless (satisfies-test-p x elt :key key :test test :testp testp
93                                    :test-not test-not :test-not-p test-not-p)
94            (let ((new (list elt)))
95              (rplacd tail new)
96              (setq tail new))))
97        (cdr head)))
98     (t
99      (let (vector)
100        (do-sequence (elt seq index)
101          (if (satisfies-test-p x elt :key key :test test :testp testp
102                                :test-not test-not :test-not-p test-not-p)
103              ;; Copy the beginning of the vector only when we find an element
104              ;; that does not match.
105              (unless vector
106                (setq vector (make-array 0))
107                (dotimes (i index)
108                  (vector-push-extend (aref seq i) vector)))
109              (when vector
110                (vector-push-extend elt vector))))
111        (or vector seq)))))
112
113
114 (defun some (function seq)
115   (do-sequence (elt seq)
116     (when (funcall function elt)
117       (return-from some t))))
118
119 (defun every (function seq)
120   (do-sequence (elt seq)
121     (unless (funcall function elt)
122       (return-from every nil)))
123   t)
124
125 (defun remove-if (func seq)
126   (cond
127     ((listp  seq) (list-remove-if   func seq nil))
128     ((arrayp seq) (vector-remove-if func seq nil))
129     (t (not-seq-error seq))))
130
131 (defun remove-if-not (func seq)
132   (cond
133     ((listp  seq) (list-remove-if   func seq t))
134     ((arrayp seq) (vector-remove-if func seq t))
135     (t (not-seq-error seq))))
136
137 (defun list-remove-if (func list negate)
138   (if (endp list)
139     ()
140     (let ((test (funcall func (car list))))
141       (if (if negate (not test) test)
142         (list-remove-if func (cdr list) negate)
143         (cons (car list) (list-remove-if func (cdr list) negate))))))
144
145 (defun vector-remove-if (func vector negate)
146   (let ((out-vector (make-array 0)))
147     (do-sequence (element vector i)
148       (let ((test (funcall func element)))
149         (when (if negate test (not test))
150           (vector-push-extend element out-vector))))
151     out-vector))
152
153 (defun subseq (seq a &optional b)
154   (cond
155     ((listp seq)
156      (if b
157        (let ((diff (- b a)))
158          (cond
159            ((zerop  diff) ())
160            ((minusp diff)
161             (error "Start index must be smaller than end index"))
162            (t
163             (let* ((drop-a (copy-list (nthcdr a seq)))
164                    (pointer drop-a))
165               (dotimes (_ (1- diff))
166                 (setq pointer (cdr pointer))
167                 (when (null pointer)
168                   (error "Ending index larger than length of list")))
169               (rplacd pointer ())
170               drop-a))))
171        (copy-list (nthcdr a seq))))
172     ((vectorp seq)
173      (let* ((b (or b (length seq)))
174             (size (- b a))
175             (new (make-array size :element-type (array-element-type seq))))
176        (do ((i 0 (1+ i))
177             (j a (1+ j)))
178            ((= j b) new)
179          (aset new i (aref seq j)))))
180     (t (not-seq-error seq))))
181
182 (defun copy-seq (sequence)
183   (subseq sequence 0))
184
185
186 ;;; Reduce (based on SBCL's version)
187
188 (defun reduce (function sequence &key key from-end (start 0) end (initial-value nil ivp))
189   (let ((key (or key #'identity))
190         (end (or end (length sequence))))
191     (if (= end start)
192         (if ivp initial-value (funcall function))
193         (macrolet ((reduce-list (function sequence key start end initial-value ivp from-end)
194                      `(let ((sequence
195                              ,(if from-end
196                                   `(reverse (nthcdr ,start ,sequence))
197                                   `(nthcdr ,start ,sequence))))
198                         (do ((count (if ,ivp ,start (1+ ,start))
199                                     (1+ count))
200                              (sequence (if ,ivp sequence (cdr sequence))
201                                        (cdr sequence))
202                              (value (if ,ivp ,initial-value (funcall ,key (car sequence)))
203                                     ,(if from-end
204                                          `(funcall ,function (funcall ,key (car sequence)) value)
205                                          `(funcall ,function value (funcall ,key (car sequence))))))
206                             ((>= count ,end) value)))))
207           (if from-end
208               (reduce-list function sequence key start end initial-value ivp t)
209               (reduce-list function sequence key start end initial-value ivp nil))))))