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
@@ -1,7 +1,7 @@
1
1
  # CAStruct::Builder -- the DSL backend behind CArray.struct { ... } and
2
2
  # CArray.union { ... }. Autoloaded from lib/carray/struct.rb on first
3
3
  # reference to CAStruct::Builder, so applications that only consume
4
- # pre-defined struct classes never pay for loading the Builder.
4
+ # pre-defined struct classes never load the Builder.
5
5
 
6
6
  # ---------------------------------------------------------------------
7
7
  #
@@ -13,7 +13,7 @@ class CAStruct::Builder # :nodoc:
13
13
 
14
14
  class Member # :nodoc:
15
15
 
16
- def initialize (name, type, opt={})
16
+ def initialize(name, type, opt={})
17
17
  @name, @type, @opt = name, type, opt
18
18
  if @type == :bitfield
19
19
  # Bit members carry their own offset/length math via the
@@ -49,7 +49,7 @@ class CAStruct::Builder # :nodoc:
49
49
  # `data_size` is the struct's DATA_SIZE; needed up front so we can
50
50
  # bounds-check bit-field projections at definition time rather than
51
51
  # at first access.
52
- def self.build_dispatch_table (member_table, data_size)
52
+ def self.build_dispatch_table(member_table, data_size)
53
53
  dispatch = {}
54
54
  member_table.each do |name, entry|
55
55
  offset, type, opts = *entry
@@ -104,7 +104,7 @@ class CAStruct::Builder # :nodoc:
104
104
  FAST_KIND_ENDIAN = 2
105
105
 
106
106
  # @!visibility private
107
- def self.build_fast_primitives (member_table, data_size = nil)
107
+ private_class_method def self.build_fast_primitives(member_table, data_size = nil)
108
108
  fast = {}
109
109
  host_endian = CArray.endian
110
110
  member_table.each do |name, entry|
@@ -139,7 +139,7 @@ class CAStruct::Builder # :nodoc:
139
139
  # word reaches past DATA_SIZE -- the existing build_bitfield_dispatcher
140
140
  # raises in that case at definition time; here we just bow out and
141
141
  # let DISPATCH_TABLE raise).
142
- def self.build_fast_bitfield_entry (opts, data_size)
142
+ private_class_method def self.build_fast_bitfield_entry(opts, data_size)
143
143
  bits = opts[:bits]
144
144
  bit_offset = opts[:bit_offset]
145
145
  start_byte = bit_offset / 8
@@ -159,11 +159,10 @@ class CAStruct::Builder # :nodoc:
159
159
  # an actual byte swap on the current host. Matches CArray#endian's
160
160
  # short-circuit logic: :preserve and :native are always identity;
161
161
  # :big / :little are identity when the host already matches.
162
- def self.endian_needs_swap? (endian_sym, host_endian)
162
+ private_class_method def self.endian_needs_swap?(endian_sym, host_endian)
163
163
  case endian_sym
164
164
  when :preserve, :native then false
165
- when :big then host_endian != CA_BIG_ENDIAN
166
- when :little then host_endian != CA_LITTLE_ENDIAN
165
+ when :big, :little then host_endian != endian_sym
167
166
  else false # unknown sym — be conservative, defer to DISPATCH_TABLE
168
167
  end
169
168
  end
@@ -202,7 +201,7 @@ class CAStruct::Builder # :nodoc:
202
201
 
203
202
  # -- per-kind dispatcher builders -------------------------------------
204
203
 
205
- def self.build_bitfield_dispatcher (name, opts, data_size)
204
+ private_class_method def self.build_bitfield_dispatcher(name, opts, data_size)
206
205
  bits = opts[:bits]
207
206
  bit_offset = opts[:bit_offset]
208
207
  start_byte = bit_offset / 8
@@ -240,7 +239,7 @@ class CAStruct::Builder # :nodoc:
240
239
  # parent's own bytes (a field view), so writing to it writes to the
241
240
  # parent, as writing to an array member does. A frozen parent gives a
242
241
  # frozen nested element.
