numo-narray 0.9.0.1 → 0.9.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -1
  3. data/ext/numo/narray/array.c +3 -3
  4. data/ext/numo/narray/data.c +102 -81
  5. data/ext/numo/narray/depend.erb +9 -7
  6. data/ext/numo/narray/extconf.rb +4 -24
  7. data/ext/numo/narray/gen/cogen.rb +19 -4
  8. data/ext/numo/narray/gen/def/bit.rb +31 -0
  9. data/ext/numo/narray/gen/def/robject.rb +1 -1
  10. data/ext/numo/narray/gen/dtype.erb.c +11 -257
  11. data/ext/numo/narray/gen/spec.rb +294 -0
  12. data/ext/numo/narray/gen/tmpl/aset.c +1 -1
  13. data/ext/numo/narray/gen/tmpl/each_with_index.c +1 -1
  14. data/ext/numo/narray/gen/tmpl/eye.c +5 -5
  15. data/ext/numo/narray/gen/tmpl/logseq.c +82 -0
  16. data/ext/numo/narray/gen/tmpl/map_with_index.c +1 -1
  17. data/ext/numo/narray/gen/tmpl/rand.c +126 -5
  18. data/ext/numo/narray/gen/tmpl/seq.c +43 -12
  19. data/ext/numo/narray/gen/tmpl/store_bit.c +55 -0
  20. data/ext/numo/narray/gen/tmpl_bit/allocate.c +28 -0
  21. data/ext/numo/narray/gen/tmpl_bit/aref.c +53 -0
  22. data/ext/numo/narray/gen/tmpl_bit/aset.c +63 -0
  23. data/ext/numo/narray/gen/{tmpl/bit_binary.c → tmpl_bit/binary.c} +1 -1
  24. data/ext/numo/narray/gen/{tmpl → tmpl_bit}/bit_count.c +5 -3
  25. data/ext/numo/narray/gen/tmpl_bit/bit_reduce.c +128 -0
  26. data/ext/numo/narray/gen/tmpl_bit/cast.c +37 -0
  27. data/ext/numo/narray/gen/tmpl_bit/cast_array.c +80 -0
  28. data/ext/numo/narray/gen/tmpl_bit/cast_numeric.c +22 -0
  29. data/ext/numo/narray/gen/tmpl_bit/coerce_cast.c +8 -0
  30. data/ext/numo/narray/gen/tmpl_bit/each.c +44 -0
  31. data/ext/numo/narray/gen/tmpl_bit/each_with_index.c +66 -0
  32. data/ext/numo/narray/gen/tmpl_bit/extract.c +25 -0
  33. data/ext/numo/narray/gen/tmpl_bit/fill.c +65 -0
  34. data/ext/numo/narray/gen/tmpl_bit/format.c +61 -0
  35. data/ext/numo/narray/gen/tmpl_bit/format_to_a.c +48 -0
  36. data/ext/numo/narray/gen/tmpl_bit/inspect.c +18 -0
  37. data/ext/numo/narray/gen/tmpl_bit/map_with_index.c +94 -0
  38. data/ext/numo/narray/gen/tmpl_bit/mask.c +117 -0
  39. data/ext/numo/narray/gen/tmpl_bit/none_p.c +14 -0
  40. data/ext/numo/narray/gen/tmpl_bit/store.c +32 -0
  41. data/ext/numo/narray/gen/tmpl_bit/store_array.c +5 -0
  42. data/ext/numo/narray/gen/tmpl_bit/store_bit.c +66 -0
  43. data/ext/numo/narray/gen/tmpl_bit/store_from.c +56 -0
  44. data/ext/numo/narray/gen/tmpl_bit/store_numeric.c +22 -0
  45. data/ext/numo/narray/gen/tmpl_bit/to_a.c +43 -0
  46. data/ext/numo/narray/gen/{tmpl/bit_unary.c → tmpl_bit/unary.c} +1 -1
  47. data/ext/numo/narray/gen/tmpl_bit/where.c +102 -0
  48. data/ext/numo/narray/gen/tmpl_bit/where2.c +37 -0
  49. data/ext/numo/narray/index.c +9 -7
  50. data/ext/numo/narray/math.c +4 -1
  51. data/ext/numo/narray/narray.c +302 -100
  52. data/ext/numo/narray/ndloop.c +21 -36
  53. data/ext/numo/narray/numo/intern.h +3 -3
  54. data/ext/numo/narray/numo/narray.h +5 -41
  55. data/ext/numo/narray/numo/template.h +2 -2
  56. data/ext/numo/narray/numo/types/bit.h +27 -13
  57. data/ext/numo/narray/numo/types/complex.h +2 -2
  58. data/ext/numo/narray/numo/types/complex_macro.h +19 -7
  59. data/ext/numo/narray/numo/types/dcomplex.h +9 -0
  60. data/ext/numo/narray/numo/types/dfloat.h +6 -0
  61. data/ext/numo/narray/numo/types/float_macro.h +9 -2
  62. data/ext/numo/narray/numo/types/int16.h +0 -1
  63. data/ext/numo/narray/numo/types/int32.h +0 -1
  64. data/ext/numo/narray/numo/types/int64.h +0 -1
  65. data/ext/numo/narray/numo/types/int8.h +0 -1
  66. data/ext/numo/narray/numo/types/int_macro.h +2 -1
  67. data/ext/numo/narray/numo/types/robj_macro.h +9 -2
  68. data/ext/numo/narray/numo/types/robject.h +6 -0
  69. data/ext/numo/narray/numo/types/scomplex.h +9 -0
  70. data/ext/numo/narray/numo/types/sfloat.h +6 -0
  71. data/ext/numo/narray/numo/types/uint16.h +0 -1
  72. data/ext/numo/narray/numo/types/uint32.h +0 -1
  73. data/ext/numo/narray/numo/types/uint64.h +0 -1
  74. data/ext/numo/narray/numo/types/uint8.h +0 -1
  75. data/ext/numo/narray/numo/types/uint_macro.h +2 -1
  76. data/ext/numo/narray/numo/types/xint_macro.h +6 -1
  77. data/ext/numo/narray/rand.c +2 -2
  78. data/ext/numo/narray/step.c +7 -7
  79. data/ext/numo/narray/struct.c +9 -9
  80. data/lib/erbpp.rb +6 -2
  81. data/lib/erbpp/narray_def.rb +21 -21
  82. metadata +35 -6
  83. data/ext/numo/narray/gen/bit.erb.c +0 -811
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numo-narray
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0.1
4
+ version: 0.9.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masahiro TANAKA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-21 00:00:00.000000000 Z
11
+ date: 2016-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,8 +99,8 @@ files:
99
99
  - ext/numo/narray/data.c
