Change internal format of strings
[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 &key key (test #'eql testp)
52                      (test-not #'eql test-not-p))
53   (do-sequence (x sequence index)
54     (when (satisfies-test-p elt x :key key :test test :testp testp
55                            :test-not test-not :test-not-p test-not-p ) 
56       (return index))))
57
58 (defun remove (x seq &key key (test #'eql testp) (test-not #'eql test-not-p))
59   (cond
60     ((null seq)
61      nil)
62     ((listp seq)
63      (let* ((head (cons nil nil))
64             (tail head))
65        (do-sequence (elt seq)
66          (unless (satisfies-test-p x elt :key key :test test :testp testp 
67                                    :test-not test-not :test-not-p test-not-p)
68            (let ((new (list elt)))
69              (rplacd tail new)
70              (setq tail new))))
71        (cdr head)))
72     (t
73      (let (vector)
74        (do-sequence (elt seq index)
75          (if (satisfies-test-p x elt :key key :test test :testp testp 
76                                :test-not test-not :test-not-p test-not-p)
77              ;; Copy the beginning of the vector only when we find an element
78              ;; that does not match.
79              (unless vector
80                (setq vector (make-array 0))
81                (dotimes (i index)
82                  (vector-push-extend (aref seq i) vector)))
83              (when vector
84                (vector-push-extend elt vector))))
85        (or vector seq)))))
86
87
88 (defun some (function seq)
89   (do-sequence (elt seq)
90     (when (funcall function elt)
91       (return-from some t))))
92
93 (defun every (function seq)
94   (do-sequence (elt seq)
95     (unless (funcall function elt)
96       (return-from every nil)))
97   t)
98
99 (defun remove-if (func seq)
100   (cond
101     ((listp  seq) (list-remove-if   func seq nil))
102     ((arrayp seq) (vector-remove-if func seq nil))
103     (t (not-seq-error seq))))
104
105 (defun remove-if-not (func seq)
106   (cond
107     ((listp  seq) (list-remove-if   func seq t))
108     ((arrayp seq) (vector-remove-if func seq t))
109     (t (not-seq-error seq))))
110
111 (defun list-remove-if (func list negate)
112   (if (endp list)
113     ()
114     (let ((test (funcall func (car list))))
115       (if (if negate (not test) test)
116         (list-remove-if func (cdr list) negate)
117         (cons (car list) (list-remove-if func (cdr list) negate))))))
118
119 (defun vector-remove-if (func vector negate)
120   (let ((out-vector (make-array 0)))
121     (do-sequence (element vector i)
122       (let ((test (funcall func element)))
123         (when (if negate test (not test))
124           (vector-push-extend element out-vector))))
125     out-vector))
126
127 (defun subseq (seq a &optional b)
128   (cond
129     ((listp seq)
130      (if b
131        (let ((diff (- b a)))
132          (cond
133            ((zerop  diff) ()) 
134            ((minusp diff)
135             (error "Start index must be smaller than end index"))
136            (t
137             (let* ((drop-a (copy-list (nthcdr a seq)))
138                    (pointer drop-a))
139               (dotimes (_ (1- diff))
140                 (setq pointer (cdr pointer))
141                 (when (null pointer)
142                   (error "Ending index larger than length of list")))
143               (rplacd pointer ()) 
144               drop-a))))
145        (copy-list (nthcdr a seq))))
146     ((arrayp seq) 
147      (if b
148        (slice seq a b)
149        (slice seq a)))
150     (t (not-seq-error seq))))