243
- def self.build_nested_struct_dispatcher (offset, nested_class)
242
+ private_class_method def self.build_nested_struct_dispatcher(offset, nested_class)
244
243
  reader = ->(data) {
245
244
  element = nested_class.allocate
246
245
  element.instance_variable_set(:@data, data.field(offset, nested_class).send(:face_storage))
@@ -252,14 +251,14 @@ class CAStruct::Builder # :nodoc:
252
251
  end
253
252
 
254
253
  # @!visibility private
255
- def self.build_carray_template_dispatcher (offset, template)
254
+ private_class_method def self.build_carray_template_dispatcher(offset, template)
256
255
  reader = ->(data) { data.field(offset, template)[0, false] }
257
256
  writer = ->(data, val) { data.field(offset, template)[0, false] = val }
258
257
  [reader, writer].freeze
259
258
  end
260
259
 
261
260
  # @!visibility private
262
- def self.build_primitive_dispatcher (offset, type, opts)
261
+ private_class_method def self.build_primitive_dispatcher(offset, type, opts)
263
262
  # Specialise the reader/writer to the smallest amount of work
264
263
  # this particular member needs: no opts → 2-arg field; opts with
265
264
  # only :bytes → 3-arg field; opts with :endian → 3-arg field +
@@ -293,34 +292,23 @@ class CAStruct::Builder # :nodoc:
293
292
  # Shared with every Ractor when it can be. A member whose type is an
294
293
  # array template holds a CArray, which cannot be shared, and its struct
295
294
  # is then used in the main Ractor only.
296
- def self.shareable (object)
295
+ private_class_method def self.shareable(object)
297
296
  Ractor.make_shareable(object)
298
297
  rescue Ractor::Error
299
298
  object.freeze
300
299
  end
301
300
 
302
- ALIGN_TABLE = {
303
- CA_FIXLEN => CA_ALIGN_FIXLEN,
304
- CA_BOOLEAN => CA_ALIGN_INT8,
305
- CA_INT8 => CA_ALIGN_INT8,
306
- CA_UINT8 => CA_ALIGN_INT8,
307
- CA_INT16 => CA_ALIGN_INT16,
308
- CA_UINT16 => CA_ALIGN_INT16,
309
- CA_INT32 => CA_ALIGN_INT32,
310
- CA_UINT32 => CA_ALIGN_INT32,
311
- CA_INT64 => CA_ALIGN_INT64,
312
- CA_UINT64 => CA_ALIGN_INT64,
313
- CA_FLOAT32 => CA_ALIGN_FLOAT32,
314
- CA_FLOAT64 => CA_ALIGN_FLOAT64,
315
- CA_CMPLX64 => CA_ALIGN_FLOAT32,
316
- CA_CMPLX128 => CA_ALIGN_FLOAT64,
317
- }
318
- Ractor.make_shareable(ALIGN_TABLE)
301
+ # The alignment of each member type, as the C compiler lays it out
302
+ # (data type Symbol => bytes), and of a pointer.
303
+ ALIGN_TABLE = Ractor.make_shareable(
304
+ CArray.const_get(:STRUCT_ALIGNMENT).reject { |type, _| type == :voidp })
305
+ ALIGN_VOIDP = CArray.const_get(:STRUCT_ALIGNMENT)[:voidp]
306
+ private_constant :ALIGN_TABLE, :ALIGN_VOIDP
319
307
 
320
308
  # Raises ArgumentError when `opt` names a key outside `allowed`. The
321
309
  # options are read one key at a time, so a misspelt key would otherwise
322
310
  # be ignored and the layout would change without a word.
323
- def self.check_keys! (opt, allowed, where)
311
+ private_class_method def self.check_keys!(opt, allowed, where)
324
312
  unknown = opt.keys - allowed
325
313
  return if unknown.empty?
326
314
  raise ArgumentError, "#{where}: unknown keyword#{unknown.size > 1 ? "s" : ""}: " +
@@ -331,14 +319,17 @@ class CAStruct::Builder # :nodoc:
331
319
  MEMBER_KEYS = [:offset, :bytes, :endian].freeze
332
320
  NESTED_KEYS = (BUILDER_KEYS + [:type, :offset]).freeze
333
321
 
334
- def initialize (type, opt = {})
335
- CAStruct::Builder.check_keys!(opt, BUILDER_KEYS, type.to_s)
336
- if not opt[:pack].nil? and not opt[:pack].is_a?(Integer)
322
+ def initialize(type, opt = {})
323
+ CAStruct::Builder.send(:check_keys!, opt, BUILDER_KEYS, type.to_s)
324
+ # pack: 1 places each member right after the previous one; nil aligns
325
+ # each member to its type. A packing bound other than 1 is not
326
+ # implemented, so it is refused rather than read as 1.
327
+ unless opt[:pack].nil? or opt[:pack].equal?(1)
337
328
  raise CAStruct::DefinitionError,
338
- "invalid :pack value #{opt[:pack].inspect} (expected nil or Integer)"
329
+ "invalid :pack value #{opt[:pack].inspect} (expected nil or 1)"
339
330
  end
340
331
  @type = type ### :struct or :union
341
- @align = opt[:pack] ### nil for alignment, int for pack(n)
332
+ @align = opt[:pack] ### nil aligns each member, 1 packs them
342
333
  @members = [] ### array of CArray::Struct::Builder::Member
343
334
  @offset = 0 ### offset of each member and size of struct
344
335
  @extent = 0 ### furthest byte any member reaches
@@ -351,7 +342,7 @@ class CAStruct::Builder # :nodoc:
351
342
  # The `order_by:` members, as member names. Each has to be a member of
352
343
  # this struct that holds one ordered value: not a nested record, an array
353
344
  # member or a complex number.
354
- def order_keys (table)
345
+ def order_keys(table)
355
346
  keys = Array(@order_by).map(&:to_s)
356
347
  if keys.empty?
357
348
  raise CAStruct::DefinitionError, "order_by: names no member"
@@ -388,9 +379,10 @@ class CAStruct::Builder # :nodoc:
388
379
  @bit_offset = 0
389
380
  end
390
381
  end
382
+ private :flush_bit_offset
391
383
 
392
- # @!visibility private
393
- def define (&block)
384
+ # Runs the definition block and returns the struct class it describes.
385
+ private def define(&block)
394
386
  # ---
395
387
  case block.arity
396
388
  when 1
@@ -475,9 +467,9 @@ class CAStruct::Builder # :nodoc:
475
467
  end
476
468
  table.freeze
477
469
  names.freeze
478
- klass.const_set(:MEMBER_TABLE, self.class.shareable(table)) ### required element as data class
479
- klass.const_set(:MEMBERS, self.class.shareable(names))
480
- klass.const_set(:ORDER_BY, self.class.shareable(order_keys(table))) if @order_by
470
+ klass.const_set(:MEMBER_TABLE, self.class.send(:shareable, table)) ### required element as data class
471
+ klass.const_set(:MEMBERS, self.class.send(:shareable, names))
472
+ klass.const_set(:ORDER_BY, self.class.send(:shareable, order_keys(table))) if @order_by
481
473
  # ---
482
474
  # Step 3 dispatch table: precompute a per-member [reader, writer]
483
475
  # Proc pair so `record[name]` is one Hash lookup + one Proc call,
@@ -485,7 +477,7 @@ class CAStruct::Builder # :nodoc:
485
477
  # checks and view-shape decisions happen here, once, at class
486
478
  # definition time.
487
479
  klass.const_set(:DISPATCH_TABLE,
488
- self.class.shareable(self.class.build_dispatch_table(table, @offset)))
480
+ self.class.send(:shareable, self.class.build_dispatch_table(table, @offset)))
489
481
  # Also precompute FAST_PRIMITIVES, the subset of members that the
