Move vector-push-extend to arrays.lisp
[jscl.git] / src / arrays.lisp
1 ;;; arrays.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 upgraded-array-element-type (typespec &optional environment)
17   (declare (ignore environment))
18   (if (eq typespec 'character)
19       'character
20       t))
21
22 (defun make-array (dimensions &key element-type initial-element adjustable fill-pointer)
23   (let* ((dimensions (ensure-list dimensions))
24          (size (!reduce #'* dimensions 1))
25          (array (make-storage-vector size)))
26     ;; Upgrade type
27     (if (eq element-type 'character)
28         (setf element-type 'character
29               initial-element (or initial-element #\space))
30         (setf element-type t))
31     ;; Initialize array
32     (dotimes (i size)
33       (storage-vector-set array i initial-element))
34     ;; Record and return the object
35     (oset array "type" element-type)
36     (oset array "dimensions" dimensions)
37     array))
38
39
40 (defun arrayp (x)
41   (storage-vector-p x))
42
43 (defun adjustable-array-p (array)
44   (unless (arrayp array)
45     (error "~S is not an array." array))
46   t)
47
48 (defun array-element-type (array)
49   (unless (arrayp array)
50     (error "~S is not an array." array))
51   (oget array "type"))
52
53 (defun array-dimensions (array)
54   (unless (arrayp array)
55     (error "~S is not an array." array))
56   (oget array "dimensions"))
57
58 ;; TODO: Error checking
59 (defun array-dimension (array axis)
60   (nth axis (array-dimensions array)))
61
62 (defun aref (array index)
63   (unless (arrayp array)
64     (error "~S is not an array." array))  
65   (storage-vector-ref array index))
66
67 (defun aset (array index value)
68   (unless (arrayp array)
69     (error "~S is not an array." array))  
70   (storage-vector-set array index value))
71
72
73 ;;; Vectors
74
75 (defun vectorp (x)
76   (and (arrayp x) (null (cdr (array-dimensions x)))))
77
78 ;;; FIXME: should take optional min-extension.
79 ;;; FIXME: should use fill-pointer instead of the absolute end of array
80 (defun vector-push-extend (new vector)
81   (let ((size (storage-vector-size vector)))
82     (resize-storage-vector vector (1+ size))
83     (aset vector size new)
84     size))