Debian Codegraph @

src/lsp/seq.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
6 ;;;; Copyright (c) 1990, Giuseppe Attardi.
7 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
8 ;;;;
9 ;;;; See file 'LICENSE' for the copyright details.
10
11 ;;;; sequence routines
12
13 (in-package "SYSTEM")
14
15 #+ecl-min
16 (eval-when (:execute)
17 (load (merge-pathnames "seqmacros.lsp" *load-truename*)))
18
19 (defun error-not-a-sequence (value)
20 (declare (si::c-local))
21 (signal-type-error value 'sequence))
22
23 (defun error-sequence-index (sequence index)
24 (declare (si::c-local))
25 (error 'simple-type-error
26 :datum index
27 :expected-type 'unsigned-byte
28 :format-control "Not a valid index ~A into sequence ~A"
29 :format-arguments (list index sequence)))
30
31 (defun error-sequence-type (type)
32 (declare (si::c-local))
33 (error 'simple-type-error
34 :datum (vector) ;; Any sequence object will do, because it does not belong to TYPE
35 :expected-type type
36 :format-control "~S does not specify a sequence type"
37 :format-arguments (list type)))
38
39 (defun error-sequence-length (object type size)
40 (declare (si::c-local))
41 (error 'simple-type-error
42 :format-control
43 "Cannot create a sequence of size ~S which matches type ~S."
44 :format-arguments (list size type)
45 :expected-type type
46 :datum object))
47
48 (defun closest-sequence-type (type)
49 (let (elt-type length name args)
50 (cond ((consp type)
51 (setq name (first type) args (cdr type)))
52 ((si::instancep type)
53 (setf name (class-name (truly-the class type)) args nil))
54 (t
55 (setq name type args nil)))
56 (case name
57 ((LIST)
58 ;; This is the only descriptor that does not match a real
59 ;; array type.
60 (setq elt-type 'LIST length '*))
61 ((VECTOR)
62 (setq elt-type (if (endp args) 'T (first args))
63 length (if (endp (rest args)) '* (second args))))
64 ((SIMPLE-VECTOR)
65 (setq elt-type 'T
66 length (if (endp args) '* (first args))))
67 #-unicode
68 ((STRING SIMPLE-STRING)
69 (setq elt-type 'BASE-CHAR
70 length (if (endp args) '* (first args))))
71 #+unicode
72 ((BASE-STRING SIMPLE-BASE-STRING)
73 (setq elt-type 'BASE-CHAR
74 length (if (endp args) '* (first args))))
75 #+unicode
76 ((STRING SIMPLE-STRING)
77 (setq elt-type 'CHARACTER
78 length (if (endp args) '* (first args))))
79 ((BIT-VECTOR SIMPLE-BIT-VECTOR)
80 (setq elt-type 'BIT
81 length (if (endp args) '* (first args))))
82 ((ARRAY SIMPLE-ARRAY)
83 (let ((dimension-spec (second args)))
84 (cond
85 ((eql dimension-spec 1)
86 (setf length '*))
87 ((and (consp dimension-spec)
88 (null (cdr dimension-spec)))
89 (setf length (car dimension-spec)))
90 (T (error-sequence-type type))))
91 (setq elt-type (upgraded-array-element-type (first args))))
92 (t
93 ;; We arrive here when the sequence type is not easy to parse.
94 ;; We give up trying to guess the length of the sequence.
95 ;; Furthermore, we also give up trying to find if the element
96 ;; type is *. Instead we just compare with some specialized
97 ;; types and otherwise fail.
98 (dolist (i '((NIL . NIL)
99 (LIST . LIST)
100 (STRING . CHARACTER)
101 . #.(mapcar #'(lambda (i) `((VECTOR ,i) . ,i))
102 +upgraded-array-element-types+))
103 (if (subtypep type 'vector)
104 ;; Does this have to be a type-error?
105 ;; 17.3 for MAKE-SEQUENCE says it should be an error,
106 ;; but does not specialize what kind.
107 (error "Cannot find the element type in vector type ~S" type)
108 (error-sequence-type type)))
109 (when (subtypep type (car i))
110 (setq elt-type (cdr i) length '*)
111 ;; The (NIL . NIL) case above
112 (unless elt-type
113 (error-sequence-type type))
114 (return)))))
115 (values elt-type length)))
116
117 (defun make-sequence (type size &key (initial-element nil iesp) &aux sequence)
118 "Args: (type length &key initial-element)
119 Creates and returns a sequence of the given TYPE and LENGTH. If INITIAL-
120 ELEMENT is given, then it becomes the elements of the created sequence. The
121 default value of INITIAL-ELEMENT depends on TYPE."
122 (multiple-value-bind (element-type length)
123 (closest-sequence-type type)
124 (cond ((eq element-type 'LIST)
125 (setq sequence (make-list size :initial-element initial-element))
126 (unless (subtypep 'LIST type)
127 (when (or (and (subtypep type 'NULL) (plusp size))
128 (and (subtypep type 'CONS) (zerop size)))
129 (error-sequence-length (make-list size :initial-element initial-element) type 0))))
130 (t
131 (setq sequence (sys:make-vector (if (eq element-type '*) T element-type)
132 size nil nil nil nil))
133 (when iesp
134 (si::fill-array-with-elt sequence initial-element 0 nil))
135 (unless (or (eql length '*) (eql length size))
136 (error-sequence-length sequence type size))))
137 sequence))
138
139 (defun make-seq-iterator (sequence &optional (start 0))
140 (declare (optimize (safety 0)))
141 (cond ((fixnump start)
142 (let ((aux start))
143 (declare (fixnum aux))
144 (cond ((minusp aux)
145 (error-sequence-index sequence start))
146 ((listp sequence)
147 (nthcdr aux sequence))
148 ((vectorp sequence)
149 (and (< start (length (truly-the vector sequence)))
150 start))
151 (t
152 (error-not-a-sequence sequence)))))
153 ((not (or (listp sequence) (vectorp sequence)))
154 (error-not-a-sequence sequence))
155 ((integerp start)
156 nil)
157 (t
158 (error-sequence-index sequence start))))
159
160 (defun seq-iterator-ref (sequence iterator)
161 (declare (optimize (safety 0)))
162 (if (si::fixnump iterator)
163 (aref (truly-the vector sequence) iterator)
164 (car (truly-the cons iterator))))
165
166 (defun seq-iterator-set (sequence iterator value)
167 (declare (optimize (safety 0)))
168 (if (si::fixnump iterator)
169 (setf (aref (truly-the vector sequence) iterator) value)
170 (setf (car (truly-the cons iterator)) value)))
171
172 (defun seq-iterator-next (sequence iterator)
173 (declare (optimize (safety 0)))
174 (cond ((fixnump iterator)
175 (let ((aux (1+ iterator)))
176 (declare (fixnum aux))
177 (and (< aux (length (truly-the vector sequence)))
178 aux)))
179 ((atom iterator)
180 (error-not-a-sequence iterator))
181 (t
182 (setf iterator (cdr (truly-the cons iterator)))
183 (unless (listp iterator)
184 (error-not-a-sequence iterator))
185 iterator)))
186
187 (defun seq-iterator-list-pop (values-list seq-list iterator-list)
188 (declare (optimize (safety 0)))
189 (do* ((it-list iterator-list)
190 (v-list values-list))
191 ((null v-list)
192 values-list)
193 (let* ((it (cons-car it-list))
194 (sequence (cons-car seq-list)))
195 (cond ((null it)
196 (return nil))
197 ((fixnump it)
198 (let* ((n it) (s sequence))
199 (declare (fixnum n) (vector s))
200 (rplaca v-list (aref s n))
201 (rplaca it-list (and (< (incf n) (length s)) n))))
202 ((atom it)
203 (error-not-a-sequence it))
204 (t
205 (rplaca v-list (cons-car it))
206 (unless (listp (setf it (cons-cdr it)))
207 (error-not-a-sequence it))
208 (rplaca it-list it)))
209 (setf v-list (cons-cdr v-list)
210 it-list (cons-cdr it-list)
211 seq-list (cons-cdr seq-list)))))
212
213 (defun coerce-to-list (object)
214 (if (listp object)
215 object
216 (do ((it (make-seq-iterator object) (seq-iterator-next object it))
217 (output nil))
218 ((null it) (nreverse output))
219 (push (seq-iterator-ref object it) output))))
220
221 (defun coerce-to-vector (object elt-type length simple-array-p)
222 (let ((output object))
223 (unless (and (vectorp object)
224 (or (null simple-array-p) (simple-array-p object))
225 (eq (array-element-type object) elt-type))
226 (let* ((final-length (if (eq length '*) (length object) length)))
227 (setf output (make-vector elt-type final-length nil nil nil 0))
228 (do ((i (make-seq-iterator object) (seq-iterator-next output i))
229 (j 0 (truly-the index (1+ j))))
230 ((= j final-length)
231 (setf object output))
232 (declare (index j))
233 (setf (aref output j) (seq-iterator-ref object i)))))
234 output))
235
236 (defun concatenate (result-type &rest sequences)
237 "Args: (type &rest sequences)
238 Returns a new sequence of the specified type, consisting of all elements of
239 SEQUENCEs."
240 (do* ((length-list (mapcar #'length sequences) (rest length-list))
241 (output (make-sequence result-type (apply #'+ length-list)))
242 (sequences sequences (rest sequences))
243 (i (make-seq-iterator output)))
244 ((null sequences) output)
245 (do* ((s (first sequences))
246 (j (make-seq-iterator s) (seq-iterator-next s j)))
247 ((null j))
248 (seq-iterator-set output i (seq-iterator-ref s j))
249 (setq i (seq-iterator-next output i)))))
250
251
252 (defun map (result-type function sequence &rest more-sequences)
253 "Args: (type function sequence &rest more-sequences)
254 Creates and returns a sequence of TYPE with K elements, with the N-th element
255 being the value of applying FUNCTION to the N-th elements of the given
256 SEQUENCEs, where K is the minimum length of the given SEQUENCEs."
257 (let* ((sequences (list* sequence more-sequences))
258 (function (si::coerce-to-function function))
259 output
260 it)
261 (when result-type
262 (let ((l (length sequence)))
263 (when more-sequences
264 (setf l (reduce #'min more-sequences
265 :initial-value l
266 :key #'length)))
267 (setf output (make-sequence result-type l)
268 it (make-seq-iterator output))))
269 (do-sequences (elt-list sequences :output output)
270 (let ((value (apply function elt-list)))
271 (when result-type
272 (seq-iterator-set output it value)
273 (setf it (seq-iterator-next output it)))))))
274
275 (defun some (predicate sequence &rest more-sequences)
276 "Args: (predicate sequence &rest more-sequences)
277 Returns T if at least one of the elements in SEQUENCEs satisfies PREDICATE;
278 NIL otherwise."
279 (reckless
280 (do-sequences (elt-list (cons sequence more-sequences) :output nil)
281 (let ((x (apply predicate elt-list)))
282 (when x (return x))))))
283
284 (defun every (predicate sequence &rest more-sequences)
285 "Args: (predicate sequence &rest more-sequences)
286 Returns T if every elements of SEQUENCEs satisfy PREDICATE; NIL otherwise."
287 (reckless
288 (do-sequences (elt-list (cons sequence more-sequences) :output t)
289 (unless (apply predicate elt-list)
290 (return nil)))))
291
292 #|
293 (def-seq-bool-parser notany
294 "Args: (predicate sequence &rest more-sequences)
295 Returns T if none of the elements in SEQUENCEs satisfies PREDICATE; NIL
296 otherwise."
297 (when that-value (return nil))
298 t)
299
300 (def-seq-bool-parser notevery
301 "Args: (predicate sequence &rest more-sequences)
302 Returns T if at least one of the elements in SEQUENCEs does not satisfy
303 PREDICATE; NIL otherwise."
304 (unless that-value (return t))
305 nil)
306 |#
307
308 (defun every* (predicate &rest sequences)
309 "Args: (predicate sequence &rest more-sequences)
310 Returns T if every elements of SEQUENCEs satisfy PREDICATE and all sequences
311 have the same length; NIL otherwise."
312 (and (apply #'= (mapcar #'length sequences))
313 (apply #'every predicate sequences)))
314
315
316 (defun notany (predicate sequence &rest more-sequences)
317 "Args: (predicate sequence &rest more-sequences)
318 Returns T if none of the elements in SEQUENCEs satisfies PREDICATE; NIL
319 otherwise."
320 (not (apply #'some predicate sequence more-sequences)))
321
322
323 (defun notevery (predicate sequence &rest more-sequences)
324 "Args: (predicate sequence &rest more-sequences)
325 Returns T if at least one of the elements in SEQUENCEs does not satisfy
326 PREDICATE; NIL otherwise."
327 (not (apply #'every predicate sequence more-sequences)))
328
329 (defun map-into (result-sequence function &rest sequences)
330 "Fills the output sequence with the values returned by applying FUNCTION to the
331 elements of the given sequences. The i-th element of RESULT-SEQUENCE is the output
332 of applying FUNCTION to the i-th element of each of the sequences. The map routine
333 stops when it reaches the end of one of the given sequences."
334 (let ((nel (apply #'min (if (vectorp result-sequence)
335 (array-dimension result-sequence 0)
336 (length result-sequence))
337 (mapcar #'length sequences))))
338 (declare (fixnum nel))
339 ;; Set the fill pointer to the number of iterations
340 (when (and (vectorp result-sequence)
341 (array-has-fill-pointer-p result-sequence))
342 (setf (fill-pointer result-sequence) nel))
343 ;; Perform mapping
344 (do ((ir (make-seq-iterator result-sequence) (seq-iterator-next result-sequence ir))
345 (it (mapcar #'make-seq-iterator sequences))
346 (val (make-sequence 'list (length sequences))))
347 ((null ir) result-sequence)
348 (do ((i it (cdr i))
349 (v val (cdr v))
350 (s sequences (cdr s)))
351 ((null i))
352 (unless (car i) (return-from map-into result-sequence))
353 (rplaca v (seq-iterator-ref (car s) (car i)))
354 (rplaca i (seq-iterator-next (car s) (car i))))
355 (seq-iterator-set result-sequence ir (apply function val)))))