1 /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006, 2009, 2010, 2013, 2014 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
8 * This library 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 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "libguile/_scm.h"
27 #include "libguile/__scm.h"
29 #include "libguile/uniform.h"
32 const size_t scm_i_array_element_type_sizes
[SCM_ARRAY_ELEMENT_TYPE_LAST
+ 1] = {
46 scm_array_handle_uniform_element_size (scm_t_array_handle
*h
)
48 size_t ret
= scm_i_array_element_type_sizes
[h
->element_type
];
49 if (ret
&& ret
% 8 == 0)
52 scm_wrong_type_arg_msg (NULL
, 0, h
->array
, "byte-aligned uniform array");
54 scm_wrong_type_arg_msg (NULL
, 0, h
->array
, "uniform array");
58 scm_array_handle_uniform_element_bit_size (scm_t_array_handle
*h
)
60 size_t ret
= scm_i_array_element_type_sizes
[h
->element_type
];
64 scm_wrong_type_arg_msg (NULL
, 0, h
->array
, "uniform array");
68 scm_array_handle_uniform_elements (scm_t_array_handle
*h
)
70 return scm_array_handle_uniform_writable_elements (h
);
74 scm_array_handle_uniform_writable_elements (scm_t_array_handle
*h
)
79 esize
= scm_array_handle_uniform_element_size (h
);
80 ret
= ((scm_t_uint8
*) h
->writable_elements
) + h
->base
* esize
;
85 scm_is_uniform_vector (SCM obj
)
90 if (scm_is_array (obj
))
92 scm_array_get_handle (obj
, &h
);
93 ret
= (scm_array_handle_rank (&h
) == 1
94 && SCM_ARRAY_ELEMENT_TYPE_IS_UNBOXED (h
.element_type
));
95 scm_array_handle_release (&h
);
101 scm_c_uniform_vector_length (SCM uvec
)
103 if (!scm_is_uniform_vector (uvec
))
104 scm_wrong_type_arg_msg ("uniform-vector-length", 1, uvec
,
106 return scm_c_array_length (uvec
);
109 SCM_DEFINE (scm_uniform_vector_p
, "uniform-vector?", 1, 0, 0,
111 "Return @code{#t} if @var{obj} is a uniform vector.")
112 #define FUNC_NAME s_scm_uniform_vector_p
114 return scm_from_bool (scm_is_uniform_vector (obj
));
118 SCM_DEFINE (scm_uniform_vector_element_type
, "uniform-vector-element-type", 1, 0, 0,
120 "Return the type of the elements in the uniform vector, @var{v}.")
121 #define FUNC_NAME s_scm_uniform_vector_element_type
123 scm_t_array_handle h
;
126 if (!scm_is_uniform_vector (v
))
127 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, v
, "uniform vector");
128 scm_array_get_handle (v
, &h
);
129 ret
= scm_array_handle_element_type (&h
);
130 scm_array_handle_release (&h
);
135 SCM_DEFINE (scm_uniform_vector_element_type_code
,
136 "uniform-vector-element-type-code", 1, 0, 0,
138 "Return the type of the elements in the uniform vector, @var{v},\n"
139 "as an integer code.")
140 #define FUNC_NAME s_scm_uniform_vector_element_type_code
142 scm_t_array_handle h
;
145 if (!scm_is_uniform_vector (v
))
146 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, v
, "uniform vector");
147 scm_array_get_handle (v
, &h
);
148 ret
= scm_from_uint16 (h
.element_type
);
149 scm_array_handle_release (&h
);
154 SCM_DEFINE (scm_uniform_vector_element_size
, "uniform-vector-element-size", 1, 0, 0,
156 "Return the number of bytes allocated to each element in the\n"
157 "uniform vector, @var{v}.")
158 #define FUNC_NAME s_scm_uniform_vector_element_size
160 scm_t_array_handle h
;
164 scm_uniform_vector_elements (v
, &h
, &len
, &inc
);
165 ret
= scm_from_size_t (scm_array_handle_uniform_element_size (&h
));
166 scm_array_handle_release (&h
);
172 scm_c_uniform_vector_ref (SCM v
, size_t pos
)
174 if (!scm_is_uniform_vector (v
))
175 scm_wrong_type_arg_msg (NULL
, 0, v
, "uniform vector");
176 return scm_c_array_ref_1 (v
, pos
);
179 SCM_DEFINE (scm_uniform_vector_ref
, "uniform-vector-ref", 2, 0, 0,
181 "Return the element at index @var{idx} of the\n"
182 "homogeneous numeric vector @var{v}.")
183 #define FUNC_NAME s_scm_uniform_vector_ref
185 return scm_c_uniform_vector_ref (v
, scm_to_size_t (idx
));
190 scm_c_uniform_vector_set_x (SCM v
, size_t pos
, SCM val
)
192 if (!scm_is_uniform_vector (v
))
193 scm_wrong_type_arg_msg (NULL
, 0, v
, "uniform vector");
194 scm_c_array_set_1_x (v
, val
, pos
);
197 SCM_DEFINE (scm_uniform_vector_set_x
, "uniform-vector-set!", 3, 0, 0,
198 (SCM v
, SCM idx
, SCM val
),
199 "Set the element at index @var{idx} of the\n"
200 "homogeneous numeric vector @var{v} to @var{val}.")
201 #define FUNC_NAME s_scm_uniform_vector_set_x
203 scm_c_uniform_vector_set_x (v
, scm_to_size_t (idx
), val
);
204 return SCM_UNSPECIFIED
;
208 SCM_DEFINE (scm_uniform_vector_to_list
, "uniform-vector->list", 1, 0, 0,
210 "Convert the uniform numeric vector @var{uvec} to a list.")
211 #define FUNC_NAME s_scm_uniform_vector_to_list
213 if (!scm_is_uniform_vector (uvec
))
214 scm_wrong_type_arg_msg (FUNC_NAME
, SCM_ARG1
, uvec
, "uniform vector");
215 return scm_array_to_list (uvec
);
220 scm_uniform_vector_elements (SCM uvec
,
221 scm_t_array_handle
*h
,
222 size_t *lenp
, ssize_t
*incp
)
224 return scm_uniform_vector_writable_elements (uvec
, h
, lenp
, incp
);
228 scm_uniform_vector_writable_elements (SCM uvec
,
229 scm_t_array_handle
*h
,
230 size_t *lenp
, ssize_t
*incp
)
233 scm_array_get_handle (uvec
, h
);
234 if (scm_array_handle_rank (h
) != 1)
235 scm_wrong_type_arg_msg (0, SCM_ARG1
, uvec
, "uniform vector");
236 ret
= scm_array_handle_uniform_writable_elements (h
);
239 scm_t_array_dim
*dim
= scm_array_handle_dims (h
);
240 *lenp
= dim
->ubnd
- dim
->lbnd
+ 1;
246 SCM_DEFINE (scm_uniform_vector_length
, "uniform-vector-length", 1, 0, 0,
248 "Return the number of elements in the uniform vector @var{v}.")
249 #define FUNC_NAME s_scm_uniform_vector_length
251 return scm_from_size_t (scm_c_uniform_vector_length (v
));
257 scm_init_uniform (void)
259 #include "libguile/uniform.x"