Changed license terms to the plain LGPL thru-out.
[bpt/guile.git] / libguile / vectors.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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 */
17
18
19 \f
20
21 #include "libguile/_scm.h"
22 #include "libguile/eq.h"
23 #include "libguile/root.h"
24 #include "libguile/strings.h"
25 #include "libguile/lang.h"
26
27 #include "libguile/validate.h"
28 #include "libguile/vectors.h"
29 #include "libguile/unif.h"
30 \f
31
32 SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0,
33 (SCM obj),
34 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
35 "@code{#f}.")
36 #define FUNC_NAME s_scm_vector_p
37 {
38 return SCM_BOOL (SCM_VECTORP (obj));
39 }
40 #undef FUNC_NAME
41
42 SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length);
43 /* Returns the number of elements in @var{vector} as an exact integer. */
44 SCM
45 scm_vector_length (SCM v)
46 {
47 SCM_GASSERT1 (SCM_VECTORP(v),
48 g_vector_length, v, SCM_ARG1, s_vector_length);
49 return SCM_MAKINUM (SCM_VECTOR_LENGTH (v));
50 }
51
52 SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector);
53 /*
54 "Return a newly created vector initialized to the elements of"
55 "the list @var{list}.\n\n"
56 "@lisp\n"
57 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
58 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
59 "@end lisp")
60 */
61 SCM_DEFINE (scm_vector, "vector", 0, 0, 1,
62 (SCM l),
63 "@deffnx {Scheme Procedure} list->vector l\n"
64 "Return a newly allocated vector composed of the\n"
65 "given arguments. Analogous to @code{list}.\n"
66 "\n"
67 "@lisp\n"
68 "(vector 'a 'b 'c) @result{} #(a b c)\n"
69 "@end lisp")
70 #define FUNC_NAME s_scm_vector
71 {
72 SCM res;
73 SCM *data;
74 long i;
75
76 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
77 while the vector is being created. */
78 SCM_VALIDATE_LIST_COPYLEN (1, l, i);
79 res = scm_c_make_vector (i, SCM_UNSPECIFIED);
80
81 /*
82 this code doesn't alloc. -- accessing RES is safe.
83 */
84 data = SCM_WRITABLE_VELTS (res);
85 while (!SCM_NULL_OR_NIL_P (l))
86 {
87 *data++ = SCM_CAR (l);
88 l = SCM_CDR (l);
89 }
90
91 return res;
92 }
93 #undef FUNC_NAME
94
95 SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref);
96
97 /*
98 "@var{k} must be a valid index of @var{vector}.\n"
99 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
100 "@var{vector}.\n\n"
101 "@lisp\n"
102 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
103 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
104 " (let ((i (round (* 2 (acos -1)))))\n"
105 " (if (inexact? i)\n"
106 " (inexact->exact i)\n"
107 " i))) @result{} 13\n"
108 "@end lisp"
109 */
110
111 SCM
112 scm_vector_ref (SCM v, SCM k)
113 #define FUNC_NAME s_vector_ref
114 {
115 SCM_GASSERT2 (SCM_VECTORP (v),
116 g_vector_ref, v, k, SCM_ARG1, s_vector_ref);
117 SCM_GASSERT2 (SCM_INUMP (k),
118 g_vector_ref, v, k, SCM_ARG2, s_vector_ref);
119 SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0);
120 return SCM_VELTS (v)[(long) SCM_INUM (k)];
121 }
122 #undef FUNC_NAME
123
124 SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
125
126 /* "@var{k} must be a valid index of @var{vector}.\n"
127 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
128 "The value returned by @samp{vector-set!} is unspecified.\n"
129 "@lisp\n"
130 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
131 " (vector-set! vec 1 '("Sue" "Sue"))\n"
132 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
133 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
134 "@end lisp"
135 */
136
137 SCM
138 scm_vector_set_x (SCM v, SCM k, SCM obj)
139 #define FUNC_NAME s_vector_set_x
140 {
141 SCM_GASSERTn (SCM_VECTORP (v),
142 g_vector_set_x, scm_list_3 (v, k, obj),
143 SCM_ARG1, s_vector_set_x);
144 SCM_GASSERTn (SCM_INUMP (k),
145 g_vector_set_x, scm_list_3 (v, k, obj),
146 SCM_ARG2, s_vector_set_x);
147 SCM_ASSERT_RANGE (2, k, SCM_INUM (k) < SCM_VECTOR_LENGTH (v) && SCM_INUM (k) >= 0);
148 SCM_VECTOR_SET (v, (long) SCM_INUM(k), obj);
149 return SCM_UNSPECIFIED;
150 }
151 #undef FUNC_NAME
152
153
154 SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
155 (SCM k, SCM fill),
156 "Return a newly allocated vector of @var{k} elements. If a\n"
157 "second argument is given, then each position is initialized to\n"
158 "@var{fill}. Otherwise the initial contents of each position is\n"
159 "unspecified.")
160 #define FUNC_NAME s_scm_make_vector
161 {
162 if (SCM_UNBNDP (fill))
163 fill = SCM_UNSPECIFIED;
164
165 if (SCM_INUMP (k))
166 {
167 SCM_ASSERT_RANGE (1, k, SCM_INUM (k) >= 0);
168 return scm_c_make_vector (SCM_INUM (k), fill);
169 }
170 else if (SCM_BIGP (k))
171 SCM_OUT_OF_RANGE (1, k);
172 else
173 SCM_WRONG_TYPE_ARG (1, k);
174 }
175 #undef FUNC_NAME
176
177
178 SCM
179 scm_c_make_vector (unsigned long int k, SCM fill)
180 #define FUNC_NAME s_scm_make_vector
181 {
182 SCM v;
183 scm_t_bits *base;
184
185 if (k > 0)
186 {
187 unsigned long int j;
188
189 SCM_ASSERT_RANGE (1, scm_ulong2num (k), k <= SCM_VECTOR_MAX_LENGTH);
190
191 base = scm_gc_malloc (k * sizeof (scm_t_bits), "vector");
192 for (j = 0; j != k; ++j)
193 base[j] = SCM_UNPACK (fill);
194 }
195 else
196 base = NULL;
197
198 v = scm_cell (SCM_MAKE_VECTOR_TAG (k, scm_tc7_vector), (scm_t_bits) base);
199 scm_remember_upto_here_1 (fill);
200
201 return v;
202 }
203 #undef FUNC_NAME
204
205
206 SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
207 (SCM v),
208 "Return a newly allocated list composed of the elements of @var{v}.\n"
209 "\n"
210 "@lisp\n"
211 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
212 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
213 "@end lisp")
214 #define FUNC_NAME s_scm_vector_to_list
215 {
216 SCM res = SCM_EOL;
217 long i;
218 SCM const *data;
219 SCM_VALIDATE_VECTOR (1, v);
220 data = SCM_VELTS(v);
221 for(i = SCM_VECTOR_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
222 return res;
223 }
224 #undef FUNC_NAME
225
226
227 SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
228 (SCM v, SCM fill),
229 "Store @var{fill} in every position of @var{vector}. The value\n"
230 "returned by @code{vector-fill!} is unspecified.")
231 #define FUNC_NAME s_scm_vector_fill_x
232 {
233 register long i;
234 SCM_VALIDATE_VECTOR (1, v);
235
236 for(i = SCM_VECTOR_LENGTH (v) - 1; i >= 0; i--)
237 SCM_VECTOR_SET(v, i, fill);
238 return SCM_UNSPECIFIED;
239 }
240 #undef FUNC_NAME
241
242
243 SCM
244 scm_vector_equal_p(SCM x, SCM y)
245 {
246 long i;
247 for(i = SCM_VECTOR_LENGTH (x) - 1; i >= 0; i--)
248 if (SCM_FALSEP (scm_equal_p (SCM_VELTS (x)[i], SCM_VELTS (y)[i])))
249 return SCM_BOOL_F;
250 return SCM_BOOL_T;
251 }
252
253
254 SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
255 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
256 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
257 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
258 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
259 "@code{vector-move-left!} copies elements in leftmost order.\n"
260 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
261 "same vector, @code{vector-move-left!} is usually appropriate when\n"
262 "@var{start1} is greater than @var{start2}.")
263 #define FUNC_NAME s_scm_vector_move_left_x
264 {
265 long i;
266 long j;
267 long e;
268
269 SCM_VALIDATE_VECTOR (1, vec1);
270 SCM_VALIDATE_INUM_COPY (2, start1, i);
271 SCM_VALIDATE_INUM_COPY (3, end1, e);
272 SCM_VALIDATE_VECTOR (4, vec2);
273 SCM_VALIDATE_INUM_COPY (5, start2, j);
274 SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0);
275 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0);
276 SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0);
277 SCM_ASSERT_RANGE (5, start2, e-i+j <= SCM_VECTOR_LENGTH (vec2));
278
279 while (i<e)
280 {
281 SCM_VECTOR_SET (vec2, j, SCM_VELTS (vec1)[i]);
282 i++;
283 j++;
284 }
285
286 return SCM_UNSPECIFIED;
287 }
288 #undef FUNC_NAME
289
290 SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
291 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
292 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
293 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
294 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
295 "@code{vector-move-right!} copies elements in rightmost order.\n"
296 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
297 "same vector, @code{vector-move-right!} is usually appropriate when\n"
298 "@var{start1} is less than @var{start2}.")
299 #define FUNC_NAME s_scm_vector_move_right_x
300 {
301 long i;
302 long j;
303 long e;
304
305 SCM_VALIDATE_VECTOR (1, vec1);
306 SCM_VALIDATE_INUM_COPY (2, start1, i);
307 SCM_VALIDATE_INUM_COPY (3, end1, e);
308 SCM_VALIDATE_VECTOR (4, vec2);
309 SCM_VALIDATE_INUM_COPY (5, start2, j);
310 SCM_ASSERT_RANGE (2, start1, i <= SCM_VECTOR_LENGTH (vec1) && i >= 0);
311 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2) && j >= 0);
312 SCM_ASSERT_RANGE (3, end1, e <= SCM_VECTOR_LENGTH (vec1) && e >= 0);
313 j = e - i + j;
314 SCM_ASSERT_RANGE (5, start2, j <= SCM_VECTOR_LENGTH (vec2));
315 while (i < e)
316 {
317 j--;
318 e--;
319 SCM_VECTOR_SET (vec2, j, SCM_VELTS (vec1)[e]);
320 }
321
322 return SCM_UNSPECIFIED;
323 }
324 #undef FUNC_NAME
325
326
327
328 void
329 scm_init_vectors ()
330 {
331 scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED);
332
333 #include "libguile/vectors.x"
334 }
335
336
337 /*
338 Local Variables:
339 c-file-style: "gnu"
340 End:
341 */