| 1 | /* classes: h_files */ |
| 2 | |
| 3 | #ifndef SCM_ARRAY_HANDLE_H |
| 4 | #define SCM_ARRAY_HANDLE_H |
| 5 | |
| 6 | /* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2004, 2006, |
| 7 | * 2008, 2009, 2011, 2013, 2014 Free Software Foundation, Inc. |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU Lesser General Public License |
| 11 | * as published by the Free Software Foundation; either version 3 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This library is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public |
| 20 | * License along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | \f |
| 26 | |
| 27 | #include "libguile/__scm.h" |
| 28 | #include "libguile/error.h" |
| 29 | #include "libguile/numbers.h" |
| 30 | |
| 31 | \f |
| 32 | |
| 33 | typedef SCM (*scm_t_vector_ref) (SCM, size_t); |
| 34 | typedef void (*scm_t_vector_set) (SCM, size_t, SCM); |
| 35 | |
| 36 | typedef struct scm_t_array_dim |
| 37 | { |
| 38 | ssize_t lbnd; |
| 39 | ssize_t ubnd; |
| 40 | ssize_t inc; |
| 41 | } scm_t_array_dim; |
| 42 | |
| 43 | typedef enum |
| 44 | { |
| 45 | SCM_ARRAY_ELEMENT_TYPE_SCM = 0, /* SCM values */ |
| 46 | SCM_ARRAY_ELEMENT_TYPE_CHAR = 1, /* characters */ |
| 47 | SCM_ARRAY_ELEMENT_TYPE_BIT = 2, /* packed numeric values */ |
| 48 | SCM_ARRAY_ELEMENT_TYPE_VU8 = 3, |
| 49 | SCM_ARRAY_ELEMENT_TYPE_U8 = 4, |
| 50 | SCM_ARRAY_ELEMENT_TYPE_S8 = 5, |
| 51 | SCM_ARRAY_ELEMENT_TYPE_U16 = 6, |
| 52 | SCM_ARRAY_ELEMENT_TYPE_S16 = 7, |
| 53 | SCM_ARRAY_ELEMENT_TYPE_U32 = 8, |
| 54 | SCM_ARRAY_ELEMENT_TYPE_S32 = 9, |
| 55 | SCM_ARRAY_ELEMENT_TYPE_U64 = 10, |
| 56 | SCM_ARRAY_ELEMENT_TYPE_S64 = 11, |
| 57 | SCM_ARRAY_ELEMENT_TYPE_F32 = 12, |
| 58 | SCM_ARRAY_ELEMENT_TYPE_F64 = 13, |
| 59 | SCM_ARRAY_ELEMENT_TYPE_C32 = 14, |
| 60 | SCM_ARRAY_ELEMENT_TYPE_C64 = 15, |
| 61 | SCM_ARRAY_ELEMENT_TYPE_LAST = 15 |
| 62 | } scm_t_array_element_type; |
| 63 | |
| 64 | SCM_INTERNAL SCM scm_i_array_element_types[]; |
| 65 | |
| 66 | |
| 67 | typedef struct scm_t_array_handle { |
| 68 | SCM array; |
| 69 | |
| 70 | /* `Base' is an offset into elements or writable_elements, corresponding to |
| 71 | the first element in the array. It would be nicer just to adjust the |
| 72 | elements/writable_elements pointer, but we can't because that element might |
| 73 | not even be byte-addressable, as is the case with bitvectors. A nicer |
| 74 | solution would be, well, nice. |
| 75 | */ |
| 76 | size_t base; |
| 77 | size_t ndims; /* ndims == the rank of the array */ |
| 78 | scm_t_array_dim *dims; |
| 79 | scm_t_array_dim dim0; |
| 80 | scm_t_array_element_type element_type; |
| 81 | const void *elements; |
| 82 | void *writable_elements; |
| 83 | |
| 84 | /* The backing store for the array, and its accessors. */ |
| 85 | SCM vector; |
| 86 | scm_t_vector_ref vref; |
| 87 | scm_t_vector_set vset; |
| 88 | } scm_t_array_handle; |
| 89 | |
| 90 | #define scm_array_handle_rank(h) ((h)->ndims) |
| 91 | #define scm_array_handle_dims(h) ((h)->dims) |
| 92 | |
| 93 | SCM_API void scm_array_get_handle (SCM array, scm_t_array_handle *h); |
| 94 | SCM_API ssize_t scm_array_handle_pos (scm_t_array_handle *h, SCM indices); |
| 95 | SCM_API ssize_t scm_array_handle_pos_1 (scm_t_array_handle *h, ssize_t idx0); |
| 96 | SCM_API ssize_t scm_array_handle_pos_2 (scm_t_array_handle *h, ssize_t idx0, ssize_t idx1); |
| 97 | SCM_API SCM scm_array_handle_element_type (scm_t_array_handle *h); |
| 98 | SCM_API void scm_array_handle_release (scm_t_array_handle *h); |
| 99 | SCM_API const SCM* scm_array_handle_elements (scm_t_array_handle *h); |
| 100 | SCM_API SCM* scm_array_handle_writable_elements (scm_t_array_handle *h); |
| 101 | |
| 102 | |
| 103 | SCM_INLINE SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos); |
| 104 | SCM_INLINE void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val); |
| 105 | |
| 106 | #if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES |
| 107 | /* Either inlining, or being included from inline.c. */ |
| 108 | |
| 109 | SCM_INLINE_IMPLEMENTATION SCM |
| 110 | scm_array_handle_ref (scm_t_array_handle *h, ssize_t p) |
| 111 | { |
| 112 | if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base)) |
| 113 | /* catch overflow */ |
| 114 | scm_out_of_range (NULL, scm_from_ssize_t (p)); |
| 115 | /* perhaps should catch overflow here too */ |
| 116 | return h->vref (h->vector, h->base + p); |
| 117 | } |
| 118 | |
| 119 | SCM_INLINE_IMPLEMENTATION void |
| 120 | scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v) |
| 121 | { |
| 122 | if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base)) |
| 123 | /* catch overflow */ |
| 124 | scm_out_of_range (NULL, scm_from_ssize_t (p)); |
| 125 | /* perhaps should catch overflow here too */ |
| 126 | h->vset (h->vector, h->base + p, v); |
| 127 | } |
| 128 | |
| 129 | #endif |
| 130 | |
| 131 | |
| 132 | SCM_INTERNAL void scm_init_array_handle (void); |
| 133 | |
| 134 | |
| 135 | #endif /* SCM_ARRAY_HANDLE_H */ |
| 136 | |
| 137 | /* |
| 138 | Local Variables: |
| 139 | c-file-style: "gnu" |
| 140 | End: |
| 141 | */ |