Changeset 5631


Ignore:
Timestamp:
Dec 18, 2006, 12:22:41 PM (18 years ago)
Author:
Gary Byers
Message:

COERCE/MAKE-SEQUENCE try to simplify vector types into concrete vector types.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ccl/lib/sequences.lisp

    r5389 r5631  
    6464      res)))
    6565
    66 
    67 
     66;;; CTYPE is a recognizable subtype of VECTOR, which means that it's either
     67;;; a) an ARRAY-CTYPE
     68;;; b) a UNION-CTYPE whose leaves are ARRAY-CTYPE
     69;;; c) the NIL type, which is trivially a subtype of VECTOR but isn't really
     70;;;    worth considering here
     71;;; d) a MEMBER-CTYPE whose members are all vectors and which therefore have
     72;;;    corresponding ARRAY-CTYPEs.
     73;;; Try to find the interesection of all ARRAY-CTYPEs referenced in CTYPE and
     74;;;  return it.
     75;;; Note that this intersection may be the null type.
     76(defun simplify-vector-ctype (ctype)
     77  (typecase ctype
     78    (array-ctype
     79     (make-array-ctype :complexp nil
     80                       :element-type (array-ctype-element-type ctype)
     81                       :specialized-element-type (array-ctype-specialized-element-type ctype)
     82                       :dimensions '(*)))
     83                                     
     84    (named-ctype ctype)
     85    (member-ctype
     86     (apply #'type-intersection (mapcar #'(lambda (x)
     87                                    (simplify-vector-ctype
     88                                     (ctype-of x)))
     89                                (member-ctype-members ctype))))
     90    (union-ctype
     91     (apply #'type-intersection (mapcar #'simplify-vector-ctype (union-ctype-types ctype))))))
     92   
    6893(defun make-sequence (type length &key (initial-element nil initial-element-p))
    6994  "Return a sequence of the given TYPE and LENGTH, with elements initialized
     
    86111             (make-string length
    87112                          :element-type 'base-char)))
    88           ((csubtypep ctype (specifier-type 'string))
    89            (if initial-element-p
    90              (make-string length :element-type 'character :initial-element initial-element)
    91              (make-string length :element-type 'character)))
    92113          ((csubtypep ctype (specifier-type 'vector))
    93            (let* ((element-type (type-specifier (array-ctype-element-type ctype))))
    94              (if (eq element-type '*) (setq element-type t))
    95              (if initial-element-p
    96                (make-array (the fixnum length)
    97                            :element-type element-type
    98                            :initial-element initial-element)
    99                (make-array (the fixnum length)
    100                            :element-type element-type))))
    101           ((csubtypep ctype (specifier-type 'array))
    102            (let* ((dims (array-ctype-dimensions ctype)))
    103              (when (consp dims)
    104                (when (not (null (cdr dims)))
    105                  (error 'invalid-subtype-error :datum  type :expected-type 'vector))))
    106            (let* ((element-type (type-specifier (array-ctype-element-type ctype))))
    107              (if (eq element-type '*) (setq element-type t))
    108              (if initial-element-p
    109                (make-array (the fixnum length)
    110                            :element-type element-type
    111                            :initial-element initial-element)
    112                (make-array (the fixnum length)
    113                            :element-type element-type))))           
     114           (let* ((atype (simplify-vector-ctype ctype)))
     115             (unless (typep atype 'array-ctype)
     116               (error "Can't determine vector element-type of ~s" (type-specifier ctype)))
     117             (let* ((element-type (type-specifier (array-ctype-element-type atype))))
     118               (if (eq element-type '*) (setq element-type t))
     119               (if initial-element-p
     120                 (make-array (the fixnum length)
     121                             :element-type element-type
     122                             :initial-element initial-element)
     123                 (make-array (the fixnum length)
     124                             :element-type element-type)))))
    114125          ((csubtypep ctype (specifier-type 'null))
    115126           (unless (zerop length)
     
    197208               (declare (fixnum i j))
    198209               (setf (sbit dest j) (sbit src i))))))))))
    199                    
    200                                      
    201        
    202210
    203211
     
    758766        (if (null (cdr dims))
    759767          (let* ((dim0 (car dims)))
    760             (unless (eq dim0 '*) dim0)))))
    761     (if (typep ctype 'union-ctype)
    762       ; this isn't really right either but does trt for (string 2)
    763       ; check if all specify a length and lengths are the same?
    764       (dolist (type (union-ctype-types ctype))
    765         (let ((len (array-ctype-length type)))
    766           (when len (return len)))))))
     768            (unless (eq dim0 '*) dim0)))))))
    767769
    768770
     
    802804                          t))
    803805       ((csubtypep type (specifier-type 'vector))
    804         (let* ((element-type (type-specifier (array-ctype-element-type type))))
    805           (let ((length (array-ctype-length type)))
    806             (if (and length (neq length (length object)))
    807               (report-bad-arg (make-array length :element-type element-type)
    808                               `(vector ,element-type ,(length object))))
    809             (coerce-to-uarray object (element-type-subtype element-type) t))))
     806        (let* ((atype (simplify-vector-ctype type)))
     807          (unless (typep atype 'array-ctype)
     808            (error "Can't determine vector type of ~s" output-type-spec))
     809          (let* ((element-type (type-specifier (array-ctype-element-type atype))))
     810            (let ((length (array-ctype-length atype)))
     811              (if (and length (neq length (length object)))
     812                (report-bad-arg (make-array length :element-type element-type)
     813                                `(vector ,element-type ,(length object))))
     814              (coerce-to-uarray object (element-type-subtype element-type) t)))))
    810815       ((csubtypep type (specifier-type 'array))
    811816        (let* ((dims (array-ctype-dimensions type)))
Note: See TracChangeset for help on using the changeset viewer.