carray 3.0.5 → 3.0.7

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 (173) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +343 -0
  3. data/README.md +25 -4
  4. data/carray.gemspec +3 -2
  5. data/ext/ca_axis_dispatch.c +6 -0
  6. data/ext/ca_axis_group.c +7 -7
  7. data/ext/ca_binop_dispatch.c +8 -0
  8. data/ext/ca_binop_dispatch.h +1 -0
  9. data/ext/ca_categorical_iterator.c +14 -14
  10. data/ext/ca_kernel_iterator.c +116 -231
  11. data/ext/ca_kernel_iterator.h +416 -258
  12. data/ext/ca_obj_array.c +4 -4
  13. data/ext/ca_obj_bincmp.c +28 -4
  14. data/ext/ca_obj_binop.c +46 -12
  15. data/ext/ca_obj_bitarray.c +2 -4
  16. data/ext/ca_obj_bitfield.c +88 -4
  17. data/ext/ca_obj_byte_swap.c +4 -4
  18. data/ext/ca_obj_const_string.c +422 -257
  19. data/ext/ca_obj_face.c +73 -33
  20. data/ext/ca_obj_fake.c +114 -13
  21. data/ext/ca_obj_farray.c +7 -1
  22. data/ext/ca_obj_fixlen_string.c +51 -17
  23. data/ext/ca_obj_grid.c +179 -19
  24. data/ext/ca_obj_meld.c +74 -118
  25. data/ext/ca_obj_moncmp.c +9 -1
  26. data/ext/ca_obj_monop.c +100 -36
  27. data/ext/ca_obj_object.c +63 -29
  28. data/ext/ca_obj_reduce.c +17 -5
  29. data/ext/ca_obj_refer.c +162 -23
  30. data/ext/ca_obj_remap.c +69 -10
  31. data/ext/ca_obj_repeat.c +1 -1
  32. data/ext/ca_obj_roll.c +3 -4
  33. data/ext/ca_obj_select.c +12 -7
  34. data/ext/ca_obj_select_axis.c +9 -6
  35. data/ext/ca_obj_shift.c +1 -0
  36. data/ext/ca_obj_stack.c +315 -116
  37. data/ext/ca_obj_stride.c +177 -53
  38. data/ext/ca_obj_string.c +20 -4
  39. data/ext/ca_obj_tile.c +3 -4
  40. data/ext/ca_obj_triop.c +24 -13
  41. data/ext/ca_obj_window.c +23 -15
  42. data/ext/ca_op_ipower.c +71 -23
  43. data/ext/ca_op_powi.h +6 -29
  44. data/ext/ca_sort_kernels.h +3 -0
  45. data/ext/ca_sweep_engine.c +90 -23
  46. data/ext/ca_sweep_engine.h +5 -6
  47. data/ext/caframe_csv_reader.c +2 -2
  48. data/ext/carray.h +5 -2
  49. data/ext/carray_access.c +125 -103
  50. data/ext/carray_address_basis.c +209 -1
  51. data/ext/carray_attribute.c +8 -0
  52. data/ext/carray_broadcast.c +5 -6
  53. data/ext/carray_build_flags.h +1 -1
  54. data/ext/carray_call_cfunc.c +5 -8
  55. data/ext/carray_cast.c +319 -48
  56. data/ext/carray_cast_func.rb +37 -5
  57. data/ext/carray_conversion.c +65 -7
  58. data/ext/carray_copy.c +24 -0
  59. data/ext/carray_core.c +874 -72
  60. data/ext/carray_count.c +28 -9
  61. data/ext/carray_element.c +22 -12
  62. data/ext/carray_factorize.c +11 -11
  63. data/ext/carray_histogram.c +27 -5
  64. data/ext/carray_index_classifier.c +7 -5
  65. data/ext/carray_internal.h +241 -12
  66. data/ext/carray_kernels_bincmp.c +2007 -3
  67. data/ext/carray_kernels_binop.c +25502 -5336
  68. data/ext/carray_kernels_init.c +22 -22
  69. data/ext/carray_kernels_monop.c +10739 -3771
  70. data/ext/carray_kernels_reduce_aggregate.c +4429 -702
  71. data/ext/carray_kernels_reduce_boolean.c +1533 -15
  72. data/ext/carray_kernels_reduce_cumulative.c +1808 -248
  73. data/ext/carray_kernels_reduce_extreme.c +4508 -511
  74. data/ext/carray_kernels_reduce_variance.c +2284 -152
  75. data/ext/carray_kernels_scan.c +240 -240
  76. data/ext/carray_kernels_search.c +4211 -264
  77. data/ext/carray_kernels_sort.c +367 -450
  78. data/ext/carray_lazy.c +63 -1
  79. data/ext/carray_loop.c +10 -3
  80. data/ext/carray_mask.c +247 -149
  81. data/ext/carray_median_percentile.c +17 -12
  82. data/ext/carray_memory_view.c +54 -29
  83. data/ext/carray_operator.c +219 -55
  84. data/ext/carray_order.c +55 -11
  85. data/ext/carray_partition.c +1 -1
  86. data/ext/carray_random.c +5 -2
  87. data/ext/carray_round.c +24 -1
  88. data/ext/carray_scatter.c +4 -0
  89. data/ext/carray_segment.c +1 -1
  90. data/ext/carray_slab.c +110 -25
  91. data/ext/carray_sort.c +7 -4
  92. data/ext/carray_sort_kernel.c +24 -0
  93. data/ext/carray_struct.c +21 -6
  94. data/ext/carray_test.c +154 -53
  95. data/ext/carray_time_text.c +8 -2
  96. data/ext/carray_utils.c +124 -54
  97. data/ext/extconf.rb +11 -0
  98. data/ext/mk_call_cfunc.rb +6 -11
  99. data/ext/mkkernel.rb +383 -121
  100. data/ext/ruby_carray.c +4 -1
  101. data/ext/version.h +4 -4
  102. data/lib/carray/attribute.rb +25 -1
  103. data/lib/carray/autoload_carray.rb +3 -3
  104. data/lib/carray/autoload_method_extension.rb +5 -3
  105. data/lib/carray/axis_group.rb +22 -13
  106. data/lib/carray/basics.rb +27 -9
  107. data/lib/carray/bincount_nd.rb +8 -2
  108. data/lib/carray/block_iterator.rb +65 -24
  109. data/lib/carray/categorical.rb +27 -16
  110. data/lib/carray/categorical_iterator.rb +4 -3
  111. data/lib/carray/complex.rb +2 -2
  112. data/lib/carray/conditional.rb +55 -6
  113. data/lib/carray/const_string.rb +74 -0
  114. data/lib/carray/construct.rb +2 -2
  115. data/lib/carray/core_extensions.rb +19 -68
  116. data/lib/carray/data_type_extension.rb +1 -0
  117. data/lib/carray/fixlen_string.rb +83 -1
  118. data/lib/carray/frame/column_verbs.rb +102 -16
  119. data/lib/carray/frame/concat.rb +50 -6
  120. data/lib/carray/frame/convert.rb +19 -0
  121. data/lib/carray/frame/csv_parser.rb +45 -36
  122. data/lib/carray/frame/describe.rb +4 -2
  123. data/lib/carray/frame/frame.rb +436 -83
  124. data/lib/carray/frame/group.rb +731 -78
  125. data/lib/carray/frame/io.rb +216 -73
  126. data/lib/carray/frame/join.rb +166 -43
  127. data/lib/carray/frame/records.rb +9 -7
  128. data/lib/carray/frame/reshape.rb +109 -11
  129. data/lib/carray/frame/sort.rb +57 -9
  130. data/lib/carray/frame/verbs.rb +448 -60
  131. data/lib/carray/frame/windows.rb +71 -9
  132. data/lib/carray/fuse_source.rb +4 -2
  133. data/lib/carray/inspect.rb +7 -2
  134. data/lib/carray/iterator.rb +40 -0
  135. data/lib/carray/key_windows.rb +10 -4
  136. data/lib/carray/lazy.rb +28 -24
  137. data/lib/carray/mask_gap_fill.rb +37 -17
  138. data/lib/carray/meld_reduce.rb +1 -1
  139. data/lib/carray/methods/bin.rb +3 -0
  140. data/lib/carray/methods/choose.rb +3 -2
  141. data/lib/carray/methods/composition.rb +1 -1
  142. data/lib/carray/methods/factorize.rb +2 -0
  143. data/lib/carray/methods/gather_nd.rb +4 -1
  144. data/lib/carray/methods/is_in.rb +23 -3
  145. data/lib/carray/methods/locate_addr.rb +63 -12
  146. data/lib/carray/methods/meshgrid.rb +3 -1
  147. data/lib/carray/methods/repeat.rb +11 -1
  148. data/lib/carray/record_order.rb +35 -6
  149. data/lib/carray/runtime.rb +57 -8
  150. data/lib/carray/segment_iterator.rb +42 -24
  151. data/lib/carray/serialize.rb +56 -17
  152. data/lib/carray/slab_iterator.rb +1 -1
  153. data/lib/carray/stack.rb +21 -9
  154. data/lib/carray/string.rb +6 -6
  155. data/lib/carray/string_operation_extension.rb +25 -4
  156. data/lib/carray/struct_builder.rb +4 -1
  157. data/lib/carray/time.rb +195 -46
  158. data/lib/carray/window_iterator.rb +64 -16
  159. data/yard-stubs/ca_obj_array.rb +26 -1
  160. data/yard-stubs/ca_obj_bitfield.rb +4 -1
  161. data/yard-stubs/ca_obj_refer.rb +3 -0
  162. data/yard-stubs/ca_obj_window.rb +2 -1
  163. data/yard-stubs/carray_access.rb +4 -2
  164. data/yard-stubs/carray_cast.rb +14 -5
  165. data/yard-stubs/carray_conversion.rb +8 -2
  166. data/yard-stubs/carray_count.rb +6 -3
  167. data/yard-stubs/carray_mask.rb +20 -1
  168. data/yard-stubs/carray_math.rb +36 -16
  169. data/yard-stubs/carray_memory_view.rb +7 -0
  170. data/yard-stubs/carray_order.rb +6 -0
  171. data/yard-stubs/carray_scatter.rb +2 -1
  172. data/yard-stubs/carray_test.rb +23 -7
  173. metadata +17 -3