100
100
  - ext/numo/narray/depend.erb
101
101
  - ext/numo/narray/extconf.rb
102
- - ext/numo/narray/gen/bit.erb.c
103
102
  - ext/numo/narray/gen/cogen.rb
103
+ - ext/numo/narray/gen/def/bit.rb
104
104
  - ext/numo/narray/gen/def/dcomplex.rb
105
105
  - ext/numo/narray/gen/def/dfloat.rb
106
106
  - ext/numo/narray/gen/def/int16.rb
@@ -115,6 +115,7 @@ files:
115
115
  - ext/numo/narray/gen/def/uint64.rb
116
116
  - ext/numo/narray/gen/def/uint8.rb
117
117
  - ext/numo/narray/gen/dtype.erb.c
118
+ - ext/numo/narray/gen/spec.rb
118
119
  - ext/numo/narray/gen/tmpl/accum.c
119
120
  - ext/numo/narray/gen/tmpl/accum_binary.c
120
121
  - ext/numo/narray/gen/tmpl/accum_index.c
@@ -124,9 +125,6 @@ files:
124
125
  - ext/numo/narray/gen/tmpl/binary.c
125
126
  - ext/numo/narray/gen/tmpl/binary2.c
126
127
  - ext/numo/narray/gen/tmpl/binary_s.c
127
- - ext/numo/narray/gen/tmpl/bit_binary.c
128
- - ext/numo/narray/gen/tmpl/bit_count.c
129
- - ext/numo/narray/gen/tmpl/bit_unary.c
130
128
  - ext/numo/narray/gen/tmpl/cast.c
131
129
  - ext/numo/narray/gen/tmpl/cast_array.c
132
130
  - ext/numo/narray/gen/tmpl/cast_numeric.c
@@ -143,6 +141,7 @@ files:
143
141
  - ext/numo/narray/gen/tmpl/format_to_a.c
144
142
  - ext/numo/narray/gen/tmpl/head.c
145
143
  - ext/numo/narray/gen/tmpl/inspect.c
144
+ - ext/numo/narray/gen/tmpl/logseq.c
146
145
  - ext/numo/narray/gen/tmpl/map_with_index.c
147
146
  - ext/numo/narray/gen/tmpl/median.c
148
147
  - ext/numo/narray/gen/tmpl/minmax.c
@@ -159,12 +158,42 @@ files:
159
158
  - ext/numo/narray/gen/tmpl/sort_index.c
160
159
  - ext/numo/narray/gen/tmpl/store.c
161
160
  - ext/numo/narray/gen/tmpl/store_array.c
161
+ - ext/numo/narray/gen/tmpl/store_bit.c
162
162
  - ext/numo/narray/gen/tmpl/store_from.c
163
163
  - ext/numo/narray/gen/tmpl/store_numeric.c
164
164
  - ext/numo/narray/gen/tmpl/to_a.c
165
165
  - ext/numo/narray/gen/tmpl/unary.c
166
166
  - ext/numo/narray/gen/tmpl/unary2.c
167
167
  - ext/numo/narray/gen/tmpl/unary_s.c
