Commit | Line | Data |
---|---|---|
22a52da1 | 1 | /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 | 41 | |
1bbd0b84 | 42 | |
0f2d19dd JB |
43 | \f |
44 | ||
a0599745 MD |
45 | #include "libguile/_scm.h" |
46 | #include "libguile/eq.h" | |
47 | #include "libguile/root.h" | |
48 | #include "libguile/strings.h" | |
c96d76b8 | 49 | #include "libguile/lang.h" |
a0599745 MD |
50 | |
51 | #include "libguile/validate.h" | |
52 | #include "libguile/vectors.h" | |
53 | #include "libguile/unif.h" | |
0f2d19dd JB |
54 | \f |
55 | ||
a1ec6916 | 56 | SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0, |
5ffe9968 | 57 | (SCM obj), |
1e6808ea MG |
58 | "Return @code{#t} if @var{obj} is a vector, otherwise return\n" |
59 | "@code{#f}.") | |
1bbd0b84 | 60 | #define FUNC_NAME s_scm_vector_p |
0f2d19dd | 61 | { |
5ffe9968 | 62 | return SCM_BOOL (SCM_VECTORP (obj)); |
0f2d19dd | 63 | } |
1bbd0b84 | 64 | #undef FUNC_NAME |
0f2d19dd | 65 | |
f172c0b7 | 66 | SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length); |
1e6808ea | 67 | /* Returns the number of elements in @var{vector} as an exact integer. */ |
0f2d19dd | 68 | SCM |
f172c0b7 | 69 | scm_vector_length (SCM v) |
0f2d19dd | 70 | { |
f172c0b7 MD |
71 | SCM_GASSERT1 (SCM_VECTORP(v), |
72 | g_vector_length, v, SCM_ARG1, s_vector_length); | |
b5c2579a | 73 | return SCM_MAKINUM (SCM_VECTOR_LENGTH (v)); |
0f2d19dd JB |
74 | } |
75 | ||
f172c0b7 | 76 | SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector); |
5ffe9968 | 77 | /* |
942e5b91 MG |
78 | "Return a newly created vector initialized to the elements of" |
79 | "the list @var{list}.\n\n" | |
80 | "@lisp\n" | |
81 | "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n" | |
82 | "(list->vector '(dididit dah)) @result{} #(dididit dah)\n" | |
83 | "@end lisp") | |
5ffe9968 | 84 | */ |
a1ec6916 | 85 | SCM_DEFINE (scm_vector, "vector", 0, 0, 1, |
f172c0b7 | 86 | (SCM l), |
8f85c0c6 NJ |
87 | "@deffnx {Scheme Procedure} list->vector l\n" |
88 | "Return a newly allocated vector composed of the\n" | |
1e6808ea MG |
89 | "given arguments. Analogous to @code{list}.\n" |
90 | "\n" | |
942e5b91 | 91 | "@lisp\n" |
1e6808ea | 92 | "(vector 'a 'b 'c) @result{} #(a b c)\n" |
942e5b91 | 93 | "@end lisp") |
1bbd0b84 | 94 | #define FUNC_NAME s_scm_vector |
0f2d19dd JB |
95 | { |
96 | SCM res; | |
22a52da1 | 97 | SCM *data; |
c014a02e | 98 | long i; |
22a52da1 DH |
99 | |
100 | /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted | |
101 | while the vector is being created. */ | |
102 | SCM_VALIDATE_LIST_COPYLEN (1, l, i); | |
00ffa0e7 | 103 | res = scm_c_make_vector (i, SCM_UNSPECIFIED); |
f172c0b7 | 104 | data = SCM_VELTS (res); |
c96d76b8 | 105 | while (!SCM_NULL_OR_NIL_P (l)) |
22a52da1 DH |
106 | { |
107 | *data++ = SCM_CAR (l); | |
108 | l = SCM_CDR (l); | |
109 | } | |
110 | ||
0f2d19dd JB |
111 | return res; |
112 | } | |
1bbd0b84 | 113 | #undef FUNC_NAME |
0f2d19dd | 114 | |
f172c0b7 | 115 | SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref); |
1cc91f1b | 116 | |
5ffe9968 GB |
117 | /* |
118 | "@var{k} must be a valid index of @var{vector}.\n" | |
119 | "@samp{Vector-ref} returns the contents of element @var{k} of\n" | |
120 | "@var{vector}.\n\n" | |
942e5b91 MG |
121 | "@lisp\n" |
122 | "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n" | |
5ffe9968 GB |
123 | "(vector-ref '#(1 1 2 3 5 8 13 21)\n" |
124 | " (let ((i (round (* 2 (acos -1)))))\n" | |
125 | " (if (inexact? i)\n" | |
126 | " (inexact->exact i)\n" | |
942e5b91 MG |
127 | " i))) @result{} 13\n" |
128 | "@end lisp" | |
5ffe9968 GB |
129 | */ |
130 | ||
0f2d19dd | 131 | SCM |
ea633082 | 132 | scm_vector_ref (SCM v, SCM k) |
685c0d71 | 133 | #define FUNC_NAME s_vector_ref |
0f2d19dd | 134 | { |
0c95b57d | 135 | SCM_GASSERT2 (SCM_VECTORP (v), |
9eb8500a MD |
136 | g_vector_ref, v, k, SCM_ARG1, s_vector_ref); |
137 | SCM_GASSERT2 (SCM_INUMP (k), | |
138 | g_vector_ref, v, k, SCM_ARG2, s_vector_ref); | |
b5c2579a | 139 | SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0); |
c014a02e | 140 | return SCM_VELTS (v)[(long) SCM_INUM (k)]; |
0f2d19dd | 141 | } |
685c0d71 | 142 | #undef FUNC_NAME |
0f2d19dd | 143 | |
f172c0b7 | 144 | SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x); |
1cc91f1b | 145 | |
942e5b91 MG |
146 | /* "@var{k} must be a valid index of @var{vector}.\n" |
147 | "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n" | |
148 | "The value returned by @samp{vector-set!} is unspecified.\n" | |
149 | "@lisp\n" | |
150 | "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n" | |
151 | " (vector-set! vec 1 '("Sue" "Sue"))\n" | |
152 | " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n" | |
153 | "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n" | |
154 | "@end lisp" | |
5ffe9968 GB |
155 | */ |
156 | ||
0f2d19dd | 157 | SCM |
f172c0b7 | 158 | scm_vector_set_x (SCM v, SCM k, SCM obj) |
685c0d71 | 159 | #define FUNC_NAME s_vector_set_x |
0f2d19dd | 160 | { |
f172c0b7 | 161 | SCM_GASSERTn (SCM_VECTORP (v), |
1afff620 | 162 | g_vector_set_x, scm_list_3 (v, k, obj), |
9eb8500a | 163 | SCM_ARG1, s_vector_set_x); |
f172c0b7 | 164 | SCM_GASSERTn (SCM_INUMP (k), |
1afff620 | 165 | g_vector_set_x, scm_list_3 (v, k, obj), |
9eb8500a | 166 | SCM_ARG2, s_vector_set_x); |
b5c2579a | 167 | SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0); |
c014a02e | 168 | SCM_VELTS(v)[(long) SCM_INUM(k)] = obj; |
60c497a3 | 169 | return SCM_UNSPECIFIED; |
0f2d19dd | 170 | } |
685c0d71 | 171 | #undef FUNC_NAME |
0f2d19dd JB |
172 | |
173 | ||
a1ec6916 | 174 | SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0, |
1bbd0b84 | 175 | (SCM k, SCM fill), |
1e6808ea | 176 | "Return a newly allocated vector of @var{k} elements. If a\n" |
8f85c0c6 NJ |
177 | "second argument is given, then each position is initialized to\n" |
178 | "@var{fill}. Otherwise the initial contents of each position is\n" | |
1e6808ea | 179 | "unspecified.") |
1bbd0b84 | 180 | #define FUNC_NAME s_scm_make_vector |
0f2d19dd | 181 | { |
1b9be268 | 182 | if (SCM_UNBNDP (fill)) |
d60cebe2 | 183 | fill = SCM_UNSPECIFIED; |
e382fdbe DH |
184 | |
185 | if (SCM_INUMP (k)) | |
186 | { | |
cb0d8be2 | 187 | SCM_ASSERT_RANGE (1, k, SCM_INUM (k) >= 0); |
e382fdbe DH |
188 | return scm_c_make_vector (SCM_INUM (k), fill); |
189 | } | |
190 | else if (SCM_BIGP (k)) | |
191 | SCM_OUT_OF_RANGE (1, k); | |
192 | else | |
193 | SCM_WRONG_TYPE_ARG (1, k); | |
00ffa0e7 KN |
194 | } |
195 | #undef FUNC_NAME | |
196 | ||
e382fdbe | 197 | |
00ffa0e7 | 198 | SCM |
c014a02e | 199 | scm_c_make_vector (unsigned long int k, SCM fill) |
00ffa0e7 KN |
200 | #define FUNC_NAME s_scm_make_vector |
201 | { | |
202 | SCM v; | |
92c2555f | 203 | scm_t_bits *base; |
1b9be268 | 204 | |
e382fdbe DH |
205 | if (k > 0) |
206 | { | |
c014a02e | 207 | unsigned long int j; |
1b9be268 | 208 | |
c014a02e | 209 | SCM_ASSERT_RANGE (1, scm_ulong2num (k), k <= SCM_VECTOR_MAX_LENGTH); |
1b9be268 | 210 | |
4c9419ac | 211 | base = scm_gc_malloc (k * sizeof (scm_t_bits), "vector"); |
e382fdbe DH |
212 | for (j = 0; j != k; ++j) |
213 | base[j] = SCM_UNPACK (fill); | |
214 | } | |
215 | else | |
216 | base = NULL; | |
1b9be268 | 217 | |
228a24ef | 218 | v = scm_cell (SCM_MAKE_VECTOR_TAG (k, scm_tc7_vector), (scm_t_bits) base); |
e382fdbe | 219 | scm_remember_upto_here_1 (fill); |
1b9be268 | 220 | |
0f2d19dd JB |
221 | return v; |
222 | } | |
1bbd0b84 | 223 | #undef FUNC_NAME |
0f2d19dd | 224 | |
e382fdbe | 225 | |
3b3b36dd | 226 | SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0, |
1e6808ea | 227 | (SCM v), |
8f85c0c6 | 228 | "Return a newly allocated list composed of the elements of @var{v}.\n" |
1e6808ea | 229 | "\n" |
942e5b91 MG |
230 | "@lisp\n" |
231 | "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n" | |
232 | "(list->vector '(dididit dah)) @result{} #(dididit dah)\n" | |
233 | "@end lisp") | |
1bbd0b84 | 234 | #define FUNC_NAME s_scm_vector_to_list |
0f2d19dd JB |
235 | { |
236 | SCM res = SCM_EOL; | |
c014a02e | 237 | long i; |
0f2d19dd | 238 | SCM *data; |
3b3b36dd | 239 | SCM_VALIDATE_VECTOR (1,v); |
0f2d19dd | 240 | data = SCM_VELTS(v); |
b5c2579a | 241 | for(i = SCM_VECTOR_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res); |
0f2d19dd JB |
242 | return res; |
243 | } | |
1bbd0b84 | 244 | #undef FUNC_NAME |
0f2d19dd JB |
245 | |
246 | ||
a1ec6916 | 247 | SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0, |
1e6808ea | 248 | (SCM v, SCM fill), |
8f85c0c6 | 249 | "Store @var{fill} in every position of @var{vector}. The value\n" |
1e6808ea | 250 | "returned by @code{vector-fill!} is unspecified.") |
1bbd0b84 | 251 | #define FUNC_NAME s_scm_vector_fill_x |
0f2d19dd | 252 | { |
c014a02e | 253 | register long i; |
0f2d19dd | 254 | register SCM *data; |
1be6b49c ML |
255 | SCM_VALIDATE_VECTOR (1, v); |
256 | data = SCM_VELTS (v); | |
257 | for(i = SCM_VECTOR_LENGTH (v) - 1; i >= 0; i--) | |
1e6808ea | 258 | data[i] = fill; |
0f2d19dd JB |
259 | return SCM_UNSPECIFIED; |
260 | } | |
1bbd0b84 | 261 | #undef FUNC_NAME |
0f2d19dd JB |
262 | |
263 | ||
0f2d19dd | 264 | SCM |
1bbd0b84 | 265 | scm_vector_equal_p(SCM x, SCM y) |
0f2d19dd | 266 | { |
c014a02e | 267 | long i; |
1be6b49c ML |
268 | for(i = SCM_VECTOR_LENGTH (x) - 1; i >= 0; i--) |
269 | if (SCM_FALSEP (scm_equal_p (SCM_VELTS (x)[i], SCM_VELTS (y)[i]))) | |
0f2d19dd JB |
270 | return SCM_BOOL_F; |
271 | return SCM_BOOL_T; | |
272 | } | |
273 | ||
274 | ||
a1ec6916 | 275 | SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0, |
1bbd0b84 | 276 | (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2), |
694a9bb3 NJ |
277 | "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n" |
278 | "to @var{vec2} starting at position @var{start2}. @var{start1} and\n" | |
279 | "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n" | |
280 | "@code{vector-move-left!} copies elements in leftmost order.\n" | |
281 | "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n" | |
282 | "same vector, @code{vector-move-left!} is usually appropriate when\n" | |
283 | "@var{start1} is greater than @var{start2}.") | |
1bbd0b84 | 284 | #define FUNC_NAME s_scm_vector_move_left_x |
0f2d19dd | 285 | { |
c014a02e ML |
286 | long i; |
287 | long j; | |
288 | long e; | |
0f2d19dd | 289 | |
3b3b36dd GB |
290 | SCM_VALIDATE_VECTOR (1,vec1); |
291 | SCM_VALIDATE_INUM_COPY (2,start1,i); | |
292 | SCM_VALIDATE_INUM_COPY (3,end1,e); | |
293 | SCM_VALIDATE_VECTOR (4,vec2); | |
294 | SCM_VALIDATE_INUM_COPY (5,start2,j); | |
b5c2579a DH |
295 | SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0); |
296 | SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0); | |
297 | SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0); | |
298 | SCM_ASSERT_RANGE (5, start2, e-i+j <= SCM_VECTOR_LENGTH (vec2)); | |
0f2d19dd JB |
299 | while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++]; |
300 | return SCM_UNSPECIFIED; | |
301 | } | |
1bbd0b84 | 302 | #undef FUNC_NAME |
0f2d19dd | 303 | |
a1ec6916 | 304 | SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0, |
1bbd0b84 | 305 | (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2), |
694a9bb3 NJ |
306 | "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n" |
307 | "to @var{vec2} starting at position @var{start2}. @var{start1} and\n" | |
308 | "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n" | |
309 | "@code{vector-move-right!} copies elements in rightmost order.\n" | |
310 | "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n" | |
311 | "same vector, @code{vector-move-right!} is usually appropriate when\n" | |
312 | "@var{start1} is less than @var{start2}.") | |
1bbd0b84 | 313 | #define FUNC_NAME s_scm_vector_move_right_x |
0f2d19dd | 314 | { |
c014a02e ML |
315 | long i; |
316 | long j; | |
317 | long e; | |
0f2d19dd | 318 | |
3b3b36dd GB |
319 | SCM_VALIDATE_VECTOR (1,vec1); |
320 | SCM_VALIDATE_INUM_COPY (2,start1,i); | |
321 | SCM_VALIDATE_INUM_COPY (3,end1,e); | |
322 | SCM_VALIDATE_VECTOR (4,vec2); | |
323 | SCM_VALIDATE_INUM_COPY (5,start2,j); | |
b5c2579a DH |
324 | SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0); |
325 | SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0); | |
326 | SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0); | |
2cc41672 | 327 | j = e - i + j; |
b5c2579a | 328 | SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2)); |
2cc41672 MD |
329 | while (i < e) |
330 | SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e]; | |
0f2d19dd JB |
331 | return SCM_UNSPECIFIED; |
332 | } | |
1bbd0b84 | 333 | #undef FUNC_NAME |
0f2d19dd JB |
334 | |
335 | ||
1cc91f1b | 336 | |
0f2d19dd JB |
337 | void |
338 | scm_init_vectors () | |
0f2d19dd | 339 | { |
7c33806a DH |
340 | scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED); |
341 | ||
a0599745 | 342 | #include "libguile/vectors.x" |
0f2d19dd JB |
343 | } |
344 | ||
89e00824 ML |
345 | |
346 | /* | |
347 | Local Variables: | |
348 | c-file-style: "gnu" | |
349 | End: | |
350 | */ |