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