1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
;; Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa
;; This file is part of GNU Common Lisp, herein referred to as GCL
;;
;; GCL is free software; you can redistribute it and/or modify it under
;; the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; GCL is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
;; License for more details.
;;
;; You should have received a copy of the GNU Library General Public License
;; along with GCL; see the file COPYING. If not, write to the Free Software
;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;;; seq.lsp
;;;;
;;;; sequence routines
(in-package 'lisp)
(export '(make-sequence concatenate map some every notany notevery))
(in-package 'system)
(proclaim '(optimize (safety 2) (space 3)))
(defun make-sequence (type size &key (initial-element nil iesp)
&aux element-type sequence)
(setq element-type
(cond ((eq type 'list)
(return-from make-sequence
(if iesp
(make-list size :initial-element initial-element)
(make-list size))))
((or (eq type 'simple-string) (eq type 'string)) 'string-char)
((or (eq type 'simple-bit-vector) (eq type 'bit-vector)) 'bit)
((or (eq type 'simple-vector) (eq type 'vector)) t)
(t
(setq type (normalize-type type))
(when (subtypep (car type) 'list)
(if (or (and (eq 'null (car type)) (not (equal size 0)))
(and (eq 'cons (car type)) (equal size 0)))
(specific-error :wrong-type-argument "~S is not of type ~S."
type (format nil "list (size ~S)" size)))
(return-from make-sequence
(if iesp
(make-list size :initial-element initial-element)
(make-list size))))
(unless (or (eq (car type) 'array)
(eq (car type) 'simple-array))
(specific-error :wrong-type-argument "~S is not of type ~S."
type 'sequence))
(let ((ssize (caddr type)))
(if (listp ssize) (setq ssize (car ssize)))
(if (not (si::fixnump ssize)) (setq ssize size))
(unless (equal ssize size)
(specific-error :wrong-type-argument "~S is not of type ~S."
type (format nil "~S (size ~S)" type size))))
(or (cadr type) t))))
(setq element-type (si::best-array-element-type element-type))
(setq sequence (si:make-vector element-type size nil nil nil nil nil))
(when iesp
(do ((i 0 (1+ i))
(size size))
((>= i size))
(declare (fixnum i size))
(setf (elt sequence i) initial-element)))
sequence)
(defun concatenate (result-type &rest sequences)
(do ((x (make-sequence result-type
(apply #'+ (mapcar #'length sequences))))
(s sequences (cdr s))
(i 0))
((null s) x)
(declare (fixnum i))
(do ((j 0 (1+ j))
(n (length (car s))))
((>= j n))
(declare (fixnum j n))
(setf (elt x i) (elt (car s) j))
(incf i))))
(defun map (result-type function sequence &rest more-sequences)
(setq more-sequences (cons sequence more-sequences))
(let ((l (apply #'min (mapcar #'length more-sequences))))
(if (null result-type)
(do ((i 0 (1+ i))
(l l))
((>= i l) nil)
(declare (fixnum i l))
(apply function (mapcar #'(lambda (z) (elt z i))
more-sequences)))
(let ((x (make-sequence result-type l)))
(do ((i 0 (1+ i))
(l l))
((>= i l) x)
(declare (fixnum i l))
(setf (elt x i)
(apply function (mapcar #'(lambda (z) (elt z i))
more-sequences))))))))
(defun some (predicate sequence &rest more-sequences)
(setq more-sequences (cons sequence more-sequences))
(do ((i 0 (1+ i))
(l (apply #'min (mapcar #'length more-sequences))))
((>= i l) nil)
(declare (fixnum i l))
(let ((that-value
(apply predicate
(mapcar #'(lambda (z) (elt z i)) more-sequences))))
(when that-value (return that-value)))))
(defun every (predicate sequence &rest more-sequences)
(setq more-sequences (cons sequence more-sequences))
(do ((i 0 (1+ i))
(l (apply #'min (mapcar #'length more-sequences))))
((>= i l) t)
(declare (fixnum i l))
(unless (apply predicate (mapcar #'(lambda (z) (elt z i)) more-sequences))
(return nil))))
(defun notany (predicate sequence &rest more-sequences)
(not (apply #'some predicate sequence more-sequences)))
(defun notevery (predicate sequence &rest more-sequences)
(not (apply #'every predicate sequence more-sequences)))
|