| 1 | /* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | /* |
| 20 | This file has code for arrays in lots of variants (double, integer, |
| 21 | unsigned etc. ). It suffers from hugely repetitive code because |
| 22 | there is similar (but different) code for every variant included. (urg.) |
| 23 | |
| 24 | --hwn |
| 25 | */ |
| 26 | \f |
| 27 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | # include <config.h> |
| 30 | #endif |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <errno.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include "libguile/_scm.h" |
| 37 | #include "libguile/__scm.h" |
| 38 | #include "libguile/eq.h" |
| 39 | #include "libguile/chars.h" |
| 40 | #include "libguile/eval.h" |
| 41 | #include "libguile/fports.h" |
| 42 | #include "libguile/smob.h" |
| 43 | #include "libguile/feature.h" |
| 44 | #include "libguile/root.h" |
| 45 | #include "libguile/strings.h" |
| 46 | #include "libguile/srfi-13.h" |
| 47 | #include "libguile/srfi-4.h" |
| 48 | #include "libguile/vectors.h" |
| 49 | #include "libguile/list.h" |
| 50 | #include "libguile/deprecation.h" |
| 51 | #include "libguile/dynwind.h" |
| 52 | |
| 53 | #include "libguile/validate.h" |
| 54 | #include "libguile/unif.h" |
| 55 | #include "libguile/ramap.h" |
| 56 | #include "libguile/print.h" |
| 57 | #include "libguile/read.h" |
| 58 | |
| 59 | #ifdef HAVE_UNISTD_H |
| 60 | #include <unistd.h> |
| 61 | #endif |
| 62 | |
| 63 | #ifdef HAVE_IO_H |
| 64 | #include <io.h> |
| 65 | #endif |
| 66 | |
| 67 | \f |
| 68 | /* The set of uniform scm_vector types is: |
| 69 | * Vector of: Called: Replaced by: |
| 70 | * unsigned char string |
| 71 | * char byvect s8 or u8, depending on signedness of 'char' |
| 72 | * boolean bvect |
| 73 | * signed long ivect s32 |
| 74 | * unsigned long uvect u32 |
| 75 | * float fvect f32 |
| 76 | * double dvect d32 |
| 77 | * complex double cvect c64 |
| 78 | * short svect s16 |
| 79 | * long long llvect s64 |
| 80 | */ |
| 81 | |
| 82 | scm_t_bits scm_i_tc16_array; |
| 83 | scm_t_bits scm_i_tc16_enclosed_array; |
| 84 | |
| 85 | #define SCM_SET_ARRAY_CONTIGUOUS_FLAG(x) \ |
| 86 | (SCM_SET_CELL_WORD_0 ((x), SCM_CELL_WORD_0 (x) | SCM_I_ARRAY_FLAG_CONTIGUOUS)) |
| 87 | #define SCM_CLR_ARRAY_CONTIGUOUS_FLAG(x) \ |
| 88 | (SCM_SET_CELL_WORD_0 ((x), SCM_CELL_WORD_0 (x) & ~SCM_I_ARRAY_FLAG_CONTIGUOUS)) |
| 89 | |
| 90 | typedef SCM creator_proc (SCM len, SCM fill); |
| 91 | |
| 92 | struct { |
| 93 | char *type_name; |
| 94 | SCM type; |
| 95 | creator_proc *creator; |
| 96 | } type_creator_table[] = { |
| 97 | { "a", SCM_UNSPECIFIED, scm_make_string }, |
| 98 | { "b", SCM_UNSPECIFIED, scm_make_bitvector }, |
| 99 | { "u8", SCM_UNSPECIFIED, scm_make_u8vector }, |
| 100 | { "s8", SCM_UNSPECIFIED, scm_make_s8vector }, |
| 101 | { "u16", SCM_UNSPECIFIED, scm_make_u16vector }, |
| 102 | { "s16", SCM_UNSPECIFIED, scm_make_s16vector }, |
| 103 | { "u32", SCM_UNSPECIFIED, scm_make_u32vector }, |
| 104 | { "s32", SCM_UNSPECIFIED, scm_make_s32vector }, |
| 105 | { "u64", SCM_UNSPECIFIED, scm_make_u64vector }, |
| 106 | { "s64", SCM_UNSPECIFIED, scm_make_s64vector }, |
| 107 | { "f32", SCM_UNSPECIFIED, scm_make_f32vector }, |
| 108 | { "f64", SCM_UNSPECIFIED, scm_make_f64vector }, |
| 109 | { "c32", SCM_UNSPECIFIED, scm_make_c32vector }, |
| 110 | { "c64", SCM_UNSPECIFIED, scm_make_c64vector }, |
| 111 | { NULL } |
| 112 | }; |
| 113 | |
| 114 | static void |
| 115 | init_type_creator_table () |
| 116 | { |
| 117 | int i; |
| 118 | for (i = 0; type_creator_table[i].type_name; i++) |
| 119 | { |
| 120 | SCM sym = scm_from_locale_symbol (type_creator_table[i].type_name); |
| 121 | type_creator_table[i].type = scm_permanent_object (sym); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static creator_proc * |
| 126 | type_to_creator (SCM type) |
| 127 | { |
| 128 | int i; |
| 129 | |
| 130 | if (scm_is_eq (type, SCM_BOOL_T)) |
| 131 | return scm_make_vector; |
| 132 | for (i = 0; type_creator_table[i].type_name; i++) |
| 133 | if (scm_is_eq (type, type_creator_table[i].type)) |
| 134 | return type_creator_table[i].creator; |
| 135 | |
| 136 | scm_misc_error (NULL, "unknown array type: ~a", scm_list_1 (type)); |
| 137 | } |
| 138 | |
| 139 | static SCM |
| 140 | make_typed_vector (SCM type, size_t len) |
| 141 | { |
| 142 | creator_proc *creator = type_to_creator (type); |
| 143 | return creator (scm_from_size_t (len), SCM_UNDEFINED); |
| 144 | } |
| 145 | |
| 146 | #if SCM_ENABLE_DEPRECATED |
| 147 | |
| 148 | SCM_SYMBOL (scm_sym_s, "s"); |
| 149 | SCM_SYMBOL (scm_sym_l, "l"); |
| 150 | |
| 151 | static int |
| 152 | singp (SCM obj) |
| 153 | { |
| 154 | if (!SCM_REALP (obj)) |
| 155 | return 0; |
| 156 | else |
| 157 | { |
| 158 | double x = SCM_REAL_VALUE (obj); |
| 159 | float fx = x; |
| 160 | return (- SCM_FLTMAX < x) && (x < SCM_FLTMAX) && (fx == x); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | SCM_API int scm_i_inump (SCM obj); |
| 165 | SCM_API scm_t_signed_bits scm_i_inum (SCM obj); |
| 166 | |
| 167 | static SCM |
| 168 | prototype_to_type (SCM proto) |
| 169 | { |
| 170 | const char *type_name; |
| 171 | |
| 172 | if (scm_is_eq (proto, SCM_BOOL_T)) |
| 173 | type_name = "b"; |
| 174 | else if (scm_is_eq (proto, SCM_MAKE_CHAR (0))) |
| 175 | type_name = "s8"; |
| 176 | else if (SCM_CHARP (proto)) |
| 177 | type_name = "a"; |
| 178 | else if (scm_i_inump (proto)) |
| 179 | { |
| 180 | if (scm_i_inum (proto) > 0) |
| 181 | type_name = "u32"; |
| 182 | else |
| 183 | type_name = "s32"; |
| 184 | } |
| 185 | else if (scm_is_eq (proto, scm_sym_s)) |
| 186 | type_name = "s16"; |
| 187 | else if (scm_is_eq (proto, scm_sym_l)) |
| 188 | type_name = "s64"; |
| 189 | else if (SCM_REALP (proto) |
| 190 | || scm_is_true (scm_eqv_p (proto, |
| 191 | scm_divide (scm_from_int (1), |
| 192 | scm_from_int (3))))) |
| 193 | { |
| 194 | if (singp (proto)) |
| 195 | type_name = "f32"; |
| 196 | else |
| 197 | type_name = "f64"; |
| 198 | } |
| 199 | else if (SCM_COMPLEXP (proto)) |
| 200 | type_name = "c64"; |
| 201 | else if (scm_is_null (proto)) |
| 202 | type_name = NULL; |
| 203 | else |
| 204 | type_name = NULL; |
| 205 | |
| 206 | if (type_name) |
| 207 | return scm_from_locale_symbol (type_name); |
| 208 | else |
| 209 | return SCM_BOOL_T; |
| 210 | } |
| 211 | |
| 212 | static SCM |
| 213 | scm_i_get_old_prototype (SCM uvec) |
| 214 | { |
| 215 | if (scm_is_bitvector (uvec)) |
| 216 | return SCM_BOOL_T; |
| 217 | else if (scm_is_string (uvec)) |
| 218 | return SCM_MAKE_CHAR ('a'); |
| 219 | else if (scm_is_true (scm_s8vector_p (uvec))) |
| 220 | return SCM_MAKE_CHAR ('\0'); |
| 221 | else if (scm_is_true (scm_s16vector_p (uvec))) |
| 222 | return scm_sym_s; |
| 223 | else if (scm_is_true (scm_u32vector_p (uvec))) |
| 224 | return scm_from_int (1); |
| 225 | else if (scm_is_true (scm_s32vector_p (uvec))) |
| 226 | return scm_from_int (-1); |
| 227 | else if (scm_is_true (scm_s64vector_p (uvec))) |
| 228 | return scm_sym_l; |
| 229 | else if (scm_is_true (scm_f32vector_p (uvec))) |
| 230 | return scm_from_double (1.0); |
| 231 | else if (scm_is_true (scm_f64vector_p (uvec))) |
| 232 | return scm_divide (scm_from_int (1), scm_from_int (3)); |
| 233 | else if (scm_is_true (scm_c64vector_p (uvec))) |
| 234 | return scm_c_make_rectangular (0, 1); |
| 235 | else if (scm_is_vector (uvec)) |
| 236 | return SCM_EOL; |
| 237 | else |
| 238 | scm_misc_error (NULL, "~a has no prototype", scm_list_1 (uvec)); |
| 239 | } |
| 240 | |
| 241 | SCM |
| 242 | scm_make_uve (long k, SCM prot) |
| 243 | #define FUNC_NAME "scm_make_uve" |
| 244 | { |
| 245 | scm_c_issue_deprecation_warning |
| 246 | ("`scm_make_uve' is deprecated, see the manual for alternatives."); |
| 247 | |
| 248 | return make_typed_vector (prototype_to_type (prot), k); |
| 249 | } |
| 250 | #undef FUNC_NAME |
| 251 | |
| 252 | #endif |
| 253 | |
| 254 | int |
| 255 | scm_is_array (SCM obj) |
| 256 | { |
| 257 | return (SCM_I_ENCLOSED_ARRAYP (obj) |
| 258 | || SCM_I_ARRAYP (obj) |
| 259 | || scm_is_generalized_vector (obj)); |
| 260 | } |
| 261 | |
| 262 | int |
| 263 | scm_is_typed_array (SCM obj, SCM type) |
| 264 | { |
| 265 | if (SCM_I_ENCLOSED_ARRAYP (obj)) |
| 266 | { |
| 267 | /* Enclosed arrays are arrays but are not of any type. |
| 268 | */ |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | /* Get storage vector. |
| 273 | */ |
| 274 | if (SCM_I_ARRAYP (obj)) |
| 275 | obj = SCM_I_ARRAY_V (obj); |
| 276 | |
| 277 | /* It must be a generalized vector (which includes vectors, strings, etc). |
| 278 | */ |
| 279 | if (!scm_is_generalized_vector (obj)) |
| 280 | return 0; |
| 281 | |
| 282 | return scm_is_eq (type, scm_i_generalized_vector_type (obj)); |
| 283 | } |
| 284 | |
| 285 | static SCM |
| 286 | enclosed_ref (scm_t_array_handle *h, ssize_t pos) |
| 287 | { |
| 288 | return scm_i_cvref (SCM_I_ARRAY_V (h->array), pos + h->base, 1); |
| 289 | } |
| 290 | |
| 291 | static SCM |
| 292 | vector_ref (scm_t_array_handle *h, ssize_t pos) |
| 293 | { |
| 294 | return ((const SCM *)h->elements)[pos]; |
| 295 | } |
| 296 | |
| 297 | static SCM |
| 298 | string_ref (scm_t_array_handle *h, ssize_t pos) |
| 299 | { |
| 300 | pos += h->base; |
| 301 | if (SCM_I_ARRAYP (h->array)) |
| 302 | return scm_c_string_ref (SCM_I_ARRAY_V (h->array), pos); |
| 303 | else |
| 304 | return scm_c_string_ref (h->array, pos); |
| 305 | } |
| 306 | |
| 307 | static SCM |
| 308 | bitvector_ref (scm_t_array_handle *h, ssize_t pos) |
| 309 | { |
| 310 | pos += scm_array_handle_bit_elements_offset (h); |
| 311 | return |
| 312 | scm_from_bool (((scm_t_uint32 *)h->elements)[pos/32] & (1l << (pos % 32))); |
| 313 | } |
| 314 | |
| 315 | static SCM |
| 316 | memoize_ref (scm_t_array_handle *h, ssize_t pos) |
| 317 | { |
| 318 | SCM v = h->array; |
| 319 | |
| 320 | if (SCM_I_ENCLOSED_ARRAYP (v)) |
| 321 | { |
| 322 | h->ref = enclosed_ref; |
| 323 | return enclosed_ref (h, pos); |
| 324 | } |
| 325 | |
| 326 | if (SCM_I_ARRAYP (v)) |
| 327 | v = SCM_I_ARRAY_V (v); |
| 328 | |
| 329 | if (scm_is_vector (v)) |
| 330 | { |
| 331 | h->elements = scm_array_handle_elements (h); |
| 332 | h->ref = vector_ref; |
| 333 | } |
| 334 | else if (scm_is_uniform_vector (v)) |
| 335 | { |
| 336 | h->elements = scm_array_handle_uniform_elements (h); |
| 337 | h->ref = scm_i_uniform_vector_ref_proc (v); |
| 338 | } |
| 339 | else if (scm_is_string (v)) |
| 340 | { |
| 341 | h->ref = string_ref; |
| 342 | } |
| 343 | else if (scm_is_bitvector (v)) |
| 344 | { |
| 345 | h->elements = scm_array_handle_bit_elements (h); |
| 346 | h->ref = bitvector_ref; |
| 347 | } |
| 348 | else |
| 349 | scm_misc_error (NULL, "unknown array type: ~a", scm_list_1 (h->array)); |
| 350 | |
| 351 | return h->ref (h, pos); |
| 352 | } |
| 353 | |
| 354 | static void |
| 355 | enclosed_set (scm_t_array_handle *h, ssize_t pos, SCM val) |
| 356 | { |
| 357 | scm_wrong_type_arg_msg (NULL, 0, h->array, "non-enclosed array"); |
| 358 | } |
| 359 | |
| 360 | static void |
| 361 | vector_set (scm_t_array_handle *h, ssize_t pos, SCM val) |
| 362 | { |
| 363 | ((SCM *)h->writable_elements)[pos] = val; |
| 364 | } |
| 365 | |
| 366 | static void |
| 367 | string_set (scm_t_array_handle *h, ssize_t pos, SCM val) |
| 368 | { |
| 369 | pos += h->base; |
| 370 | if (SCM_I_ARRAYP (h->array)) |
| 371 | scm_c_string_set_x (SCM_I_ARRAY_V (h->array), pos, val); |
| 372 | else |
| 373 | scm_c_string_set_x (h->array, pos, val); |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | bitvector_set (scm_t_array_handle *h, ssize_t pos, SCM val) |
| 378 | { |
| 379 | scm_t_uint32 mask; |
| 380 | pos += scm_array_handle_bit_elements_offset (h); |
| 381 | mask = 1l << (pos % 32); |
| 382 | if (scm_to_bool (val)) |
| 383 | ((scm_t_uint32 *)h->writable_elements)[pos/32] |= mask; |
| 384 | else |
| 385 | ((scm_t_uint32 *)h->writable_elements)[pos/32] &= ~mask; |
| 386 | } |
| 387 | |
| 388 | static void |
| 389 | memoize_set (scm_t_array_handle *h, ssize_t pos, SCM val) |
| 390 | { |
| 391 | SCM v = h->array; |
| 392 | |
| 393 | if (SCM_I_ENCLOSED_ARRAYP (v)) |
| 394 | { |
| 395 | h->set = enclosed_set; |
| 396 | enclosed_set (h, pos, val); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | if (SCM_I_ARRAYP (v)) |
| 401 | v = SCM_I_ARRAY_V (v); |
| 402 | |
| 403 | if (scm_is_vector (v)) |
| 404 | { |
| 405 | h->writable_elements = scm_array_handle_writable_elements (h); |
| 406 | h->set = vector_set; |
| 407 | } |
| 408 | else if (scm_is_uniform_vector (v)) |
| 409 | { |
| 410 | h->writable_elements = scm_array_handle_uniform_writable_elements (h); |
| 411 | h->set = scm_i_uniform_vector_set_proc (v); |
| 412 | } |
| 413 | else if (scm_is_string (v)) |
| 414 | { |
| 415 | h->set = string_set; |
| 416 | } |
| 417 | else if (scm_is_bitvector (v)) |
| 418 | { |
| 419 | h->writable_elements = scm_array_handle_bit_writable_elements (h); |
| 420 | h->set = bitvector_set; |
| 421 | } |
| 422 | else |
| 423 | scm_misc_error (NULL, "unknown array type: ~a", scm_list_1 (h->array)); |
| 424 | |
| 425 | h->set (h, pos, val); |
| 426 | } |
| 427 | |
| 428 | void |
| 429 | scm_array_get_handle (SCM array, scm_t_array_handle *h) |
| 430 | { |
| 431 | h->array = array; |
| 432 | h->ref = memoize_ref; |
| 433 | h->set = memoize_set; |
| 434 | |
| 435 | if (SCM_I_ARRAYP (array) || SCM_I_ENCLOSED_ARRAYP (array)) |
| 436 | { |
| 437 | h->dims = SCM_I_ARRAY_DIMS (array); |
| 438 | h->base = SCM_I_ARRAY_BASE (array); |
| 439 | } |
| 440 | else if (scm_is_generalized_vector (array)) |
| 441 | { |
| 442 | h->dim0.lbnd = 0; |
| 443 | h->dim0.ubnd = scm_c_generalized_vector_length (array) - 1; |
| 444 | h->dim0.inc = 1; |
| 445 | h->dims = &h->dim0; |
| 446 | h->base = 0; |
| 447 | } |
| 448 | else |
| 449 | scm_wrong_type_arg_msg (NULL, 0, array, "array"); |
| 450 | } |
| 451 | |
| 452 | void |
| 453 | scm_array_handle_release (scm_t_array_handle *h) |
| 454 | { |
| 455 | /* Nothing to do here until arrays need to be reserved for real. |
| 456 | */ |
| 457 | } |
| 458 | |
| 459 | size_t |
| 460 | scm_array_handle_rank (scm_t_array_handle *h) |
| 461 | { |
| 462 | if (SCM_I_ARRAYP (h->array) || SCM_I_ENCLOSED_ARRAYP (h->array)) |
| 463 | return SCM_I_ARRAY_NDIM (h->array); |
| 464 | else |
| 465 | return 1; |
| 466 | } |
| 467 | |
| 468 | scm_t_array_dim * |
| 469 | scm_array_handle_dims (scm_t_array_handle *h) |
| 470 | { |
| 471 | return h->dims; |
| 472 | } |
| 473 | |
| 474 | const SCM * |
| 475 | scm_array_handle_elements (scm_t_array_handle *h) |
| 476 | { |
| 477 | SCM vec = h->array; |
| 478 | if (SCM_I_ARRAYP (vec)) |
| 479 | vec = SCM_I_ARRAY_V (vec); |
| 480 | if (SCM_I_IS_VECTOR (vec)) |
| 481 | return SCM_I_VECTOR_ELTS (vec) + h->base; |
| 482 | scm_wrong_type_arg_msg (NULL, 0, h->array, "non-uniform array"); |
| 483 | } |
| 484 | |
| 485 | SCM * |
| 486 | scm_array_handle_writable_elements (scm_t_array_handle *h) |
| 487 | { |
| 488 | SCM vec = h->array; |
| 489 | if (SCM_I_ARRAYP (vec)) |
| 490 | vec = SCM_I_ARRAY_V (vec); |
| 491 | if (SCM_I_IS_VECTOR (vec)) |
| 492 | return SCM_I_VECTOR_WELTS (vec) + h->base; |
| 493 | scm_wrong_type_arg_msg (NULL, 0, h->array, "non-uniform array"); |
| 494 | } |
| 495 | |
| 496 | #if SCM_ENABLE_DEPRECATED |
| 497 | |
| 498 | SCM_DEFINE (scm_array_p, "array?", 1, 1, 0, |
| 499 | (SCM obj, SCM prot), |
| 500 | "Return @code{#t} if the @var{obj} is an array, and @code{#f} if\n" |
| 501 | "not.") |
| 502 | #define FUNC_NAME s_scm_array_p |
| 503 | { |
| 504 | if (!SCM_UNBNDP (prot)) |
| 505 | { |
| 506 | scm_c_issue_deprecation_warning |
| 507 | ("Using prototypes with `array?' is deprecated." |
| 508 | " Use `typed-array?' instead."); |
| 509 | |
| 510 | return scm_typed_array_p (obj, prototype_to_type (prot)); |
| 511 | } |
| 512 | else |
| 513 | return scm_from_bool (scm_is_array (obj)); |
| 514 | } |
| 515 | #undef FUNC_NAME |
| 516 | |
| 517 | #else /* !SCM_ENABLE_DEPRECATED */ |
| 518 | |
| 519 | /* We keep the old 2-argument C prototype for a while although the old |
| 520 | PROT argument is always ignored now. C code should probably use |
| 521 | scm_is_array or scm_is_typed_array anyway. |
| 522 | */ |
| 523 | |
| 524 | static SCM scm_i_array_p (SCM obj); |
| 525 | |
| 526 | SCM_DEFINE (scm_i_array_p, "array?", 1, 0, 0, |
| 527 | (SCM obj), |
| 528 | "Return @code{#t} if the @var{obj} is an array, and @code{#f} if\n" |
| 529 | "not.") |
| 530 | #define FUNC_NAME s_scm_i_array_p |
| 531 | { |
| 532 | return scm_from_bool (scm_is_array (obj)); |
| 533 | } |
| 534 | #undef FUNC_NAME |
| 535 | |
| 536 | SCM |
| 537 | scm_array_p (SCM obj, SCM prot) |
| 538 | { |
| 539 | return scm_from_bool (scm_is_array (obj)); |
| 540 | } |
| 541 | |
| 542 | #endif /* !SCM_ENABLE_DEPRECATED */ |
| 543 | |
| 544 | |
| 545 | SCM_DEFINE (scm_typed_array_p, "typed-array?", 2, 0, 0, |
| 546 | (SCM obj, SCM type), |
| 547 | "Return @code{#t} if the @var{obj} is an array of type\n" |
| 548 | "@var{type}, and @code{#f} if not.") |
| 549 | #define FUNC_NAME s_scm_typed_array_p |
| 550 | { |
| 551 | return scm_from_bool (scm_is_typed_array (obj, type)); |
| 552 | } |
| 553 | #undef FUNC_NAME |
| 554 | |
| 555 | size_t |
| 556 | scm_c_array_rank (SCM array) |
| 557 | { |
| 558 | scm_t_array_handle handle; |
| 559 | size_t res; |
| 560 | |
| 561 | scm_array_get_handle (array, &handle); |
| 562 | res = scm_array_handle_rank (&handle); |
| 563 | scm_array_handle_release (&handle); |
| 564 | return res; |
| 565 | } |
| 566 | |
| 567 | SCM_DEFINE (scm_array_rank, "array-rank", 1, 0, 0, |
| 568 | (SCM array), |
| 569 | "Return the number of dimensions of the array @var{array.}\n") |
| 570 | #define FUNC_NAME s_scm_array_rank |
| 571 | { |
| 572 | return scm_from_size_t (scm_c_array_rank (array)); |
| 573 | } |
| 574 | #undef FUNC_NAME |
| 575 | |
| 576 | |
| 577 | SCM_DEFINE (scm_array_dimensions, "array-dimensions", 1, 0, 0, |
| 578 | (SCM ra), |
| 579 | "@code{array-dimensions} is similar to @code{array-shape} but replaces\n" |
| 580 | "elements with a @code{0} minimum with one greater than the maximum. So:\n" |
| 581 | "@lisp\n" |
| 582 | "(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) 5)\n" |
| 583 | "@end lisp") |
| 584 | #define FUNC_NAME s_scm_array_dimensions |
| 585 | { |
| 586 | scm_t_array_handle handle; |
| 587 | scm_t_array_dim *s; |
| 588 | SCM res = SCM_EOL; |
| 589 | size_t k; |
| 590 | |
| 591 | scm_array_get_handle (ra, &handle); |
| 592 | s = scm_array_handle_dims (&handle); |
| 593 | k = scm_array_handle_rank (&handle); |
| 594 | |
| 595 | while (k--) |
| 596 | res = scm_cons (s[k].lbnd |
| 597 | ? scm_cons2 (scm_from_ssize_t (s[k].lbnd), |
| 598 | scm_from_ssize_t (s[k].ubnd), |
| 599 | SCM_EOL) |
| 600 | : scm_from_ssize_t (1 + s[k].ubnd), |
| 601 | res); |
| 602 | |
| 603 | scm_array_handle_release (&handle); |
| 604 | return res; |
| 605 | } |
| 606 | #undef FUNC_NAME |
| 607 | |
| 608 | |
| 609 | SCM_DEFINE (scm_shared_array_root, "shared-array-root", 1, 0, 0, |
| 610 | (SCM ra), |
| 611 | "Return the root vector of a shared array.") |
| 612 | #define FUNC_NAME s_scm_shared_array_root |
| 613 | { |
| 614 | if (SCM_I_ARRAYP (ra) || SCM_I_ENCLOSED_ARRAYP (ra)) |
| 615 | return SCM_I_ARRAY_V (ra); |
| 616 | else if (scm_is_generalized_vector (ra)) |
| 617 | return ra; |
| 618 | scm_wrong_type_arg_msg (NULL, 0, ra, "array"); |
| 619 | } |
| 620 | #undef FUNC_NAME |
| 621 | |
| 622 | |
| 623 | SCM_DEFINE (scm_shared_array_offset, "shared-array-offset", 1, 0, 0, |
| 624 | (SCM ra), |
| 625 | "Return the root vector index of the first element in the array.") |
| 626 | #define FUNC_NAME s_scm_shared_array_offset |
| 627 | { |
| 628 | scm_t_array_handle handle; |
| 629 | SCM res; |
| 630 | |
| 631 | scm_array_get_handle (ra, &handle); |
| 632 | res = scm_from_size_t (handle.base); |
| 633 | scm_array_handle_release (&handle); |
| 634 | return res; |
| 635 | } |
| 636 | #undef FUNC_NAME |
| 637 | |
| 638 | |
| 639 | SCM_DEFINE (scm_shared_array_increments, "shared-array-increments", 1, 0, 0, |
| 640 | (SCM ra), |
| 641 | "For each dimension, return the distance between elements in the root vector.") |
| 642 | #define FUNC_NAME s_scm_shared_array_increments |
| 643 | { |
| 644 | scm_t_array_handle handle; |
| 645 | SCM res = SCM_EOL; |
| 646 | size_t k; |
| 647 | scm_t_array_dim *s; |
| 648 | |
| 649 | scm_array_get_handle (ra, &handle); |
| 650 | k = scm_array_handle_rank (&handle); |
| 651 | s = scm_array_handle_dims (&handle); |
| 652 | while (k--) |
| 653 | res = scm_cons (scm_from_ssize_t (s[k].inc), res); |
| 654 | scm_array_handle_release (&handle); |
| 655 | return res; |
| 656 | } |
| 657 | #undef FUNC_NAME |
| 658 | |
| 659 | ssize_t |
| 660 | scm_array_handle_pos (scm_t_array_handle *h, SCM indices) |
| 661 | { |
| 662 | scm_t_array_dim *s = scm_array_handle_dims (h); |
| 663 | ssize_t pos = 0, i; |
| 664 | size_t k = scm_array_handle_rank (h); |
| 665 | |
| 666 | while (k > 0 && scm_is_pair (indices)) |
| 667 | { |
| 668 | i = scm_to_signed_integer (SCM_CAR (indices), s->lbnd, s->ubnd); |
| 669 | pos += (i - s->lbnd) * s->inc; |
| 670 | k--; |
| 671 | s++; |
| 672 | indices = SCM_CDR (indices); |
| 673 | } |
| 674 | if (k > 0 || !scm_is_null (indices)) |
| 675 | scm_misc_error (NULL, "wrong number of indices, expecting ~a", |
| 676 | scm_list_1 (scm_from_size_t (scm_array_handle_rank (h)))); |
| 677 | return pos; |
| 678 | } |
| 679 | |
| 680 | SCM |
| 681 | scm_i_make_ra (int ndim, int enclosed) |
| 682 | { |
| 683 | scm_t_bits tag = enclosed? scm_i_tc16_enclosed_array : scm_i_tc16_array; |
| 684 | SCM ra; |
| 685 | SCM_NEWSMOB(ra, ((scm_t_bits) ndim << 17) + tag, |
| 686 | scm_gc_malloc ((sizeof (scm_i_t_array) + |
| 687 | ndim * sizeof (scm_t_array_dim)), |
| 688 | "array")); |
| 689 | SCM_I_ARRAY_V (ra) = SCM_BOOL_F; |
| 690 | return ra; |
| 691 | } |
| 692 | |
| 693 | static char s_bad_spec[] = "Bad scm_array dimension"; |
| 694 | |
| 695 | |
| 696 | /* Increments will still need to be set. */ |
| 697 | |
| 698 | static SCM |
| 699 | scm_i_shap2ra (SCM args) |
| 700 | { |
| 701 | scm_t_array_dim *s; |
| 702 | SCM ra, spec, sp; |
| 703 | int ndim = scm_ilength (args); |
| 704 | if (ndim < 0) |
| 705 | scm_misc_error (NULL, s_bad_spec, SCM_EOL); |
| 706 | |
| 707 | ra = scm_i_make_ra (ndim, 0); |
| 708 | SCM_I_ARRAY_BASE (ra) = 0; |
| 709 | s = SCM_I_ARRAY_DIMS (ra); |
| 710 | for (; !scm_is_null (args); s++, args = SCM_CDR (args)) |
| 711 | { |
| 712 | spec = SCM_CAR (args); |
| 713 | if (scm_is_integer (spec)) |
| 714 | { |
| 715 | if (scm_to_long (spec) < 0) |
| 716 | scm_misc_error (NULL, s_bad_spec, SCM_EOL); |
| 717 | s->lbnd = 0; |
| 718 | s->ubnd = scm_to_long (spec) - 1; |
| 719 | s->inc = 1; |
| 720 | } |
| 721 | else |
| 722 | { |
| 723 | if (!scm_is_pair (spec) || !scm_is_integer (SCM_CAR (spec))) |
| 724 | scm_misc_error (NULL, s_bad_spec, SCM_EOL); |
| 725 | s->lbnd = scm_to_long (SCM_CAR (spec)); |
| 726 | sp = SCM_CDR (spec); |
| 727 | if (!scm_is_pair (sp) |
| 728 | || !scm_is_integer (SCM_CAR (sp)) |
| 729 | || !scm_is_null (SCM_CDR (sp))) |
| 730 | scm_misc_error (NULL, s_bad_spec, SCM_EOL); |
| 731 | s->ubnd = scm_to_long (SCM_CAR (sp)); |
| 732 | s->inc = 1; |
| 733 | } |
| 734 | } |
| 735 | return ra; |
| 736 | } |
| 737 | |
| 738 | SCM_DEFINE (scm_make_typed_array, "make-typed-array", 2, 0, 1, |
| 739 | (SCM type, SCM fill, SCM bounds), |
| 740 | "Create and return an array of type @var{type}.") |
| 741 | #define FUNC_NAME s_scm_make_typed_array |
| 742 | { |
| 743 | size_t k, rlen = 1; |
| 744 | scm_t_array_dim *s; |
| 745 | creator_proc *creator; |
| 746 | SCM ra; |
| 747 | |
| 748 | creator = type_to_creator (type); |
| 749 | ra = scm_i_shap2ra (bounds); |
| 750 | SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra); |
| 751 | s = SCM_I_ARRAY_DIMS (ra); |
| 752 | k = SCM_I_ARRAY_NDIM (ra); |
| 753 | |
| 754 | while (k--) |
| 755 | { |
| 756 | s[k].inc = rlen; |
| 757 | SCM_ASSERT_RANGE (1, bounds, s[k].lbnd <= s[k].ubnd + 1); |
| 758 | rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc; |
| 759 | } |
| 760 | |
| 761 | if (scm_is_eq (fill, SCM_UNSPECIFIED)) |
| 762 | fill = SCM_UNDEFINED; |
| 763 | |
| 764 | SCM_I_ARRAY_V (ra) = creator (scm_from_size_t (rlen), fill); |
| 765 | |
| 766 | if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra)) |
| 767 | if (s->ubnd < s->lbnd || (0 == s->lbnd && 1 == s->inc)) |
| 768 | return SCM_I_ARRAY_V (ra); |
| 769 | return ra; |
| 770 | } |
| 771 | #undef FUNC_NAME |
| 772 | |
| 773 | SCM |
| 774 | scm_from_contiguous_typed_array (SCM type, SCM bounds, const void *bytes, |
| 775 | size_t byte_len) |
| 776 | #define FUNC_NAME "scm_from_contiguous_typed_array" |
| 777 | { |
| 778 | size_t k, rlen = 1; |
| 779 | scm_t_array_dim *s; |
| 780 | creator_proc *creator; |
| 781 | SCM ra; |
| 782 | scm_t_array_handle h; |
| 783 | void *base; |
| 784 | size_t sz; |
| 785 | |
| 786 | creator = type_to_creator (type); |
| 787 | ra = scm_i_shap2ra (bounds); |
| 788 | SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra); |
| 789 | s = SCM_I_ARRAY_DIMS (ra); |
| 790 | k = SCM_I_ARRAY_NDIM (ra); |
| 791 | |
| 792 | while (k--) |
| 793 | { |
| 794 | s[k].inc = rlen; |
| 795 | SCM_ASSERT_RANGE (1, bounds, s[k].lbnd <= s[k].ubnd + 1); |
| 796 | rlen = (s[k].ubnd - s[k].lbnd + 1) * s[k].inc; |
| 797 | } |
| 798 | SCM_I_ARRAY_V (ra) = creator (scm_from_size_t (rlen), SCM_UNDEFINED); |
| 799 | |
| 800 | |
| 801 | scm_array_get_handle (ra, &h); |
| 802 | base = scm_array_handle_uniform_writable_elements (&h); |
| 803 | sz = scm_array_handle_uniform_element_size (&h); |
| 804 | scm_array_handle_release (&h); |
| 805 | |
| 806 | if (byte_len % sz) |
| 807 | SCM_MISC_ERROR ("byte length not a multiple of the unit size", SCM_EOL); |
| 808 | if (byte_len / sz != rlen) |
| 809 | SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL); |
| 810 | |
| 811 | memcpy (base, bytes, byte_len); |
| 812 | |
| 813 | if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra)) |
| 814 | if (s->ubnd < s->lbnd || (0 == s->lbnd && 1 == s->inc)) |
| 815 | return SCM_I_ARRAY_V (ra); |
| 816 | return ra; |
| 817 | } |
| 818 | #undef FUNC_NAME |
| 819 | |
| 820 | SCM_DEFINE (scm_make_array, "make-array", 1, 0, 1, |
| 821 | (SCM fill, SCM bounds), |
| 822 | "Create and return an array.") |
| 823 | #define FUNC_NAME s_scm_make_array |
| 824 | { |
| 825 | return scm_make_typed_array (SCM_BOOL_T, fill, bounds); |
| 826 | } |
| 827 | #undef FUNC_NAME |
| 828 | |
| 829 | #if SCM_ENABLE_DEPRECATED |
| 830 | |
| 831 | SCM_DEFINE (scm_dimensions_to_uniform_array, "dimensions->uniform-array", 2, 1, 0, |
| 832 | (SCM dims, SCM prot, SCM fill), |
| 833 | "@deffnx {Scheme Procedure} make-uniform-vector length prototype [fill]\n" |
| 834 | "Create and return a uniform array or vector of type\n" |
| 835 | "corresponding to @var{prototype} with dimensions @var{dims} or\n" |
| 836 | "length @var{length}. If @var{fill} is supplied, it's used to\n" |
| 837 | "fill the array, otherwise @var{prototype} is used.") |
| 838 | #define FUNC_NAME s_scm_dimensions_to_uniform_array |
| 839 | { |
| 840 | scm_c_issue_deprecation_warning |
| 841 | ("`dimensions->uniform-array' is deprecated. " |
| 842 | "Use `make-typed-array' instead."); |
| 843 | |
| 844 | if (scm_is_integer (dims)) |
| 845 | dims = scm_list_1 (dims); |
| 846 | |
| 847 | if (SCM_UNBNDP (fill)) |
| 848 | { |
| 849 | /* Using #\nul as the prototype yields a s8 array, but numeric |
| 850 | arrays can't store characters, so we have to special case this. |
| 851 | */ |
| 852 | if (scm_is_eq (prot, SCM_MAKE_CHAR (0))) |
| 853 | fill = scm_from_int (0); |
| 854 | else |
| 855 | fill = prot; |
| 856 | } |
| 857 | |
| 858 | return scm_make_typed_array (prototype_to_type (prot), fill, dims); |
| 859 | } |
| 860 | #undef FUNC_NAME |
| 861 | |
| 862 | #endif |
| 863 | |
| 864 | static void |
| 865 | scm_i_ra_set_contp (SCM ra) |
| 866 | { |
| 867 | size_t k = SCM_I_ARRAY_NDIM (ra); |
| 868 | if (k) |
| 869 | { |
| 870 | long inc = SCM_I_ARRAY_DIMS (ra)[k - 1].inc; |
| 871 | while (k--) |
| 872 | { |
| 873 | if (inc != SCM_I_ARRAY_DIMS (ra)[k].inc) |
| 874 | { |
| 875 | SCM_CLR_ARRAY_CONTIGUOUS_FLAG (ra); |
| 876 | return; |
| 877 | } |
| 878 | inc *= (SCM_I_ARRAY_DIMS (ra)[k].ubnd |
| 879 | - SCM_I_ARRAY_DIMS (ra)[k].lbnd + 1); |
| 880 | } |
| 881 | } |
| 882 | SCM_SET_ARRAY_CONTIGUOUS_FLAG (ra); |
| 883 | } |
| 884 | |
| 885 | |
| 886 | SCM_DEFINE (scm_make_shared_array, "make-shared-array", 2, 0, 1, |
| 887 | (SCM oldra, SCM mapfunc, SCM dims), |
| 888 | "@code{make-shared-array} can be used to create shared subarrays of other\n" |
| 889 | "arrays. The @var{mapper} is a function that translates coordinates in\n" |
| 890 | "the new array into coordinates in the old array. A @var{mapper} must be\n" |
| 891 | "linear, and its range must stay within the bounds of the old array, but\n" |
| 892 | "it can be otherwise arbitrary. A simple example:\n" |
| 893 | "@lisp\n" |
| 894 | "(define fred (make-array #f 8 8))\n" |
| 895 | "(define freds-diagonal\n" |
| 896 | " (make-shared-array fred (lambda (i) (list i i)) 8))\n" |
| 897 | "(array-set! freds-diagonal 'foo 3)\n" |
| 898 | "(array-ref fred 3 3) @result{} foo\n" |
| 899 | "(define freds-center\n" |
| 900 | " (make-shared-array fred (lambda (i j) (list (+ 3 i) (+ 3 j))) 2 2))\n" |
| 901 | "(array-ref freds-center 0 0) @result{} foo\n" |
| 902 | "@end lisp") |
| 903 | #define FUNC_NAME s_scm_make_shared_array |
| 904 | { |
| 905 | scm_t_array_handle old_handle; |
| 906 | SCM ra; |
| 907 | SCM inds, indptr; |
| 908 | SCM imap; |
| 909 | size_t k; |
| 910 | ssize_t i; |
| 911 | long old_base, old_min, new_min, old_max, new_max; |
| 912 | scm_t_array_dim *s; |
| 913 | |
| 914 | SCM_VALIDATE_REST_ARGUMENT (dims); |
| 915 | SCM_VALIDATE_PROC (2, mapfunc); |
| 916 | ra = scm_i_shap2ra (dims); |
| 917 | |
| 918 | scm_array_get_handle (oldra, &old_handle); |
| 919 | |
| 920 | if (SCM_I_ARRAYP (oldra)) |
| 921 | { |
| 922 | SCM_I_ARRAY_V (ra) = SCM_I_ARRAY_V (oldra); |
| 923 | old_base = old_min = old_max = SCM_I_ARRAY_BASE (oldra); |
| 924 | s = scm_array_handle_dims (&old_handle); |
| 925 | k = scm_array_handle_rank (&old_handle); |
| 926 | while (k--) |
| 927 | { |
| 928 | if (s[k].inc > 0) |
| 929 | old_max += (s[k].ubnd - s[k].lbnd) * s[k].inc; |
| 930 | else |
| 931 | old_min += (s[k].ubnd - s[k].lbnd) * s[k].inc; |
| 932 | } |
| 933 | } |
| 934 | else |
| 935 | { |
| 936 | SCM_I_ARRAY_V (ra) = oldra; |
| 937 | old_base = old_min = 0; |
| 938 | old_max = scm_c_generalized_vector_length (oldra) - 1; |
| 939 | } |
| 940 | |
| 941 | inds = SCM_EOL; |
| 942 | s = SCM_I_ARRAY_DIMS (ra); |
| 943 | for (k = 0; k < SCM_I_ARRAY_NDIM (ra); k++) |
| 944 | { |
| 945 | inds = scm_cons (scm_from_long (s[k].lbnd), inds); |
| 946 | if (s[k].ubnd < s[k].lbnd) |
| 947 | { |
| 948 | if (1 == SCM_I_ARRAY_NDIM (ra)) |
| 949 | ra = make_typed_vector (scm_array_type (ra), 0); |
| 950 | else |
| 951 | SCM_I_ARRAY_V (ra) = make_typed_vector (scm_array_type (ra), 0); |
| 952 | scm_array_handle_release (&old_handle); |
| 953 | return ra; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | imap = scm_apply_0 (mapfunc, scm_reverse (inds)); |
| 958 | i = scm_array_handle_pos (&old_handle, imap); |
| 959 | SCM_I_ARRAY_BASE (ra) = new_min = new_max = i + old_base; |
| 960 | indptr = inds; |
| 961 | k = SCM_I_ARRAY_NDIM (ra); |
| 962 | while (k--) |
| 963 | { |
| 964 | if (s[k].ubnd > s[k].lbnd) |
| 965 | { |
| 966 | SCM_SETCAR (indptr, scm_sum (SCM_CAR (indptr), scm_from_int (1))); |
| 967 | imap = scm_apply_0 (mapfunc, scm_reverse (inds)); |
| 968 | s[k].inc = scm_array_handle_pos (&old_handle, imap) - i; |
| 969 | i += s[k].inc; |
| 970 | if (s[k].inc > 0) |
| 971 | new_max += (s[k].ubnd - s[k].lbnd) * s[k].inc; |
| 972 | else |
| 973 | new_min += (s[k].ubnd - s[k].lbnd) * s[k].inc; |
| 974 | } |
| 975 | else |
| 976 | s[k].inc = new_max - new_min + 1; /* contiguous by default */ |
| 977 | indptr = SCM_CDR (indptr); |
| 978 | } |
| 979 | |
| 980 | scm_array_handle_release (&old_handle); |
| 981 | |
| 982 | if (old_min > new_min || old_max < new_max) |
| 983 | SCM_MISC_ERROR ("mapping out of range", SCM_EOL); |
| 984 | if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra)) |
| 985 | { |
| 986 | SCM v = SCM_I_ARRAY_V (ra); |
| 987 | size_t length = scm_c_generalized_vector_length (v); |
| 988 | if (1 == s->inc && 0 == s->lbnd && length == 1 + s->ubnd) |
| 989 | return v; |
| 990 | if (s->ubnd < s->lbnd) |
| 991 | return make_typed_vector (scm_array_type (ra), 0); |
| 992 | } |
| 993 | scm_i_ra_set_contp (ra); |
| 994 | return ra; |
| 995 | } |
| 996 | #undef FUNC_NAME |
| 997 | |
| 998 | |
| 999 | /* args are RA . DIMS */ |
| 1000 | SCM_DEFINE (scm_transpose_array, "transpose-array", 1, 0, 1, |
| 1001 | (SCM ra, SCM args), |
| 1002 | "Return an array sharing contents with @var{array}, but with\n" |
| 1003 | "dimensions arranged in a different order. There must be one\n" |
| 1004 | "@var{dim} argument for each dimension of @var{array}.\n" |
| 1005 | "@var{dim0}, @var{dim1}, @dots{} should be integers between 0\n" |
| 1006 | "and the rank of the array to be returned. Each integer in that\n" |
| 1007 | "range must appear at least once in the argument list.\n" |
| 1008 | "\n" |
| 1009 | "The values of @var{dim0}, @var{dim1}, @dots{} correspond to\n" |
| 1010 | "dimensions in the array to be returned, their positions in the\n" |
| 1011 | "argument list to dimensions of @var{array}. Several @var{dim}s\n" |
| 1012 | "may have the same value, in which case the returned array will\n" |
| 1013 | "have smaller rank than @var{array}.\n" |
| 1014 | "\n" |
| 1015 | "@lisp\n" |
| 1016 | "(transpose-array '#2((a b) (c d)) 1 0) @result{} #2((a c) (b d))\n" |
| 1017 | "(transpose-array '#2((a b) (c d)) 0 0) @result{} #1(a d)\n" |
| 1018 | "(transpose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 1 0) @result{}\n" |
| 1019 | " #2((a 4) (b 5) (c 6))\n" |
| 1020 | "@end lisp") |
| 1021 | #define FUNC_NAME s_scm_transpose_array |
| 1022 | { |
| 1023 | SCM res, vargs; |
| 1024 | scm_t_array_dim *s, *r; |
| 1025 | int ndim, i, k; |
| 1026 | |
| 1027 | SCM_VALIDATE_REST_ARGUMENT (args); |
| 1028 | SCM_ASSERT (SCM_NIMP (ra), ra, SCM_ARG1, FUNC_NAME); |
| 1029 | |
| 1030 | if (scm_is_generalized_vector (ra)) |
| 1031 | { |
| 1032 | /* Make sure that we are called with a single zero as |
| 1033 | arguments. |
| 1034 | */ |
| 1035 | if (scm_is_null (args) || !scm_is_null (SCM_CDR (args))) |
| 1036 | SCM_WRONG_NUM_ARGS (); |
| 1037 | SCM_VALIDATE_INT_COPY (SCM_ARG2, SCM_CAR (args), i); |
| 1038 | SCM_ASSERT_RANGE (SCM_ARG2, SCM_CAR (args), i == 0); |
| 1039 | return ra; |
| 1040 | } |
| 1041 | |
| 1042 | if (SCM_I_ARRAYP (ra) || SCM_I_ENCLOSED_ARRAYP (ra)) |
| 1043 | { |
| 1044 | vargs = scm_vector (args); |
| 1045 | if (SCM_SIMPLE_VECTOR_LENGTH (vargs) != SCM_I_ARRAY_NDIM (ra)) |
| 1046 | SCM_WRONG_NUM_ARGS (); |
| 1047 | ndim = 0; |
| 1048 | for (k = 0; k < SCM_I_ARRAY_NDIM (ra); k++) |
| 1049 | { |
| 1050 | i = scm_to_signed_integer (SCM_SIMPLE_VECTOR_REF (vargs, k), |
| 1051 | 0, SCM_I_ARRAY_NDIM(ra)); |
| 1052 | if (ndim < i) |
| 1053 | ndim = i; |
| 1054 | } |
| 1055 | ndim++; |
| 1056 | res = scm_i_make_ra (ndim, 0); |
| 1057 | SCM_I_ARRAY_V (res) = SCM_I_ARRAY_V (ra); |
| 1058 | SCM_I_ARRAY_BASE (res) = SCM_I_ARRAY_BASE (ra); |
| 1059 | for (k = ndim; k--;) |
| 1060 | { |
| 1061 | SCM_I_ARRAY_DIMS (res)[k].lbnd = 0; |
| 1062 | SCM_I_ARRAY_DIMS (res)[k].ubnd = -1; |
| 1063 | } |
| 1064 | for (k = SCM_I_ARRAY_NDIM (ra); k--;) |
| 1065 | { |
| 1066 | i = scm_to_int (SCM_SIMPLE_VECTOR_REF (vargs, k)); |
| 1067 | s = &(SCM_I_ARRAY_DIMS (ra)[k]); |
| 1068 | r = &(SCM_I_ARRAY_DIMS (res)[i]); |
| 1069 | if (r->ubnd < r->lbnd) |
| 1070 | { |
| 1071 | r->lbnd = s->lbnd; |
| 1072 | r->ubnd = s->ubnd; |
| 1073 | r->inc = s->inc; |
| 1074 | ndim--; |
| 1075 | } |
| 1076 | else |
| 1077 | { |
| 1078 | if (r->ubnd > s->ubnd) |
| 1079 | r->ubnd = s->ubnd; |
| 1080 | if (r->lbnd < s->lbnd) |
| 1081 | { |
| 1082 | SCM_I_ARRAY_BASE (res) += (s->lbnd - r->lbnd) * r->inc; |
| 1083 | r->lbnd = s->lbnd; |
| 1084 | } |
| 1085 | r->inc += s->inc; |
| 1086 | } |
| 1087 | } |
| 1088 | if (ndim > 0) |
| 1089 | SCM_MISC_ERROR ("bad argument list", SCM_EOL); |
| 1090 | scm_i_ra_set_contp (res); |
| 1091 | return res; |
| 1092 | } |
| 1093 | |
| 1094 | scm_wrong_type_arg_msg (NULL, 0, ra, "array"); |
| 1095 | } |
| 1096 | #undef FUNC_NAME |
| 1097 | |
| 1098 | /* args are RA . AXES */ |
| 1099 | SCM_DEFINE (scm_enclose_array, "enclose-array", 1, 0, 1, |
| 1100 | (SCM ra, SCM axes), |
| 1101 | "@var{dim0}, @var{dim1} @dots{} should be nonnegative integers less than\n" |
| 1102 | "the rank of @var{array}. @var{enclose-array} returns an array\n" |
| 1103 | "resembling an array of shared arrays. The dimensions of each shared\n" |
| 1104 | "array are the same as the @var{dim}th dimensions of the original array,\n" |
| 1105 | "the dimensions of the outer array are the same as those of the original\n" |
| 1106 | "array that did not match a @var{dim}.\n\n" |
| 1107 | "An enclosed array is not a general Scheme array. Its elements may not\n" |
| 1108 | "be set using @code{array-set!}. Two references to the same element of\n" |
| 1109 | "an enclosed array will be @code{equal?} but will not in general be\n" |
| 1110 | "@code{eq?}. The value returned by @var{array-prototype} when given an\n" |
| 1111 | "enclosed array is unspecified.\n\n" |
| 1112 | "examples:\n" |
| 1113 | "@lisp\n" |
| 1114 | "(enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1) @result{}\n" |
| 1115 | " #<enclosed-array (#1(a d) #1(b e) #1(c f)) (#1(1 4) #1(2 5) #1(3 6))>\n\n" |
| 1116 | "(enclose-array '#3(((a b c) (d e f)) ((1 2 3) (4 5 6))) 1 0) @result{}\n" |
| 1117 | " #<enclosed-array #2((a 1) (d 4)) #2((b 2) (e 5)) #2((c 3) (f 6))>\n" |
| 1118 | "@end lisp") |
| 1119 | #define FUNC_NAME s_scm_enclose_array |
| 1120 | { |
| 1121 | SCM axv, res, ra_inr; |
| 1122 | const char *c_axv; |
| 1123 | scm_t_array_dim vdim, *s = &vdim; |
| 1124 | int ndim, j, k, ninr, noutr; |
| 1125 | |
| 1126 | SCM_VALIDATE_REST_ARGUMENT (axes); |
| 1127 | if (scm_is_null (axes)) |
| 1128 | axes = scm_cons ((SCM_I_ARRAYP (ra) ? scm_from_size_t (SCM_I_ARRAY_NDIM (ra) - 1) : SCM_INUM0), SCM_EOL); |
| 1129 | ninr = scm_ilength (axes); |
| 1130 | if (ninr < 0) |
| 1131 | SCM_WRONG_NUM_ARGS (); |
| 1132 | ra_inr = scm_i_make_ra (ninr, 0); |
| 1133 | |
| 1134 | if (scm_is_generalized_vector (ra)) |
| 1135 | { |
| 1136 | s->lbnd = 0; |
| 1137 | s->ubnd = scm_c_generalized_vector_length (ra) - 1; |
| 1138 | s->inc = 1; |
| 1139 | SCM_I_ARRAY_V (ra_inr) = ra; |
| 1140 | SCM_I_ARRAY_BASE (ra_inr) = 0; |
| 1141 | ndim = 1; |
| 1142 | } |
| 1143 | else if (SCM_I_ARRAYP (ra)) |
| 1144 | { |
| 1145 | s = SCM_I_ARRAY_DIMS (ra); |
| 1146 | SCM_I_ARRAY_V (ra_inr) = SCM_I_ARRAY_V (ra); |
| 1147 | SCM_I_ARRAY_BASE (ra_inr) = SCM_I_ARRAY_BASE (ra); |
| 1148 | ndim = SCM_I_ARRAY_NDIM (ra); |
| 1149 | } |
| 1150 | else |
| 1151 | scm_wrong_type_arg_msg (NULL, 0, ra, "array"); |
| 1152 | |
| 1153 | noutr = ndim - ninr; |
| 1154 | if (noutr < 0) |
| 1155 | SCM_WRONG_NUM_ARGS (); |
| 1156 | axv = scm_make_string (scm_from_int (ndim), SCM_MAKE_CHAR (0)); |
| 1157 | res = scm_i_make_ra (noutr, 1); |
| 1158 | SCM_I_ARRAY_BASE (res) = SCM_I_ARRAY_BASE (ra_inr); |
| 1159 | SCM_I_ARRAY_V (res) = ra_inr; |
| 1160 | for (k = 0; k < ninr; k++, axes = SCM_CDR (axes)) |
| 1161 | { |
| 1162 | if (!scm_is_integer (SCM_CAR (axes))) |
| 1163 | SCM_MISC_ERROR ("bad axis", SCM_EOL); |
| 1164 | j = scm_to_int (SCM_CAR (axes)); |
| 1165 | SCM_I_ARRAY_DIMS (ra_inr)[k].lbnd = s[j].lbnd; |
| 1166 | SCM_I_ARRAY_DIMS (ra_inr)[k].ubnd = s[j].ubnd; |
| 1167 | SCM_I_ARRAY_DIMS (ra_inr)[k].inc = s[j].inc; |
| 1168 | scm_c_string_set_x (axv, j, SCM_MAKE_CHAR (1)); |
| 1169 | } |
| 1170 | c_axv = scm_i_string_chars (axv); |
| 1171 | for (j = 0, k = 0; k < noutr; k++, j++) |
| 1172 | { |
| 1173 | while (c_axv[j]) |
| 1174 | j++; |
| 1175 | SCM_I_ARRAY_DIMS (res)[k].lbnd = s[j].lbnd; |
| 1176 | SCM_I_ARRAY_DIMS (res)[k].ubnd = s[j].ubnd; |
| 1177 | SCM_I_ARRAY_DIMS (res)[k].inc = s[j].inc; |
| 1178 | } |
| 1179 | scm_remember_upto_here_1 (axv); |
| 1180 | scm_i_ra_set_contp (ra_inr); |
| 1181 | scm_i_ra_set_contp (res); |
| 1182 | return res; |
| 1183 | } |
| 1184 | #undef FUNC_NAME |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | SCM_DEFINE (scm_array_in_bounds_p, "array-in-bounds?", 1, 0, 1, |
| 1189 | (SCM v, SCM args), |
| 1190 | "Return @code{#t} if its arguments would be acceptable to\n" |
| 1191 | "@code{array-ref}.") |
| 1192 | #define FUNC_NAME s_scm_array_in_bounds_p |
| 1193 | { |
| 1194 | SCM res = SCM_BOOL_T; |
| 1195 | |
| 1196 | SCM_VALIDATE_REST_ARGUMENT (args); |
| 1197 | |
| 1198 | if (SCM_I_ARRAYP (v) || SCM_I_ENCLOSED_ARRAYP (v)) |
| 1199 | { |
| 1200 | size_t k, ndim = SCM_I_ARRAY_NDIM (v); |
| 1201 | scm_t_array_dim *s = SCM_I_ARRAY_DIMS (v); |
| 1202 | |
| 1203 | for (k = 0; k < ndim; k++) |
| 1204 | { |
| 1205 | long ind; |
| 1206 | |
| 1207 | if (!scm_is_pair (args)) |
| 1208 | SCM_WRONG_NUM_ARGS (); |
| 1209 | ind = scm_to_long (SCM_CAR (args)); |
| 1210 | args = SCM_CDR (args); |
| 1211 | |
| 1212 | if (ind < s[k].lbnd || ind > s[k].ubnd) |
| 1213 | { |
| 1214 | res = SCM_BOOL_F; |
| 1215 | /* We do not stop the checking after finding a violation |
| 1216 | since we want to validate the type-correctness and |
| 1217 | number of arguments in any case. |
| 1218 | */ |
| 1219 | } |
| 1220 | } |
| 1221 | } |
| 1222 | else if (scm_is_generalized_vector (v)) |
| 1223 | { |
| 1224 | /* Since real arrays have been covered above, all generalized |
| 1225 | vectors are guaranteed to be zero-origin here. |
| 1226 | */ |
| 1227 | |
| 1228 | long ind; |
| 1229 | |
| 1230 | if (!scm_is_pair (args)) |
| 1231 | SCM_WRONG_NUM_ARGS (); |
| 1232 | ind = scm_to_long (SCM_CAR (args)); |
| 1233 | args = SCM_CDR (args); |
| 1234 | res = scm_from_bool (ind >= 0 |
| 1235 | && ind < scm_c_generalized_vector_length (v)); |
| 1236 | } |
| 1237 | else |
| 1238 | scm_wrong_type_arg_msg (NULL, 0, v, "array"); |
| 1239 | |
| 1240 | if (!scm_is_null (args)) |
| 1241 | SCM_WRONG_NUM_ARGS (); |
| 1242 | |
| 1243 | return res; |
| 1244 | } |
| 1245 | #undef FUNC_NAME |
| 1246 | |
| 1247 | SCM |
| 1248 | scm_i_cvref (SCM v, size_t pos, int enclosed) |
| 1249 | { |
| 1250 | if (enclosed) |
| 1251 | { |
| 1252 | int k = SCM_I_ARRAY_NDIM (v); |
| 1253 | SCM res = scm_i_make_ra (k, 0); |
| 1254 | SCM_I_ARRAY_V (res) = SCM_I_ARRAY_V (v); |
| 1255 | SCM_I_ARRAY_BASE (res) = pos; |
| 1256 | while (k--) |
| 1257 | { |
| 1258 | SCM_I_ARRAY_DIMS (res)[k].ubnd = SCM_I_ARRAY_DIMS (v)[k].ubnd; |
| 1259 | SCM_I_ARRAY_DIMS (res)[k].lbnd = SCM_I_ARRAY_DIMS (v)[k].lbnd; |
| 1260 | SCM_I_ARRAY_DIMS (res)[k].inc = SCM_I_ARRAY_DIMS (v)[k].inc; |
| 1261 | } |
| 1262 | return res; |
| 1263 | } |
| 1264 | else |
| 1265 | return scm_c_generalized_vector_ref (v, pos); |
| 1266 | } |
| 1267 | |
| 1268 | SCM_DEFINE (scm_array_ref, "array-ref", 1, 0, 1, |
| 1269 | (SCM v, SCM args), |
| 1270 | "Return the element at the @code{(index1, index2)} element in\n" |
| 1271 | "@var{array}.") |
| 1272 | #define FUNC_NAME s_scm_array_ref |
| 1273 | { |
| 1274 | scm_t_array_handle handle; |
| 1275 | SCM res; |
| 1276 | |
| 1277 | scm_array_get_handle (v, &handle); |
| 1278 | res = scm_array_handle_ref (&handle, scm_array_handle_pos (&handle, args)); |
| 1279 | scm_array_handle_release (&handle); |
| 1280 | return res; |
| 1281 | } |
| 1282 | #undef FUNC_NAME |
| 1283 | |
| 1284 | |
| 1285 | SCM_DEFINE (scm_array_set_x, "array-set!", 2, 0, 1, |
| 1286 | (SCM v, SCM obj, SCM args), |
| 1287 | "Set the element at the @code{(index1, index2)} element in @var{array} to\n" |
| 1288 | "@var{new-value}. The value returned by array-set! is unspecified.") |
| 1289 | #define FUNC_NAME s_scm_array_set_x |
| 1290 | { |
| 1291 | scm_t_array_handle handle; |
| 1292 | |
| 1293 | scm_array_get_handle (v, &handle); |
| 1294 | scm_array_handle_set (&handle, scm_array_handle_pos (&handle, args), obj); |
| 1295 | scm_array_handle_release (&handle); |
| 1296 | return SCM_UNSPECIFIED; |
| 1297 | } |
| 1298 | #undef FUNC_NAME |
| 1299 | |
| 1300 | /* attempts to unroll an array into a one-dimensional array. |
| 1301 | returns the unrolled array or #f if it can't be done. */ |
| 1302 | /* if strict is not SCM_UNDEFINED, return #f if returned array |
| 1303 | wouldn't have contiguous elements. */ |
| 1304 | SCM_DEFINE (scm_array_contents, "array-contents", 1, 1, 0, |
| 1305 | (SCM ra, SCM strict), |
| 1306 | "If @var{array} may be @dfn{unrolled} into a one dimensional shared array\n" |
| 1307 | "without changing their order (last subscript changing fastest), then\n" |
| 1308 | "@code{array-contents} returns that shared array, otherwise it returns\n" |
| 1309 | "@code{#f}. All arrays made by @var{make-array} and\n" |
| 1310 | "@var{make-uniform-array} may be unrolled, some arrays made by\n" |
| 1311 | "@var{make-shared-array} may not be.\n\n" |
| 1312 | "If the optional argument @var{strict} is provided, a shared array will\n" |
| 1313 | "be returned only if its elements are stored internally contiguous in\n" |
| 1314 | "memory.") |
| 1315 | #define FUNC_NAME s_scm_array_contents |
| 1316 | { |
| 1317 | SCM sra; |
| 1318 | |
| 1319 | if (scm_is_generalized_vector (ra)) |
| 1320 | return ra; |
| 1321 | |
| 1322 | if (SCM_I_ARRAYP (ra)) |
| 1323 | { |
| 1324 | size_t k, ndim = SCM_I_ARRAY_NDIM (ra), len = 1; |
| 1325 | if (!SCM_I_ARRAYP (ra) || !SCM_I_ARRAY_CONTP (ra)) |
| 1326 | return SCM_BOOL_F; |
| 1327 | for (k = 0; k < ndim; k++) |
| 1328 | len *= SCM_I_ARRAY_DIMS (ra)[k].ubnd - SCM_I_ARRAY_DIMS (ra)[k].lbnd + 1; |
| 1329 | if (!SCM_UNBNDP (strict)) |
| 1330 | { |
| 1331 | if (ndim && (1 != SCM_I_ARRAY_DIMS (ra)[ndim - 1].inc)) |
| 1332 | return SCM_BOOL_F; |
| 1333 | if (scm_is_bitvector (SCM_I_ARRAY_V (ra))) |
| 1334 | { |
| 1335 | if (len != scm_c_bitvector_length (SCM_I_ARRAY_V (ra)) || |
| 1336 | SCM_I_ARRAY_BASE (ra) % SCM_LONG_BIT || |
| 1337 | len % SCM_LONG_BIT) |
| 1338 | return SCM_BOOL_F; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | { |
| 1343 | SCM v = SCM_I_ARRAY_V (ra); |
| 1344 | size_t length = scm_c_generalized_vector_length (v); |
| 1345 | if ((len == length) && 0 == SCM_I_ARRAY_BASE (ra) && SCM_I_ARRAY_DIMS (ra)->inc) |
| 1346 | return v; |
| 1347 | } |
| 1348 | |
| 1349 | sra = scm_i_make_ra (1, 0); |
| 1350 | SCM_I_ARRAY_DIMS (sra)->lbnd = 0; |
| 1351 | SCM_I_ARRAY_DIMS (sra)->ubnd = len - 1; |
| 1352 | SCM_I_ARRAY_V (sra) = SCM_I_ARRAY_V (ra); |
| 1353 | SCM_I_ARRAY_BASE (sra) = SCM_I_ARRAY_BASE (ra); |
| 1354 | SCM_I_ARRAY_DIMS (sra)->inc = (ndim ? SCM_I_ARRAY_DIMS (ra)[ndim - 1].inc : 1); |
| 1355 | return sra; |
| 1356 | } |
| 1357 | else if (SCM_I_ENCLOSED_ARRAYP (ra)) |
| 1358 | scm_wrong_type_arg_msg (NULL, 0, ra, "non-enclosed array"); |
| 1359 | else |
| 1360 | scm_wrong_type_arg_msg (NULL, 0, ra, "array"); |
| 1361 | } |
| 1362 | #undef FUNC_NAME |
| 1363 | |
| 1364 | |
| 1365 | SCM |
| 1366 | scm_ra2contig (SCM ra, int copy) |
| 1367 | { |
| 1368 | SCM ret; |
| 1369 | long inc = 1; |
| 1370 | size_t k, len = 1; |
| 1371 | for (k = SCM_I_ARRAY_NDIM (ra); k--;) |
| 1372 | len *= SCM_I_ARRAY_DIMS (ra)[k].ubnd - SCM_I_ARRAY_DIMS (ra)[k].lbnd + 1; |
| 1373 | k = SCM_I_ARRAY_NDIM (ra); |
| 1374 | if (SCM_I_ARRAY_CONTP (ra) && ((0 == k) || (1 == SCM_I_ARRAY_DIMS (ra)[k - 1].inc))) |
| 1375 | { |
| 1376 | if (!scm_is_bitvector (SCM_I_ARRAY_V (ra))) |
| 1377 | return ra; |
| 1378 | if ((len == scm_c_bitvector_length (SCM_I_ARRAY_V (ra)) && |
| 1379 | 0 == SCM_I_ARRAY_BASE (ra) % SCM_LONG_BIT && |
| 1380 | 0 == len % SCM_LONG_BIT)) |
| 1381 | return ra; |
| 1382 | } |
| 1383 | ret = scm_i_make_ra (k, 0); |
| 1384 | SCM_I_ARRAY_BASE (ret) = 0; |
| 1385 | while (k--) |
| 1386 | { |
| 1387 | SCM_I_ARRAY_DIMS (ret)[k].lbnd = SCM_I_ARRAY_DIMS (ra)[k].lbnd; |
| 1388 | SCM_I_ARRAY_DIMS (ret)[k].ubnd = SCM_I_ARRAY_DIMS (ra)[k].ubnd; |
| 1389 | SCM_I_ARRAY_DIMS (ret)[k].inc = inc; |
| 1390 | inc *= SCM_I_ARRAY_DIMS (ra)[k].ubnd - SCM_I_ARRAY_DIMS (ra)[k].lbnd + 1; |
| 1391 | } |
| 1392 | SCM_I_ARRAY_V (ret) = make_typed_vector (scm_array_type (ra), inc); |
| 1393 | if (copy) |
| 1394 | scm_array_copy_x (ra, ret); |
| 1395 | return ret; |
| 1396 | } |
| 1397 | |
| 1398 | |
| 1399 | |
| 1400 | SCM_DEFINE (scm_uniform_array_read_x, "uniform-array-read!", 1, 3, 0, |
| 1401 | (SCM ura, SCM port_or_fd, SCM start, SCM end), |
| 1402 | "@deffnx {Scheme Procedure} uniform-vector-read! uve [port-or-fdes] [start] [end]\n" |
| 1403 | "Attempt to read all elements of @var{ura}, in lexicographic order, as\n" |
| 1404 | "binary objects from @var{port-or-fdes}.\n" |
| 1405 | "If an end of file is encountered,\n" |
| 1406 | "the objects up to that point are put into @var{ura}\n" |
| 1407 | "(starting at the beginning) and the remainder of the array is\n" |
| 1408 | "unchanged.\n\n" |
| 1409 | "The optional arguments @var{start} and @var{end} allow\n" |
| 1410 | "a specified region of a vector (or linearized array) to be read,\n" |
| 1411 | "leaving the remainder of the vector unchanged.\n\n" |
| 1412 | "@code{uniform-array-read!} returns the number of objects read.\n" |
| 1413 | "@var{port-or-fdes} may be omitted, in which case it defaults to the value\n" |
| 1414 | "returned by @code{(current-input-port)}.") |
| 1415 | #define FUNC_NAME s_scm_uniform_array_read_x |
| 1416 | { |
| 1417 | if (SCM_UNBNDP (port_or_fd)) |
| 1418 | port_or_fd = scm_current_input_port (); |
| 1419 | |
| 1420 | if (scm_is_uniform_vector (ura)) |
| 1421 | { |
| 1422 | return scm_uniform_vector_read_x (ura, port_or_fd, start, end); |
| 1423 | } |
| 1424 | else if (SCM_I_ARRAYP (ura)) |
| 1425 | { |
| 1426 | size_t base, vlen, cstart, cend; |
| 1427 | SCM cra, ans; |
| 1428 | |
| 1429 | cra = scm_ra2contig (ura, 0); |
| 1430 | base = SCM_I_ARRAY_BASE (cra); |
| 1431 | vlen = SCM_I_ARRAY_DIMS (cra)->inc * |
| 1432 | (SCM_I_ARRAY_DIMS (cra)->ubnd - SCM_I_ARRAY_DIMS (cra)->lbnd + 1); |
| 1433 | |
| 1434 | cstart = 0; |
| 1435 | cend = vlen; |
| 1436 | if (!SCM_UNBNDP (start)) |
| 1437 | { |
| 1438 | cstart = scm_to_unsigned_integer (start, 0, vlen); |
| 1439 | if (!SCM_UNBNDP (end)) |
| 1440 | cend = scm_to_unsigned_integer (end, cstart, vlen); |
| 1441 | } |
| 1442 | |
| 1443 | ans = scm_uniform_vector_read_x (SCM_I_ARRAY_V (cra), port_or_fd, |
| 1444 | scm_from_size_t (base + cstart), |
| 1445 | scm_from_size_t (base + cend)); |
| 1446 | |
| 1447 | if (!scm_is_eq (cra, ura)) |
| 1448 | scm_array_copy_x (cra, ura); |
| 1449 | return ans; |
| 1450 | } |
| 1451 | else if (SCM_I_ENCLOSED_ARRAYP (ura)) |
| 1452 | scm_wrong_type_arg_msg (NULL, 0, ura, "non-enclosed array"); |
| 1453 | else |
| 1454 | scm_wrong_type_arg_msg (NULL, 0, ura, "array"); |
| 1455 | } |
| 1456 | #undef FUNC_NAME |
| 1457 | |
| 1458 | SCM_DEFINE (scm_uniform_array_write, "uniform-array-write", 1, 3, 0, |
| 1459 | (SCM ura, SCM port_or_fd, SCM start, SCM end), |
| 1460 | "Writes all elements of @var{ura} as binary objects to\n" |
| 1461 | "@var{port-or-fdes}.\n\n" |
| 1462 | "The optional arguments @var{start}\n" |
| 1463 | "and @var{end} allow\n" |
| 1464 | "a specified region of a vector (or linearized array) to be written.\n\n" |
| 1465 | "The number of objects actually written is returned.\n" |
| 1466 | "@var{port-or-fdes} may be\n" |
| 1467 | "omitted, in which case it defaults to the value returned by\n" |
| 1468 | "@code{(current-output-port)}.") |
| 1469 | #define FUNC_NAME s_scm_uniform_array_write |
| 1470 | { |
| 1471 | if (SCM_UNBNDP (port_or_fd)) |
| 1472 | port_or_fd = scm_current_output_port (); |
| 1473 | |
| 1474 | if (scm_is_uniform_vector (ura)) |
| 1475 | { |
| 1476 | return scm_uniform_vector_write (ura, port_or_fd, start, end); |
| 1477 | } |
| 1478 | else if (SCM_I_ARRAYP (ura)) |
| 1479 | { |
| 1480 | size_t base, vlen, cstart, cend; |
| 1481 | SCM cra, ans; |
| 1482 | |
| 1483 | cra = scm_ra2contig (ura, 1); |
| 1484 | base = SCM_I_ARRAY_BASE (cra); |
| 1485 | vlen = SCM_I_ARRAY_DIMS (cra)->inc * |
| 1486 | (SCM_I_ARRAY_DIMS (cra)->ubnd - SCM_I_ARRAY_DIMS (cra)->lbnd + 1); |
| 1487 | |
| 1488 | cstart = 0; |
| 1489 | cend = vlen; |
| 1490 | if (!SCM_UNBNDP (start)) |
| 1491 | { |
| 1492 | cstart = scm_to_unsigned_integer (start, 0, vlen); |
| 1493 | if (!SCM_UNBNDP (end)) |
| 1494 | cend = scm_to_unsigned_integer (end, cstart, vlen); |
| 1495 | } |
| 1496 | |
| 1497 | ans = scm_uniform_vector_write (SCM_I_ARRAY_V (cra), port_or_fd, |
| 1498 | scm_from_size_t (base + cstart), |
| 1499 | scm_from_size_t (base + cend)); |
| 1500 | |
| 1501 | return ans; |
| 1502 | } |
| 1503 | else if (SCM_I_ENCLOSED_ARRAYP (ura)) |
| 1504 | scm_wrong_type_arg_msg (NULL, 0, ura, "non-enclosed array"); |
| 1505 | else |
| 1506 | scm_wrong_type_arg_msg (NULL, 0, ura, "array"); |
| 1507 | } |
| 1508 | #undef FUNC_NAME |
| 1509 | |
| 1510 | |
| 1511 | /** Bit vectors */ |
| 1512 | |
| 1513 | static scm_t_bits scm_tc16_bitvector; |
| 1514 | |
| 1515 | #define IS_BITVECTOR(obj) SCM_SMOB_PREDICATE(scm_tc16_bitvector,(obj)) |
| 1516 | #define BITVECTOR_BITS(obj) ((scm_t_uint32 *)SCM_SMOB_DATA(obj)) |
| 1517 | #define BITVECTOR_LENGTH(obj) ((size_t)SCM_SMOB_DATA_2(obj)) |
| 1518 | |
| 1519 | static size_t |
| 1520 | bitvector_free (SCM vec) |
| 1521 | { |
| 1522 | scm_gc_free (BITVECTOR_BITS (vec), |
| 1523 | sizeof (scm_t_uint32) * ((BITVECTOR_LENGTH (vec)+31)/32), |
| 1524 | "bitvector"); |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | static int |
| 1529 | bitvector_print (SCM vec, SCM port, scm_print_state *pstate) |
| 1530 | { |
| 1531 | size_t bit_len = BITVECTOR_LENGTH (vec); |
| 1532 | size_t word_len = (bit_len+31)/32; |
| 1533 | scm_t_uint32 *bits = BITVECTOR_BITS (vec); |
| 1534 | size_t i, j; |
| 1535 | |
| 1536 | scm_puts ("#*", port); |
| 1537 | for (i = 0; i < word_len; i++, bit_len -= 32) |
| 1538 | { |
| 1539 | scm_t_uint32 mask = 1; |
| 1540 | for (j = 0; j < 32 && j < bit_len; j++, mask <<= 1) |
| 1541 | scm_putc ((bits[i] & mask)? '1' : '0', port); |
| 1542 | } |
| 1543 | |
| 1544 | return 1; |
| 1545 | } |
| 1546 | |
| 1547 | static SCM |
| 1548 | bitvector_equalp (SCM vec1, SCM vec2) |
| 1549 | { |
| 1550 | size_t bit_len = BITVECTOR_LENGTH (vec1); |
| 1551 | size_t word_len = (bit_len + 31) / 32; |
| 1552 | scm_t_uint32 last_mask = ((scm_t_uint32)-1) >> (32*word_len - bit_len); |
| 1553 | scm_t_uint32 *bits1 = BITVECTOR_BITS (vec1); |
| 1554 | scm_t_uint32 *bits2 = BITVECTOR_BITS (vec2); |
| 1555 | |
| 1556 | /* compare lengths */ |
| 1557 | if (BITVECTOR_LENGTH (vec2) != bit_len) |
| 1558 | return SCM_BOOL_F; |
| 1559 | /* avoid underflow in word_len-1 below. */ |
| 1560 | if (bit_len == 0) |
| 1561 | return SCM_BOOL_T; |
| 1562 | /* compare full words */ |
| 1563 | if (memcmp (bits1, bits2, sizeof (scm_t_uint32) * (word_len-1))) |
| 1564 | return SCM_BOOL_F; |
| 1565 | /* compare partial last words */ |
| 1566 | if ((bits1[word_len-1] & last_mask) != (bits2[word_len-1] & last_mask)) |
| 1567 | return SCM_BOOL_F; |
| 1568 | return SCM_BOOL_T; |
| 1569 | } |
| 1570 | |
| 1571 | int |
| 1572 | scm_is_bitvector (SCM vec) |
| 1573 | { |
| 1574 | return IS_BITVECTOR (vec); |
| 1575 | } |
| 1576 | |
| 1577 | SCM_DEFINE (scm_bitvector_p, "bitvector?", 1, 0, 0, |
| 1578 | (SCM obj), |
| 1579 | "Return @code{#t} when @var{obj} is a bitvector, else\n" |
| 1580 | "return @code{#f}.") |
| 1581 | #define FUNC_NAME s_scm_bitvector_p |
| 1582 | { |
| 1583 | return scm_from_bool (scm_is_bitvector (obj)); |
| 1584 | } |
| 1585 | #undef FUNC_NAME |
| 1586 | |
| 1587 | SCM |
| 1588 | scm_c_make_bitvector (size_t len, SCM fill) |
| 1589 | { |
| 1590 | size_t word_len = (len + 31) / 32; |
| 1591 | scm_t_uint32 *bits; |
| 1592 | SCM res; |
| 1593 | |
| 1594 | bits = scm_gc_malloc (sizeof (scm_t_uint32) * word_len, |
| 1595 | "bitvector"); |
| 1596 | SCM_NEWSMOB2 (res, scm_tc16_bitvector, bits, len); |
| 1597 | |
| 1598 | if (!SCM_UNBNDP (fill)) |
| 1599 | scm_bitvector_fill_x (res, fill); |
| 1600 | |
| 1601 | return res; |
| 1602 | } |
| 1603 | |
| 1604 | SCM_DEFINE (scm_make_bitvector, "make-bitvector", 1, 1, 0, |
| 1605 | (SCM len, SCM fill), |
| 1606 | "Create a new bitvector of length @var{len} and\n" |
| 1607 | "optionally initialize all elements to @var{fill}.") |
| 1608 | #define FUNC_NAME s_scm_make_bitvector |
| 1609 | { |
| 1610 | return scm_c_make_bitvector (scm_to_size_t (len), fill); |
| 1611 | } |
| 1612 | #undef FUNC_NAME |
| 1613 | |
| 1614 | SCM_DEFINE (scm_bitvector, "bitvector", 0, 0, 1, |
| 1615 | (SCM bits), |
| 1616 | "Create a new bitvector with the arguments as elements.") |
| 1617 | #define FUNC_NAME s_scm_bitvector |
| 1618 | { |
| 1619 | return scm_list_to_bitvector (bits); |
| 1620 | } |
| 1621 | #undef FUNC_NAME |
| 1622 | |
| 1623 | size_t |
| 1624 | scm_c_bitvector_length (SCM vec) |
| 1625 | { |
| 1626 | scm_assert_smob_type (scm_tc16_bitvector, vec); |
| 1627 | return BITVECTOR_LENGTH (vec); |
| 1628 | } |
| 1629 | |
| 1630 | SCM_DEFINE (scm_bitvector_length, "bitvector-length", 1, 0, 0, |
| 1631 | (SCM vec), |
| 1632 | "Return the length of the bitvector @var{vec}.") |
| 1633 | #define FUNC_NAME s_scm_bitvector_length |
| 1634 | { |
| 1635 | return scm_from_size_t (scm_c_bitvector_length (vec)); |
| 1636 | } |
| 1637 | #undef FUNC_NAME |
| 1638 | |
| 1639 | const scm_t_uint32 * |
| 1640 | scm_array_handle_bit_elements (scm_t_array_handle *h) |
| 1641 | { |
| 1642 | return scm_array_handle_bit_writable_elements (h); |
| 1643 | } |
| 1644 | |
| 1645 | scm_t_uint32 * |
| 1646 | scm_array_handle_bit_writable_elements (scm_t_array_handle *h) |
| 1647 | { |
| 1648 | SCM vec = h->array; |
| 1649 | if (SCM_I_ARRAYP (vec)) |
| 1650 | vec = SCM_I_ARRAY_V (vec); |
| 1651 | if (IS_BITVECTOR (vec)) |
| 1652 | return BITVECTOR_BITS (vec) + h->base/32; |
| 1653 | scm_wrong_type_arg_msg (NULL, 0, h->array, "bit array"); |
| 1654 | } |
| 1655 | |
| 1656 | size_t |
| 1657 | scm_array_handle_bit_elements_offset (scm_t_array_handle *h) |
| 1658 | { |
| 1659 | return h->base % 32; |
| 1660 | } |
| 1661 | |
| 1662 | const scm_t_uint32 * |
| 1663 | scm_bitvector_elements (SCM vec, |
| 1664 | scm_t_array_handle *h, |
| 1665 | size_t *offp, |
| 1666 | size_t *lenp, |
| 1667 | ssize_t *incp) |
| 1668 | { |
| 1669 | return scm_bitvector_writable_elements (vec, h, offp, lenp, incp); |
| 1670 | } |
| 1671 | |
| 1672 | |
| 1673 | scm_t_uint32 * |
| 1674 | scm_bitvector_writable_elements (SCM vec, |
| 1675 | scm_t_array_handle *h, |
| 1676 | size_t *offp, |
| 1677 | size_t *lenp, |
| 1678 | ssize_t *incp) |
| 1679 | { |
| 1680 | scm_generalized_vector_get_handle (vec, h); |
| 1681 | if (offp) |
| 1682 | { |
| 1683 | scm_t_array_dim *dim = scm_array_handle_dims (h); |
| 1684 | *offp = scm_array_handle_bit_elements_offset (h); |
| 1685 | *lenp = dim->ubnd - dim->lbnd + 1; |
| 1686 | *incp = dim->inc; |
| 1687 | } |
| 1688 | return scm_array_handle_bit_writable_elements (h); |
| 1689 | } |
| 1690 | |
| 1691 | SCM |
| 1692 | scm_c_bitvector_ref (SCM vec, size_t idx) |
| 1693 | { |
| 1694 | scm_t_array_handle handle; |
| 1695 | const scm_t_uint32 *bits; |
| 1696 | |
| 1697 | if (IS_BITVECTOR (vec)) |
| 1698 | { |
| 1699 | if (idx >= BITVECTOR_LENGTH (vec)) |
| 1700 | scm_out_of_range (NULL, scm_from_size_t (idx)); |
| 1701 | bits = BITVECTOR_BITS(vec); |
| 1702 | return scm_from_bool (bits[idx/32] & (1L << (idx%32))); |
| 1703 | } |
| 1704 | else |
| 1705 | { |
| 1706 | SCM res; |
| 1707 | size_t len, off; |
| 1708 | ssize_t inc; |
| 1709 | |
| 1710 | bits = scm_bitvector_elements (vec, &handle, &off, &len, &inc); |
| 1711 | if (idx >= len) |
| 1712 | scm_out_of_range (NULL, scm_from_size_t (idx)); |
| 1713 | idx = idx*inc + off; |
| 1714 | res = scm_from_bool (bits[idx/32] & (1L << (idx%32))); |
| 1715 | scm_array_handle_release (&handle); |
| 1716 | return res; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | SCM_DEFINE (scm_bitvector_ref, "bitvector-ref", 2, 0, 0, |
| 1721 | (SCM vec, SCM idx), |
| 1722 | "Return the element at index @var{idx} of the bitvector\n" |
| 1723 | "@var{vec}.") |
| 1724 | #define FUNC_NAME s_scm_bitvector_ref |
| 1725 | { |
| 1726 | return scm_c_bitvector_ref (vec, scm_to_size_t (idx)); |
| 1727 | } |
| 1728 | #undef FUNC_NAME |
| 1729 | |
| 1730 | void |
| 1731 | scm_c_bitvector_set_x (SCM vec, size_t idx, SCM val) |
| 1732 | { |
| 1733 | scm_t_array_handle handle; |
| 1734 | scm_t_uint32 *bits, mask; |
| 1735 | |
| 1736 | if (IS_BITVECTOR (vec)) |
| 1737 | { |
| 1738 | if (idx >= BITVECTOR_LENGTH (vec)) |
| 1739 | scm_out_of_range (NULL, scm_from_size_t (idx)); |
| 1740 | bits = BITVECTOR_BITS(vec); |
| 1741 | } |
| 1742 | else |
| 1743 | { |
| 1744 | size_t len, off; |
| 1745 | ssize_t inc; |
| 1746 | |
| 1747 | bits = scm_bitvector_writable_elements (vec, &handle, &off, &len, &inc); |
| 1748 | if (idx >= len) |
| 1749 | scm_out_of_range (NULL, scm_from_size_t (idx)); |
| 1750 | idx = idx*inc + off; |
| 1751 | } |
| 1752 | |
| 1753 | mask = 1L << (idx%32); |
| 1754 | if (scm_is_true (val)) |
| 1755 | bits[idx/32] |= mask; |
| 1756 | else |
| 1757 | bits[idx/32] &= ~mask; |
| 1758 | |
| 1759 | if (!IS_BITVECTOR (vec)) |
| 1760 | scm_array_handle_release (&handle); |
| 1761 | } |
| 1762 | |
| 1763 | SCM_DEFINE (scm_bitvector_set_x, "bitvector-set!", 3, 0, 0, |
| 1764 | (SCM vec, SCM idx, SCM val), |
| 1765 | "Set the element at index @var{idx} of the bitvector\n" |
| 1766 | "@var{vec} when @var{val} is true, else clear it.") |
| 1767 | #define FUNC_NAME s_scm_bitvector_set_x |
| 1768 | { |
| 1769 | scm_c_bitvector_set_x (vec, scm_to_size_t (idx), val); |
| 1770 | return SCM_UNSPECIFIED; |
| 1771 | } |
| 1772 | #undef FUNC_NAME |
| 1773 | |
| 1774 | SCM_DEFINE (scm_bitvector_fill_x, "bitvector-fill!", 2, 0, 0, |
| 1775 | (SCM vec, SCM val), |
| 1776 | "Set all elements of the bitvector\n" |
| 1777 | "@var{vec} when @var{val} is true, else clear them.") |
| 1778 | #define FUNC_NAME s_scm_bitvector_fill_x |
| 1779 | { |
| 1780 | scm_t_array_handle handle; |
| 1781 | size_t off, len; |
| 1782 | ssize_t inc; |
| 1783 | scm_t_uint32 *bits; |
| 1784 | |
| 1785 | bits = scm_bitvector_writable_elements (vec, &handle, |
| 1786 | &off, &len, &inc); |
| 1787 | |
| 1788 | if (off == 0 && inc == 1 && len > 0) |
| 1789 | { |
| 1790 | /* the usual case |
| 1791 | */ |
| 1792 | size_t word_len = (len + 31) / 32; |
| 1793 | scm_t_uint32 last_mask = ((scm_t_uint32)-1) >> (32*word_len - len); |
| 1794 | |
| 1795 | if (scm_is_true (val)) |
| 1796 | { |
| 1797 | memset (bits, 0xFF, sizeof(scm_t_uint32)*(word_len-1)); |
| 1798 | bits[word_len-1] |= last_mask; |
| 1799 | } |
| 1800 | else |
| 1801 | { |
| 1802 | memset (bits, 0x00, sizeof(scm_t_uint32)*(word_len-1)); |
| 1803 | bits[word_len-1] &= ~last_mask; |
| 1804 | } |
| 1805 | } |
| 1806 | else |
| 1807 | { |
| 1808 | size_t i; |
| 1809 | for (i = 0; i < len; i++) |
| 1810 | scm_array_handle_set (&handle, i*inc, val); |
| 1811 | } |
| 1812 | |
| 1813 | scm_array_handle_release (&handle); |
| 1814 | |
| 1815 | return SCM_UNSPECIFIED; |
| 1816 | } |
| 1817 | #undef FUNC_NAME |
| 1818 | |
| 1819 | SCM_DEFINE (scm_list_to_bitvector, "list->bitvector", 1, 0, 0, |
| 1820 | (SCM list), |
| 1821 | "Return a new bitvector initialized with the elements\n" |
| 1822 | "of @var{list}.") |
| 1823 | #define FUNC_NAME s_scm_list_to_bitvector |
| 1824 | { |
| 1825 | size_t bit_len = scm_to_size_t (scm_length (list)); |
| 1826 | SCM vec = scm_c_make_bitvector (bit_len, SCM_UNDEFINED); |
| 1827 | size_t word_len = (bit_len+31)/32; |
| 1828 | scm_t_array_handle handle; |
| 1829 | scm_t_uint32 *bits = scm_bitvector_writable_elements (vec, &handle, |
| 1830 | NULL, NULL, NULL); |
| 1831 | size_t i, j; |
| 1832 | |
| 1833 | for (i = 0; i < word_len && scm_is_pair (list); i++, bit_len -= 32) |
| 1834 | { |
| 1835 | scm_t_uint32 mask = 1; |
| 1836 | bits[i] = 0; |
| 1837 | for (j = 0; j < 32 && j < bit_len; |
| 1838 | j++, mask <<= 1, list = SCM_CDR (list)) |
| 1839 | if (scm_is_true (SCM_CAR (list))) |
| 1840 | bits[i] |= mask; |
| 1841 | } |
| 1842 | |
| 1843 | scm_array_handle_release (&handle); |
| 1844 | |
| 1845 | return vec; |
| 1846 | } |
| 1847 | #undef FUNC_NAME |
| 1848 | |
| 1849 | SCM_DEFINE (scm_bitvector_to_list, "bitvector->list", 1, 0, 0, |
| 1850 | (SCM vec), |
| 1851 | "Return a new list initialized with the elements\n" |
| 1852 | "of the bitvector @var{vec}.") |
| 1853 | #define FUNC_NAME s_scm_bitvector_to_list |
| 1854 | { |
| 1855 | scm_t_array_handle handle; |
| 1856 | size_t off, len; |
| 1857 | ssize_t inc; |
| 1858 | scm_t_uint32 *bits; |
| 1859 | SCM res = SCM_EOL; |
| 1860 | |
| 1861 | bits = scm_bitvector_writable_elements (vec, &handle, |
| 1862 | &off, &len, &inc); |
| 1863 | |
| 1864 | if (off == 0 && inc == 1) |
| 1865 | { |
| 1866 | /* the usual case |
| 1867 | */ |
| 1868 | size_t word_len = (len + 31) / 32; |
| 1869 | size_t i, j; |
| 1870 | |
| 1871 | for (i = 0; i < word_len; i++, len -= 32) |
| 1872 | { |
| 1873 | scm_t_uint32 mask = 1; |
| 1874 | for (j = 0; j < 32 && j < len; j++, mask <<= 1) |
| 1875 | res = scm_cons ((bits[i] & mask)? SCM_BOOL_T : SCM_BOOL_F, res); |
| 1876 | } |
| 1877 | } |
| 1878 | else |
| 1879 | { |
| 1880 | size_t i; |
| 1881 | for (i = 0; i < len; i++) |
| 1882 | res = scm_cons (scm_array_handle_ref (&handle, i*inc), res); |
| 1883 | } |
| 1884 | |
| 1885 | scm_array_handle_release (&handle); |
| 1886 | |
| 1887 | return scm_reverse_x (res, SCM_EOL); |
| 1888 | } |
| 1889 | #undef FUNC_NAME |
| 1890 | |
| 1891 | /* From mmix-arith.w by Knuth. |
| 1892 | |
| 1893 | Here's a fun way to count the number of bits in a tetrabyte. |
| 1894 | |
| 1895 | [This classical trick is called the ``Gillies--Miller method for |
| 1896 | sideways addition'' in {\sl The Preparation of Programs for an |
| 1897 | Electronic Digital Computer\/} by Wilkes, Wheeler, and Gill, second |
| 1898 | edition (Reading, Mass.:\ Addison--Wesley, 1957), 191--193. Some of |
| 1899 | the tricks used here were suggested by Balbir Singh, Peter |
| 1900 | Rossmanith, and Stefan Schwoon.] |
| 1901 | */ |
| 1902 | |
| 1903 | static size_t |
| 1904 | count_ones (scm_t_uint32 x) |
| 1905 | { |
| 1906 | x=x-((x>>1)&0x55555555); |
| 1907 | x=(x&0x33333333)+((x>>2)&0x33333333); |
| 1908 | x=(x+(x>>4))&0x0f0f0f0f; |
| 1909 | x=x+(x>>8); |
| 1910 | return (x+(x>>16)) & 0xff; |
| 1911 | } |
| 1912 | |
| 1913 | SCM_DEFINE (scm_bit_count, "bit-count", 2, 0, 0, |
| 1914 | (SCM b, SCM bitvector), |
| 1915 | "Return the number of occurrences of the boolean @var{b} in\n" |
| 1916 | "@var{bitvector}.") |
| 1917 | #define FUNC_NAME s_scm_bit_count |
| 1918 | { |
| 1919 | scm_t_array_handle handle; |
| 1920 | size_t off, len; |
| 1921 | ssize_t inc; |
| 1922 | scm_t_uint32 *bits; |
| 1923 | int bit = scm_to_bool (b); |
| 1924 | size_t count = 0; |
| 1925 | |
| 1926 | bits = scm_bitvector_writable_elements (bitvector, &handle, |
| 1927 | &off, &len, &inc); |
| 1928 | |
| 1929 | if (off == 0 && inc == 1 && len > 0) |
| 1930 | { |
| 1931 | /* the usual case |
| 1932 | */ |
| 1933 | size_t word_len = (len + 31) / 32; |
| 1934 | scm_t_uint32 last_mask = ((scm_t_uint32)-1) >> (32*word_len - len); |
| 1935 | size_t i; |
| 1936 | |
| 1937 | for (i = 0; i < word_len-1; i++) |
| 1938 | count += count_ones (bits[i]); |
| 1939 | count += count_ones (bits[i] & last_mask); |
| 1940 | } |
| 1941 | else |
| 1942 | { |
| 1943 | size_t i; |
| 1944 | for (i = 0; i < len; i++) |
| 1945 | if (scm_is_true (scm_array_handle_ref (&handle, i*inc))) |
| 1946 | count++; |
| 1947 | } |
| 1948 | |
| 1949 | scm_array_handle_release (&handle); |
| 1950 | |
| 1951 | return scm_from_size_t (bit? count : len-count); |
| 1952 | } |
| 1953 | #undef FUNC_NAME |
| 1954 | |
| 1955 | /* returns 32 for x == 0. |
| 1956 | */ |
| 1957 | static size_t |
| 1958 | find_first_one (scm_t_uint32 x) |
| 1959 | { |
| 1960 | size_t pos = 0; |
| 1961 | /* do a binary search in x. */ |
| 1962 | if ((x & 0xFFFF) == 0) |
| 1963 | x >>= 16, pos += 16; |
| 1964 | if ((x & 0xFF) == 0) |
| 1965 | x >>= 8, pos += 8; |
| 1966 | if ((x & 0xF) == 0) |
| 1967 | x >>= 4, pos += 4; |
| 1968 | if ((x & 0x3) == 0) |
| 1969 | x >>= 2, pos += 2; |
| 1970 | if ((x & 0x1) == 0) |
| 1971 | pos += 1; |
| 1972 | return pos; |
| 1973 | } |
| 1974 | |
| 1975 | SCM_DEFINE (scm_bit_position, "bit-position", 3, 0, 0, |
| 1976 | (SCM item, SCM v, SCM k), |
| 1977 | "Return the index of the first occurrance of @var{item} in bit\n" |
| 1978 | "vector @var{v}, starting from @var{k}. If there is no\n" |
| 1979 | "@var{item} entry between @var{k} and the end of\n" |
| 1980 | "@var{bitvector}, then return @code{#f}. For example,\n" |
| 1981 | "\n" |
| 1982 | "@example\n" |
| 1983 | "(bit-position #t #*000101 0) @result{} 3\n" |
| 1984 | "(bit-position #f #*0001111 3) @result{} #f\n" |
| 1985 | "@end example") |
| 1986 | #define FUNC_NAME s_scm_bit_position |
| 1987 | { |
| 1988 | scm_t_array_handle handle; |
| 1989 | size_t off, len, first_bit; |
| 1990 | ssize_t inc; |
| 1991 | const scm_t_uint32 *bits; |
| 1992 | int bit = scm_to_bool (item); |
| 1993 | SCM res = SCM_BOOL_F; |
| 1994 | |
| 1995 | bits = scm_bitvector_elements (v, &handle, &off, &len, &inc); |
| 1996 | first_bit = scm_to_unsigned_integer (k, 0, len); |
| 1997 | |
| 1998 | if (off == 0 && inc == 1 && len > 0) |
| 1999 | { |
| 2000 | size_t i, word_len = (len + 31) / 32; |
| 2001 | scm_t_uint32 last_mask = ((scm_t_uint32)-1) >> (32*word_len - len); |
| 2002 | size_t first_word = first_bit / 32; |
| 2003 | scm_t_uint32 first_mask = |
| 2004 | ((scm_t_uint32)-1) << (first_bit - 32*first_word); |
| 2005 | scm_t_uint32 w; |
| 2006 | |
| 2007 | for (i = first_word; i < word_len; i++) |
| 2008 | { |
| 2009 | w = (bit? bits[i] : ~bits[i]); |
| 2010 | if (i == first_word) |
| 2011 | w &= first_mask; |
| 2012 | if (i == word_len-1) |
| 2013 | w &= last_mask; |
| 2014 | if (w) |
| 2015 | { |
| 2016 | res = scm_from_size_t (32*i + find_first_one (w)); |
| 2017 | break; |
| 2018 | } |
| 2019 | } |
| 2020 | } |
| 2021 | else |
| 2022 | { |
| 2023 | size_t i; |
| 2024 | for (i = first_bit; i < len; i++) |
| 2025 | { |
| 2026 | SCM elt = scm_array_handle_ref (&handle, i*inc); |
| 2027 | if ((bit && scm_is_true (elt)) || (!bit && scm_is_false (elt))) |
| 2028 | { |
| 2029 | res = scm_from_size_t (i); |
| 2030 | break; |
| 2031 | } |
| 2032 | } |
| 2033 | } |
| 2034 | |
| 2035 | scm_array_handle_release (&handle); |
| 2036 | |
| 2037 | return res; |
| 2038 | } |
| 2039 | #undef FUNC_NAME |
| 2040 | |
| 2041 | SCM_DEFINE (scm_bit_set_star_x, "bit-set*!", 3, 0, 0, |
| 2042 | (SCM v, SCM kv, SCM obj), |
| 2043 | "Set entries of bit vector @var{v} to @var{obj}, with @var{kv}\n" |
| 2044 | "selecting the entries to change. The return value is\n" |
| 2045 | "unspecified.\n" |
| 2046 | "\n" |
| 2047 | "If @var{kv} is a bit vector, then those entries where it has\n" |
| 2048 | "@code{#t} are the ones in @var{v} which are set to @var{obj}.\n" |
| 2049 | "@var{kv} and @var{v} must be the same length. When @var{obj}\n" |
| 2050 | "is @code{#t} it's like @var{kv} is OR'ed into @var{v}. Or when\n" |
| 2051 | "@var{obj} is @code{#f} it can be seen as an ANDNOT.\n" |
| 2052 | "\n" |
| 2053 | "@example\n" |
| 2054 | "(define bv #*01000010)\n" |
| 2055 | "(bit-set*! bv #*10010001 #t)\n" |
| 2056 | "bv\n" |
| 2057 | "@result{} #*11010011\n" |
| 2058 | "@end example\n" |
| 2059 | "\n" |
| 2060 | "If @var{kv} is a u32vector, then its elements are\n" |
| 2061 | "indices into @var{v} which are set to @var{obj}.\n" |
| 2062 | "\n" |
| 2063 | "@example\n" |
| 2064 | "(define bv #*01000010)\n" |
| 2065 | "(bit-set*! bv #u32(5 2 7) #t)\n" |
| 2066 | "bv\n" |
| 2067 | "@result{} #*01100111\n" |
| 2068 | "@end example") |
| 2069 | #define FUNC_NAME s_scm_bit_set_star_x |
| 2070 | { |
| 2071 | scm_t_array_handle v_handle; |
| 2072 | size_t v_off, v_len; |
| 2073 | ssize_t v_inc; |
| 2074 | scm_t_uint32 *v_bits; |
| 2075 | int bit; |
| 2076 | |
| 2077 | /* Validate that OBJ is a boolean so this is done even if we don't |
| 2078 | need BIT. |
| 2079 | */ |
| 2080 | bit = scm_to_bool (obj); |
| 2081 | |
| 2082 | v_bits = scm_bitvector_writable_elements (v, &v_handle, |
| 2083 | &v_off, &v_len, &v_inc); |
| 2084 | |
| 2085 | if (scm_is_bitvector (kv)) |
| 2086 | { |
| 2087 | scm_t_array_handle kv_handle; |
| 2088 | size_t kv_off, kv_len; |
| 2089 | ssize_t kv_inc; |
| 2090 | const scm_t_uint32 *kv_bits; |
| 2091 | |
| 2092 | kv_bits = scm_bitvector_elements (v, &kv_handle, |
| 2093 | &kv_off, &kv_len, &kv_inc); |
| 2094 | |
| 2095 | if (v_len != kv_len) |
| 2096 | scm_misc_error (NULL, |
| 2097 | "bit vectors must have equal length", |
| 2098 | SCM_EOL); |
| 2099 | |
| 2100 | if (v_off == 0 && v_inc == 1 && kv_off == 0 && kv_inc == 1 && kv_len > 0) |
| 2101 | { |
| 2102 | size_t word_len = (kv_len + 31) / 32; |
| 2103 | scm_t_uint32 last_mask = ((scm_t_uint32)-1) >> (32*word_len - kv_len); |
| 2104 | size_t i; |
| 2105 | |
| 2106 | if (bit == 0) |
| 2107 | { |
| 2108 | for (i = 0; i < word_len-1; i++) |
| 2109 | v_bits[i] &= ~kv_bits[i]; |
| 2110 | v_bits[i] &= ~(kv_bits[i] & last_mask); |
| 2111 | } |
| 2112 | else |
| 2113 | { |
| 2114 | for (i = 0; i < word_len-1; i++) |
| 2115 | v_bits[i] |= kv_bits[i]; |
| 2116 | v_bits[i] |= kv_bits[i] & last_mask; |
| 2117 | } |
| 2118 | } |
| 2119 | else |
| 2120 | { |
| 2121 | size_t i; |
| 2122 | for (i = 0; i < kv_len; i++) |
| 2123 | if (scm_is_true (scm_array_handle_ref (&kv_handle, i*kv_inc))) |
| 2124 | scm_array_handle_set (&v_handle, i*v_inc, obj); |
| 2125 | } |
| 2126 | |
| 2127 | scm_array_handle_release (&kv_handle); |
| 2128 | |
| 2129 | } |
| 2130 | else if (scm_is_true (scm_u32vector_p (kv))) |
| 2131 | { |
| 2132 | scm_t_array_handle kv_handle; |
| 2133 | size_t i, kv_len; |
| 2134 | ssize_t kv_inc; |
| 2135 | const scm_t_uint32 *kv_elts; |
| 2136 | |
| 2137 | kv_elts = scm_u32vector_elements (kv, &kv_handle, &kv_len, &kv_inc); |
| 2138 | for (i = 0; i < kv_len; i++, kv_elts += kv_inc) |
| 2139 | scm_array_handle_set (&v_handle, (*kv_elts)*v_inc, obj); |
| 2140 | |
| 2141 | scm_array_handle_release (&kv_handle); |
| 2142 | } |
| 2143 | else |
| 2144 | scm_wrong_type_arg_msg (NULL, 0, kv, "bitvector or u32vector"); |
| 2145 | |
| 2146 | scm_array_handle_release (&v_handle); |
| 2147 | |
| 2148 | return SCM_UNSPECIFIED; |
| 2149 | } |
| 2150 | #undef FUNC_NAME |
| 2151 | |
| 2152 | |
| 2153 | SCM_DEFINE (scm_bit_count_star, "bit-count*", 3, 0, 0, |
| 2154 | (SCM v, SCM kv, SCM obj), |
| 2155 | "Return a count of how many entries in bit vector @var{v} are\n" |
| 2156 | "equal to @var{obj}, with @var{kv} selecting the entries to\n" |
| 2157 | "consider.\n" |
| 2158 | "\n" |
| 2159 | "If @var{kv} is a bit vector, then those entries where it has\n" |
| 2160 | "@code{#t} are the ones in @var{v} which are considered.\n" |
| 2161 | "@var{kv} and @var{v} must be the same length.\n" |
| 2162 | "\n" |
| 2163 | "If @var{kv} is a u32vector, then it contains\n" |
| 2164 | "the indexes in @var{v} to consider.\n" |
| 2165 | "\n" |
| 2166 | "For example,\n" |
| 2167 | "\n" |
| 2168 | "@example\n" |
| 2169 | "(bit-count* #*01110111 #*11001101 #t) @result{} 3\n" |
| 2170 | "(bit-count* #*01110111 #u32(7 0 4) #f) @result{} 2\n" |
| 2171 | "@end example") |
| 2172 | #define FUNC_NAME s_scm_bit_count_star |
| 2173 | { |
| 2174 | scm_t_array_handle v_handle; |
| 2175 | size_t v_off, v_len; |
| 2176 | ssize_t v_inc; |
| 2177 | const scm_t_uint32 *v_bits; |
| 2178 | size_t count = 0; |
| 2179 | int bit; |
| 2180 | |
| 2181 | /* Validate that OBJ is a boolean so this is done even if we don't |
| 2182 | need BIT. |
| 2183 | */ |
| 2184 | bit = scm_to_bool (obj); |
| 2185 | |
| 2186 | v_bits = scm_bitvector_elements (v, &v_handle, |
| 2187 | &v_off, &v_len, &v_inc); |
| 2188 | |
| 2189 | if (scm_is_bitvector (kv)) |
| 2190 | { |
| 2191 | scm_t_array_handle kv_handle; |
| 2192 | size_t kv_off, kv_len; |
| 2193 | ssize_t kv_inc; |
| 2194 | const scm_t_uint32 *kv_bits; |
| 2195 | |
| 2196 | kv_bits = scm_bitvector_elements (v, &kv_handle, |
| 2197 | &kv_off, &kv_len, &kv_inc); |
| 2198 | |
| 2199 | if (v_len != kv_len) |
| 2200 | scm_misc_error (NULL, |
| 2201 | "bit vectors must have equal length", |
| 2202 | SCM_EOL); |
| 2203 | |
| 2204 | if (v_off == 0 && v_inc == 1 && kv_off == 0 && kv_inc == 1 && kv_len > 0) |
| 2205 | { |
| 2206 | size_t i, word_len = (kv_len + 31) / 32; |
| 2207 | scm_t_uint32 last_mask = ((scm_t_uint32)-1) >> (32*word_len - kv_len); |
| 2208 | scm_t_uint32 xor_mask = bit? 0 : ((scm_t_uint32)-1); |
| 2209 | |
| 2210 | for (i = 0; i < word_len-1; i++) |
| 2211 | count += count_ones ((v_bits[i]^xor_mask) & kv_bits[i]); |
| 2212 | count += count_ones ((v_bits[i]^xor_mask) & kv_bits[i] & last_mask); |
| 2213 | } |
| 2214 | else |
| 2215 | { |
| 2216 | size_t i; |
| 2217 | for (i = 0; i < kv_len; i++) |
| 2218 | if (scm_is_true (scm_array_handle_ref (&kv_handle, i))) |
| 2219 | { |
| 2220 | SCM elt = scm_array_handle_ref (&v_handle, i*v_inc); |
| 2221 | if ((bit && scm_is_true (elt)) || (!bit && scm_is_false (elt))) |
| 2222 | count++; |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | scm_array_handle_release (&kv_handle); |
| 2227 | |
| 2228 | } |
| 2229 | else if (scm_is_true (scm_u32vector_p (kv))) |
| 2230 | { |
| 2231 | scm_t_array_handle kv_handle; |
| 2232 | size_t i, kv_len; |
| 2233 | ssize_t kv_inc; |
| 2234 | const scm_t_uint32 *kv_elts; |
| 2235 | |
| 2236 | kv_elts = scm_u32vector_elements (kv, &kv_handle, &kv_len, &kv_inc); |
| 2237 | for (i = 0; i < kv_len; i++, kv_elts += kv_inc) |
| 2238 | { |
| 2239 | SCM elt = scm_array_handle_ref (&v_handle, (*kv_elts)*v_inc); |
| 2240 | if ((bit && scm_is_true (elt)) || (!bit && scm_is_false (elt))) |
| 2241 | count++; |
| 2242 | } |
| 2243 | |
| 2244 | scm_array_handle_release (&kv_handle); |
| 2245 | } |
| 2246 | else |
| 2247 | scm_wrong_type_arg_msg (NULL, 0, kv, "bitvector or u32vector"); |
| 2248 | |
| 2249 | scm_array_handle_release (&v_handle); |
| 2250 | |
| 2251 | return scm_from_size_t (count); |
| 2252 | } |
| 2253 | #undef FUNC_NAME |
| 2254 | |
| 2255 | SCM_DEFINE (scm_bit_invert_x, "bit-invert!", 1, 0, 0, |
| 2256 | (SCM v), |
| 2257 | "Modify the bit vector @var{v} by replacing each element with\n" |
| 2258 | "its negation.") |
| 2259 | #define FUNC_NAME s_scm_bit_invert_x |
| 2260 | { |
| 2261 | scm_t_array_handle handle; |
| 2262 | size_t off, len; |
| 2263 | ssize_t inc; |
| 2264 | scm_t_uint32 *bits; |
| 2265 | |
| 2266 | bits = scm_bitvector_writable_elements (v, &handle, &off, &len, &inc); |
| 2267 | |
| 2268 | if (off == 0 && inc == 1 && len > 0) |
| 2269 | { |
| 2270 | size_t word_len = (len + 31) / 32; |
| 2271 | scm_t_uint32 last_mask = ((scm_t_uint32)-1) >> (32*word_len - len); |
| 2272 | size_t i; |
| 2273 | |
| 2274 | for (i = 0; i < word_len-1; i++) |
| 2275 | bits[i] = ~bits[i]; |
| 2276 | bits[i] = bits[i] ^ last_mask; |
| 2277 | } |
| 2278 | else |
| 2279 | { |
| 2280 | size_t i; |
| 2281 | for (i = 0; i < len; i++) |
| 2282 | scm_array_handle_set (&handle, i*inc, |
| 2283 | scm_not (scm_array_handle_ref (&handle, i*inc))); |
| 2284 | } |
| 2285 | |
| 2286 | scm_array_handle_release (&handle); |
| 2287 | |
| 2288 | return SCM_UNSPECIFIED; |
| 2289 | } |
| 2290 | #undef FUNC_NAME |
| 2291 | |
| 2292 | |
| 2293 | SCM |
| 2294 | scm_istr2bve (SCM str) |
| 2295 | { |
| 2296 | scm_t_array_handle handle; |
| 2297 | size_t len = scm_i_string_length (str); |
| 2298 | SCM vec = scm_c_make_bitvector (len, SCM_UNDEFINED); |
| 2299 | SCM res = vec; |
| 2300 | |
| 2301 | scm_t_uint32 mask; |
| 2302 | size_t k, j; |
| 2303 | const char *c_str; |
| 2304 | scm_t_uint32 *data; |
| 2305 | |
| 2306 | data = scm_bitvector_writable_elements (vec, &handle, NULL, NULL, NULL); |
| 2307 | c_str = scm_i_string_chars (str); |
| 2308 | |
| 2309 | for (k = 0; k < (len + 31) / 32; k++) |
| 2310 | { |
| 2311 | data[k] = 0L; |
| 2312 | j = len - k * 32; |
| 2313 | if (j > 32) |
| 2314 | j = 32; |
| 2315 | for (mask = 1L; j--; mask <<= 1) |
| 2316 | switch (*c_str++) |
| 2317 | { |
| 2318 | case '0': |
| 2319 | break; |
| 2320 | case '1': |
| 2321 | data[k] |= mask; |
| 2322 | break; |
| 2323 | default: |
| 2324 | res = SCM_BOOL_F; |
| 2325 | goto exit; |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | exit: |
| 2330 | scm_array_handle_release (&handle); |
| 2331 | scm_remember_upto_here_1 (str); |
| 2332 | return res; |
| 2333 | } |
| 2334 | |
| 2335 | |
| 2336 | |
| 2337 | static SCM |
| 2338 | ra2l (SCM ra, unsigned long base, unsigned long k) |
| 2339 | { |
| 2340 | SCM res = SCM_EOL; |
| 2341 | long inc; |
| 2342 | size_t i; |
| 2343 | int enclosed = SCM_I_ENCLOSED_ARRAYP (ra); |
| 2344 | |
| 2345 | if (k == SCM_I_ARRAY_NDIM (ra)) |
| 2346 | return scm_i_cvref (SCM_I_ARRAY_V (ra), base, enclosed); |
| 2347 | |
| 2348 | inc = SCM_I_ARRAY_DIMS (ra)[k].inc; |
| 2349 | if (SCM_I_ARRAY_DIMS (ra)[k].ubnd < SCM_I_ARRAY_DIMS (ra)[k].lbnd) |
| 2350 | return SCM_EOL; |
| 2351 | i = base + (1 + SCM_I_ARRAY_DIMS (ra)[k].ubnd - SCM_I_ARRAY_DIMS (ra)[k].lbnd) * inc; |
| 2352 | do |
| 2353 | { |
| 2354 | i -= inc; |
| 2355 | res = scm_cons (ra2l (ra, i, k + 1), res); |
| 2356 | } |
| 2357 | while (i != base); |
| 2358 | return res; |
| 2359 | } |
| 2360 | |
| 2361 | |
| 2362 | SCM_DEFINE (scm_array_to_list, "array->list", 1, 0, 0, |
| 2363 | (SCM v), |
| 2364 | "Return a list consisting of all the elements, in order, of\n" |
| 2365 | "@var{array}.") |
| 2366 | #define FUNC_NAME s_scm_array_to_list |
| 2367 | { |
| 2368 | if (scm_is_generalized_vector (v)) |
| 2369 | return scm_generalized_vector_to_list (v); |
| 2370 | else if (SCM_I_ARRAYP (v) || SCM_I_ENCLOSED_ARRAYP (v)) |
| 2371 | return ra2l (v, SCM_I_ARRAY_BASE (v), 0); |
| 2372 | |
| 2373 | scm_wrong_type_arg_msg (NULL, 0, v, "array"); |
| 2374 | } |
| 2375 | #undef FUNC_NAME |
| 2376 | |
| 2377 | |
| 2378 | static void l2ra (SCM lst, scm_t_array_handle *handle, ssize_t pos, size_t k); |
| 2379 | |
| 2380 | SCM_DEFINE (scm_list_to_typed_array, "list->typed-array", 3, 0, 0, |
| 2381 | (SCM type, SCM shape, SCM lst), |
| 2382 | "Return an array of the type @var{type}\n" |
| 2383 | "with elements the same as those of @var{lst}.\n" |
| 2384 | "\n" |
| 2385 | "The argument @var{shape} determines the number of dimensions\n" |
| 2386 | "of the array and their shape. It is either an exact integer,\n" |
| 2387 | "giving the\n" |
| 2388 | "number of dimensions directly, or a list whose length\n" |
| 2389 | "specifies the number of dimensions and each element specified\n" |
| 2390 | "the lower and optionally the upper bound of the corresponding\n" |
| 2391 | "dimension.\n" |
| 2392 | "When the element is list of two elements, these elements\n" |
| 2393 | "give the lower and upper bounds. When it is an exact\n" |
| 2394 | "integer, it gives only the lower bound.") |
| 2395 | #define FUNC_NAME s_scm_list_to_typed_array |
| 2396 | { |
| 2397 | SCM row; |
| 2398 | SCM ra; |
| 2399 | scm_t_array_handle handle; |
| 2400 | |
| 2401 | row = lst; |
| 2402 | if (scm_is_integer (shape)) |
| 2403 | { |
| 2404 | size_t k = scm_to_size_t (shape); |
| 2405 | shape = SCM_EOL; |
| 2406 | while (k-- > 0) |
| 2407 | { |
| 2408 | shape = scm_cons (scm_length (row), shape); |
| 2409 | if (k > 0 && !scm_is_null (row)) |
| 2410 | row = scm_car (row); |
| 2411 | } |
| 2412 | } |
| 2413 | else |
| 2414 | { |
| 2415 | SCM shape_spec = shape; |
| 2416 | shape = SCM_EOL; |
| 2417 | while (1) |
| 2418 | { |
| 2419 | SCM spec = scm_car (shape_spec); |
| 2420 | if (scm_is_pair (spec)) |
| 2421 | shape = scm_cons (spec, shape); |
| 2422 | else |
| 2423 | shape = scm_cons (scm_list_2 (spec, |
| 2424 | scm_sum (scm_sum (spec, |
| 2425 | scm_length (row)), |
| 2426 | scm_from_int (-1))), |
| 2427 | shape); |
| 2428 | shape_spec = scm_cdr (shape_spec); |
| 2429 | if (scm_is_pair (shape_spec)) |
| 2430 | { |
| 2431 | if (!scm_is_null (row)) |
| 2432 | row = scm_car (row); |
| 2433 | } |
| 2434 | else |
| 2435 | break; |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | ra = scm_make_typed_array (type, SCM_UNSPECIFIED, |
| 2440 | scm_reverse_x (shape, SCM_EOL)); |
| 2441 | |
| 2442 | scm_array_get_handle (ra, &handle); |
| 2443 | l2ra (lst, &handle, 0, 0); |
| 2444 | scm_array_handle_release (&handle); |
| 2445 | |
| 2446 | return ra; |
| 2447 | } |
| 2448 | #undef FUNC_NAME |
| 2449 | |
| 2450 | SCM_DEFINE (scm_list_to_array, "list->array", 2, 0, 0, |
| 2451 | (SCM ndim, SCM lst), |
| 2452 | "Return an array with elements the same as those of @var{lst}.") |
| 2453 | #define FUNC_NAME s_scm_list_to_array |
| 2454 | { |
| 2455 | return scm_list_to_typed_array (SCM_BOOL_T, ndim, lst); |
| 2456 | } |
| 2457 | #undef FUNC_NAME |
| 2458 | |
| 2459 | static void |
| 2460 | l2ra (SCM lst, scm_t_array_handle *handle, ssize_t pos, size_t k) |
| 2461 | { |
| 2462 | if (k == scm_array_handle_rank (handle)) |
| 2463 | scm_array_handle_set (handle, pos, lst); |
| 2464 | else |
| 2465 | { |
| 2466 | scm_t_array_dim *dim = scm_array_handle_dims (handle) + k; |
| 2467 | ssize_t inc = dim->inc; |
| 2468 | size_t len = 1 + dim->ubnd - dim->lbnd, n; |
| 2469 | char *errmsg = NULL; |
| 2470 | |
| 2471 | n = len; |
| 2472 | while (n > 0 && scm_is_pair (lst)) |
| 2473 | { |
| 2474 | l2ra (SCM_CAR (lst), handle, pos, k + 1); |
| 2475 | pos += inc; |
| 2476 | lst = SCM_CDR (lst); |
| 2477 | n -= 1; |
| 2478 | } |
| 2479 | if (n != 0) |
| 2480 | errmsg = "too few elements for array dimension ~a, need ~a"; |
| 2481 | if (!scm_is_null (lst)) |
| 2482 | errmsg = "too many elements for array dimension ~a, want ~a"; |
| 2483 | if (errmsg) |
| 2484 | scm_misc_error (NULL, errmsg, scm_list_2 (scm_from_ulong (k), |
| 2485 | scm_from_size_t (len))); |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | #if SCM_ENABLE_DEPRECATED |
| 2490 | |
| 2491 | SCM_DEFINE (scm_list_to_uniform_array, "list->uniform-array", 3, 0, 0, |
| 2492 | (SCM ndim, SCM prot, SCM lst), |
| 2493 | "Return a uniform array of the type indicated by prototype\n" |
| 2494 | "@var{prot} with elements the same as those of @var{lst}.\n" |
| 2495 | "Elements must be of the appropriate type, no coercions are\n" |
| 2496 | "done.\n" |
| 2497 | "\n" |
| 2498 | "The argument @var{ndim} determines the number of dimensions\n" |
| 2499 | "of the array. It is either an exact integer, giving the\n" |
| 2500 | "number directly, or a list of exact integers, whose length\n" |
| 2501 | "specifies the number of dimensions and each element is the\n" |
| 2502 | "lower index bound of its dimension.") |
| 2503 | #define FUNC_NAME s_scm_list_to_uniform_array |
| 2504 | { |
| 2505 | return scm_list_to_typed_array (prototype_to_type (prot), ndim, lst); |
| 2506 | } |
| 2507 | #undef FUNC_NAME |
| 2508 | |
| 2509 | #endif |
| 2510 | |
| 2511 | /* Print dimension DIM of ARRAY. |
| 2512 | */ |
| 2513 | |
| 2514 | static int |
| 2515 | scm_i_print_array_dimension (SCM array, int dim, int base, int enclosed, |
| 2516 | SCM port, scm_print_state *pstate) |
| 2517 | { |
| 2518 | scm_t_array_dim *dim_spec = SCM_I_ARRAY_DIMS (array) + dim; |
| 2519 | long idx; |
| 2520 | |
| 2521 | scm_putc ('(', port); |
| 2522 | |
| 2523 | for (idx = dim_spec->lbnd; idx <= dim_spec->ubnd; idx++) |
| 2524 | { |
| 2525 | if (dim < SCM_I_ARRAY_NDIM(array)-1) |
| 2526 | scm_i_print_array_dimension (array, dim+1, base, enclosed, |
| 2527 | port, pstate); |
| 2528 | else |
| 2529 | scm_iprin1 (scm_i_cvref (SCM_I_ARRAY_V (array), base, enclosed), |
| 2530 | port, pstate); |
| 2531 | if (idx < dim_spec->ubnd) |
| 2532 | scm_putc (' ', port); |
| 2533 | base += dim_spec->inc; |
| 2534 | } |
| 2535 | |
| 2536 | scm_putc (')', port); |
| 2537 | return 1; |
| 2538 | } |
| 2539 | |
| 2540 | /* Print an array. (Only for strict arrays, not for generalized vectors.) |
| 2541 | */ |
| 2542 | |
| 2543 | static int |
| 2544 | scm_i_print_array (SCM array, SCM port, scm_print_state *pstate) |
| 2545 | { |
| 2546 | long ndim = SCM_I_ARRAY_NDIM (array); |
| 2547 | scm_t_array_dim *dim_specs = SCM_I_ARRAY_DIMS (array); |
| 2548 | SCM v = SCM_I_ARRAY_V (array); |
| 2549 | unsigned long base = SCM_I_ARRAY_BASE (array); |
| 2550 | long i; |
| 2551 | int print_lbnds = 0, zero_size = 0, print_lens = 0; |
| 2552 | |
| 2553 | scm_putc ('#', port); |
| 2554 | if (ndim != 1 || dim_specs[0].lbnd != 0) |
| 2555 | scm_intprint (ndim, 10, port); |
| 2556 | if (scm_is_uniform_vector (v)) |
| 2557 | scm_puts (scm_i_uniform_vector_tag (v), port); |
| 2558 | else if (scm_is_bitvector (v)) |
| 2559 | scm_puts ("b", port); |
| 2560 | else if (scm_is_string (v)) |
| 2561 | scm_puts ("a", port); |
| 2562 | else if (!scm_is_vector (v)) |
| 2563 | scm_puts ("?", port); |
| 2564 | |
| 2565 | for (i = 0; i < ndim; i++) |
| 2566 | { |
| 2567 | if (dim_specs[i].lbnd != 0) |
| 2568 | print_lbnds = 1; |
| 2569 | if (dim_specs[i].ubnd - dim_specs[i].lbnd + 1 == 0) |
| 2570 | zero_size = 1; |
| 2571 | else if (zero_size) |
| 2572 | print_lens = 1; |
| 2573 | } |
| 2574 | |
| 2575 | if (print_lbnds || print_lens) |
| 2576 | for (i = 0; i < ndim; i++) |
| 2577 | { |
| 2578 | if (print_lbnds) |
| 2579 | { |
| 2580 | scm_putc ('@', port); |
| 2581 | scm_intprint (dim_specs[i].lbnd, 10, port); |
| 2582 | } |
| 2583 | if (print_lens) |
| 2584 | { |
| 2585 | scm_putc (':', port); |
| 2586 | scm_intprint (dim_specs[i].ubnd - dim_specs[i].lbnd + 1, |
| 2587 | 10, port); |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | if (ndim == 0) |
| 2592 | { |
| 2593 | /* Rank zero arrays, which are really just scalars, are printed |
| 2594 | specially. The consequent way would be to print them as |
| 2595 | |
| 2596 | #0 OBJ |
| 2597 | |
| 2598 | where OBJ is the printed representation of the scalar, but we |
| 2599 | print them instead as |
| 2600 | |
| 2601 | #0(OBJ) |
| 2602 | |
| 2603 | to make them look less strange. |
| 2604 | |
| 2605 | Just printing them as |
| 2606 | |
| 2607 | OBJ |
| 2608 | |
| 2609 | would be correct in a way as well, but zero rank arrays are |
| 2610 | not really the same as Scheme values since they are boxed and |
| 2611 | can be modified with array-set!, say. |
| 2612 | */ |
| 2613 | scm_putc ('(', port); |
| 2614 | scm_iprin1 (scm_i_cvref (v, base, 0), port, pstate); |
| 2615 | scm_putc (')', port); |
| 2616 | return 1; |
| 2617 | } |
| 2618 | else |
| 2619 | return scm_i_print_array_dimension (array, 0, base, 0, port, pstate); |
| 2620 | } |
| 2621 | |
| 2622 | static int |
| 2623 | scm_i_print_enclosed_array (SCM array, SCM port, scm_print_state *pstate) |
| 2624 | { |
| 2625 | size_t base; |
| 2626 | |
| 2627 | scm_putc ('#', port); |
| 2628 | base = SCM_I_ARRAY_BASE (array); |
| 2629 | scm_puts ("<enclosed-array ", port); |
| 2630 | scm_i_print_array_dimension (array, 0, base, 1, port, pstate); |
| 2631 | scm_putc ('>', port); |
| 2632 | return 1; |
| 2633 | } |
| 2634 | |
| 2635 | /* Read an array. This function can also read vectors and uniform |
| 2636 | vectors. Also, the conflict between '#f' and '#f32' and '#f64' is |
| 2637 | handled here. |
| 2638 | |
| 2639 | C is the first character read after the '#'. |
| 2640 | */ |
| 2641 | |
| 2642 | static SCM |
| 2643 | tag_to_type (const char *tag, SCM port) |
| 2644 | { |
| 2645 | #if SCM_ENABLE_DEPRECATED |
| 2646 | { |
| 2647 | /* Recognize the old syntax. |
| 2648 | */ |
| 2649 | const char *instead; |
| 2650 | switch (tag[0]) |
| 2651 | { |
| 2652 | case 'u': |
| 2653 | instead = "u32"; |
| 2654 | break; |
| 2655 | case 'e': |
| 2656 | instead = "s32"; |
| 2657 | break; |
| 2658 | case 's': |
| 2659 | instead = "f32"; |
| 2660 | break; |
| 2661 | case 'i': |
| 2662 | instead = "f64"; |
| 2663 | break; |
| 2664 | case 'y': |
| 2665 | instead = "s8"; |
| 2666 | break; |
| 2667 | case 'h': |
| 2668 | instead = "s16"; |
| 2669 | break; |
| 2670 | case 'l': |
| 2671 | instead = "s64"; |
| 2672 | break; |
| 2673 | case 'c': |
| 2674 | instead = "c64"; |
| 2675 | break; |
| 2676 | default: |
| 2677 | instead = NULL; |
| 2678 | break; |
| 2679 | } |
| 2680 | |
| 2681 | if (instead && tag[1] == '\0') |
| 2682 | { |
| 2683 | scm_c_issue_deprecation_warning_fmt |
| 2684 | ("The tag '%c' is deprecated for uniform vectors. " |
| 2685 | "Use '%s' instead.", tag[0], instead); |
| 2686 | return scm_from_locale_symbol (instead); |
| 2687 | } |
| 2688 | } |
| 2689 | #endif |
| 2690 | |
| 2691 | if (*tag == '\0') |
| 2692 | return SCM_BOOL_T; |
| 2693 | else |
| 2694 | return scm_from_locale_symbol (tag); |
| 2695 | } |
| 2696 | |
| 2697 | static int |
| 2698 | read_decimal_integer (SCM port, int c, ssize_t *resp) |
| 2699 | { |
| 2700 | ssize_t sign = 1; |
| 2701 | ssize_t res = 0; |
| 2702 | int got_it = 0; |
| 2703 | |
| 2704 | if (c == '-') |
| 2705 | { |
| 2706 | sign = -1; |
| 2707 | c = scm_getc (port); |
| 2708 | } |
| 2709 | |
| 2710 | while ('0' <= c && c <= '9') |
| 2711 | { |
| 2712 | res = 10*res + c-'0'; |
| 2713 | got_it = 1; |
| 2714 | c = scm_getc (port); |
| 2715 | } |
| 2716 | |
| 2717 | if (got_it) |
| 2718 | *resp = sign * res; |
| 2719 | return c; |
| 2720 | } |
| 2721 | |
| 2722 | SCM |
| 2723 | scm_i_read_array (SCM port, int c) |
| 2724 | { |
| 2725 | ssize_t rank; |
| 2726 | int got_rank; |
| 2727 | char tag[80]; |
| 2728 | int tag_len; |
| 2729 | |
| 2730 | SCM shape = SCM_BOOL_F, elements; |
| 2731 | |
| 2732 | /* XXX - shortcut for ordinary vectors. Shouldn't be necessary but |
| 2733 | the array code can not deal with zero-length dimensions yet, and |
| 2734 | we want to allow zero-length vectors, of course. |
| 2735 | */ |
| 2736 | if (c == '(') |
| 2737 | { |
| 2738 | scm_ungetc (c, port); |
| 2739 | return scm_vector (scm_read (port)); |
| 2740 | } |
| 2741 | |
| 2742 | /* Disambiguate between '#f' and uniform floating point vectors. |
| 2743 | */ |
| 2744 | if (c == 'f') |
| 2745 | { |
| 2746 | c = scm_getc (port); |
| 2747 | if (c != '3' && c != '6') |
| 2748 | { |
| 2749 | if (c != EOF) |
| 2750 | scm_ungetc (c, port); |
| 2751 | return SCM_BOOL_F; |
| 2752 | } |
| 2753 | rank = 1; |
| 2754 | got_rank = 1; |
| 2755 | tag[0] = 'f'; |
| 2756 | tag_len = 1; |
| 2757 | goto continue_reading_tag; |
| 2758 | } |
| 2759 | |
| 2760 | /* Read rank. |
| 2761 | */ |
| 2762 | rank = 1; |
| 2763 | c = read_decimal_integer (port, c, &rank); |
| 2764 | if (rank < 0) |
| 2765 | scm_i_input_error (NULL, port, "array rank must be non-negative", |
| 2766 | SCM_EOL); |
| 2767 | |
| 2768 | /* Read tag. |
| 2769 | */ |
| 2770 | tag_len = 0; |
| 2771 | continue_reading_tag: |
| 2772 | while (c != EOF && c != '(' && c != '@' && c != ':' && tag_len < 80) |
| 2773 | { |
| 2774 | tag[tag_len++] = c; |
| 2775 | c = scm_getc (port); |
| 2776 | } |
| 2777 | tag[tag_len] = '\0'; |
| 2778 | |
| 2779 | /* Read shape. |
| 2780 | */ |
| 2781 | if (c == '@' || c == ':') |
| 2782 | { |
| 2783 | shape = SCM_EOL; |
| 2784 | |
| 2785 | do |
| 2786 | { |
| 2787 | ssize_t lbnd = 0, len = 0; |
| 2788 | SCM s; |
| 2789 | |
| 2790 | if (c == '@') |
| 2791 | { |
| 2792 | c = scm_getc (port); |
| 2793 | c = read_decimal_integer (port, c, &lbnd); |
| 2794 | } |
| 2795 | |
| 2796 | s = scm_from_ssize_t (lbnd); |
| 2797 | |
| 2798 | if (c == ':') |
| 2799 | { |
| 2800 | c = scm_getc (port); |
| 2801 | c = read_decimal_integer (port, c, &len); |
| 2802 | if (len < 0) |
| 2803 | scm_i_input_error (NULL, port, |
| 2804 | "array length must be non-negative", |
| 2805 | SCM_EOL); |
| 2806 | |
| 2807 | s = scm_list_2 (s, scm_from_ssize_t (lbnd+len-1)); |
| 2808 | } |
| 2809 | |
| 2810 | shape = scm_cons (s, shape); |
| 2811 | } while (c == '@' || c == ':'); |
| 2812 | |
| 2813 | shape = scm_reverse_x (shape, SCM_EOL); |
| 2814 | } |
| 2815 | |
| 2816 | /* Read nested lists of elements. |
| 2817 | */ |
| 2818 | if (c != '(') |
| 2819 | scm_i_input_error (NULL, port, |
| 2820 | "missing '(' in vector or array literal", |
| 2821 | SCM_EOL); |
| 2822 | scm_ungetc (c, port); |
| 2823 | elements = scm_read (port); |
| 2824 | |
| 2825 | if (scm_is_false (shape)) |
| 2826 | shape = scm_from_ssize_t (rank); |
| 2827 | else if (scm_ilength (shape) != rank) |
| 2828 | scm_i_input_error |
| 2829 | (NULL, port, |
| 2830 | "the number of shape specifications must match the array rank", |
| 2831 | SCM_EOL); |
| 2832 | |
| 2833 | /* Handle special print syntax of rank zero arrays; see |
| 2834 | scm_i_print_array for a rationale. |
| 2835 | */ |
| 2836 | if (rank == 0) |
| 2837 | { |
| 2838 | if (!scm_is_pair (elements)) |
| 2839 | scm_i_input_error (NULL, port, |
| 2840 | "too few elements in array literal, need 1", |
| 2841 | SCM_EOL); |
| 2842 | if (!scm_is_null (SCM_CDR (elements))) |
| 2843 | scm_i_input_error (NULL, port, |
| 2844 | "too many elements in array literal, want 1", |
| 2845 | SCM_EOL); |
| 2846 | elements = SCM_CAR (elements); |
| 2847 | } |
| 2848 | |
| 2849 | /* Construct array. |
| 2850 | */ |
| 2851 | return scm_list_to_typed_array (tag_to_type (tag, port), shape, elements); |
| 2852 | } |
| 2853 | |
| 2854 | SCM_DEFINE (scm_array_type, "array-type", 1, 0, 0, |
| 2855 | (SCM ra), |
| 2856 | "") |
| 2857 | #define FUNC_NAME s_scm_array_type |
| 2858 | { |
| 2859 | if (SCM_I_ARRAYP (ra)) |
| 2860 | return scm_i_generalized_vector_type (SCM_I_ARRAY_V (ra)); |
| 2861 | else if (scm_is_generalized_vector (ra)) |
| 2862 | return scm_i_generalized_vector_type (ra); |
| 2863 | else if (SCM_I_ENCLOSED_ARRAYP (ra)) |
| 2864 | scm_wrong_type_arg_msg (NULL, 0, ra, "non-enclosed array"); |
| 2865 | else |
| 2866 | scm_wrong_type_arg_msg (NULL, 0, ra, "array"); |
| 2867 | } |
| 2868 | #undef FUNC_NAME |
| 2869 | |
| 2870 | #if SCM_ENABLE_DEPRECATED |
| 2871 | |
| 2872 | SCM_DEFINE (scm_array_prototype, "array-prototype", 1, 0, 0, |
| 2873 | (SCM ra), |
| 2874 | "Return an object that would produce an array of the same type\n" |
| 2875 | "as @var{array}, if used as the @var{prototype} for\n" |
| 2876 | "@code{make-uniform-array}.") |
| 2877 | #define FUNC_NAME s_scm_array_prototype |
| 2878 | { |
| 2879 | if (SCM_I_ARRAYP (ra)) |
| 2880 | return scm_i_get_old_prototype (SCM_I_ARRAY_V (ra)); |
| 2881 | else if (scm_is_generalized_vector (ra)) |
| 2882 | return scm_i_get_old_prototype (ra); |
| 2883 | else if (SCM_I_ENCLOSED_ARRAYP (ra)) |
| 2884 | return SCM_UNSPECIFIED; |
| 2885 | else |
| 2886 | scm_wrong_type_arg_msg (NULL, 0, ra, "array"); |
| 2887 | } |
| 2888 | #undef FUNC_NAME |
| 2889 | |
| 2890 | #endif |
| 2891 | |
| 2892 | static SCM |
| 2893 | array_mark (SCM ptr) |
| 2894 | { |
| 2895 | return SCM_I_ARRAY_V (ptr); |
| 2896 | } |
| 2897 | |
| 2898 | static size_t |
| 2899 | array_free (SCM ptr) |
| 2900 | { |
| 2901 | scm_gc_free (SCM_I_ARRAY_MEM (ptr), |
| 2902 | (sizeof (scm_i_t_array) |
| 2903 | + SCM_I_ARRAY_NDIM (ptr) * sizeof (scm_t_array_dim)), |
| 2904 | "array"); |
| 2905 | return 0; |
| 2906 | } |
| 2907 | |
| 2908 | #if SCM_ENABLE_DEPRECATED |
| 2909 | |
| 2910 | SCM |
| 2911 | scm_make_ra (int ndim) |
| 2912 | { |
| 2913 | scm_c_issue_deprecation_warning |
| 2914 | ("scm_make_ra is deprecated. Use scm_make_array or similar instead."); |
| 2915 | return scm_i_make_ra (ndim, 0); |
| 2916 | } |
| 2917 | |
| 2918 | SCM |
| 2919 | scm_shap2ra (SCM args, const char *what) |
| 2920 | { |
| 2921 | scm_c_issue_deprecation_warning |
| 2922 | ("scm_shap2ra is deprecated. Use scm_make_array or similar instead."); |
| 2923 | return scm_i_shap2ra (args); |
| 2924 | } |
| 2925 | |
| 2926 | SCM |
| 2927 | scm_cvref (SCM v, unsigned long pos, SCM last) |
| 2928 | { |
| 2929 | scm_c_issue_deprecation_warning |
| 2930 | ("scm_cvref is deprecated. Use scm_c_generalized_vector_ref instead."); |
| 2931 | return scm_c_generalized_vector_ref (v, pos); |
| 2932 | } |
| 2933 | |
| 2934 | void |
| 2935 | scm_ra_set_contp (SCM ra) |
| 2936 | { |
| 2937 | scm_c_issue_deprecation_warning |
| 2938 | ("scm_ra_set_contp is deprecated. There should be no need for it."); |
| 2939 | scm_i_ra_set_contp (ra); |
| 2940 | } |
| 2941 | |
| 2942 | long |
| 2943 | scm_aind (SCM ra, SCM args, const char *what) |
| 2944 | { |
| 2945 | scm_t_array_handle handle; |
| 2946 | ssize_t pos; |
| 2947 | |
| 2948 | scm_c_issue_deprecation_warning |
| 2949 | ("scm_aind is deprecated. Use scm_array_handle_pos instead."); |
| 2950 | |
| 2951 | if (scm_is_integer (args)) |
| 2952 | args = scm_list_1 (args); |
| 2953 | |
| 2954 | scm_array_get_handle (ra, &handle); |
| 2955 | pos = scm_array_handle_pos (&handle, args) + SCM_I_ARRAY_BASE (ra); |
| 2956 | scm_array_handle_release (&handle); |
| 2957 | return pos; |
| 2958 | } |
| 2959 | |
| 2960 | int |
| 2961 | scm_raprin1 (SCM exp, SCM port, scm_print_state *pstate) |
| 2962 | { |
| 2963 | scm_c_issue_deprecation_warning |
| 2964 | ("scm_raprin1 is deprecated. Use scm_display or scm_write instead."); |
| 2965 | |
| 2966 | scm_iprin1 (exp, port, pstate); |
| 2967 | return 1; |
| 2968 | } |
| 2969 | |
| 2970 | #endif |
| 2971 | |
| 2972 | void |
| 2973 | scm_init_unif () |
| 2974 | { |
| 2975 | scm_i_tc16_array = scm_make_smob_type ("array", 0); |
| 2976 | scm_set_smob_mark (scm_i_tc16_array, array_mark); |
| 2977 | scm_set_smob_free (scm_i_tc16_array, array_free); |
| 2978 | scm_set_smob_print (scm_i_tc16_array, scm_i_print_array); |
| 2979 | scm_set_smob_equalp (scm_i_tc16_array, scm_array_equal_p); |
| 2980 | |
| 2981 | scm_i_tc16_enclosed_array = scm_make_smob_type ("enclosed-array", 0); |
| 2982 | scm_set_smob_mark (scm_i_tc16_enclosed_array, array_mark); |
| 2983 | scm_set_smob_free (scm_i_tc16_enclosed_array, array_free); |
| 2984 | scm_set_smob_print (scm_i_tc16_enclosed_array, scm_i_print_enclosed_array); |
| 2985 | scm_set_smob_equalp (scm_i_tc16_enclosed_array, scm_array_equal_p); |
| 2986 | |
| 2987 | scm_add_feature ("array"); |
| 2988 | |
| 2989 | scm_tc16_bitvector = scm_make_smob_type ("bitvector", 0); |
| 2990 | scm_set_smob_free (scm_tc16_bitvector, bitvector_free); |
| 2991 | scm_set_smob_print (scm_tc16_bitvector, bitvector_print); |
| 2992 | scm_set_smob_equalp (scm_tc16_bitvector, bitvector_equalp); |
| 2993 | |
| 2994 | init_type_creator_table (); |
| 2995 | |
| 2996 | #include "libguile/unif.x" |
| 2997 | |
| 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | Local Variables: |
| 3002 | c-file-style: "gnu" |
| 3003 | End: |
| 3004 | */ |