* deprecated.h, deprecated.c, numbers.h (SCM_INUMP, SCM_NINUMP,
[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_from_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_from_size_t (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_I_INUMP (k),
118 g_vector_ref, v, k, SCM_ARG2, s_vector_ref);
119 SCM_ASSERT_RANGE (2, k,
120 SCM_I_INUM (k) < SCM_VECTOR_LENGTH (v)
121 && SCM_I_INUM (k) >= 0);
122 return SCM_VELTS (v)[(long) SCM_I_INUM (k)];
123 }
124 #undef FUNC_NAME
125
126 SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x);
127
128 /* "@var{k} must be a valid index of @var{vector}.\n"
129 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
130 "The value returned by @samp{vector-set!} is unspecified.\n"
131 "@lisp\n"
132 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
133 " (vector-set! vec 1 '("Sue" "Sue"))\n"
134 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
135 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
136 "@end lisp"
137 */
138
139 SCM
140 scm_vector_set_x (SCM v, SCM k, SCM obj)
141 #define FUNC_NAME s_vector_set_x
142 {
143 SCM_GASSERTn (SCM_VECTORP (v),
144 g_vector_set_x, scm_list_3 (v, k, obj),
145 SCM_ARG1, s_vector_set_x);
146 SCM_GASSERTn (SCM_I_INUMP (k),
147 g_vector_set_x, scm_list_3 (v, k, obj),
148 SCM_ARG2, s_vector_set_x);
149 SCM_ASSERT_RANGE (2, k,
150 SCM_I_INUM (k) < SCM_VECTOR_LENGTH (v)
151 && SCM_I_INUM (k) >= 0);
152 SCM_VECTOR_SET (v, (long) SCM_I_INUM(k), obj);
153 return SCM_UNSPECIFIED;
154 }
155 #undef FUNC_NAME
156
157
158 SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0,
159 (SCM k, SCM fill),
160 "Return a newly allocated vector of @var{k} elements. If a\n"
161 "second argument is given, then each position is initialized to\n"
162 "@var{fill}. Otherwise the initial contents of each position is\n"
163 "unspecified.")
164 #define FUNC_NAME s_scm_make_vector
165 {
166 size_t l = scm_to_unsigned_integer (k, 0, SCM_VECTOR_MAX_LENGTH);
167
168 if (SCM_UNBNDP (fill))
169 fill = SCM_UNSPECIFIED;
170
171 return scm_c_make_vector (l, fill);
172 }
173 #undef FUNC_NAME
174
175
176 SCM
177 scm_c_make_vector (unsigned long int k, SCM fill)
178 #define FUNC_NAME s_scm_make_vector
179 {
180 SCM v;
181 scm_t_bits *base;
182
183 if (k > 0)
184 {
185 unsigned long int j;
186
187 SCM_ASSERT_RANGE (1, scm_ulong2num (k), k <= SCM_VECTOR_MAX_LENGTH);
188
189 base = scm_gc_malloc (k * sizeof (scm_t_bits), "vector");
190 for (j = 0; j != k; ++j)
191 base[j] = SCM_UNPACK (fill);
192 }
193 else
194 base = NULL;
195
196 v = scm_cell (SCM_MAKE_VECTOR_TAG (k, scm_tc7_vector), (scm_t_bits) base);
197 scm_remember_upto_here_1 (fill);
198
199 return v;
200 }
201 #undef FUNC_NAME
202
203
204 SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0,
205 (SCM v),
206 "Return a newly allocated list composed of the elements of @var{v}.\n"
207 "\n"
208 "@lisp\n"
209 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
210 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
211 "@end lisp")
212 #define FUNC_NAME s_scm_vector_to_list
213 {
214 SCM res = SCM_EOL;
215 long i;
216 SCM const *data;
217 SCM_VALIDATE_VECTOR (1, v);
218 data = SCM_VELTS(v);
219 for(i = SCM_VECTOR_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res);
220 return res;
221 }
222 #undef FUNC_NAME
223
224
225 SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0,
226 (SCM v, SCM fill),
227 "Store @var{fill} in every position of @var{vector}. The value\n"
228 "returned by @code{vector-fill!} is unspecified.")
229 #define FUNC_NAME s_scm_vector_fill_x
230 {
231 register long i;
232 SCM_VALIDATE_VECTOR (1, v);
233
234 for(i = SCM_VECTOR_LENGTH (v) - 1; i >= 0; i--)
235 SCM_VECTOR_SET(v, i, fill);
236 return SCM_UNSPECIFIED;
237 }
238 #undef FUNC_NAME
239
240
241 SCM
242 scm_vector_equal_p(SCM x, SCM y)
243 {
244 long i;
245 for(i = SCM_VECTOR_LENGTH (x) - 1; i >= 0; i--)
246 if (scm_is_false (scm_equal_p (SCM_VELTS (x)[i], SCM_VELTS (y)[i])))
247 return SCM_BOOL_F;
248 return SCM_BOOL_T;
249 }
250
251
252 SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0,
253 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
254 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
255 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
256 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
257 "@code{vector-move-left!} copies elements in leftmost order.\n"
258 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
259 "same vector, @code{vector-move-left!} is usually appropriate when\n"
260 "@var{start1} is greater than @var{start2}.")
261 #define FUNC_NAME s_scm_vector_move_left_x
262 {
263 size_t i, j, e;
264
265 SCM_VALIDATE_VECTOR (1, vec1);
266 SCM_VALIDATE_VECTOR (4, vec2);
267 i = scm_to_unsigned_integer (start1, 0, SCM_VECTOR_LENGTH(vec1));
268 e = scm_to_unsigned_integer (end1, i, SCM_VECTOR_LENGTH(vec1));
269 j = scm_to_unsigned_integer (start2, 0, SCM_VECTOR_LENGTH(vec2)-(i-e));
270
271 while (i<e)
272 {
273 SCM_VECTOR_SET (vec2, j, SCM_VELTS (vec1)[i]);
274 i++;
275 j++;
276 }
277
278 return SCM_UNSPECIFIED;
279 }
280 #undef FUNC_NAME
281
282 SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0,
283 (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2),
284 "Copy elements from @var{vec1}, positions @var{start1} to @var{end1},\n"
285 "to @var{vec2} starting at position @var{start2}. @var{start1} and\n"
286 "@var{start2} are inclusive indices; @var{end1} is exclusive.\n\n"
287 "@code{vector-move-right!} copies elements in rightmost order.\n"
288 "Therefore, in the case where @var{vec1} and @var{vec2} refer to the\n"
289 "same vector, @code{vector-move-right!} is usually appropriate when\n"
290 "@var{start1} is less than @var{start2}.")
291 #define FUNC_NAME s_scm_vector_move_right_x
292 {
293 size_t i, j, e;
294
295 SCM_VALIDATE_VECTOR (1, vec1);
296 SCM_VALIDATE_VECTOR (4, vec2);
297 i = scm_to_unsigned_integer (start1, 0, SCM_VECTOR_LENGTH(vec1));
298 e = scm_to_unsigned_integer (end1, i, SCM_VECTOR_LENGTH(vec1));
299 j = scm_to_unsigned_integer (start2, 0, SCM_VECTOR_LENGTH(vec2)-(i-e));
300
301 j += e - i;
302 while (i < e)
303 {
304 j--;
305 e--;
306 SCM_VECTOR_SET (vec2, j, SCM_VELTS (vec1)[e]);
307 }
308
309 return SCM_UNSPECIFIED;
310 }
311 #undef FUNC_NAME
312
313
314
315 void
316 scm_init_vectors ()
317 {
318 scm_nullvect = scm_c_make_vector (0, SCM_UNDEFINED);
319
320 #include "libguile/vectors.x"
321 }
322
323
324 /*
325 Local Variables:
326 c-file-style: "gnu"
327 End:
328 */