data/ext/carray_count.c CHANGED
@@ -82,7 +82,7 @@ ca_count_each_body (VALUE arg)
82
82
  /* Extract vk as a scalar VALUE via flat addr. rb_ca_fetch_addr
83
83
  takes a ca_size_t addr (not a Ruby VALUE). */
84
84
  VALUE vk = rb_ca_fetch_addr(c->rval, k);
85
- VALUE sub;
85
+ volatile VALUE sub;
86
86
  /* A masked query cell asks nothing: its count is masked. Passing it
87
87
  on would ask count(UNDEF), the count of self's masked cells. */
88
88
  if ( vk == CA_UNDEF ) {
@@ -154,7 +154,14 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
154
154
  }
155
155
  }
156
156
 
157
- volatile VALUE rval = argv[0];
157
+ /* true and false count the cells holding 1 and 0 of a numeric array */
158
+ volatile VALUE rval = ca_truth_as_number(src, argv[0]);
159
+ if ( rval != argv[0] ) {
160
+ VALUE *nargv = ALLOCA_N(VALUE, argc);
161
+ MEMCPY(nargv, argv, VALUE, argc);
162
+ nargv[0] = rval;
163
+ argv = nargv;
164
+ }
158
165
 
159
166
  /* count(UNDEF) == count_masked: UNDEF is mask-state vocabulary, not a
160
167
  value to compare against. Forward, dropping the UNDEF from argv. */
