carray 3.0.6 → 3.1.0

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 (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
data/ext/ca_obj_refer.c CHANGED
@@ -14,7 +14,6 @@
14
14
  ---------------------------------------------------------------------------- */
15
15
 
16
16
  #include "carray.h"
17
- #include "carray_internal.h" /* per-obj_type view constructors */
18
17
  #include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE for CARecord parent */
19
18
  #include "ruby/thread_native.h"
20
19
 
@@ -52,7 +51,7 @@ const rb_data_type_t carefer_data_type = {
52
51
 
53
52
  const rb_data_type_t carefer_mask_data_type = {
54
53
  .parent = &carefer_data_type,
55
- .wrap_struct_name = "CAReferMask",
54
+ .wrap_struct_name = "CARefer::Mask",
56
55
  .function = {
57
56
  .dmark = NULL,
58
57
  .dfree = ca_free_nop,
@@ -75,10 +74,10 @@ ca_refer_setup (CARefer *ca, CArray *parent,
75
74
  ca_size_t s;
76
75
  int8_t i;
77
76
 
78
- CA_CHECK_DATA_TYPE_GIVEN(data_type);
79
- CA_CHECK_RANK_GIVEN(ndim);
80
- CA_CHECK_DIM_GIVEN(ndim, dim);
81
- CA_CHECK_BYTES_GIVEN(data_type, bytes);
77
+ CA_CHECK_DATA_TYPE(data_type);
78
+ CA_CHECK_RANK(ndim);
79
+ CA_CHECK_DIM(ndim, dim);
80
+ CA_CHECK_BYTES(data_type, bytes);
82
81
 
83
82
  /* An object cell is a VALUE the GC marks: reading other bytes as one
84
83
  marks garbage, and reading one as bytes exposes an address. */
@@ -261,7 +260,7 @@ rb_ca_refer_s_allocate (VALUE klass)
261
260
  }
262
261
 
263
262
  static VALUE
264
- rb_ca_refer_initialize_copy (VALUE self, VALUE other)
263
+ rb_ca_refer_initialize_copy (volatile VALUE self, volatile VALUE other)
265
264
  {
266
265
  CARefer *ca, *cs;
267
266
  TypedData_Get_Struct(self, CARefer, &carefer_data_type, ca);
@@ -339,16 +338,75 @@ ca_keep_chain_flags (CArray *view, CArray *src)
339
338
  }
340
339
 
341
340
  /* The sources of folded views (CA_FLAG_FOLDED), keyed by the view. Views are
342
- made and freed in any Ractor, so the table is taken under a lock. */
341
+ made and freed in any Ractor, so the table is taken under a lock.
342
+
343
+ st_insert can grow the table through ruby_xmalloc, which can run a GC, and
344
+ the GC frees folded views through ca_fold_forget. ca_fold_forget therefore
345
+ never waits for the lock: when it is held (by the inserting thread itself,
346
+ or by a Ractor stopped for this GC) the view is put on a list instead, and
347
+ whoever takes the lock next removes the listed views first. A freed view's
348
+ address can be reused by a new view only once that view is recorded, which
349
+ takes the lock and so empties the list first. */
343
350
  static st_table *ca_fold_sources;
344
351
  static rb_nativethread_lock_t ca_fold_lock;
345
352
 
353
+ typedef struct ca_fold_gone {
354
+ struct ca_fold_gone *next;
355
+ st_data_t key;
356
+ } ca_fold_gone_t;
357
+
358
+ static ca_fold_gone_t *ca_fold_gone_list = NULL;
359
+
360
+ /* With the lock held: remove the views freed while it was held elsewhere.
361
+ The list is allocated with malloc, not xmalloc: it is grown inside a GC. */
362
+ static void
363
+ ca_fold_forget_gone (void)
364
+ {
365
+ ca_fold_gone_t *g = __atomic_exchange_n(&ca_fold_gone_list, NULL, __ATOMIC_ACQ_REL);
366
+ while ( g ) {
367
+ ca_fold_gone_t *next = g->next;
368
+ st_data_t key = g->key;
369
+ st_delete(ca_fold_sources, &key, NULL);
370
+ free(g);
371
+ g = next;
372
+ }
373
+ }
374
+
375
+ static void
376
+ ca_fold_lock_table (void)
377
+ {
378
+ ca_native_lock_without_gvl(&ca_fold_lock);
379
+ ca_fold_forget_gone();
380
+ }
381
+
382
+ typedef struct {
383
+ CArray *view;
384
+ CArray *src;
385
+ } ca_fold_note_arg_t;
386
+
387
+ static VALUE
388
+ ca_fold_insert (VALUE varg)
389
+ {
390
+ ca_fold_note_arg_t *a = (ca_fold_note_arg_t *) varg;
391
+ st_insert(ca_fold_sources, (st_data_t) a->view, (st_data_t) a->src);
392
+ return Qnil;
393
+ }
394
+
395
+ /* The growth of the table can raise NoMemoryError; the lock is released
396
+ before that raise goes on. */
346
397
  void
347
398
  ca_fold_note_source (CArray *view, CArray *src)
348
399
  {
349
- rb_native_mutex_lock(&ca_fold_lock);
350
- st_insert(ca_fold_sources, (st_data_t) view, (st_data_t) src);
400
+ ca_fold_note_arg_t arg = { view, src };
401
+ int state = 0;
402
+ /* lock: st_insert can run a GC; a view it frees is listed, not waited for */
403
+ ca_fold_lock_table();
404
+ /* protect: releases a native lock; no array is held */
405
+ rb_protect(ca_fold_insert, (VALUE) &arg, &state);
351
406
  rb_native_mutex_unlock(&ca_fold_lock);
407
+ if ( state ) {
408
+ rb_jump_tag(state);
409
+ }
352
410
  ca_set_flag(view, CA_FLAG_FOLDED);
353
411
  }
354
412
 
@@ -356,7 +414,7 @@ CArray *
356
414
  ca_fold_source (CArray *view)
357
415
  {
358
416
  st_data_t src = 0;
359
- rb_native_mutex_lock(&ca_fold_lock);
417
+ ca_fold_lock_table();
360
418
  st_lookup(ca_fold_sources, (st_data_t) view, &src);
361
419
  rb_native_mutex_unlock(&ca_fold_lock);
362
420
  return (CArray *) src;
@@ -366,9 +424,30 @@ void
366
424
  ca_fold_forget (CArray *view)
367
425
  {
368
426
  st_data_t key = (st_data_t) view;
369
- rb_native_mutex_lock(&ca_fold_lock);
370
- st_delete(ca_fold_sources, &key, NULL);
371
- rb_native_mutex_unlock(&ca_fold_lock);
427
+ ca_fold_gone_t *g;
428
+ if ( rb_native_mutex_trylock(&ca_fold_lock) == 0 ) {
429
+ ca_fold_forget_gone();
430
+ st_delete(ca_fold_sources, &key, NULL);
431
+ rb_native_mutex_unlock(&ca_fold_lock);
432
+ return;
433
+ }
434
+ g = (ca_fold_gone_t *) malloc(sizeof(*g));
435
+ if ( g == NULL ) {
436
+ /* No memory for the list: wait for the lock, as there is no other way
437
+ to drop the record. */
438
+ /* lock: only with no memory for the list; the wait may block the GC */
439
+ rb_native_mutex_lock(&ca_fold_lock);
440
+ ca_fold_forget_gone();
441
+ st_delete(ca_fold_sources, &key, NULL);
442
+ rb_native_mutex_unlock(&ca_fold_lock);
443
+ return;
444
+ }
445
+ g->key = key;
446
+ g->next = __atomic_load_n(&ca_fold_gone_list, __ATOMIC_ACQUIRE);
447
+ while ( ! __atomic_compare_exchange_n(&ca_fold_gone_list, &g->next, g, 0,
448
+ __ATOMIC_RELEASE, __ATOMIC_ACQUIRE) ) {
449
+ /* g->next now holds the head that won; try again on top of it */
450
+ }
372
451
  }
373
452
 
374
453
  /* Builds the folded view of `ca` for reshape / flatten: a CAStride over
@@ -521,7 +600,7 @@ ca_reshape_try_strides (CArray *parent,
521
600
  build the options themselves (a Hash at the end of argv is read as
522
601
  options only when the Ruby caller passed keywords). */
523
602
  static VALUE
524
- rb_ca_refer_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
603
+ rb_ca_refer_kw (int argc, VALUE *argv, VALUE ropt, volatile VALUE self)
525
604
  {
526
605
  volatile VALUE obj = Qnil;
527
606
  CArray *ca;
@@ -532,7 +611,7 @@ rb_ca_refer_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
532
611
  ca_size_t bytes, offset = 0;
533
612
  int8_t i;
534
613
 
535
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
614
+ GetCArray(self, ca);
536
615
 
537
616
  if (argc == 0 && NIL_P(ropt)) {
538
617
  data_type = ca->data_type;
@@ -544,10 +623,15 @@ rb_ca_refer_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
544
623
  rb_ca_set_parent(obj, self);
545
624
  }
546
625
  else {
547
- volatile VALUE rtype, rdim, rbytes = Qnil, roffset = Qnil;
548
- rb_scan_args(argc, argv, "11", (VALUE *) &rtype, (VALUE *) &rdim);
626
+ VALUE rtype, rdim, rbytes = Qnil, roffset = Qnil;
627
+ rb_scan_args(argc, argv, "11", &rtype, &rdim);
549
628
  rb_scan_options(ropt, "bytes,offset", &rbytes, &roffset);
550
- if (NIL_P(rbytes)) rbytes = rb_ca_bytes(self);
629
+ ca_refuse_type_number(rtype);
630
+ /* A :fixlen refer without bytes: is as wide as self. */
631
+ if ( NIL_P(rbytes) && TYPE(rtype) != T_CLASS &&
632
+ rb_ca_guess_type(rtype) == CA_FIXLEN ) {
633
+ rbytes = rb_ca_bytes(self);
634
+ }
551
635
  rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
552
636
  if (NIL_P(rdim)) {
553
637
  if (ca->bytes != bytes) {
@@ -557,11 +641,25 @@ rb_ca_refer_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
557
641
  rdim = rb_ca_dim(self);
558
642
  }
559
643
  }
644
+ if ( ca_is_face(ca) && ! rb_obj_is_data_class(rtype)
645
+ && ( data_type == CA_OBJECT || ca->data_type == CA_FIXLEN ) ) {
646
+ /* A Face's cells do not mean their storage bytes, and the view below
647
+ would be lifted back to a Face reading cells of the wrong width. */
648
+ CArray *storage = ca_strip_face(ca);
649
+ if ( ! ( data_type == ca->data_type && bytes == ca->bytes )
650
+ && ! ( data_type == storage->data_type
651
+ && bytes == storage->bytes ) ) {
652
+ rb_raise(rb_eTypeError,
653
+ "%"PRIsVALUE" has no view of its values in another data_type: "
654
+ "#to_type gives the values, #parent.refer the raw storage",
655
+ rb_obj_class(self));
656
+ }
657
+ }
560
658
  Check_Type(rdim, T_ARRAY);
561
- CA_CHECK_RANK_GIVEN(RARRAY_LEN(rdim)); /* before dim[] is filled */
659
+ CA_CHECK_RANK(RARRAY_LEN(rdim)); /* before dim[] is filled */
562
660
  ndim = RARRAY_LEN(rdim);
563
- for (i = 0; i < ndim; i++) dim[i] = NUM2SIZE(rb_ary_entry(rdim, i));
564
- if (! NIL_P(roffset)) offset = NUM2SIZE(roffset);
661
+ for (i = 0; i < ndim; i++) dim[i] = ca_integer_arg(rb_ary_entry(rdim, i), "a dimension", NULL);
662
+ if (! NIL_P(roffset)) offset = ca_integer_arg(roffset, "the offset", NULL);
565
663
  /* When rtype is a data_class the result is a record array. It is
566
664
  built on the storage, never on a Face, so that a Face stays on top
567
665
  of the chain: re-reading a record array as its own data_class lifts
@@ -569,7 +667,7 @@ rb_ca_refer_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
569
667
  if ( rb_obj_is_data_class(rtype) ) {
570
668
  volatile VALUE base = rb_ca_strip_face_value(self);
571
669
  CArray *base_ca;
572
- TypedData_Get_Struct(base, CArray, &carray_data_type, base_ca);
670
+ GetCArray(base, base_ca);
573
671
  cr = ca_refer_new(base_ca, data_type, ndim, dim, bytes, offset);
574
672
  obj = ca_wrap_struct(cr);
575
673
  rb_ca_set_parent(obj, base);
@@ -603,16 +701,16 @@ rb_ca_refer_new (VALUE self,
603
701
  volatile VALUE list, rdim, ropt;
604
702
  int8_t i;
605
703
 
606
- rdim = rb_ary_new2(ndim);
704
+ rdim = rb_ary_new_capa(ndim);
607
705
  for (i = 0; i < ndim; i++) {
608
706
  rb_ary_store(rdim, i, SIZE2NUM(dim[i]));
609
707
  }
610
708
 
611
- list = rb_ary_new2(3);
709
+ list = rb_ary_new_capa(3);
612
710
  if (data_type == CA_FIXLEN && rb_ca_has_data_class(self)) {
613
711
  rb_ary_store(list, 0, rb_ca_data_class(self));
614
712
  } else {
615
- rb_ary_store(list, 0, INT2NUM(data_type));
713
+ rb_ary_store(list, 0, ID2SYM(ca_data_type_sym[data_type]));
616
714
  }
617
715
  rb_ary_store(list, 1, rdim);
618
716
  ropt = rb_hash_new();
@@ -638,7 +736,7 @@ rb_ca_refer_new (VALUE self,
638
736
 
639
737
  Faces stay lifted: rb_ca_value_array depends on that and says so
640
738
  where it marks the storage level. */
641
- TypedData_Get_Struct(obj, CArray, &carray_data_type, co);
739
+ GetCArray(obj, co);
642
740
  if ( ca_is_lazy_marker(co) ) {
643
741
  obj = rb_ca_parent(obj);
644
742
  }
@@ -666,7 +764,7 @@ rb_ca_refer_new (VALUE self,
666
764
  For typed-dim usage (bypassing the placeholder vocabulary) call
667
765
  rb_ca_refer_new(self, ca->data_type, ndim, dim, ca->bytes, 0). */
668
766
  VALUE
669
- rb_ca_reshape (int argc, VALUE *argv, VALUE self)
767
+ rb_ca_reshape (int argc, VALUE *argv, volatile VALUE self)
670
768
  {
671
769
  CArray *ca;
672
770
  CARefer *cr;
@@ -678,7 +776,8 @@ rb_ca_reshape (int argc, VALUE *argv, VALUE self)
678
776
  volatile VALUE obj;
679
777
  VALUE sym_tilde = ID2SYM(rb_intern("~")); /* :~ = infer-placeholder alias for -1 */
680
778
 
681
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
779
+ rb_reject_options(rb_pop_options(&argc, &argv));
780
+ GetCArray(self, ca);
682
781
 
683
782
  if ( argc == 0 ) {
684
783
  rb_raise(rb_eArgError, "reshape: give at least one axis");
@@ -723,7 +822,7 @@ rb_ca_reshape (int argc, VALUE *argv, VALUE self)
723
822
  new_dim[i] = ca->dim[src];
724
823
  }
725
824
  else {
726
- new_dim[i] = NUM2SIZE(argv[i]);
825
+ new_dim[i] = ca_integer_arg(argv[i], "a dimension", "reshape");
727
826
  }
728
827
  /* A product too large to count cannot equal the element count; an
729
828
  extent of zero anywhere makes it zero whatever it overflowed to. */
@@ -757,10 +856,18 @@ rb_ca_reshape (int argc, VALUE *argv, VALUE self)
757
856
  a subset), and on non-contiguous parents the compose-fold
758
857
  path corrupts data outright — hence the raise. */
759
858
  if (prod != ca->elements) {
859
+ /* Name the shape with each nil replaced by the axis it copied, so
860
+ the reader sees where the element count comes from. */
861
+ volatile VALUE shape = rb_str_new_cstr("[");
862
+ for (i = 0; i < argc; i++) {
863
+ rb_str_catf(shape, (i ? ", %lld" : "%lld"), (long long) new_dim[i]);
864
+ }
865
+ rb_str_cat_cstr(shape, "]");
760
866
  rb_raise(rb_eArgError,
761
- "reshape: cannot reshape array of %lld elements into a shape "
762
- "of %lld elements (use -1 / :~ to infer a dim)",
763
- (long long) ca->elements, (long long) prod);
867
+ "reshape: cannot reshape array of %lld elements into the shape "
868
+ "%s of %lld elements (use -1 / :~ to infer a dim)",
869
+ (long long) ca->elements, StringValueCStr(shape),
870
+ (long long) prod);
764
871
  }
765
872
  }
766
873
 
@@ -793,14 +900,14 @@ rb_ca_reshape (int argc, VALUE *argv, VALUE self)
793
900
  (a CAStride when ca_reshape_try_strides succeeds, otherwise a
794
901
  CARefer). Also exported (declared in carray.h). */
795
902
  VALUE
796
- rb_ca_flatten (VALUE self)
903
+ rb_ca_flatten (volatile VALUE self)
797
904
  {
798
905
  CArray *ca;
799
906
  CARefer *cr;
800
907
  ca_size_t dim[1];
801
908
  volatile VALUE obj;
802
909
 
803
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
910
+ GetCArray(self, ca);
804
911
  dim[0] = ca->elements;
805
912
 
806
913
  {
@@ -841,7 +948,7 @@ rb_ca_flatten (VALUE self)
841
948
  carray_median_percentile.c) can call directly; they pass a single axis,
842
949
  for which the source and output frames coincide. */
843
950
  VALUE
844
- rb_ca_insert_axis (int argc, VALUE *argv, VALUE self)
951
+ rb_ca_insert_axis (int argc, VALUE *argv, volatile VALUE self)
845
952
  {
846
953
  CArray *ca;
847
954
  VALUE new_argv[CA_RANK_MAX];
@@ -849,7 +956,8 @@ rb_ca_insert_axis (int argc, VALUE *argv, VALUE self)
849
956
  int ndim, new_ndim, ngap;
850
957
  int i, c, k;
851
958
 
852
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
959
+ rb_reject_options(rb_pop_options(&argc, &argv));
960
+ GetCArray(self, ca);
853
961
 
854
962
  ndim = (int) ca->ndim;
855
963
  new_ndim = ndim + argc;
@@ -863,7 +971,7 @@ rb_ca_insert_axis (int argc, VALUE *argv, VALUE self)
863
971
  gap_count[i] = 0;
864
972
  }
865
973
  for (i = 0; i < argc; i++) {
866
- int g = ca_normalize_axis_arg(argv[i], ngap, "insert_axis");
974
+ int g = rb_ca_normalize_axis_arg(argv[i], ngap, "insert_axis");
867
975
  gap_count[g]++;
868
976
  }
869
977
 
@@ -902,7 +1010,6 @@ Init_ca_obj_refer (void)
902
1010
  ca_refer_func.struct_size = sizeof(CARefer);
903
1011
  ca_func[CA_OBJ_REFER] = ca_refer_func;
904
1012
 
905
- rb_define_const(rb_cObject, "CA_OBJ_REFER", INT2NUM(CA_OBJ_REFER));
906
1013
 
907
1014
  rb_define_method(rb_cCArray, "refer", rb_ca_refer, -1);
908
1015
  rb_define_method(rb_cCArray, "reshape", rb_ca_reshape, -1);
data/ext/ca_obj_remap.c CHANGED
@@ -18,19 +18,9 @@
18
18
  ---------------------------------------------------------------------------- */
19
19
 
20
20
  #include "carray.h"
21
- #include "carray_internal.h"
22
21
 
23
22
  typedef struct {
24
- int16_t obj_type;
25
- int8_t data_type;
26
- int8_t ndim;
27
- int32_t flags;
28
- ca_size_t bytes;
29
- ca_size_t elements;
30
- ca_size_t *dim;
31
- char *ptr;
32
- CArray *mask;
33
- char *_pool; /* framework-managed pool buffer (NULL = legacy ALLOC_N path). */
23
+ CA_ARRAY_HEADER
34
24
  CArray *parent; /* = ref */
35
25
  uint32_t attach;
36
26
  uint8_t nosync;
@@ -38,7 +28,7 @@ typedef struct {
38
28
  CArray *idx; /* shared reference; data_type = CA_SIZE, same shape as parent */
39
29
  } CARemap;
40
30
 
41
- static int8_t CA_OBJ_REMAP;
31
+ static int16_t CA_OBJ_REMAP;
42
32
 
43
33
  /* Ruby classes — rb_define_class'd in Init so users see the right
44
34
  class name on .class inspection. No constructor is bound (no
@@ -83,7 +73,7 @@ const rb_data_type_t caremap_data_type = {
83
73
 
84
74
  const rb_data_type_t caremap_mask_data_type = {
85
75
  .parent = &caremap_data_type,
86
- .wrap_struct_name = "CARemapMask",
76
+ .wrap_struct_name = "CARemap::Mask",
87
77
  .function = {
88
78
  .dmark = NULL,
89
79
  .dfree = ca_free_nop,
@@ -476,7 +466,7 @@ ca_operation_function_t ca_remap_func = {
476
466
  keep their indices where Ruby cannot reach them; this is the same
477
467
  promise for an index array that stays visible. */
478
468
  VALUE
479
- rb_ca_remap_new (VALUE cary, VALUE rmapper)
469
+ rb_ca_remap_new (volatile VALUE cary, volatile VALUE rmapper)
480
470
  {
481
471
  volatile VALUE obj;
482
472
  CArray *ref, *idx;
@@ -484,8 +474,8 @@ rb_ca_remap_new (VALUE cary, VALUE rmapper)
484
474
 
485
475
  rb_check_carray_object(cary);
486
476
  rb_check_carray_object(rmapper);
487
- TypedData_Get_Struct(cary, CArray, &carray_data_type, ref);
488
- TypedData_Get_Struct(rmapper, CArray, &carray_data_type, idx);
477
+ GetCArray(cary, ref);
478
+ GetCArray(rmapper, idx);
489
479
 
490
480
  ca = ca_remap_new(ref, idx);
491
481
  obj = ca_wrap_struct(ca);
@@ -516,7 +506,7 @@ rb_ca_remap_mapper (VALUE self)
516
506
  Object#dup cannot copy it; these build the copy the way the indexer
517
507
  does: a new view over the same parent and the same index array. */
518
508
  static VALUE
519
- rb_ca_remap_dup (VALUE self)
509
+ rb_ca_remap_dup (volatile VALUE self)
520
510
  {
521
511
  volatile VALUE rmapper = rb_ivar_get(self, rb_intern("remap_idx"));
522
512
  if ( NIL_P(rmapper) ) {
@@ -525,7 +515,7 @@ rb_ca_remap_dup (VALUE self)
525
515
  of keeps both alive, so the copy holds that array too. */
526
516
  volatile VALUE obj;
527
517
  CArray *ca, *cp;
528
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
518
+ GetCArray(self, ca);
529
519
  cp = (CArray *) ca_remap_func_clone(ca);
530
520
  ca_keep_chain_flags(cp, ca);
531
521
  obj = TypedData_Wrap_Struct(rb_obj_class(self), &caremap_data_type, cp);
@@ -542,16 +532,8 @@ rb_ca_remap_clone (int argc, VALUE *argv, VALUE self)
542
532
  VALUE opts = Qnil, vfreeze = Qnil;
543
533
  volatile VALUE obj;
544
534
  rb_scan_args(argc, argv, "0:", &opts);
545
- if ( ! NIL_P(opts) ) {
546
- /* rb_get_kwargs refuses any key but freeze:, as Kernel#clone does. */
547
- ID kw[1];
548
- VALUE val[1];
549
- kw[0] = rb_intern("freeze");
550
- rb_get_kwargs(opts, kw, 0, 1, val);
551
- if ( val[0] != Qundef ) {
552
- vfreeze = val[0];
553
- }
554
- }
535
+ /* Any key but freeze: is refused, as Kernel#clone does. */
536
+ rb_scan_options(opts, "freeze", &vfreeze);
555
537
  obj = rb_ca_remap_dup(self);
556
538
  if ( RTEST(vfreeze) || ( NIL_P(vfreeze) && OBJ_FROZEN(self) ) ) {
557
539
  rb_obj_freeze(obj);
@@ -569,7 +551,7 @@ Init_ca_obj_remap (void)
569
551
  integer remains unexported (it's an internal type tag, not
570
552
  user-meaningful). */
571
553
  rb_cCARemap = rb_define_class("CARemap", rb_cCAView);
572
- rb_cCARemapMask = rb_define_class("CARemapMask", rb_cCARemap);
554
+ rb_cCARemapMask = rb_define_class_under(rb_cCARemap, "Mask", rb_cCARemap);
573
555
  rb_undef_alloc_func(rb_cCARemap);
574
556
  rb_undef_alloc_func(rb_cCARemapMask);
575
557
  rb_define_method(rb_cCARemap, "dup", rb_ca_remap_dup, 0);
data/ext/ca_obj_repeat.c CHANGED
@@ -10,7 +10,6 @@
10
10
  ---------------------------------------------------------------------------- */
11
11
 
12
12
  #include "carray.h"
13
- #include "carray_internal.h" /* per-obj_type view constructors */
14
13
  #include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE */
15
14
 
16
15
  extern ca_operation_function_t ca_stride_func;
@@ -109,13 +108,13 @@ ca_repeat_new (CArray *parent, int8_t ndim, ca_size_t *count)
109
108
  /* ------------------------------------------------------------------- */
110
109
 
111
110
  VALUE
112
- rb_ca_repeat_new (VALUE cary, int8_t ndim, ca_size_t *count)
111
+ rb_ca_repeat_new (volatile VALUE cary, int8_t ndim, ca_size_t *count)
113
112
  {
114
113
  volatile VALUE obj;
115
114
  CArray *parent;
116
115
  CAStride *ca;
117
116
  rb_check_carray_object(cary);
118
- TypedData_Get_Struct(cary, CArray, &carray_data_type, parent);
117
+ GetCArray(cary, parent);
119
118
  ca = ca_repeat_new(parent, ndim, count);
120
119
  obj = ca_wrap_struct(ca);
121
120
  rb_ca_set_parent(obj, cary);
@@ -138,8 +137,9 @@ rb_ca_repeat_new (VALUE cary, int8_t ndim, ca_size_t *count)
138
137
 
139
138
  Called by carray_access.c indexer dispatch when the index includes
140
139
  the :% symbol (e.g. `a[:%, nil, :%, 3]`). */
140
+ /* recursion: re-enters once, with the arguments rewritten */
141
141
  VALUE
142
- rb_ca_repeat (int argc, VALUE *argv, VALUE self)
142
+ rb_ca_repeat (int argc, VALUE *argv, volatile VALUE self)
143
143
  {
144
144
  volatile VALUE obj;
145
145
  CArray *ca;
@@ -147,7 +147,7 @@ rb_ca_repeat (int argc, VALUE *argv, VALUE self)
147
147
  ca_size_t repeat;
148
148
  ca_size_t i;
149
149
 
150
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
150
+ GetCArray(self, ca);
151
151
 
152
152
  if ( argc == 2 &&
153
153
  (
@@ -159,13 +159,18 @@ rb_ca_repeat (int argc, VALUE *argv, VALUE self)
159
159
  ca_size_t ndim, dim[CA_RANK_MAX];
160
160
  int k;
161
161
  if ( argv[0] == ID2SYM(rb_intern("%") ) ) {
162
- TypedData_Get_Struct(argv[1], CArray, &carray_data_type, ct);
162
+ /* gc: argv holds it for the call */
163
+ GetCArray(argv[1], ct);
163
164
  }
164
165
  else {
165
- TypedData_Get_Struct(argv[0], CArray, &carray_data_type, ct);
166
+ /* gc: argv holds it for the call */
167
+ GetCArray(argv[0], ct);
166
168
  }
167
169
  if ( ct->ndim < ca->ndim ) {
168
- rb_raise(rb_eArgError, "invalid ndim to template");
170
+ rb_raise(rb_eArgError,
171
+ "%s: a template of %d axes for an array of %d axes; the "
172
+ "template beside :%% needs at least the array's axes",
173
+ ca_calling_method_name(), (int) ct->ndim, (int) ca->ndim);
169
174
  }
170
175
  args = rb_ary_new();
171
176
  ndim = 0;
@@ -216,29 +221,43 @@ rb_ca_repeat (int argc, VALUE *argv, VALUE self)
216
221
  }
217
222
  }
218
223
  self = rb_ca_repeat((int)RARRAY_LEN(args), (VALUE *)RARRAY_CONST_PTR(args), self);
219
- RB_GC_GUARD(args); /* the repeat reads its arguments out of args */
220
224
  return self;
221
225
  }
222
226
 
223
227
  ca_check_rank_fits(argc, NULL); /* count[] and dim[] hold CA_RANK_MAX */
224
228
 
225
229
  repeat = 1;
226
- for (i=0; i<argc; i++) {
227
- if ( rb_obj_is_kind_of(argv[i], rb_cSymbol) ) {
228
- if ( argv[i] == ID2SYM(rb_intern("%")) ) {
229
- count[i] = 0;
230
+ {
231
+ int ndata = 0;
232
+ for (i=0; i<argc; i++) {
233
+ if ( rb_obj_is_kind_of(argv[i], rb_cSymbol) ) {
234
+ if ( argv[i] == ID2SYM(rb_intern("%")) ) {
235
+ count[i] = 0;
236
+ ndata++;
237
+ }
238
+ else {
239
+ rb_raise(rb_eArgError,
240
+ "%s: symbol :%"PRIsVALUE" is not a repeat count (:%% "
241
+ "stands for an axis of the array, an Integer repeats)",
242
+ ca_calling_method_name(), rb_sym2str(argv[i]));
243
+ }
230
244
  }
231
245
  else {
232
- rb_raise(rb_eArgError, "unknown symbol (!= ':%%') in arguments");
246
+ count[i] = ca_integer_arg(argv[i], "a repeat count",
247
+ ca_calling_method_name());
248
+ if ( count[i] <= 0 ) {
249
+ rb_raise(rb_eArgError,
250
+ "%s: a repeat count must be 1 or more (got %"PRId64")",
251
+ ca_calling_method_name(), (int64_t) count[i]);
252
+ }
253
+ repeat *= count[i];
233
254
  }
234
255
  }
235
- else {
236
- count[i] = NUM2SIZE(argv[i]);
237
- if ( count[i] == 0 ) {
238
- rb_raise(rb_eArgError,
239
- "zero repeat count specified in creating CARepeat object");
240
- }
241
- repeat *= count[i];
256
+ if ( ndata != ca->ndim ) {
257
+ rb_raise(rb_eArgError,
258
+ "%s: %d :%% given for an array of %d axes (one :%% stands "
259
+ "for each axis of the array)",
260
+ ca_calling_method_name(), ndata, (int) ca->ndim);
242
261
  }
243
262
  }
244
263
 
@@ -265,7 +284,7 @@ rb_ca_repeat (int argc, VALUE *argv, VALUE self)
265
284
  }
266
285
 
267
286
  static VALUE
268
- rb_ca_repeat_initialize_copy (VALUE self, VALUE other)
287
+ rb_ca_repeat_initialize_copy (volatile VALUE self, volatile VALUE other)
269
288
  {
270
289
  CAStride *ca, *cs;
271
290
  TypedData_Get_Struct(self, CAStride, &castride_data_type, ca);
@@ -287,7 +306,6 @@ Init_ca_obj_repeat (void)
287
306
  {
288
307
  /* rb_cCARepeat, CA_OBJ_REPEAT are defined in ruby_carray.c / carray_core.c */
289
308
 
290
- rb_define_const(rb_cObject, "CA_OBJ_REPEAT", INT2NUM(CA_OBJ_REPEAT));
291
309
 
292
310
  rb_define_method(rb_cCARepeat, "initialize_copy",
293
311
  rb_ca_repeat_initialize_copy, 1);