Commit | Line | Data |
---|---|---|
22a52da1 | 1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001 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 | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
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" |
88797580 | 35 | |
0f2d19dd JB |
36 | \f |
37 | ||
de5c0f58 MV |
38 | int |
39 | scm_is_vector (SCM obj) | |
40 | { | |
41 | return (SCM_VECTORP (obj) | |
42 | || (SCM_ARRAYP (obj) && SCM_ARRAY_NDIM (obj) == 1)); | |
43 | } | |
44 | ||
1d0df896 MV |
45 | SCM * |
46 | scm_vector_writable_elements (SCM vec) | |
47 | { | |
48 | if (SCM_VECTORP (vec)) | |
49 | return SCM_WRITABLE_VELTS (vec); | |
50 | else | |
51 | scm_wrong_type_arg_msg (NULL, 0, vec, "simple vector"); | |
52 | } | |
53 | ||
54 | const SCM * | |
55 | scm_vector_elements (SCM vec) | |
56 | { | |
57 | if (SCM_VECTORP (vec)) | |
58 | return SCM_VELTS (vec); | |
59 | else | |
60 | scm_wrong_type_arg_msg (NULL, 0, vec, "simple vector"); | |
61 | } | |
62 | ||
63 | void | |
64 | scm_vector_release_writable_elements (SCM vec) | |
65 | { | |
66 | scm_remember_upto_here_1 (vec); | |
67 | } | |
68 | ||
69 | void | |
70 | scm_vector_release_elements (SCM vec) | |
71 | { | |
72 | scm_remember_upto_here_1 (vec); | |
73 | } | |
74 | ||
75 | void | |
76 | scm_frame_vector_release_writable_elements (SCM vec) | |
77 | { | |
78 | scm_frame_unwind_handler_with_scm | |
79 | (scm_vector_release_writable_elements, vec, | |
80 | SCM_F_WIND_EXPLICITLY); | |
81 | } | |
82 | ||
83 | void | |
84 | scm_frame_vector_release_elements (SCM vec) | |
85 | { | |
86 | scm_frame_unwind_handler_with_scm | |
87 | (scm_vector_release_elements, vec, | |
88 | SCM_F_WIND_EXPLICITLY); | |
89 | } | |
90 | ||
a1ec6916 | 91 | SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0, |
5ffe9968 | 92 | (SCM obj), |
1e6808ea MG |
93 | "Return @code{#t} if @var{obj} is a vector, otherwise return\n" |
94 | "@code{#f}.") | |
1bbd0b84 | 95 | #define FUNC_NAME s_scm_vector_p |
0f2d19dd | 96 | { |
de5c0f58 | 97 | return scm_from_bool (scm_is_vector (obj)); |
0f2d19dd | 98 | } |
1bbd0b84 | 99 | #undef FUNC_NAME |
0f2d19dd | 100 | |
f172c0b7 | 101 | SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length); |
1e6808ea | 102 | /* Returns the number of elements in @var{vector} as an exact integer. */ |
0f2d19dd | 103 | SCM |
f172c0b7 | 104 | scm_vector_length (SCM v) |
0f2d19dd | 105 | { |
de5c0f58 MV |
106 | if (SCM_VECTORP (v)) |
107 | return scm_from_size_t (SCM_VECTOR_LENGTH (v)); | |
108 | else if (SCM_ARRAYP (v) && SCM_ARRAY_NDIM (v) == 1) | |
109 | { | |
110 | scm_t_array_dim *dim = SCM_ARRAY_DIMS (v); | |
111 | return scm_from_size_t (dim->ubnd - dim->lbnd + 1); | |
112 | } | |
113 | else | |
114 | SCM_WTA_DISPATCH_1 (g_vector_length, v, 1, NULL); | |
0f2d19dd JB |
115 | } |
116 | ||
88797580 MV |
117 | size_t |
118 | scm_c_vector_length (SCM v) | |
119 | { | |
120 | if (SCM_VECTORP (v)) | |
121 | return SCM_VECTOR_LENGTH (v); | |
122 | else | |
de5c0f58 | 123 | return scm_to_size_t (scm_vector_length (v)); |
88797580 MV |
124 | } |
125 | ||
f172c0b7 | 126 | SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector); |
5ffe9968 | 127 | /* |
942e5b91 MG |
128 | "Return a newly created vector initialized to the elements of" |
129 | "the list @var{list}.\n\n" | |
130 | "@lisp\n" | |
131 | "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n" | |
132 | "(list->vector '(dididit dah)) @result{} #(dididit dah)\n" | |
133 | "@end lisp") | |
5ffe9968 | 134 | */ |
a1ec6916 | 135 | SCM_DEFINE (scm_vector, "vector", 0, 0, 1, |
f172c0b7 | 136 | (SCM l), |
8f85c0c6 NJ |
137 | "@deffnx {Scheme Procedure} list->vector l\n" |
138 | "Return a newly allocated vector composed of the\n" | |
1e6808ea MG |
139 | "given arguments. Analogous to @code{list}.\n" |
140 | "\n" | |
942e5b91 | 141 | "@lisp\n" |
1e6808ea | 142 | "(vector 'a 'b 'c) @result{} #(a b c)\n" |
942e5b91 | 143 | "@end lisp") |
1bbd0b84 | 144 | #define FUNC_NAME s_scm_vector |
0f2d19dd JB |
145 | { |
146 | SCM res; | |
22a52da1 | 147 | SCM *data; |
1d0df896 | 148 | long i, len; |
22a52da1 | 149 | |
1d0df896 MV |
150 | SCM_VALIDATE_LIST_COPYLEN (1, l, len); |
151 | res = scm_c_make_vector (len, SCM_UNSPECIFIED); | |
34d19ef6 | 152 | |
1d0df896 MV |
153 | data = scm_vector_writable_elements (res); |
154 | i = 0; | |
155 | while (!SCM_NULL_OR_NIL_P (l) && i < len) | |
22a52da1 | 156 | { |
1d0df896 | 157 | data[i] = SCM_CAR (l); |
22a52da1 | 158 | l = SCM_CDR (l); |
1d0df896 | 159 | i++; |
22a52da1 | 160 | } |
1d0df896 | 161 | scm_vector_release_writable_elements (res); |
22a52da1 | 162 | |
0f2d19dd JB |
163 | return res; |
164 | } | |
1bbd0b84 | 165 | #undef FUNC_NAME |
0f2d19dd | 166 | |
f172c0b7 | 167 | SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref); |
1cc91f1b | 168 | |
5ffe9968 GB |
169 | /* |
170 | "@var{k} must be a valid index of @var{vector}.\n" | |
171 | "@samp{Vector-ref} returns the contents of element @var{k} of\n" | |
172 | "@var{vector}.\n\n" | |
942e5b91 MG |
173 | "@lisp\n" |
174 | "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n" | |
5ffe9968 GB |
175 | "(vector-ref '#(1 1 2 3 5 8 13 21)\n" |
176 | " (let ((i (round (* 2 (acos -1)))))\n" | |
177 | " (if (inexact? i)\n" | |
178 | " (inexact->exact i)\n" | |
942e5b91 MG |
179 | " i))) @result{} 13\n" |
180 | "@end lisp" | |
5ffe9968 GB |
181 | */ |
182 | ||
0f2d19dd | 183 | SCM |
ea633082 | 184 | scm_vector_ref (SCM v, SCM k) |
685c0d71 | 185 | #define FUNC_NAME s_vector_ref |
0f2d19dd | 186 | { |
de5c0f58 | 187 | return scm_c_vector_ref (v, scm_to_size_t (k)); |
0f2d19dd | 188 | } |
685c0d71 | 189 | #undef FUNC_NAME |
0f2d19dd | 190 | |
88797580 MV |
191 | SCM |
192 | scm_c_vector_ref (SCM v, size_t k) | |
193 | { | |
de5c0f58 MV |
194 | if (SCM_VECTORP (v)) |
195 | { | |
196 | if (k >= SCM_VECTOR_LENGTH (v)) | |
197 | scm_out_of_range (NULL, scm_from_size_t (k)); | |
198 | return SCM_VECTOR_REF (v, k); | |
199 | } | |
200 | else if (SCM_ARRAYP (v) && SCM_ARRAY_NDIM (v) == 1) | |
88797580 | 201 | { |
de5c0f58 MV |
202 | scm_t_array_dim *dim = SCM_ARRAY_DIMS (v); |
203 | if (k >= dim->ubnd - dim->lbnd + 1) | |
204 | scm_out_of_range (NULL, scm_from_size_t (k)); | |
205 | k = SCM_ARRAY_BASE (v) + k*dim->inc; | |
206 | return scm_c_generalized_vector_ref (SCM_ARRAY_V (v), k); | |
88797580 | 207 | } |
de5c0f58 MV |
208 | else |
209 | SCM_WTA_DISPATCH_2 (g_vector_ref, v, scm_from_size_t (k), 2, NULL); | |
88797580 MV |
210 | } |
211 | ||
f172c0b7 | 212 | SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x); |
1cc91f1b | 213 | |
942e5b91 MG |
214 | /* "@var{k} must be a valid index of @var{vector}.\n" |
215 | "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n" | |
216 | "The value returned by @samp{vector-set!} is unspecified.\n" | |
217 | "@lisp\n" | |
218 | "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n" | |
219 | " (vector-set! vec 1 '("Sue" "Sue"))\n" | |
220 | " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n" | |
221 | "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n" | |
222 | "@end lisp" | |
5ffe9968 GB |
223 | */ |
224 | ||
0f2d19dd | 225 | SCM |
f172c0b7 | 226 | scm_vector_set_x (SCM v, SCM k, SCM obj) |
685c0d71 | 227 | #define FUNC_NAME s_vector_set_x |
0f2d19dd | 228 | { |
de5c0f58 | 229 | scm_c_vector_set_x (v, scm_to_size_t (k), obj); |
60c497a3 | 230 | return SCM_UNSPECIFIED; |
0f2d19dd | 231 | } |
685c0d71 | 232 | #undef FUNC_NAME |
0f2d19dd | 233 | |
de5c0f58 | 234 | void |
88797580 MV |
235 | scm_c_vector_set_x (SCM v, size_t k, SCM obj) |
236 | { | |
de5c0f58 | 237 | if (SCM_VECTORP (v)) |
88797580 | 238 | { |
de5c0f58 MV |
239 | if (k >= SCM_VECTOR_LENGTH (v)) |
240 | scm_out_of_range (NULL, scm_from_size_t (k)); | |
88797580 | 241 | SCM_VECTOR_SET (v, k, obj); |
de5c0f58 MV |
242 | } |
243 | else if (SCM_ARRAYP (v) && SCM_ARRAY_NDIM (v) == 1) | |
244 | { | |
245 | scm_t_array_dim *dim = SCM_ARRAY_DIMS (v); | |
246 | if (k >= dim->ubnd - dim->lbnd + 1) | |
247 | scm_out_of_range (NULL, scm_from_size_t (k)); | |
248 | k = SCM_ARRAY_BASE (v) + k*dim->inc; | |
249 | scm_c_generalized_vector_set_x (SCM_ARRAY_V (v), k, obj); | |
88797580 MV |
250 | } |
251 | else | |
252 | { | |
de5c0f58 MV |
253 | if (SCM_UNPACK (g_vector_set_x)) |
254 | scm_apply_generic (g_vector_set_x, | |
255 | scm_list_3 (v, scm_from_size_t (k), obj)); | |
256 | else | |
257 | scm_wrong_type_arg_msg (NULL, 0, v, "vector"); | |
88797580 MV |
258 | } |
259 | } | |
0f2d19dd | 260 | |
a1ec6916 | 261 | SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0, |
1bbd0b84 | 262 | (SCM k, SCM fill), |
1e6808ea | 263 | "Return a newly allocated vector of @var{k} elements. If a\n" |
8f85c0c6 NJ |
264 | "second argument is given, then each position is initialized to\n" |
265 | "@var{fill}. Otherwise the initial contents of each position is\n" | |
1e6808ea | 266 | "unspecified.") |
1bbd0b84 | 267 | #define FUNC_NAME s_scm_make_vector |
0f2d19dd | 268 | { |
e11e83f3 MV |
269 | size_t l = scm_to_unsigned_integer (k, 0, SCM_VECTOR_MAX_LENGTH); |
270 | ||
1b9be268 | 271 | if (SCM_UNBNDP (fill)) |
d60cebe2 | 272 | fill = SCM_UNSPECIFIED; |
e11e83f3 MV |
273 | |
274 | return scm_c_make_vector (l, fill); | |
00ffa0e7 KN |
275 | } |
276 | #undef FUNC_NAME | |
277 | ||
e382fdbe | 278 | |
00ffa0e7 | 279 | SCM |
88797580 | 280 | scm_c_make_vector (size_t k, SCM fill) |
00ffa0e7 KN |
281 | #define FUNC_NAME s_scm_make_vector |
282 | { | |
283 | SCM v; | |
92c2555f | 284 | scm_t_bits *base; |
1b9be268 | 285 | |
e382fdbe DH |
286 | if (k > 0) |
287 | { | |
c014a02e | 288 | unsigned long int j; |
1b9be268 | 289 | |
b9bd8526 | 290 | SCM_ASSERT_RANGE (1, scm_from_ulong (k), k <= SCM_VECTOR_MAX_LENGTH); |
1b9be268 | 291 | |
4c9419ac | 292 | base = scm_gc_malloc (k * sizeof (scm_t_bits), "vector"); |
e382fdbe DH |
293 | for (j = 0; j != k; ++j) |
294 | base[j] = SCM_UNPACK (fill); | |
295 | } | |
296 | else | |
297 | base = NULL; | |
1b9be268 | 298 | |
228a24ef | 299 | v = scm_cell (SCM_MAKE_VECTOR_TAG (k, scm_tc7_vector), (scm_t_bits) base); |
e382fdbe | 300 | scm_remember_upto_here_1 (fill); |
1b9be268 | 301 | |
0f2d19dd JB |
302 | return v; |
303 | } | |
1bbd0b84 | 304 | #undef FUNC_NAME |
0f2d19dd | 305 | |
e382fdbe | 306 | |
3b3b36dd | 307 | SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0, |
1e6808ea | 308 | (SCM v), |
8f85c0c6 | 309 | "Return a newly allocated list composed of the elements of @var{v}.\n" |
1e6808ea | 310 | "\n" |
942e5b91 MG |
311 | "@lisp\n" |
312 | "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n" | |
313 | "(list->vector '(dididit dah)) @result{} #(dididit dah)\n" | |
314 | "@end lisp") | |
1bbd0b84 | 315 | #define FUNC_NAME s_scm_vector_to_list |
0f2d19dd | 316 | { |
de5c0f58 MV |
317 | if (SCM_VECTORP (v)) |
318 | { | |
319 | SCM res = SCM_EOL; | |
320 | long i; | |
1d0df896 MV |
321 | const SCM *data; |
322 | data = scm_vector_elements (v); | |
de5c0f58 | 323 | for(i = SCM_VECTOR_LENGTH(v)-1; i >= 0; i--) |
1d0df896 MV |
324 | res = scm_cons (data[i], res); |
325 | scm_vector_release_elements (v); | |
de5c0f58 MV |
326 | return res; |
327 | } | |
328 | else if (SCM_ARRAYP (v) && SCM_ARRAY_NDIM (v) == 1) | |
329 | return scm_array_to_list (v); | |
330 | else | |
331 | scm_wrong_type_arg_msg (NULL, 0, v, "vector"); | |
0f2d19dd | 332 | } |
1bbd0b84 | 333 | #undef FUNC_NAME |
0f2d19dd JB |
334 | |
335 | ||
a1ec6916 | 336 | SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0, |
1e6808ea | 337 | (SCM v, SCM fill), |
8f85c0c6 | 338 | "Store @var{fill} in every position of @var{vector}. The value\n" |
1e6808ea | 339 | "returned by @code{vector-fill!} is unspecified.") |
1bbd0b84 | 340 | #define FUNC_NAME s_scm_vector_fill_x |
0f2d19dd | 341 | { |
de5c0f58 MV |
342 | if (SCM_VECTORP (v)) |
343 | { | |
1d0df896 MV |
344 | size_t i, len; |
345 | SCM *elts = scm_vector_writable_elements (v); | |
346 | for (i = 0, len = SCM_VECTOR_LENGTH (v); i < len; i++) | |
347 | elts[i] = fill; | |
de5c0f58 MV |
348 | return SCM_UNSPECIFIED; |
349 | } | |
350 | else if (SCM_ARRAYP (v) && SCM_ARRAY_NDIM (v) == 1) | |
351 | return scm_array_fill_x (v, fill); | |
352 | else | |
353 | scm_wrong_type_arg_msg (NULL, 0, v, "vector"); | |
0f2d19dd | 354 | } |
1bbd0b84 | 355 | #undef FUNC_NAME |
0f2d19dd JB |
356 | |
357 | ||
0f2d19dd | 358 | SCM |
1d0df896 | 359 | scm_vector_equal_p (SCM x, SCM y) |
0f2d19dd | 360 | { |
c014a02e | 361 | long i; |
1d0df896 | 362 | for (i = SCM_VECTOR_LENGTH (x) - 1; i >= 0; i--) |
7888309b | 363 | if (scm_is_false (scm_equal_p (SCM_VELTS (x)[i], SCM_VELTS (y)[i]))) |
0f2d19dd JB |
364 | return SCM_BOOL_F; |
365 | return SCM_BOOL_T; | |
366 | } | |
367 | ||
368 | ||
a1ec6916 | 369 | SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0, |
1bbd0b84 | 370 | (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2), |
694a9bb3 NJ |
371 | "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n" |
372 | "to @var{vec2} starting at position @var{start2}. @var{start1} and\n" | |
373 | "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n" | |
374 | "@code{vector-move-left!} copies elements in leftmost order.\n" | |
375 | "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n" | |
376 | "same vector, @code{vector-move-left!} is usually appropriate when\n" | |
377 | "@var{start1} is greater than @var{start2}.") | |
1bbd0b84 | 378 | #define FUNC_NAME s_scm_vector_move_left_x |
0f2d19dd | 379 | { |
de5c0f58 | 380 | size_t len1, len2; |
a55c2b68 | 381 | size_t i, j, e; |
34d19ef6 | 382 | |
de5c0f58 MV |
383 | len1 = scm_c_vector_length (vec1); |
384 | len2 = scm_c_vector_length (vec2); | |
385 | i = scm_to_unsigned_integer (start1, 0, len1); | |
386 | e = scm_to_unsigned_integer (end1, i, len1); | |
387 | j = scm_to_unsigned_integer (start2, 0, len2 - (i-e)); | |
388 | ||
1d0df896 | 389 | /* Optimize common case of two simple vectors. |
de5c0f58 MV |
390 | */ |
391 | if (SCM_VECTORP (vec1) && SCM_VECTORP (vec2)) | |
392 | { | |
1d0df896 MV |
393 | const SCM *elts1 = scm_vector_elements (vec1); |
394 | SCM *elts2 = scm_vector_writable_elements (vec2); | |
de5c0f58 | 395 | for (; i < e; i++, j++) |
1d0df896 MV |
396 | elts2[j] = elts1[i]; |
397 | scm_vector_release_elements (vec1); | |
398 | scm_vector_release_writable_elements (vec2); | |
de5c0f58 MV |
399 | } |
400 | else | |
1d1559ce | 401 | { |
de5c0f58 MV |
402 | for (; i < e; i++, j++) |
403 | scm_c_vector_set_x (vec2, j, scm_c_vector_ref (vec1, i)); | |
1d1559ce | 404 | } |
34d19ef6 | 405 | |
0f2d19dd JB |
406 | return SCM_UNSPECIFIED; |
407 | } | |
1bbd0b84 | 408 | #undef FUNC_NAME |
0f2d19dd | 409 | |
a1ec6916 | 410 | SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0, |
1bbd0b84 | 411 | (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2), |
694a9bb3 NJ |
412 | "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n" |
413 | "to @var{vec2} starting at position @var{start2}. @var{start1} and\n" | |
414 | "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n" | |
415 | "@code{vector-move-right!} copies elements in rightmost order.\n" | |
416 | "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n" | |
417 | "same vector, @code{vector-move-right!} is usually appropriate when\n" | |
418 | "@var{start1} is less than @var{start2}.") | |
1bbd0b84 | 419 | #define FUNC_NAME s_scm_vector_move_right_x |
0f2d19dd | 420 | { |
de5c0f58 | 421 | size_t len1, len2; |
a55c2b68 | 422 | size_t i, j, e; |
0f2d19dd | 423 | |
de5c0f58 MV |
424 | len1 = scm_c_vector_length (vec1); |
425 | len2 = scm_c_vector_length (vec2); | |
426 | i = scm_to_unsigned_integer (start1, 0, len1); | |
427 | e = scm_to_unsigned_integer (end1, i, len1); | |
428 | j = scm_to_unsigned_integer (start2, 0, len2 - (i-e)); | |
429 | ||
430 | /* Optimize common case of two regular vectors. | |
431 | */ | |
a55c2b68 | 432 | j += e - i; |
de5c0f58 MV |
433 | if (SCM_VECTORP (vec1) && SCM_VECTORP (vec2)) |
434 | { | |
1d0df896 MV |
435 | const SCM *elts1 = scm_vector_elements (vec1); |
436 | SCM *elts2 = scm_vector_writable_elements (vec2); | |
de5c0f58 MV |
437 | while (i < e) |
438 | { | |
439 | e--, j--; | |
1d0df896 | 440 | elts2[j] = elts1[e]; |
de5c0f58 | 441 | } |
1d0df896 MV |
442 | scm_vector_release_elements (vec1); |
443 | scm_vector_release_writable_elements (vec2); | |
de5c0f58 MV |
444 | } |
445 | else | |
1d1559ce | 446 | { |
de5c0f58 MV |
447 | while (i < e) |
448 | { | |
449 | e--, j--; | |
450 | scm_c_vector_set_x (vec2, j, scm_c_vector_ref (vec1, e)); | |
451 | } | |
1d1559ce HWN |
452 | } |
453 | ||
0f2d19dd JB |
454 | return SCM_UNSPECIFIED; |
455 | } | |
1bbd0b84 | 456 | #undef FUNC_NAME |
0f2d19dd JB |
457 | |
458 | ||
88797580 MV |
459 | /* Generalized vectors. */ |
460 | ||
461 | int | |
462 | scm_is_generalized_vector (SCM obj) | |
463 | { | |
464 | return (scm_is_vector (obj) | |
465 | || scm_is_string (obj) | |
466 | || scm_is_bitvector (obj) | |
467 | || scm_is_uniform_vector (obj)); | |
468 | } | |
469 | ||
470 | SCM_DEFINE (scm_generalized_vector_p, "generalized-vector?", 1, 0, 0, | |
471 | (SCM obj), | |
472 | "Return @code{#t} if @var{obj} is a vector, string,\n" | |
473 | "bitvector, or uniform numeric vector.") | |
474 | #define FUNC_NAME s_scm_generalized_vector_p | |
475 | { | |
476 | return scm_from_bool (scm_is_generalized_vector (obj)); | |
477 | } | |
478 | #undef FUNC_NAME | |
479 | ||
480 | size_t | |
481 | scm_c_generalized_vector_length (SCM v) | |
482 | { | |
483 | if (scm_is_vector (v)) | |
484 | return scm_c_vector_length (v); | |
485 | else if (scm_is_string (v)) | |
486 | return scm_c_string_length (v); | |
487 | else if (scm_is_bitvector (v)) | |
488 | return scm_c_bitvector_length (v); | |
489 | else if (scm_is_uniform_vector (v)) | |
490 | return scm_c_uniform_vector_length (v); | |
491 | else | |
492 | scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector"); | |
493 | } | |
494 | ||
495 | SCM_DEFINE (scm_generalized_vector_length, "generalized-vector-length", 1, 0, 0, | |
496 | (SCM v), | |
497 | "Return the length of the generalized vector @var{v}.") | |
498 | #define FUNC_NAME s_scm_generalized_vector_length | |
499 | { | |
500 | return scm_from_size_t (scm_c_generalized_vector_length (v)); | |
501 | } | |
502 | #undef FUNC_NAME | |
503 | ||
504 | SCM | |
505 | scm_c_generalized_vector_ref (SCM v, size_t idx) | |
506 | { | |
507 | if (scm_is_vector (v)) | |
508 | return scm_c_vector_ref (v, idx); | |
509 | else if (scm_is_string (v)) | |
510 | return scm_c_string_ref (v, idx); | |
511 | else if (scm_is_bitvector (v)) | |
512 | return scm_c_bitvector_ref (v, idx); | |
513 | else if (scm_is_uniform_vector (v)) | |
514 | return scm_c_uniform_vector_ref (v, idx); | |
515 | else | |
516 | scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector"); | |
517 | } | |
518 | ||
519 | SCM_DEFINE (scm_generalized_vector_ref, "generalized-vector-ref", 2, 0, 0, | |
520 | (SCM v, SCM idx), | |
521 | "Return the element at index @var{idx} of the\n" | |
522 | "generalized vector @var{v}.") | |
523 | #define FUNC_NAME s_scm_generalized_vector_ref | |
524 | { | |
525 | return scm_c_generalized_vector_ref (v, scm_to_size_t (idx)); | |
526 | } | |
527 | #undef FUNC_NAME | |
528 | ||
529 | void | |
530 | scm_c_generalized_vector_set_x (SCM v, size_t idx, SCM val) | |
531 | { | |
532 | if (scm_is_vector (v)) | |
533 | scm_c_vector_set_x (v, idx, val); | |
534 | else if (scm_is_string (v)) | |
535 | scm_c_string_set_x (v, idx, val); | |
536 | else if (scm_is_bitvector (v)) | |
537 | scm_c_bitvector_set_x (v, idx, val); | |
538 | else if (scm_is_uniform_vector (v)) | |
539 | scm_c_uniform_vector_set_x (v, idx, val); | |
540 | else | |
541 | scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector"); | |
542 | } | |
543 | ||
544 | SCM_DEFINE (scm_generalized_vector_set_x, "generalized-vector-set!", 3, 0, 0, | |
545 | (SCM v, SCM idx, SCM val), | |
546 | "Set the element at index @var{idx} of the\n" | |
547 | "generalized vector @var{v} to @var{val}.") | |
548 | #define FUNC_NAME s_scm_generalized_vector_set_x | |
549 | { | |
550 | scm_c_generalized_vector_set_x (v, scm_to_size_t (idx), val); | |
551 | return SCM_UNSPECIFIED; | |
552 | } | |
553 | #undef FUNC_NAME | |
554 | ||
555 | SCM_DEFINE (scm_generalized_vector_to_list, "generalized-vector->list", 1, 0, 0, | |
556 | (SCM v), | |
557 | "Return a new list whose elements are the elements of the\n" | |
558 | "generalized vector @var{v}.") | |
559 | #define FUNC_NAME s_scm_generalized_vector_to_list | |
560 | { | |
561 | if (scm_is_vector (v)) | |
562 | return scm_vector_to_list (v); | |
563 | else if (scm_is_string (v)) | |
564 | return scm_string_to_list (v); | |
565 | else if (scm_is_bitvector (v)) | |
566 | return scm_bitvector_to_list (v); | |
567 | else if (scm_is_uniform_vector (v)) | |
568 | return scm_uniform_vector_to_list (v); | |
569 | else | |
570 | scm_wrong_type_arg_msg (NULL, 0, v, "generalized vector"); | |
571 | } | |
572 | #undef FUNC_NAME | |
573 | ||
1cc91f1b | 574 | |
0f2d19dd JB |
575 | void |
576 | scm_init_vectors () | |
0f2d19dd | 577 | { |
7c33806a DH |
578 | scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED); |
579 | ||
a0599745 | 580 | #include "libguile/vectors.x" |
0f2d19dd JB |
581 | } |
582 | ||
89e00824 ML |
583 | |
584 | /* | |
585 | Local Variables: | |
586 | c-file-style: "gnu" | |
587 | End: | |
588 | */ |