summaryrefslogtreecommitdiff
path: root/libguile/array-handle.c
blob: 35fc1fc2fbea22e38b35b6cdae942b85f2b44519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Copyright (C) 1995,1996,1997,1998,2000,2001,2002,2003,2004, 2005, 2006, 2009 Free Software Foundation, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */




#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include "libguile/_scm.h"
#include "libguile/__scm.h"

#include "libguile/array-handle.h"
#include "libguile/arrays.h"
#include "libguile/strings.h"
#include "libguile/vectors.h"
#include "libguile/srfi-4.h"
#include "libguile/bitvectors.h"
#include "libguile/bytevectors.h"


static SCM
enclosed_ref (scm_t_array_handle *h, ssize_t pos)
{
  return scm_i_cvref (SCM_I_ARRAY_V (h->array), pos + h->base, 1);
}

static SCM
vector_ref (scm_t_array_handle *h, ssize_t pos)
{
  return ((const SCM *)h->elements)[pos];
}

static SCM
string_ref (scm_t_array_handle *h, ssize_t pos)
{
  pos += h->base;
  if (SCM_I_ARRAYP (h->array))
    return scm_c_string_ref (SCM_I_ARRAY_V (h->array), pos);
  else
    return scm_c_string_ref (h->array, pos);
}

static SCM
bitvector_ref (scm_t_array_handle *h, ssize_t pos)
{
  pos += scm_array_handle_bit_elements_offset (h);
  return
    scm_from_bool (((scm_t_uint32 *)h->elements)[pos/32] & (1l << (pos % 32)));
}

static SCM
bytevector_ref (scm_t_array_handle *h, ssize_t pos)
{
  return scm_from_uint8 (((scm_t_uint8 *) h->elements)[pos]);
}

static SCM
memoize_ref (scm_t_array_handle *h, ssize_t pos)
{
  SCM v = h->array;

  if (SCM_I_ENCLOSED_ARRAYP (v))
    {
      h->ref = enclosed_ref;
      return enclosed_ref (h, pos);
    }

  if (SCM_I_ARRAYP (v))
    v = SCM_I_ARRAY_V (v);

  if (scm_is_vector (v))
    {
      h->elements = scm_array_handle_elements (h);
      h->ref = vector_ref;
    }
  else if (scm_is_uniform_vector (v))
    {
      h->elements = scm_array_handle_uniform_elements (h);
      h->ref = scm_i_uniform_vector_ref_proc (v);
    }
  else if (scm_is_string (v))
    {
      h->ref = string_ref;
    }
  else if (scm_is_bitvector (v))
    {
      h->elements = scm_array_handle_bit_elements (h);
      h->ref = bitvector_ref;
    }
  else if (scm_is_bytevector (v))
    {
      h->elements = scm_array_handle_uniform_elements (h);
      h->ref = bytevector_ref;
    }
  else
    scm_misc_error (NULL, "unknown array type: ~a", scm_list_1 (h->array));

  return h->ref (h, pos);
}

static void
enclosed_set (scm_t_array_handle *h, ssize_t pos, SCM val)
{
  scm_wrong_type_arg_msg (NULL, 0, h->array, "non-enclosed array");
}

static void
vector_set (scm_t_array_handle *h, ssize_t pos, SCM val)
{
  ((SCM *)h->writable_elements)[pos] = val;
}

static void
string_set (scm_t_array_handle *h, ssize_t pos, SCM val)
{
  pos += h->base;
  if (SCM_I_ARRAYP (h->array))
    scm_c_string_set_x (SCM_I_ARRAY_V (h->array), pos, val);
  else
    scm_c_string_set_x (h->array, pos, val);
}

static void
bitvector_set (scm_t_array_handle *h, ssize_t pos, SCM val)
{
  scm_t_uint32 mask;
  pos += scm_array_handle_bit_elements_offset (h);
  mask = 1l << (pos % 32);
  if (scm_to_bool (val))
    ((scm_t_uint32 *)h->writable_elements)[pos/32] |= mask;
  else
    ((scm_t_uint32 *)h->writable_elements)[pos/32] &= ~mask;
}