168
+ - ext/numo/narray/gen/tmpl_bit/allocate.c
169
+ - ext/numo/narray/gen/tmpl_bit/aref.c
170
+ - ext/numo/narray/gen/tmpl_bit/aset.c
171
+ - ext/numo/narray/gen/tmpl_bit/binary.c
172
+ - ext/numo/narray/gen/tmpl_bit/bit_count.c
173
+ - ext/numo/narray/gen/tmpl_bit/bit_reduce.c
174
+ - ext/numo/narray/gen/tmpl_bit/cast.c
175
+ - ext/numo/narray/gen/tmpl_bit/cast_array.c
176
+ - ext/numo/narray/gen/tmpl_bit/cast_numeric.c
177
+ - ext/numo/narray/gen/tmpl_bit/coerce_cast.c
178
+ - ext/numo/narray/gen/tmpl_bit/each.c
179
+ - ext/numo/narray/gen/tmpl_bit/each_with_index.c
180
+ - ext/numo/narray/gen/tmpl_bit/extract.c
181
+ - ext/numo/narray/gen/tmpl_bit/fill.c
182
+ - ext/numo/narray/gen/tmpl_bit/format.c
183
+ - ext/numo/narray/gen/tmpl_bit/format_to_a.c
184
+ - ext/numo/narray/gen/tmpl_bit/inspect.c
185
+ - ext/numo/narray/gen/tmpl_bit/map_with_index.c
186
+ - ext/numo/narray/gen/tmpl_bit/mask.c
187
+ - ext/numo/narray/gen/tmpl_bit/none_p.c
188
+ - ext/numo/narray/gen/tmpl_bit/store.c
189
+ - ext/numo/narray/gen/tmpl_bit/store_array.c
190
+ - ext/numo/narray/gen/tmpl_bit/store_bit.c
191
+ - ext/numo/narray/gen/tmpl_bit/store_from.c
192
+ - ext/numo/narray/gen/tmpl_bit/store_numeric.c
193
+ - ext/numo/narray/gen/tmpl_bit/to_a.c
194
+ - ext/numo/narray/gen/tmpl_bit/unary.c
195
+ - ext/numo/narray/gen/tmpl_bit/where.c
196
+ - ext/numo/narray/gen/tmpl_bit/where2.c
168
197
  - ext/numo/narray/index.c
169
198
  - ext/numo/narray/kwarg.c
170
199
  - ext/numo/narray/math.c
