carray 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/COPYING +56 -0
- data/GPL +340 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +33 -0
- data/LEGAL +50 -0
- data/NOTE +73 -0
- data/Rakefile +20 -0
- data/TODO +5 -0
- data/ca_iter_block.c +242 -0
- data/ca_iter_dimension.c +287 -0
- data/ca_iter_window.c +202 -0
- data/ca_obj_array.c +1189 -0
- data/ca_obj_bitarray.c +523 -0
- data/ca_obj_bitfield.c +636 -0
- data/ca_obj_block.c +885 -0
- data/ca_obj_fake.c +405 -0
- data/ca_obj_farray.c +482 -0
- data/ca_obj_field.c +625 -0
- data/ca_obj_grid.c +738 -0
- data/ca_obj_mapping.c +614 -0
- data/ca_obj_object.c +777 -0
- data/ca_obj_reduce.c +299 -0
- data/ca_obj_refer.c +627 -0
- data/ca_obj_repeat.c +640 -0
- data/ca_obj_select.c +558 -0
- data/ca_obj_shift.c +952 -0
- data/ca_obj_transpose.c +582 -0
- data/ca_obj_unbound_repeat.c +557 -0
- data/ca_obj_window.c +1023 -0
- data/carray.h +1381 -0
- data/carray_access.c +1798 -0
- data/carray_attribute.c +903 -0
- data/carray_call_cfunc.c +1107 -0
- data/carray_cast.c +1155 -0
- data/carray_cast_func.rb +498 -0
- data/carray_class.c +132 -0
- data/carray_conversion.c +518 -0
- data/carray_copy.c +453 -0
- data/carray_core.c +1307 -0
- data/carray_element.c +572 -0
- data/carray_generate.c +681 -0
- data/carray_iterator.c +630 -0
- data/carray_loop.c +462 -0
- data/carray_mask.c +1174 -0
- data/carray_math.rb +834 -0
- data/carray_numeric.c +257 -0
- data/carray_operator.c +582 -0
- data/carray_order.c +1040 -0
- data/carray_random.c +529 -0
- data/carray_sort_addr.c +261 -0
- data/carray_stat.c +2102 -0
- data/carray_stat_proc.rb +1990 -0
- data/carray_test.c +602 -0
- data/carray_undef.c +69 -0
- data/carray_utils.c +740 -0
- data/ext/calculus/carray_calculus.c +792 -0
- data/ext/calculus/carray_interp.c +355 -0
- data/ext/calculus/extconf.rb +12 -0
- data/ext/calculus/lib/autoload/autoload_math_calculus.rb +2 -0
- data/ext/calculus/lib/math/calculus.rb +119 -0
- data/ext/calculus/lib/math/interp/adapter_interp1d.rb +31 -0
- data/ext/dataframe/API.txt +11 -0
- data/ext/dataframe/extconf.rb +3 -0
- data/ext/dataframe/lib/carray/autoload/autoload_dataframe_dataframe.rb +14 -0
- data/ext/dataframe/lib/carray/dataframe/dataframe.rb +1104 -0
- data/ext/dataframe/sample/test_uniq_sort.rb +5 -0
- data/ext/fortio/extconf.rb +3 -0
- data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +5 -0
- data/ext/fortio/lib/carray/io/fortran_format.rb +43 -0
- data/ext/fortio/lib/fortio.rb +3 -0
- data/ext/fortio/lib/fortio/fortran_format.rb +603 -0
- data/ext/fortio/lib/fortio/fortran_format.tab.rb +536 -0
- data/ext/fortio/lib/fortio/fortran_format.y +215 -0
- data/ext/fortio/lib/fortio/fortran_namelist.rb +151 -0
- data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +470 -0
- data/ext/fortio/lib/fortio/fortran_namelist.y +213 -0
- data/ext/fortio/lib/fortio/fortran_sequential.rb +345 -0
- data/ext/fortio/ruby_fortio.c +182 -0
- data/ext/fortio/test/test_H.rb +5 -0
- data/ext/fortio/test/test_T.rb +7 -0
- data/ext/fortio/test/test_fortran_format.rb +86 -0
- data/ext/fortio/test/test_namelist.rb +25 -0
- data/ext/fortio/test/test_sequential.rb +13 -0
- data/ext/fortio/test/test_sequential2.rb +13 -0
- data/ext/fortio/work/test.rb +10 -0
- data/ext/fortio/work/test_e.rb +19 -0
- data/ext/fortio/work/test_ep.rb +10 -0
- data/ext/fortio/work/test_parse.rb +12 -0
- data/ext/imagemap/carray_imagemap.c +495 -0
- data/ext/imagemap/doc/call_graph.dot +28 -0
- data/ext/imagemap/draw.c +567 -0
- data/ext/imagemap/extconf.rb +13 -0
- data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +1 -0
- data/ext/imagemap/lib/graphics/imagemap.rb +273 -0
- data/ext/imagemap/lib/image_map.rb +4 -0
- data/ext/imagemap/test/swath_index.rb +83 -0
- data/ext/imagemap/test/swath_warp.rb +99 -0
- data/ext/imagemap/test/test.rb +23 -0
- data/ext/imagemap/test/test_image.rb +42 -0
- data/ext/imagemap/test/test_line.rb +14 -0
- data/ext/imagemap/test/test_rotate.rb +17 -0
- data/ext/imagemap/test/test_triangle.rb +20 -0
- data/ext/imagemap/test/test_warp.rb +26 -0
- data/ext/mathfunc/carray_mathfunc.c +321 -0
- data/ext/mathfunc/extconf.rb +18 -0
- data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +1 -0
- data/ext/mathfunc/lib/math/mathfunc.rb +15 -0
- data/ext/mathfunc/test/test_hypot.rb +5 -0
- data/ext/mathfunc/test/test_j0.rb +22 -0
- data/ext/mathfunc/test/test_jn.rb +8 -0
- data/ext/mathfunc/test/test_sph.rb +9 -0
- data/ext/narray/README +22 -0
- data/ext/narray/ca_wrap_narray.c +491 -0
- data/ext/narray/carray_narray.c +21 -0
- data/ext/narray/extconf.rb +57 -0
- data/ext/narray/lib/autoload/autoload_math_narray.rb +1 -0
- data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +11 -0
- data/ext/narray/lib/math/narray.rb +17 -0
- data/ext/narray/lib/math/narray_miss.rb +45 -0
- data/extconf.rb +3 -25
- data/lib/carray.rb +28 -0
- data/lib/carray/autoload/autoload_base.rb +23 -0
- data/lib/carray/autoload/autoload_graphics_gnuplot.rb +2 -0
- data/lib/carray/autoload/autoload_io_csv.rb +14 -0
- data/lib/carray/autoload/autoload_io_excel.rb +5 -0
- data/lib/carray/autoload/autoload_io_imagemagick.rb +6 -0
- data/lib/carray/autoload/autoload_io_pg.rb +6 -0
- data/lib/carray/autoload/autoload_io_sqlite3.rb +12 -0
- data/lib/carray/autoload/autoload_io_table.rb +1 -0
- data/lib/carray/autoload/autoload_math_histogram.rb +5 -0
- data/lib/carray/autoload/autoload_math_interp.rb +4 -0
- data/lib/carray/autoload/autoload_math_recurrence.rb +6 -0
- data/lib/carray/autoload/autoload_object_iterator.rb +1 -0
- data/lib/carray/autoload/autoload_object_link.rb +1 -0
- data/lib/carray/autoload/autoload_object_pack.rb +2 -0
- data/lib/carray/base/autoload.rb +94 -0
- data/lib/carray/base/basic.rb +1051 -0
- data/lib/carray/base/inspect.rb +252 -0
- data/lib/carray/base/iterator.rb +367 -0
- data/lib/carray/base/math.rb +403 -0
- data/lib/carray/base/obsolete.rb +93 -0
- data/lib/carray/base/serialize.rb +260 -0
- data/lib/carray/base/struct.rb +634 -0
- data/lib/carray/graphics/gnuplot.rb +2116 -0
- data/lib/carray/info.rb +112 -0
- data/lib/carray/io/csv.rb +560 -0
- data/lib/carray/io/excel.rb +26 -0
- data/lib/carray/io/imagemagick.rb +231 -0
- data/lib/carray/io/pg.rb +101 -0
- data/lib/carray/io/sqlite3.rb +202 -0
- data/lib/carray/io/table.rb +77 -0
- data/lib/carray/math/histogram.rb +179 -0
- data/lib/carray/math/interp.rb +57 -0
- data/lib/carray/math/interp/adapter_gsl_spline.rb +47 -0
- data/lib/carray/math/recurrence.rb +95 -0
- data/lib/carray/mkmf.rb +145 -0
- data/lib/carray/object/ca_obj_iterator.rb +52 -0
- data/lib/carray/object/ca_obj_link.rb +52 -0
- data/lib/carray/object/ca_obj_pack.rb +101 -0
- data/mkmath.rb +731 -0
- data/mt19937ar.c +182 -0
- data/mt19937ar.h +86 -0
- data/rdoc_main.rb +27 -0
- data/rdoc_math.rb +5 -0
- data/rdoc_stat.rb +31 -0
- data/ruby_carray.c +242 -0
- data/ruby_ccomplex.c +497 -0
- data/ruby_float_func.c +83 -0
- data/spec/CABlockIterator/CABlockIterator_spec.rb +113 -0
- data/spec/CArray/bug/store_spec.rb +27 -0
- data/spec/CArray/index/repeat_spec.rb +10 -0
- data/spec/CArray/method/eq_spec.rb +80 -0
- data/spec/CArray/method/is_nan_spec.rb +12 -0
- data/spec/CArray/method/ne_spec.rb +18 -0
- data/spec/CArray/method/round_spec.rb +11 -0
- data/spec/CArray/object/_attribute_spec.rb +32 -0
- data/spec/CArray/object/s_new_spec.rb +31 -0
- data/spec/CArray/serialize/Serialization_spec.rb +89 -0
- data/spec/spec_all.rb +11 -0
- data/test/test_ALL.rb +50 -0
- data/test/test_CABitfield.rb +59 -0
- data/test/test_CABlock.rb +208 -0
- data/test/test_CAField.rb +40 -0
- data/test/test_CAGrid.rb +76 -0
- data/test/test_CAMapping.rb +106 -0
- data/test/test_CAMmap.rb +11 -0
- data/test/test_CARefer.rb +94 -0
- data/test/test_CARepeat.rb +66 -0
- data/test/test_CASelect.rb +23 -0
- data/test/test_CAShift.rb +17 -0
- data/test/test_CATranspose.rb +61 -0
- data/test/test_CAVirtual.rb +214 -0
- data/test/test_CAWindow.rb +55 -0
- data/test/test_CAWrap.rb +9 -0
- data/test/test_CArray.rb +228 -0
- data/test/test_CComplex.rb +83 -0
- data/test/test_CScalar.rb +91 -0
- data/test/test_attribute.rb +281 -0
- data/test/test_block_iterator.rb +17 -0
- data/test/test_boolean.rb +99 -0
- data/test/test_cast.rb +33 -0
- data/test/test_class.rb +85 -0
- data/test/test_complex.rb +43 -0
- data/test/test_composite.rb +125 -0
- data/test/test_convert.rb +79 -0
- data/test/test_copy.rb +141 -0
- data/test/test_creation.rb +85 -0
- data/test/test_element.rb +146 -0
- data/test/test_extream.rb +55 -0
- data/test/test_generate.rb +75 -0
- data/test/test_index.rb +71 -0
- data/test/test_mask.rb +578 -0
- data/test/test_math.rb +98 -0
- data/test/test_narray.rb +64 -0
- data/test/test_order.rb +147 -0
- data/test/test_random.rb +15 -0
- data/test/test_ref_store.rb +211 -0
- data/test/test_stat.rb +414 -0
- data/test/test_struct.rb +72 -0
- data/test/test_virtual.rb +49 -0
- data/utils/ca_ase.rb +21 -0
- data/utils/ca_methods.rb +15 -0
- data/utils/cast_checker.rb +30 -0
- data/utils/create_rdoc.sh +9 -0
- data/utils/diff_method.rb +52 -0
- data/utils/extract_rdoc.rb +27 -0
- data/utils/make_tgz.sh +3 -0
- data/utils/remove_resource_fork.sh +5 -0
- data/version.h +3 -3
- metadata +266 -1
data/carray_cast.c
ADDED
@@ -0,0 +1,1155 @@
|
|
1
|
+
/* ---------------------------------------------------------------------------
|
2
|
+
|
3
|
+
carray_cast.c
|
4
|
+
|
5
|
+
This file is part of Ruby/CArray extension library.
|
6
|
+
You can redistribute it and/or modify it under the terms of
|
7
|
+
the Ruby Licence.
|
8
|
+
|
9
|
+
Copyright (C) 2005 Hiroki Motoyoshi
|
10
|
+
|
11
|
+
---------------------------------------------------------------------------- */
|
12
|
+
|
13
|
+
#include "carray.h"
|
14
|
+
|
15
|
+
boolean8_t
|
16
|
+
OBJ2BOOL (VALUE v)
|
17
|
+
{
|
18
|
+
if ( v == Qfalse || v == Qnil ) {
|
19
|
+
return 0;
|
20
|
+
}
|
21
|
+
else if ( v == Qtrue ) {
|
22
|
+
return 1;
|
23
|
+
}
|
24
|
+
else if ( rb_obj_is_kind_of(v, rb_cInteger) ) {
|
25
|
+
int flag = NUM2LONG(v);
|
26
|
+
if ( flag == 0 || flag == 1 ) {
|
27
|
+
return flag;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
{
|
31
|
+
VALUE inspect = rb_inspect(v);
|
32
|
+
rb_raise(rb_eCADataTypeError,
|
33
|
+
"can't cast object '%s' to <boolean>", StringValuePtr(inspect));
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
VALUE
|
38
|
+
BOOL2OBJ (boolean8_t x)
|
39
|
+
{
|
40
|
+
return ( x != 0 ) ? INT2FIX(1) : INT2FIX(0);
|
41
|
+
}
|
42
|
+
|
43
|
+
double
|
44
|
+
OBJ2DBL (VALUE val)
|
45
|
+
{
|
46
|
+
switch ( TYPE(val) ) {
|
47
|
+
case T_FLOAT:
|
48
|
+
return NUM2DBL(val);
|
49
|
+
case T_NIL:
|
50
|
+
return 0.0/0.0;
|
51
|
+
/* rb_raise(rb_eTypeError, "no implicit conversion to float from nil"); */
|
52
|
+
break;
|
53
|
+
case T_STRING: {
|
54
|
+
volatile VALUE rstr = rb_funcall(val, rb_intern("strip"), 0);
|
55
|
+
char *str = StringValuePtr(rstr);
|
56
|
+
if ( ! strncasecmp("nan", str, 3) ) {
|
57
|
+
return 0.0/0.0;
|
58
|
+
}
|
59
|
+
else if ( ! strncasecmp("inf", str, 3) ) {
|
60
|
+
return 1.0/0.0;
|
61
|
+
}
|
62
|
+
else if ( ! strncasecmp("-inf", str, 4) ) {
|
63
|
+
return -1.0/0.0;
|
64
|
+
}
|
65
|
+
else if ( ! strncasecmp("infinity", str, 8) ) {
|
66
|
+
return 1.0/0.0;
|
67
|
+
}
|
68
|
+
else if ( ! strncasecmp("-infinity", str, 9) ) {
|
69
|
+
return -1.0/0.0;
|
70
|
+
}
|
71
|
+
return NUM2DBL(rb_Float(val));
|
72
|
+
}
|
73
|
+
default:
|
74
|
+
return NUM2DBL(rb_Float(val));
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
long
|
79
|
+
rb_obj2long (VALUE val)
|
80
|
+
{
|
81
|
+
switch ( TYPE(val) ) {
|
82
|
+
case T_FIXNUM:
|
83
|
+
return FIX2LONG(val);
|
84
|
+
case T_BIGNUM:
|
85
|
+
return (long) NUM2LL(val);
|
86
|
+
case T_NIL:
|
87
|
+
rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
|
88
|
+
break;
|
89
|
+
default:
|
90
|
+
return NUM2LONG(rb_Integer(val));
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
unsigned long
|
95
|
+
rb_obj2ulong (VALUE val)
|
96
|
+
{
|
97
|
+
switch ( TYPE(val) ) {
|
98
|
+
case T_FIXNUM:
|
99
|
+
return FIX2ULONG(val);
|
100
|
+
case T_BIGNUM:
|
101
|
+
return (unsigned long) rb_num2ull(val);
|
102
|
+
case T_NIL:
|
103
|
+
rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
|
104
|
+
break;
|
105
|
+
default:
|
106
|
+
return NUM2ULONG(rb_Integer(val));
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
long long
|
111
|
+
rb_obj2ll (VALUE val)
|
112
|
+
{
|
113
|
+
switch ( TYPE(val) ) {
|
114
|
+
case T_FIXNUM:
|
115
|
+
return FIX2LONG(val);
|
116
|
+
case T_NIL:
|
117
|
+
rb_raise(rb_eTypeError, "no implicit conversion fron nil to integer");
|
118
|
+
break;
|
119
|
+
default:
|
120
|
+
return NUM2LL(rb_Integer(val));
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
unsigned long long
|
125
|
+
rb_obj2ull (VALUE val)
|
126
|
+
{
|
127
|
+
switch ( TYPE(val) ) {
|
128
|
+
case T_FIXNUM:
|
129
|
+
return FIX2ULONG(val);
|
130
|
+
case T_NIL:
|
131
|
+
rb_raise(rb_eTypeError, "no implicit conversion from nil to integer");
|
132
|
+
break;
|
133
|
+
default:
|
134
|
+
return rb_num2ull(rb_Integer(val));
|
135
|
+
}
|
136
|
+
}
|
137
|
+
|
138
|
+
void
|
139
|
+
ca_cast_block (int32_t n, void *ap1, void *ptr1,
|
140
|
+
void *ap2, void *ptr2)
|
141
|
+
{
|
142
|
+
CArray *ca1 = (CArray *) ap1;
|
143
|
+
CArray *ca2 = (CArray *) ap2;
|
144
|
+
if ( n < 0 ) {
|
145
|
+
rb_raise(rb_eRuntimeError,
|
146
|
+
"[BUG] in ca_cast_block(): negative count");
|
147
|
+
}
|
148
|
+
ca_cast_func_table[ca1->data_type][ca2->data_type](n, ca1, ptr1, ca2, ptr2, NULL);
|
149
|
+
}
|
150
|
+
|
151
|
+
void
|
152
|
+
ca_cast_block_with_mask (int32_t n, void *ap1, void *ptr1,
|
153
|
+
void *ap2, void *ptr2, boolean8_t *m)
|
154
|
+
{
|
155
|
+
CArray *ca1 = (CArray *) ap1;
|
156
|
+
CArray *ca2 = (CArray *) ap2;
|
157
|
+
if ( n < 0 ) {
|
158
|
+
rb_raise(rb_eRuntimeError,
|
159
|
+
"[BUG] in ca_cast_block_with_mask(): negative count");
|
160
|
+
}
|
161
|
+
ca_cast_func_table[ca1->data_type][ca2->data_type](n, ca1, ptr1, ca2, ptr2, m);
|
162
|
+
}
|
163
|
+
|
164
|
+
VALUE
|
165
|
+
ca_ptr2obj (void *ap, void *ptr)
|
166
|
+
{
|
167
|
+
volatile VALUE obj;
|
168
|
+
static CArray dummy;
|
169
|
+
CArray *ca = (CArray *) ap;
|
170
|
+
dummy.data_type = CA_OBJECT;
|
171
|
+
ca_cast_func_table[ca->data_type][CA_OBJECT](1, ca, ptr, &dummy, (void*)&obj, NULL);
|
172
|
+
return obj;
|
173
|
+
}
|
174
|
+
|
175
|
+
void
|
176
|
+
ca_obj2ptr (void *ap, VALUE obj, void *ptr)
|
177
|
+
{
|
178
|
+
CArray *ca = (CArray *)ap;
|
179
|
+
static CArray dummy;
|
180
|
+
dummy.data_type = CA_OBJECT;
|
181
|
+
ca_cast_func_table[CA_OBJECT][ca->data_type](1, &dummy, &obj, ca, ptr, NULL);
|
182
|
+
return;
|
183
|
+
}
|
184
|
+
|
185
|
+
void
|
186
|
+
ca_ptr2ptr (void *ap1, void *ptr1, void *ap2, void *ptr2)
|
187
|
+
{
|
188
|
+
CArray *ca1 = (CArray *)ap1;
|
189
|
+
CArray *ca2 = (CArray *)ap2;
|
190
|
+
ca_cast_func_table[ca1->data_type][ca2->data_type](1, ca1, ptr1, ca2, ptr2, NULL);
|
191
|
+
return;
|
192
|
+
}
|
193
|
+
|
194
|
+
void
|
195
|
+
ca_ptr2val (void *ap1, void *ptr1, int8_t data_type, void *ptr2)
|
196
|
+
{
|
197
|
+
CArray *ca1 = (CArray *)ap1;
|
198
|
+
static CArray dummy;
|
199
|
+
CA_CHECK_DATA_TYPE(data_type);
|
200
|
+
dummy.data_type = data_type;
|
201
|
+
ca_cast_func_table[ca1->data_type][data_type](1, ca1, ptr1, &dummy, ptr2, NULL);
|
202
|
+
return;
|
203
|
+
}
|
204
|
+
|
205
|
+
void
|
206
|
+
ca_val2ptr (int8_t data_type, void *ptr1, void *ap2, void *ptr2)
|
207
|
+
{
|
208
|
+
CArray *ca2 = (CArray *)ap2;
|
209
|
+
static CArray dummy;
|
210
|
+
CA_CHECK_DATA_TYPE(data_type);
|
211
|
+
dummy.data_type = data_type;
|
212
|
+
ca_cast_func_table[data_type][ca2->data_type](1, &dummy, ptr1, ca2, ptr2, NULL);
|
213
|
+
return;
|
214
|
+
}
|
215
|
+
|
216
|
+
void
|
217
|
+
ca_val2val (int8_t data_type1, void *ptr1, int8_t data_type2, void *ptr2)
|
218
|
+
{
|
219
|
+
static CArray dummy1, dummy2;
|
220
|
+
CA_CHECK_DATA_TYPE(data_type1);
|
221
|
+
CA_CHECK_DATA_TYPE(data_type2);
|
222
|
+
dummy1.data_type = data_type1;
|
223
|
+
dummy2.data_type = data_type2;
|
224
|
+
ca_cast_func_table[data_type1][data_type2](1, &dummy1, ptr1, &dummy2, ptr2, NULL);
|
225
|
+
return;
|
226
|
+
}
|
227
|
+
|
228
|
+
/* --------------------------------------------------------------------- */
|
229
|
+
|
230
|
+
static VALUE
|
231
|
+
rb_ca_data_class_to_object (VALUE self)
|
232
|
+
{
|
233
|
+
volatile VALUE obj;
|
234
|
+
CArray *ca;
|
235
|
+
int i;
|
236
|
+
|
237
|
+
Data_Get_Struct(self, CArray, ca);
|
238
|
+
|
239
|
+
if ( ca_is_scalar(ca) ) {
|
240
|
+
obj = rb_cscalar_new(CA_OBJECT, 0, ca->mask);
|
241
|
+
}
|
242
|
+
else {
|
243
|
+
obj = rb_carray_new(CA_OBJECT, ca->rank, ca->dim, 0, ca->mask);
|
244
|
+
}
|
245
|
+
|
246
|
+
for (i=0; i<ca->elements; i++) {
|
247
|
+
rb_ca_store_addr(obj, i, rb_ca_fetch_addr(self, i));
|
248
|
+
}
|
249
|
+
|
250
|
+
return obj;
|
251
|
+
}
|
252
|
+
|
253
|
+
static VALUE
|
254
|
+
rb_ca_object_to_data_class (VALUE self, VALUE rtype, int32_t bytes)
|
255
|
+
{
|
256
|
+
volatile VALUE obj, rval;
|
257
|
+
CArray *ca;
|
258
|
+
int i;
|
259
|
+
ID id_encode = rb_intern("encode");
|
260
|
+
|
261
|
+
Data_Get_Struct(self, CArray, ca);
|
262
|
+
|
263
|
+
if ( ca_is_scalar(ca) ) {
|
264
|
+
obj = rb_cscalar_new(CA_FIXLEN, bytes, ca->mask);
|
265
|
+
}
|
266
|
+
else {
|
267
|
+
obj = rb_carray_new(CA_FIXLEN, ca->rank, ca->dim, bytes, ca->mask);
|
268
|
+
}
|
269
|
+
rb_ca_data_type_import(obj, rtype);
|
270
|
+
|
271
|
+
for (i=0; i<ca->elements; i++) {
|
272
|
+
rval = rb_ca_fetch_addr(self, i);
|
273
|
+
if ( TYPE(rval) == T_STRING ) {
|
274
|
+
rb_ca_store_addr(obj, i, rval);
|
275
|
+
}
|
276
|
+
else {
|
277
|
+
rb_ca_store_addr(obj, i, rb_funcall(rval, id_encode, 0));
|
278
|
+
}
|
279
|
+
}
|
280
|
+
|
281
|
+
return obj;
|
282
|
+
}
|
283
|
+
|
284
|
+
/* CArray#to_type */
|
285
|
+
|
286
|
+
static VALUE
|
287
|
+
rb_ca_to_type_internal (int argc, VALUE *argv, VALUE self)
|
288
|
+
{
|
289
|
+
volatile VALUE obj, rtype = Qnil, ropt, rbytes = Qnil;
|
290
|
+
CArray *ca, *cb;
|
291
|
+
int8_t data_type;
|
292
|
+
int32_t bytes;
|
293
|
+
|
294
|
+
Data_Get_Struct(self, CArray, ca);
|
295
|
+
|
296
|
+
rb_scan_args(argc, argv, "11", &rtype, &ropt);
|
297
|
+
rb_scan_options(ropt, "bytes", &rbytes);
|
298
|
+
|
299
|
+
rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
|
300
|
+
|
301
|
+
if ( rb_ca_has_data_class(self) && data_type == CA_OBJECT ) {
|
302
|
+
return rb_ca_data_class_to_object(self);
|
303
|
+
}
|
304
|
+
|
305
|
+
if ( rb_ca_is_object_type(self) && rb_obj_is_data_class(rtype) ) {
|
306
|
+
return rb_ca_object_to_data_class(self, rtype, bytes);
|
307
|
+
}
|
308
|
+
|
309
|
+
ca_update_mask(ca);
|
310
|
+
|
311
|
+
if ( ca_is_scalar(ca) ) {
|
312
|
+
obj = rb_cscalar_new(data_type, bytes, ca->mask);
|
313
|
+
}
|
314
|
+
else {
|
315
|
+
obj = rb_carray_new(data_type, ca->rank, ca->dim, bytes, ca->mask);
|
316
|
+
}
|
317
|
+
|
318
|
+
rb_ca_data_type_import(obj, rtype);
|
319
|
+
|
320
|
+
Data_Get_Struct(obj, CArray, cb);
|
321
|
+
|
322
|
+
ca_attach(ca);
|
323
|
+
if ( ca_has_mask(ca) ) {
|
324
|
+
ca_cast_block_with_mask(cb->elements, ca, ca->ptr, cb, cb->ptr,
|
325
|
+
(boolean8_t*)ca->mask->ptr);
|
326
|
+
}
|
327
|
+
else {
|
328
|
+
ca_cast_block(cb->elements, ca, ca->ptr, cb, cb->ptr);
|
329
|
+
}
|
330
|
+
ca_detach(ca);
|
331
|
+
|
332
|
+
return obj;
|
333
|
+
}
|
334
|
+
|
335
|
+
VALUE
|
336
|
+
rb_ca_to_type (VALUE self, VALUE rtype, VALUE rbytes)
|
337
|
+
{
|
338
|
+
volatile VALUE ropt = rb_hash_new();
|
339
|
+
VALUE args[2] = { rtype, ropt };
|
340
|
+
rb_set_options(ropt, "bytes", rbytes);
|
341
|
+
return rb_ca_to_type_internal(2, args, self);
|
342
|
+
}
|
343
|
+
|
344
|
+
#define rb_ca_to_type_method(type, code) \
|
345
|
+
rb_ca_to_## type (VALUE self) \
|
346
|
+
{ \
|
347
|
+
VALUE rcode = INT2NUM(code); \
|
348
|
+
return rb_ca_to_type_internal(1, &rcode, self); \
|
349
|
+
}
|
350
|
+
|
351
|
+
/*
|
352
|
+
CArray#fixlen([:bytes=>bytes]])
|
353
|
+
*/
|
354
|
+
|
355
|
+
VALUE
|
356
|
+
rb_ca_to_fixlen (int argc, VALUE *argv, VALUE self)
|
357
|
+
{
|
358
|
+
volatile VALUE ropt = rb_pop_options(&argc, &argv);
|
359
|
+
VALUE list[2];
|
360
|
+
rb_scan_args(argc, argv, "0");
|
361
|
+
list[0] = INT2NUM(CA_FIXLEN);
|
362
|
+
list[1] = ropt;
|
363
|
+
return rb_ca_to_type_internal(2, list, self);
|
364
|
+
}
|
365
|
+
|
366
|
+
VALUE rb_ca_to_type_method(boolean, CA_BOOLEAN);
|
367
|
+
VALUE rb_ca_to_type_method(int8, CA_INT8);
|
368
|
+
VALUE rb_ca_to_type_method(uint8, CA_UINT8);
|
369
|
+
VALUE rb_ca_to_type_method(int16, CA_INT16);
|
370
|
+
VALUE rb_ca_to_type_method(uint16, CA_UINT16);
|
371
|
+
VALUE rb_ca_to_type_method(int32, CA_INT32);
|
372
|
+
VALUE rb_ca_to_type_method(uint32, CA_UINT32);
|
373
|
+
VALUE rb_ca_to_type_method(int64, CA_INT64);
|
374
|
+
VALUE rb_ca_to_type_method(uint64, CA_UINT64);
|
375
|
+
VALUE rb_ca_to_type_method(float32, CA_FLOAT32);
|
376
|
+
VALUE rb_ca_to_type_method(float64, CA_FLOAT64);
|
377
|
+
VALUE rb_ca_to_type_method(float128, CA_FLOAT128);
|
378
|
+
VALUE rb_ca_to_type_method(cmplx64, CA_CMPLX64);
|
379
|
+
VALUE rb_ca_to_type_method(cmplx128, CA_CMPLX128);
|
380
|
+
VALUE rb_ca_to_type_method(cmplx256, CA_CMPLX256);
|
381
|
+
VALUE rb_ca_to_type_method(VALUE, CA_OBJECT);
|
382
|
+
|
383
|
+
/* rdoc:
|
384
|
+
class CArray
|
385
|
+
# call-seq:
|
386
|
+
# ca.boolean
|
387
|
+
# ca.int8
|
388
|
+
# ca.uint8
|
389
|
+
# ca.int16
|
390
|
+
# ca.uint16
|
391
|
+
# ca.int32
|
392
|
+
# ca.uint32
|
393
|
+
# ca.int64
|
394
|
+
# ca.uint64
|
395
|
+
# ca.float32
|
396
|
+
# ca.float64
|
397
|
+
# ca.float128
|
398
|
+
# ca.cmplx64
|
399
|
+
# ca.cmplx128
|
400
|
+
# ca.cmplx256
|
401
|
+
# ca.object
|
402
|
+
# ca.fixlen
|
403
|
+
# ca.byte
|
404
|
+
# ca.short
|
405
|
+
# ca.int
|
406
|
+
# ca.float
|
407
|
+
# ca.double
|
408
|
+
# ca.complex
|
409
|
+
# ca.dcomplex
|
410
|
+
#
|
411
|
+
# Convert to given data_type with copying values
|
412
|
+
def to_type
|
413
|
+
end
|
414
|
+
end
|
415
|
+
*/
|
416
|
+
|
417
|
+
|
418
|
+
/* ------------------------------------------------------------------------*/
|
419
|
+
|
420
|
+
/* CArray#as_type */
|
421
|
+
|
422
|
+
static VALUE
|
423
|
+
rb_ca_as_type_internal (int argc, VALUE *argv, VALUE self)
|
424
|
+
{
|
425
|
+
volatile VALUE obj, rtype = Qnil, ropt, rbytes = Qnil;
|
426
|
+
CArray *ca;
|
427
|
+
int8_t data_type;
|
428
|
+
int32_t bytes;
|
429
|
+
|
430
|
+
rb_scan_args(argc, argv, "11", &rtype, &ropt);
|
431
|
+
rb_scan_options(ropt, "bytes", &rbytes);
|
432
|
+
|
433
|
+
rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
|
434
|
+
|
435
|
+
Data_Get_Struct(self, CArray, ca);
|
436
|
+
if ( ca->data_type == data_type ) {
|
437
|
+
if ( ! ca_is_fixlen_type(ca) ) {
|
438
|
+
return self;
|
439
|
+
}
|
440
|
+
}
|
441
|
+
|
442
|
+
obj = rb_ca_fake_type(self, rtype, rbytes);
|
443
|
+
rb_ca_data_type_import(obj, rtype);
|
444
|
+
|
445
|
+
return obj;
|
446
|
+
}
|
447
|
+
|
448
|
+
VALUE
|
449
|
+
rb_ca_as_type (VALUE self, VALUE rtype, VALUE rbytes)
|
450
|
+
{
|
451
|
+
volatile VALUE ropt = rb_hash_new();
|
452
|
+
VALUE args[2] = { rtype, ropt };
|
453
|
+
rb_set_options(ropt, "bytes", rbytes);
|
454
|
+
return rb_ca_as_type_internal(2, args, self);
|
455
|
+
}
|
456
|
+
|
457
|
+
#define rb_ca_as_type_method(type, code) \
|
458
|
+
rb_ca_as_## type (VALUE self) \
|
459
|
+
{ \
|
460
|
+
VALUE rcode = INT2NUM(code); \
|
461
|
+
return rb_ca_as_type_internal(1, &rcode, self); \
|
462
|
+
}
|
463
|
+
|
464
|
+
/*
|
465
|
+
CArray#as_fixlen([:bytes=>bytes]])
|
466
|
+
*/
|
467
|
+
|
468
|
+
VALUE
|
469
|
+
rb_ca_as_fixlen (int argc, VALUE *argv, VALUE self)
|
470
|
+
{
|
471
|
+
volatile VALUE rtype, ropt = rb_pop_options(&argc, &argv);
|
472
|
+
VALUE list[2];
|
473
|
+
rb_scan_args(argc, argv, "01", &rtype);
|
474
|
+
list[0] = ( NIL_P(rtype) ) ? INT2NUM(CA_FIXLEN) : rtype;
|
475
|
+
list[1] = ropt;
|
476
|
+
return rb_ca_as_type_internal(2, list, self);
|
477
|
+
}
|
478
|
+
|
479
|
+
VALUE rb_ca_as_type_method(boolean, CA_BOOLEAN);
|
480
|
+
VALUE rb_ca_as_type_method(int8, CA_INT8);
|
481
|
+
VALUE rb_ca_as_type_method(uint8, CA_UINT8);
|
482
|
+
VALUE rb_ca_as_type_method(int16, CA_INT16);
|
483
|
+
VALUE rb_ca_as_type_method(uint16, CA_UINT16);
|
484
|
+
VALUE rb_ca_as_type_method(int32, CA_INT32);
|
485
|
+
VALUE rb_ca_as_type_method(uint32, CA_UINT32);
|
486
|
+
VALUE rb_ca_as_type_method(int64, CA_INT64);
|
487
|
+
VALUE rb_ca_as_type_method(uint64, CA_UINT64);
|
488
|
+
VALUE rb_ca_as_type_method(float32, CA_FLOAT32);
|
489
|
+
VALUE rb_ca_as_type_method(float64, CA_FLOAT64);
|
490
|
+
VALUE rb_ca_as_type_method(float128, CA_FLOAT128);
|
491
|
+
VALUE rb_ca_as_type_method(cmplx64, CA_CMPLX64);
|
492
|
+
VALUE rb_ca_as_type_method(cmplx128, CA_CMPLX128);
|
493
|
+
VALUE rb_ca_as_type_method(cmplx256, CA_CMPLX256);
|
494
|
+
VALUE rb_ca_as_type_method(VALUE, CA_OBJECT);
|
495
|
+
|
496
|
+
|
497
|
+
/* rdoc:
|
498
|
+
class CArray
|
499
|
+
# call-seq:
|
500
|
+
# ca.as_int8
|
501
|
+
# ca.as_uint8
|
502
|
+
# ca.as_int16
|
503
|
+
# ca.as_uint16
|
504
|
+
# ca.as_int32
|
505
|
+
# ca.as_uint32
|
506
|
+
# ca.as_int64
|
507
|
+
# ca.as_uint64
|
508
|
+
# ca.as_float32
|
509
|
+
# ca.as_float64
|
510
|
+
# ca.as_float128
|
511
|
+
# ca.as_cmplx64
|
512
|
+
# ca.as_cmplx128
|
513
|
+
# ca.as_cmplx256
|
514
|
+
# ca.as_object
|
515
|
+
# ca.as_byte
|
516
|
+
# ca.as_short
|
517
|
+
# ca.as_int
|
518
|
+
# ca.as_float
|
519
|
+
# ca.as_double
|
520
|
+
# ca.as_complex
|
521
|
+
# ca.as_dcomplex
|
522
|
+
#
|
523
|
+
# Fakes data_type
|
524
|
+
def as_type
|
525
|
+
end
|
526
|
+
end
|
527
|
+
*/
|
528
|
+
|
529
|
+
|
530
|
+
/* ------------------------------------------------------------------------*/
|
531
|
+
|
532
|
+
VALUE
|
533
|
+
rb_ca_cast_block (int32_t n, VALUE ra1, void *ptr1,
|
534
|
+
VALUE ra2, void *ptr2)
|
535
|
+
{
|
536
|
+
CArray *ca1, *ca2;
|
537
|
+
Data_Get_Struct(ra1, CArray, ca1);
|
538
|
+
Data_Get_Struct(ra2, CArray, ca2);
|
539
|
+
if ( n < 0 ) {
|
540
|
+
rb_raise(rb_eRuntimeError, "[BUG] in rb_ca_cast_block: negative count");
|
541
|
+
}
|
542
|
+
ca_cast_func_table[ca1->data_type][ca2->data_type](n, ca1, ptr1, ca2, ptr2, NULL);
|
543
|
+
return Qnil;
|
544
|
+
}
|
545
|
+
|
546
|
+
VALUE
|
547
|
+
rb_ca_ptr2ptr (VALUE ra1, void *ptr1, VALUE ra2, void *ptr2)
|
548
|
+
{
|
549
|
+
CArray *ca1, *ca2;
|
550
|
+
Data_Get_Struct(ra1, CArray, ca1);
|
551
|
+
Data_Get_Struct(ra2, CArray, ca2);
|
552
|
+
ca_cast_func_table[ca1->data_type][ca2->data_type](1, ca1, ptr1, ca2, ptr2, NULL);
|
553
|
+
return Qnil;
|
554
|
+
}
|
555
|
+
|
556
|
+
|
557
|
+
VALUE
|
558
|
+
rb_ca_ptr2obj (VALUE self, void *ptr)
|
559
|
+
{
|
560
|
+
volatile VALUE obj;
|
561
|
+
static CArray dummy;
|
562
|
+
CArray *ca;
|
563
|
+
Data_Get_Struct(self, CArray, ca);
|
564
|
+
dummy.data_type = CA_OBJECT;
|
565
|
+
ca_cast_func_table[ca->data_type][CA_OBJECT](1, ca, ptr, &dummy, (void*)&obj, NULL);
|
566
|
+
if ( ca_is_fixlen_type(ca) ) {
|
567
|
+
OBJ_TAINT(obj);
|
568
|
+
return rb_ca_data_class_decode(self, obj);
|
569
|
+
}
|
570
|
+
else {
|
571
|
+
return obj;
|
572
|
+
}
|
573
|
+
}
|
574
|
+
|
575
|
+
VALUE
|
576
|
+
rb_ca_obj2ptr (VALUE self, VALUE obj, void *ptr)
|
577
|
+
{
|
578
|
+
static CArray dummy;
|
579
|
+
CArray *ca;
|
580
|
+
Data_Get_Struct(self, CArray, ca);
|
581
|
+
if ( obj == CA_UNDEF ) {
|
582
|
+
memset(ptr, 0, ca->bytes);
|
583
|
+
}
|
584
|
+
else {
|
585
|
+
obj = ( ca_is_fixlen_type(ca) ) ? rb_ca_data_class_encode(self, obj) : obj;
|
586
|
+
dummy.data_type = CA_OBJECT;
|
587
|
+
ca_cast_func_table[CA_OBJECT][ca->data_type](1, &dummy, &obj, ca, ptr, NULL);
|
588
|
+
}
|
589
|
+
return Qnil;
|
590
|
+
}
|
591
|
+
|
592
|
+
VALUE
|
593
|
+
rb_ca_wrap_writable (VALUE arg, VALUE rtype)
|
594
|
+
{
|
595
|
+
volatile VALUE obj = arg;
|
596
|
+
CArray *ca = NULL;
|
597
|
+
int8_t data_type;
|
598
|
+
|
599
|
+
if ( rb_obj_is_carray(obj) ) { /* obj == carray */
|
600
|
+
Data_Get_Struct(obj, CArray, ca);
|
601
|
+
if ( ca_is_readonly(ca) ) {
|
602
|
+
rb_raise(rb_eRuntimeError, "can't modify read-only carray");
|
603
|
+
}
|
604
|
+
if ( NIL_P(rtype) ) {
|
605
|
+
data_type = ca->data_type;
|
606
|
+
}
|
607
|
+
else {
|
608
|
+
data_type = rb_ca_guess_type(rtype);
|
609
|
+
}
|
610
|
+
if ( ca->data_type != data_type ) {
|
611
|
+
obj = rb_ca_fake_type(obj, rtype, Qnil);
|
612
|
+
}
|
613
|
+
}
|
614
|
+
else if ( NIL_P(obj) ) { /* obj == nil */
|
615
|
+
if ( NIL_P(rtype) ) {
|
616
|
+
data_type = CA_OBJECT;
|
617
|
+
}
|
618
|
+
else {
|
619
|
+
data_type = rb_ca_guess_type(rtype);
|
620
|
+
}
|
621
|
+
obj = rb_cscalar_new(data_type, 0, NULL);
|
622
|
+
}
|
623
|
+
else if ( rb_respond_to(obj, rb_intern("ca")) ) { /* respond_to obj.ca */
|
624
|
+
obj = rb_funcall(obj, rb_intern("ca"), 0);
|
625
|
+
Data_Get_Struct(obj, CArray, ca);
|
626
|
+
if ( NIL_P(rtype) ) {
|
627
|
+
data_type = ca->data_type;
|
628
|
+
}
|
629
|
+
else {
|
630
|
+
data_type = rb_ca_guess_type(rtype);
|
631
|
+
}
|
632
|
+
if ( ca->data_type != data_type ) {
|
633
|
+
obj = rb_ca_fake_type(obj, INT2NUM(data_type), Qnil);
|
634
|
+
}
|
635
|
+
}
|
636
|
+
else {
|
637
|
+
volatile VALUE inspect = rb_inspect(CLASS_OF(obj));
|
638
|
+
rb_raise(rb_eRuntimeError,
|
639
|
+
"given object '%s' can't be wrapped as carray",
|
640
|
+
StringValuePtr(inspect));
|
641
|
+
}
|
642
|
+
|
643
|
+
return obj;
|
644
|
+
}
|
645
|
+
|
646
|
+
/* rdoc:
|
647
|
+
def CArray.wrap_writable
|
648
|
+
end
|
649
|
+
*/
|
650
|
+
|
651
|
+
static VALUE
|
652
|
+
rb_ca_s_wrap_writable (int argc, VALUE *argv, VALUE klass)
|
653
|
+
{
|
654
|
+
volatile VALUE obj, rtype;
|
655
|
+
rb_scan_args(argc, argv, "11", &obj, &rtype);
|
656
|
+
return rb_ca_wrap_writable(obj, rtype);
|
657
|
+
}
|
658
|
+
|
659
|
+
VALUE
|
660
|
+
rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
|
661
|
+
{
|
662
|
+
volatile VALUE obj = arg;
|
663
|
+
CArray *ca = NULL;
|
664
|
+
int8_t data_type;
|
665
|
+
|
666
|
+
if ( rb_obj_is_carray(obj) ) { /* carray */
|
667
|
+
Data_Get_Struct(obj, CArray, ca);
|
668
|
+
if ( NIL_P(rtype) ) {
|
669
|
+
data_type = ca->data_type;
|
670
|
+
}
|
671
|
+
else {
|
672
|
+
data_type = rb_ca_guess_type(rtype);
|
673
|
+
}
|
674
|
+
if ( ca->data_type != data_type ) {
|
675
|
+
obj = rb_ca_fake_type(obj, rtype, Qnil);
|
676
|
+
}
|
677
|
+
}
|
678
|
+
else if ( rb_obj_is_kind_of(obj, rb_cNumeric) ) { /* number */
|
679
|
+
if ( NIL_P(rtype) ) {
|
680
|
+
data_type = CA_OBJECT;
|
681
|
+
}
|
682
|
+
else {
|
683
|
+
data_type = rb_ca_guess_type(rtype);
|
684
|
+
}
|
685
|
+
obj = rb_cscalar_new_with_value(data_type, 0, obj);
|
686
|
+
}
|
687
|
+
else if ( TYPE(obj) == T_ARRAY ) { /* array */
|
688
|
+
obj = rb_funcall(obj, rb_intern("to_ca"), 0);
|
689
|
+
Data_Get_Struct(obj, CArray, ca);
|
690
|
+
if ( NIL_P(rtype) ) {
|
691
|
+
data_type = CA_OBJECT;
|
692
|
+
}
|
693
|
+
else {
|
694
|
+
data_type = rb_ca_guess_type(rtype);
|
695
|
+
}
|
696
|
+
if ( ca->data_type != data_type ) {
|
697
|
+
obj = rb_ca_fake_type(obj, INT2NUM(data_type), Qnil);
|
698
|
+
}
|
699
|
+
}
|
700
|
+
else if ( TYPE(obj) == T_STRING ) { /* string */
|
701
|
+
if ( NIL_P(rtype) ) {
|
702
|
+
data_type = CA_OBJECT;
|
703
|
+
}
|
704
|
+
else {
|
705
|
+
data_type = rb_ca_guess_type(rtype);
|
706
|
+
}
|
707
|
+
if ( data_type == CA_OBJECT ) {
|
708
|
+
obj = rb_cscalar_new_with_value(data_type, 0, obj);
|
709
|
+
}
|
710
|
+
else {
|
711
|
+
rb_raise(rb_eCADataTypeError,
|
712
|
+
"can't convert string to %s array",
|
713
|
+
ca_type_name[data_type]);
|
714
|
+
}
|
715
|
+
}
|
716
|
+
else if ( NIL_P(obj) ) { /* nil */
|
717
|
+
if ( NIL_P(rtype) ) {
|
718
|
+
data_type = CA_OBJECT;
|
719
|
+
}
|
720
|
+
else {
|
721
|
+
data_type = rb_ca_guess_type(rtype);
|
722
|
+
}
|
723
|
+
obj = rb_cscalar_new(data_type, 0, NULL);
|
724
|
+
}
|
725
|
+
else if ( rb_respond_to(obj, rb_intern("ca")) ) {
|
726
|
+
obj = rb_funcall(obj, rb_intern("ca"), 0);
|
727
|
+
Data_Get_Struct(obj, CArray, ca);
|
728
|
+
if ( NIL_P(rtype) ) {
|
729
|
+
data_type = ca->data_type;
|
730
|
+
}
|
731
|
+
else {
|
732
|
+
data_type = rb_ca_guess_type(rtype);
|
733
|
+
}
|
734
|
+
if ( ca->data_type != data_type ) {
|
735
|
+
obj = rb_ca_fake_type(obj, INT2NUM(data_type), Qnil);
|
736
|
+
}
|
737
|
+
}
|
738
|
+
else if ( rb_respond_to(obj, rb_intern("to_ca")) ) {
|
739
|
+
obj = rb_funcall(obj, rb_intern("to_ca"), 0);
|
740
|
+
Data_Get_Struct(obj, CArray, ca);
|
741
|
+
if ( NIL_P(rtype) ) {
|
742
|
+
data_type = ca->data_type;
|
743
|
+
}
|
744
|
+
else {
|
745
|
+
data_type = rb_ca_guess_type(rtype);
|
746
|
+
}
|
747
|
+
if ( ca->data_type != data_type ) {
|
748
|
+
obj = rb_ca_fake_type(obj, INT2NUM(data_type), Qnil);
|
749
|
+
}
|
750
|
+
}
|
751
|
+
else { /* object */
|
752
|
+
if ( NIL_P(rtype) ) {
|
753
|
+
data_type = CA_OBJECT;
|
754
|
+
}
|
755
|
+
else {
|
756
|
+
data_type = rb_ca_guess_type(rtype);
|
757
|
+
}
|
758
|
+
obj = rb_cscalar_new_with_value(data_type, 0, obj);
|
759
|
+
}
|
760
|
+
|
761
|
+
return obj;
|
762
|
+
}
|
763
|
+
|
764
|
+
/* rdoc:
|
765
|
+
def CArray.wrap_readonly
|
766
|
+
end
|
767
|
+
*/
|
768
|
+
|
769
|
+
static VALUE
|
770
|
+
rb_ca_s_wrap_readonly (int argc, VALUE *argv, VALUE klass)
|
771
|
+
{
|
772
|
+
volatile VALUE obj, rtype;
|
773
|
+
rb_scan_args(argc, argv, "11", &obj, &rtype);
|
774
|
+
return rb_ca_wrap_readonly(obj, rtype);
|
775
|
+
}
|
776
|
+
|
777
|
+
VALUE
|
778
|
+
rb_ca_cast (volatile VALUE self)
|
779
|
+
{
|
780
|
+
volatile VALUE obj = self;
|
781
|
+
if ( ! rb_obj_is_carray(obj) ) {
|
782
|
+
switch ( TYPE(obj) ) {
|
783
|
+
case T_FIXNUM:
|
784
|
+
obj = rb_cscalar_new_with_value(CA_INT32, 0, obj);
|
785
|
+
break;
|
786
|
+
case T_BIGNUM:
|
787
|
+
if ( ca_valid[CA_INT64] ) {
|
788
|
+
obj = rb_cscalar_new_with_value(CA_INT64, 0, obj);
|
789
|
+
}
|
790
|
+
else {
|
791
|
+
obj = rb_cscalar_new_with_value(CA_INT32, 0, obj);
|
792
|
+
}
|
793
|
+
break;
|
794
|
+
case T_FLOAT:
|
795
|
+
obj = rb_cscalar_new_with_value(CA_FLOAT64, 0, obj);
|
796
|
+
break;
|
797
|
+
case T_TRUE:
|
798
|
+
case T_FALSE:
|
799
|
+
obj = rb_cscalar_new_with_value(CA_BOOLEAN, 0, obj);
|
800
|
+
break;
|
801
|
+
case T_ARRAY:
|
802
|
+
obj = rb_funcall(obj, rb_intern("to_ca"), 0);
|
803
|
+
break;
|
804
|
+
default:
|
805
|
+
if ( rb_obj_is_kind_of(obj, rb_cRange) ) {
|
806
|
+
obj = rb_funcall(obj, rb_intern("to_ca"), 0);
|
807
|
+
break;
|
808
|
+
}
|
809
|
+
#ifdef HAVE_COMPLEX_H
|
810
|
+
if ( rb_obj_is_kind_of(obj, rb_cCComplex) ) {
|
811
|
+
obj = rb_cscalar_new_with_value(CA_CMPLX128, 0, obj);
|
812
|
+
break;
|
813
|
+
}
|
814
|
+
#endif
|
815
|
+
obj = rb_cscalar_new_with_value(CA_OBJECT, 0, obj);
|
816
|
+
break;
|
817
|
+
}
|
818
|
+
}
|
819
|
+
return obj;
|
820
|
+
}
|
821
|
+
|
822
|
+
static VALUE
|
823
|
+
rb_ca_s_cast (VALUE klass, VALUE val)
|
824
|
+
{
|
825
|
+
return rb_ca_cast(val);
|
826
|
+
}
|
827
|
+
|
828
|
+
void
|
829
|
+
rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
|
830
|
+
{
|
831
|
+
CArray *ca, *cb;
|
832
|
+
int test;
|
833
|
+
int self_is_object = 0;
|
834
|
+
int other_is_object = 0;
|
835
|
+
|
836
|
+
if ( ! rb_obj_is_carray(*self) ) {
|
837
|
+
self_is_object = 1;
|
838
|
+
if ( rb_ca_is_object_type(*other) ) {
|
839
|
+
*self = rb_cscalar_new_with_value(CA_OBJECT, 0, *self);
|
840
|
+
return;
|
841
|
+
}
|
842
|
+
switch ( TYPE(*self) ) {
|
843
|
+
case T_FIXNUM:
|
844
|
+
*self = rb_cscalar_new_with_value(CA_INT32, 0, *self);
|
845
|
+
break;
|
846
|
+
case T_BIGNUM:
|
847
|
+
if ( ca_valid[CA_INT64] ) {
|
848
|
+
*self = rb_cscalar_new_with_value(CA_INT64, 0, *self);
|
849
|
+
}
|
850
|
+
else {
|
851
|
+
*self = rb_cscalar_new_with_value(CA_INT32, 0, *self);
|
852
|
+
}
|
853
|
+
break;
|
854
|
+
case T_FLOAT:
|
855
|
+
*self = rb_cscalar_new_with_value(CA_FLOAT64, 0, *self);
|
856
|
+
break;
|
857
|
+
case T_TRUE:
|
858
|
+
case T_FALSE:
|
859
|
+
*self = rb_cscalar_new_with_value(CA_BOOLEAN, 0, *self);
|
860
|
+
break;
|
861
|
+
default:
|
862
|
+
#ifdef HAVE_COMPLEX_H
|
863
|
+
if ( rb_obj_is_kind_of(*self, rb_cCComplex) ) {
|
864
|
+
*self = rb_cscalar_new_with_value(CA_CMPLX128, 0, *self);
|
865
|
+
break;
|
866
|
+
}
|
867
|
+
#endif
|
868
|
+
|
869
|
+
*self = rb_cscalar_new_with_value(CA_OBJECT, 0, *self);
|
870
|
+
break;
|
871
|
+
}
|
872
|
+
}
|
873
|
+
|
874
|
+
if ( ! rb_obj_is_carray(*other) ) {
|
875
|
+
other_is_object = 1;
|
876
|
+
|
877
|
+
if ( rb_ca_is_object_type(*self) ) {
|
878
|
+
*other = rb_cscalar_new_with_value(CA_OBJECT, 0, *other);
|
879
|
+
return;
|
880
|
+
}
|
881
|
+
|
882
|
+
switch ( TYPE(*other) ) {
|
883
|
+
case T_FIXNUM:
|
884
|
+
*other = rb_cscalar_new_with_value(CA_INT32, 0, *other);
|
885
|
+
break;
|
886
|
+
case T_BIGNUM:
|
887
|
+
if ( ca_valid[CA_INT64] ) {
|
888
|
+
*other = rb_cscalar_new_with_value(CA_INT64, 0, *other);
|
889
|
+
}
|
890
|
+
else {
|
891
|
+
*other = rb_cscalar_new_with_value(CA_INT32, 0, *other);
|
892
|
+
}
|
893
|
+
break;
|
894
|
+
case T_FLOAT:
|
895
|
+
*other = rb_cscalar_new_with_value(CA_FLOAT64, 0, *other);
|
896
|
+
break;
|
897
|
+
case T_TRUE:
|
898
|
+
case T_FALSE:
|
899
|
+
*other = rb_cscalar_new_with_value(CA_BOOLEAN, 0, *other);
|
900
|
+
break;
|
901
|
+
default:
|
902
|
+
#ifdef HAVE_COMPLEX_H
|
903
|
+
if ( rb_obj_is_kind_of(*other, rb_cCComplex) ) {
|
904
|
+
*other = rb_cscalar_new_with_value(CA_CMPLX128, 0, *other);
|
905
|
+
break;
|
906
|
+
}
|
907
|
+
#endif
|
908
|
+
|
909
|
+
*other = rb_cscalar_new_with_value(CA_OBJECT, 0, *other);
|
910
|
+
break;
|
911
|
+
}
|
912
|
+
}
|
913
|
+
|
914
|
+
Data_Get_Struct(*self, CArray, ca);
|
915
|
+
Data_Get_Struct(*other, CArray, cb);
|
916
|
+
|
917
|
+
if ( ca->obj_type == CA_OBJ_UNBOUND_REPEAT ) {
|
918
|
+
*self = ca_ubrep_bind_with(*self, *other);
|
919
|
+
Data_Get_Struct(*self, CArray, ca);
|
920
|
+
}
|
921
|
+
|
922
|
+
if ( cb->obj_type == CA_OBJ_UNBOUND_REPEAT ) {
|
923
|
+
*other = ca_ubrep_bind_with(*other, *self);
|
924
|
+
Data_Get_Struct(*other, CArray, cb);
|
925
|
+
}
|
926
|
+
|
927
|
+
if ( ca_is_scalar(ca) ^ ca_is_scalar(cb) ||
|
928
|
+
self_is_object ||
|
929
|
+
other_is_object ) {
|
930
|
+
if ( other_is_object ||
|
931
|
+
( ( ! other_is_object ) && ca_is_scalar(cb) ) ) {
|
932
|
+
test = ca_cast_table2[cb->data_type][ca->data_type];
|
933
|
+
if ( test == 0 ) {
|
934
|
+
return;
|
935
|
+
}
|
936
|
+
else if ( test > 0 ) {
|
937
|
+
*other = rb_ca_wrap_readonly(*other, INT2FIX(ca->data_type));
|
938
|
+
return;
|
939
|
+
}
|
940
|
+
}
|
941
|
+
if ( self_is_object ||
|
942
|
+
( ( ! self_is_object ) && ca_is_scalar(ca) ) ) {
|
943
|
+
test = ca_cast_table2[ca->data_type][cb->data_type];
|
944
|
+
if ( test == 0 ) {
|
945
|
+
return;
|
946
|
+
}
|
947
|
+
else if ( test > 0 ) {
|
948
|
+
*self = rb_ca_wrap_readonly(*self, INT2FIX(cb->data_type));
|
949
|
+
return;
|
950
|
+
}
|
951
|
+
}
|
952
|
+
}
|
953
|
+
|
954
|
+
test = ca_cast_table[cb->data_type][ca->data_type];
|
955
|
+
|
956
|
+
if ( test == 0 ) {
|
957
|
+
return;
|
958
|
+
}
|
959
|
+
else if ( test > 0 ) {
|
960
|
+
*other = rb_ca_wrap_readonly(*other, INT2FIX(ca->data_type));
|
961
|
+
return;
|
962
|
+
}
|
963
|
+
|
964
|
+
test = ca_cast_table[ca->data_type][cb->data_type];
|
965
|
+
|
966
|
+
if ( test > 0 ) {
|
967
|
+
*self = rb_ca_wrap_readonly(*self, INT2FIX(cb->data_type));
|
968
|
+
return;
|
969
|
+
}
|
970
|
+
|
971
|
+
rb_raise(rb_eRuntimeError,
|
972
|
+
"can't coerce carray with data_types of '%s' and '%s'",
|
973
|
+
ca_type_name[ca->data_type],
|
974
|
+
ca_type_name[cb->data_type]);
|
975
|
+
}
|
976
|
+
|
977
|
+
/* rdoc
|
978
|
+
def CArray.cast_self_or_other
|
979
|
+
end
|
980
|
+
*/
|
981
|
+
|
982
|
+
VALUE
|
983
|
+
rb_ca_s_cast_self_or_other (VALUE klass, VALUE self, VALUE other)
|
984
|
+
{
|
985
|
+
rb_ca_cast_self_or_other(&self, &other);
|
986
|
+
return rb_assoc_new(self, other);
|
987
|
+
}
|
988
|
+
|
989
|
+
void
|
990
|
+
rb_ca_cast_other (VALUE *self, volatile VALUE *other)
|
991
|
+
{
|
992
|
+
CArray *ca, *cb;
|
993
|
+
CScalar *cs;
|
994
|
+
int test0, test1;
|
995
|
+
|
996
|
+
Data_Get_Struct(*self, CArray, ca);
|
997
|
+
|
998
|
+
if ( ! rb_obj_is_carray(*other) ) {
|
999
|
+
if ( rb_ca_is_object_type(*self) ) {
|
1000
|
+
*other = rb_cscalar_new_with_value(CA_OBJECT, 0, *other);
|
1001
|
+
return;
|
1002
|
+
}
|
1003
|
+
switch ( TYPE(*other) ) {
|
1004
|
+
case T_FIXNUM:
|
1005
|
+
*other = rb_cscalar_new_with_value(CA_INT32, 0, *other);
|
1006
|
+
break;
|
1007
|
+
case T_BIGNUM:
|
1008
|
+
if ( ca_valid[CA_INT64] ) {
|
1009
|
+
*other = rb_cscalar_new_with_value(CA_INT64, 0, *other);
|
1010
|
+
}
|
1011
|
+
else {
|
1012
|
+
*other = rb_cscalar_new_with_value(CA_INT32, 0, *other);
|
1013
|
+
}
|
1014
|
+
break;
|
1015
|
+
case T_FLOAT:
|
1016
|
+
*other = rb_cscalar_new_with_value(CA_FLOAT64, 0, *other);
|
1017
|
+
break;
|
1018
|
+
case T_TRUE:
|
1019
|
+
case T_FALSE:
|
1020
|
+
*other = rb_cscalar_new_with_value(CA_BOOLEAN, 0, *other);
|
1021
|
+
break;
|
1022
|
+
default:
|
1023
|
+
#ifdef HAVE_COMPLEX_H
|
1024
|
+
if ( rb_obj_is_kind_of(*other, rb_cCComplex) ) {
|
1025
|
+
*other = rb_cscalar_new_with_value(CA_CMPLX128, 0, *other);
|
1026
|
+
break;
|
1027
|
+
}
|
1028
|
+
#endif
|
1029
|
+
*other = rb_cscalar_new_with_value(CA_OBJECT, 0, *other);
|
1030
|
+
break;
|
1031
|
+
}
|
1032
|
+
|
1033
|
+
Data_Get_Struct(*other, CScalar, cs);
|
1034
|
+
|
1035
|
+
test0 = ca_cast_table2[cs->data_type][ca->data_type];
|
1036
|
+
|
1037
|
+
if ( test0 > 0 ) {
|
1038
|
+
*other = rb_ca_wrap_readonly(*other, INT2FIX(ca->data_type));
|
1039
|
+
}
|
1040
|
+
|
1041
|
+
}
|
1042
|
+
|
1043
|
+
Data_Get_Struct(*other, CArray, cb);
|
1044
|
+
|
1045
|
+
if ( cb->obj_type == CA_OBJ_UNBOUND_REPEAT ) {
|
1046
|
+
*other = ca_ubrep_bind_with(*other, *self);
|
1047
|
+
Data_Get_Struct(*other, CArray, cb);
|
1048
|
+
}
|
1049
|
+
|
1050
|
+
test1 = ca_cast_table[cb->data_type][ca->data_type];
|
1051
|
+
|
1052
|
+
if ( test1 == 0 ) {
|
1053
|
+
return;
|
1054
|
+
}
|
1055
|
+
else if ( test1 > 0 ) {
|
1056
|
+
*other = rb_ca_wrap_readonly(*other, INT2FIX(ca->data_type));
|
1057
|
+
return;
|
1058
|
+
}
|
1059
|
+
|
1060
|
+
rb_raise(rb_eRuntimeError,
|
1061
|
+
"can't coerce carray with data_types of '%s' and '%s'",
|
1062
|
+
ca_type_name[ca->data_type],
|
1063
|
+
ca_type_name[cb->data_type]);
|
1064
|
+
}
|
1065
|
+
|
1066
|
+
/* rdoc
|
1067
|
+
def CArray.cast_other
|
1068
|
+
end
|
1069
|
+
*/
|
1070
|
+
|
1071
|
+
VALUE
|
1072
|
+
rb_ca_s_cast_other (VALUE klass, VALUE self, VALUE other)
|
1073
|
+
{
|
1074
|
+
if ( rb_obj_is_carray(self) ) {
|
1075
|
+
rb_ca_cast_self_or_other(&self, &other);
|
1076
|
+
}
|
1077
|
+
else {
|
1078
|
+
rb_raise(rb_eRuntimeError, "first argument should be a carray");
|
1079
|
+
}
|
1080
|
+
return rb_assoc_new(self, other);
|
1081
|
+
}
|
1082
|
+
|
1083
|
+
void
|
1084
|
+
Init_carray_cast ()
|
1085
|
+
{
|
1086
|
+
/* CArray data_type conversion */
|
1087
|
+
|
1088
|
+
rb_define_method(rb_cCArray, "to_type", rb_ca_to_type_internal, -1);
|
1089
|
+
|
1090
|
+
rb_define_method(rb_cCArray, "fixlen", rb_ca_to_fixlen, -1);
|
1091
|
+
rb_define_method(rb_cCArray, "boolean", rb_ca_to_boolean, 0);
|
1092
|
+
rb_define_method(rb_cCArray, "int8", rb_ca_to_int8, 0);
|
1093
|
+
rb_define_method(rb_cCArray, "uint8", rb_ca_to_uint8, 0);
|
1094
|
+
rb_define_method(rb_cCArray, "int16", rb_ca_to_int16, 0);
|
1095
|
+
rb_define_method(rb_cCArray, "uint16", rb_ca_to_uint16, 0);
|
1096
|
+
rb_define_method(rb_cCArray, "int32", rb_ca_to_int32, 0);
|
1097
|
+
rb_define_method(rb_cCArray, "uint32", rb_ca_to_uint32, 0);
|
1098
|
+
rb_define_method(rb_cCArray, "int64", rb_ca_to_int64, 0);
|
1099
|
+
rb_define_method(rb_cCArray, "uint64", rb_ca_to_uint64, 0);
|
1100
|
+
rb_define_method(rb_cCArray, "float32", rb_ca_to_float32, 0);
|
1101
|
+
rb_define_method(rb_cCArray, "float64", rb_ca_to_float64, 0);
|
1102
|
+
rb_define_method(rb_cCArray, "float128", rb_ca_to_float128, 0);
|
1103
|
+
rb_define_method(rb_cCArray, "cmplx64", rb_ca_to_cmplx64, 0);
|
1104
|
+
rb_define_method(rb_cCArray, "cmplx128", rb_ca_to_cmplx128, 0);
|
1105
|
+
rb_define_method(rb_cCArray, "cmplx256", rb_ca_to_cmplx256, 0);
|
1106
|
+
rb_define_method(rb_cCArray, "object", rb_ca_to_VALUE, 0);
|
1107
|
+
|
1108
|
+
rb_define_method(rb_cCArray, "byte", rb_ca_to_uint8, 0);
|
1109
|
+
rb_define_method(rb_cCArray, "short", rb_ca_to_int16, 0);
|
1110
|
+
rb_define_method(rb_cCArray, "int", rb_ca_to_int32, 0);
|
1111
|
+
rb_define_method(rb_cCArray, "float", rb_ca_to_float32, 0);
|
1112
|
+
rb_define_method(rb_cCArray, "double", rb_ca_to_float64, 0);
|
1113
|
+
rb_define_method(rb_cCArray, "complex", rb_ca_to_cmplx64, 0);
|
1114
|
+
rb_define_method(rb_cCArray, "dcomplex", rb_ca_to_cmplx128, 0);
|
1115
|
+
|
1116
|
+
rb_define_method(rb_cCArray, "as_type", rb_ca_as_type_internal, -1);
|
1117
|
+
|
1118
|
+
rb_define_method(rb_cCArray, "as_fixlen", rb_ca_as_fixlen, -1);
|
1119
|
+
rb_define_method(rb_cCArray, "as_boolean", rb_ca_as_boolean, 0);
|
1120
|
+
rb_define_method(rb_cCArray, "as_int8", rb_ca_as_int8, 0);
|
1121
|
+
rb_define_method(rb_cCArray, "as_uint8", rb_ca_as_uint8, 0);
|
1122
|
+
rb_define_method(rb_cCArray, "as_int16", rb_ca_as_int16, 0);
|
1123
|
+
rb_define_method(rb_cCArray, "as_uint16", rb_ca_as_uint16, 0);
|
1124
|
+
rb_define_method(rb_cCArray, "as_int32", rb_ca_as_int32, 0);
|
1125
|
+
rb_define_method(rb_cCArray, "as_uint32", rb_ca_as_uint32, 0);
|
1126
|
+
rb_define_method(rb_cCArray, "as_int64", rb_ca_as_int64, 0);
|
1127
|
+
rb_define_method(rb_cCArray, "as_uint64", rb_ca_as_uint64, 0);
|
1128
|
+
rb_define_method(rb_cCArray, "as_float32", rb_ca_as_float32, 0);
|
1129
|
+
rb_define_method(rb_cCArray, "as_float64", rb_ca_as_float64, 0);
|
1130
|
+
rb_define_method(rb_cCArray, "as_float128", rb_ca_as_float128, 0);
|
1131
|
+
rb_define_method(rb_cCArray, "as_cmplx64", rb_ca_as_cmplx64, 0);
|
1132
|
+
rb_define_method(rb_cCArray, "as_cmplx128", rb_ca_as_cmplx128, 0);
|
1133
|
+
rb_define_method(rb_cCArray, "as_cmplx256", rb_ca_as_cmplx256, 0);
|
1134
|
+
rb_define_method(rb_cCArray, "as_object", rb_ca_as_VALUE, 0);
|
1135
|
+
|
1136
|
+
rb_define_method(rb_cCArray, "as_byte", rb_ca_as_uint8, 0);
|
1137
|
+
rb_define_method(rb_cCArray, "as_short", rb_ca_as_int16, 0);
|
1138
|
+
rb_define_method(rb_cCArray, "as_int", rb_ca_as_int32, 0);
|
1139
|
+
rb_define_method(rb_cCArray, "as_float", rb_ca_as_float32, 0);
|
1140
|
+
rb_define_method(rb_cCArray, "as_double", rb_ca_as_float64, 0);
|
1141
|
+
rb_define_method(rb_cCArray, "as_complex", rb_ca_as_cmplx64, 0);
|
1142
|
+
rb_define_method(rb_cCArray, "as_dcomplex", rb_ca_as_cmplx128, 0);
|
1143
|
+
|
1144
|
+
rb_define_singleton_method(rb_cCArray,
|
1145
|
+
"wrap_writable", rb_ca_s_wrap_writable, -1);
|
1146
|
+
rb_define_singleton_method(rb_cCArray,
|
1147
|
+
"wrap_readonly", rb_ca_s_wrap_readonly, -1);
|
1148
|
+
rb_define_singleton_method(rb_cCArray,
|
1149
|
+
"cast", rb_ca_s_cast, 1);
|
1150
|
+
rb_define_singleton_method(rb_cCArray,
|
1151
|
+
"cast_self_or_other", rb_ca_s_cast_self_or_other, 2);
|
1152
|
+
rb_define_singleton_method(rb_cCArray,
|
1153
|
+
"cast_other", rb_ca_s_cast_other, 2);
|
1154
|
+
}
|
1155
|
+
|