| 1 | /* Copyright (C) 1995, 1996, 1998, 1999, 2000 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 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, |
| 43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ |
| 44 | |
| 45 | \f |
| 46 | |
| 47 | #include <stdio.h> |
| 48 | #include "libguile/_scm.h" |
| 49 | #include "libguile/eq.h" |
| 50 | #include "libguile/root.h" |
| 51 | #include "libguile/strings.h" |
| 52 | |
| 53 | #include "libguile/validate.h" |
| 54 | #include "libguile/vectors.h" |
| 55 | #include "libguile/unif.h" |
| 56 | \f |
| 57 | |
| 58 | /* |
| 59 | * This complicates things too much if allowed on any array. |
| 60 | * C code can safely call it on arrays known to be used in a single |
| 61 | * threaded manner. |
| 62 | * |
| 63 | * SCM_REGISTER_PROC(s_vector_set_length_x, "vector-set-length!", 2, 0, 0, scm_vector_set_length_x); |
| 64 | */ |
| 65 | static char s_vector_set_length_x[] = "vector-set-length!"; |
| 66 | |
| 67 | |
| 68 | SCM |
| 69 | scm_vector_set_length_x (SCM vect, SCM len) |
| 70 | { |
| 71 | long l; |
| 72 | scm_sizet siz; |
| 73 | scm_sizet sz; |
| 74 | |
| 75 | l = SCM_INUM (len); |
| 76 | SCM_ASRTGO (SCM_NIMP (vect), badarg1); |
| 77 | |
| 78 | #ifdef HAVE_ARRAYS |
| 79 | if (SCM_TYP7 (vect) == scm_tc7_bvect) |
| 80 | { |
| 81 | l = (l + SCM_LONG_BIT - 1) / SCM_LONG_BIT; |
| 82 | } |
| 83 | sz = scm_uniform_element_size (vect); |
| 84 | if (sz == 0) |
| 85 | #endif |
| 86 | switch (SCM_TYP7 (vect)) |
| 87 | { |
| 88 | default: |
| 89 | badarg1: scm_wta (vect, (char *) SCM_ARG1, s_vector_set_length_x); |
| 90 | case scm_tc7_string: |
| 91 | SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullstr), badarg1); |
| 92 | sz = sizeof (char); |
| 93 | l++; |
| 94 | break; |
| 95 | case scm_tc7_vector: |
| 96 | case scm_tc7_wvect: |
| 97 | SCM_ASRTGO (!SCM_EQ_P (vect, scm_nullvect), badarg1); |
| 98 | sz = sizeof (SCM); |
| 99 | break; |
| 100 | } |
| 101 | SCM_ASSERT (SCM_INUMP (len), len, SCM_ARG2, s_vector_set_length_x); |
| 102 | if (!l) |
| 103 | l = 1L; |
| 104 | siz = l * sz; |
| 105 | if (siz != l * sz) |
| 106 | scm_memory_error (s_vector_set_length_x); |
| 107 | SCM_REDEFER_INTS; |
| 108 | SCM_SETCHARS (vect, |
| 109 | ((char *) |
| 110 | scm_must_realloc (SCM_CHARS (vect), |
| 111 | (long) SCM_LENGTH (vect) * sz, |
| 112 | (long) siz, |
| 113 | s_vector_set_length_x))); |
| 114 | if (SCM_VECTORP (vect)) |
| 115 | { |
| 116 | sz = SCM_LENGTH (vect); |
| 117 | while (l > sz) |
| 118 | SCM_VELTS (vect)[--l] = SCM_UNSPECIFIED; |
| 119 | } |
| 120 | else if (SCM_STRINGP (vect)) |
| 121 | SCM_CHARS (vect)[l - 1] = 0; |
| 122 | SCM_SETLENGTH (vect, SCM_INUM (len), SCM_TYP7 (vect)); |
| 123 | SCM_REALLOW_INTS; |
| 124 | return vect; |
| 125 | } |
| 126 | |
| 127 | SCM_DEFINE (scm_vector_p, "vector?", 1, 0, 0, |
| 128 | (SCM obj), |
| 129 | "Returns @t{#t} if @var{obj} is a vector, otherwise returns @t{#f}. (r5rs)") |
| 130 | #define FUNC_NAME s_scm_vector_p |
| 131 | { |
| 132 | if (SCM_IMP (obj)) |
| 133 | return SCM_BOOL_F; |
| 134 | return SCM_BOOL (SCM_VECTORP (obj)); |
| 135 | } |
| 136 | #undef FUNC_NAME |
| 137 | |
| 138 | SCM_GPROC (s_vector_length, "vector-length", 1, 0, 0, scm_vector_length, g_vector_length); |
| 139 | /* Returns the number of elements in @var{vector} as an exact integer. (r5rs) */ |
| 140 | SCM |
| 141 | scm_vector_length (SCM v) |
| 142 | { |
| 143 | SCM_GASSERT1 (SCM_VECTORP(v), |
| 144 | g_vector_length, v, SCM_ARG1, s_vector_length); |
| 145 | return SCM_MAKINUM (SCM_LENGTH (v)); |
| 146 | } |
| 147 | |
| 148 | SCM_REGISTER_PROC (s_list_to_vector, "list->vector", 1, 0, 0, scm_vector); |
| 149 | /* |
| 150 | "@samp{List->vector} returns a newly\n" |
| 151 | "created vector initialized to the elements of the list @var{list}.\n\n" |
| 152 | "@format\n" |
| 153 | "@t{(vector->list '#(dah dah didah))\n" |
| 154 | "=> (dah dah didah)\n" |
| 155 | "list->vector '(dididit dah))\n" |
| 156 | "=> #(dididit dah)\n" |
| 157 | "}\n" |
| 158 | "@end format") |
| 159 | */ |
| 160 | SCM_DEFINE (scm_vector, "vector", 0, 0, 1, |
| 161 | (SCM l), |
| 162 | "Returns a newly allocated vector whose elements contain the given\n" |
| 163 | "arguments. Analogous to @samp{list}. (r5rs)\n\n" |
| 164 | "@format\n" |
| 165 | "@t{(vector 'a 'b 'c) ==> #(a b c) }\n" |
| 166 | "@end format") |
| 167 | #define FUNC_NAME s_scm_vector |
| 168 | { |
| 169 | SCM res; |
| 170 | register SCM *data; |
| 171 | int i; |
| 172 | SCM_VALIDATE_LIST_COPYLEN (1,l,i); |
| 173 | res = scm_make_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED); |
| 174 | data = SCM_VELTS (res); |
| 175 | for(; i && SCM_NIMP(l); --i, l = SCM_CDR (l)) |
| 176 | *data++ = SCM_CAR (l); |
| 177 | return res; |
| 178 | } |
| 179 | #undef FUNC_NAME |
| 180 | |
| 181 | SCM_GPROC (s_vector_ref, "vector-ref", 2, 0, 0, scm_vector_ref, g_vector_ref); |
| 182 | |
| 183 | /* |
| 184 | "@var{k} must be a valid index of @var{vector}.\n" |
| 185 | "@samp{Vector-ref} returns the contents of element @var{k} of\n" |
| 186 | "@var{vector}.\n\n" |
| 187 | "@format\n" |
| 188 | "@t{(vector-ref '#(1 1 2 3 5 8 13 21)\n" |
| 189 | " 5)\n" |
| 190 | " ==> 8\n" |
| 191 | "(vector-ref '#(1 1 2 3 5 8 13 21)\n" |
| 192 | " (let ((i (round (* 2 (acos -1)))))\n" |
| 193 | " (if (inexact? i)\n" |
| 194 | " (inexact->exact i)\n" |
| 195 | " i))) \n" |
| 196 | " ==> 13\n" |
| 197 | "}\n" |
| 198 | "@end format" |
| 199 | */ |
| 200 | |
| 201 | SCM |
| 202 | scm_vector_ref (SCM v, SCM k) |
| 203 | { |
| 204 | SCM_GASSERT2 (SCM_VECTORP (v), |
| 205 | g_vector_ref, v, k, SCM_ARG1, s_vector_ref); |
| 206 | SCM_GASSERT2 (SCM_INUMP (k), |
| 207 | g_vector_ref, v, k, SCM_ARG2, s_vector_ref); |
| 208 | SCM_ASSERT (SCM_INUM (k) < SCM_LENGTH (v) && SCM_INUM (k) >= 0, |
| 209 | k, SCM_OUTOFRANGE, s_vector_ref); |
| 210 | return SCM_VELTS (v)[(long) SCM_INUM (k)]; |
| 211 | } |
| 212 | |
| 213 | SCM_GPROC (s_vector_set_x, "vector-set!", 3, 0, 0, scm_vector_set_x, g_vector_set_x); |
| 214 | |
| 215 | /* (r5rs) |
| 216 | @var{k} must be a valid index of @var{vector}. |
| 217 | @samp{Vector-set!} stores @var{obj} in element @var{k} of @var{vector}. |
| 218 | The value returned by @samp{vector-set!} is unspecified. |
| 219 | @c <!> |
| 220 | |
| 221 | |
| 222 | @format |
| 223 | @t{(let ((vec (vector 0 '(2 2 2 2) "Anna"))) |
| 224 | (vector-set! vec 1 '("Sue" "Sue")) |
| 225 | vec) |
| 226 | ==> #(0 ("Sue" "Sue") "Anna") |
| 227 | |
| 228 | (vector-set! '#(0 1 2) 1 "doe") |
| 229 | ==> @emph{error} ; constant vector |
| 230 | } |
| 231 | @end format |
| 232 | */ |
| 233 | |
| 234 | SCM |
| 235 | scm_vector_set_x (SCM v, SCM k, SCM obj) |
| 236 | { |
| 237 | SCM_GASSERTn (SCM_VECTORP (v), |
| 238 | g_vector_set_x, SCM_LIST3 (v, k, obj), |
| 239 | SCM_ARG1, s_vector_set_x); |
| 240 | SCM_GASSERTn (SCM_INUMP (k), |
| 241 | g_vector_set_x, SCM_LIST3 (v, k, obj), |
| 242 | SCM_ARG2, s_vector_set_x); |
| 243 | SCM_ASSERT ((SCM_INUM (k) < SCM_LENGTH (v)) && (SCM_INUM (k) >= 0), |
| 244 | k, SCM_OUTOFRANGE, s_vector_set_x); |
| 245 | SCM_VELTS(v)[(long) SCM_INUM(k)] = obj; |
| 246 | return SCM_UNSPECIFIED; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | SCM_DEFINE (scm_make_vector, "make-vector", 1, 1, 0, |
| 251 | (SCM k, SCM fill), |
| 252 | "Returns a newly allocated vector of @var{k} elements. If a second\n" |
| 253 | "argument is given, then each element is initialized to @var{fill}.\n" |
| 254 | "Otherwise the initial contents of each element is unspecified. (r5rs)") |
| 255 | #define FUNC_NAME s_scm_make_vector |
| 256 | { |
| 257 | SCM v; |
| 258 | register long i; |
| 259 | register long j; |
| 260 | register SCM *velts; |
| 261 | |
| 262 | SCM_VALIDATE_INUM_MIN (1,k,0); |
| 263 | if (SCM_UNBNDP(fill)) |
| 264 | fill = SCM_UNSPECIFIED; |
| 265 | i = SCM_INUM(k); |
| 266 | SCM_NEWCELL(v); |
| 267 | SCM_DEFER_INTS; |
| 268 | SCM_SETCHARS(v, scm_must_malloc(i?(long)(i*sizeof(SCM)):1L, FUNC_NAME)); |
| 269 | SCM_SETLENGTH(v, i, scm_tc7_vector); |
| 270 | velts = SCM_VELTS(v); |
| 271 | j = 0; |
| 272 | while(--i >= j) (velts)[i] = fill; |
| 273 | SCM_ALLOW_INTS; |
| 274 | return v; |
| 275 | } |
| 276 | #undef FUNC_NAME |
| 277 | |
| 278 | |
| 279 | SCM_DEFINE (scm_vector_to_list, "vector->list", 1, 0, 0, |
| 280 | (SCM v), |
| 281 | "@samp{Vector->list} returns a newly allocated list of the objects contained\n" |
| 282 | "in the elements of @var{vector}. (r5rs)\n\n" |
| 283 | "@format\n" |
| 284 | "@t{(vector->list '#(dah dah didah))\n" |
| 285 | "=> (dah dah didah)\n" |
| 286 | "list->vector '(dididit dah))\n" |
| 287 | "=> #(dididit dah)\n" |
| 288 | "}\n" |
| 289 | "@end format") |
| 290 | #define FUNC_NAME s_scm_vector_to_list |
| 291 | { |
| 292 | SCM res = SCM_EOL; |
| 293 | long i; |
| 294 | SCM *data; |
| 295 | SCM_VALIDATE_VECTOR (1,v); |
| 296 | data = SCM_VELTS(v); |
| 297 | for(i = SCM_LENGTH(v)-1;i >= 0;i--) res = scm_cons(data[i], res); |
| 298 | return res; |
| 299 | } |
| 300 | #undef FUNC_NAME |
| 301 | |
| 302 | |
| 303 | SCM_DEFINE (scm_vector_fill_x, "vector-fill!", 2, 0, 0, |
| 304 | (SCM v, SCM fill_x), |
| 305 | "Stores @var{fill} in every element of @var{vector}.\n" |
| 306 | "The value returned by @samp{vector-fill!} is unspecified. (r5rs)") |
| 307 | #define FUNC_NAME s_scm_vector_fill_x |
| 308 | { |
| 309 | register long i; |
| 310 | register SCM *data; |
| 311 | SCM_VALIDATE_VECTOR (1,v); |
| 312 | data = SCM_VELTS(v); |
| 313 | for(i = SCM_LENGTH(v) - 1; i >= 0; i--) |
| 314 | data[i] = fill_x; |
| 315 | return SCM_UNSPECIFIED; |
| 316 | } |
| 317 | #undef FUNC_NAME |
| 318 | |
| 319 | |
| 320 | SCM |
| 321 | scm_vector_equal_p(SCM x, SCM y) |
| 322 | { |
| 323 | long i; |
| 324 | for(i = SCM_LENGTH(x)-1;i >= 0;i--) |
| 325 | if (SCM_FALSEP(scm_equal_p(SCM_VELTS(x)[i], SCM_VELTS(y)[i]))) |
| 326 | return SCM_BOOL_F; |
| 327 | return SCM_BOOL_T; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | SCM_DEFINE (scm_vector_move_left_x, "vector-move-left!", 5, 0, 0, |
| 332 | (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2), |
| 333 | "Vector version of @code{substring-move-left!}.") |
| 334 | #define FUNC_NAME s_scm_vector_move_left_x |
| 335 | { |
| 336 | long i; |
| 337 | long j; |
| 338 | long e; |
| 339 | |
| 340 | SCM_VALIDATE_VECTOR (1,vec1); |
| 341 | SCM_VALIDATE_INUM_COPY (2,start1,i); |
| 342 | SCM_VALIDATE_INUM_COPY (3,end1,e); |
| 343 | SCM_VALIDATE_VECTOR (4,vec2); |
| 344 | SCM_VALIDATE_INUM_COPY (5,start2,j); |
| 345 | SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, FUNC_NAME); |
| 346 | SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, FUNC_NAME); |
| 347 | SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, FUNC_NAME); |
| 348 | SCM_ASSERT (e-i+j <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, FUNC_NAME); |
| 349 | while (i<e) SCM_VELTS (vec2)[j++] = SCM_VELTS (vec1)[i++]; |
| 350 | return SCM_UNSPECIFIED; |
| 351 | } |
| 352 | #undef FUNC_NAME |
| 353 | |
| 354 | SCM_DEFINE (scm_vector_move_right_x, "vector-move-right!", 5, 0, 0, |
| 355 | (SCM vec1, SCM start1, SCM end1, SCM vec2, SCM start2), |
| 356 | "Vector version of @code{substring-move-right!}.") |
| 357 | #define FUNC_NAME s_scm_vector_move_right_x |
| 358 | { |
| 359 | long i; |
| 360 | long j; |
| 361 | long e; |
| 362 | |
| 363 | SCM_VALIDATE_VECTOR (1,vec1); |
| 364 | SCM_VALIDATE_INUM_COPY (2,start1,i); |
| 365 | SCM_VALIDATE_INUM_COPY (3,end1,e); |
| 366 | SCM_VALIDATE_VECTOR (4,vec2); |
| 367 | SCM_VALIDATE_INUM_COPY (5,start2,j); |
| 368 | SCM_ASSERT (i <= SCM_LENGTH (vec1) && i >= 0, start1, SCM_OUTOFRANGE, FUNC_NAME); |
| 369 | SCM_ASSERT (j <= SCM_LENGTH (vec2) && j >= 0, start2, SCM_OUTOFRANGE, FUNC_NAME); |
| 370 | SCM_ASSERT (e <= SCM_LENGTH (vec1) && e >= 0, end1, SCM_OUTOFRANGE, FUNC_NAME); |
| 371 | j = e - i + j; |
| 372 | SCM_ASSERT (j <= SCM_LENGTH (vec2), start2, SCM_OUTOFRANGE, FUNC_NAME); |
| 373 | while (i < e) |
| 374 | SCM_VELTS (vec2)[--j] = SCM_VELTS (vec1)[--e]; |
| 375 | return SCM_UNSPECIFIED; |
| 376 | } |
| 377 | #undef FUNC_NAME |
| 378 | |
| 379 | |
| 380 | |
| 381 | void |
| 382 | scm_init_vectors () |
| 383 | { |
| 384 | #include "libguile/vectors.x" |
| 385 | /* |
| 386 | scm_make_subr (s_resizuve, scm_tc7_subr_2, scm_vector_set_length_x); */ |
| 387 | } |
| 388 | |
| 389 | |
| 390 | /* |
| 391 | Local Variables: |
| 392 | c-file-style: "gnu" |
| 393 | End: |
| 394 | */ |