Debian Codegraph @

src/lsp/seqlib.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 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
5 ;;;; Copyright (c) 1990, Giuseppe Attardi.
6 ;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll.
7 ;;;;
8 ;;;; See file 'LICENSE' for the copyright details.
9
10 ;;;; sequence routines
11
12 (in-package "SYSTEM")
13
14 #+ecl-min
15 (eval-when (:execute)
16 (load (merge-pathnames "seqmacros.lsp" *load-truename*)))
17
18 #-ecl-min
19 (eval-when (:compile-toplevel)
20 (define-compiler-macro copy-subarray (&rest args)
21 `(ffi:c-inline ,args (:object :fixnum :object :fixnum :fixnum) :void
22 "ecl_copy_subarray(#0,#1,#2,#3,#4)"
23 :one-liner t))
24 )
25
26 (defun seqtype (sequence)
27 (declare (si::c-local))
28 (cond ((listp sequence) 'list)
29 ((base-string-p sequence) 'base-string)
30 ((stringp sequence) 'string)
31 ((bit-vector-p sequence) 'bit-vector)
32 ((vectorp sequence) (list 'vector (array-element-type sequence)))
33 (t (error "~S is not a sequence." sequence))))
34
35 (defun sequence-count (count)
36 (cond ((null count)
37 most-positive-fixnum)
38 ((fixnump count)
39 count)
40 ((integerp count)
41 (if (minusp count)
42 -1
43 most-positive-fixnum))
44 (t
45 (error 'simple-type-error
46 :datum count
47 :expected-type 'integer
48 :format-control "The value of :COUNT is not a valid counter~%~4I~A"
49 :format-arguments (list count)))))
50
51 (defun test-error()
52 (declare (si::c-local))
53 (error "both test and test-not are supplied"))
54
55 (defun unsafe-funcall1 (f x)
56 (declare (function f)
57 (optimize (speed 3) (safety 0)))
58 (funcall f x))
59
60 (defun reduce (function sequence
61 &key from-end
62 (start 0)
63 end
64 key (initial-value nil ivsp))
65 (let ((function (si::coerce-to-function function)))
66 (declare (optimize (speed 3) (safety 0) (debug 0)))
67 (with-start-end (start end sequence length)
68 (declare (ignore length))
69 (with-key (key)
70 (cond ((>= start end)
71 (if ivsp
72 initial-value
73 (funcall function)))
74 ((listp sequence)
75 (when from-end
76 (let* ((output nil))
77 (do-sublist (elt sequence start end)
78 (setf output (cons elt output)))
79 (setf sequence output
80 end (- end start) start 0)))
81 (while (plusp start)
82 (setf sequence (cons-cdr sequence)
83 start (1- start)
84 end (1- end)))
85 (unless ivsp
86 (setf initial-value (key (cons-car sequence))
87 sequence (cons-cdr sequence)
88 end (1- end)))
89 (do-sublist (elt sequence 0 end :output initial-value)
90 (setf initial-value
91 (if from-end
92 (funcall function (key elt) initial-value)
93 (funcall function initial-value (key elt))))))
94 (from-end
95 (unless ivsp
96 (setf initial-value (key (aref sequence (1- end)))
97 end (1- end)))
98 (do-vector(elt sequence start end :from-end t
99 :output initial-value)
100 (setf initial-value
101 (funcall function (key elt) initial-value))))
102 (t
103 (unless ivsp
104 (setf initial-value (key (aref sequence start))
105 start (1+ start)))
106 (do-vector(elt sequence start end :output initial-value)
107 (setf initial-value
108 (funcall function initial-value (key elt))))
109 ))))))
110
111 (defun fill (sequence item &key (start 0) end)
112 ;; INV: WITH-START-END checks the sequence type and size.
113 (reckless
114 (with-start-end (start end sequence)
115 (if (listp sequence)
116 (do* ((x (nthcdr start sequence) (cdr x))
117 (i (- end start) (1- i)))
118 ((zerop i)
119 sequence)
120 (declare (fixnum i) (cons x))
121 (setf (first x) item))
122 (si::fill-array-with-elt sequence item start end)))))
123
124 (defun replace (sequence1 sequence2 &key (start1 0) end1 (start2 0) end2)
125 (with-start-end (start1 end1 sequence1)
126 (with-start-end (start2 end2 sequence2)
127 (declare (optimize (speed 3) (safety 0) (debug 0)))
128 (let ((length (min (- end2 start2) (- end1 start1))))
129 (declare (fixnum length))
130 ;; If the two sequences are arrays, we can use COPY-SUBARRAY.
131 ;; Otherwise we have our own loop, which relies on sequence
132 ;; iterators. It becomes inefficient when sequences overlap
133 ;; because it has to save the data.
134 (if (and (vectorp sequence1)
135 (vectorp sequence2))
136 (copy-subarray sequence1 start1 sequence2 start2 length)
137 (do* ((data (if (and (eq sequence1 sequence2)
138 (> start1 start2))
139 (subseq sequence2 start2 end2)
140 sequence2))
141 (it2 (make-seq-iterator data start2)
142 (seq-iterator-next data it2))
143 (it1 (make-seq-iterator sequence1 start1)
144 (seq-iterator-next sequence1 it1)))
145 ((or (<= length 0) (null it1) (null it2)))
146 (seq-iterator-set sequence1 it1
147 (seq-iterator-ref sequence2 it2))
148 (decf length))))))
149 sequence1)
150
151 (defun filter-vector (which out in start end from-end count
152 test test-not key)
153 (with-tests (test test-not key)
154 (declare (optimize (speed 3) (safety 0) (debug 0)))
155 (with-start-end (start end in l)
156 (with-count (%count count :output in)
157 (let* ((existing 0))
158 (declare (fixnum existing))
159 ;; If the OUT is empty that means we REMOVE and we have to
160 ;; create the destination array. For that we first count how
161 ;; many elements are deletable and allocate the
162 ;; corresponding amount of space.
163 (unless (eq out in)
164 (setf existing (count which in :start start :end end
165 :test test :test-not test-not :key key))
166 (when (zerop existing)
167 (return-from filter-vector
168 (values in l)))
169 (setf out (make-array (- l (min existing %count))
170 :element-type
171 (array-element-type in))))
172 ;; We begin by copying the elements in [0, start)
173 (unless (eq out in)
174 (copy-subarray out 0 in 0 start))
175 ;; ... skip the elements in [start, end) which either
176 ;; do not need to be filtered (because of :from-end)
177 ;; or do not satisfy the test,
178 (let ((skip 0))
179 (declare (fixnum skip))
180 (when from-end
181 (unless (plusp existing)
182 (setf existing (count which in :start start :end end
183 :test test :test-not test-not
184 :key key)))
185 (setf skip (if (< existing %count) 0 (- existing %count))))
186 (if (eq out in)
187 (do-vector (elt in start end :index index)
188 (when (and (compare which (key elt))
189 (minusp (decf skip)))
190 (return))
191 (incf start))
192 (do-vector (elt in start end :index index)
193 (when (and (compare which (key elt))
194 (minusp (decf skip)))
195 (return))
196 (setf (aref (truly-the vector out) start) elt
197 start (1+ start)))))
198 ;; ... now filter the rest
199 (do-vector (elt in start end :index index)
200 (if (compare which (key elt))
201 (when (zerop (decf %count))
202 (setf end (1+ index))
203 (return))
204 (setf (aref (truly-the vector out) start) elt
205 start (1+ start))))
206 ;; ... and copy the elements outside the limits
207 (copy-subarray out start in end l)
208 (values out (+ start (- l end))))))))
209
210 (defun remove-list (which sequence start end count test test-not key)
211 (with-tests (test test-not key)
212 (declare (optimize (speed 3) (safety 0) (debug 0)))
213 (with-start-end (start end sequence)
214 (with-count (%count count :output sequence)
215 (let* ((output nil)
216 (index 0))
217 (declare (fixnum index))
218 (while (and sequence (< index start))
219 (setf output (cons (cons-car sequence) output)
220 sequence (cons-cdr sequence)
221 index (1+ index)))
222 (loop
223 (unless (< index end) (return))
224 (let ((elt (cons-car sequence)))
225 (setf sequence (cons-cdr sequence))
226 (if (compare which (key elt))
227 (when (zerop (decf %count))
228 (return))
229 (push elt output))
230 (incf index)))
231 (nreconc output sequence))))))
232
233 (defun remove (which sequence &key test test-not (start 0) end
234 from-end count key)
235 (declare (optimize (speed 3) (safety 1) (debug 0)))
236 (if (listp sequence)
237 (if from-end
238 (let ((l (length sequence)))
239 (nreverse (delete which (reverse sequence)
240 :start (if end (- l end) 0) :end (- l start)
241 :from-end nil
242 :test test :test-not test-not :key key
243 :count count)))
244 (remove-list which sequence start end count test test-not key))
245 (values (filter-vector which nil sequence start end from-end count
246 test test-not key))))
247
248 (defun remove-if (predicate sequence &key (start 0) end from-end count key)
249 (remove (si::coerce-to-function predicate) sequence
250 :start start :end end :from-end from-end :count count
251 :test #'unsafe-funcall1 :key key))
252
253 (defun remove-if-not (predicate sequence &key (start 0) end from-end count key)
254 (remove (si::coerce-to-function predicate) sequence
255 :start start :end end :from-end from-end :count count
256 :test-not #'unsafe-funcall1 :key key))
257
258 (defun delete-list (which sequence start end count test test-not key)
259 (with-tests (test test-not key)
260 (declare (optimize (speed 3) (safety 0) (debug 0)))
261 (with-start-end (start end sequence)
262 (with-count (%count count :output sequence)
263 (let* ((splice (cons nil sequence))
264 (output splice)
265 (index 0))
266 (declare (fixnum index)
267 (cons splice))
268 (while (and sequence (< index start))
269 (setf sequence (cons-cdr sequence)
270 splice (cons-cdr splice)
271 index (1+ index)))
272 (loop
273 (unless (< index end)
274 (return))
275 (let ((elt (cons-car sequence)))
276 (setf sequence (cons-cdr sequence))
277 (cond ((compare which (key elt))
278 (setf (cdr splice) sequence)
279 (when (zerop (decf %count))
280 (return)))
281 (t
282 (setf splice (cdr splice))))
283 (incf index)))
284 (cdr output))))))
285
286 (defun delete (which sequence &key test test-not (start 0) end
287 from-end count key)
288 (declare (optimize (speed 3) (safety 1) (debug 0))
289 (ext:check-arguments-type nil))
290 (cond ((listp sequence)
291 (if from-end
292 (let ((l (length sequence)))
293 (nreverse
294 (delete-list which (nreverse sequence)
295 (if end (- l end) 0) (- l start)
296 count test test-not key)))
297 (delete-list which sequence start end count test test-not key)))
298 ((not (vectorp sequence))
299 (signal-type-error sequence 'sequence))
300 ((array-has-fill-pointer-p (truly-the vector sequence))
301 (multiple-value-bind (sequence l)
302 (filter-vector which sequence sequence start end from-end count
303 test test-not key)
304 (setf (fill-pointer (truly-the vector sequence)) l)
305 sequence))
306 (t
307 (values (filter-vector which nil sequence start end from-end count
308 test test-not key)))))
309
310 (defun delete-if (predicate sequence &key (start 0) end from-end count key)
311 (delete (si::coerce-to-function predicate) sequence
312 :start start :end end :from-end from-end :count count
313 :test #'unsafe-funcall1 :key key))
314
315 (defun delete-if-not (predicate sequence &key (start 0) end
316 from-end count key)
317 (delete (si::coerce-to-function predicate) sequence
318 :start start :end end :from-end from-end :count count
319 :test-not #'unsafe-funcall1 :key key))
320
321 (defun count (item sequence &key test test-not from-end (start 0) end key)
322 (with-tests (test test-not key)
323 (declare (optimize (speed 3) (safety 0) (debug 0)))
324 (with-start-end (start end sequence l)
325 (let ((counter 0))
326 (declare (fixnum counter))
327 (if from-end
328 (if (listp sequence)
329 (count item (reverse sequence)
330 :start (- l end) :end (- l start)
331 :test test :test-not test-not :key key)
332 (do-vector (elt sequence start end :from-end t
333 :output counter)
334 (when (compare item (key elt))
335 (incf counter))))
336 (do-sequence (elt sequence start end :specialize t
337 :output counter)
338 (when (compare item (key elt))
339 (incf counter))))))))
340
341 (defun count-if (predicate sequence &key from-end (start 0) end key)
342 (count (si::coerce-to-function predicate) sequence
343 :from-end from-end :start start :end end
344 :test #'unsafe-funcall1 :key key))
345
346 (defun count-if-not (predicate sequence &key from-end (start 0) end key)
347 (count (si::coerce-to-function predicate)
348 sequence :from-end from-end :start start :end end
349 :test-not #'unsafe-funcall1 :key key))
350
351 (defun substitute (new old sequence &key test test-not (start 0) end
352 from-end count key)
353 (nsubstitute new old (copy-seq sequence) :start start :end end :from-end from-end
354 :count count :key key :test test :test-not test-not))
355
356 (defun substitute-if (new predicate sequence
357 &key (start 0) end from-end count key)
358 (nsubstitute new (si::coerce-to-function predicate) (copy-seq sequence)
359 :key key :test #'unsafe-funcall1
360 :start start :end end :from-end from-end :count count
361 :key key))
362
363 (defun substitute-if-not (new predicate sequence
364 &key (start 0) end from-end count key)
365 (nsubstitute new (si::coerce-to-function predicate) (copy-seq sequence)
366 :key key :test-not #'unsafe-funcall1
367 :start start :end end :from-end from-end :count count
368 :key key))
369
370 (defun nsubstitute (new old sequence &key test test-not (start 0) end
371 from-end count key)
372 (with-tests (test test-not key)
373 (declare (optimize (speed 3) (safety 0) (debug 0)))
374 (with-start-end (start end sequence l)
375 (with-count (%count count :output sequence)
376 ;; FIXME! This could be simplified to (AND FROM-END COUNT)
377 ;; but the ANSI test suite complains because it expects always
378 ;; a from-end inspection order!
379 (if from-end
380 (if (listp sequence)
381 (nreverse
382 (nsubstitute new old (nreverse sequence)
383 :start (- l end) :end (- l start)
384 :key key :test test :test-not test-not
385 :count count))
386 (do-vector (elt sequence start end :setter elt-set
387 :from-end t :output sequence)
388 (when (compare old (key elt))
389 (elt-set new)
390 (when (zerop (decf %count))
391 (return sequence)))))
392 (do-sequence (elt sequence start end :setter elt-set
393 :output sequence :specialize t)
394 (when (compare old (key elt))
395 (elt-set new)
396 (when (zerop (decf %count))
397 (return sequence)))))))))
398
399 (defun nsubstitute-if (new predicate sequence
400 &key (start 0) end from-end count key)
401 (nsubstitute new (si::coerce-to-function predicate) sequence
402 :key key :test #'unsafe-funcall1
403 :start start :end end :from-end from-end :count count
404 :key key))
405
406 (defun nsubstitute-if-not (new predicate sequence
407 &key (start 0) end from-end count key)
408 (nsubstitute new (si::coerce-to-function predicate) sequence
409 :key key :test-not #'unsafe-funcall1
410 :start start :end end :from-end from-end :count count
411 :key key))
412
413
414 (defun find (item sequence &key test test-not (start 0) end from-end key)
415 (with-tests (test test-not key)
416 (declare (optimize (speed 3) (safety 0) (debug 0)))
417 (with-start-end (start end sequence length)
418 (declare (ignore length))
419 (let ((output nil))
420 (do-sequence (elt sequence start end
421 :output output :index index :specialize t)
422 (when (compare item (key elt))
423 (unless from-end
424 (return elt))
425 (setf output elt)))))))
426
427 (defun find-if (predicate sequence &key from-end (start 0) end key)
428 (find (si::coerce-to-function predicate) sequence
429 :from-end from-end :start start :end end
430 :test #'unsafe-funcall1 :key key))
431
432 (defun find-if-not (predicate sequence &key from-end (start 0) end key)
433 (find (si::coerce-to-function predicate) sequence
434 :from-end from-end :start start :end end
435 :test-not #'unsafe-funcall1 :key key))
436
437
438 (defun position (item sequence &key test test-not from-end (start 0) end key)
439 (with-tests (test test-not key)
440 (declare (optimize (speed 3) (safety 0) (debug 0)))
441 (with-start-end (start end sequence)
442 (let ((output nil))
443 (do-sequence (elt sequence start end
444 :output output :index index :specialize t)
445 (when (compare item (key elt))
446 (unless from-end
447 (return index))
448 (setf output index)))))))
449
450 (defun position-if (predicate sequence &key from-end (start 0) end key)
451 (position (si::coerce-to-function predicate) sequence
452 :from-end from-end :start start :end end
453 :test #'unsafe-funcall1 :key key))
454
455 (defun position-if-not (predicate sequence &key from-end (start 0) end key)
456 (position (si::coerce-to-function predicate) sequence
457 :from-end from-end :start start :end end
458 :test-not #'unsafe-funcall1 :key key))
459
460 (defun remove-duplicates-list (sequence start end from-end test test-not key)
461 (with-tests (test test-not key)
462 (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
463 (with-start-end (start end sequence)
464 (let* ((output nil))
465 (while (and sequence (plusp start))
466 (setf output (cons (cons-car sequence) output)
467 sequence (cons-cdr sequence)
468 start (1- start)
469 end (1- end)))
470 (let ((start sequence)
471 (end (nthcdr (- end start) sequence)))
472 ;; When from-end, keep the first occurrence of each duplicate
473 ;; element; otherwise we keep the last one. Hence, A-I-L-P
474 ;; 1) if from-end, return T only when there are no duplicates
475 ;; before current;
476 ;; 2) otherwise, return T only when there are no duplicates
477 ;; after the current one.
478 (flet ((already-in-list-p (start current end from-end)
479 (let ((elt (key (cons-car current))))
480 (if from-end
481 (loop
482 (when (eq start current)
483 (return nil))
484 (when (compare elt (key (cons-car start)))
485 (return t))
486 (setf start (cons-cdr start)))
487 (loop
488 (setf current (cons-cdr current))
489 (when (eq current end)
490 (return nil))
491 (when (compare elt (key (cons-car current)))
492 (return t)))))))
493 (loop
494 (when (eq sequence end)
495 (return (nreconc output sequence)))
496 (unless (already-in-list-p start sequence end from-end)
497 (push (cons-car sequence) output))
498 (setf sequence (cons-cdr sequence)))))))))
499
500 (defun remove-duplicates (sequence
501 &key test test-not from-end (start 0) end key)
502 "Args: (sequence
503 &key key (test '#'eql) test-not
504 (start 0) (end (length sequence)) (from-end nil))
505 Returns a copy of SEQUENCE without duplicated elements."
506 (cond ((listp sequence)
507 (remove-duplicates-list sequence start
508 end from-end test test-not key))
509 ((vectorp sequence)
510 (let* ((l (filter-duplicates-vector nil sequence
511 start end from-end
512 test test-not key))
513 (v (make-array l :element-type
514 (array-element-type sequence))))
515 (filter-duplicates-vector v sequence
516 start end from-end
517 test test-not key)
518 v))
519 ((not (vectorp sequence))
520 (signal-type-error sequence 'sequence))))
521
522 (defun delete-duplicates-list (sequence start end from-end test test-not key)
523 (with-tests (test test-not key)
524 (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
525 (with-start-end (start end sequence)
526 (let* ((splice (cons nil sequence))
527 (output splice))
528 (while (and sequence (plusp start))
529 (setf splice (cons-cdr splice)
530 sequence (cons-cdr sequence)
531 start (1- start)
532 end (1- end)))
533 (let ((start splice)
534 (end (nthcdr (- end start) sequence)))
535 (flet ((already-in-list-p (start current end from-end)
536 (let ((elt (key (cons-car current))))
537 (if from-end
538 (loop
539 (when (eq start current)
540 (return nil))
541 (when (compare elt (key (cons-car start)))
542 (return t))
543 (setf start (cons-cdr start)))
544 (loop
545 (setf current (cons-cdr current))
546 (when (eq current end)
547 (return nil))
548 (when (compare elt (key (cons-car current)))
549 (return t)))))))
550 (loop
551 (when (eq sequence end)
552 (return (cons-cdr output)))
553 (if (already-in-list-p (cons-cdr start)
554 sequence end from-end)
555 (setf sequence (cons-cdr sequence)
556 (cdr splice) sequence)
557 (setf sequence (cons-cdr sequence)
558 splice (cons-cdr splice))))))))))
559
560 (defun filter-duplicates-vector (out in start end from-end test test-not key)
561 (with-tests (test test-not key)
562 (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
563 (with-start-end (start end in length)
564 (when (and out (not (eq out in)))
565 (copy-subarray out 0 in 0 start))
566 (flet ((already-in-vector-p (sequence start current end from-end)
567 (declare (vector sequence)
568 (fixnum start current end))
569 (if from-end
570 (setf end current)
571 (setf start (1+ current)))
572 (let ((base (key (aref sequence current))))
573 (do-vector (elt sequence start end :output nil)
574 (when (compare base (key elt))
575 (return t))))))
576 (let ((index start)
577 (jndex start))
578 (declare (fixnum index jndex))
579 (loop
580 (when (= index end)
581 (return (progn
582 (when out (copy-subarray out jndex in end length))
583 (+ jndex (- length end)))))
584 (unless (already-in-vector-p in start index end from-end)
585 (when out
586 (setf (aref (truly-the vector out) jndex)
587 (aref (truly-the vector in) index)))
588 (setf jndex (1+ jndex)))
589 (setf index (1+ index))))))))
590
591 (defun delete-duplicates (sequence
592 &key test test-not from-end (start 0) end key)
593 "Args: (sequence &key key
594 (test '#'eql) test-not
595 (start 0) (end (length sequence)) (from-end nil))
596 Destructive REMOVE-DUPLICATES. SEQUENCE may be destroyed."
597 (cond ((listp sequence)
598 (delete-duplicates-list sequence start end from-end
599 test test-not key))
600 ((not (vectorp sequence))
601 (signal-type-error sequence 'sequence))
602 ((array-has-fill-pointer-p sequence)
603 (let ((l (filter-duplicates-vector sequence sequence
604 start end from-end
605 test test-not key)))
606 (setf (fill-pointer sequence) l)
607 sequence))
608 (t
609 (let* ((l (filter-duplicates-vector nil sequence
610 start end from-end
611 test test-not key))
612 (v (make-array l :element-type
613 (array-element-type sequence))))
614 (filter-duplicates-vector v sequence
615 start end from-end
616 test test-not key)
617 v))))
618
619 (defun mismatch (sequence1 sequence2
620 &key from-end test test-not key
621 (start1 0) (start2 0)
622 end1 end2)
623 "Args: (sequence1 sequence2
624 &key key (test '#'eql) test-not
625 (start1 0) (end1 (length sequence1))
626 (start2 0) (end2 (length sequence2))
627 (from-end nil))
628 Compares element-wise the specified subsequences of SEQUENCE1 and SEQUENCE2.
629 Returns NIL if they are of the same length and they have the same elements in
630 the sense of TEST. Otherwise, returns the index of SEQUENCE1 to the first
631 element that does not match."
632 (with-start-end (start1 end1 sequence1)
633 (with-start-end (start2 end2 sequence2)
634 (with-tests (test test-not key)
635 (if (not from-end)
636 (do ((i1 start1 (1+ i1))
637 (i2 start2 (1+ i2)))
638 ((or (>= i1 end1) (>= i2 end2))
639 (if (and (>= i1 end1) (>= i2 end2)) nil i1))
640 (declare (fixnum i1 i2))
641 (unless (compare (key (elt sequence1 i1))
642 (key (elt sequence2 i2)))
643 (return i1)))
644 (do ((i1 (1- end1) (1- i1))
645 (i2 (1- end2) (1- i2)))
646 ((or (< i1 start1) (< i2 start2))
647 (if (and (< i1 start1) (< i2 start2)) nil (1+ i1)))
648 (declare (fixnum i1 i2))
649 (unless (compare (key (elt sequence1 i1))
650 (key (elt sequence2 i2)))
651 (return (1+ i1)))))))))
652
653
654 (defun search (sequence1 sequence2
655 &key from-end test test-not key
656 (start1 0) (start2 0)
657 end1 end2)
658 "Args: (sequence1 sequence2
659 &key key (test '#'eql) test-not
660 (start1 0) (end1 (length sequence1))
661 (start2 0) (end2 (length sequence2))
662 (from-end nil))
663 Searches SEQUENCE2 for a subsequence that element-wise matches SEQUENCE1.
664 Returns the index to the first element of the subsequence if such a
665 subsequence is found. Returns NIL otherwise."
666 #+(or)
667 (search-generic sequence1 start1 end1 sequence2 start2 end2
668 test test-not key from-end)
669 (if (and (vectorp sequence1) (vectorp sequence2))
670 (search-vector sequence1 start1 end1 sequence2 start2 end2
671 test test-not key from-end)
672 (search-generic sequence1 start1 end1 sequence2 start2 end2
673 test test-not key from-end)))
674
675 (defun search-vector (sequence1 start1 end1 sequence2 start2 end2
676 test test-not key from-end)
677 (declare (si::c-local)
678 (optimize (speed 3) (safety 0) (debug 0) (space 0))
679 (vector sequence1 sequence2))
680 (with-tests (test test-not key)
681 (with-start-end (start1 end1 sequence1)
682 (with-start-end (start2 end2 sequence2)
683 (do* ((last-index -1)
684 (last (let* ((l (- end1 start1))
685 (e (- end2 l)))
686 (declare (fixnum l e))
687 (unless (plusp l)
688 (return-from search-vector
689 (if from-end end2 0)))
690 (1+ e)))
691 (start2 start2 (1+ start2))
692 (base (key (aref sequence1 start1))))
693 ((>= start2 last)
694 (if (minusp last-index) nil last-index))
695 (declare (fixnum start2 last-index))
696 (when (compare base (key (aref sequence2 start2)))
697 (let* ((i1 start1)
698 (i2 start2))
699 (declare (fixnum i1 i2))
700 (loop
701 (setf i1 (1+ i1)
702 i2 (1+ i2))
703 (when (or (>= i1 end1) (>= i2 end2))
704 (when from-end
705 (setf last-index start2)
706 (return))
707 (return-from search-vector start2))
708 (unless (compare (key (aref sequence1 i1))
709 (key (aref sequence2 i2)))
710 (return))))))))))
711
712 (defun search-generic (sequence1 start1 end1 sequence2 start2 end2
713 test test-not key from-end)
714 (declare (si::c-local)
715 (optimize (speed 3) (safety 2) (debug 0) (space 0)))
716 (with-tests (test test-not key)
717 (with-start-end (start1 end1 sequence1)
718 (with-start-end (start2 end2 sequence2)
719 (do* ((last-index -1)
720 (start2 start2 (1+ start2))
721 (last (let* ((l (- end1 start1))
722 (e (- end2 l)))
723 (declare (fixnum l e))
724 (unless (plusp l)
725 (return-from search-generic
726 (if from-end end2 0)))
727 (1+ e)))
728 (it1 (make-seq-iterator sequence1 start1))
729 (base (key (seq-iterator-ref sequence1 it1)))
730 (it2 (make-seq-iterator sequence2 start2)
731 (seq-iterator-next sequence2 it2)))
732 ((or (null it2) (>= start2 last))
733 (if (minusp last-index) nil last-index))
734 (declare (fixnum start2 last-index last))
735 (when (compare base (key (seq-iterator-ref sequence2 it2)))
736 (let* ((it1 it1)
737 (it2 it2)
738 (i1 start1)
739 (i2 start2))
740 (declare (fixnum i1 i2))
741 (loop
742 (setf it1 (seq-iterator-next sequence1 it1)
743 it2 (seq-iterator-next sequence2 it2)
744 i1 (1+ i1)
745 i2 (1+ i2))
746 (when (or (>= i1 end1) (>= i2 end2)
747 (null it1) (null it2))
748 (when from-end
749 (setf last-index start2)
750 (return))
751 (return-from search-generic start2))
752 (unless (compare (key (seq-iterator-ref sequence1 it1))
753 (key (seq-iterator-ref sequence2 it2)))
754 (return))))))))))
755
756
757 (defun sort (sequence predicate &key key)
758 "Args: (sequence test &key key)
759 Destructively sorts SEQUENCE and returns the result. TEST should return non-
760 NIL if its first argument is to precede its second argument. The order of two
761 elements X and Y is arbitrary if both
762 (FUNCALL TEST X Y)
763 (FUNCALL TEST Y X)
764 evaluates to NIL. See STABLE-SORT."
765 (setf key (if key (si::coerce-to-function key) #'identity)
766 predicate (si::coerce-to-function predicate))
767 (if (listp sequence)
768 (list-merge-sort sequence predicate key)
769 (quick-sort sequence 0 (truly-the fixnum (1- (length sequence))) predicate key)))
770
771
772 (defun list-merge-sort (l predicate key)
773 (declare (si::c-local)
774 (optimize (safety 0) (speed 3))
775 (function predicate key))
776 (prog ((i 0) left right l0 l1 key-left key-right)
777 (declare (fixnum i))
778 (setq i (length l))
779 (cond ((< i 2) (return l))
780 ((= i 2)
781 (setq key-left (funcall key (car l)))
782 (setq key-right (funcall key (cadr l)))
783 (cond ((funcall predicate key-left key-right) (return l))
784 ((funcall predicate key-right key-left)
785 (return (nreverse l)))
786 (t (return l)))))
787 (setq i (floor i 2))
788 (do ((j 1 (1+ j)) (l1 l (cdr l1)))
789 ((>= j i)
790 (setq left l)
791 (setq right (cdr l1))
792 (rplacd l1 nil))
793 (declare (fixnum j)))
794 (setq left (list-merge-sort left predicate key))
795 (setq right (list-merge-sort right predicate key))
796 (cond ((endp left) (return right))
797 ((endp right) (return left)))
798 (setq l0 (cons nil nil))
799 (setq l1 l0)
800 (setq key-left (funcall key (car left)))
801 (setq key-right (funcall key (car right)))
802 loop
803 (cond ((funcall predicate key-left key-right) (go left))
804 ((funcall predicate key-right key-left) (go right))
805 (t (go left)))
806 left
807 (rplacd l1 left)
808 (setq l1 (cdr l1))
809 (setq left (cdr left))
810 (when (endp left)
811 (rplacd l1 right)
812 (return (cdr l0)))
813 (setq key-left (funcall key (car left)))
814 (go loop)
815 right
816 (rplacd l1 right)
817 (setq l1 (cdr l1))
818 (setq right (cdr right))
819 (when (endp right)
820 (rplacd l1 left)
821 (return (cdr l0)))
822 (setq key-right (funcall key (car right)))
823 (go loop)))
824
825
826 (defun quick-sort (seq start end pred key)
827 (declare (fixnum start end)
828 (function pred key)
829 (optimize (safety 0))
830 (si::c-local))
831 (if (< start end)
832 (let* ((j (1+ end)))
833 (declare (fixnum j))
834 (let* ((i start)
835 (l (- end start))
836 (l-half (ash l -1))
837 (p (+ start l-half))
838 (d (elt seq p))
839 (kd (funcall key d)))
840 (declare (fixnum i p l l-half))
841 (rotatef (elt seq p) (elt seq start))
842 (block outer-loop
843 (loop
844 (loop
845 (unless (> (decf j) i) (return-from outer-loop))
846 (when (funcall pred
847 (funcall key (elt seq j)) kd)
848 (return)))
849 (loop
850 (unless (< (incf i) j) (return-from outer-loop))
851 (unless (funcall pred
852 (funcall key (elt seq i)) kd)
853 (return)))
854 (rotatef (elt seq i) (elt seq j))))
855 (setf (elt seq start) (elt seq j)
856 (elt seq j) d))
857 (if (< (truly-the fixnum (- j start))
858 (truly-the fixnum (- end j)))
859 (progn
860 (quick-sort seq start (1- j) pred key)
861 (quick-sort seq (1+ j) end pred key))
862 (progn
863 (quick-sort seq (1+ j) end pred key)
864 (quick-sort seq start (1- j) pred key))))
865 seq))
866
867
868 (defun stable-sort-merge-vectors (source target start-1
869 end-1 end-2 pred key)
870 (declare (si::c-local))
871 (let ((i start-1)
872 (j end-1) ; start-2
873 (target-i start-1))
874 (declare (fixnum i j target-i))
875 (loop
876 (cond ((= i end-1)
877 (loop (if (= j end-2) (return))
878 (setf (aref target target-i)
879 (aref source j))
880 (incf target-i)
881 (incf j))
882 (return))
883 ((= j end-2)
884 (loop (if (= i end-1) (return))
885 (setf (aref target target-i)
886 (aref source i))
887 (incf target-i)
888 (incf i))
889 (return))
890 ((if key
891 (funcall pred (funcall key (aref source j))
892 (funcall key (aref source i)))
893 (funcall pred (aref source j) (aref source i)))
894 (setf (aref target target-i)
895 (aref source j))
896 (incf j))
897 (t (setf (aref target target-i)
898 (aref source i))
899 (incf i)))
900 (incf target-i))))
901
902
903 (defun vector-merge-sort (vector pred key)
904 (declare (si::c-local))
905 (let* ((vector-len (length (the vector vector)))
906 (n 1) ; bottom-up size of contiguous runs to be merged
907 (direction t) ; t vector --> temp nil temp --> vector
908 (temp (make-array vector-len))
909 (unsorted 0) ; unsorted..vector-len are the elements that need
910 ; to be merged for a given n
911 (start-1 0)) ; one n-len subsequence to be merged with the next
912 (declare (fixnum vector-len n unsorted start-1))
913 (loop
914 ;; for each n we start taking n-runs from the start of the vector
915 (setf unsorted 0)
916 (loop
917 (setf start-1 unsorted)
918 (let ((end-1 (+ start-1 n)))
919 (declare (fixnum end-1))
920 (cond ((< end-1 vector-len)
921 ;; there are enough elements for a second run
922 (let ((end-2 (+ end-1 n)))
923 (declare (fixnum end-2))
924 (if (> end-2 vector-len) (setf end-2 vector-len))
925 (setf unsorted end-2)
926 (if direction
927 (stable-sort-merge-vectors
928 vector temp start-1 end-1 end-2 pred key)
929 (stable-sort-merge-vectors
930 temp vector start-1 end-1 end-2 pred key))
931 (if (= unsorted vector-len) (return))))
932 ;; if there is only one run copy those elements to the end
933 (t (if direction
934 (do ((i start-1 (1+ i)))
935 ((= i vector-len))
936 (declare (fixnum i))
937 (setf (aref temp i) (aref vector i)))
938 (do ((i start-1 (1+ i)))
939 ((= i vector-len))
940 (declare (fixnum i))
941 (setf (aref vector i) (aref temp i))))
942 (return)))))
943 ;; If the inner loop only executed once then there were only enough
944 ;; elements for two subsequences given n so all the elements have
945 ;; been merged into one list. Start-1 will have remained 0 upon exit.
946 (when (zerop start-1)
947 (when direction
948 ;; if we just merged into the temporary copy it all back
949 ;; to the given vector.
950 (dotimes (i vector-len)
951 (setf (aref vector i) (aref temp i))))
952 (return vector))
953 (setf n (ash n 1)) ; (* 2 n)
954 (setf direction (not direction)))))
955
956
957 (defun stable-sort (sequence predicate &key key)
958 "Args: (sequence test &key key)
959 Destructively sorts SEQUENCE and returns the result. TEST should return non-
960 NIL if its first argument is to precede its second argument. For two elements
961 X and Y, if both
962 (FUNCALL TEST X Y)
963 (FUNCALL TEST Y X)
964 evaluates to NIL, then the order of X and Y are the same as in the original
965 SEQUENCE. See SORT."
966 (setf key (if key (si::coerce-to-function key) #'identity)
967 predicate (si::coerce-to-function predicate))
968 (if (listp sequence)
969 (list-merge-sort sequence predicate key)
970 (if (bit-vector-p sequence)
971 (sort sequence predicate :key key)
972 (vector-merge-sort sequence predicate key))))
973
974
975 (defun merge (result-type sequence1 sequence2 predicate &key key
976 &aux (l1 (length sequence1)) (l2 (length sequence2)))
977 "Args: (type sequence1 sequence2 test &key key)
978 Merges two sequences in the way specified by TEST and returns the result as a
979 sequence of TYPE. Both SEQUENCEs may be destroyed. If both SEQUENCE1 and
980 SEQUENCE2 are sorted in the sense of TEST, then the result is also sorted in
981 the sense of TEST."
982 (declare (fixnum l1 l2))
983 (with-key (key)
984 (with-predicate (predicate)
985 (do* ((size (truly-the fixnum (+ l1 l2)))
986 (j 0 (1+ j))
987 (newseq (make-sequence result-type size))
988 (i1 0)
989 (i2 0))
990 ((= j size) newseq)
991 (declare (fixnum size j i1 i2))
992 (if (>= i1 l1)
993 (setf (elt newseq j) (elt sequence2 i2)
994 i2 (1+ i2))
995 (let ((v1 (elt sequence1 i1)))
996 (if (>= i2 l2)
997 (setf (elt newseq j) v1
998 i1 (1+ i1))
999 (let* ((v2 (elt sequence2 i2))
1000 (k2 (key v2))
1001 (k1 (key v1)))
1002 (cond ((predicate k1 k2)
1003 (setf (elt newseq j) v1
1004 i1 (1+ i1)))
1005 ((predicate k2 k1)
1006 (setf (elt newseq j) v2
1007 i2 (1+ i2)))
1008 (t
1009 (setf (elt newseq j) v1
1010 i1 (1+ i1))))))))))))
1011
1012 (defun complement (f)
1013 "Args: (f)
1014 Returns a new function which first applies F to its arguments and then negates
1015 the output."
1016 #'(lambda (&rest x) (not (apply f x))))