add generic array implementation facility
[bpt/guile.git] / libguile / array-handle.h
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, 2008, 2009 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24 \f
25
26 #include "libguile/__scm.h"
27
28 \f
29
30 struct scm_t_array_handle;
31
32 typedef SCM (*scm_i_t_array_ref) (struct scm_t_array_handle *, size_t);
33 typedef void (*scm_i_t_array_set) (struct scm_t_array_handle *, size_t, SCM);
34
35 typedef struct
36 {
37 scm_t_bits tag;
38 scm_t_bits mask;
39 scm_i_t_array_ref vref;
40 scm_i_t_array_set vset;
41 void (*get_handle)(SCM, struct scm_t_array_handle*);
42 } scm_t_array_implementation;
43
44 #define SCM_ARRAY_IMPLEMENTATION(tag_,mask_,vref_,vset_,handle_) \
45 SCM_SNARF_INIT ({ \
46 scm_t_array_implementation impl; \
47 impl.tag = tag_; impl.mask = mask_; \
48 impl.vref = vref_; impl.vset = vset_; \
49 impl.get_handle = handle_; \
50 scm_i_register_array_implementation (&impl); \
51 })
52
53
54 SCM_INTERNAL void scm_i_register_array_implementation (scm_t_array_implementation *impl);
55 SCM_INTERNAL scm_t_array_implementation* scm_i_array_implementation_for_obj (SCM obj);
56
57
58 \f
59
60 typedef struct scm_t_array_dim
61 {
62 ssize_t lbnd;
63 ssize_t ubnd;
64 ssize_t inc;
65 } scm_t_array_dim;
66
67 typedef enum {
68 SCM_ARRAY_ELEMENT_TYPE_SCM = 0, /* SCM values */
69 SCM_ARRAY_ELEMENT_TYPE_CHAR = 1, /* characters */
70 SCM_ARRAY_ELEMENT_TYPE_BIT = 2, /* packed numeric values */
71 SCM_ARRAY_ELEMENT_TYPE_VU8 = 3,
72 SCM_ARRAY_ELEMENT_TYPE_U8 = 4,
73 SCM_ARRAY_ELEMENT_TYPE_S8 = 5,
74 SCM_ARRAY_ELEMENT_TYPE_U16 = 6,
75 SCM_ARRAY_ELEMENT_TYPE_S16 = 7,
76 SCM_ARRAY_ELEMENT_TYPE_U32 = 8,
77 SCM_ARRAY_ELEMENT_TYPE_S32 = 9,
78 SCM_ARRAY_ELEMENT_TYPE_U64 = 10,
79 SCM_ARRAY_ELEMENT_TYPE_S64 = 11,
80 SCM_ARRAY_ELEMENT_TYPE_F32 = 12,
81 SCM_ARRAY_ELEMENT_TYPE_F64 = 13,
82 SCM_ARRAY_ELEMENT_TYPE_C32 = 14,
83 SCM_ARRAY_ELEMENT_TYPE_C64 = 15,
84 SCM_ARRAY_ELEMENT_TYPE_LAST = 15,
85 } scm_t_array_element_type;
86
87 SCM_INTERNAL SCM scm_i_array_element_types[];
88
89
90 typedef struct scm_t_array_handle {
91 SCM array;
92 scm_t_array_implementation *impl;
93 /* `Base' is an offset into elements or writable_elements, corresponding to
94 the first element in the array. It would be nicer just to adjust the
95 elements/writable_elements pointer, but we can't because that element might
96 not even be byte-addressable, as is the case with bitvectors. A nicer
97 solution would be, well, nice.
98 */
99 size_t base;
100 size_t ndims; /* ndims == the rank of the array */
101 scm_t_array_dim *dims;
102 scm_t_array_dim dim0;
103 scm_t_array_element_type element_type;
104 const void *elements;
105 void *writable_elements;
106 } scm_t_array_handle;
107
108 #define scm_array_handle_rank(h) ((h)->ndims)
109 #define scm_array_handle_dims(h) ((h)->dims)
110
111 SCM_API void scm_array_get_handle (SCM array, scm_t_array_handle *h);
112 SCM_API ssize_t scm_array_handle_pos (scm_t_array_handle *h, SCM indices);
113 SCM_API SCM scm_array_handle_element_type (scm_t_array_handle *h);
114 SCM_API void scm_array_handle_release (scm_t_array_handle *h);
115 SCM_API const SCM* scm_array_handle_elements (scm_t_array_handle *h);
116 SCM_API SCM* scm_array_handle_writable_elements (scm_t_array_handle *h);
117
118 /* See inline.h for scm_array_handle_ref and scm_array_handle_set */
119
120 SCM_INTERNAL void scm_init_array_handle (void);
121
122
123 #endif /* SCM_ARRAY_HANDLE_H */
124
125 /*
126 Local Variables:
127 c-file-style: "gnu"
128 End:
129 */