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