@@ -162,6 +169,17 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
162
169
  return rb_ca_count_masked(argc - 1, argv + 1, self);
163
170
  }
164
171
 
172
+ /* An array that can not hold the value (nil, a String) counts no cell
173
+ for it: the cells that matched, none, are counted the way count(true)
174
+ counts a mask. */
175
+ if ( ca_query_matches_nothing(src, rval) && ! ca_is_complex_type(src) ) {
176
+ VALUE *nargv = ALLOCA_N(VALUE, argc);
177
+ MEMCPY(nargv, argv, VALUE, argc);
178
+ nargv[0] = Qtrue;
179
+ return rb_ca_count(argc, nargv,
180
+ rb_funcall(self, rb_intern("eq"), 1, rval));
181
+ }
182
+
165
183
  /* Face gate, mirroring the search / linear family (ext/carray_order.c):
166
184
  descend an ORDERABLE Face to its storage so the equality runs on the
167
185
  numeric storage, and let the reference reconcile the query into its own
@@ -284,7 +302,7 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
284
302
  }
285
303
 
286
304
  /* Allocate output i64 CArray. */
287
- VALUE vout = rb_funcall(rb_cCArray, rb_intern("new"), 2,
305
+ volatile VALUE vout = rb_funcall(rb_cCArray, rb_intern("new"), 2,
288
306
  INT2NUM(CA_INT64),
289
307
  rb_ary_new4(out_ndim,
290
308
  ({ VALUE *tmp = ALLOCA_N(VALUE, out_ndim);
@@ -348,16 +366,17 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
348
366
  iv);
349
367
  } else {
350
368
  rb_raise(rb_eTypeError,
351
- "count(v) on boolean array: v must be true / false / 1 / 0, got %s",
352
- rb_obj_classname(rval));
369
+ "count(v) on boolean array: v must be true / false / 1 / 0, got %"PRIsVALUE"",
370
+ rb_obj_class(rval));
353
371
  }
354
372
  } else {
355
- /* true / false are the boolean array's own domain, not a number --
356
- except in an object array, where they are ordinary stored values. */
373
+ /* An integer, float or complex array has read true / false as 1 / 0
374
+ above (ca_truth_as_number); an object array stores them as values.
375
+ Any other array that reaches here can not count them. */
357
376
  if ( ( rval == Qtrue || rval == Qfalse ) && src->data_type != CA_OBJECT ) {
358
377
  rb_raise(rb_eTypeError,
359
- "count(v) on numeric array: v must be numeric, got %s",
360
- rb_obj_classname(rval));
378
+ "count(v) on numeric array: v must be numeric, got %"PRIsVALUE"",
379
+ rb_obj_class(rval));
361
380
  }
362
381
  /* The query is compared in the type it and the array match in, as `eq`
363
382
  compares (ca_query_match_type): 1.5 does not match 1, and an Integer
data/ext/carray_element.c CHANGED
@@ -38,22 +38,28 @@ elem_index_value (VALUE v)
38
38
  }
39
39
  }
40
40
 
41
+ ca_size_t
42
+ ca_index_key_value (VALUE v)
43
+ {
44
+ return elem_index_value(v);
45
+ }
46
+
41
47
  /* T_ARRAY index parser. ridx is assumed already known to be T_ARRAY.
42
48
  Direct VALUE * access + FIXNUM_P fast path eliminates rb_ary_entry
43
49
  bounds check and NUM2SIZE Bignum branching for the common case. */
44
50
  static inline void
45
51
  elem_parse_idx_array (CArray *ca, VALUE rary, ca_size_t *idx)
46
52
  {
47
- const VALUE *iarr;
48
53
  int8_t ndim = ca->ndim;
49
54
  int8_t i;
50
55
  if ( RARRAY_LEN(rary) != ndim ) {
51
56
  rb_raise(rb_eIndexError, "%ld indices given for a carray of ndim %i",
52
57
  (long) RARRAY_LEN(rary), (int) ndim);
53
58
  }
54
- iarr = RARRAY_CONST_PTR(rary);
55
59
  for (i=0; i<ndim; i++) {
56
- ca_size_t k = elem_index_value(iarr[i]);
60
+ /* Read each entry afresh: converting one can run Ruby that changes the
61
+ Array (an entry gone reads as nil, which is refused). */
62
+ ca_size_t k = elem_index_value(rb_ary_entry(rary, i));
57
63
  CA_CHECK_INDEX(k, ca->dim[i]);
58
64
  idx[i] = k;
59
65
  }
@@ -82,8 +88,9 @@ elem_probe_mask (CArray *ca)
82
88
 
83
89
  /* Entity-array fast path: compute flat addr from idx[] and access ptr directly.
84
90
  Bypasses ca_func[ARRAY].xfer_index function-pointer dispatch.
85
- Caller must ensure ca_is_entity(ca) and ca->ptr is attached (= entity
86
- arrays are always attached by definition). */
91
+ Caller must ensure ca_is_entity(ca) and ca->ptr != NULL: an entity can
92
+ leave ptr NULL at rest (a CASource), and then the dispatched path is
93
+ the way in. */
87
94
  static inline ca_size_t
88
95
  elem_entity_addr (CArray *ca, ca_size_t *idx)
89
96
  {
@@ -273,7 +280,7 @@ rb_ca_elem_store (VALUE self, VALUE ridx, VALUE obj)
273
280
  Bypasses rb_ca_store_index/addr -> ca_update_mask + rb_ca_obj2ptr ->
274
281
  cast_table dispatch. Falls through to general path for: views,
275
282
  existing mask, non-numeric data type, UNDEF, or non-Numeric obj. */
276
- if ( ca_is_entity(ca) && ! ca->mask && obj != CA_UNDEF ) {
283
+ if ( ca_is_entity(ca) && ca->ptr && ! ca->mask && obj != CA_UNDEF ) {
277
284
  int8_t dt = ca->data_type;
278
285
  if ( dt == CA_FLOAT64 || dt == CA_FLOAT32 ||
279
286
  dt == CA_INT64 || dt == CA_INT32 ||
@@ -383,7 +390,7 @@ rb_ca_elem_fetch (VALUE self, VALUE ridx)
383
390
  /* Super-hot path: entity + common numeric data type + no mask.
384
391
  Bypass ca_fetch_index dispatch AND rb_ca_ptr2obj_storage cast-table dispatch.
385
392
  Covers the bench's float64 / int64 / int32 cases. */
386
- if ( ca_is_entity(ca) && ! ca->mask ) {
393
+ if ( ca_is_entity(ca) && ca->ptr && ! ca->mask ) {
387
394
  ca_size_t addr = elem_entity_addr(ca, idx);
388
395
  char *p = ca->ptr + ca->bytes * addr;
389
396
  switch ( ca->data_type ) {
@@ -406,7 +413,7 @@ rb_ca_elem_fetch (VALUE self, VALUE ridx)
406
413
  TypedData_Get_Struct). Semantics identical to rb_ca_fetch_index. */
407
414
  if ( ca->bytes <= 64 ) {
408
415
  char v[64];
409
- if ( ca_is_entity(ca) ) {
416
+ if ( ca_is_entity(ca) && ca->ptr ) {
410
417
  ca_size_t addr = elem_entity_addr(ca, idx);
411
418
  memcpy(v, ca->ptr + ca->bytes * addr, ca->bytes);
412
419
  }
@@ -439,12 +446,15 @@ rb_ca_elem_fetch (VALUE self, VALUE ridx)
439
446
  }
440
447
 
441
448
  CA_FACE_STORAGE_TO_ELEMENT_IF_FACE(out, self, ca);
449
+ if ( ca->data_type == CA_BOOLEAN ) {
450
+ out = RTEST(out) && out != INT2FIX(0) ? Qtrue : Qfalse;
451
+ }
442
452
  return out;
443
453
  }
444
454
  else {
445
455
  ca_size_t addr = elem_parse_idx_scalar(ca, ridx);
446
456
  /* Super-hot flat-addr path: entity + numeric + no mask. */
447
- if ( ca_is_entity(ca) && ! ca->mask ) {
457
+ if ( ca_is_entity(ca) && ca->ptr && ! ca->mask ) {
448
458
  char *p = ca->ptr + ca->bytes * addr;
449
459
  switch ( ca->data_type ) {
450
460
  case CA_FLOAT64: return rb_float_new(*(double *)p);
@@ -493,7 +503,7 @@ rb_ca_elem_incr (VALUE self, VALUE ridx1)
493
503
 
494
504
  /* Super-hot path: entity + no mask + integer data type.
495
505
  Reads p directly, modifies in place — no separate fetch/store dispatch. */
496
- if ( ca_is_entity(ca) && ! has_mask ) {
506
+ if ( ca_is_entity(ca) && ca->ptr && ! has_mask ) {
497
507
  ca_size_t addr;
498
508
  if ( TYPE(ridx1) == T_ARRAY ) {
499
509
  elem_parse_idx_array(ca, ridx1, idx1);
@@ -583,7 +593,7 @@ rb_ca_elem_decr (VALUE self, VALUE ridx1)
583
593
  has_mask = elem_probe_mask(ca);
584
594
 
585
595
  /* Super-hot path: mirror of elem_incr. */
586
- if ( ca_is_entity(ca) && ! has_mask ) {
596
+ if ( ca_is_entity(ca) && ca->ptr && ! has_mask ) {
587
597
  ca_size_t addr;
588
598
  if ( TYPE(ridx1) == T_ARRAY ) {
589
599
  elem_parse_idx_array(ca, ridx1, idx1);
@@ -798,7 +808,7 @@ rb_ca_elem_unmask (VALUE self, VALUE ridx)
798
808
  general path, which compares in Ruby and stores as `self[idx] = v`
799
809
  does, so a value that wins but does not fit raises. */
800
810
  #define ELEM_MINMAX_BODY(OP) do { \
801
- if ( ca_is_entity(ca) ) { \
811
+ if ( ca_is_entity(ca) && ca->ptr ) { \
802
812
  ca_size_t addr; \
803
813
  if ( TYPE(ridx) == T_ARRAY ) { \
804
814
  elem_parse_idx_array(ca, ridx, idx); \
@@ -818,7 +818,7 @@ static VALUE
818
818
  fz_factorize_appearance_body (VALUE arg)
819
819
  {
820
820
  fz_frame *fr = (fz_frame *) arg;
821
- VALUE self = fr->self;
821
+ volatile VALUE self = fr->self;
822
822
  CArray *ca;
823
823
  volatile VALUE face;
824
824
  fr->self = self = fz_face_descend(self, &face, "categorize");
@@ -843,7 +843,7 @@ fz_factorize_appearance_body (VALUE arg)
843
843
  ca_size_t N = ca->elements;
844
844
 
845
845
  /* uint32 code scratch, same shape as self; narrowed once k is known. */
846
- VALUE vu32 = rb_carray_new(CA_UINT32, ca->ndim, ca->dim, 0, NULL);
846
+ volatile VALUE vu32 = rb_carray_new(CA_UINT32, ca->ndim, ca->dim, 0, NULL);
847
847
  CArray *cu32;
848
848
  TypedData_Get_Struct(vu32, CArray, &carray_data_type, cu32);
849
849
 
@@ -1028,7 +1028,7 @@ static VALUE
1028
1028
  fz_mask_duplicates_body (VALUE arg)
1029
1029
  {
1030
1030
  fz_frame *fr = (fz_frame *) arg;
1031
- VALUE self = fr->self;
1031
+ volatile VALUE self = fr->self;
1032
1032
  VALUE vaxis = fr->a;
1033
1033
  CArray *ca;
1034
1034
  TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
@@ -1189,7 +1189,7 @@ static VALUE
1189
1189
  fz_unique_flat_body (VALUE arg)
1190
1190
  {
1191
1191
  fz_frame *fr = (fz_frame *) arg;
1192
- VALUE self = fr->self;
1192
+ volatile VALUE self = fr->self;
1193
1193
  CArray *ca;
1194
1194
  volatile VALUE face;
1195
1195
  fr->self = self = fz_face_descend(self, &face, "unique");
@@ -1429,7 +1429,7 @@ static VALUE
1429
1429
  fz_is_in_body (VALUE arg)
1430
1430
  {
1431
1431
  fz_frame *fr = (fz_frame *) arg;
1432
- VALUE self = fr->self;
1432
+ volatile VALUE self = fr->self;
1433
1433
  VALUE rvalues = fr->a;
1434
1434
  CArray *ca, *cv;
1435
1435
  volatile VALUE face;
@@ -1592,7 +1592,7 @@ static VALUE
1592
1592
  fz_locate_addr_body (VALUE arg)
1593
1593
  {
1594
1594
  fz_frame *fr = (fz_frame *) arg;
1595
- VALUE self = fr->self;
1595
+ volatile VALUE self = fr->self;
1596
1596
  VALUE rref = fr->a;
1597
1597
  CArray *ca, *cr;
1598
1598
  /* Here the *reference* is the argument (self is the query being placed on
@@ -1859,7 +1859,7 @@ static VALUE
1859
1859
  fz_set_relation_body (VALUE arg)
1860
1860
  {
1861
1861
  fz_frame *fr = (fz_frame *) arg;
1862
- VALUE self = fr->self;
1862
+ volatile VALUE self = fr->self;
1863
1863
  VALUE rother = fr->a;
1864
1864
  int keep_when_hit = fr->i;
1865
1865
  CArray *ca, *co;
@@ -2055,7 +2055,7 @@ static VALUE
2055
2055
  fz_set_union_body (VALUE arg)
2056
2056
  {
2057
2057
  fz_frame *fr = (fz_frame *) arg;
2058
- VALUE self = fr->self;
2058
+ volatile VALUE self = fr->self;
2059
2059
  VALUE rother = fr->a;
2060
2060
  CArray *ca, *co;
2061
2061
  volatile VALUE face;
@@ -2149,7 +2149,7 @@ static VALUE
2149
2149
  fz_value_counts_flat_body (VALUE arg)
2150
2150
  {
2151
2151
  fz_frame *fr = (fz_frame *) arg;
2152
- VALUE self = fr->self;
2152
+ volatile VALUE self = fr->self;
2153
2153
  CArray *ca;
2154
2154
  volatile VALUE face;
2155
2155
  fr->self = self = fz_face_descend(self, &face, "value_counts");
@@ -2325,7 +2325,7 @@ static VALUE
2325
2325
  fz_nunique_body (VALUE arg)
2326
2326
  {
2327
2327
  fz_frame *fr = (fz_frame *) arg;
2328
- VALUE self = fr->self;
2328
+ volatile VALUE self = fr->self;
2329
2329
  VALUE vaxis = fr->a;
2330
2330
  VALUE vkeep = fr->b;
2331
2331
  CArray *ca;
@@ -2472,7 +2472,7 @@ static VALUE
2472
2472
  fz_is_mode_body (VALUE arg)
2473
2473
  {
2474
2474
  fz_frame *fr = (fz_frame *) arg;
2475
- VALUE self = fr->self;
2475
+ volatile VALUE self = fr->self;
2476
2476
  VALUE vaxis = fr->a;
2477
2477
  CArray *ca;
2478
2478
  TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
@@ -25,6 +25,7 @@
25
25
  #include "carray.h"
26
26
  #include "carray_internal.h" /* ca_attach_window */
27
27
  #include <math.h>
28
+ #include <float.h>
28
29
 
29
30
  /* Edges are the bin boundaries and must read as such: no masked edge (its
30
31
  stored value is no boundary), no NaN, and no edge below the one before
@@ -88,6 +89,8 @@ typedef struct {
88
89
  const double *ep;
89
90
  ca_size_t n; /* number of bins */
90
91
  double lo, hi, inv_dx;
92
+ double window; /* fraction of a bin within which the
93
+ linearised floor is checked */
91
94
  int uniform;
92
95
  int include_max;
93
96
  } histbin_axis_t;
@@ -115,7 +118,7 @@ histbin_index (const histbin_axis_t *ax, double v)
115
118
  Interior values (the bulk) skip the data-dependent edge loads, keeping
116
119
  the fast path vectorisable. */
117
120
  frac = pos - (double) k;
118
- if ( frac < 1e-9 || frac > 1.0 - 1e-9 ) {
121
+ if ( frac < ax->window || frac > 1.0 - ax->window ) {
119
122
  while ( k + 1 < ax->n && v >= ax->ep[k + 1] ) k++;
120
123
  while ( k > 0 && v < ax->ep[k] ) k--;
121
124
  }
@@ -136,7 +139,7 @@ histbin_axis_setup (histbin_axis_t *ax, const double *ep, ca_size_t ne,
136
139
  int include_max)
137
140
  {
138
141
  ca_size_t kk;
139
- double dx, tol;
142
+ double dx, tol, maxdev;
140
143
 
141
144
  ax->ep = ep;
142
145
  ax->n = ne - 1;
@@ -149,8 +152,27 @@ histbin_axis_setup (histbin_axis_t *ax, const double *ep, ca_size_t ne,
149
152
  width infinite, and a subnormal width makes the reciprocal infinite.
150
153
  Either turns the position into NaN, which has no integer. */
151
154
  ax->uniform = isfinite(dx) && dx != 0.0 && isfinite(1.0 / dx);
155
+ maxdev = 0.0;
152
156
  for ( kk = 0; ax->uniform && kk <= ax->n; kk++ ) {
153
- if ( fabs(ax->ep[kk] - (ax->lo + (double) kk * dx)) > tol ) { ax->uniform = 0; break; }
157
+ double dev = fabs(ax->ep[kk] - (ax->lo + (double) kk * dx));
158
+ if ( dev > tol ) { ax->uniform = 0; break; }
159
+ if ( dev > maxdev ) maxdev = dev;
160
+ }
161
+ /* A value can land on the wrong side of the linearised floor by as much
162
+ as an edge strays from the straight line, plus the rounding of the
163
+ position itself: v - lo is rounded at the scale of the edges' magnitude
164
+ (an ulp of lo or hi, which can be most of a bin when the edges are
165
+ close together far from zero), and the product grows with the number
166
+ of bins. Values within that much of a boundary are checked against the
167
+ stored edges. When the window covers a good part of a bin, the binary
168
+ search is cheaper. */
169
+ ax->window = ( ax->uniform )
170
+ ? maxdev / fabs(dx)
171
+ + 4.0 * DBL_EPSILON * (fabs(ax->lo) + fabs(ax->hi)) / fabs(dx)
172
+ + (double) (ax->n + 2) * 8.0 * DBL_EPSILON + 1e-9
173
+ : 0.0;
174
+ if ( ax->window > 0.25 ) {
175
+ ax->uniform = 0;
154
176
  }
155
177
  ax->inv_dx = (dx != 0.0) ? 1.0 / dx : 0.0;
156
178
  ax->include_max = include_max;
@@ -415,7 +437,7 @@ rb_ca_histogram_scatter_ki (VALUE self, VALUE rcounts, VALUE redges,
415
437
  }
416
438
 
417
439
  for ( k = 0; k < M; k++ ) {
418
- VALUE e = RARRAY_AREF(redges, k);
440
+ volatile VALUE e = RARRAY_AREF(redges, k);
419
441
  GetCArray(e, edge_ca[k]);
420
442
  histbin_check_edge_mask(edge_ca[k], "histogram");
421
443
  }
@@ -506,7 +528,7 @@ bincount_nd_label_u64 (uint64_t x)
506
528
  } while (0)
507
529
 
508
530
  static VALUE
509
- rb_ca_bincount_nd_count_ki (VALUE self, VALUE rcounts, VALUE rweights)
531
+ rb_ca_bincount_nd_count_ki (VALUE self, VALUE rcounts, volatile VALUE rweights)
510
532
  {
511
533
  CArray *ca, *cc, *cw = NULL;
512
534
  long M, nf, k;
@@ -322,15 +322,15 @@ ca_classifier_check_reserved_symbol (VALUE sym)
322
322
  {
323
323
  const char *name;
324
324
  if ( ! SYMBOL_P(sym) ) return;
325
- name = rb_id2name(SYM2ID(sym));
325
+ name = rb_id2name(SYM2ID(sym)); /* name: its first two bytes are read at once */
326
326
  if ( name != NULL
327
327
  && name[0] != '\0' && name[1] == '\0'
328
328
  && ( ( name[0] >= 'a' && name[0] <= 'z' )
329
329
  || ( name[0] >= 'A' && name[0] <= 'Z' ) )
330
330
  && name[0] != '_' /* :_ is iterator marker, handled separately */ ) {
331
331
  rb_raise(rb_eIndexError,
332
- "symbol :%s is reserved",
333
- name);
332
+ "symbol :%"PRIsVALUE" is reserved",
333
+ rb_sym2str(sym));
334
334
  }
335
335
  }
336
336
 
@@ -1005,7 +1005,7 @@ rb_ca_scan_index_v2 (int ca_ndim, ca_size_t *ca_dim, ca_size_t ca_elements,
1005
1005
  also fall through here and are raised later by the per-axis
1006
1006
  dispatcher's SYMBOL_P branch. */
1007
1007
  if ( SYMBOL_P(argv[0]) ) {
1008
- const char *name = rb_id2name(SYM2ID(argv[0]));
1008
+ const char *name = rb_id2name(SYM2ID(argv[0])); /* name: read at once by strlen */
1009
1009
  if ( name != NULL && strlen(name) > 1 ) {
1010
1010
  info->type = CA_REG_METHOD_CALL;
1011
1011
  info->symbol = argv[0];
@@ -1069,7 +1069,7 @@ rb_ca_scan_index_v2 (int ca_ndim, ca_size_t *ca_dim, ca_size_t ca_elements,
1069
1069
  downstream introspection. */
1070
1070
 
1071
1071
  static VALUE
1072
- rb_ca_s_scan_index_v2 (VALUE self, VALUE rdim, VALUE ridx)
1072
+ rb_ca_s_scan_index_v2 (VALUE self, VALUE rdim, volatile VALUE ridx)
1073
1073
  {
1074
1074
  CAIndexInfo info;
1075
1075
  ca_size_t dim[CA_RANK_MAX];
@@ -1079,6 +1079,8 @@ rb_ca_s_scan_index_v2 (VALUE self, VALUE rdim, VALUE ridx)
1079
1079
 
1080
1080
  Check_Type(rdim, T_ARRAY);
1081
1081
  Check_Type(ridx, T_ARRAY);
1082
+ /* Converting an entry can run Ruby that changes the Array: read a copy. */
1083
+ ridx = rb_ary_dup(ridx);
1082
1084
 
1083
1085
  ndim = (int) RARRAY_LEN(rdim);
1084
1086
  elements = 1;