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_face.c CHANGED
@@ -30,7 +30,6 @@
30
30
  #include "ruby.h"
31
31
  #include "carray.h"
32
32
  #include "ca_obj_face.h"
33
- #include "carray_internal.h"
34
33
 
35
34
  /* ---- shared op helpers (thin-forward) ---- */
36
35
 
@@ -39,7 +38,7 @@ ca_face_attach (void *ap)
39
38
  {
40
39
  CAView *cav = (CAView *) ap;
41
40
  CArray *ca = (CArray *) ap;
42
- ca_attach(cav->parent); /* window: a Face's attach slot; its detach slot releases */
41
+ ca_attach(cav->parent); /* attach: a Face's attach slot; its detach slot releases */
43
42
  /* alias parent->ptr (Face has identical data layout, so copy is unneeded) */
44
43
  ca->ptr = cav->parent->ptr;
45
44
  }
@@ -122,7 +121,7 @@ ca_face_xfer_all (void *ap, void *data, int dir)
122
121
  *
123
122
  * Called from ca_face_lift and from subclass lift / clone / dup paths. */
124
123
  VALUE
125
- rb_ca_face_template (VALUE original_face, CArray *new_parent,
124
+ rb_ca_face_template (volatile VALUE original_face, CArray *new_parent,
126
125
  ca_size_t *new_dim)
127
126
  {
128
127
  CArray *src;
@@ -131,7 +130,7 @@ rb_ca_face_template (VALUE original_face, CArray *new_parent,
131
130
  size_t size;
132
131
  const rb_data_type_t *tdata;
133
132
 
134
- TypedData_Get_Struct(original_face, CArray, &carray_data_type, src);
133
+ GetCArray(original_face, src);
135
134
  tdata = ca_typeddata[src->obj_type];
136
135
 
137
136
  /* The struct with its subclass tail. struct_size is the struct alone;
@@ -187,22 +186,37 @@ rb_ca_face_template (VALUE original_face, CArray *new_parent,
187
186
  dst);
188
187
  }
189
188
 
189
+ /* A Face put on a fresh entity has nothing to be read-only for: the template
190
+ copied the flag of the Face it came from, which may be read-only only as
191
+ that view was. A Face that cannot hold a new value keeps it. */
192
+ static void
193
+ ca_face_fresh_entity_is_writable (volatile VALUE lifted, VALUE face)
194
+ {
195
+ CArray *lc;
196
+ GetCArray(lifted, lc);
197
+ if ( ca_test_flag(lc, CA_FLAG_READ_ONLY)
198
+ && RTEST(rb_funcall(face, rb_intern("holds_new_values?"), 0)) ) {
199
+ ca_unset_flag(lc, CA_FLAG_READ_ONLY);
200
+ }
201
+ }
202
+
190
203
  VALUE
191
- ca_face_relift_entity (VALUE face, VALUE result)
204
+ ca_face_relift_entity (VALUE face, volatile VALUE result)
192
205
  {
193
206
  static ID id_copy_state = 0;
194
207
  CArray *rc, *lc;
195
- VALUE lifted;
208
+ volatile VALUE lifted;
196
209
  if ( id_copy_state == 0 ) id_copy_state = rb_intern("copy_state");
197
- TypedData_Get_Struct(result, CArray, &carray_data_type, rc);
210
+ GetCArray(result, rc);
198
211
  lifted = rb_ca_face_template(face, rc, rc->dim);
199
212
  rb_ca_set_parent(lifted, result);
213
+ ca_face_fresh_entity_is_writable(lifted, face);
200
214
  if ( ca_has_mask(rc) ) {
201
215
  /* The template starts without a mask. */
202
- TypedData_Get_Struct(lifted, CArray, &carray_data_type, lc);
216
+ GetCArray(lifted, lc);
203
217
  ca_copy_mask(lc, rc);
204
218
  }
205
- if ( rb_respond_to(face, id_copy_state) ) {
219
+ if ( rb_obj_respond_to(face, id_copy_state, Qtrue) ) {
206
220
  rb_funcall(lifted, id_copy_state, 1, face);
207
221
  }
208
222
  return lifted;
@@ -233,14 +247,14 @@ ca_lift_move_mask_down (CArray *view_ca, CArray *wp)
233
247
  * Caller (rb_ca_aref / rb_ca_store) must have already checked that
234
248
  * face_parent carries the CA_FLAG_IS_FACE flag. */
235
249
  VALUE
236
- ca_face_lift (VALUE view, VALUE face_parent)
250
+ ca_face_lift (volatile VALUE view, volatile VALUE face_parent)
237
251
  {
238
252
  CArray *view_ca;
239
- VALUE lifted;
253
+ volatile VALUE lifted;
240
254
  static ID id_copy_state = 0;
241
255
  if ( id_copy_state == 0 ) id_copy_state = rb_intern("copy_state");
242
256
 
243
- TypedData_Get_Struct(view, CArray, &carray_data_type, view_ca);
257
+ GetCArray(view, view_ca);
244
258
 
245
259
  /* Idempotent lift: if the view is already a Face of the same class as
246
260
  face_parent, it has already been lifted — a builder that lifts internally
@@ -262,7 +276,7 @@ ca_face_lift (VALUE view, VALUE face_parent)
262
276
  presents the surface. */
263
277
  {
264
278
  CArray *fp, *p;
265
- TypedData_Get_Struct(face_parent, CArray, &carray_data_type, fp);
279
+ GetCArray(face_parent, fp);
266
280
  p = fp;
267
281
  while (p && ca_is_face(p)) {
268
282
  p = ((CAView *) p)->parent;
@@ -304,18 +318,21 @@ ca_face_lift (VALUE view, VALUE face_parent)
304
318
  if ( ca_is_view(view_ca) ) {
305
319
  rb_ca_set_lifted_from(lifted, face_parent);
306
320
  }
321
+ else {
322
+ ca_face_fresh_entity_is_writable(lifted, face_parent);
323
+ }
307
324
 
308
325
  /* Optional Ruby callback for ivar / state carry. Faces holding
309
326
  semantic state in the struct tail (CATime / CATimedelta
310
327
  etc.) auto-carry via the template memcpy; Faces holding state in
311
328
  Ruby ivars (Ruby-implemented CAObject-derived Faces etc.) should
312
329
  override copy_state to copy them. */
313
- if ( rb_respond_to(face_parent, id_copy_state) ) {
330
+ if ( rb_obj_respond_to(face_parent, id_copy_state, Qtrue) ) {
314
331
  rb_funcall(lifted, id_copy_state, 1, face_parent);
315
332
  }
316
333
 
317
334
  #ifdef CARRAY_DEV_BUILD
318
- /* Soundness tripwire (docs/internal/FaceLiftSoundness.md §0/§7): after a lift the view
335
+ /* Soundness check (docs/internal/FaceLiftSoundness.md §0/§7): after a lift the view
319
336
  node under the new Face must not point back at the Face we lifted from —
320
337
  the §8.3 one-level swap must have moved it exactly one level, so a
321
338
  redundant middle Face is structurally caught here, wherever it enters.
@@ -325,9 +342,9 @@ ca_face_lift (VALUE view, VALUE face_parent)
325
342
  ->parent (slot 0) is storage; this checks that slot. */
326
343
  {
327
344
  CArray *lifted_ca, *lp, *fps;
328
- TypedData_Get_Struct(lifted, CArray, &carray_data_type, lifted_ca);
345
+ GetCArray(lifted, lifted_ca);
329
346
  lp = CAVIEW(lifted_ca)->parent;
330
- TypedData_Get_Struct(face_parent, CArray, &carray_data_type, fps);
347
+ GetCArray(face_parent, fps);
331
348
  if ( lp && ca_is_view(lp) ) {
332
349
  if ( CAVIEW(lp)->parent == fps ) {
333
350
  rb_bug("ca_face_lift: §8.3 one-level swap did not fire — redundant "
@@ -358,12 +375,12 @@ ca_face_lift (VALUE view, VALUE face_parent)
358
375
  * builder handed us a view whose parent is the marker, and leaving it
359
376
  * there would stack a redundant marker in the middle. */
360
377
  static VALUE
361
- ca_lazy_marker_lift (VALUE view, VALUE marker)
378
+ ca_lazy_marker_lift (volatile VALUE view, volatile VALUE marker)
362
379
  {
363
380
  CArray *view_ca, *mp;
364
381
  extern VALUE rb_ca_lazy_marker_new (VALUE cary);
365
382
 
366
- TypedData_Get_Struct(view, CArray, &carray_data_type, view_ca);
383
+ GetCArray(view, view_ca);
367
384
 
368
385
  /* Idempotent: a builder that wrapped internally already returned a
369
386
  marker (mirrors the same guard in ca_face_lift). */
@@ -371,7 +388,7 @@ ca_lazy_marker_lift (VALUE view, VALUE marker)
371
388
  return view;
372
389
  }
373
390
 
374
- TypedData_Get_Struct(marker, CArray, &carray_data_type, mp);
391
+ GetCArray(marker, mp);
375
392
 
376
393
  /* One-level swap (CAFace.md section 8.3, same reasoning): move the view
377
394
  off the marker and onto what the marker wraps. Guarded on pointer
@@ -391,13 +408,13 @@ ca_lazy_marker_lift (VALUE view, VALUE marker)
391
408
  * CA_WRAPPER_LIFT. The macro has already established that wrapper_ca
392
409
  * carries one of the two flags and that view is a CArray. */
393
410
  VALUE
394
- ca_wrapper_lift (VALUE view, VALUE wrapper, void *wrapper_ca)
411
+ ca_wrapper_lift (volatile VALUE view, VALUE wrapper, void *wrapper_ca)
395
412
  {
396
413
  CArray *ca = (CArray *) wrapper_ca;
397
414
 
398
415
  if ( ca_is_face(ca) ) {
399
416
  CArray *view_ca;
400
- TypedData_Get_Struct(view, CArray, &carray_data_type, view_ca);
417
+ GetCArray(view, view_ca);
401
418
  /* Some builders (select / repeat / ...) lift their own result; a
402
419
  second lift here would double-wrap. Mirrors the guard the `[]`
403
420
  call site used to carry inline. */
@@ -433,14 +450,14 @@ ca_strip_face (CArray *src)
433
450
  * result into a Face wrapper (index / addr / linear outputs are not
434
451
  * Face-semantic). Complements ca_strip_face on the C-struct axis. */
435
452
  VALUE
436
- rb_ca_strip_face_value (VALUE v)
453
+ rb_ca_strip_face_value (volatile VALUE v)
437
454
  {
438
455
  CArray *ca;
439
456
  if ( ! rb_obj_is_kind_of(v, rb_cCArray) ) return v;
440
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
457
+ GetCArray(v, ca);
441
458
  while ( ca_is_face(ca) ) {
442
459
  v = rb_ca_parent(v);
443
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
460
+ GetCArray(v, ca);
444
461
  }
445
462
  return v;
446
463
  }
@@ -480,7 +497,7 @@ rb_ca_face_s_allocate_forbidden (VALUE klass)
480
497
  }
481
498
 
482
499
  VALUE
483
- ca_face_storage_value (CArray *ca, VALUE raw)
500
+ ca_face_storage_value (CArray *ca, volatile VALUE raw)
484
501
  {
485
502
  CArray *storage;
486
503
  CArray as_storage;
@@ -510,7 +527,7 @@ ca_face_storage_value (CArray *ca, VALUE raw)
510
527
  ca_face_storage_to_element_fn ca_face_storage_to_element_table[CA_OBJ_TYPE_MAX];
511
528
 
512
529
  void
513
- ca_face_register_storage_to_element (int obj_type, ca_face_storage_to_element_fn fn)
530
+ ca_face_register_storage_to_element (int16_t obj_type, ca_face_storage_to_element_fn fn)
514
531
  {
515
532
  if ( obj_type < 0 || obj_type >= CA_OBJ_TYPE_MAX ) {
516
533
  rb_raise(rb_eArgError,
@@ -527,7 +544,7 @@ ca_face_register_storage_to_element (int obj_type, ca_face_storage_to_element_fn
527
544
  ca_face_element_to_storage_fn ca_face_element_to_storage_table[CA_OBJ_TYPE_MAX];
528
545
 
529
546
  void
530
- ca_face_register_element_to_storage (int obj_type, ca_face_element_to_storage_fn fn)
547
+ ca_face_register_element_to_storage (int16_t obj_type, ca_face_element_to_storage_fn fn)
531
548
  {
532
549
  if ( obj_type < 0 || obj_type >= CA_OBJ_TYPE_MAX ) {
533
550
  rb_raise(rb_eArgError,
@@ -542,7 +559,7 @@ ca_face_register_element_to_storage (int obj_type, ca_face_element_to_storage_fn
542
559
  ca_face_state_compatible_fn ca_face_state_compatible_table[CA_OBJ_TYPE_MAX];
543
560
 
544
561
  void
545
- ca_face_register_state_compatible (int obj_type, ca_face_state_compatible_fn fn)
562
+ ca_face_register_state_compatible (int16_t obj_type, ca_face_state_compatible_fn fn)
546
563
  {
547
564
  if ( obj_type < 0 || obj_type >= CA_OBJ_TYPE_MAX ) {
548
565
  rb_raise(rb_eArgError,
@@ -583,7 +600,7 @@ ca_face_state_compatible (VALUE va, CArray *a, VALUE vb, CArray *b)
583
600
 
584
601
  /* Ruby-side: CArray#face_state_compatible?(other) */
585
602
  static VALUE
586
- rb_ca_face_state_compatible_p (VALUE self, VALUE other)
603
+ rb_ca_face_state_compatible_p (volatile VALUE self, volatile VALUE other)
587
604
  {
588
605
  CArray *a, *b;
589
606
 
@@ -591,8 +608,8 @@ rb_ca_face_state_compatible_p (VALUE self, VALUE other)
591
608
  rb_raise(rb_eArgError,
592
609
  "face_state_compatible?: argument must be a CArray");
593
610
  }
594
- TypedData_Get_Struct(self, CArray, &carray_data_type, a);
595
- TypedData_Get_Struct(other, CArray, &carray_data_type, b);
611
+ GetCArray(self, a);
612
+ GetCArray(other, b);
596
613
  if ( ! ca_is_face(a) || ! ca_is_face(b) ) {
597
614
  rb_raise(rb_eArgError,
598
615
  "face_state_compatible?: both receiver and argument must be Face");
@@ -613,7 +630,7 @@ rb_ca_face_state_compatible_p (VALUE self, VALUE other)
613
630
  static int8_t ca_face_memory_view_table[CA_OBJ_TYPE_MAX];
614
631
 
615
632
  void
616
- ca_face_register_memory_view (int obj_type, int exportable)
633
+ ca_face_register_memory_view (int16_t obj_type, int exportable)
617
634
  {
618
635
  if ( obj_type < 0 || obj_type >= CA_OBJ_TYPE_MAX ) {
619
636
  rb_raise(rb_eArgError,
@@ -653,7 +670,7 @@ rb_ca_s_face_state_portable_false (VALUE klass)
653
670
  }
654
671
 
655
672
  void
656
- ca_face_register_state_portable (int obj_type, int portable)
673
+ ca_face_register_state_portable (int16_t obj_type, int portable)
657
674
  {
658
675
  if ( obj_type < 0 || obj_type >= CA_OBJ_TYPE_MAX ) {
659
676
  rb_raise(rb_eArgError,
@@ -667,7 +684,7 @@ ca_face_register_state_portable (int obj_type, int portable)
667
684
  ca_install_obj_type before this call — Faces register from
668
685
  within their Init_*, which runs after ca_install_obj_type. */
669
686
  if ( !NIL_P(ca_class[obj_type]) ) {
670
- rb_define_singleton_method(ca_class[obj_type], "face_state_portable?",
687
+ rb_define_private_method(rb_singleton_class(ca_class[obj_type]), "face_state_portable?",
671
688
  portable ? rb_ca_s_face_state_portable_true
672
689
  : rb_ca_s_face_state_portable_false,
673
690
  0);
@@ -686,7 +703,7 @@ ca_face_class_has_portable_method (VALUE klass)
686
703
  ID id_owner = rb_intern("owner");
687
704
  VALUE m, owner;
688
705
  if ( NIL_P(klass) ) return 0;
689
- if ( !rb_respond_to(klass, id_face_state_portable_p) ) return 0;
706
+ if ( !rb_obj_respond_to(klass, id_face_state_portable_p, Qtrue) ) return 0;
690
707
  m = rb_funcall(klass, id_method, 1, ID2SYM(id_face_state_portable_p));
691
708
  owner = rb_funcall(m, id_owner, 0);
692
709
  /* owner is a (singleton) class — if it isn't rb_singleton_class(rb_cCArray)
@@ -741,16 +758,16 @@ ca_face_refuse_order (const char *name, VALUE face, const char *verb)
741
758
  const char *storage = ca_face_storage_method(face);
742
759
  if ( storage ) {
743
760
  rb_raise(rb_eArgError,
744
- "%s: %s is not orderable by storage (a cell encodes the value "
761
+ "%s: %"PRIsVALUE" is not orderable by storage (a cell encodes the value "
745
762
  "rather than being it); %s its values instead -- %s -- or %s "
746
763
  "its storage, which #%s gives",
747
- name, rb_obj_classname(face), verb,
764
+ name, rb_obj_class(face), verb,
748
765
  ca_face_conversion_hint(face), verb, storage);
749
766
  }
750
767
  rb_raise(rb_eArgError,
751
- "%s: %s is not orderable by storage (a cell encodes the value "
768
+ "%s: %"PRIsVALUE" is not orderable by storage (a cell encodes the value "
752
769
  "rather than being it); %s its values instead -- %s",
753
- name, rb_obj_classname(face), verb,
770
+ name, rb_obj_class(face), verb,
754
771
  ca_face_conversion_hint(face));
755
772
  }
756
773
 
@@ -761,28 +778,29 @@ ca_face_refuse_order (const char *name, VALUE face, const char *verb)
761
778
  when the Face is ORDERABLE; refuses otherwise (ca_face_refuse_order). A
762
779
  family that can order by a key asks ca_face_orders_by_itself first. */
763
780
  void
764
- ca_face_require_orderable (VALUE face, const char *name, const char *verb)
781
+ ca_face_require_orderable (volatile VALUE face, const char *name, const char *verb)
765
782
  {
766
783
  CArray *ca;
767
- TypedData_Get_Struct(face, CArray, &carray_data_type, ca);
784
+ GetCArray(face, ca);
768
785
  if ( ! ca_test_flag(ca, CA_FLAG_FACE_ORDERABLE_STORAGE) ) {
769
786
  ca_face_refuse_order(name, face, verb);
770
787
  }
771
788
  }
772
789
 
773
- /* Whether a Face is ordered by its own #order rather than by its storage:
774
- it is not ORDERABLE and answers #order itself (CAConstString, CARecord),
775
- so a family that orders by a key can take that order. CArray#order is
776
- not one: it sorts the storage and comes back to the gate. */
790
+ /* Whether a Face is ordered by its own #rank_index rather than by its
791
+ storage: it is not ORDERABLE and answers #rank_index itself
792
+ (CAConstString, CARecord), so a family that orders by a key can take that
793
+ order. CArray#rank_index is not one: it sorts the storage and comes back
794
+ to the gate. */
777
795
  int
778
- ca_face_orders_by_itself (VALUE face)
796
+ ca_face_orders_by_itself (volatile VALUE face)
779
797
  {
780
- ID id_order = rb_intern("order");
798
+ ID id_order = rb_intern("rank_index");
781
799
  ID id_method = rb_intern("method");
782
800
  ID id_owner = rb_intern("owner");
783
801
  CArray *ca;
784
802
  VALUE m;
785
- TypedData_Get_Struct(face, CArray, &carray_data_type, ca);
803
+ GetCArray(face, ca);
786
804
  if ( ca_test_flag(ca, CA_FLAG_FACE_ORDERABLE_STORAGE) ) {
787
805
  return 0;
788
806
  }
@@ -794,7 +812,7 @@ ca_face_orders_by_itself (VALUE face)
794
812
  whose cells encode its values rather than being numbers, pointing at what
795
813
  gives numbers. Returns when `self` is not a Face. */
796
814
  void
797
- ca_face_refuse_numeric (VALUE self, const char *name, VALUE eclass)
815
+ ca_face_refuse_numeric (volatile VALUE self, const char *name, VALUE eclass)
798
816
  {
799
817
  CArray *ca;
800
818
  if ( ! rb_obj_is_carray(self) ) {
@@ -808,27 +826,27 @@ ca_face_refuse_numeric (VALUE self, const char *name, VALUE eclass)
808
826
  const char *storage = ca_face_storage_method(self);
809
827
  if ( storage ) {
810
828
  rb_raise(eclass,
811
- "%s: %s does not define it (a cell encodes the value rather "
829
+ "%s: %"PRIsVALUE" does not define it (a cell encodes the value rather "
812
830
  "than being a number); compute on its values instead -- %s -- "
813
831
  "or on its storage, which #%s gives",
814
- name, rb_obj_classname(self), ca_face_conversion_hint(self),
832
+ name, rb_obj_class(self), ca_face_conversion_hint(self),
815
833
  storage);
816
834
  }
817
835
  }
818
836
  rb_raise(eclass,
819
- "%s: %s does not define it (a cell encodes the value rather than "
837
+ "%s: %"PRIsVALUE" does not define it (a cell encodes the value rather than "
820
838
  "being a number); compute on its values instead -- %s",
821
- name, rb_obj_classname(self), ca_face_conversion_hint(self));
839
+ name, rb_obj_class(self), ca_face_conversion_hint(self));
822
840
  }
823
841
 
824
842
  VALUE
825
- ca_face_operand_descend (VALUE operand, const char *name)
843
+ ca_face_operand_descend (volatile VALUE operand, const char *name)
826
844
  {
827
845
  CArray *op;
828
846
  if ( ! RTEST(rb_obj_is_carray(operand)) ) {
829
847
  return operand;
830
848
  }
831
- TypedData_Get_Struct(operand, CArray, &carray_data_type, op);
849
+ GetCArray(operand, op);
832
850
  if ( ! ca_is_face(op) ) {
833
851
  return operand;
834
852
  }
@@ -839,21 +857,21 @@ ca_face_operand_descend (VALUE operand, const char *name)
839
857
  const char *storage = ca_face_storage_method(operand);
840
858
  if ( storage ) {
841
859
  rb_raise(rb_eArgError,
842
- "%s: cannot take a %s operand: its storage is not its surface "
860
+ "%s: cannot take a %"PRIsVALUE" operand: its storage is not its surface "
843
861
  "(a cell encodes the value rather than being it), so comparing "
844
862
  "the storage would compare the encoding. Convert the operand "
845
863
  "to the receiver's space first -- %s -- or compare the storage "
846
864
  "of both sides, which #%s gives",
847
- name, rb_obj_classname(operand),
865
+ name, rb_obj_class(operand),
848
866
  ca_face_conversion_hint(operand), storage);
849
867
  }
850
868
  }
851
869
  rb_raise(rb_eArgError,
852
- "%s: cannot take a %s operand: its storage is not its surface "
870
+ "%s: cannot take a %"PRIsVALUE" operand: its storage is not its surface "
853
871
  "(a cell encodes the value rather than being it), so comparing "
854
872
  "the storage would compare the encoding. Convert the operand to "
855
873
  "the receiver's space first -- %s",
856
- name, rb_obj_classname(operand), ca_face_conversion_hint(operand));
874
+ name, rb_obj_class(operand), ca_face_conversion_hint(operand));
857
875
  return Qnil; /* not reached */
858
876
  }
859
877
 
@@ -862,18 +880,19 @@ ca_face_operand_descend (VALUE operand, const char *name)
862
880
 
863
881
  A Face equals only a Face of its own class. When the two share their
864
882
  state (a time array's unit) and the storage holds the values in their
865
- order, equal storage is equal values, so the storage is compared.
866
- Otherwise `==` compares the values themselves (two time arrays of
867
- different units, two const strings over different pools, two
868
- categoricals with differently ordered labels), and `eql?`, which has to
869
- agree with #hash, answers false. */
883
+ order, cell for cell at one width, equal storage is equal values, so the
884
+ storage is compared. Otherwise `==` compares the values themselves (two
885
+ time arrays of different units, two const strings over different pools,
886
+ two categoricals with differently ordered labels, two fixed-length
887
+ strings of different widths), and `eql?`, which has to agree with #hash,
888
+ answers false. */
870
889
  int
871
- ca_face_equal (VALUE va, VALUE vb, int strict)
890
+ ca_face_equal (volatile VALUE va, volatile VALUE vb, int strict)
872
891
  {
873
892
  CArray *a, *b;
874
893
  int8_t i;
875
- TypedData_Get_Struct(va, CArray, &carray_data_type, a);
876
- TypedData_Get_Struct(vb, CArray, &carray_data_type, b);
894
+ GetCArray(va, a);
895
+ GetCArray(vb, b);
877
896
  if ( ! ca_is_face(a) || ! ca_is_face(b) ) {
878
897
  return 0;
879
898
  }
@@ -889,7 +908,8 @@ ca_face_equal (VALUE va, VALUE vb, int strict)
889
908
  }
890
909
  }
891
910
  if ( ca_face_state_compatible(va, a, vb, b)
892
- && ( strict || ca_test_flag(a, CA_FLAG_FACE_ORDERABLE_STORAGE) ) ) {
911
+ && ( strict || ( ca_test_flag(a, CA_FLAG_FACE_ORDERABLE_STORAGE)
912
+ && a->bytes == b->bytes ) ) ) {
893
913
  return strict ? ca_eql_storage(ca_strip_face(a), ca_strip_face(b))
894
914
  : ca_equal(ca_strip_face(a), ca_strip_face(b));
895
915
  }
@@ -905,13 +925,13 @@ ca_face_equal (VALUE va, VALUE vb, int strict)
905
925
  (#to_string) or, for any other, its values (to_type(:object)). Any
906
926
  other operand is returned as it is. */
907
927
  static VALUE
908
- ca_face_operand_values (VALUE operand)
928
+ ca_face_operand_values (volatile VALUE operand)
909
929
  {
910
930
  CArray *op;
911
931
  if ( ! RTEST(rb_obj_is_carray(operand)) ) {
912
932
  return operand;
913
933
  }
914
- TypedData_Get_Struct(operand, CArray, &carray_data_type, op);
934
+ GetCArray(operand, op);
915
935
  if ( ! ca_is_face(op) || op->obj_type == CA_OBJ_RECORD
916
936
  || ca_test_flag(op, CA_FLAG_FACE_ORDERABLE_STORAGE) ) {
917
937
  return operand;
@@ -919,7 +939,8 @@ ca_face_operand_values (VALUE operand)
919
939
  if ( rb_respond_to(operand, rb_intern("to_string")) ) {
920
940
  return rb_funcall(operand, rb_intern("to_string"), 0);
921
941
  }
922
- return rb_funcall(operand, rb_intern("to_type"), 1, INT2NUM(CA_OBJECT));
942
+ /* coerce: a fixed type, the operand's values as objects */
943
+ return rb_ca_to_type(operand, ID2SYM(ca_data_type_sym[CA_OBJECT]), Qnil);
923
944
  }
924
945
 
925
946
  /* Bring `operand` into the storage space of `reference`, a Face, so the
@@ -945,22 +966,22 @@ ca_face_operand_values (VALUE operand)
945
966
  UNDEF is undefined in every space, so it passes as it is and the entry
946
967
  answers it as it does for a plain array. */
947
968
  VALUE
948
- ca_face_reconcile_operand (VALUE reference, VALUE operand, const char *name)
969
+ ca_face_reconcile_operand (volatile VALUE reference, volatile VALUE operand, const char *name)
949
970
  {
950
971
  CArray *ca;
951
972
  if ( operand == CA_UNDEF ) {
952
973
  return operand;
953
974
  }
954
- TypedData_Get_Struct(reference, CArray, &carray_data_type, ca);
975
+ GetCArray(reference, ca);
955
976
  if ( ca->obj_type == CA_OBJ_RECORD ) {
956
977
  if ( RTEST(rb_obj_is_carray(operand)) ) {
957
978
  CArray *op;
958
- TypedData_Get_Struct(operand, CArray, &carray_data_type, op);
979
+ GetCArray(operand, op);
959
980
  if ( ca_is_face(op) ) {
960
981
  if ( op->obj_type != CA_OBJ_RECORD ) {
961
982
  rb_raise(rb_eTypeError,
962
- "%s: can not take a %s operand for a record array of "
963
- "%"PRIsVALUE, name, rb_obj_classname(operand),
983
+ "%s: can not take a %"PRIsVALUE" operand for a record array of "
984
+ "%"PRIsVALUE, name, rb_obj_class(operand),
964
985
  rb_ca_data_class(reference));
965
986
  }
966
987
  if ( rb_ca_data_class(operand) != rb_ca_data_class(reference) ) {
@@ -978,7 +999,7 @@ ca_face_reconcile_operand (VALUE reference, VALUE operand, const char *name)
978
999
  return ca_face_operand_descend(ca_face_operand_values(operand), name);
979
1000
  }
980
1001
  ca_face_require_orderable(reference, name, "compare");
981
- if ( rb_respond_to(reference, rb_intern("to_comparable")) ) {
1002
+ if ( rb_obj_respond_to(reference, rb_intern("to_comparable"), Qtrue) ) {
982
1003
  return rb_ca_strip_face_value(rb_funcall(reference,
983
1004
  rb_intern("to_comparable"),
984
1005
  1, operand));
@@ -987,16 +1008,16 @@ ca_face_reconcile_operand (VALUE reference, VALUE operand, const char *name)
987
1008
  const char *storage = ca_face_storage_method(reference);
988
1009
  if ( storage ) {
989
1010
  rb_raise(rb_eArgError,
990
- "%s: %s is not comparable by storage and has no to_comparable "
1011
+ "%s: %"PRIsVALUE" is not comparable by storage and has no to_comparable "
991
1012
  "to bring the operand into its space; compare the storage of "
992
1013
  "both sides, which #%s gives",
993
- name, rb_obj_classname(reference), storage);
1014
+ name, rb_obj_class(reference), storage);
994
1015
  }
995
1016
  }
996
1017
  rb_raise(rb_eArgError,
997
- "%s: %s is not comparable by storage and has no to_comparable "
1018
+ "%s: %"PRIsVALUE" is not comparable by storage and has no to_comparable "
998
1019
  "to bring the operand into its space",
999
- name, rb_obj_classname(reference));
1020
+ name, rb_obj_class(reference));
1000
1021
  return Qnil; /* not reached */
1001
1022
  }
1002
1023
 
@@ -1013,17 +1034,18 @@ ca_face_reconcile_pair (volatile VALUE *pref, volatile VALUE *poperand,
1013
1034
  const char *name)
1014
1035
  {
1015
1036
  static ID id_comparable_pair = 0;
1016
- VALUE ref = *pref, operand = *poperand;
1037
+ volatile VALUE ref = *pref;
1038
+ VALUE operand = *poperand;
1017
1039
  CArray *ca;
1018
1040
  if ( id_comparable_pair == 0 ) {
1019
1041
  id_comparable_pair = rb_intern("comparable_pair");
1020
1042
  }
1021
- TypedData_Get_Struct(ref, CArray, &carray_data_type, ca);
1043
+ GetCArray(ref, ca);
1022
1044
  if ( operand != CA_UNDEF
1023
1045
  && ca->obj_type != CA_OBJ_RECORD
1024
1046
  && ! ca_test_flag(ca, CA_FLAG_FACE_COMPARABLE_STORAGE)
1025
1047
  && ca_test_flag(ca, CA_FLAG_FACE_ORDERABLE_STORAGE)
1026
- && rb_respond_to(ref, id_comparable_pair) ) {
1048
+ && rb_obj_respond_to(ref, id_comparable_pair, Qtrue) ) {
1027
1049
  VALUE pair = rb_funcall(ref, id_comparable_pair, 1, operand);
1028
1050
  *pref = rb_ary_entry(pair, 0);
1029
1051
  *poperand = rb_ca_strip_face_value(rb_ary_entry(pair, 1));
@@ -1033,7 +1055,7 @@ ca_face_reconcile_pair (volatile VALUE *pref, volatile VALUE *poperand,
1033
1055
  }
1034
1056
 
1035
1057
  int
1036
- ca_face_state_portable (int obj_type, VALUE klass)
1058
+ ca_face_state_portable (int16_t obj_type, VALUE klass)
1037
1059
  {
1038
1060
  int8_t entry;
1039
1061
  if ( obj_type < 0 || obj_type >= CA_OBJ_TYPE_MAX ) return 1;
@@ -1057,10 +1079,10 @@ ca_face_list_class (VALUE list, const char *who)
1057
1079
  CArray *ref = NULL;
1058
1080
 
1059
1081
  for ( i = 0; i < n; i++ ) {
1060
- VALUE p = rb_ary_entry(list, i);
1082
+ volatile VALUE p = rb_ary_entry(list, i);
1061
1083
  CArray *ca;
1062
1084
  rb_check_carray_object(p);
1063
- TypedData_Get_Struct(p, CArray, &carray_data_type, ca);
1085
+ GetCArray(p, ca);
1064
1086
  if ( ! ca_is_face(ca) ) { all_face = 0; continue; }
1065
1087
  any_face = 1;
1066
1088
  if ( NIL_P(face_class) ) {
@@ -1088,8 +1110,8 @@ ca_face_list_class (VALUE list, const char *who)
1088
1110
  "%s: cannot mix Face and non-Face arrays, or arrays of "
1089
1111
  "different Face classes; convert them first to one Face "
1090
1112
  "class or to no Face, or combine their storage (#%s gives "
1091
- "the storage of a %s)",
1092
- who, storage, rb_obj_classname(face));
1113
+ "the storage of a %"PRIsVALUE")",
1114
+ who, storage, rb_obj_class(face));
1093
1115
  }
1094
1116
  rb_raise(rb_eArgError,
1095
1117
  "%s: cannot mix Face and non-Face arrays, or arrays of "
@@ -1099,30 +1121,30 @@ ca_face_list_class (VALUE list, const char *who)
1099
1121
  }
1100
1122
  if ( n > 1 && ! ca_face_state_portable(ref->obj_type, face_class) ) {
1101
1123
  rb_raise(rb_eArgError,
1102
- "%s: %s state is not portable across several arrays (each "
1124
+ "%s: %"PRIsVALUE" state is not portable across several arrays (each "
1103
1125
  "keeps its values in storage of its own); convert them first "
1104
1126
  "(%s)",
1105
- who, rb_class2name(face_class),
1127
+ who, face_class,
1106
1128
  ca_face_conversion_hint(face));
1107
1129
  }
1108
1130
  for ( i = 1; i < n; i++ ) {
1109
- VALUE p = rb_ary_entry(list, i);
1131
+ volatile VALUE p = rb_ary_entry(list, i);
1110
1132
  CArray *ca;
1111
- TypedData_Get_Struct(p, CArray, &carray_data_type, ca);
1133
+ GetCArray(p, ca);
1112
1134
  if ( ! ca_face_state_compatible(rb_ary_entry(list, 0), ref, p, ca) ) {
1113
1135
  rb_raise(rb_eArgError,
1114
- "%s: Face state mismatch (the %s at index %ld differs from "
1115
- "index 0)", who, rb_class2name(face_class), i);
1136
+ "%s: Face state mismatch (the %"PRIsVALUE" at index %ld differs from "
1137
+ "index 0)", who, face_class, i);
1116
1138
  }
1117
1139
  }
1118
1140
  return face_class;
1119
1141
  }
1120
1142
 
1121
1143
  VALUE
1122
- ca_face_strip_one (VALUE v)
1144
+ ca_face_strip_one (volatile VALUE v)
1123
1145
  {
1124
1146
  CArray *c;
1125
- TypedData_Get_Struct(v, CArray, &carray_data_type, c);
1147
+ GetCArray(v, c);
1126
1148
  return ca_is_face(c) ? rb_ca_parent(v) : v;
1127
1149
  }
1128
1150
 
@@ -1142,7 +1164,7 @@ rb_ca_s_face_state_portable_p (VALUE klass)
1142
1164
  CA_FLAG_FACE_ORDERABLE_STORAGE, false for a Face whose cells encode the
1143
1165
  value (a const string's ranges, a categorical's codes). */
1144
1166
  static VALUE
1145
- rb_ca_orderable_storage_p (VALUE self)
1167
+ rb_ca_orderable_storage_p (volatile VALUE self)
1146
1168
  {
1147
1169
  CArray *ca;
1148
1170
  GetCArray(self, ca);
@@ -1155,7 +1177,7 @@ rb_ca_orderable_storage_p (VALUE self)
1155
1177
  for an array that is not a Face and for a Face that declares
1156
1178
  CA_FLAG_FACE_COMPARABLE_STORAGE. */
1157
1179
  static VALUE
1158
- rb_ca_comparable_storage_p (VALUE self)
1180
+ rb_ca_comparable_storage_p (volatile VALUE self)
1159
1181
  {
1160
1182
  CArray *ca;
1161
1183
  GetCArray(self, ca);
@@ -1176,7 +1198,7 @@ rb_ca_face_storage (VALUE self)
1176
1198
  (ca_face_reconcile_operand for a Face, ca_face_operand_descend for an
1177
1199
  array that is not one). For the families written in Ruby. */
1178
1200
  static VALUE
1179
- rb_ca_reconcile_operand (VALUE self, VALUE operand, VALUE rname)
1201
+ rb_ca_reconcile_operand (volatile VALUE self, VALUE operand, volatile VALUE rname)
1180
1202
  {
1181
1203
  CArray *ca;
1182
1204
  const char *name = StringValueCStr(rname);
@@ -1193,16 +1215,34 @@ rb_ca_reconcile_operand (VALUE self, VALUE operand, VALUE rname)
1193
1215
  promote_list pass). Delegates the data_type rewrite and optional
1194
1216
  copy_state callback to ca_face_lift. */
1195
1217
  static VALUE
1196
- rb_ca_face_lift_method (VALUE self, VALUE face_parent)
1218
+ rb_ca_face_lift_method (volatile VALUE self, volatile VALUE face_parent)
1197
1219
  {
1198
- CArray *fp;
1220
+ CArray *fp, *vc, *storage;
1199
1221
  if ( ! rb_obj_is_kind_of(face_parent, rb_cCArray) ) {
1200
1222
  rb_raise(rb_eArgError, "face_lift: face_parent must be a CArray");
1201
1223
  }
1202
- TypedData_Get_Struct(face_parent, CArray, &carray_data_type, fp);
1224
+ GetCArray(face_parent, fp);
1203
1225
  if ( ! ca_is_face(fp) ) {
1204
1226
  rb_raise(rb_eArgError, "face_lift: face_parent must be a Face instance");
1205
1227
  }
1228
+ rb_check_carray_object(self);
1229
+ GetCArray(self, vc);
1230
+ /* A Face of the same class is already lifted (see ca_face_lift). */
1231
+ if ( ca_is_face(vc) && rb_obj_class(self) == rb_obj_class(face_parent) ) {
1232
+ return self;
1233
+ }
1234
+ storage = ca_strip_face(fp);
1235
+ /* A Face reads the cells under it as its own, so they must be held in
1236
+ its surface type or its storage type. A Face receiver is held in its
1237
+ surface type, and is checked the same way. */
1238
+ if ( ! ( vc->data_type == fp->data_type && vc->bytes == fp->bytes )
1239
+ && ! ( vc->data_type == storage->data_type
1240
+ && vc->bytes == storage->bytes ) ) {
1241
+ rb_raise(rb_eArgError,
1242
+ "face_lift: a %"PRIsVALUE" holds its cells as %s, not as %s",
1243
+ rb_obj_class(face_parent), ca_type_name[storage->data_type],
1244
+ ca_type_name[vc->data_type]);
1245
+ }
1206
1246
  return ca_face_lift(self, face_parent);
1207
1247
  }
1208
1248
 
@@ -1225,13 +1265,13 @@ rb_ca_face_lift_method (VALUE self, VALUE face_parent)
1225
1265
  void
1226
1266
  ca_face_reconcile_comparison (volatile VALUE *pself, volatile VALUE *pother)
1227
1267
  {
1228
- VALUE self = *pself, other = *pother;
1268
+ volatile VALUE self = *pself, other = *pother;
1229
1269
  CArray *sc, *storage;
1230
1270
 
1231
1271
  if ( ! rb_obj_is_kind_of(self, rb_cCArray) ) {
1232
1272
  return;
1233
1273
  }
1234
- TypedData_Get_Struct(self, CArray, &carray_data_type, sc);
1274
+ GetCArray(self, sc);
1235
1275
  if ( ! ca_is_face(sc) ) {
1236
1276
  return;
1237
1277
  }
@@ -1244,7 +1284,7 @@ ca_face_reconcile_comparison (volatile VALUE *pself, volatile VALUE *pother)
1244
1284
  static ID id_to_string = 0;
1245
1285
  CArray *oc;
1246
1286
  if ( id_to_string == 0 ) id_to_string = rb_intern("to_string");
1247
- TypedData_Get_Struct(other, CArray, &carray_data_type, oc);
1287
+ GetCArray(other, oc);
1248
1288
  if ( ca_is_face(oc) && rb_respond_to(self, id_to_string)
1249
1289
  && rb_respond_to(other, id_to_string)
1250
1290
  && ( rb_obj_class(self) != rb_obj_class(other)
@@ -1284,22 +1324,29 @@ ca_face_reconcile_comparison (volatile VALUE *pself, volatile VALUE *pother)
1284
1324
  }
1285
1325
  }
1286
1326
 
1287
- /* A Face's parent, the array its values are stored in, is handed out as a
1288
- read-only view: a write through it would change the Face's values
1289
- without the Face's conversion (a tick count stored into a time, bytes
1290
- into a record). The storage is written through the Face's own named
1291
- methods (CATime#ticks), which take it with face_storage. */
1292
- static VALUE
1293
- rb_ca_face_parent (VALUE self)
1327
+ /* CArray#parent. A Face's parent, the array its values are stored in, is
1328
+ handed out as a read-only view: a write through it would change the
1329
+ Face's values without the Face's conversion (a tick count stored into a
1330
+ time, bytes into a record). The storage is written through the Face's
1331
+ own named methods (CATime#ticks), which take it with face_storage. A
1332
+ Face is told by its flag, so a Face written in Ruby (a CAObject with
1333
+ face: true) is handed the same view as a subclass of CAFace. Any other
1334
+ array gets its parent itself. */
1335
+ VALUE
1336
+ rb_ca_parent_method (volatile VALUE self)
1294
1337
  {
1295
1338
  volatile VALUE st = rb_ca_parent(self), view;
1296
- CArray *cs, *cv;
1339
+ CArray *ca, *cs, *cv;
1297
1340
  if ( NIL_P(st) ) {
1298
1341
  return st;
1299
1342
  }
1300
- TypedData_Get_Struct(st, CArray, &carray_data_type, cs);
1343
+ GetCArray(self, ca);
1344
+ if ( ! ca_is_face(ca) ) {
1345
+ return st;
1346
+ }
1347
+ GetCArray(st, cs);
1301
1348
  view = rb_ca_refer_new(st, cs->data_type, cs->ndim, cs->dim, cs->bytes, 0);
1302
- TypedData_Get_Struct(view, CArray, &carray_data_type, cv);
1349
+ GetCArray(view, cv);
1303
1350
  ca_set_flag(cv, CA_FLAG_READ_ONLY);
1304
1351
  return view;
1305
1352
  }
@@ -1313,7 +1360,6 @@ Init_ca_face (void)
1313
1360
  ahead of this Init) — CAFace inherits from it. */
1314
1361
  rb_cCAFace = rb_define_class("CAFace", rb_cCAView);
1315
1362
  rb_define_alloc_func(rb_cCAFace, rb_ca_face_s_allocate_forbidden);
1316
- rb_define_method(rb_cCAFace, "parent", rb_ca_face_parent, 0);
1317
1363
 
1318
1364
  /* Initialise the portable table to -1 for every slot so dispatch
1319
1365
  can distinguish "explicitly registered 0" (CAConstString) from
@@ -1322,11 +1368,11 @@ Init_ca_face (void)
1322
1368
  ca_face_state_portable_table[i] = -1;
1323
1369
  }
1324
1370
 
1325
- rb_define_method(rb_cCArray, "face_state_compatible?",
1371
+ rb_define_private_method(rb_cCArray, "face_state_compatible?",
1326
1372
  rb_ca_face_state_compatible_p, 1);
1327
- rb_define_singleton_method(rb_cCArray, "face_state_portable?",
1373
+ rb_define_private_method(rb_singleton_class(rb_cCArray), "face_state_portable?",
1328
1374
  rb_ca_s_face_state_portable_p, 0);
1329
- rb_define_method(rb_cCArray, "face_lift", rb_ca_face_lift_method, 1);
1375
+ rb_define_private_method(rb_cCArray, "face_lift", rb_ca_face_lift_method, 1);
1330
1376
  rb_define_private_method(rb_cCArray, "orderable_storage?",
1331
1377
  rb_ca_orderable_storage_p, 0);
1332
1378
  rb_define_private_method(rb_cCArray, "comparable_storage?",