Optimize 'string-hash'.
[bpt/guile.git] / libguile / array-handle.h
CommitLineData
c53c0893
AW
1/* classes: h_files */
2
3#ifndef SCM_ARRAY_HANDLE_H
4#define SCM_ARRAY_HANDLE_H
5
9c3fa20a 6/* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2004, 2006,
18cd9aff 7 * 2008, 2009, 2011, 2013 Free Software Foundation, Inc.
c53c0893
AW
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"
18cd9aff
AW
28#include "libguile/error.h"
29#include "libguile/numbers.h"
c53c0893
AW
30
31\f
32
2a610be5
AW
33struct scm_t_array_handle;
34
35typedef SCM (*scm_i_t_array_ref) (struct scm_t_array_handle *, size_t);
36typedef void (*scm_i_t_array_set) (struct scm_t_array_handle *, size_t, SCM);
37
38typedef struct
39{
40 scm_t_bits tag;
41 scm_t_bits mask;
42 scm_i_t_array_ref vref;
43 scm_i_t_array_set vset;
44 void (*get_handle)(SCM, struct scm_t_array_handle*);
45} scm_t_array_implementation;
46
47#define SCM_ARRAY_IMPLEMENTATION(tag_,mask_,vref_,vset_,handle_) \
48 SCM_SNARF_INIT ({ \
49 scm_t_array_implementation impl; \
50 impl.tag = tag_; impl.mask = mask_; \
51 impl.vref = vref_; impl.vset = vset_; \
52 impl.get_handle = handle_; \
53 scm_i_register_array_implementation (&impl); \
54 })
55
56
57SCM_INTERNAL void scm_i_register_array_implementation (scm_t_array_implementation *impl);
58SCM_INTERNAL scm_t_array_implementation* scm_i_array_implementation_for_obj (SCM obj);
59
60
61\f
62
c53c0893
AW
63typedef struct scm_t_array_dim
64{
65 ssize_t lbnd;
66 ssize_t ubnd;
67 ssize_t inc;
68} scm_t_array_dim;
69
9c3fa20a
LC
70typedef enum
71 {
72 SCM_ARRAY_ELEMENT_TYPE_SCM = 0, /* SCM values */
73 SCM_ARRAY_ELEMENT_TYPE_CHAR = 1, /* characters */
74 SCM_ARRAY_ELEMENT_TYPE_BIT = 2, /* packed numeric values */
75 SCM_ARRAY_ELEMENT_TYPE_VU8 = 3,
76 SCM_ARRAY_ELEMENT_TYPE_U8 = 4,
77 SCM_ARRAY_ELEMENT_TYPE_S8 = 5,
78 SCM_ARRAY_ELEMENT_TYPE_U16 = 6,
79 SCM_ARRAY_ELEMENT_TYPE_S16 = 7,
80 SCM_ARRAY_ELEMENT_TYPE_U32 = 8,
81 SCM_ARRAY_ELEMENT_TYPE_S32 = 9,
82 SCM_ARRAY_ELEMENT_TYPE_U64 = 10,
83 SCM_ARRAY_ELEMENT_TYPE_S64 = 11,
84 SCM_ARRAY_ELEMENT_TYPE_F32 = 12,
85 SCM_ARRAY_ELEMENT_TYPE_F64 = 13,
86 SCM_ARRAY_ELEMENT_TYPE_C32 = 14,
87 SCM_ARRAY_ELEMENT_TYPE_C64 = 15,
88 SCM_ARRAY_ELEMENT_TYPE_LAST = 15
89 } scm_t_array_element_type;
2a610be5
AW
90
91SCM_INTERNAL SCM scm_i_array_element_types[];
c53c0893 92
c53c0893
AW
93
94typedef struct scm_t_array_handle {
95 SCM array;
2a610be5
AW
96 scm_t_array_implementation *impl;
97 /* `Base' is an offset into elements or writable_elements, corresponding to
98 the first element in the array. It would be nicer just to adjust the
99 elements/writable_elements pointer, but we can't because that element might
100 not even be byte-addressable, as is the case with bitvectors. A nicer
101 solution would be, well, nice.
102 */
c53c0893 103 size_t base;
2a610be5 104 size_t ndims; /* ndims == the rank of the array */
c53c0893
AW
105 scm_t_array_dim *dims;
106 scm_t_array_dim dim0;
2a610be5 107 scm_t_array_element_type element_type;
c53c0893
AW
108 const void *elements;
109 void *writable_elements;
110} scm_t_array_handle;
111
2a610be5
AW
112#define scm_array_handle_rank(h) ((h)->ndims)
113#define scm_array_handle_dims(h) ((h)->dims)
114
c53c0893 115SCM_API void scm_array_get_handle (SCM array, scm_t_array_handle *h);
c53c0893 116SCM_API ssize_t scm_array_handle_pos (scm_t_array_handle *h, SCM indices);
336c9211
AW
117SCM_API ssize_t scm_array_handle_pos_1 (scm_t_array_handle *h, ssize_t idx0);
118SCM_API ssize_t scm_array_handle_pos_2 (scm_t_array_handle *h, ssize_t idx0, ssize_t idx1);
2a610be5 119SCM_API SCM scm_array_handle_element_type (scm_t_array_handle *h);
c53c0893 120SCM_API void scm_array_handle_release (scm_t_array_handle *h);
2a610be5
AW
121SCM_API const SCM* scm_array_handle_elements (scm_t_array_handle *h);
122SCM_API SCM* scm_array_handle_writable_elements (scm_t_array_handle *h);
c53c0893 123
18cd9aff
AW
124
125SCM_INLINE SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t pos);
126SCM_INLINE void scm_array_handle_set (scm_t_array_handle *h, ssize_t pos, SCM val);
127
128#if SCM_CAN_INLINE || defined SCM_INLINE_C_IMPLEMENTING_INLINES
129/* Either inlining, or being included from inline.c. */
130
131SCM_INLINE_IMPLEMENTATION SCM
132scm_array_handle_ref (scm_t_array_handle *h, ssize_t p)
133{
134 if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base))
135 /* catch overflow */
136 scm_out_of_range (NULL, scm_from_ssize_t (p));
137 /* perhaps should catch overflow here too */
138 return h->impl->vref (h, h->base + p);
139}
140
141SCM_INLINE_IMPLEMENTATION void
142scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v)
143{
144 if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base))
145 /* catch overflow */
146 scm_out_of_range (NULL, scm_from_ssize_t (p));
147 /* perhaps should catch overflow here too */
148 h->impl->vset (h, h->base + p, v);
149}
150
151#endif
152
c53c0893
AW
153
154SCM_INTERNAL void scm_init_array_handle (void);
155
156
157#endif /* SCM_ARRAY_HANDLE_H */
158
159/*
160 Local Variables:
161 c-file-style: "gnu"
162 End:
163*/