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