1 /* Copyright (C) 1995, 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48 #include "libguile/_scm.h"
49 #include "libguile/eq.h"
50 #include "libguile/root.h"
51 #include "libguile/strings.h"
53 #include "libguile/validate.h"
54 #include "libguile/vectors.h"
55 #include "libguile/unif.h"
58 #if (SCM_DEBUG_DEPRECATED == 0)
60 /* The function scm_vector_set_length_x will disappear in the next release of
65 * This complicates things too much if allowed on any array.
66 * C code can safely call it on arrays known to be used in a single
69 * SCM_REGISTER_PROC(s_vector_set_length_x, "vector-set-length!", 2, 0, 0, scm_vector_set_length_x);
71 static char s_vector_set_length_x
[] = "vector-set-length!";
75 scm_vector_set_length_x (SCM vect
, SCM len
)
83 SCM_ASRTGO (SCM_NIMP (vect
), badarg1
);
86 if (SCM_TYP7 (vect
) == scm_tc7_bvect
)
88 l
= (l
+ SCM_LONG_BIT
- 1) / SCM_LONG_BIT
;
90 sz
= scm_uniform_element_size (vect
);
92 base
= SCM_UVECTOR_BASE (vect
);
95 switch (SCM_TYP7 (vect
))
98 badarg1
: scm_wta (vect
, (char *) SCM_ARG1
, s_vector_set_length_x
);
100 SCM_ASRTGO (!SCM_EQ_P (vect
, scm_nullstr
), badarg1
);
102 base
= SCM_STRING_CHARS (vect
);
107 SCM_ASRTGO (!SCM_EQ_P (vect
, scm_nullvect
), badarg1
);
109 base
= (char *) SCM_VECTOR_BASE (vect
);
112 SCM_ASSERT (SCM_INUMP (len
), len
, SCM_ARG2
, s_vector_set_length_x
);
117 scm_memory_error (s_vector_set_length_x
);
121 scm_must_realloc (base
,
122 (long) SCM_LENGTH (vect
) * sz
,
124 s_vector_set_length_x
)));
125 if (SCM_VECTORP (vect
))
127 sz
= SCM_LENGTH (vect
);
129 SCM_VELTS (vect
)[--l
] = SCM_UNSPECIFIED
;
131 else if (SCM_STRINGP (vect
))
132 SCM_STRING_CHARS (vect
)[l
- 1] = 0;
133 SCM_SETLENGTH (vect
, SCM_INUM (len
), SCM_TYP7 (vect
));
138 #endif /* (SCM_DEBUG_DEPRECATED == 0) */
140 SCM_DEFINE (scm_vector_p
, "vector?", 1, 0, 0,
142 "Returns @t{#t} if @var{obj} is a vector, otherwise returns @t{#f}. (r5rs)")
143 #define FUNC_NAME s_scm_vector_p
147 return SCM_BOOL (SCM_VECTORP (obj
));
151 SCM_GPROC (s_vector_length
, "vector-length", 1, 0, 0, scm_vector_length
, g_vector_length
);
152 /* Returns the number of elements in @var{vector} as an exact integer. (r5rs) */
154 scm_vector_length (SCM v
)
156 SCM_GASSERT1 (SCM_VECTORP(v
),
157 g_vector_length
, v
, SCM_ARG1
, s_vector_length
);
158 return SCM_MAKINUM (SCM_VECTOR_LENGTH (v
));
161 SCM_REGISTER_PROC (s_list_to_vector
, "list->vector", 1, 0, 0, scm_vector
);
163 "@samp{List->vector} returns a newly\n"
164 "created vector initialized to the elements of the list @var{list}.\n\n"
166 "@t{(vector->list '#(dah dah didah))\n"
167 "=> (dah dah didah)\n"
168 "list->vector '(dididit dah))\n"
169 "=> #(dididit dah)\n"
173 SCM_DEFINE (scm_vector
, "vector", 0, 0, 1,
175 "@deffnx primitive list->vector l\n"
176 "Returns a newly allocated vector whose elements contain the given\n"
177 "arguments. Analogous to @samp{list}. (r5rs)\n\n"
179 "@t{(vector 'a 'b 'c) ==> #(a b c) }\n"
181 #define FUNC_NAME s_scm_vector
186 SCM_VALIDATE_LIST_COPYLEN (1,l
,i
);
187 res
= scm_c_make_vector (i
, SCM_UNSPECIFIED
);
188 data
= SCM_VELTS (res
);
189 for(; i
&& SCM_NIMP(l
); --i
, l
= SCM_CDR (l
))
190 *data
++ = SCM_CAR (l
);
195 SCM_GPROC (s_vector_ref
, "vector-ref", 2, 0, 0, scm_vector_ref
, g_vector_ref
);
198 "@var{k} must be a valid index of @var{vector}.\n"
199 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
202 "@t{(vector-ref '#(1 1 2 3 5 8 13 21)\n"
205 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
206 " (let ((i (round (* 2 (acos -1)))))\n"
207 " (if (inexact? i)\n"
208 " (inexact->exact i)\n"
216 scm_vector_ref (SCM v
, SCM k
)
217 #define FUNC_NAME s_vector_ref
219 SCM_GASSERT2 (SCM_VECTORP (v
),
220 g_vector_ref
, v
, k
, SCM_ARG1
, s_vector_ref
);
221 SCM_GASSERT2 (SCM_INUMP (k
),
222 g_vector_ref
, v
, k
, SCM_ARG2
, s_vector_ref
);
223 SCM_ASSERT_RANGE (2, k
, SCM_INUM (k
) < SCM_VECTOR_LENGTH (v
) && SCM_INUM (k
) >= 0);
224 return SCM_VELTS (v
)[(long) SCM_INUM (k
)];
228 SCM_GPROC (s_vector_set_x
, "vector-set!", 3, 0, 0, scm_vector_set_x
, g_vector_set_x
);
231 @var{k} must be a valid index of @var{vector}.
232 @samp{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.
233 The value returned by @samp{vector-set!} is unspecified.
238 @t{(let ((vec (vector 0 '(2 2 2 2) "Anna")))
239 (vector-set! vec 1 '("Sue" "Sue"))
241 ==> #(0 ("Sue" "Sue") "Anna")
243 (vector-set! '#(0 1 2) 1 "doe")
244 ==> @emph{error} ; constant vector
250 scm_vector_set_x (SCM v
, SCM k
, SCM obj
)
251 #define FUNC_NAME s_vector_set_x
253 SCM_GASSERTn (SCM_VECTORP (v
),
254 g_vector_set_x
, SCM_LIST3 (v
, k
, obj
),
255 SCM_ARG1
, s_vector_set_x
);
256 SCM_GASSERTn (SCM_INUMP (k
),
257 g_vector_set_x
, SCM_LIST3 (v
, k
, obj
),
258 SCM_ARG2
, s_vector_set_x
);
259 SCM_ASSERT_RANGE (2, k
, SCM_INUM (k
) < SCM_VECTOR_LENGTH (v
) && SCM_INUM (k
) >= 0);
260 SCM_VELTS(v
)[(long) SCM_INUM(k
)] = obj
;
261 return SCM_UNSPECIFIED
;
266 SCM_DEFINE (scm_make_vector
, "make-vector", 1, 1, 0,
268 "Returns a newly allocated vector of @var{k} elements. If a second\n"
269 "argument is given, then each element is initialized to @var{fill}.\n"
270 "Otherwise the initial contents of each element is unspecified. (r5rs)")
271 #define FUNC_NAME s_scm_make_vector
273 if (SCM_UNBNDP (fill
))
274 fill
= SCM_UNSPECIFIED
;
278 SCM_ASSERT_RANGE (1, k
, SCM_INUM (k
) >= 0);
279 return scm_c_make_vector (SCM_INUM (k
), fill
);
281 else if (SCM_BIGP (k
))
282 SCM_OUT_OF_RANGE (1, k
);
284 SCM_WRONG_TYPE_ARG (1, k
);
290 scm_c_make_vector (unsigned long int k
, SCM fill
)
291 #define FUNC_NAME s_scm_make_vector
300 SCM_ASSERT_RANGE (1, scm_ulong2num (k
), k
<= SCM_VECTOR_MAX_LENGTH
);
302 base
= scm_must_malloc (k
* sizeof (scm_bits_t
), FUNC_NAME
);
303 for (j
= 0; j
!= k
; ++j
)
304 base
[j
] = SCM_UNPACK (fill
);
310 SCM_SET_VECTOR_BASE (v
, base
);
311 SCM_SET_VECTOR_LENGTH (v
, k
, scm_tc7_vector
);
312 scm_remember_upto_here_1 (fill
);
319 SCM_DEFINE (scm_vector_to_list
, "vector->list", 1, 0, 0,
321 "@samp{Vector->list} returns a newly allocated list of the objects contained\n"
322 "in the elements of @var{vector}. (r5rs)\n\n"
324 "@t{(vector->list '#(dah dah didah))\n"
325 "=> (dah dah didah)\n"
326 "list->vector '(dididit dah))\n"
327 "=> #(dididit dah)\n"
330 #define FUNC_NAME s_scm_vector_to_list
335 SCM_VALIDATE_VECTOR (1,v
);
337 for(i
= SCM_VECTOR_LENGTH(v
)-1;i
>= 0;i
--) res
= scm_cons(data
[i
], res
);
343 SCM_DEFINE (scm_vector_fill_x
, "vector-fill!", 2, 0, 0,
345 "Stores @var{fill} in every element of @var{vector}.\n"
346 "The value returned by @samp{vector-fill!} is unspecified. (r5rs)")
347 #define FUNC_NAME s_scm_vector_fill_x
351 SCM_VALIDATE_VECTOR (1,v
);
353 for(i
= SCM_VECTOR_LENGTH(v
) - 1; i
>= 0; i
--)
355 return SCM_UNSPECIFIED
;
361 scm_vector_equal_p(SCM x
, SCM y
)
364 for(i
= SCM_VECTOR_LENGTH(x
)-1;i
>= 0;i
--)
365 if (SCM_FALSEP(scm_equal_p(SCM_VELTS(x
)[i
], SCM_VELTS(y
)[i
])))
371 SCM_DEFINE (scm_vector_move_left_x
, "vector-move-left!", 5, 0, 0,
372 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
373 "Vector version of @code{substring-move-left!}.")
374 #define FUNC_NAME s_scm_vector_move_left_x
380 SCM_VALIDATE_VECTOR (1,vec1
);
381 SCM_VALIDATE_INUM_COPY (2,start1
,i
);
382 SCM_VALIDATE_INUM_COPY (3,end1
,e
);
383 SCM_VALIDATE_VECTOR (4,vec2
);
384 SCM_VALIDATE_INUM_COPY (5,start2
,j
);
385 SCM_ASSERT_RANGE (2, start1
, i
<= SCM_VECTOR_LENGTH (vec1
) && i
>= 0);
386 SCM_ASSERT_RANGE (5, start2
, j
<= SCM_VECTOR_LENGTH (vec2
) && j
>= 0);
387 SCM_ASSERT_RANGE (3, end1
, e
<= SCM_VECTOR_LENGTH (vec1
) && e
>= 0);
388 SCM_ASSERT_RANGE (5, start2
, e
-i
+j
<= SCM_VECTOR_LENGTH (vec2
));
389 while (i
<e
) SCM_VELTS (vec2
)[j
++] = SCM_VELTS (vec1
)[i
++];
390 return SCM_UNSPECIFIED
;
394 SCM_DEFINE (scm_vector_move_right_x
, "vector-move-right!", 5, 0, 0,
395 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
396 "Vector version of @code{substring-move-right!}.")
397 #define FUNC_NAME s_scm_vector_move_right_x
403 SCM_VALIDATE_VECTOR (1,vec1
);
404 SCM_VALIDATE_INUM_COPY (2,start1
,i
);
405 SCM_VALIDATE_INUM_COPY (3,end1
,e
);
406 SCM_VALIDATE_VECTOR (4,vec2
);
407 SCM_VALIDATE_INUM_COPY (5,start2
,j
);
408 SCM_ASSERT_RANGE (2, start1
, i
<= SCM_VECTOR_LENGTH (vec1
) && i
>= 0);
409 SCM_ASSERT_RANGE (5, start2
, j
<= SCM_VECTOR_LENGTH (vec2
) && j
>= 0);
410 SCM_ASSERT_RANGE (3, end1
, e
<= SCM_VECTOR_LENGTH (vec1
) && e
>= 0);
412 SCM_ASSERT_RANGE (5, start2
, j
<= SCM_VECTOR_LENGTH (vec2
));
414 SCM_VELTS (vec2
)[--j
] = SCM_VELTS (vec1
)[--e
];
415 return SCM_UNSPECIFIED
;
424 #ifndef SCM_MAGIC_SNARFER
425 #include "libguile/vectors.x"