3 #ifndef SCM_ARRAY_HANDLE_H
4 #define SCM_ARRAY_HANDLE_H
6 /* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2004, 2006,
7 * 2008, 2009, 2011, 2013, 2014 Free Software Foundation, Inc.
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 3 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include "libguile/__scm.h"
28 #include "libguile/error.h"
29 #include "libguile/numbers.h"
33 typedef SCM (*scm_t_vector_ref
) (SCM
, size_t);
34 typedef void (*scm_t_vector_set
) (SCM
, size_t, SCM
);
36 typedef struct scm_t_array_dim
45 SCM_ARRAY_ELEMENT_TYPE_SCM
= 0, /* SCM values */
46 SCM_ARRAY_ELEMENT_TYPE_CHAR
= 1, /* characters */
47 SCM_ARRAY_ELEMENT_TYPE_BIT
= 2, /* packed numeric values */
48 SCM_ARRAY_ELEMENT_TYPE_VU8
= 3,
49 SCM_ARRAY_ELEMENT_TYPE_U8
= 4,
50 SCM_ARRAY_ELEMENT_TYPE_S8
= 5,
51 SCM_ARRAY_ELEMENT_TYPE_U16
= 6,
52 SCM_ARRAY_ELEMENT_TYPE_S16
= 7,
53 SCM_ARRAY_ELEMENT_TYPE_U32
= 8,
54 SCM_ARRAY_ELEMENT_TYPE_S32
= 9,
55 SCM_ARRAY_ELEMENT_TYPE_U64
= 10,
56 SCM_ARRAY_ELEMENT_TYPE_S64
= 11,
57 SCM_ARRAY_ELEMENT_TYPE_F32
= 12,
58 SCM_ARRAY_ELEMENT_TYPE_F64
= 13,
59 SCM_ARRAY_ELEMENT_TYPE_C32
= 14,
60 SCM_ARRAY_ELEMENT_TYPE_C64
= 15,
61 SCM_ARRAY_ELEMENT_TYPE_LAST
= 15
62 } scm_t_array_element_type
;
64 SCM_INTERNAL SCM scm_i_array_element_types
[];
67 typedef struct scm_t_array_handle
{
70 /* `Base' is an offset into elements or writable_elements, corresponding to
71 the first element in the array. It would be nicer just to adjust the
72 elements/writable_elements pointer, but we can't because that element might
73 not even be byte-addressable, as is the case with bitvectors. A nicer
74 solution would be, well, nice.
77 size_t ndims
; /* ndims == the rank of the array */
78 scm_t_array_dim
*dims
;
80 scm_t_array_element_type element_type
;
82 void *writable_elements
;
84 /* The backing store for the array, and its accessors. */
86 scm_t_vector_ref vref
;
87 scm_t_vector_set vset
;
90 #define scm_array_handle_rank(h) ((h)->ndims)
91 #define scm_array_handle_dims(h) ((h)->dims)
93 SCM_API
void scm_array_get_handle (SCM array
, scm_t_array_handle
*h
);
94 SCM_API ssize_t
scm_array_handle_pos (scm_t_array_handle
*h
, SCM indices
);
95 SCM_API ssize_t
scm_array_handle_pos_1 (scm_t_array_handle
*h
, ssize_t idx0
);
96 SCM_API ssize_t
scm_array_handle_pos_2 (scm_t_array_handle
*h
, ssize_t idx0
, ssize_t idx1
);
97 SCM_API SCM
scm_array_handle_element_type (scm_t_array_handle
*h
);
98 SCM_API
void scm_array_handle_release (scm_t_array_handle
*h
);
99 SCM_API
const SCM
* scm_array_handle_elements (scm_t_array_handle
*h
);
100 SCM_API SCM
* scm_array_handle_writable_elements (scm_t_array_handle
*h
);
103 SCM_INLINE SCM
scm_array_handle_ref (scm_t_array_handle
*h
, ssize_t pos
);
104 SCM_INLINE
void scm_array_handle_set (scm_t_array_handle
*h
, ssize_t pos
, SCM val
);
106 #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
107 /* Either inlining, or being included from inline.c. */
109 SCM_INLINE_IMPLEMENTATION SCM
110 scm_array_handle_ref (scm_t_array_handle
*h
, ssize_t p
)
112 if (SCM_UNLIKELY (p
< 0 && ((size_t)-p
) > h
->base
))
114 scm_out_of_range (NULL
, scm_from_ssize_t (p
));
115 /* perhaps should catch overflow here too */
116 return h
->vref (h
->vector
, h
->base
+ p
);
119 SCM_INLINE_IMPLEMENTATION
void
120 scm_array_handle_set (scm_t_array_handle
*h
, ssize_t p
, SCM v
)
122 if (SCM_UNLIKELY (p
< 0 && ((size_t)-p
) > h
->base
))
124 scm_out_of_range (NULL
, scm_from_ssize_t (p
));
125 /* perhaps should catch overflow here too */
126 h
->vset (h
->vector
, h
->base
+ p
, v
);
132 SCM_INTERNAL
void scm_init_array_handle (void);
135 #endif /* SCM_ARRAY_HANDLE_H */