490
482
  # C-side `[]` / `[]=` can handle without
491
483
  # allocating a CAField view. Entries are `[offset, type_code]`;
@@ -494,7 +486,7 @@ class CAStruct::Builder # :nodoc:
494
486
  # members are absent here -- they stay on the DISPATCH_TABLE Proc
495
487
  # path.
496
488
  klass.const_set(:FAST_PRIMITIVES,
497
- self.class.build_fast_primitives(table, @offset).freeze)
489
+ self.class.send(:build_fast_primitives, table, @offset).freeze)
498
490
  # ---
499
491
  # An accessor for each member, unless the name is already a method of
500
492
  # the element (hash, size, class, initialize, ...): the accessor would
@@ -522,8 +514,8 @@ class CAStruct::Builder # :nodoc:
522
514
 
523
515
  private
524
516
 
525
- def alignment (addr, data_type, opt={})
526
- # ALIGN_TABLE keys are CA_* constants, which are Symbols. Dispatch on
517
+ def alignment(addr, data_type, opt={})
518
+ # ALIGN_TABLE keys are data type Symbols. Dispatch on
527
519
  # Symbol directly; the Integer path (= raw data_type code) maps via the
528
520
  # canonical Symbol name.
529
521
  case data_type
@@ -538,7 +530,7 @@ class CAStruct::Builder # :nodoc:
538
530
  when Integer
