Commit | Line | Data |
---|---|---|
438974d0 | 1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc. |
0f2d19dd | 2 | * |
73be1d9e | 3 | * This library is free software; you can redistribute it and/or |
53befeb7 NJ |
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. | |
0f2d19dd | 7 | * |
53befeb7 NJ |
8 | * This library is distributed in the hope that it will be useful, but |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
73be1d9e MV |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * Lesser General Public License for more details. | |
0f2d19dd | 12 | * |
73be1d9e MV |
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 | |
53befeb7 NJ |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
16 | * 02110-1301 USA | |
73be1d9e | 17 | */ |
1bbd0b84 | 18 | |
1bbd0b84 | 19 | |
0f2d19dd | 20 | \f |
dbb605f5 LC |
21 | #ifdef HAVE_CONFIG_H |
22 | # include <config.h> | |
23 | #endif | |
0f2d19dd | 24 | |
a0599745 MD |
25 | #include "libguile/_scm.h" |
26 | #include "libguile/eq.h" | |
27 | #include "libguile/root.h" | |
28 | #include "libguile/strings.h" | |
c96d76b8 | 29 | #include "libguile/lang.h" |
a0599745 MD |
30 | |
31 | #include "libguile/validate.h" | |
32 | #include "libguile/vectors.h" | |
33 | #include "libguile/unif.h" | |
cf396142 | 34 | #include "libguile/bitvectors.h" |
438974d0 | 35 | #include "libguile/bytevectors.h" |
5d1b3b2d | 36 | #include "libguile/array-map.h" |
88797580 MV |
37 | #include "libguile/srfi-4.h" |
38 | #include "libguile/strings.h" | |
39 | #include "libguile/srfi-13.h" | |
1d0df896 | 40 | #include "libguile/dynwind.h" |
6e708ef2 | 41 | #include "libguile/deprecation.h" |
88797580 | 42 | |
0f2d19dd JB |
43 | \f |
44 | ||
6e708ef2 MV |
45 | #define VECTOR_MAX_LENGTH (SCM_T_BITS_MAX >> 8) |
46 | ||
6e708ef2 MV |
47 | int |
48 | scm_is_vector (SCM obj) | |
49 | { | |
50 | if (SCM_I_IS_VECTOR (obj)) | |
51 | return 1; | |
04b87de5 | 52 | if (SCM_I_ARRAYP (obj) && SCM_I_ARRAY_NDIM (obj) == 1) |
6e708ef2 | 53 | { |
04b87de5 | 54 | SCM v = SCM_I_ARRAY_V (obj); |
6e708ef2 MV |
55 | return SCM_I_IS_VECTOR (v); |
56 | } | |
57 | return 0; | |
58 | } | |
59 | ||
60 | int | |
61 | scm_is_simple_vector (SCM obj) | |
1d0df896 | 62 | { |
6e708ef2 | 63 | return SCM_I_IS_VECTOR (obj); |
1d0df896 MV |
64 | } |
65 | ||
354116f7 MV |
66 | const SCM * |
67 | scm_vector_elements (SCM vec, scm_t_array_handle *h, | |
68 | size_t *lenp, ssize_t *incp) | |
69 | { | |
70 | scm_generalized_vector_get_handle (vec, h); | |
71 | if (lenp) | |
72 | { | |
73 | scm_t_array_dim *dim = scm_array_handle_dims (h); | |
74 | *lenp = dim->ubnd - dim->lbnd + 1; | |
75 | *incp = dim->inc; | |
76 | } | |
77 | return scm_array_handle_elements (h); | |
78 | } | |
79 | ||
80 | SCM * | |
81 | scm_vector_writable_elements (SCM vec, scm_t_array_handle *h, | |
82 | size_t *lenp, ssize_t *incp) | |
83 | { | |
84 | scm_generalized_vector_get_handle (vec, h); | |
85 | if (lenp) | |
86 | { | |
87 | scm_t_array_dim *dim = scm_array_handle_dims (h); | |
88 | *lenp = dim->ubnd - dim->lbnd + 1; | |
89 | *incp = dim->inc; | |
90 | } | |
91 | return scm_array_handle_writable_elements (h); | |
92 | } | |
93 | ||
a1ec6916 | 94 | SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0, |
5ffe9968 | 95 | (SCM obj), |
1e6808ea MG |
96 | "Return @code{#t} if @var{obj} is a vector, otherwise return\n" |
97 | "@code{#f}.") | |
1bbd0b84 | 98 | #define FUNC_NAME s_scm_vector_p |
0f2d19dd | 99 | { |
de5c0f58 | 100 | return scm_from_bool (scm_is_vector (obj)); |
0f2d19dd | 101 | } |
1bbd0b84 | 102 | #undef FUNC_NAME |
0f2d19dd | 103 | |
f172c0b7 | 104 | SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length); |
1e6808ea | 105 | /* Returns the number of elements in @var{vector} as an exact integer. */ |
0f2d19dd | 106 | SCM |
f172c0b7 | 107 | scm_vector_length (SCM v) |
0f2d19dd | 108 | { |
6e708ef2 MV |
109 | if (SCM_I_IS_VECTOR (v)) |
110 | return scm_from_size_t (SCM_I_VECTOR_LENGTH (v)); | |
04b87de5 | 111 | else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1) |
de5c0f58 | 112 | { |
04b87de5 | 113 | scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v); |
de5c0f58 MV |
114 | return scm_from_size_t (dim->ubnd - dim->lbnd + 1); |
115 | } | |
116 | else | |
117 | SCM_WTA_DISPATCH_1 (g_vector_length, v, 1, NULL); | |
0f2d19dd JB |
118 | } |
119 | ||
88797580 MV |
120 | size_t |
121 | scm_c_vector_length (SCM v) | |
122 | { | |
6e708ef2 MV |
123 | if (SCM_I_IS_VECTOR (v)) |
124 | return SCM_I_VECTOR_LENGTH (v); | |
88797580 | 125 | else |
de5c0f58 | 126 | return scm_to_size_t (scm_vector_length (v)); |
88797580 MV |
127 | } |
128 | ||
f172c0b7 | 129 | SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector); |
5ffe9968 | 130 | /* |
942e5b91 MG |
131 | "Return a newly created vector initialized to the elements of" |
132 | "the list @var{list}.\n\n" | |
133 | "@lisp\n" | |
134 | "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n" | |
135 | "(list->vector '(dididit dah)) @result{} #(dididit dah)\n" | |
136 | "@end lisp") | |
5ffe9968 | 137 | */ |
a1ec6916 | 138 | SCM_DEFINE (scm_vector, "vector", 0, 0, 1, |
f172c0b7 | 139 | (SCM l), |
8f85c0c6 NJ |
140 | "@deffnx {Scheme Procedure} list->vector l\n" |
141 | "Return a newly allocated vector composed of the\n" | |
1e6808ea MG |
142 | "given arguments. Analogous to @code{list}.\n" |
143 | "\n" | |
942e5b91 | 144 | "@lisp\n" |
1e6808ea | 145 | "(vector 'a 'b 'c) @result{} #(a b c)\n" |
942e5b91 | 146 | "@end lisp") |
1bbd0b84 | 147 | #define FUNC_NAME s_scm_vector |
0f2d19dd JB |
148 | { |
149 | SCM res; | |
22a52da1 | 150 | SCM *data; |
1d0df896 | 151 | long i, len; |
6e708ef2 | 152 | scm_t_array_handle handle; |
22a52da1 | 153 | |
1d0df896 | 154 | SCM_VALIDATE_LIST_COPYLEN (1, l, len); |
34d19ef6 | 155 | |
6e708ef2 MV |
156 | res = scm_c_make_vector (len, SCM_UNSPECIFIED); |
157 | data = scm_vector_writable_elements (res, &handle, NULL, NULL); | |
1d0df896 | 158 | i = 0; |
c8857a4d | 159 | while (scm_is_pair (l) && i < len) |
22a52da1 | 160 | { |
1d0df896 | 161 | data[i] = SCM_CAR (l); |
22a52da1 | 162 | l = SCM_CDR (l); |
6e708ef2 | 163 | i += 1; |
22a52da1 DH |
164 | } |
165 | ||
c8857a4d MV |
166 | scm_array_handle_release (&handle); |
167 | ||
0f2d19dd JB |
168 | return res; |
169 | } | |
1bbd0b84 | 170 | #undef FUNC_NAME |
0f2d19dd | 171 | |
f172c0b7 | 172 | SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref); |
1cc91f1b | 173 | |
5ffe9968 GB |
174 | /* |
175 | "@var{k} must be a valid index of @var{vector}.\n" | |
176 | "@samp{Vector-ref} returns the contents of element @var{k} of\n" | |
177 | "@var{vector}.\n\n" | |
942e5b91 MG |
178 | "@lisp\n" |
179 | "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n" | |
5ffe9968 GB |
180 | "(vector-ref '#(1 1 2 3 5 8 13 21)\n" |
181 | " (let ((i (round (* 2 (acos -1)))))\n" | |
182 | " (if (inexact? i)\n" | |
183 | " (inexact->exact i)\n" | |
942e5b91 MG |
184 | " i))) @result{} 13\n" |
185 | "@end lisp" | |
5ffe9968 GB |
186 | */ |
187 | ||
0f2d19dd | 188 | SCM |
ea633082 | 189 | scm_vector_ref (SCM v, SCM k) |
685c0d71 | 190 | #define FUNC_NAME s_vector_ref |
0f2d19dd | 191 | { |
de5c0f58 | 192 | return scm_c_vector_ref (v, scm_to_size_t (k)); |
0f2d19dd | 193 | } |
685c0d71 | 194 | #undef FUNC_NAME |
0f2d19dd | 195 | |
88797580 MV |
196 | SCM |
197 | scm_c_vector_ref (SCM v, size_t k) | |
198 | { | |
6e708ef2 | 199 | if (SCM_I_IS_VECTOR (v)) |
de5c0f58 | 200 | { |
6e708ef2 | 201 | if (k >= SCM_I_VECTOR_LENGTH (v)) |
de5c0f58 | 202 | scm_out_of_range (NULL, scm_from_size_t (k)); |
6e708ef2 | 203 | return (SCM_I_VECTOR_ELTS(v))[k]; |
de5c0f58 | 204 | } |
04b87de5 | 205 | else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1) |
88797580 | 206 | { |
04b87de5 MV |
207 | scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v); |
208 | SCM vv = SCM_I_ARRAY_V (v); | |
6e708ef2 MV |
209 | if (SCM_I_IS_VECTOR (vv)) |
210 | { | |
211 | if (k >= dim->ubnd - dim->lbnd + 1) | |
212 | scm_out_of_range (NULL, scm_from_size_t (k)); | |
04b87de5 | 213 | k = SCM_I_ARRAY_BASE (v) + k*dim->inc; |
6e708ef2 MV |
214 | return (SCM_I_VECTOR_ELTS (vv))[k]; |
215 | } | |
216 | scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector"); | |
88797580 | 217 | } |
de5c0f58 MV |
218 | else |
219 | SCM_WTA_DISPATCH_2 (g_vector_ref, v, scm_from_size_t (k), 2, NULL); | |
88797580 MV |
220 | } |
221 | ||
f172c0b7 | 222 | SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x); |
1cc91f1b | 223 | |
942e5b91 MG |
224 | /* "@var{k} must be a valid index of @var{vector}.\n" |
225 | "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n" | |
226 | "The value returned by @samp{vector-set!} is unspecified.\n" | |
227 | "@lisp\n" | |
228 | "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n" | |
229 | " (vector-set! vec 1 '("Sue" "Sue"))\n" | |
230 | " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n" | |
231 | "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n" | |
232 | "@end lisp" | |
5ffe9968 GB |
233 | */ |
234 | ||
0f2d19dd | 235 | SCM |
f172c0b7 | 236 | scm_vector_set_x (SCM v, SCM k, SCM obj) |
685c0d71 | 237 | #define FUNC_NAME s_vector_set_x |
0f2d19dd | 238 | { |
de5c0f58 | 239 | scm_c_vector_set_x (v, scm_to_size_t (k), obj); |
60c497a3 | 240 | return SCM_UNSPECIFIED; |
0f2d19dd | 241 | } |
685c0d71 | 242 | #undef FUNC_NAME |
0f2d19dd | 243 | |
de5c0f58 | 244 | void |
88797580 MV |
245 | scm_c_vector_set_x (SCM v, size_t k, SCM obj) |
246 | { | |
6e708ef2 | 247 | if (SCM_I_IS_VECTOR (v)) |
88797580 | 248 | { |
6e708ef2 | 249 | if (k >= SCM_I_VECTOR_LENGTH (v)) |
de5c0f58 | 250 | scm_out_of_range (NULL, scm_from_size_t (k)); |
6e708ef2 | 251 | (SCM_I_VECTOR_WELTS(v))[k] = obj; |
de5c0f58 | 252 | } |
04b87de5 | 253 | else if (SCM_I_ARRAYP (v) && SCM_I_ARRAY_NDIM (v) == 1) |
de5c0f58 | 254 | { |
04b87de5 MV |
255 | scm_t_array_dim *dim = SCM_I_ARRAY_DIMS (v); |
256 | SCM vv = SCM_I_ARRAY_V (v); | |
6e708ef2 MV |
257 | if (SCM_I_IS_VECTOR (vv)) |
258 | { | |
259 | if (k >= dim->ubnd - dim->lbnd + 1) | |
260 | scm_out_of_range (NULL, scm_from_size_t (k)); | |
04b87de5 | 261 | k = SCM_I_ARRAY_BASE (v) + k*dim->inc; |
6e708ef2 MV |
262 | (SCM_I_VECTOR_WELTS (vv))[k] = obj; |
263 | } | |
264 | else | |
265 | scm_wrong_type_arg_msg (NULL, 0, v, "non-uniform vector"); | |
88797580 MV |
266 | } |
267 | else | |
268 | { | |
de5c0f58 MV |
269 | if (SCM_UNPACK (g_vector_set_x)) |
270 | scm_apply_generic (g_vector_set_x, | |
271 | scm_list_3 (v, scm_from_size_t (k), obj)); | |
272 | else | |
273 | scm_wrong_type_arg_msg (NULL, 0, v, "vector"); | |
88797580 MV |
274 | } |
275 | } | |
0f2d19dd | 276 | |
a1ec6916 | 277 | SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0, |
1bbd0b84 | 278 | (SCM k, SCM fill), |
1e6808ea | 279 | "Return a newly allocated vector of @var{k} elements. If a\n" |
8f85c0c6 NJ |
280 | "second argument is given, then each position is initialized to\n" |
281 | "@var{fill}. Otherwise the initial contents of each position is\n" | |
1e6808ea | 282 | "unspecified.") |
1bbd0b84 | 283 | #define FUNC_NAME s_scm_make_vector |
0f2d19dd | 284 | { |
6e708ef2 | 285 | size_t l = scm_to_unsigned_integer (k, 0, VECTOR_MAX_LENGTH); |
e11e83f3 | 286 | |
1b9be268 | 287 | if (SCM_UNBNDP (fill)) |
d60cebe2 | 288 | fill = SCM_UNSPECIFIED; |
e11e83f3 MV |
289 | |
290 | return scm_c_make_vector (l, fill); | |
00ffa0e7 KN |
291 | } |
292 | #undef FUNC_NAME | |
293 | ||
e382fdbe | 294 | |
00ffa0e7 | 295 | SCM |
88797580 | 296 | scm_c_make_vector (size_t k, SCM fill) |
00ffa0e7 KN |
297 | #define FUNC_NAME s_scm_make_vector |
298 | { | |
299 | SCM v; | |
6e708ef2 | 300 | SCM *base; |
1b9be268 | 301 | |
e382fdbe DH |
302 | if (k > 0) |
303 | { | |
c014a02e | 304 | unsigned long int j; |
1b9be268 | 305 | |
6e708ef2 | 306 | SCM_ASSERT_RANGE (1, scm_from_ulong (k), k <= VECTOR_MAX_LENGTH); |
1b9be268 | 307 | |
6e708ef2 | 308 | base = scm_gc_malloc (k * sizeof (SCM), "vector"); |
e382fdbe | 309 | for (j = 0; j != k; ++j) |
6e708ef2 | 310 | base[j] = fill; |
e382fdbe DH |
311 | } |
312 | else | |
313 | base = NULL; | |
1b9be268 | 314 | |
6e708ef2 | 315 | v = scm_cell ((k << 8) | scm_tc7_vector, (scm_t_bits) base); |
e382fdbe | 316 | scm_remember_upto_here_1 (fill); |
1b9be268 | 317 | |
0f2d19dd JB |
318 | return v; |
319 | } | |
1bbd0b84 | 320 | #undef FUNC_NAME |
0f2d19dd | 321 | |
6e708ef2 MV |
322 | SCM_DEFINE (scm_vector_copy, "vector-copy", 1, 0, 0, |
323 | (SCM vec), | |
324 | "Return a copy of @var{vec}.") | |
325 | #define FUNC_NAME s_scm_vector_copy | |
326 | { | |
327 | scm_t_array_handle handle; | |
328 | size_t i, len; | |
329 | ssize_t inc; | |
330 | const SCM *src; | |
331 | SCM *dst; | |
332 | ||
333 | src = scm_vector_elements (vec, &handle, &len, &inc); | |
334 | dst = scm_gc_malloc (len * sizeof (SCM), "vector"); | |
335 | for (i = 0; i < len; i++, src += inc) | |
336 | dst[i] = *src; | |
c8857a4d | 337 | scm_array_handle_release (&handle); |
6e708ef2 MV |
338 | |
339 | return scm_cell ((len << 8) | scm_tc7_vector, (scm_t_bits) dst); | |
340 | } | |
341 | #undef FUNC_NAME | |
342 | ||
343 | void | |
344 | scm_i_vector_free (SCM vec) | |
345 | { | |
346 | scm_gc_free (SCM_I_VECTOR_WELTS (vec), | |
347 | SCM_I_VECTOR_LENGTH (vec) * sizeof(SCM), | |
348 | "vector"); | |
349 | } | |
350 | ||
351 | /* Allocate memory for a weak vector on behalf of the caller. The allocated | |
352 | * vector will be of the given weak vector subtype. It will contain size | |
353 | * elements which are initialized with the 'fill' object, or, if 'fill' is | |
354 | * undefined, with an unspecified object. | |
355 | */ | |
356 | SCM | |
357 | scm_i_allocate_weak_vector (scm_t_bits type, SCM size, SCM fill) | |
358 | { | |
359 | size_t c_size; | |
360 | SCM *base; | |
361 | SCM v; | |
362 | ||
363 | c_size = scm_to_unsigned_integer (size, 0, VECTOR_MAX_LENGTH); | |
364 | ||
365 | if (c_size > 0) | |
366 | { | |
367 | size_t j; | |
368 | ||
369 | if (SCM_UNBNDP (fill)) | |
370 | fill = SCM_UNSPECIFIED; | |
371 | ||
372 | base = scm_gc_malloc (c_size * sizeof (SCM), "weak vector"); | |
373 | for (j = 0; j != c_size; ++j) | |
374 | base[j] = fill; | |
375 | } | |
376 | else | |
377 | base = NULL; | |
378 | ||
379 | v = scm_double_cell ((c_size << 8) | scm_tc7_wvect, | |
380 | (scm_t_bits) base, | |
381 | type, | |
382 | SCM_UNPACK (SCM_EOL)); | |
383 | scm_remember_upto_here_1 (fill); | |
384 | ||
385 | return v; | |
386 | } | |
e382fdbe | 387 | |
3b3b36dd | 388 | SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0, |
1e6808ea | 389 | (SCM v), |
8f85c0c6 | 390 | "Return a newly allocated list composed of the elements of @var{v}.\n" |
1e6808ea | 391 | "\n" |
942e5b91 MG |
392 | "@lisp\n" |
393 | "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n" | |
394 | "(list->vector '(dididit dah)) @result{} #(dididit dah)\n" | |
395 | "@end lisp") | |
1bbd0b84 | 396 | #define FUNC_NAME s_scm_vector_to_list |
0f2d19dd | 397 | { |
6e708ef2 MV |
398 | SCM res = SCM_EOL; |
399 | const SCM *data; | |
400 | scm_t_array_handle handle; | |
22be72d3 | 401 | size_t i, count, len; |
6e708ef2 MV |
402 | ssize_t inc; |
403 | ||
404 | data = scm_vector_elements (v, &handle, &len, &inc); | |
22be72d3 LC |
405 | for (i = (len - 1) * inc, count = 0; |
406 | count < len; | |
407 | i -= inc, count++) | |
408 | res = scm_cons (data[i], res); | |
409 | ||
c8857a4d | 410 | scm_array_handle_release (&handle); |
6e708ef2 | 411 | return res; |
0f2d19dd | 412 | } |
1bbd0b84 | 413 | #undef FUNC_NAME |
0f2d19dd JB |
414 | |
415 | ||
a1ec6916 | 416 | SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0, |
1e6808ea | 417 | (SCM v, SCM fill), |
8f85c0c6 | 418 | "Store @var{fill} in every position of @var{vector}. The value\n" |
1e6808ea | 419 | "returned by @code{vector-fill!} is unspecified.") |
1bbd0b84 | 420 | #define FUNC_NAME s_scm_vector_fill_x |
0f2d19dd | 421 | { |
6e708ef2 MV |
422 | scm_t_array_handle handle; |
423 | SCM *data; | |
424 | size_t i, len; | |
425 | ssize_t inc; | |
426 | ||
427 | data = scm_vector_writable_elements (v, &handle, &len, &inc); | |
428 | for (i = 0; i < len; i += inc) | |
429 | data[i] = fill; | |
c8857a4d | 430 | scm_array_handle_release (&handle); |
6e708ef2 | 431 | return SCM_UNSPECIFIED; |
0f2d19dd | 432 | } |
1bbd0b84 | 433 | #undef FUNC_NAME |
0f2d19dd JB |
434 | |
435 | ||
0f2d19dd | 436 | SCM |
354116f7 | 437 | scm_i_vector_equal_p (SCM x, SCM y) |
0f2d19dd | 438 | { |
c014a02e | 439 | long i; |
6e708ef2 MV |
440 | for (i = SCM_I_VECTOR_LENGTH (x) - 1; i >= 0; i--) |
441 | if (scm_is_false (scm_equal_p (SCM_I_VECTOR_ELTS (x)[i], | |
442 | SCM_I_VECTOR_ELTS (y)[i]))) | |
0f2d19dd JB |
443 | return SCM_BOOL_F; |
444 | return SCM_BOOL_T; | |
445 | } | |
446 | ||
447 | ||
a1ec6916 | 448 | SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0, |
1bbd0b84 | 449 | (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2), |
694a9bb3 NJ |
450 | "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n" |
451 | "to @var{vec2} starting at position @var{start2}. @var{start1} and\n" | |
452 | "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n" | |
453 | "@code{vector-move-left!} copies elements in leftmost order.\n" | |
454 | "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n" | |
455 | "same vector, @code{vector-move-left!} is usually appropriate when\n" | |
456 | "@var{start1} is greater than @var{start2}.") | |
1bbd0b84 | 457 | #define FUNC_NAME s_scm_vector_move_left_x |
0f2d19dd | 458 | { |
6e708ef2 MV |
459 | scm_t_array_handle handle1, handle2; |
460 | const SCM *elts1; | |
461 | SCM *elts2; | |
de5c0f58 | 462 | size_t len1, len2; |
6e708ef2 | 463 | ssize_t inc1, inc2; |
a55c2b68 | 464 | size_t i, j, e; |
6e708ef2 MV |
465 | |
466 | elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1); | |
467 | elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2); | |
34d19ef6 | 468 | |
de5c0f58 MV |
469 | i = scm_to_unsigned_integer (start1, 0, len1); |
470 | e = scm_to_unsigned_integer (end1, i, len1); | |
471 | j = scm_to_unsigned_integer (start2, 0, len2 - (i-e)); | |
472 | ||
6e708ef2 MV |
473 | i *= inc1; |
474 | e *= inc1; | |
475 | j *= inc2; | |
476 | for (; i < e; i += inc1, j += inc2) | |
477 | elts2[j] = elts1[i]; | |
478 | ||
c8857a4d MV |
479 | scm_array_handle_release (&handle2); |
480 | scm_array_handle_release (&handle1); | |
481 | ||
0f2d19dd JB |
482 | return SCM_UNSPECIFIED; |
483 | } | |
1bbd0b84 | 484 | #undef FUNC_NAME |
0f2d19dd | 485 | |
a1ec6916 | 486 | SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0, |
1bbd0b84 | 487 | (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2), |
694a9bb3 NJ |
488 | "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n" |
489 | "to @var{vec2} starting at position @var{start2}. @var{start1} and\n" | |
490 | "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n" | |
491 | "@code{vector-move-right!} copies elements in rightmost order.\n" | |
492 | "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n" | |
493 | "same vector, @code{vector-move-right!} is usually appropriate when\n" | |
494 | "@var{start1} is less than @var{start2}.") | |
1bbd0b84 | 495 | #define FUNC_NAME s_scm_vector_move_right_x |
0f2d19dd | 496 | { |
6e708ef2 MV |
497 | scm_t_array_handle handle1, handle2; |
498 | const SCM *elts1; | |
499 | SCM *elts2; | |
de5c0f58 | 500 | size_t len1, len2; |
6e708ef2 | 501 | ssize_t inc1, inc2; |
a55c2b68 | 502 | size_t i, j, e; |
6e708ef2 MV |
503 | |
504 | elts1 = scm_vector_elements (vec1, &handle1, &len1, &inc1); | |
505 | elts2 = scm_vector_writable_elements (vec2, &handle2, &len2, &inc2); | |
0f2d19dd | 506 | |
de5c0f58 MV |
507 | i = scm_to_unsigned_integer (start1, 0, len1); |
508 | e = scm_to_unsigned_integer (end1, i, len1); | |
509 | j = scm_to_unsigned_integer (start2, 0, len2 - (i-e)); | |
510 | ||
6e708ef2 MV |
511 | i *= inc1; |
512 | e *= inc1; | |
513 | j *= inc2; | |
514 | while (i < e) | |
de5c0f58 | 515 | { |
6e708ef2 MV |
516 | e -= inc1; |
517 | j -= inc2; | |
518 | elts2[j] = elts1[e]; | |
de5c0f58 | 519 | } |
6e708ef2 | 520 | |
c8857a4d MV |
521 | scm_array_handle_release (&handle2); |
522 | scm_array_handle_release (&handle1); | |
523 | ||
0f2d19dd JB |
524 | return SCM_UNSPECIFIED; |
525 | } | |
1bbd0b84 | 526 | #undef FUNC_NAME |
0f2d19dd | 527 | |
438974d0 | 528 | \f |
88797580 MV |
529 | /* Generalized vectors. */ |
530 | ||
531 | int | |
532 | scm_is_generalized_vector (SCM obj) | |
533 | { | |
534 | return (scm_is_vector (obj) | |
535 | || scm_is_string (obj) | |
536 | || scm_is_bitvector (obj) | |
438974d0 LC |
537 | || scm_is_uniform_vector (obj) |
538 | || scm_is_bytevector (obj)); | |
88797580 MV |
539 | } |
540 | ||
541 | SCM_DEFINE (scm_generalized_vector_p, "generalized-vector?", 1, 0, 0, | |
542 | (SCM obj), | |
543 | "Return @code{#t} if @var{obj} is a vector, string,\n" | |
544 | "bitvector, or uniform numeric vector.") | |
545 | #define FUNC_NAME s_scm_generalized_vector_p | |
546 | { | |
547 | return scm_from_bool (scm_is_generalized_vector (obj)); | |
548 | } | |
549 | #undef FUNC_NAME | |
550 | ||
354116f7 MV |
551 | void |
552 | scm_generalized_vector_get_handle (SCM vec, scm_t_array_handle *h) | |
553 | { | |
554 | scm_array_get_handle (vec, h); | |
555 | if (scm_array_handle_rank (h) != 1) | |
556 | scm_wrong_type_arg_msg (NULL, 0, vec, "vector"); | |
557 | } | |
558 | ||
88797580 MV |
559 | size_t |
560 | scm_c_generalized_vector_length (SCM v) | |
561 | { | |
562 | if (scm_is_vector (v)) | |
563 | return scm_c_vector_length (v); | |
564 | else if (scm_is_string (v)) | |
565 | return scm_c_string_length (v); | |
566 | else if (scm_is_bitvector (v)) | |
567 | return scm_c_bitvector_length (v); | |
568 | else if (scm_is_uniform_vector (v)) | |
569 | return scm_c_uniform_vector_length (v); | |
438974d0 LC |
570 | else if (scm_is_bytevector (v)) |
571 | return scm_c_bytevector_length (v); | |
88797580 MV |
572 | else |
573 | scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector"); | |
574 | } | |
575 | ||
576 | SCM_DEFINE (scm_generalized_vector_length, "generalized-vector-length", 1, 0, 0, | |
577 | (SCM v), | |
578 | "Return the length of the generalized vector @var{v}.") | |
579 | #define FUNC_NAME s_scm_generalized_vector_length | |
580 | { | |
581 | return scm_from_size_t (scm_c_generalized_vector_length (v)); | |
582 | } | |
583 | #undef FUNC_NAME | |
584 | ||
585 | SCM | |
586 | scm_c_generalized_vector_ref (SCM v, size_t idx) | |
587 | { | |
588 | if (scm_is_vector (v)) | |
589 | return scm_c_vector_ref (v, idx); | |
590 | else if (scm_is_string (v)) | |
591 | return scm_c_string_ref (v, idx); | |
592 | else if (scm_is_bitvector (v)) | |
593 | return scm_c_bitvector_ref (v, idx); | |
594 | else if (scm_is_uniform_vector (v)) | |
595 | return scm_c_uniform_vector_ref (v, idx); | |
438974d0 LC |
596 | else if (scm_is_bytevector (v)) |
597 | return scm_from_uint8 (scm_c_bytevector_ref (v, idx)); | |
88797580 MV |
598 | else |
599 | scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector"); | |
600 | } | |
601 | ||
602 | SCM_DEFINE (scm_generalized_vector_ref, "generalized-vector-ref", 2, 0, 0, | |
603 | (SCM v, SCM idx), | |
604 | "Return the element at index @var{idx} of the\n" | |
605 | "generalized vector @var{v}.") | |
606 | #define FUNC_NAME s_scm_generalized_vector_ref | |
607 | { | |
608 | return scm_c_generalized_vector_ref (v, scm_to_size_t (idx)); | |
609 | } | |
610 | #undef FUNC_NAME | |
611 | ||
612 | void | |
613 | scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val) | |
614 | { | |
615 | if (scm_is_vector (v)) | |
616 | scm_c_vector_set_x (v, idx, val); | |
617 | else if (scm_is_string (v)) | |
618 | scm_c_string_set_x (v, idx, val); | |
619 | else if (scm_is_bitvector (v)) | |
620 | scm_c_bitvector_set_x (v, idx, val); | |
621 | else if (scm_is_uniform_vector (v)) | |
622 | scm_c_uniform_vector_set_x (v, idx, val); | |
438974d0 LC |
623 | else if (scm_is_bytevector (v)) |
624 | scm_i_bytevector_generalized_set_x (v, idx, val); | |
88797580 MV |
625 | else |
626 | scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector"); | |
627 | } | |
628 | ||
629 | SCM_DEFINE (scm_generalized_vector_set_x, "generalized-vector-set!", 3, 0, 0, | |
630 | (SCM v, SCM idx, SCM val), | |
631 | "Set the element at index @var{idx} of the\n" | |
632 | "generalized vector @var{v} to @var{val}.") | |
633 | #define FUNC_NAME s_scm_generalized_vector_set_x | |
634 | { | |
635 | scm_c_generalized_vector_set_x (v, scm_to_size_t (idx), val); | |
636 | return SCM_UNSPECIFIED; | |
637 | } | |
638 | #undef FUNC_NAME | |
639 | ||
640 | SCM_DEFINE (scm_generalized_vector_to_list, "generalized-vector->list", 1, 0, 0, | |
641 | (SCM v), | |
642 | "Return a new list whose elements are the elements of the\n" | |
643 | "generalized vector @var{v}.") | |
644 | #define FUNC_NAME s_scm_generalized_vector_to_list | |
645 | { | |
646 | if (scm_is_vector (v)) | |
647 | return scm_vector_to_list (v); | |
648 | else if (scm_is_string (v)) | |
649 | return scm_string_to_list (v); | |
650 | else if (scm_is_bitvector (v)) | |
651 | return scm_bitvector_to_list (v); | |
652 | else if (scm_is_uniform_vector (v)) | |
653 | return scm_uniform_vector_to_list (v); | |
654 | else | |
655 | scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector"); | |
656 | } | |
657 | #undef FUNC_NAME | |
658 | ||
1cc91f1b | 659 | |
0f2d19dd JB |
660 | void |
661 | scm_init_vectors () | |
0f2d19dd | 662 | { |
7c33806a DH |
663 | scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED); |
664 | ||
a0599745 | 665 | #include "libguile/vectors.x" |
0f2d19dd JB |
666 | } |
667 | ||
89e00824 ML |
668 | |
669 | /* | |
670 | Local Variables: | |
671 | c-file-style: "gnu" | |
672 | End: | |
673 | */ |