@@ -1,811 +0,0 @@
1
- /*
2
- bit.c
3
- Numerical Array Extension for Ruby
4
- (C) Copyright 1999-2011,2013 by Masahiro TANAKA
5
-
6
- This program is free software.
7
- You can distribute/modify this program
8
- under the same terms as Ruby itself.
9
- NO WARRANTY.
10
- */
11
- #include <ruby.h>
12
- #include "numo/narray.h"
13
- #include "numo/template.h"
14
-
15
- <%
16
- $embed = true
17
- class_name "Bit"
18
- %>
19
-
20
- #define cT <%=type_var%>
21
-
22
- typedef int dtype;
23
-
24
- #define m_load_data(ptr,pos) load_data(ptr,pos)
25
- #define m_sprintf(s,x) sprintf(s,"%1d",(int)(x))
26
-
27
- #define m_copy(x) (x)
28
- #define m_not(x) (~(x))
29
- #define m_and(x,y) ((x)&(y))
30
- #define m_or(x,y) ((x)|(y))
31
- #define m_xor(x,y) ((x)^(y))
32
- #define m_eq(x,y) (~((x)^(y)))
33
- #define m_count_true(x) (x!=0)
34
- #define m_count_false(x) (x==0)
35
-
36
- static inline dtype load_data(void *ptr, size_t pos) {
37
- return (((BIT_DIGIT*)(ptr))[(pos)/NB]>>((pos)%NB)) & 1u;
38
- }
39
-
40
- VALUE <%=type_var%>;
41
-
42
- static VALUE
43
- numo_bit_allocate(VALUE self)
44
- {
45
- narray_t *na;
46
- char *ptr;
47
-
48
- GetNArray(self,na);
49
-
50
- switch(NA_TYPE(na)) {
51
- case NARRAY_DATA_T:
52
- ptr = NA_DATA_PTR(na);
53
- if (na->size > 0 && ptr == NULL) {
54
- ptr = xmalloc(((na->size-1)/sizeof(BIT_DIGIT)+1)*sizeof(BIT_DIGIT)/8);
55
- NA_DATA_PTR(na) = ptr;
56
- }
57
- break;
58
- case NARRAY_FILEMAP_T:
59
- //ptr = ((narray_filemap_t*)na)->ptr;
60
- // to be implemented
61
- break;
62
- case NARRAY_VIEW_T:
63
- rb_funcall(NA_VIEW_DATA(na), rb_intern("allocate"), 0);
64
- break;
65
- default:
66
- rb_raise(rb_eRuntimeError,"invalid narray type");
67
- }
68
- return self;
69
- }
70
-
71
-
72
- static VALUE numo_cast_array_to_bit(VALUE ary);
73
-
74
- static VALUE
75
- numo_bit_cast_numeric(VALUE val)
76
- {
77
- VALUE v;
78
- BIT_DIGIT *ptr, b=2;
79
- size_t dig_ofs;
80
- int bit_ofs;
81
-
82
- if (FIXNUM_P(val)) {
83
- b = FIX2INT(val);
84
- } else if (val==Qtrue) {
85
- b = 1;
86
- } else if (val==Qfalse) {
87
- b = 0;
88
- }
89
- if (b!=0 && b!=1) {
90
- rb_raise(rb_eArgError, "bit can be cast from 0 or 1 or true or false");
91
- }
92
-
93
- v = rb_narray_new(cT, 0, NULL);
94
- //dig_ofs = na->offset / NB;
95
- //bit_ofs = na->offset % NB;
96
- dig_ofs = 0;
97
- bit_ofs = 0;
98
- ptr = (BIT_DIGIT*)na_get_pointer_for_write(v) + dig_ofs;
99
- *ptr = (*ptr & ~(1u<<bit_ofs)) | (b<<bit_ofs);
100
- na_release_lock(v);
101
- return v;
102
- }
103
-
104
-
105
- static VALUE
106
- numo_bit_s_cast(VALUE type, VALUE obj)
107
- {
108
- VALUE r;
109
-
110
- if (CLASS_OF(obj)==cT) {
111
- return obj;
112
- } else if (TYPE(obj)==T_ARRAY) {
113
- return numo_cast_array_to_bit(obj);
114
- } else if (TYPE(obj)==T_FLOAT || FIXNUM_P(obj) || TYPE(obj)==T_BIGNUM) {
115
- return numo_bit_cast_numeric(obj);
116
- }
117
-
118
- if (IsNArray(obj)) {
119
- r = rb_funcall(obj, rb_intern("coerce_cast"), 1, cT);
120
- if (RTEST(r)) {
121
- return r;
122
- }
123
- }
124
-
125
- rb_raise(nary_eCastError, "unknown conversion from %s to %s",
126
- rb_class2name(CLASS_OF(obj)),
127
- rb_class2name(type));
128
- return Qnil;
129
- }
130
-
131
- static VALUE
132
- numo_bit_coerce_cast(VALUE value, VALUE type)
133
- {
134
- return Qnil;
135
- }
136
-
137
- //----------------------------------------------------------------------
138
-
139
- static VALUE
140
- format_bit(VALUE fmt, int x)
141
- {
142
- if (NIL_P(fmt)) {
143
- char s[4];
144
- int n;
145
- n = sprintf(s,"%1d",x);
146
- return rb_str_new(s,n);
147
- }
148
- return rb_funcall(fmt, '%', 1, INT2FIX(x));
149
- }
150
-
151
- static VALUE
152
- bit_inspect_element(char *ptr, size_t pos, VALUE fmt)
153
- {
154
- int x;
155
- LOAD_BIT(ptr,pos,x);
156
- return format_bit(fmt, x);
157
- }
158
- VALUE
159
- numo_bit_inspect(VALUE ary)
160
- {
161
- return na_ndloop_inspect(ary, bit_inspect_element, Qnil);
162
- }
163
-
164
-
165
- static void
166
- iter_bit_format(na_loop_t *const lp)
167
- {
168
- size_t i;
169
- BIT_DIGIT *a1, x=0;
170
- size_t p1;
171
- char *p2;
172
- ssize_t s1, s2;
173
- size_t *idx1;
174
- VALUE y;
175
- //VALUE fmt = *(VALUE*)(lp->opt_ptr);
176
- VALUE fmt = lp->option;
177
-
178
- INIT_COUNTER(lp, i);
179
- INIT_PTR_BIT_IDX(lp, 0, a1, p1, s1, idx1);
180
- INIT_PTR(lp, 1, p2, s2);
181
-
182
- if (idx1) {
183
- for (; i--;) {
184
- LOAD_BIT(a1, p1 + *idx1, x); idx1++;
185
- y = format_<%=tp%>(fmt, x);
186
- SET_DATA_STRIDE(p2, s2, VALUE, y);
187
- }
188
- } else {
189
- for (; i--;) {
190
- LOAD_BIT(a1, p1, x); p1+=s1;
191
- y = format_<%=tp%>(fmt, x);
192
- SET_DATA_STRIDE(p2, s2, VALUE, y);
193
- }
194
- }
195
- }
196
-
197
- static VALUE
198
- numo_bit_format(int argc, VALUE *argv, VALUE self)
199
- {
200
- VALUE fmt=Qnil;
201
- ndfunc_arg_in_t ain[2] = {{Qnil,0},{sym_option}};
202
- ndfunc_arg_out_t aout[1] = {{numo_cRObject,0}};
203
- ndfunc_t ndf = { iter_bit_format, FULL_LOOP, 2, 1, ain, aout };
204
-
205
- rb_scan_args(argc, argv, "01", &fmt);
206
- return na_ndloop(&ndf, 2, self, fmt);
207
- }
208
-
209
-
210
- static void
211
- iter_bit_format_to_a(na_loop_t *const lp)
212
- {
213
- size_t i;
214
- BIT_DIGIT *a1, x=0;
215
- size_t p1;
216
- ssize_t s1;
217
- size_t *idx1;
218
- VALUE y;
219
- //VALUE fmt = *(VALUE*)(lp->opt_ptr);
220
- VALUE fmt = lp->option;
221
- volatile VALUE a;
222
-
223
- INIT_COUNTER(lp, i);
224
- INIT_PTR_BIT_IDX(lp, 0, a1, p1, s1, idx1);
225
- a = rb_ary_new2(i);
226
- rb_ary_push(lp->args[1].value, a);
227
- if (idx1) {
228
- for (; i--;) {
229
- LOAD_BIT(a1, p1+*idx1, x); idx1++;
230
- y = format_bit(fmt, x);
231
- rb_ary_push(a,y);
232
- }
233
- } else {
234
- for (; i--;) {
235
- LOAD_BIT(a1, p1, x); p1+=s1;
236
- y = format_bit(fmt, x);
237
- rb_ary_push(a,y);
238
- }
239
- }
240
- }
241
-
242
- static VALUE
243
- numo_bit_format_to_a(int argc, VALUE *argv, VALUE self)
244
- {
245
- volatile VALUE fmt=Qnil;
246
- ndfunc_arg_in_t ain[3] = {{Qnil,0},{sym_loop_opt},{sym_option}};
247
- ndfunc_arg_out_t aout[1] = {{rb_cArray,0}}; // dummy?
248
- ndfunc_t ndf = { iter_bit_format_to_a, FULL_LOOP, 3, 1, ain, aout };
249
-
250
- rb_scan_args(argc, argv, "01", &fmt);
251
- return na_ndloop_cast_narray_to_rarray(&ndf, self, fmt);
252
- }
253
-
254
-
255
- static void
256
- iter_bit_fill(na_loop_t *const lp)
257
- {
258
- size_t n;
259
- size_t p3;
260
- ssize_t s3;
261
- size_t *idx3;
262
- int len;
263
- BIT_DIGIT *a3;
264
- BIT_DIGIT y;
265
- //VALUE x = *(VALUE*)(lp->opt_ptr);
266
- VALUE x = lp->option;
267
-
268
- if (x==INT2FIX(0) || x==Qfalse) {
269
- y = 0;
270
- } else
271
- if (x==INT2FIX(1) || x==Qtrue) {
272
- y = ~(BIT_DIGIT)0;
273
- } else {
274
- rb_raise(rb_eArgError, "invalid value for Bit");
275
- }
276
-
277
- INIT_COUNTER(lp, n);
278
- INIT_PTR_BIT_IDX(lp, 0, a3, p3, s3, idx3);
279
- if (idx3) {
280
- y = y&1;
281
- for (; n--;) {
282
- STORE_BIT(a3, p3+*idx3, y); idx3++;
283
- }
284
- } else if (s3!=1) {
285
- y = y&1;
286
- for (; n--;) {
287
- STORE_BIT(a3, p3, y); p3+=s3;
288
- }
289
- } else {
290
- if (p3>0 || n<NB) {
291
- len = NB - p3;
292
- if ((int)n<len) len=n;
293
- *a3 = (y & (SLB(len)<<p3)) | (*a3 & ~(SLB(len)<<p3));
294
- a3++;
295
- n -= len;
296
- }
297
- for (; n>=NB; n-=NB) {
298
- *(a3++) = y;
299
- }
300
- if (n>0) {
301
- *a3 = (y & SLB(n)) | (*a3 & BALL<<n);
302
- }
303
- }
304
- }
305
-
306
-
307
- static VALUE
308
- numo_bit_fill(VALUE self, VALUE val)
309
- {
310
- ndfunc_arg_in_t ain[2] = {{OVERWRITE,0},{sym_option}};
311
- ndfunc_t ndf = { iter_bit_fill, FULL_LOOP, 2, 0, ain, 0 };
312
-
313
- na_ndloop(&ndf, 2, self, val);
314
- return self;
315
- }
316
-
317
-
318
- void
319
- bit_cast_to_robj(na_loop_t *const lp)
320
- {
321
- size_t i;
322
- ssize_t s1;
323
- size_t *idx1;
324
- BIT_DIGIT *a1;
325
- size_t p1;
326
- BIT_DIGIT x=0;
327
- VALUE y;
328
- volatile VALUE a;
329
-
330
- INIT_COUNTER(lp, i);
331
- INIT_PTR_BIT_IDX(lp, 0, a1, p1, s1, idx1);
332
- a = rb_ary_new2(i);
333
- rb_ary_push(lp->args[1].value, a);
334
- if (idx1) {
335
- for (; i--;) {
336
- LOAD_BIT(a1, p1+*idx1, x); idx1++;
337
- y = INT2FIX(x);
338
- rb_ary_push(a,y);
339
- }
340
- } else {
341
- for (; i--;) {
342
- LOAD_BIT(a1, p1, x); p1+=s1;
343
- y = INT2FIX(x);
344
- rb_ary_push(a,y);
345
- }
346
- }
347
- }
348
-
349
- static VALUE
350
- numo_bit_cast_to_rarray(VALUE self)
351
- {
352
- ndfunc_arg_in_t ain[3] = {{Qnil,0},{sym_loop_opt},{sym_option}};
353
- ndfunc_arg_out_t aout[1] = {{rb_cArray,0}}; // dummy?
354
- ndfunc_t ndf = { bit_cast_to_robj, FULL_LOOP, 3, 1, ain, aout };
355
- return na_ndloop_cast_narray_to_rarray(&ndf, self, Qnil);
356
- }
357
-
358
-
359
- static void
360
- iter_cast_rarray_to_bit(na_loop_t *const lp)
361
- {
362
- size_t i, n, n1;
363
- VALUE v1, *ptr;
364
- BIT_DIGIT *a2;
365
- size_t p2;
366
- size_t s2, *idx2;
367
- VALUE x;
368
- BIT_DIGIT y;
369
-
370
- INIT_COUNTER(lp, n);
371
- INIT_PTR_BIT_IDX(lp, 1, a2, p2, s2, idx2);
372
- v1 = lp->args[0].value;
373
- ptr = &v1;
374
-
375
- switch(TYPE(v1)) {
376
- case T_ARRAY:
377
- n1 = RARRAY_LEN(v1);
378
- ptr = RARRAY_PTR(v1);
379
- break;
380
- case T_NIL:
381
- n1 = 0;
382
- break;
383
- default:
384
- n1 = 1;
385
- }
386
- if (idx2) {
387
- <%
388
- ["STORE_BIT(a2,p2+*idx2,y); idx2++;",
389
- "STORE_BIT(a2,p2,y); p2+=s2;"].each_with_index do |s,i|
390
- %>
391
- for (i=0; i<n1 && i<n; i++) {
392
- x = ptr[i];
393
- y = 2;
394
- if (FIXNUM_P(x)) {
395
- y = FIX2INT(x);
396
- } else if (x==Qtrue) {
397
- y = 1;
398
- } else if (x==Qfalse) {
399
- y = 0;
400
- } else if (x==Qnil) {
401
- y = 0;
402
- }
403
- if (y!=0 && y!=1) {
404
- rb_raise(rb_eArgError, "bit can be cast from 0 or 1 or true or false");
405
- }
406
- <%= s %>
407
- }
408
- y = 0;
409
- for (; i<n; i++) {
410
- <%= s %>
411
- }
412
- <% if i<1 %>
413
- } else {
414
- <% end; end %>
415
- }
416
- }
417
-
418
- static VALUE
419
- numo_cast_array_to_bit(VALUE rary)
420
- {
421
- volatile VALUE vnc, nary;
422
- na_compose_t *nc;
423
- ndfunc_arg_in_t ain[2] = {{Qnil,0},{rb_cArray,0}};
424
- ndfunc_t ndf = { iter_cast_rarray_to_bit, FULL_LOOP, 2, 0, ain, 0 };
425
-
426
- vnc = na_ary_composition(rary);
427
- Data_Get_Struct(vnc, na_compose_t, nc);
428
- nary = rb_narray_new(cT, nc->ndim, nc->shape);
429
- numo_bit_allocate(nary);
430
- na_ndloop_cast_rarray_to_narray(&ndf, rary, nary);
431
- return nary;
432
- }
433
-
434
-
435
- static VALUE
436
- numo_bit_extract(VALUE self)
437
- {
438
- BIT_DIGIT *ptr, val;
439
- size_t pos;
440
- narray_t *na;
441
- GetNArray(self,na);
442
-
443
- if (na->ndim==0) {
444
- pos = na_get_offset(self);
445
- ptr = (BIT_DIGIT*)na_get_pointer_for_read(self);
446
- val = ((*((ptr)+(pos)/NB)) >> ((pos)%NB)) & 1u;
447
- na_release_lock(self);
448
- return INT2FIX(val);
449
- }
450
- return self;
451
- }
452
-
453
- <%
454
- bit_binary "and", "&"
455
- bit_binary "or" , "|"
456
- bit_binary "xor", "^"
457
- bit_binary "eq"
458
- bit_unary "not", "~"
459
- bit_unary "copy"
460
- bit_count "count_true"
461
- bit_count "count_false"
462
- Function.codes.each do |x|
463
- %>
464
- <%= x %>
465
- <% end %>
466
-
467
-
468
- /* !!! Bit#store: under construction !!! */
469
- VALUE
470
- numo_bit_store(VALUE dst, VALUE src)
471
- {
472
- // check and fix me
473
- ndfunc_arg_in_t ain[2] = {{INT2FIX(1),0},{Qnil,0}};
474
- ndfunc_t ndf = { iter_bit_copy, FULL_LOOP, 2, 0, ain, 0 };
475
-
476
- na_ndloop(&ndf, 2, src, dst);
477
- return src;
478
- }
479
-
480
- //VALUE na_aref(int argc, VALUE *argv, VALUE self);
481
-
482
- /* method: []=(idx1,idx2,...,idxN,val) */
483
- static VALUE
484
- numo_bit_aset(int argc, VALUE *argv, VALUE self)
485
- {
486
- VALUE a;
487
- argc--;
488
-
489
- if (argc==0)
490
- numo_bit_store(self, argv[argc]);
491
- else {
492
- //a = na_aref(argc, argv, self);
493
- a = rb_funcall2(self, rb_intern("[]"), argc, argv);
494
- numo_bit_store(a, argv[argc]);
495
- }
496
- return argv[argc];
497
- }
498
-
499
-
500
- typedef struct {
501
- size_t count;
502
- char *idx0;
503
- char *idx1;
504
- size_t elmsz;
505
- } where_opt_t;
506
-
507
- #define STORE_INT(ptr, esz, x) memcpy(ptr,&(x),esz)
508
-
509
- static void
510
- iter_bit_where(na_loop_t *const lp)
511
- {
512
- size_t i;
513
- BIT_DIGIT *a;
514
- size_t p;
515
- ssize_t s;
516
- size_t *idx;
517
- BIT_DIGIT x=0;
518
- char *idx0, *idx1;
519
- size_t count;
520
- size_t e;
521
- where_opt_t *g;
522
-
523
- g = (where_opt_t*)(lp->opt_ptr);
524
- count = g->count;
525
- idx0 = g->idx0;
526
- idx1 = g->idx1;
527
- e = g->elmsz;
528
- INIT_COUNTER(lp, i);
529
- INIT_PTR_BIT_IDX(lp, 0, a, p, s, idx);
530
- if (idx) {
531
- for (; i--;) {
532
- LOAD_BIT(a, p+*idx, x);
533
- idx++;
534
- if (x==0) {
535
- if (idx0) {
536
- STORE_INT(idx0,e,count);
537
- idx0 += e;
538
- }
539
- } else {
540
- if (idx1) {
541
- STORE_INT(idx1,e,count);
542
- idx1 += e;
543
- }
544
- }
545
- count++;
546
- }
547
- } else {
548
- for (; i--;) {
549
- LOAD_BIT(a, p, x);
550
- p+=s;
551
- if (x==0) {
552
- if (idx0) {
553
- STORE_INT(idx0,e,count);
554
- idx0 += e;
555
- }
556
- } else {
557
- if (idx1) {
558
- STORE_INT(idx1,e,count);
559
- idx1 += e;
560
- }
561
- }
562
- count++;
563
- }
564
- }
565
- g->count = count;
566
- g->idx0 = idx0;
567
- g->idx1 = idx1;
568
- }
569
-
570
- static VALUE
571
- numo_bit_where(VALUE self)
572
- {
573
- volatile VALUE idx_1;
574
- size_t size, n_1;
575
- where_opt_t *g;
576
-
577
- ndfunc_arg_in_t ain[1] = {{cT,0}};
578
- ndfunc_t ndf = { iter_bit_where, FULL_LOOP, 1, 0, ain, 0 };
579
-
580
- size = RNARRAY_SIZE(self);
581
- n_1 = NUM2SIZE(numo_bit_count_true(0, NULL, self));
582
- g = ALLOCA_N(where_opt_t,1);
583
- g->count = 0;
584
- if (size>4294967295ul) {
585
- idx_1 = rb_narray_new(numo_cInt64, 1, &n_1);
586
- g->elmsz = 8;
587
- } else {
588
- idx_1 = rb_narray_new(numo_cInt32, 1, &n_1);
589
- g->elmsz = 4;
590
- }
591
- g->idx1 = na_get_pointer_for_write(idx_1);
592
- g->idx0 = NULL;
593
- na_ndloop3(&ndf, g, 1, self);
594
- na_release_lock(idx_1);
595
- return idx_1;
596
- }
597
-
598
- static VALUE
599
- numo_bit_where2(VALUE self)
600
- {
601
- VALUE idx_1, idx_0;
602
- size_t size, n_1;
603
- where_opt_t *g;
604
-
605
- ndfunc_arg_in_t ain[1] = {{cT,0}};
606
- ndfunc_t ndf = { iter_bit_where, FULL_LOOP, 1, 0, ain, 0 };
607
-
608
- size = RNARRAY_SIZE(self);
609
- n_1 = NUM2SIZE(numo_bit_count_true(0, NULL, self));
610
- g = ALLOCA_N(where_opt_t,1);
611
- g->count = 0;
612
- if (size>4294967295ul) {
613
- idx_1 = rb_narray_new(numo_cInt64, 1, &n_1);
614
- idx_0 = rb_narray_new(numo_cInt64, 1, &n_1);
615
- g->elmsz = 8;
616
- } else {
617
- idx_1 = rb_narray_new(numo_cInt32, 1, &n_1);
618
- idx_0 = rb_narray_new(numo_cInt32, 1, &n_1);
619
- g->elmsz = 4;
620
- }
621
- g->idx1 = na_get_pointer_for_write(idx_1);
622
- g->idx0 = na_get_pointer_for_write(idx_0);
623
- na_ndloop3(&ndf, g, 1, self);
624
- na_release_lock(idx_0);
625
- na_release_lock(idx_1);
626
- return rb_assoc_new(idx_1,idx_0);
627
- }
628
-
629
-
630
- static void
631
- iter_bit_pointer(na_loop_t *const lp)
632
- {
633
- size_t i;
634
- BIT_DIGIT *a;
635
- size_t p1, p2;
636
- ssize_t s1, s2;
637
- size_t *idx1, *idx2, *pidx;
638
- BIT_DIGIT x=0;
639
- size_t count;
640
- where_opt_t *g;
641
-
642
- g = (where_opt_t*)(lp->opt_ptr);
643
- count = g->count;
644
- pidx = (size_t*)(g->idx1);
645
- INIT_COUNTER(lp, i);
646
- INIT_PTR_BIT_IDX(lp, 0, a, p1, s1, idx1);
647
- //INIT_PTR_IDX(lp, 1, p2, s2, idx2);
648
- p2 = lp->args[1].iter[0].pos;
649
- s2 = lp->args[1].iter[0].step;
650
- idx2 = lp->args[1].iter[0].idx;
651
-
652
- if (idx1) {
653
- if (idx2) {
654
- for (; i--;) {
655
- LOAD_BIT(a, p1+*idx1, x);
656
- idx1++;
657
- if (x) {
658
- *(pidx++) = p2+*idx2;
659
- count++;
660
- }
661
- idx2++;
662
- }
663
- } else {
664
- for (; i--;) {
665
- LOAD_BIT(a, p1+*idx1, x);
666
- idx1++;
667
- if (x) {
668
- *(pidx++) = p2;
669
- count++;
670
- }
671
- p2 += s2;
672
- }
673
- }
674
- } else {
675
- if (idx2) {
676
- for (; i--;) {
677
- LOAD_BIT(a, p1, x);
678
- p1 += s1;
679
- if (x) {
680
- *(pidx++) = p2+*idx2;
681
- count++;
682
- }
683
- idx2++;
684
- }
685
- } else {
686
- for (; i--;) {
687
- LOAD_BIT(a, p1, x);
688
- p1 += s1;
689
- if (x) {
690
- *(pidx++) = p2;
691
- count++;
692
- }
693
- p2 += s2;
694
- }
695
- }
696
- }
697
- g->count = count;
698
- g->idx1 = (char*)pidx;
699
- }
700
-
701
- #if SIZEOF_VOIDP == 8
702
- #define cIndex numo_cInt64
703
- #elif SIZEOF_VOIDP == 4
704
- #define cIndex numo_cInt32
705
- #endif
706
-
707
- static VALUE
708
- numo_bit_mask(VALUE mask, VALUE val)
709
- {
710
- volatile VALUE idx_1, view;
711
- narray_data_t *nidx;
712
- narray_view_t *nv;
713
- stridx_t stridx0;
714
- size_t n_1;
715
- where_opt_t g;
716
- ndfunc_arg_in_t ain[2] = {{cT,0},{Qnil,0}};
717
- ndfunc_t ndf = {iter_bit_pointer, FULL_LOOP, 2, 0, ain, 0};
718
-
719
- n_1 = NUM2SIZE(numo_bit_count_true(0, NULL, mask));
720
- idx_1 = rb_narray_new(cIndex, 1, &n_1);
721
- g.count = 0;
722
- g.elmsz = SIZEOF_VOIDP;
723
- g.idx1 = na_get_pointer_for_write(idx_1);
724
- g.idx0 = NULL;
725
- na_ndloop3(&ndf, &g, 2, mask, val);
726
-
727
- view = na_s_allocate_view(CLASS_OF(val));
728
- GetNArrayView(view, nv);
729
- na_setup_shape((narray_t*)nv, 1, &n_1);
730
-
731
- GetNArrayData(idx_1,nidx);
732
- SDX_SET_INDEX(stridx0,(size_t*)nidx->ptr);
733
- nidx->ptr = NULL;
734
-
735
- nv->stridx = ALLOC_N(stridx_t,1);
736
- nv->stridx[0] = stridx0;
737
- nv->offset = 0;
738
- nv->data = val;
739
- return view;
740
- }
741
-
742
-
743
- VALUE
744
- numo_bit_all_p(VALUE self)
745
- {
746
- return (rb_funcall(self, rb_intern("count_false"), 0)==INT2FIX(0)) ? Qtrue : Qfalse;
747
- }
748
-
749
- VALUE
750
- numo_bit_any_p(VALUE self)
751
- {
752
- return (rb_funcall(self, rb_intern("count_true"), 0)!=INT2FIX(0)) ? Qtrue : Qfalse;
753
- }
754
-
755
- VALUE
756
- numo_bit_none_p(VALUE self)
757
- {
758
- return (rb_funcall(self, rb_intern("count_true"), 0)==INT2FIX(0)) ? Qtrue : Qfalse;
759
- }
760
-
761
-
762
- void
763
- Init_nary_bit()
764
- {
765
- volatile VALUE hCast;
766
-
767
- cT = rb_define_class_under(mNumo, "Bit", cNArray);
768
-
769
- rb_define_const(cT, "ELEMENT_BIT_SIZE", INT2FIX(1));
770
- rb_define_const(cT, "ELEMENT_BYTE_SIZE", rb_float_new(1.0/8));
771
- rb_define_const(cT, "CONTIGUOUS_STRIDE", INT2FIX(1));
772
-
773
- rb_define_method(cT, "allocate", numo_bit_allocate, 0);
774
-
775
- rb_define_singleton_method(cT, "cast", numo_bit_s_cast, 1);
776
- rb_define_singleton_method(cT, "[]", numo_bit_s_cast, -2);
777
- rb_define_method(cT, "coerce_cast", numo_bit_coerce_cast, 1);
778
-
779
- <% Function.definitions.each do |x| %>
780
- <%= x %><% end %>
781
-
782
- rb_define_alias (cT, "count_1","count_true");
783
- rb_define_alias (cT, "count_0","count_false");
784
- rb_define_method(cT, "where", numo_bit_where, 0);
785
- rb_define_method(cT, "where2", numo_bit_where2, 0);
786
- rb_define_method(cT, "mask", numo_bit_mask, 1);
787
-
788
- rb_define_method(cT, "all?", numo_bit_all_p, 0);
789
- rb_define_method(cT, "any?", numo_bit_any_p, 0);
790
- rb_define_method(cT, "none?", numo_bit_none_p, 0);
791
-
792
- rb_define_method(cT, "inspect", numo_bit_inspect, 0);
793
- rb_define_method(cT, "format", numo_bit_format, -1);
794
- rb_define_method(cT, "format_to_a", numo_bit_format_to_a, -1);
795
-
796
- rb_define_method(cT, "fill", numo_bit_fill, 1);
797
-
798
- rb_define_method(cT, "to_a", numo_bit_cast_to_rarray, 0);
799
-
800
- rb_define_method(cT, "extract", numo_bit_extract, 0);
801
-
802
- rb_define_method(cT, "copy", numo_bit_copy, 0);
803
- rb_define_method(cT, "store", numo_bit_store, 1);
804
- rb_define_method(cT, "[]=", numo_bit_aset, -1);
805
-
806
- hCast = rb_hash_new();
807
- rb_define_const(cT, "UPCAST", hCast);
808
- rb_hash_aset(hCast, numo_cInt32, numo_cInt32);
809
- rb_hash_aset(hCast, numo_cInt16, numo_cInt16);
810
- rb_hash_aset(hCast, numo_cInt8, numo_cInt8);
811
- }