539
531
  align = ALIGN_TABLE[CArray.data_type_name(data_type).to_sym]
540
532
  when CArray, Class
541
- align = CA_ALIGN_VOIDP
533
+ align = ALIGN_VOIDP
542
534
  else
543
535
  data_type, bytes = CArray.guess_type_and_bytes(data_type, opt[:bytes])
544
536
  return alignment(addr, data_type, :bytes=>bytes)
@@ -552,7 +544,7 @@ class CAStruct::Builder # :nodoc:
552
544
  return addr
553
545
  end
554
546
 
555
- def pack (addr, align, opt={})
547
+ def pack(addr, align, opt={})
556
548
  if addr < 0
557
549
  raise CAStruct::DefinitionError, format("negative member offset: %d", addr)
558
550
  end
@@ -575,8 +567,8 @@ class CAStruct::Builder # :nodoc:
575
567
  # when `nil`.
576
568
  # @param opt [Hash] member options (`:offset`, `:bytes`, `:endian`, ...).
577
569
  # @return [Member]
578
- def member (data_type, id = nil, opt = {})
579
- CAStruct::Builder.check_keys!(opt, MEMBER_KEYS, "member")
570
+ def member(data_type, id = nil, opt = {})
571
+ CAStruct::Builder.send(:check_keys!, opt, MEMBER_KEYS, "member")
580
572
  opt = opt.clone
581
573
  # If a bit member sequence was in flight, round up to a clean byte
582
574
  # boundary before placing this byte-typed member.
@@ -630,12 +622,12 @@ class CAStruct::Builder # :nodoc:
630
622
  # @return [Class] the nested struct class.
631
623
  # @raise [CAStruct::DefinitionError] when neither a block nor
632
624
  # `type:` is provided.
633
- def struct (*args, &block)
625
+ def struct(*args, &block)
634
626
  opt = args.last.is_a?(Hash) ? args.pop : {}
635
- CAStruct::Builder.check_keys!(opt, NESTED_KEYS, "struct")
627
+ CAStruct::Builder.send(:check_keys!, opt, NESTED_KEYS, "struct")
636
628
  if block
637
629
  inner = {:pack => @align}.update(opt.slice(*BUILDER_KEYS))
638
- st = self.class.new(:struct, inner).define(&block)
630
+ st = self.class.new(:struct, inner).send(:define, &block)
639
631
  elsif opt[:type] and opt[:type] <= CAStruct
640
632
  st = opt[:type]
641
633
  else
@@ -657,12 +649,12 @@ class CAStruct::Builder # :nodoc:
657
649
  # @return [Class] the nested union class.
658
650
  # @raise [CAStruct::DefinitionError] when neither a block nor
659
651
  # `type:` is provided.
660
- def union (*args, &block)
652
+ def union(*args, &block)
661
653
  opt = args.last.is_a?(Hash) ? args.pop : {}
662
- CAStruct::Builder.check_keys!(opt, NESTED_KEYS, "union")
654
+ CAStruct::Builder.send(:check_keys!, opt, NESTED_KEYS, "union")
663
655
  if block
664
656
  inner = {:pack => @align}.update(opt.slice(*BUILDER_KEYS))
665
- st = self.class.new(:union, inner).define(&block)
657
+ st = self.class.new(:union, inner).send(:define, &block)
666
658
  elsif opt[:type] and opt[:type] <= CAStruct
667
659
  st = opt[:type]
668
660
  else
@@ -682,9 +674,9 @@ class CAStruct::Builder # :nodoc:
682
674
  # @return [void]
683
675
  # @raise [CAStruct::DefinitionError] when `type:` is not a
684
676
  # CArray template.
685
- def array (*args)
677
+ def array(*args)
686
678
  opt = args.last.is_a?(Hash) ? args.pop : {}
687
- CAStruct::Builder.check_keys!(opt, [:type, :offset], "array")
679
+ CAStruct::Builder.send(:check_keys!, opt, [:type, :offset], "array")
688
680
  if not opt[:type] or not opt[:type].kind_of?(CArray)
