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