static void
bytevector_set (scm_t_array_handle *h, ssize_t pos, SCM val)
{
  scm_t_uint8 c_value;
  scm_t_uint8 *elements;

  c_value = scm_to_uint8 (val);
  elements = (scm_t_uint8 *) h->elements;
  elements[pos] = (scm_t_uint8) c_value;
}

static void
memoize_set (scm_t_array_handle *h, ssize_t pos, SCM val)
{
  SCM v = h->array;

  if (SCM_I_ENCLOSED_ARRAYP (v))
    {
      h->set = enclosed_set;
      enclosed_set (h, pos, val);
      return;
    }

  if (SCM_I_ARRAYP (v))
    v = SCM_I_ARRAY_V (v);

  if (scm_is_vector (v))
    {
      h->writable_elements = scm_array_handle_writable_elements (h);
      h->set = vector_set;
    }
  else if (scm_is_uniform_vector (v))
    {
      h->writable_elements = scm_array_handle_uniform_writable_elements (h);
      h->set = scm_i_uniform_vector_set_proc (v);
    }
  else if (scm_is_string (v))
    {
      h->set = string_set;
    }
  else if (scm_is_bitvector (v))
    {
      h->writable_elements = scm_array_handle_bit_writable_elements (h);
      h->set = bitvector_set;
    }
  else if (scm_is_bytevector (v))
    {
      h->elements = scm_array_handle_uniform_writable_elements (h);
      h->set = bytevector_set;
    }
  else
    scm_misc_error (NULL, "unknown array type: ~a", scm_list_1 (h->array));

  h->set (h, pos, val);
}

void
scm_array_get_handle (SCM array, scm_t_array_handle *h)
{
  h->array = array;
  h->ref = memoize_ref;
  h->set = memoize_set;

  if (SCM_I_ARRAYP (array) || SCM_I_ENCLOSED_ARRAYP (array))
    {
      h->dims = SCM_I_ARRAY_DIMS (array);
      h->base = SCM_I_ARRAY_BASE (array);
    }
  else if (scm_is_generalized_vector (array))
    {
      h->dim0.lbnd = 0;
      h->dim0.ubnd = scm_c_generalized_vector_length (array) - 1;
      h->dim0.inc = 1;
      h->dims = &h->dim0;
      h->base = 0;
    }
  else
    scm_wrong_type_arg_msg (NULL, 0, array, "array");
}

void
scm_array_handle_release (scm_t_array_handle *h)
{
  /* Nothing to do here until arrays need to be reserved for real.
   */
}

size_t
scm_array_handle_rank (scm_t_array_handle *h)
{
  if (SCM_I_ARRAYP (h->array) || SCM_I_ENCLOSED_ARRAYP (h->array))
    return SCM_I_ARRAY_NDIM (h->array);
  else
    return 1;
}

scm_t_array_dim *
scm_array_handle_dims (scm_t_array_handle *h)
{
  return h->dims;
}

const SCM *
scm_array_handle_elements (scm_t_array_handle *h)
{
  SCM vec = h->array;
  if (SCM_I_ARRAYP (vec))
    vec = SCM_I_ARRAY_V (vec);
  if (SCM_I_IS_VECTOR (vec))
    return SCM_I_VECTOR_ELTS (vec) + h->base;
  scm_wrong_type_arg_msg (NULL, 0, h->array, "non-uniform array");
}

SCM *
scm_array_handle_writable_elements (scm_t_array_handle *h)
{
  SCM vec = h->array;
  if (SCM_I_ARRAYP (vec))
    vec = SCM_I_ARRAY_V (vec);
  if (SCM_I_IS_VECTOR (vec))
    return SCM_I_VECTOR_WELTS (vec) + h->base;
  scm_wrong_type_arg_msg (NULL, 0, h->array, "non-uniform array");
}


void
scm_init_array_handle (void)
{
#include "libguile/array-handle.x"
}

/*
  Local Variables:
  c-file-style: "gnu"
  End:
*/