689
681
  raise CAStruct::DefinitionError,
690
682
  "no :type given for array member (expected a CArray template)"
@@ -699,13 +691,16 @@ class CAStruct::Builder # :nodoc:
699
691
  # Declares a bitfield member. Bits accumulate within the
700
692
  # current byte; when a byte member follows, the cursor rounds
701
693
  # up to the next byte boundary. Only supported in `:struct`
702
- # (not `:union`) and always requires a name.
694
+ # (not `:union`) and always requires a name. A member of more
695
+ # than one bit keeps the low `bits` bits of an Integer written to
696
+ # it (300 into 3 bits is 4), through an element or the member's
697
+ # column alike; a one-bit member is boolean.
703
698
  # @param name [Symbol, String] member name.
704
699
  # @param bits [Integer] bit width in `1..64`.
705
700
  # @return [void]
706
701
  # @raise [CAStruct::DefinitionError] on `:union` context, a
707
702
  # missing name, or an invalid `bits` value.
708
- def bit (name, bits:)
703
+ def bit(name, bits:)
709
704
  if @type == :union
710
705
  raise CAStruct::DefinitionError,
711
706
  "bit members in :union are not supported"
@@ -747,7 +742,7 @@ class CAStruct::Builder # :nodoc:
747
742
  # Validate the `endian:` option on a typed-member declaration.
748
743
  # `allow` is false for non-numeric types (e.g. fixlen) where a
749
744
  # byte-swap view would mangle the value.
750
- def self.validate_endian! (opt, typename, allow)
745
+ private_class_method def self.validate_endian!(opt, typename, allow)
751
746
  return unless opt.key?(:endian)
752
747
  unless allow
753
748
  raise CAStruct::DefinitionError,
@@ -760,6 +755,192 @@ class CAStruct::Builder # :nodoc:
760
755
  end
761
756
  end
762
757
 
