1 /* Copyright (C) 1995,1996,1998,1999,2000,2001 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. */
45 #include "libguile/_scm.h"
46 #include "libguile/eq.h"
47 #include "libguile/root.h"
48 #include "libguile/strings.h"
50 #include "libguile/validate.h"
51 #include "libguile/vectors.h"
52 #include "libguile/unif.h"
55 #if (SCM_DEBUG_DEPRECATED == 0)
57 /* The function scm_vector_set_length_x will disappear in the next release of
62 * This complicates things too much if allowed on any array.
63 * C code can safely call it on arrays known to be used in a single
66 * SCM_REGISTER_PROC(s_vector_set_length_x, "vector-set-length!", 2, 0, 0, scm_vector_set_length_x);
68 static char s_vector_set_length_x
[] = "vector-set-length!";
72 scm_vector_set_length_x (SCM vect
, SCM len
)
80 SCM_ASRTGO (SCM_NIMP (vect
), badarg1
);
83 if (SCM_TYP7 (vect
) == scm_tc7_bvect
)
85 l
= (l
+ SCM_LONG_BIT
- 1) / SCM_LONG_BIT
;
87 sz
= scm_uniform_element_size (vect
);
89 base
= SCM_UVECTOR_BASE (vect
);
92 switch (SCM_TYP7 (vect
))
95 badarg1
: scm_wta (vect
, (char *) SCM_ARG1
, s_vector_set_length_x
);
97 SCM_ASRTGO (!SCM_EQ_P (vect
, scm_nullstr
), badarg1
);
99 base
= SCM_STRING_CHARS (vect
);
104 SCM_ASRTGO (!SCM_EQ_P (vect
, scm_nullvect
), badarg1
);
106 base
= (char *) SCM_VECTOR_BASE (vect
);
109 SCM_ASSERT (SCM_INUMP (len
), len
, SCM_ARG2
, s_vector_set_length_x
);
114 scm_memory_error (s_vector_set_length_x
);
118 scm_must_realloc (base
,
119 (size_t) SCM_LENGTH (vect
) * sz
,
121 s_vector_set_length_x
)));
122 if (SCM_VECTORP (vect
))
124 sz
= SCM_LENGTH (vect
);
126 SCM_VELTS (vect
)[--l
] = SCM_UNSPECIFIED
;
128 else if (SCM_STRINGP (vect
))
129 SCM_STRING_CHARS (vect
)[l
- 1] = 0;
130 SCM_SETLENGTH (vect
, SCM_INUM (len
), SCM_TYP7 (vect
));
135 #endif /* (SCM_DEBUG_DEPRECATED == 0) */
137 SCM_DEFINE (scm_vector_p
, "vector?", 1, 0, 0,
139 "Return @code{#t} if @var{obj} is a vector, otherwise return\n"
141 #define FUNC_NAME s_scm_vector_p
145 return SCM_BOOL (SCM_VECTORP (obj
));
149 SCM_GPROC (s_vector_length
, "vector-length", 1, 0, 0, scm_vector_length
, g_vector_length
);
150 /* Returns the number of elements in @var{vector} as an exact integer. */
152 scm_vector_length (SCM v
)
154 SCM_GASSERT1 (SCM_VECTORP(v
),
155 g_vector_length
, v
, SCM_ARG1
, s_vector_length
);
156 return SCM_MAKINUM (SCM_VECTOR_LENGTH (v
));
159 SCM_REGISTER_PROC (s_list_to_vector
, "list->vector", 1, 0, 0, scm_vector
);
161 "Return a newly created vector initialized to the elements of"
162 "the list @var{list}.\n\n"
164 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
165 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
168 SCM_DEFINE (scm_vector
, "vector", 0, 0, 1,
170 "@deffnx primitive list->vector l\n"
171 "Return a newly allocated vector whose elements contain the\n"
172 "given arguments. Analogous to @code{list}.\n"
175 "(vector 'a 'b 'c) @result{} #(a b c)\n"
177 #define FUNC_NAME s_scm_vector
183 /* Dirk:FIXME:: In case of multiple threads, the list might get corrupted
184 while the vector is being created. */
185 SCM_VALIDATE_LIST_COPYLEN (1, l
, i
);
186 res
= scm_c_make_vector (i
, SCM_UNSPECIFIED
);
187 data
= SCM_VELTS (res
);
188 while (!SCM_NULLP (l
))
190 *data
++ = SCM_CAR (l
);
198 SCM_GPROC (s_vector_ref
, "vector-ref", 2, 0, 0, scm_vector_ref
, g_vector_ref
);
201 "@var{k} must be a valid index of @var{vector}.\n"
202 "@samp{Vector-ref} returns the contents of element @var{k} of\n"
205 "(vector-ref '#(1 1 2 3 5 8 13 21) 5) @result{} 8\n"
206 "(vector-ref '#(1 1 2 3 5 8 13 21)\n"
207 " (let ((i (round (* 2 (acos -1)))))\n"
208 " (if (inexact? i)\n"
209 " (inexact->exact i)\n"
210 " i))) @result{} 13\n"
215 scm_vector_ref (SCM v
, SCM k
)
216 #define FUNC_NAME s_vector_ref
218 SCM_GASSERT2 (SCM_VECTORP (v
),
219 g_vector_ref
, v
, k
, SCM_ARG1
, s_vector_ref
);
220 SCM_GASSERT2 (SCM_INUMP (k
),
221 g_vector_ref
, v
, k
, SCM_ARG2
, s_vector_ref
);
222 SCM_ASSERT_RANGE (2, k
, SCM_INUM (k
) < SCM_VECTOR_LENGTH (v
) && SCM_INUM (k
) >= 0);
223 return SCM_VELTS (v
)[(long) SCM_INUM (k
)];
227 SCM_GPROC (s_vector_set_x
, "vector-set!", 3, 0, 0, scm_vector_set_x
, g_vector_set_x
);
229 /* "@var{k} must be a valid index of @var{vector}.\n"
230 "@code{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}.\n"
231 "The value returned by @samp{vector-set!} is unspecified.\n"
233 "(let ((vec (vector 0 '(2 2 2 2) "Anna")))\n"
234 " (vector-set! vec 1 '("Sue" "Sue"))\n"
235 " vec) @result{} #(0 ("Sue" "Sue") "Anna")\n"
236 "(vector-set! '#(0 1 2) 1 "doe") @result{} @emph{error} ; constant vector\n"
241 scm_vector_set_x (SCM v
, SCM k
, SCM obj
)
242 #define FUNC_NAME s_vector_set_x
244 SCM_GASSERTn (SCM_VECTORP (v
),
245 g_vector_set_x
, scm_list_3 (v
, k
, obj
),
246 SCM_ARG1
, s_vector_set_x
);
247 SCM_GASSERTn (SCM_INUMP (k
),
248 g_vector_set_x
, scm_list_3 (v
, k
, obj
),
249 SCM_ARG2
, s_vector_set_x
);
250 SCM_ASSERT_RANGE (2, k
, SCM_INUM (k
) < SCM_VECTOR_LENGTH (v
) && SCM_INUM (k
) >= 0);
251 SCM_VELTS(v
)[(long) SCM_INUM(k
)] = obj
;
252 return SCM_UNSPECIFIED
;
257 SCM_DEFINE (scm_make_vector
, "make-vector", 1, 1, 0,
259 "Return a newly allocated vector of @var{k} elements. If a\n"
260 "second argument is given, then each element is initialized to\n"
261 "@var{fill}. Otherwise the initial contents of each element is\n"
263 #define FUNC_NAME s_scm_make_vector
265 if (SCM_UNBNDP (fill
))
266 fill
= SCM_UNSPECIFIED
;
270 SCM_ASSERT_RANGE (1, k
, SCM_INUM (k
) >= 0);
271 return scm_c_make_vector (SCM_INUM (k
), fill
);
273 else if (SCM_BIGP (k
))
274 SCM_OUT_OF_RANGE (1, k
);
276 SCM_WRONG_TYPE_ARG (1, k
);
282 scm_c_make_vector (unsigned long int k
, SCM fill
)
283 #define FUNC_NAME s_scm_make_vector
292 SCM_ASSERT_RANGE (1, scm_ulong2num (k
), k
<= SCM_VECTOR_MAX_LENGTH
);
294 base
= scm_must_malloc (k
* sizeof (scm_t_bits
), FUNC_NAME
);
295 for (j
= 0; j
!= k
; ++j
)
296 base
[j
] = SCM_UNPACK (fill
);
302 SCM_SET_VECTOR_BASE (v
, base
);
303 SCM_SET_VECTOR_LENGTH (v
, k
, scm_tc7_vector
);
304 scm_remember_upto_here_1 (fill
);
311 SCM_DEFINE (scm_vector_to_list
, "vector->list", 1, 0, 0,
313 "Return a newly allocated list of the objects contained in the\n"
314 "elements of @var{vector}.\n"
317 "(vector->list '#(dah dah didah)) @result{} (dah dah didah)\n"
318 "(list->vector '(dididit dah)) @result{} #(dididit dah)\n"
320 #define FUNC_NAME s_scm_vector_to_list
325 SCM_VALIDATE_VECTOR (1,v
);
327 for(i
= SCM_VECTOR_LENGTH(v
)-1;i
>= 0;i
--) res
= scm_cons(data
[i
], res
);
333 SCM_DEFINE (scm_vector_fill_x
, "vector-fill!", 2, 0, 0,
335 "Store @var{fill} in every element of @var{vector}. The value\n"
336 "returned by @code{vector-fill!} is unspecified.")
337 #define FUNC_NAME s_scm_vector_fill_x
341 SCM_VALIDATE_VECTOR (1, v
);
342 data
= SCM_VELTS (v
);
343 for(i
= SCM_VECTOR_LENGTH (v
) - 1; i
>= 0; i
--)
345 return SCM_UNSPECIFIED
;
351 scm_vector_equal_p(SCM x
, SCM y
)
354 for(i
= SCM_VECTOR_LENGTH (x
) - 1; i
>= 0; i
--)
355 if (SCM_FALSEP (scm_equal_p (SCM_VELTS (x
)[i
], SCM_VELTS (y
)[i
])))
361 SCM_DEFINE (scm_vector_move_left_x
, "vector-move-left!", 5, 0, 0,
362 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
363 "Vector version of @code{substring-move-left!}.")
364 #define FUNC_NAME s_scm_vector_move_left_x
370 SCM_VALIDATE_VECTOR (1,vec1
);
371 SCM_VALIDATE_INUM_COPY (2,start1
,i
);
372 SCM_VALIDATE_INUM_COPY (3,end1
,e
);
373 SCM_VALIDATE_VECTOR (4,vec2
);
374 SCM_VALIDATE_INUM_COPY (5,start2
,j
);
375 SCM_ASSERT_RANGE (2, start1
, i
<= SCM_VECTOR_LENGTH (vec1
) && i
>= 0);
376 SCM_ASSERT_RANGE (5, start2
, j
<= SCM_VECTOR_LENGTH (vec2
) && j
>= 0);
377 SCM_ASSERT_RANGE (3, end1
, e
<= SCM_VECTOR_LENGTH (vec1
) && e
>= 0);
378 SCM_ASSERT_RANGE (5, start2
, e
-i
+j
<= SCM_VECTOR_LENGTH (vec2
));
379 while (i
<e
) SCM_VELTS (vec2
)[j
++] = SCM_VELTS (vec1
)[i
++];
380 return SCM_UNSPECIFIED
;
384 SCM_DEFINE (scm_vector_move_right_x
, "vector-move-right!", 5, 0, 0,
385 (SCM vec1
, SCM start1
, SCM end1
, SCM vec2
, SCM start2
),
386 "Vector version of @code{substring-move-right!}.")
387 #define FUNC_NAME s_scm_vector_move_right_x
393 SCM_VALIDATE_VECTOR (1,vec1
);
394 SCM_VALIDATE_INUM_COPY (2,start1
,i
);
395 SCM_VALIDATE_INUM_COPY (3,end1
,e
);
396 SCM_VALIDATE_VECTOR (4,vec2
);
397 SCM_VALIDATE_INUM_COPY (5,start2
,j
);
398 SCM_ASSERT_RANGE (2, start1
, i
<= SCM_VECTOR_LENGTH (vec1
) && i
>= 0);
399 SCM_ASSERT_RANGE (5, start2
, j
<= SCM_VECTOR_LENGTH (vec2
) && j
>= 0);
400 SCM_ASSERT_RANGE (3, end1
, e
<= SCM_VECTOR_LENGTH (vec1
) && e
>= 0);
402 SCM_ASSERT_RANGE (5, start2
, j
<= SCM_VECTOR_LENGTH (vec2
));
404 SCM_VELTS (vec2
)[--j
] = SCM_VELTS (vec1
)[--e
];
405 return SCM_UNSPECIFIED
;
414 scm_nullvect
= scm_c_make_vector (0, SCM_UNDEFINED
);
416 #ifndef SCM_MAGIC_SNARFER
417 #include "libguile/vectors.x"