numo-narray 0.9.0.1-x86-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/README.md +47 -0
  4. data/Rakefile +41 -0
  5. data/ext/numo/narray/SFMT-params.h +97 -0
  6. data/ext/numo/narray/SFMT-params19937.h +46 -0
  7. data/ext/numo/narray/SFMT.c +620 -0
  8. data/ext/numo/narray/SFMT.h +157 -0
  9. data/ext/numo/narray/array.c +525 -0
  10. data/ext/numo/narray/data.c +901 -0
  11. data/ext/numo/narray/depend.erb +33 -0
  12. data/ext/numo/narray/extconf.rb +117 -0
  13. data/ext/numo/narray/gen/bit.erb.c +811 -0
  14. data/ext/numo/narray/gen/cogen.rb +18 -0
  15. data/ext/numo/narray/gen/def/dcomplex.rb +32 -0
  16. data/ext/numo/narray/gen/def/dfloat.rb +30 -0
  17. data/ext/numo/narray/gen/def/int16.rb +29 -0
  18. data/ext/numo/narray/gen/def/int32.rb +29 -0
  19. data/ext/numo/narray/gen/def/int64.rb +29 -0
  20. data/ext/numo/narray/gen/def/int8.rb +29 -0
  21. data/ext/numo/narray/gen/def/robject.rb +30 -0
  22. data/ext/numo/narray/gen/def/scomplex.rb +32 -0
  23. data/ext/numo/narray/gen/def/sfloat.rb +30 -0
  24. data/ext/numo/narray/gen/def/uint16.rb +29 -0
  25. data/ext/numo/narray/gen/def/uint32.rb +29 -0
  26. data/ext/numo/narray/gen/def/uint64.rb +29 -0
  27. data/ext/numo/narray/gen/def/uint8.rb +29 -0
  28. data/ext/numo/narray/gen/dtype.erb.c +328 -0
  29. data/ext/numo/narray/gen/tmpl/accum.c +36 -0
  30. data/ext/numo/narray/gen/tmpl/accum_binary.c +75 -0
  31. data/ext/numo/narray/gen/tmpl/accum_index.c +58 -0
  32. data/ext/numo/narray/gen/tmpl/allocate.c +35 -0
  33. data/ext/numo/narray/gen/tmpl/aref.c +51 -0
  34. data/ext/numo/narray/gen/tmpl/aset.c +61 -0
  35. data/ext/numo/narray/gen/tmpl/binary.c +53 -0
  36. data/ext/numo/narray/gen/tmpl/binary2.c +55 -0
  37. data/ext/numo/narray/gen/tmpl/binary_s.c +34 -0
  38. data/ext/numo/narray/gen/tmpl/bit_binary.c +94 -0
  39. data/ext/numo/narray/gen/tmpl/bit_count.c +82 -0
  40. data/ext/numo/narray/gen/tmpl/bit_unary.c +77 -0
  41. data/ext/numo/narray/gen/tmpl/cast.c +37 -0
  42. data/ext/numo/narray/gen/tmpl/cast_array.c +79 -0
  43. data/ext/numo/narray/gen/tmpl/cast_numeric.c +22 -0
  44. data/ext/numo/narray/gen/tmpl/coerce_cast.c +8 -0
  45. data/ext/numo/narray/gen/tmpl/cond_binary.c +51 -0
  46. data/ext/numo/narray/gen/tmpl/cond_unary.c +45 -0
  47. data/ext/numo/narray/gen/tmpl/cum.c +42 -0
  48. data/ext/numo/narray/gen/tmpl/each.c +43 -0
  49. data/ext/numo/narray/gen/tmpl/each_with_index.c +64 -0
  50. data/ext/numo/narray/gen/tmpl/extract.c +23 -0
  51. data/ext/numo/narray/gen/tmpl/eye.c +91 -0
  52. data/ext/numo/narray/gen/tmpl/fill.c +38 -0
  53. data/ext/numo/narray/gen/tmpl/format.c +60 -0
  54. data/ext/numo/narray/gen/tmpl/format_to_a.c +47 -0
  55. data/ext/numo/narray/gen/tmpl/head.c +25 -0
  56. data/ext/numo/narray/gen/tmpl/inspect.c +16 -0
  57. data/ext/numo/narray/gen/tmpl/map_with_index.c +94 -0
  58. data/ext/numo/narray/gen/tmpl/median.c +44 -0
  59. data/ext/numo/narray/gen/tmpl/minmax.c +47 -0
  60. data/ext/numo/narray/gen/tmpl/poly.c +49 -0
  61. data/ext/numo/narray/gen/tmpl/pow.c +74 -0
  62. data/ext/numo/narray/gen/tmpl/powint.c +17 -0
  63. data/ext/numo/narray/gen/tmpl/qsort.c +149 -0
  64. data/ext/numo/narray/gen/tmpl/rand.c +33 -0
  65. data/ext/numo/narray/gen/tmpl/rand_norm.c +46 -0
  66. data/ext/numo/narray/gen/tmpl/robj_allocate.c +32 -0
  67. data/ext/numo/narray/gen/tmpl/seq.c +61 -0
  68. data/ext/numo/narray/gen/tmpl/set2.c +56 -0
  69. data/ext/numo/narray/gen/tmpl/sort.c +36 -0
  70. data/ext/numo/narray/gen/tmpl/sort_index.c +86 -0
  71. data/ext/numo/narray/gen/tmpl/store.c +31 -0
  72. data/ext/numo/narray/gen/tmpl/store_array.c +5 -0
  73. data/ext/numo/narray/gen/tmpl/store_from.c +53 -0
  74. data/ext/numo/narray/gen/tmpl/store_numeric.c +22 -0
  75. data/ext/numo/narray/gen/tmpl/to_a.c +41 -0
  76. data/ext/numo/narray/gen/tmpl/unary.c +58 -0
  77. data/ext/numo/narray/gen/tmpl/unary2.c +58 -0
  78. data/ext/numo/narray/gen/tmpl/unary_s.c +57 -0
  79. data/ext/numo/narray/index.c +822 -0
  80. data/ext/numo/narray/kwarg.c +79 -0
  81. data/ext/numo/narray/math.c +140 -0
  82. data/ext/numo/narray/narray.c +1539 -0
  83. data/ext/numo/narray/ndloop.c +1928 -0
  84. data/ext/numo/narray/numo/compat.h +23 -0
  85. data/ext/numo/narray/numo/intern.h +112 -0
  86. data/ext/numo/narray/numo/narray.h +411 -0
  87. data/ext/numo/narray/numo/ndloop.h +99 -0
  88. data/ext/numo/narray/numo/template.h +140 -0
  89. data/ext/numo/narray/numo/types/bit.h +19 -0
  90. data/ext/numo/narray/numo/types/complex.h +410 -0
  91. data/ext/numo/narray/numo/types/complex_macro.h +205 -0
  92. data/ext/numo/narray/numo/types/dcomplex.h +11 -0
  93. data/ext/numo/narray/numo/types/dfloat.h +12 -0
  94. data/ext/numo/narray/numo/types/float_def.h +34 -0
  95. data/ext/numo/narray/numo/types/float_macro.h +277 -0
  96. data/ext/numo/narray/numo/types/int16.h +12 -0
  97. data/ext/numo/narray/numo/types/int32.h +12 -0
  98. data/ext/numo/narray/numo/types/int64.h +12 -0
  99. data/ext/numo/narray/numo/types/int8.h +12 -0
  100. data/ext/numo/narray/numo/types/int_macro.h +34 -0
  101. data/ext/numo/narray/numo/types/robj_macro.h +218 -0
  102. data/ext/numo/narray/numo/types/robject.h +21 -0
  103. data/ext/numo/narray/numo/types/scomplex.h +11 -0
  104. data/ext/numo/narray/numo/types/sfloat.h +13 -0
  105. data/ext/numo/narray/numo/types/uint16.h +12 -0
  106. data/ext/numo/narray/numo/types/uint32.h +12 -0
  107. data/ext/numo/narray/numo/types/uint64.h +12 -0
  108. data/ext/numo/narray/numo/types/uint8.h +12 -0
  109. data/ext/numo/narray/numo/types/uint_macro.h +31 -0
  110. data/ext/numo/narray/numo/types/xint_macro.h +133 -0
  111. data/ext/numo/narray/rand.c +87 -0
  112. data/ext/numo/narray/step.c +506 -0
  113. data/ext/numo/narray/struct.c +872 -0
  114. data/lib/2.1/numo/narray.so +0 -0
  115. data/lib/2.2/numo/narray.so +0 -0
  116. data/lib/2.3/numo/narray.so +0 -0
  117. data/lib/erbpp/line_number.rb +126 -0
  118. data/lib/erbpp/narray_def.rb +338 -0
  119. data/lib/erbpp.rb +286 -0
  120. data/lib/numo/narray.rb +6 -0
  121. data/numo-narray.gemspec +35 -0
  122. data/spec/bit_spec.rb +93 -0
  123. data/spec/narray_spec.rb +249 -0
  124. metadata +238 -0
@@ -0,0 +1,811 @@
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
+ }