758
+ # @!group Members by type
759
+
760
+ # @!method boolean(*names, offset: nil, endian: nil)
761
+ # Declares one `:boolean` member for each name, in order.
762
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
763
+ # declares an unnamed member, which serves as padding.
764
+ # @param offset [Integer, nil] byte offset of the member in a packed
765
+ # struct (`pack:`), in place of the next position.
766
+ # @param endian [Symbol, nil] byte order of the stored value:
767
+ # `:preserve`, `:native`, `:big` or `:little`.
768
+ # @return [void]
769
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
770
+ # `offset:` is given in a struct that is not packed.
771
+ # @raise [ArgumentError] on an unknown keyword.
772
+
773
+ # @!method int8(*names, offset: nil, endian: nil)
774
+ # Declares one `:int8` member for each name, in order.
775
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
776
+ # declares an unnamed member, which serves as padding.
777
+ # @param offset [Integer, nil] byte offset of the member in a packed
778
+ # struct (`pack:`), in place of the next position.
779
+ # @param endian [Symbol, nil] byte order of the stored value:
780
+ # `:preserve`, `:native`, `:big` or `:little`.
781
+ # @return [void]
782
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
783
+ # `offset:` is given in a struct that is not packed.
784
+ # @raise [ArgumentError] on an unknown keyword.
785
+
786
+ # @!method uint8(*names, offset: nil, endian: nil)
787
+ # Declares one `:uint8` member for each name, in order.
788
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
789
+ # declares an unnamed member, which serves as padding.
790
+ # @param offset [Integer, nil] byte offset of the member in a packed
791
+ # struct (`pack:`), in place of the next position.
792
+ # @param endian [Symbol, nil] byte order of the stored value:
793
+ # `:preserve`, `:native`, `:big` or `:little`.
794
+ # @return [void]
795
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
796
+ # `offset:` is given in a struct that is not packed.
797
+ # @raise [ArgumentError] on an unknown keyword.
798
+
799
+ # @!method int16(*names, offset: nil, endian: nil)
800
+ # Declares one `:int16` member for each name, in order.
801
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
802
+ # declares an unnamed member, which serves as padding.
803
+ # @param offset [Integer, nil] byte offset of the member in a packed
804
+ # struct (`pack:`), in place of the next position.
805
+ # @param endian [Symbol, nil] byte order of the stored value:
806
+ # `:preserve`, `:native`, `:big` or `:little`.
807
+ # @return [void]
808
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
809
+ # `offset:` is given in a struct that is not packed.
810
+ # @raise [ArgumentError] on an unknown keyword.
811
+
812
+ # @!method uint16(*names, offset: nil, endian: nil)
813
+ # Declares one `:uint16` member for each name, in order.
814
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
815
+ # declares an unnamed member, which serves as padding.
816
+ # @param offset [Integer, nil] byte offset of the member in a packed
817
+ # struct (`pack:`), in place of the next position.
818
+ # @param endian [Symbol, nil] byte order of the stored value:
819
+ # `:preserve`, `:native`, `:big` or `:little`.
820
+ # @return [void]
821
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
822
+ # `offset:` is given in a struct that is not packed.
823
+ # @raise [ArgumentError] on an unknown keyword.
824
+
825
+ # @!method int32(*names, offset: nil, endian: nil)
826
+ # Declares one `:int32` member for each name, in order.
827
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
828
+ # declares an unnamed member, which serves as padding.
829
+ # @param offset [Integer, nil] byte offset of the member in a packed
830
+ # struct (`pack:`), in place of the next position.
831
+ # @param endian [Symbol, nil] byte order of the stored value:
832
+ # `:preserve`, `:native`, `:big` or `:little`.
833
+ # @return [void]
834
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
835
+ # `offset:` is given in a struct that is not packed.
836
+ # @raise [ArgumentError] on an unknown keyword.
837
+
838
+ # @!method uint32(*names, offset: nil, endian: nil)
839
+ # Declares one `:uint32` member for each name, in order.
840
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
841
+ # declares an unnamed member, which serves as padding.
842
+ # @param offset [Integer, nil] byte offset of the member in a packed
843
+ # struct (`pack:`), in place of the next position.
844
+ # @param endian [Symbol, nil] byte order of the stored value:
845
+ # `:preserve`, `:native`, `:big` or `:little`.
846
+ # @return [void]
847
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
848
+ # `offset:` is given in a struct that is not packed.
849
+ # @raise [ArgumentError] on an unknown keyword.
850
+
851
+ # @!method int64(*names, offset: nil, endian: nil)
852
+ # Declares one `:int64` member for each name, in order.
853
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
854
+ # declares an unnamed member, which serves as padding.
855
+ # @param offset [Integer, nil] byte offset of the member in a packed
856
+ # struct (`pack:`), in place of the next position.
857
+ # @param endian [Symbol, nil] byte order of the stored value:
858
+ # `:preserve`, `:native`, `:big` or `:little`.
859
+ # @return [void]
860
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
861
+ # `offset:` is given in a struct that is not packed.
862
+ # @raise [ArgumentError] on an unknown keyword.
863
+
864
+ # @!method uint64(*names, offset: nil, endian: nil)
865
+ # Declares one `:uint64` member for each name, in order.
866
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
867
+ # declares an unnamed member, which serves as padding.
868
+ # @param offset [Integer, nil] byte offset of the member in a packed
869
+ # struct (`pack:`), in place of the next position.
870
+ # @param endian [Symbol, nil] byte order of the stored value:
871
+ # `:preserve`, `:native`, `:big` or `:little`.
872
+ # @return [void]
873
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
874
+ # `offset:` is given in a struct that is not packed.
875
+ # @raise [ArgumentError] on an unknown keyword.
876
+
877
+ # @!method float32(*names, offset: nil, endian: nil)
878
+ # Declares one `:float32` member for each name, in order.
879
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
880
+ # declares an unnamed member, which serves as padding.
881
+ # @param offset [Integer, nil] byte offset of the member in a packed
882
+ # struct (`pack:`), in place of the next position.
883
+ # @param endian [Symbol, nil] byte order of the stored value:
884
+ # `:preserve`, `:native`, `:big` or `:little`.
885
+ # @return [void]
886
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
887
+ # `offset:` is given in a struct that is not packed.
888
+ # @raise [ArgumentError] on an unknown keyword.
889
+
890
+ # @!method float64(*names, offset: nil, endian: nil)
891
+ # Declares one `:float64` member for each name, in order.
892
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
893
+ # declares an unnamed member, which serves as padding.
894
+ # @param offset [Integer, nil] byte offset of the member in a packed
895
+ # struct (`pack:`), in place of the next position.
896
+ # @param endian [Symbol, nil] byte order of the stored value:
897
+ # `:preserve`, `:native`, `:big` or `:little`.
898
+ # @return [void]
899
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
900
+ # `offset:` is given in a struct that is not packed.
901
+ # @raise [ArgumentError] on an unknown keyword.
902
+
903
+ # @!method cmplx64(*names, offset: nil, endian: nil)
904
+ # Declares one `:cmplx64` member for each name, in order.
905
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
906
+ # declares an unnamed member, which serves as padding.
907
+ # @param offset [Integer, nil] byte offset of the member in a packed
908
+ # struct (`pack:`), in place of the next position.
909
+ # @param endian [Symbol, nil] byte order of the stored value:
910
+ # `:preserve`, `:native`, `:big` or `:little`.
911
+ # @return [void]
912
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
913
+ # `offset:` is given in a struct that is not packed.
914
+ # @raise [ArgumentError] on an unknown keyword.
915
+
916
+ # @!method cmplx128(*names, offset: nil, endian: nil)
917
+ # Declares one `:cmplx128` member for each name, in order.
918
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
919
+ # declares an unnamed member, which serves as padding.
920
+ # @param offset [Integer, nil] byte offset of the member in a packed
921
+ # struct (`pack:`), in place of the next position.
922
+ # @param endian [Symbol, nil] byte order of the stored value:
923
+ # `:preserve`, `:native`, `:big` or `:little`.
924
+ # @return [void]
925
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
926
+ # `offset:` is given in a struct that is not packed.
927
+ # @raise [ArgumentError] on an unknown keyword.
928
+
929
+ # @!method fixlen(*names, bytes:, offset: nil)
930
+ # Declares one fixed-length byte-string member of `bytes` bytes for
931
+ # each name, in order.
932
+ # @param names [Array<Symbol, String, nil>] member names; `nil`
933
+ # declares an unnamed member, which serves as padding.
934
+ # @param bytes [Integer] length of the member in bytes.
935
+ # @param offset [Integer, nil] byte offset of the member in a packed
936
+ # struct (`pack:`), in place of the next position.
937
+ # @return [void]
938
+ # @raise [CAStruct::DefinitionError] when no name is given, or when
939
+ # `offset:` is given in a struct that is not packed.
940
+ # @raise [ArgumentError] on an unknown keyword.
941
+
942
+ # @!endgroup
943
+
763
944
  [
764
945
  ["fixlen", false],
765
946
  ["boolean", true],
@@ -777,15 +958,15 @@ class CAStruct::Builder # :nodoc:
777
958
  ["cmplx128", true],
778
959
  ].each do |typename, allow_endian|
