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_order.c
ADDED
@@ -0,0 +1,1040 @@
|
|
1
|
+
/* ---------------------------------------------------------------------------
|
2
|
+
|
3
|
+
carray_order.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 "ruby.h"
|
14
|
+
#include "carray.h"
|
15
|
+
#include <math.h>
|
16
|
+
#include <float.h>
|
17
|
+
|
18
|
+
static ID id_equal;
|
19
|
+
|
20
|
+
static VALUE
|
21
|
+
rb_ca_value_not_masked (VALUE self)
|
22
|
+
{
|
23
|
+
VALUE rval = rb_ca_value_array(self);
|
24
|
+
VALUE select = rb_ca_is_not_masked(self);
|
25
|
+
return rb_ca_fetch(rval, select);
|
26
|
+
}
|
27
|
+
|
28
|
+
/* ------------------------------------------------------------------- */
|
29
|
+
|
30
|
+
#define proc_project(type) \
|
31
|
+
{ \
|
32
|
+
type *p = (type*)co->ptr; \
|
33
|
+
type *q = (type*)ca->ptr; \
|
34
|
+
for (i=0; i<ci->elements; i++) { \
|
35
|
+
if ( *mii ) { \
|
36
|
+
*mio = 1; \
|
37
|
+
} \
|
38
|
+
n = *(ip+i); \
|
39
|
+
if ( n < 0 ) { \
|
40
|
+
if ( lfill ) { \
|
41
|
+
*(p+i) = *(type *)lfill; \
|
42
|
+
} \
|
43
|
+
else { \
|
44
|
+
*mio = 1; \
|
45
|
+
} \
|
46
|
+
} \
|
47
|
+
else if ( n >= map_elements ) { \
|
48
|
+
if ( ufill ) { \
|
49
|
+
*(p+i) = *(type *)ufill; \
|
50
|
+
} \
|
51
|
+
else { \
|
52
|
+
*mio = 1; \
|
53
|
+
} \
|
54
|
+
} \
|
55
|
+
else { \
|
56
|
+
if ( *(mia+n) ) { \
|
57
|
+
*mio = 1; \
|
58
|
+
} \
|
59
|
+
else { \
|
60
|
+
*(p+i) = *(q+n); \
|
61
|
+
} \
|
62
|
+
} \
|
63
|
+
mio++; mii++; \
|
64
|
+
} \
|
65
|
+
}
|
66
|
+
|
67
|
+
static void
|
68
|
+
ca_project_loop (CArray *co, CArray *ca, CArray *ci, char *lfill, char *ufill)
|
69
|
+
{
|
70
|
+
int32_t map_elements = ca->elements;
|
71
|
+
int32_t *ip = (int32_t*) ci->ptr;
|
72
|
+
int32_t n, i;
|
73
|
+
boolean8_t *mi, *ma;
|
74
|
+
boolean8_t *mio, *mii, *mia;
|
75
|
+
|
76
|
+
ca_create_mask(co);
|
77
|
+
mio = (boolean8_t *) co->mask->ptr;
|
78
|
+
mii = mi = ca_allocate_mask_iterator(1, ci);
|
79
|
+
mia = ma = ca_allocate_mask_iterator(1, ca);
|
80
|
+
|
81
|
+
switch ( co->bytes ) {
|
82
|
+
case 1: proc_project(int8_t); break;
|
83
|
+
case 2: proc_project(int16_t); break;
|
84
|
+
case 4: proc_project(int32_t); break;
|
85
|
+
case 8: proc_project(float64_t); break;
|
86
|
+
default:
|
87
|
+
{
|
88
|
+
char *p = co->ptr;
|
89
|
+
char *q = ca->ptr;
|
90
|
+
for (i=0; i<ci->elements; i++) {
|
91
|
+
if ( *mii ) {
|
92
|
+
*mio = 1;
|
93
|
+
}
|
94
|
+
n = *(ip+i);
|
95
|
+
if ( n < 0 ) {
|
96
|
+
if ( lfill ) {
|
97
|
+
memcpy(p + i * ca->bytes, lfill, ca->bytes);
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
*mio = 1;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
else if ( n >= map_elements ) {
|
104
|
+
if ( ufill ) {
|
105
|
+
memcpy(p + i * ca->bytes, ufill, ca->bytes);
|
106
|
+
}
|
107
|
+
else {
|
108
|
+
*mio = 1;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
else {
|
112
|
+
if ( *(mia+n) ) {
|
113
|
+
*mio = 1;
|
114
|
+
}
|
115
|
+
else {
|
116
|
+
memcpy(p + i * ca->bytes, q + n * ca->bytes, ca->bytes);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
mio++; mii++;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
break;
|
123
|
+
}
|
124
|
+
free(mi);
|
125
|
+
free(ma);
|
126
|
+
}
|
127
|
+
|
128
|
+
CArray *
|
129
|
+
ca_project (CArray *ca, CArray *ci, char *lfill, char *ufill)
|
130
|
+
{
|
131
|
+
CArray *co;
|
132
|
+
|
133
|
+
ca_attach_n(2, ca, ci); /* ATTACH */
|
134
|
+
|
135
|
+
co = carray_new(ca->data_type, ci->rank, ci->dim, ca->bytes, NULL);
|
136
|
+
ca_project_loop(co, ca, ci, lfill, ufill);
|
137
|
+
|
138
|
+
ca_detach_n(2, ca, ci); /* DETACH */
|
139
|
+
|
140
|
+
return co;
|
141
|
+
}
|
142
|
+
|
143
|
+
/* rdoc:
|
144
|
+
class CArray
|
145
|
+
def project (idx,lval=nil,uval=nil)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
*/
|
149
|
+
|
150
|
+
VALUE
|
151
|
+
rb_ca_project (int argc, VALUE *argv, VALUE self)
|
152
|
+
{
|
153
|
+
volatile VALUE obj, rindex, vlfval, vufval;
|
154
|
+
CArray *ca, *ci, *co;
|
155
|
+
char *lfval, *ufval;
|
156
|
+
|
157
|
+
rb_scan_args(argc, argv, "12", &rindex, &vlfval, &vufval);
|
158
|
+
|
159
|
+
Data_Get_Struct(self, CArray, ca);
|
160
|
+
|
161
|
+
rb_check_carray_object(rindex);
|
162
|
+
ci = ca_wrap_readonly(rindex, CA_INT32);
|
163
|
+
|
164
|
+
lfval = malloc_with_check(ca->bytes);
|
165
|
+
ufval = malloc_with_check(ca->bytes);
|
166
|
+
|
167
|
+
if ( ! NIL_P(vlfval) ) {
|
168
|
+
rb_ca_obj2ptr(self, vlfval, lfval);
|
169
|
+
rb_ca_obj2ptr(self, vlfval, ufval);
|
170
|
+
}
|
171
|
+
|
172
|
+
if ( ! NIL_P(vufval) ) {
|
173
|
+
rb_ca_obj2ptr(self, vufval, ufval);
|
174
|
+
}
|
175
|
+
|
176
|
+
co = ca_project(ca, ci,
|
177
|
+
( ! NIL_P(vlfval) ) ? lfval : NULL,
|
178
|
+
( ( ! NIL_P(vufval) ) || ( ! NIL_P(vlfval) ) ) ? ufval : NULL);
|
179
|
+
|
180
|
+
free(lfval);
|
181
|
+
free(ufval);
|
182
|
+
|
183
|
+
obj = ca_wrap_struct(co);
|
184
|
+
rb_ca_data_type_inherit(obj, self);
|
185
|
+
|
186
|
+
if ( ! ca_is_any_masked(co) ) {
|
187
|
+
obj = rb_ca_unmask_copy(obj);
|
188
|
+
}
|
189
|
+
|
190
|
+
return obj;
|
191
|
+
}
|
192
|
+
|
193
|
+
/* ----------------------------------------------------------------- */
|
194
|
+
|
195
|
+
#define proc_reverse_bang_mask() \
|
196
|
+
{ \
|
197
|
+
boolean8_t *p = (boolean8_t *)ca->mask->ptr; \
|
198
|
+
boolean8_t *q = (boolean8_t *)ca->mask->ptr + ca->elements - 1; \
|
199
|
+
boolean8_t v; \
|
200
|
+
for (; p<q; p++, q--) { \
|
201
|
+
v = *p; *p = *q; *q = v; \
|
202
|
+
} \
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
#define proc_reverse_bang(type) \
|
207
|
+
{ \
|
208
|
+
type *p = (type *)ca->ptr; \
|
209
|
+
type *q = (type *)ca->ptr + ca->elements - 1; \
|
210
|
+
type v; \
|
211
|
+
for (; p<q; p++, q--) { \
|
212
|
+
v = *p; *p = *q; *q = v; \
|
213
|
+
} \
|
214
|
+
}
|
215
|
+
|
216
|
+
#define proc_reverse_bang_data() \
|
217
|
+
{ \
|
218
|
+
int32_t bytes = ca->bytes; \
|
219
|
+
char *p = ca->ptr; \
|
220
|
+
char *q = ca->ptr + bytes * (ca->elements - 1); \
|
221
|
+
char *v = malloc_with_check(bytes); \
|
222
|
+
for (; p<q; p+=bytes, q-=bytes) { \
|
223
|
+
memcpy(v, p, bytes); \
|
224
|
+
memcpy(p, q, bytes); \
|
225
|
+
memcpy(q, v, bytes); \
|
226
|
+
} \
|
227
|
+
free(v); \
|
228
|
+
}
|
229
|
+
|
230
|
+
/* rdoc:
|
231
|
+
class CArray
|
232
|
+
# Reverses the elements of +ca+ in place.
|
233
|
+
def reverse!
|
234
|
+
end
|
235
|
+
end
|
236
|
+
*/
|
237
|
+
|
238
|
+
static VALUE
|
239
|
+
rb_ca_reverse_bang (VALUE self)
|
240
|
+
{
|
241
|
+
CArray *ca;
|
242
|
+
|
243
|
+
rb_ca_modify(self);
|
244
|
+
|
245
|
+
Data_Get_Struct(self, CArray, ca);
|
246
|
+
ca_attach(ca);
|
247
|
+
|
248
|
+
switch ( ca->data_type ) {
|
249
|
+
case CA_FIXLEN: proc_reverse_bang_data(); break;
|
250
|
+
case CA_BOOLEAN:
|
251
|
+
case CA_INT8:
|
252
|
+
case CA_UINT8: proc_reverse_bang(int8_t); break;
|
253
|
+
case CA_INT16:
|
254
|
+
case CA_UINT16: proc_reverse_bang(int16_t); break;
|
255
|
+
case CA_INT32:
|
256
|
+
case CA_UINT32:
|
257
|
+
case CA_FLOAT32: proc_reverse_bang(int32_t); break;
|
258
|
+
case CA_INT64:
|
259
|
+
case CA_UINT64:
|
260
|
+
case CA_FLOAT64: proc_reverse_bang(float64_t); break;
|
261
|
+
case CA_FLOAT128: proc_reverse_bang(float128_t); break;
|
262
|
+
#ifdef HAVE_COMPLEX_H
|
263
|
+
case CA_CMPLX64: proc_reverse_bang(float64_t); break;
|
264
|
+
case CA_CMPLX128: proc_reverse_bang(cmplx128_t); break;
|
265
|
+
case CA_CMPLX256: proc_reverse_bang(cmplx256_t); break;
|
266
|
+
#endif
|
267
|
+
case CA_OBJECT: proc_reverse_bang(VALUE); break;
|
268
|
+
default:
|
269
|
+
rb_raise(rb_eCADataTypeError, "[BUG] array has an unknown data type");
|
270
|
+
}
|
271
|
+
|
272
|
+
if ( ca_has_mask(ca) ) {
|
273
|
+
proc_reverse_bang_mask();
|
274
|
+
}
|
275
|
+
|
276
|
+
ca_sync(ca);
|
277
|
+
ca_detach(ca);
|
278
|
+
|
279
|
+
return self;
|
280
|
+
}
|
281
|
+
|
282
|
+
/* rdoc:
|
283
|
+
class CArray
|
284
|
+
# Returns a new CArray object containing <i>ca</i>'s elements in
|
285
|
+
# reverse order.
|
286
|
+
def reverse
|
287
|
+
end
|
288
|
+
end
|
289
|
+
*/
|
290
|
+
|
291
|
+
static VALUE
|
292
|
+
rb_ca_reversed_copy (VALUE self)
|
293
|
+
{
|
294
|
+
volatile VALUE out = rb_ca_copy(self);
|
295
|
+
rb_ca_data_type_inherit(out, self);
|
296
|
+
return rb_ca_reverse_bang(out);
|
297
|
+
}
|
298
|
+
|
299
|
+
/* ------------------------------------------------------------------------- */
|
300
|
+
|
301
|
+
typedef struct {
|
302
|
+
int32_t bytes;
|
303
|
+
char *ptr;
|
304
|
+
} cmp_data;
|
305
|
+
|
306
|
+
typedef int (*ca_qsort_cmp_func)();
|
307
|
+
|
308
|
+
#define qcmp_type(type) \
|
309
|
+
static int \
|
310
|
+
qcmp_## type (type *a, type *b) \
|
311
|
+
{ \
|
312
|
+
if ( *a > *b ) return 1; \
|
313
|
+
if ( *a < *b ) return -1; \
|
314
|
+
return 0; \
|
315
|
+
}
|
316
|
+
|
317
|
+
#define qcmp_f_type(type) \
|
318
|
+
static int \
|
319
|
+
qcmp_## type (type *a, type *b) \
|
320
|
+
{ \
|
321
|
+
if ( isnan(*a) && ( ! isnan(*b) ) ) return 1; \
|
322
|
+
if ( isnan(*b) && ( ! isnan(*a) ) ) return -1; \
|
323
|
+
if ( *a > *b ) return 1; \
|
324
|
+
if ( *a < *b ) return -1; \
|
325
|
+
return 0; \
|
326
|
+
}
|
327
|
+
|
328
|
+
static int
|
329
|
+
qcmp_VALUE (VALUE *a, VALUE *b)
|
330
|
+
{
|
331
|
+
int cmp;
|
332
|
+
cmp = NUM2INT(rb_funcall(*a, rb_intern("<=>"), 1, *b));
|
333
|
+
if ( cmp != 0 ) return cmp;
|
334
|
+
return 0;
|
335
|
+
}
|
336
|
+
|
337
|
+
static int
|
338
|
+
qcmp_data (cmp_data *a, cmp_data *b)
|
339
|
+
{
|
340
|
+
int cmp;
|
341
|
+
cmp = memcmp(a->ptr, b->ptr, a->bytes);
|
342
|
+
if ( cmp != 0 ) return cmp;
|
343
|
+
return 0;
|
344
|
+
}
|
345
|
+
|
346
|
+
qcmp_type(boolean8_t)
|
347
|
+
qcmp_type(int8_t)
|
348
|
+
qcmp_type(uint8_t)
|
349
|
+
qcmp_type(int16_t)
|
350
|
+
qcmp_type(uint16_t)
|
351
|
+
qcmp_type(int32_t)
|
352
|
+
qcmp_type(uint32_t)
|
353
|
+
qcmp_type(int64_t)
|
354
|
+
qcmp_type(uint64_t)
|
355
|
+
qcmp_f_type(float32_t)
|
356
|
+
qcmp_f_type(float64_t)
|
357
|
+
qcmp_f_type(float128_t)
|
358
|
+
|
359
|
+
static int
|
360
|
+
qcmp_not_implement (void *a, void *b)
|
361
|
+
{
|
362
|
+
rb_raise(rb_eNotImpError,
|
363
|
+
"compare function is not implemented for the data type");
|
364
|
+
}
|
365
|
+
|
366
|
+
ca_qsort_cmp_func
|
367
|
+
ca_qsort_cmp[CA_NTYPE] = {
|
368
|
+
qcmp_data,
|
369
|
+
qcmp_boolean8_t,
|
370
|
+
qcmp_int8_t,
|
371
|
+
qcmp_uint8_t,
|
372
|
+
qcmp_int16_t,
|
373
|
+
qcmp_uint16_t,
|
374
|
+
qcmp_int32_t,
|
375
|
+
qcmp_uint32_t,
|
376
|
+
qcmp_int64_t,
|
377
|
+
qcmp_uint64_t,
|
378
|
+
qcmp_float32_t,
|
379
|
+
qcmp_float64_t,
|
380
|
+
qcmp_float128_t,
|
381
|
+
qcmp_not_implement,
|
382
|
+
qcmp_not_implement,
|
383
|
+
qcmp_not_implement,
|
384
|
+
qcmp_VALUE,
|
385
|
+
};
|
386
|
+
|
387
|
+
/* rdoc:
|
388
|
+
class CArray
|
389
|
+
# Sorts <i>ca</i>'s elements in place.
|
390
|
+
def sort!
|
391
|
+
end
|
392
|
+
end
|
393
|
+
*/
|
394
|
+
|
395
|
+
static VALUE
|
396
|
+
rb_ca_sort_bang (VALUE self)
|
397
|
+
{
|
398
|
+
CArray *ca;
|
399
|
+
|
400
|
+
if ( rb_ca_is_any_masked(self) ) {
|
401
|
+
rb_ca_sort_bang(rb_ca_value_not_masked(self));
|
402
|
+
return self;
|
403
|
+
}
|
404
|
+
|
405
|
+
rb_ca_modify(self);
|
406
|
+
|
407
|
+
Data_Get_Struct(self, CArray, ca);
|
408
|
+
ca_attach(ca);
|
409
|
+
|
410
|
+
if ( ca_is_fixlen_type(ca) ) {
|
411
|
+
cmp_data *cmp_ptr, *p;
|
412
|
+
char *ca_ptr, *q;
|
413
|
+
int32_t i;
|
414
|
+
cmp_ptr = malloc_with_check(sizeof(cmp_data)*ca->elements);
|
415
|
+
ca_ptr = malloc_with_check(ca_length(ca));
|
416
|
+
for (i=0, p=cmp_ptr, q=ca->ptr; i<ca->elements; i++, p++, q+=ca->bytes) {
|
417
|
+
p->bytes = ca->bytes;
|
418
|
+
p->ptr = q;
|
419
|
+
}
|
420
|
+
qsort(cmp_ptr, ca->elements, sizeof(cmp_data), ca_qsort_cmp[CA_FIXLEN]);
|
421
|
+
for (i=0, p=cmp_ptr, q=ca_ptr; i<ca->elements; i++, p++, q+=ca->bytes) {
|
422
|
+
memcpy(q, p->ptr, ca->bytes);
|
423
|
+
}
|
424
|
+
free(ca->ptr);
|
425
|
+
ca->ptr = ca_ptr;
|
426
|
+
free(cmp_ptr);
|
427
|
+
}
|
428
|
+
else {
|
429
|
+
qsort(ca->ptr, ca->elements, ca->bytes, ca_qsort_cmp[ca->data_type]);
|
430
|
+
}
|
431
|
+
ca_sync(ca);
|
432
|
+
ca_detach(ca);
|
433
|
+
return self;
|
434
|
+
}
|
435
|
+
|
436
|
+
/* rdoc:
|
437
|
+
class CArray
|
438
|
+
# Returns a new CArray object containing <i>ca</i>'s elements sorted.
|
439
|
+
def sort
|
440
|
+
end
|
441
|
+
end
|
442
|
+
*/
|
443
|
+
|
444
|
+
static VALUE
|
445
|
+
rb_ca_sorted_copy (VALUE self)
|
446
|
+
{
|
447
|
+
volatile VALUE out = rb_ca_copy(self);
|
448
|
+
rb_ca_data_type_inherit(out, self);
|
449
|
+
return rb_ca_sort_bang(out);
|
450
|
+
}
|
451
|
+
|
452
|
+
/* --------------------------------------------------------------- */
|
453
|
+
|
454
|
+
/*
|
455
|
+
Fisher-Yates method
|
456
|
+
|
457
|
+
# [ruby-list:41356]
|
458
|
+
# From: Shin-ichiro HARA <sinara blade.nagaokaut.ac.jp>
|
459
|
+
def shuffle!
|
460
|
+
array = self
|
461
|
+
array.each_index do |idx|
|
462
|
+
jdx = rand(idx + 1)
|
463
|
+
array[idx], array[jdx] = array[jdx], array[idx]
|
464
|
+
end
|
465
|
+
self
|
466
|
+
end
|
467
|
+
*/
|
468
|
+
|
469
|
+
#define proc_shuffle(type) \
|
470
|
+
{ \
|
471
|
+
int32_t idx, jdx; \
|
472
|
+
type *ptr, *p, *q; \
|
473
|
+
type t1; \
|
474
|
+
ptr = (type *) ca->ptr; \
|
475
|
+
if ( ca->mask ) { \
|
476
|
+
boolean8_t *mptr, *mp, *mq; \
|
477
|
+
boolean8_t mt1; \
|
478
|
+
mptr = (boolean8_t *)ca->mask->ptr; \
|
479
|
+
for (idx=0; idx<ca->elements; idx++) { \
|
480
|
+
jdx = ca_rand((double)(idx+1)); \
|
481
|
+
p = ptr + idx; \
|
482
|
+
q = ptr + jdx; \
|
483
|
+
mp = mptr + idx; \
|
484
|
+
mq = mptr + jdx; \
|
485
|
+
t1 = *p; *p = *q; *q = t1; \
|
486
|
+
mt1 = *mp; *mp = *mq; *mq = mt1; \
|
487
|
+
} \
|
488
|
+
} \
|
489
|
+
else { \
|
490
|
+
for (idx=0; idx<ca->elements; idx++) { \
|
491
|
+
jdx = ca_rand((double)(idx+1)); \
|
492
|
+
p = ptr + idx; \
|
493
|
+
q = ptr + jdx; \
|
494
|
+
t1 = *p; *p = *q; *q = t1; \
|
495
|
+
} \
|
496
|
+
} \
|
497
|
+
}
|
498
|
+
|
499
|
+
#define proc_shuffle_fixlen() \
|
500
|
+
{ \
|
501
|
+
int32_t idx, jdx; \
|
502
|
+
char *ptr, *p, *q; \
|
503
|
+
char *t = (char*) malloc_with_check(ca->bytes); \
|
504
|
+
int32_t bytes = ca->bytes; \
|
505
|
+
ptr = ca->ptr; \
|
506
|
+
if ( ca->mask ) { \
|
507
|
+
boolean8_t *mptr, *mp, *mq; \
|
508
|
+
boolean8_t mt1; \
|
509
|
+
mptr = (boolean8_t *)ca->mask->ptr; \
|
510
|
+
for (idx=0; idx<ca->elements; idx++) { \
|
511
|
+
jdx = ca_rand((double)(idx+1)); \
|
512
|
+
p = ca->ptr + idx*bytes; \
|
513
|
+
q = ca->ptr + jdx*bytes; \
|
514
|
+
mp = mptr + idx; \
|
515
|
+
mq = mptr + jdx; \
|
516
|
+
memcpy(t, p, bytes); memcpy(p, q, bytes); memcpy(q, t, bytes); \
|
517
|
+
mt1 = *mp; *mp = *mq; *mq = mt1; \
|
518
|
+
} \
|
519
|
+
free(t); \
|
520
|
+
} \
|
521
|
+
else { \
|
522
|
+
for (idx=0; idx<ca->elements; idx++) { \
|
523
|
+
jdx = ca_rand((double)(idx+1)); \
|
524
|
+
p = ca->ptr + idx*bytes; \
|
525
|
+
q = ca->ptr + jdx*bytes; \
|
526
|
+
memcpy(t, p, bytes); memcpy(p, q, bytes); memcpy(q, t, bytes); \
|
527
|
+
} \
|
528
|
+
free(t); \
|
529
|
+
} \
|
530
|
+
}
|
531
|
+
|
532
|
+
void ca_check_rand_init ();
|
533
|
+
|
534
|
+
/* rdoc:
|
535
|
+
class CArray
|
536
|
+
# shuffles the elements.
|
537
|
+
# If `self` has mask, the masked elements are also moved by shuffling.
|
538
|
+
def shuffle!
|
539
|
+
end
|
540
|
+
end
|
541
|
+
*/
|
542
|
+
|
543
|
+
static VALUE
|
544
|
+
rb_ca_shuffle_bang (VALUE self)
|
545
|
+
{
|
546
|
+
CArray *ca;
|
547
|
+
|
548
|
+
rb_ca_modify(self);
|
549
|
+
|
550
|
+
Data_Get_Struct(self, CArray, ca);
|
551
|
+
|
552
|
+
ca_check_rand_init();
|
553
|
+
|
554
|
+
ca_attach(ca);
|
555
|
+
|
556
|
+
switch ( ca->data_type ) {
|
557
|
+
case CA_BOOLEAN:
|
558
|
+
case CA_INT8: proc_shuffle(int8_t); break;
|
559
|
+
case CA_UINT8: proc_shuffle(uint8_t); break;
|
560
|
+
case CA_INT16: proc_shuffle(int16_t); break;
|
561
|
+
case CA_UINT16: proc_shuffle(uint16_t); break;
|
562
|
+
case CA_INT32: proc_shuffle(int32_t); break;
|
563
|
+
case CA_UINT32: proc_shuffle(uint32_t); break;
|
564
|
+
case CA_INT64: proc_shuffle(int64_t); break;
|
565
|
+
case CA_UINT64: proc_shuffle(uint64_t); break;
|
566
|
+
case CA_FLOAT32: proc_shuffle(float32_t); break;
|
567
|
+
case CA_FLOAT64: proc_shuffle(float64_t); break;
|
568
|
+
case CA_FLOAT128: proc_shuffle(float128_t); break;
|
569
|
+
#ifdef HAVE_COMPLEX_H
|
570
|
+
case CA_CMPLX64: proc_shuffle(cmplx64_t); break;
|
571
|
+
case CA_CMPLX128: proc_shuffle(cmplx128_t); break;
|
572
|
+
case CA_CMPLX256: proc_shuffle(cmplx256_t); break;
|
573
|
+
#endif
|
574
|
+
case CA_FIXLEN: proc_shuffle_fixlen(); break;
|
575
|
+
case CA_OBJECT: proc_shuffle(VALUE); break;
|
576
|
+
default:
|
577
|
+
rb_raise(rb_eCADataTypeError, "[BUG] array has an unknown data type");
|
578
|
+
}
|
579
|
+
|
580
|
+
ca_sync(ca);
|
581
|
+
ca_detach(ca);
|
582
|
+
|
583
|
+
return self;
|
584
|
+
}
|
585
|
+
|
586
|
+
/* rdoc:
|
587
|
+
class CArray
|
588
|
+
def shuffle
|
589
|
+
end
|
590
|
+
end
|
591
|
+
*/
|
592
|
+
|
593
|
+
static VALUE
|
594
|
+
rb_ca_shuffled_copy (VALUE self)
|
595
|
+
{
|
596
|
+
volatile VALUE out = rb_ca_copy(self);
|
597
|
+
rb_ca_data_type_inherit(out, self);
|
598
|
+
return rb_ca_shuffle_bang(out);
|
599
|
+
}
|
600
|
+
|
601
|
+
/* --------------------------------------------------------------- */
|
602
|
+
|
603
|
+
/* rdoc:
|
604
|
+
class CArray
|
605
|
+
def bsearch
|
606
|
+
end
|
607
|
+
end
|
608
|
+
*/
|
609
|
+
|
610
|
+
static VALUE
|
611
|
+
rb_ca_binary_search (VALUE self, volatile VALUE rval)
|
612
|
+
{
|
613
|
+
volatile VALUE out;
|
614
|
+
CArray *ca;
|
615
|
+
char *val;
|
616
|
+
Data_Get_Struct(self, CArray, ca);
|
617
|
+
|
618
|
+
/* FIXME : treat mask */
|
619
|
+
/*
|
620
|
+
if ( ca_has_mask(ca) && ca_is_any_masked(self) ) {
|
621
|
+
VALUE val = rb_funcall(self, rb_intern("value"), 0);
|
622
|
+
VALUE select = rb_ca_is_not_masked(self);
|
623
|
+
VALUE obj = rb_funcall(val, rb_intern("[]"), 1, select);
|
624
|
+
return rb_ca_binary_search(obj, rval);
|
625
|
+
}
|
626
|
+
*/
|
627
|
+
|
628
|
+
if ( ca_is_any_masked(ca) ) {
|
629
|
+
rb_raise(rb_eRuntimeError,
|
630
|
+
"CArray#bsearch can't be applied to carray with masked element.");
|
631
|
+
}
|
632
|
+
|
633
|
+
ca_attach(ca);
|
634
|
+
|
635
|
+
if ( rb_obj_is_carray(rval) ) {
|
636
|
+
volatile VALUE vidx;
|
637
|
+
CArray *cv, *co;
|
638
|
+
char *ptr, *val;
|
639
|
+
int32_t i, idx;
|
640
|
+
Data_Get_Struct(rval, CArray, cv);
|
641
|
+
if ( ca->data_type != cv->data_type ) {
|
642
|
+
cv = ca_wrap_readonly(rval, ca->data_type);
|
643
|
+
}
|
644
|
+
co = carray_new(CA_INT32, cv->rank, cv->dim, 0, NULL);
|
645
|
+
out = ca_wrap_struct(co);
|
646
|
+
ca_attach(cv);
|
647
|
+
if ( ca_is_fixlen_type(ca) ) {
|
648
|
+
cmp_data *cmp_ptr, *p, *ptr, cmp_val;
|
649
|
+
char *q;
|
650
|
+
int32_t i;
|
651
|
+
cmp_val.bytes = ca->bytes;
|
652
|
+
cmp_ptr = malloc_with_check(sizeof(cmp_data)*ca->elements);
|
653
|
+
for (i=0, p=cmp_ptr, q=ca->ptr; i<ca->elements; i++, p++, q+=ca->bytes) {
|
654
|
+
p->bytes = ca->bytes;
|
655
|
+
p->ptr = q;
|
656
|
+
}
|
657
|
+
for (i=0; i<cv->elements; i++) {
|
658
|
+
cmp_val.ptr = ca_ptr_at_addr(cv, i);
|
659
|
+
ptr = bsearch(&cmp_val, cmp_ptr, ca->elements, sizeof(cmp_data),
|
660
|
+
ca_qsort_cmp[CA_FIXLEN]);
|
661
|
+
vidx = ( ! ptr ) ? CA_UNDEF : INT2NUM(ptr - cmp_ptr);
|
662
|
+
rb_ca_store_addr(out, i, vidx);
|
663
|
+
}
|
664
|
+
free(cmp_ptr);
|
665
|
+
}
|
666
|
+
else {
|
667
|
+
for (i=0; i<cv->elements; i++) {
|
668
|
+
val = ca_ptr_at_addr(cv, i);
|
669
|
+
ptr = bsearch(val, ca->ptr, ca->elements, ca->bytes,
|
670
|
+
ca_qsort_cmp[ca->data_type]);
|
671
|
+
if ( ! ptr ) {
|
672
|
+
rb_ca_store_addr(out, i, CA_UNDEF);
|
673
|
+
}
|
674
|
+
else {
|
675
|
+
idx = (ptr - ca->ptr)/ca->bytes;
|
676
|
+
ca_store_addr(co, i, &idx);
|
677
|
+
}
|
678
|
+
}
|
679
|
+
}
|
680
|
+
ca_detach(cv);
|
681
|
+
}
|
682
|
+
else {
|
683
|
+
val = ALLOCA_N(char, ca->bytes);
|
684
|
+
rb_ca_obj2ptr(self, rval, val);
|
685
|
+
if ( ca_is_fixlen_type(ca) ) {
|
686
|
+
cmp_data *cmp_ptr, *p, *ptr, cmp_val;
|
687
|
+
char *q;
|
688
|
+
int32_t i;
|
689
|
+
cmp_val.bytes = ca->bytes;
|
690
|
+
cmp_val.ptr = val;
|
691
|
+
cmp_ptr = malloc_with_check(sizeof(cmp_data)*ca->elements);
|
692
|
+
for (i=0, p=cmp_ptr, q=ca->ptr; i<ca->elements; i++, p++, q+=ca->bytes) {
|
693
|
+
p->bytes = ca->bytes;
|
694
|
+
p->ptr = q;
|
695
|
+
}
|
696
|
+
ptr = bsearch(&cmp_val, cmp_ptr, ca->elements, sizeof(cmp_data),
|
697
|
+
ca_qsort_cmp[CA_FIXLEN]);
|
698
|
+
out = ( ! ptr ) ? Qnil : INT2NUM((ptr - cmp_ptr));
|
699
|
+
free(cmp_ptr);
|
700
|
+
}
|
701
|
+
else {
|
702
|
+
char *ptr;
|
703
|
+
ptr = bsearch(val, ca->ptr, ca->elements, ca->bytes,
|
704
|
+
ca_qsort_cmp[ca->data_type]);
|
705
|
+
out = ( ! ptr ) ? Qnil : INT2NUM((ptr - ca->ptr)/ca->bytes);
|
706
|
+
}
|
707
|
+
}
|
708
|
+
ca_detach(ca);
|
709
|
+
return out;
|
710
|
+
}
|
711
|
+
|
712
|
+
/* rdoc:
|
713
|
+
class CArray
|
714
|
+
def bsearch_index
|
715
|
+
end
|
716
|
+
end
|
717
|
+
*/
|
718
|
+
|
719
|
+
static VALUE
|
720
|
+
rb_ca_binary_search_index (VALUE self, volatile VALUE rval)
|
721
|
+
{
|
722
|
+
VALUE raddr = rb_ca_binary_search(self, rval);
|
723
|
+
return ( NIL_P(raddr) ) ? Qnil : rb_ca_addr2index(self, raddr);
|
724
|
+
}
|
725
|
+
|
726
|
+
/* ------------------------------------------------------------------------- */
|
727
|
+
|
728
|
+
#define proc_find_value(type) \
|
729
|
+
{ \
|
730
|
+
type *ptr = (type *) ca->ptr; \
|
731
|
+
boolean8_t *m = (ca->mask) ? (boolean8_t*) ca->mask->ptr : NULL; \
|
732
|
+
type val = (type) NUM2LONG(value); \
|
733
|
+
int32_t i; \
|
734
|
+
if ( m ) { \
|
735
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
736
|
+
if ( ! *m++ ) { \
|
737
|
+
if ( *ptr == val ) { \
|
738
|
+
addr = i; \
|
739
|
+
break; \
|
740
|
+
} \
|
741
|
+
} \
|
742
|
+
} \
|
743
|
+
} \
|
744
|
+
else { \
|
745
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
746
|
+
if ( *ptr == val ) { \
|
747
|
+
addr = i; \
|
748
|
+
break; \
|
749
|
+
} \
|
750
|
+
} \
|
751
|
+
} \
|
752
|
+
}
|
753
|
+
|
754
|
+
#define proc_find_value_float(type, defeps) \
|
755
|
+
{ \
|
756
|
+
type *ptr = (type *) ca->ptr; \
|
757
|
+
boolean8_t *m = (ca->mask) ? (boolean8_t*) ca->mask->ptr : NULL; \
|
758
|
+
type val = (type) NUM2DBL(value); \
|
759
|
+
double eps = (NIL_P(veps)) ? defeps*fabs(val) : NUM2DBL(veps); \
|
760
|
+
int32_t i; \
|
761
|
+
if ( m ) { \
|
762
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
763
|
+
if ( ! *m++ ) { \
|
764
|
+
if ( fabs(*ptr - val) <= eps ) { \
|
765
|
+
addr = i; \
|
766
|
+
break; \
|
767
|
+
} \
|
768
|
+
} \
|
769
|
+
} \
|
770
|
+
} \
|
771
|
+
else { \
|
772
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
773
|
+
if ( fabs(*ptr - val) <= eps ) { \
|
774
|
+
addr = i; \
|
775
|
+
break; \
|
776
|
+
} \
|
777
|
+
} \
|
778
|
+
} \
|
779
|
+
}
|
780
|
+
|
781
|
+
#define proc_find_value_cmplx(type, defeps) \
|
782
|
+
{ \
|
783
|
+
type *ptr = (type *) ca->ptr; \
|
784
|
+
boolean8_t *m = (ca->mask) ? (boolean8_t*) ca->mask->ptr : NULL; \
|
785
|
+
type val = (type) NUM2CC(value); \
|
786
|
+
double eps = (NIL_P(veps)) ? defeps*cabs(val) : NUM2DBL(veps); \
|
787
|
+
int32_t i; \
|
788
|
+
if ( m ) { \
|
789
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
790
|
+
if ( ! *m++ ) { \
|
791
|
+
if ( cabs(*ptr - val) <= eps ) { \
|
792
|
+
addr = i; \
|
793
|
+
break; \
|
794
|
+
} \
|
795
|
+
} \
|
796
|
+
} \
|
797
|
+
} \
|
798
|
+
else { \
|
799
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
800
|
+
if ( cabs(*ptr - val) <= eps ) { \
|
801
|
+
addr = i; \
|
802
|
+
break; \
|
803
|
+
} \
|
804
|
+
} \
|
805
|
+
} \
|
806
|
+
}
|
807
|
+
|
808
|
+
#define proc_find_value_object() \
|
809
|
+
{ \
|
810
|
+
VALUE *ptr = (VALUE *) ca->ptr; \
|
811
|
+
boolean8_t *m = (ca->mask) ? (boolean8_t*) ca->mask->ptr : NULL; \
|
812
|
+
int32_t i; \
|
813
|
+
if ( m ) { \
|
814
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
815
|
+
if ( ! *m++ ) { \
|
816
|
+
if ( rb_funcall(value, id_equal, 1, *ptr) ) { \
|
817
|
+
addr = i; \
|
818
|
+
break; \
|
819
|
+
} \
|
820
|
+
} \
|
821
|
+
} \
|
822
|
+
} \
|
823
|
+
else { \
|
824
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
825
|
+
if ( rb_funcall(value, id_equal, 1, *ptr) ) { \
|
826
|
+
addr = i; \
|
827
|
+
break; \
|
828
|
+
} \
|
829
|
+
} \
|
830
|
+
} \
|
831
|
+
}
|
832
|
+
|
833
|
+
|
834
|
+
/* rdoc:
|
835
|
+
class CArray
|
836
|
+
def search
|
837
|
+
end
|
838
|
+
end
|
839
|
+
*/
|
840
|
+
|
841
|
+
static VALUE
|
842
|
+
rb_ca_linear_search (int argc, VALUE *argv, VALUE self)
|
843
|
+
{
|
844
|
+
volatile VALUE value, veps;
|
845
|
+
CArray *ca;
|
846
|
+
int32_t addr;
|
847
|
+
|
848
|
+
rb_scan_args(argc, argv, "11", &value, &veps);
|
849
|
+
|
850
|
+
Data_Get_Struct(self, CArray, ca);
|
851
|
+
|
852
|
+
ca_attach(ca);
|
853
|
+
|
854
|
+
addr = -1;
|
855
|
+
|
856
|
+
switch ( ca->data_type ) {
|
857
|
+
case CA_BOOLEAN:
|
858
|
+
case CA_INT8:
|
859
|
+
case CA_UINT8: proc_find_value(int8_t); break;
|
860
|
+
case CA_INT16:
|
861
|
+
case CA_UINT16: proc_find_value(int16_t); break;
|
862
|
+
case CA_INT32: proc_find_value(int32_t); break;
|
863
|
+
case CA_UINT32: proc_find_value(uint32_t); break;
|
864
|
+
case CA_INT64: proc_find_value(int64_t); break;
|
865
|
+
case CA_UINT64: proc_find_value(uint64_t); break;
|
866
|
+
case CA_FLOAT32: proc_find_value_float(float32_t, FLT_EPSILON); break;
|
867
|
+
case CA_FLOAT64: proc_find_value_float(float64_t, DBL_EPSILON); break;
|
868
|
+
case CA_FLOAT128: proc_find_value_float(float128_t, DBL_EPSILON); break;
|
869
|
+
#ifdef HAVE_COMPLEX_H
|
870
|
+
case CA_CMPLX64: proc_find_value_cmplx(cmplx64_t, FLT_EPSILON); break;
|
871
|
+
case CA_CMPLX128: proc_find_value_cmplx(cmplx128_t, DBL_EPSILON); break;
|
872
|
+
case CA_CMPLX256: proc_find_value_cmplx(cmplx256_t, DBL_EPSILON); break;
|
873
|
+
#endif
|
874
|
+
case CA_OBJECT: proc_find_value_object(); break;
|
875
|
+
default:
|
876
|
+
rb_raise(rb_eCADataTypeError, "invalid data type");
|
877
|
+
}
|
878
|
+
|
879
|
+
ca_detach(ca);
|
880
|
+
|
881
|
+
return ( addr == -1 ) ? Qnil : LONG2NUM(addr);
|
882
|
+
}
|
883
|
+
|
884
|
+
/* rdoc:
|
885
|
+
class CArray
|
886
|
+
def search_index
|
887
|
+
end
|
888
|
+
end
|
889
|
+
*/
|
890
|
+
|
891
|
+
static VALUE
|
892
|
+
rb_ca_linear_search_index (int argc, VALUE *argv, VALUE self)
|
893
|
+
{
|
894
|
+
VALUE raddr = rb_ca_linear_search(argc, argv, self);
|
895
|
+
return ( NIL_P(raddr) ) ? Qnil : rb_ca_addr2index(self, raddr);
|
896
|
+
}
|
897
|
+
|
898
|
+
/* ----------------------------------------------------------------- */
|
899
|
+
|
900
|
+
#define proc_nearest_addr(type, from, ABS) \
|
901
|
+
{ \
|
902
|
+
type *ptr = (type *) ca->ptr; \
|
903
|
+
boolean8_t *m = (ca->mask) ? (boolean8_t*) ca->mask->ptr : NULL; \
|
904
|
+
type val = (type) from(value); \
|
905
|
+
double trial; \
|
906
|
+
double diff = 1.0/0.0; \
|
907
|
+
int32_t i; \
|
908
|
+
addr = -1; \
|
909
|
+
if ( m ) { \
|
910
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
911
|
+
if ( ! *m++ ) { \
|
912
|
+
trial = ABS(val - *ptr); \
|
913
|
+
if ( trial < diff ) { \
|
914
|
+
addr = i; \
|
915
|
+
diff = trial; \
|
916
|
+
} \
|
917
|
+
} \
|
918
|
+
} \
|
919
|
+
} \
|
920
|
+
else { \
|
921
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
922
|
+
trial = ABS(val - *ptr); \
|
923
|
+
if ( trial < diff ) { \
|
924
|
+
addr = i; \
|
925
|
+
diff = trial; \
|
926
|
+
} \
|
927
|
+
} \
|
928
|
+
} \
|
929
|
+
}
|
930
|
+
|
931
|
+
#define proc_nearest_addr_VALUE() \
|
932
|
+
{ \
|
933
|
+
VALUE *ptr = (VALUE *) ca->ptr; \
|
934
|
+
boolean8_t *m = (ca->mask) ? (boolean8_t*) ca->mask->ptr : NULL; \
|
935
|
+
VALUE val = value; \
|
936
|
+
VALUE trial; \
|
937
|
+
VALUE diff = rb_float_new(1.0/0.0); \
|
938
|
+
int32_t i; \
|
939
|
+
addr = -1; \
|
940
|
+
if ( m ) { \
|
941
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
942
|
+
if ( ! *m++ ) { \
|
943
|
+
trial = rb_funcall(val, rb_intern("distance"), 1, *ptr); \
|
944
|
+
if ( rb_funcall(trial, rb_intern("<"), 1, diff) ) { \
|
945
|
+
addr = i; \
|
946
|
+
diff = trial; \
|
947
|
+
} \
|
948
|
+
} \
|
949
|
+
} \
|
950
|
+
} \
|
951
|
+
else { \
|
952
|
+
for (i=0; i<ca->elements; i++, ptr++) { \
|
953
|
+
trial = rb_funcall(val, rb_intern("distance"), 1, *ptr); \
|
954
|
+
if ( rb_funcall(trial, rb_intern("<"), 1, diff) ) { \
|
955
|
+
addr = i; \
|
956
|
+
diff = trial; \
|
957
|
+
} \
|
958
|
+
} \
|
959
|
+
} \
|
960
|
+
}
|
961
|
+
|
962
|
+
/* rdoc:
|
963
|
+
class CArray
|
964
|
+
def search_nearest
|
965
|
+
end
|
966
|
+
end
|
967
|
+
*/
|
968
|
+
|
969
|
+
static VALUE
|
970
|
+
rb_ca_linear_search_nearest (VALUE self, VALUE value)
|
971
|
+
{
|
972
|
+
CArray *ca;
|
973
|
+
int32_t addr;
|
974
|
+
|
975
|
+
Data_Get_Struct(self, CArray, ca);
|
976
|
+
|
977
|
+
ca_attach(ca);
|
978
|
+
|
979
|
+
switch ( ca->data_type ) {
|
980
|
+
case CA_BOOLEAN:
|
981
|
+
case CA_INT8: proc_nearest_addr(int8_t, NUM2LONG, fabs); break;
|
982
|
+
case CA_UINT8: proc_nearest_addr(uint8_t, NUM2ULONG, fabs); break;
|
983
|
+
case CA_INT16: proc_nearest_addr(int16_t, NUM2LONG, fabs); break;
|
984
|
+
case CA_UINT16: proc_nearest_addr(uint16_t, NUM2ULONG, fabs); break;
|
985
|
+
case CA_INT32: proc_nearest_addr(int32_t, NUM2LONG, fabs); break;
|
986
|
+
case CA_UINT32: proc_nearest_addr(uint32_t, NUM2ULONG, fabs); break;
|
987
|
+
case CA_INT64: proc_nearest_addr(int64_t, NUM2LL, fabs); break;
|
988
|
+
case CA_UINT64: proc_nearest_addr(uint64_t, rb_num2ull, fabs); break;
|
989
|
+
case CA_FLOAT32: proc_nearest_addr(float32_t, NUM2DBL, fabs); break;
|
990
|
+
case CA_FLOAT64: proc_nearest_addr(float64_t, NUM2DBL, fabs); break;
|
991
|
+
case CA_FLOAT128: proc_nearest_addr(float128_t, NUM2DBL, fabs); break;
|
992
|
+
#ifdef HAVE_COMPLEX_H
|
993
|
+
case CA_CMPLX64: proc_nearest_addr(cmplx64_t, NUM2CC, cabs); break;
|
994
|
+
case CA_CMPLX128: proc_nearest_addr(cmplx128_t, NUM2CC, cabs); break;
|
995
|
+
case CA_CMPLX256: proc_nearest_addr(cmplx256_t, NUM2CC, cabs); break;
|
996
|
+
#endif
|
997
|
+
case CA_OBJECT: proc_nearest_addr_VALUE(); break;
|
998
|
+
default:
|
999
|
+
rb_raise(rb_eCADataTypeError, "invalid data type for nearest_addr()");
|
1000
|
+
}
|
1001
|
+
|
1002
|
+
ca_detach(ca);
|
1003
|
+
|
1004
|
+
return ( addr == -1 ) ? Qnil : LONG2NUM(addr);
|
1005
|
+
}
|
1006
|
+
|
1007
|
+
static VALUE
|
1008
|
+
rb_ca_linear_search_nearest_index (VALUE self, VALUE value)
|
1009
|
+
{
|
1010
|
+
VALUE raddr = rb_ca_linear_search_nearest(self, value);
|
1011
|
+
return ( NIL_P(raddr) ) ? Qnil : rb_ca_addr2index(self, raddr);
|
1012
|
+
}
|
1013
|
+
|
1014
|
+
void
|
1015
|
+
Init_carray_order ()
|
1016
|
+
{
|
1017
|
+
id_equal = rb_intern("==");
|
1018
|
+
|
1019
|
+
rb_define_method(rb_cCArray, "project", rb_ca_project, -1);
|
1020
|
+
|
1021
|
+
rb_define_method(rb_cCArray, "reverse!", rb_ca_reverse_bang, 0);
|
1022
|
+
rb_define_method(rb_cCArray, "reverse", rb_ca_reversed_copy, 0);
|
1023
|
+
|
1024
|
+
rb_define_method(rb_cCArray, "sort!", rb_ca_sort_bang, 0);
|
1025
|
+
rb_define_method(rb_cCArray, "sort", rb_ca_sorted_copy, 0);
|
1026
|
+
|
1027
|
+
rb_define_method(rb_cCArray, "shuffle!", rb_ca_shuffle_bang, 0);
|
1028
|
+
rb_define_method(rb_cCArray, "shuffle", rb_ca_shuffled_copy, 0);
|
1029
|
+
|
1030
|
+
rb_define_method(rb_cCArray, "search", rb_ca_linear_search, -1);
|
1031
|
+
rb_define_method(rb_cCArray, "search_index", rb_ca_linear_search_index, -1);
|
1032
|
+
|
1033
|
+
rb_define_method(rb_cCArray, "search_nearest",
|
1034
|
+
rb_ca_linear_search_nearest, 1);
|
1035
|
+
rb_define_method(rb_cCArray, "search_nearest_index",
|
1036
|
+
rb_ca_linear_search_nearest_index, 1);
|
1037
|
+
|
1038
|
+
rb_define_method(rb_cCArray, "bsearch", rb_ca_binary_search, 1);
|
1039
|
+
rb_define_method(rb_cCArray, "bsearch_index", rb_ca_binary_search_index, 1);
|
1040
|
+
}
|