779
960
  class_eval %{
780
- def #{typename} (*args)
961
+ def #{typename}(*args)
781
962
  opt = args.last.is_a?(Hash) ? args.pop : {}
782
963
  if args.empty?
783
964
  raise CAStruct::DefinitionError,
784
965
  "#{typename} requires at least one member name " \
785
966
  "(use nil for anonymous padding)"
786
967
  end
787
- CAStruct::Builder.check_keys!(opt, MEMBER_KEYS, "#{typename}")
788
- CAStruct::Builder.validate_endian!(opt, "#{typename}", #{allow_endian})
968
+ CAStruct::Builder.send(:check_keys!, opt, MEMBER_KEYS, "#{typename}")
969
+ CAStruct::Builder.send(:validate_endian!, opt, "#{typename}", #{allow_endian})
789
970
  args.each do |arg|
790
971
  member(:#{typename}, arg, opt)
791
972
  end
@@ -793,14 +974,7 @@ class CAStruct::Builder # :nodoc:
793
974
  }
794
975
  end
795
976
 
796
- alias char_p fixlen
797
- alias byte uint8
798
- alias short int16
799
- alias int int32
800
- alias float float32
801
- alias double float64
802
- alias complex cmplx64
803
- alias dcomplex cmplx128
977
+ private_constant :FAST_KIND_PRIMITIVE, :FAST_KIND_BITFIELD, :FAST_KIND_ENDIAN,
978
+ :BUILDER_KEYS, :MEMBER_KEYS, :NESTED_KEYS, :VALID_ENDIAN
804
979
 
805
980
  end
806
-