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.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1830
- data/README.md +62 -35
- data/carray.gemspec +16 -4
- data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
- data/ext/ca_array_pool.c +4 -5
- data/ext/ca_axis_descriptor.h +45 -30
- data/ext/ca_axis_dispatch.c +7 -4
- data/ext/ca_axis_group.c +12 -15
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +35 -0
- data/ext/ca_binop_dispatch.h +22 -46
- data/ext/ca_categorical_iterator.c +36 -34
- data/ext/ca_compare.c +0 -1
- data/ext/ca_compare.h +8 -1
- data/ext/ca_composite_dispatch.h +6 -0
- data/ext/ca_for_buffer.h +6 -1
- data/ext/ca_group_iter.c +17 -18
- data/ext/ca_iter_state_fields.h +196 -0
- data/ext/ca_iter_substrate.h +84 -103
- data/ext/ca_kernel_iterator.c +125 -89
- data/ext/ca_kernel_iterator.h +229 -1504
- data/ext/ca_kernel_iterator_internal.h +1167 -0
- data/ext/ca_moncmp_dispatch.h +17 -24
- data/ext/ca_monop_dispatch.h +25 -74
- data/ext/ca_obj_array.c +92 -165
- data/ext/ca_obj_bincmp.c +65 -48
- data/ext/ca_obj_binop.c +74 -49
- data/ext/ca_obj_bitarray.c +7 -21
- data/ext/ca_obj_bitfield.c +93 -26
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +34 -34
- data/ext/ca_obj_const_string.c +464 -304
- data/ext/ca_obj_face.c +167 -121
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +127 -34
- data/ext/ca_obj_farray.c +13 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +57 -34
- data/ext/ca_obj_grid.c +149 -113
- data/ext/ca_obj_meld.c +28 -26
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +88 -73
- data/ext/ca_obj_object.c +60 -46
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +10 -14
- data/ext/ca_obj_refer.c +146 -39
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +20 -28
- data/ext/ca_obj_select.c +17 -27
- data/ext/ca_obj_select_axis.c +35 -42
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +33 -29
- data/ext/ca_obj_stride.c +99 -62
- data/ext/ca_obj_string.c +8 -20
- data/ext/ca_obj_tile.c +14 -16
- data/ext/ca_obj_time.c +12 -23
- data/ext/ca_obj_timedelta.c +12 -23
- data/ext/ca_obj_transpose.c +8 -10
- data/ext/ca_obj_triop.c +48 -49
- data/ext/ca_obj_window.c +87 -99
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +97 -27
- data/ext/ca_op_powi.h +9 -28
- data/ext/ca_pow5_table.h +2 -1
- data/ext/ca_rng_normal.h +4 -0
- data/ext/ca_rng_xoshiro256pp.h +3 -0
- data/ext/ca_sort_kernels.h +23 -26
- data/ext/ca_sweep_engine.c +92 -26
- data/ext/ca_sweep_engine.h +22 -15
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +12 -12
- data/ext/carray.h +300 -1009
- data/ext/carray_access.c +320 -207
- data/ext/carray_address_basis.c +128 -59
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +31 -27
- data/ext/carray_call_cfunc.c +174 -285
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +1191 -351
- data/ext/carray_cast_func.rb +68 -8
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +98 -39
- data/ext/carray_copy.c +52 -26
- data/ext/carray_core.c +286 -132
- data/ext/carray_count.c +53 -22
- data/ext/carray_element.c +49 -28
- data/ext/carray_factorize.c +64 -63
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +9 -11
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +127 -230
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1364 -156
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +37 -37
- data/ext/carray_loop.c +27 -23
- data/ext/carray_mask.c +235 -150
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +106 -100
- data/ext/carray_memory_view.c +83 -69
- data/ext/carray_operator.c +490 -128
- data/ext/carray_order.c +163 -84
- data/ext/carray_partition.c +14 -16
- data/ext/carray_random.c +48 -42
- data/ext/carray_round.c +33 -5
- data/ext/carray_scatter.c +57 -39
- data/ext/carray_segment.c +42 -46
- data/ext/carray_slab.c +77 -51
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +37 -31
- data/ext/carray_sort_kernel.c +36 -10
- data/ext/carray_struct.c +44 -34
- data/ext/carray_test.c +131 -81
- data/ext/carray_time_text.c +30 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +373 -139
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +25 -31
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +819 -488
- data/ext/ruby_carray.c +47 -42
- data/ext/version.h +7 -5
- data/lib/carray/attribute.rb +19 -18
- data/lib/carray/autoload_carray.rb +20 -15
- data/lib/carray/autoload_method_extension.rb +64 -57
- data/lib/carray/axis_group.rb +272 -185
- data/lib/carray/basics.rb +298 -56
- data/lib/carray/bincount_nd.rb +41 -18
- data/lib/carray/block_iterator.rb +167 -119
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +84 -59
- data/lib/carray/categorical_iterator.rb +90 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +98 -23
- data/lib/carray/const_string.rb +112 -46
- data/lib/carray/core_extensions.rb +20 -69
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +95 -12
- data/lib/carray/frame/column_verbs.rb +76 -9
- data/lib/carray/frame/concat.rb +51 -7
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +20 -5
- data/lib/carray/frame/frame.rb +73 -31
- data/lib/carray/frame/group.rb +247 -187
- data/lib/carray/frame/io.rb +36 -12
- data/lib/carray/frame/join.rb +56 -17
- data/lib/carray/frame/records.rb +7 -5
- data/lib/carray/frame/reshape.rb +27 -7
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +217 -134
- data/lib/carray/frame/windows.rb +254 -19
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +28 -23
- data/lib/carray/fusion.rb +30 -30
- data/lib/carray/histogram.rb +35 -16
- data/lib/carray/inspect.rb +7 -7
- data/lib/carray/iterator.rb +104 -23
- data/lib/carray/key_windows.rb +93 -61
- data/lib/carray/lazy.rb +208 -111
- data/lib/carray/limits.rb +80 -0
- data/lib/carray/mask_gap_fill.rb +21 -16
- data/lib/carray/math.rb +87 -50
- data/lib/carray/meld_reduce.rb +30 -8
- data/lib/carray/methods/align_addr.rb +29 -17
- data/lib/carray/methods/bin.rb +8 -1
- data/lib/carray/methods/bincount.rb +4 -2
- data/lib/carray/methods/bit_string.rb +19 -31
- data/lib/carray/methods/broadcast.rb +8 -3
- data/lib/carray/methods/choose.rb +5 -5
- data/lib/carray/methods/composition.rb +73 -72
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/factorize.rb +2 -0
- data/lib/carray/methods/gather_nd.rb +106 -103
- data/lib/carray/methods/insert_block.rb +2 -2
- data/lib/carray/methods/is_between.rb +6 -4
- data/lib/carray/methods/is_in.rb +183 -143
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +157 -94
- data/lib/carray/methods/mask_duplicates.rb +1 -1
- data/lib/carray/methods/meshgrid.rb +28 -16
- data/lib/carray/methods/mode.rb +67 -67
- data/lib/carray/methods/nunique.rb +9 -7
- data/lib/carray/methods/repeat.rb +23 -13
- data/lib/carray/methods/resize.rb +1 -1
- data/lib/carray/methods/snap.rb +24 -20
- data/lib/carray/methods/string_format.rb +2 -2
- data/lib/carray/methods/unique.rb +4 -2
- data/lib/carray/methods/value_counts.rb +1 -1
- data/lib/carray/mkmf.rb +25 -28
- data/lib/carray/percentile_arguments.rb +82 -0
- data/lib/carray/record_order.rb +50 -20
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +195 -17
- data/lib/carray/segment_iterator.rb +262 -137
- data/lib/carray/serialize.rb +635 -566
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +14 -15
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +97 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +247 -73
- data/lib/carray/time.rb +663 -365
- data/lib/carray/window_iterator.rb +232 -155
- data/lib/carray.rb +6 -11
- data/yard-stubs/ca_obj_array.rb +20 -135
- data/yard-stubs/ca_obj_bitarray.rb +1 -8
- data/yard-stubs/ca_obj_bitfield.rb +6 -2
- data/yard-stubs/ca_obj_byte_swap.rb +21 -7
- data/yard-stubs/ca_obj_fake.rb +3 -1
- data/yard-stubs/ca_obj_farray.rb +1 -1
- data/yard-stubs/ca_obj_field.rb +8 -6
- data/yard-stubs/ca_obj_refer.rb +10 -5
- data/yard-stubs/ca_obj_roll.rb +7 -6
- data/yard-stubs/ca_obj_shift.rb +2 -3
- data/yard-stubs/ca_obj_stride.rb +5 -7
- data/yard-stubs/ca_obj_tile.rb +1 -1
- data/yard-stubs/ca_obj_time.rb +43 -0
- data/yard-stubs/ca_obj_transpose.rb +1 -1
- data/yard-stubs/ca_obj_window.rb +28 -16
- data/yard-stubs/carray_access.rb +55 -6
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +121 -162
- data/yard-stubs/carray_class.rb +3 -4
- data/yard-stubs/carray_conversion.rb +6 -7
- data/yard-stubs/carray_copy.rb +2 -1
- data/yard-stubs/carray_count.rb +10 -5
- data/yard-stubs/carray_element.rb +4 -3
- data/yard-stubs/carray_limits.rb +100 -0
- data/yard-stubs/carray_loop.rb +0 -20
- data/yard-stubs/carray_mask.rb +59 -41
- data/yard-stubs/carray_math.rb +162 -100
- data/yard-stubs/carray_mathfunc.rb +415 -3
- data/yard-stubs/carray_median_percentile.rb +29 -11
- data/yard-stubs/carray_memory_view.rb +2 -2
- data/yard-stubs/carray_order.rb +72 -33
- data/yard-stubs/carray_random.rb +6 -6
- data/yard-stubs/carray_reduce.rb +353 -0
- data/yard-stubs/carray_rng.rb +92 -0
- data/yard-stubs/carray_scatter.rb +12 -3
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_test.rb +18 -5
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +63 -25
- data/CHANGELOG.v1.md +0 -126
- data/ext/carray_build_flags.h +0 -3
- data/ext/carray_config.h +0 -28
- data/ext/carray_data_type.c +0 -90
- data/ext/carray_kernels_bincmp.c +0 -6457
- data/ext/carray_kernels_binop.c +0 -31534
- data/ext/carray_kernels_init.c +0 -1734
- data/ext/carray_kernels_moncmp.c +0 -2104
- data/ext/carray_kernels_monop.c +0 -25387
- data/ext/carray_kernels_reduce_aggregate.c +0 -27545
- data/ext/carray_kernels_reduce_boolean.c +0 -1924
- data/ext/carray_kernels_reduce_cumulative.c +0 -11646
- data/ext/carray_kernels_reduce_extreme.c +0 -21631
- data/ext/carray_kernels_reduce_variance.c +0 -5981
- data/ext/carray_kernels_scan.c +0 -4302
- data/ext/carray_kernels_search.c +0 -37776
- data/ext/carray_kernels_sort.c +0 -10814
- data/ext/carray_kernels_triop.c +0 -1864
- data/lib/carray/construct.rb +0 -360
- data/lib/carray/data_type_extension.rb +0 -261
- data/lib/carray/data_type_limits.rb +0 -91
data/ext/carray_cast_func.rb
CHANGED
|
@@ -197,7 +197,6 @@ puts %{
|
|
|
197
197
|
---------------------------------------------------------------------------- */
|
|
198
198
|
|
|
199
199
|
#include "carray.h"
|
|
200
|
-
#include "carray_internal.h" /* ca_decimal_string_to / _store */
|
|
201
200
|
|
|
202
201
|
static void __attribute__((noreturn))
|
|
203
202
|
ca_cast_not_implemented(ca_size_t n, CArray *a1, void *ptr1, CArray *a2, void *ptr2, boolean8_t *m)
|
|
@@ -371,7 +370,29 @@ puts
|
|
|
371
370
|
lo_cmp = ( type2 == CA_INT64 ) ? ">=" : ">"
|
|
372
371
|
fits = "( v #{lo_cmp} #{lo} && v < #{hi} )"
|
|
373
372
|
fits = "( #{fits} && ! isnan(cimag(q1[k])) )" if complex
|
|
374
|
-
|
|
373
|
+
# The cells of a bit field view keep a float's integer part modulo
|
|
374
|
+
# 2 ** bits, as a Float stored on its own does (ca_obj2integer_store).
|
|
375
|
+
unless complex
|
|
376
|
+
num = [CA_UINT8, CA_UINT16, CA_UINT32, CA_UINT64].include?(type2) ? "rb_num2ull" : "NUM2LL"
|
|
377
|
+
nan_check << <<-END_FIELD.gsub(/^ {8}/, '')
|
|
378
|
+
if ( a2 && ca_bitfield_beneath(a2) ) {
|
|
379
|
+
for (k=0; k<n; k++) {
|
|
380
|
+
double v = (double) q1[k];
|
|
381
|
+
if ( m && m[k] ) {
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if ( ! isfinite(v) ) {
|
|
385
|
+
rb_raise(rb_eFloatDomainError, "%s: %s cannot be converted to %s",
|
|
386
|
+
ca_calling_method_name(), isinf(v) ? "an infinity" : "NaN",
|
|
387
|
+
ca_type_name[#{type_const[type2]}]);
|
|
388
|
+
}
|
|
389
|
+
q2[k] = (#{ctype2}) #{num}(ca_obj2integer_store(a2, rb_float_new(v), #{type_const[type2]}));
|
|
390
|
+
}
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
END_FIELD
|
|
394
|
+
end
|
|
395
|
+
nan_check << <<-END_CHECK.gsub(/^ {6}/, '')
|
|
375
396
|
for (k=0; k<n; k++) {
|
|
376
397
|
double v = (double) #{real};
|
|
377
398
|
if ( ( ! m || ! m[k] ) && ! #{fits} ) {
|
|
@@ -481,7 +502,7 @@ OBJECT.each do |type1|
|
|
|
481
502
|
if ( m ) {
|
|
482
503
|
while ( n-- ) {
|
|
483
504
|
if ( !*m ) {
|
|
484
|
-
VALUE str = *p1;
|
|
505
|
+
volatile VALUE str = *p1;
|
|
485
506
|
Check_Type(str, T_STRING);
|
|
486
507
|
if ( bytes <= RSTRING_LEN(str) ) {
|
|
487
508
|
memcpy(p2, StringValuePtr(str), bytes);
|
|
@@ -496,7 +517,7 @@ OBJECT.each do |type1|
|
|
|
496
517
|
}
|
|
497
518
|
else {
|
|
498
519
|
while ( n-- ) {
|
|
499
|
-
VALUE str = *p1;
|
|
520
|
+
volatile VALUE str = *p1;
|
|
500
521
|
Check_Type(str, T_STRING);
|
|
501
522
|
if ( bytes <= RSTRING_LEN(str) ) {
|
|
502
523
|
memcpy(p2, StringValuePtr(str), bytes);
|
|
@@ -523,6 +544,35 @@ OBJECT.each do |type1|
|
|
|
523
544
|
ctype1 = ctype[type1]
|
|
524
545
|
ctype2 = ctype[type2]
|
|
525
546
|
CA_CAST_TABLE[type1][type2] = "ca_cast_#{ctype1}_#{ctype2}"
|
|
547
|
+
if type2 == CA_BOOLEAN
|
|
548
|
+
# object -> boolean: a String is read by its words (1 / 0 / true /
|
|
549
|
+
# false), any other value as OBJ2BOOL takes it. With a mask buffer
|
|
550
|
+
# (to_type) a cell that does not read becomes UNDEF; without one (a
|
|
551
|
+
# store) it raises.
|
|
552
|
+
puts <<-END_DEF .gsub(/^ {8}/, '')
|
|
553
|
+
static void
|
|
554
|
+
ca_cast_#{ctype1}_#{ctype2}(ca_size_t n, CArray *a1, void *ptr1, CArray *a2, void *ptr2, boolean8_t *m)
|
|
555
|
+
{
|
|
556
|
+
ca_u_VALUE *p1 = ptr1;
|
|
557
|
+
boolean8_t *p2 = ptr2;
|
|
558
|
+
if ( m ) {
|
|
559
|
+
while ( n-- ) {
|
|
560
|
+
if ( !*m && ! ca_boolean_value_to(*p1, p2) ) {
|
|
561
|
+
*m = 1;
|
|
562
|
+
*p2 = 0;
|
|
563
|
+
}
|
|
564
|
+
p1++; p2++; m++;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
while ( n-- ) { ca_boolean_value_store(*p1, p2); p1++; p2++; }
|
|
569
|
+
}
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
END_DEF
|
|
573
|
+
puts
|
|
574
|
+
next
|
|
575
|
+
end
|
|
526
576
|
if (INTEGER+FLOAT+[CA_CMPLX64, CA_CMPLX128]).include?(type2)
|
|
527
577
|
# object -> int/float/complex: parse failure -> masked (UNDEF) output
|
|
528
578
|
# cell. The mask-aware loop runs only when a mask buffer is supplied
|
|
@@ -537,18 +587,28 @@ OBJECT.each do |type1|
|
|
|
537
587
|
# Ruby literal. Complex keeps its own reader.
|
|
538
588
|
if (INTEGER+FLOAT).include?(type2)
|
|
539
589
|
type_name = "CA_" + ctype2.sub(/_t\z/, "").upcase
|
|
590
|
+
# A number other than an Integer that does not fit an integer type
|
|
591
|
+
# is read as missing (ca_obj_number_fits_integer); an Integer keeps
|
|
592
|
+
# its low bytes.
|
|
593
|
+
fits = INTEGER.include?(type2) ?
|
|
594
|
+
"ca_obj_number_fits_integer(*p1, #{type_name}) && " : ""
|
|
540
595
|
read_ok = "( RB_TYPE_P(*p1, T_STRING) ? " \
|
|
541
596
|
"ca_decimal_string_to(*p1, #{type_name}, p2) : " \
|
|
542
|
-
"( #{conv_ok}(*p1, &tmp) ? ( *p2 = (#{ctype2}) tmp, 1 ) : 0 ) )"
|
|
597
|
+
"( #{fits}#{conv_ok}(*p1, &tmp) ? ( *p2 = (#{ctype2}) tmp, 1 ) : 0 ) )"
|
|
543
598
|
# A store refuses an integer the cell cannot hold
|
|
544
|
-
# (
|
|
599
|
+
# (ca_obj2integer_store); to_type, above, keeps the C conversion.
|
|
545
600
|
store_conv = if INTEGER.include?(type2)
|
|
546
601
|
num = [CA_UINT8, CA_UINT16, CA_UINT32, CA_UINT64].include?(type2) ? "rb_num2ull" : "NUM2LL"
|
|
547
|
-
"#{num}(
|
|
602
|
+
"#{num}(ca_obj2integer_store(a2, *p1, #{type_name}))"
|
|
548
603
|
else
|
|
549
604
|
"#{conv}(*p1)"
|
|
550
605
|
end
|
|
551
|
-
|
|
606
|
+
# The cells of a bit field view read a String through
|
|
607
|
+
# ca_obj2integer_store too (it keeps the value modulo 2 ** bits).
|
|
608
|
+
str_test = INTEGER.include?(type2) ?
|
|
609
|
+
"RB_TYPE_P(*p1, T_STRING) && ! ( a2 && ca_bitfield_beneath(a2) )" :
|
|
610
|
+
"RB_TYPE_P(*p1, T_STRING)"
|
|
611
|
+
read = "if ( #{str_test} ) { " \
|
|
552
612
|
"ca_decimal_string_store(*p1, #{type_name}, p2); } " \
|
|
553
613
|
"else { *p2 = (#{ctype2}) #{store_conv}; }"
|
|
554
614
|
else
|
data/ext/carray_class.c
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
---------------------------------------------------------------------------- */
|
|
7
7
|
|
|
8
8
|
#include "carray.h"
|
|
9
|
-
#include "carray_internal.h" /* CA_CHECK_*_GIVEN */
|
|
10
9
|
|
|
11
10
|
/* ------------------------------------------------------------------- */
|
|
12
11
|
/* Symbol cache for data_type code → Symbol lookup.
|
|
@@ -26,16 +25,15 @@ ID ca_data_type_sym[CA_NTYPE];
|
|
|
26
25
|
VALUE
|
|
27
26
|
rb_ca_data_type_to_sym (int8_t data_type)
|
|
28
27
|
{
|
|
29
|
-
|
|
28
|
+
CA_CHECK_DATA_TYPE(data_type);
|
|
30
29
|
return ID2SYM(ca_data_type_sym[data_type]);
|
|
31
30
|
}
|
|
32
31
|
|
|
33
|
-
/* Returns the
|
|
34
|
-
(0 = CA_LITTLE_ENDIAN, 1 = CA_BIG_ENDIAN). */
|
|
32
|
+
/* Returns the host byte order as :big or :little. */
|
|
35
33
|
static VALUE
|
|
36
34
|
rb_ca_s_endian (VALUE klass)
|
|
37
35
|
{
|
|
38
|
-
return
|
|
36
|
+
return ID2SYM(rb_intern(( ca_endian == CA_BIG_ENDIAN ) ? "big" : "little"));
|
|
39
37
|
}
|
|
40
38
|
|
|
41
39
|
/* Returns true if the host byte order is big-endian. */
|
|
@@ -70,8 +68,8 @@ static VALUE
|
|
|
70
68
|
rb_ca_s_data_type_name (VALUE klass, VALUE type)
|
|
71
69
|
{
|
|
72
70
|
int8_t data_type = rb_ca_guess_type(type);
|
|
73
|
-
|
|
74
|
-
return
|
|
71
|
+
CA_CHECK_DATA_TYPE(data_type);
|
|
72
|
+
return rb_str_new_cstr(ca_type_name[data_type]);
|
|
75
73
|
}
|
|
76
74
|
|
|
77
75
|
/* Returns the internal int8_t data_type code (e.g. 8 for :int64,
|
|
@@ -85,7 +83,7 @@ static VALUE
|
|
|
85
83
|
rb_ca_s_data_type_code (VALUE klass, VALUE type)
|
|
86
84
|
{
|
|
87
85
|
int8_t data_type = rb_ca_guess_type(type);
|
|
88
|
-
|
|
86
|
+
CA_CHECK_DATA_TYPE(data_type);
|
|
89
87
|
return INT2NUM(data_type);
|
|
90
88
|
}
|
|
91
89
|
|
|
@@ -98,9 +96,6 @@ Init_carray_class (void)
|
|
|
98
96
|
Init_carray_ext (ruby_carray.c) before the CA_* data_type constants
|
|
99
97
|
are defined. Don't re-initialize here. */
|
|
100
98
|
|
|
101
|
-
rb_define_const(rb_cObject, "CA_BIG_ENDIAN", INT2NUM(CA_BIG_ENDIAN));
|
|
102
|
-
rb_define_const(rb_cObject, "CA_LITTLE_ENDIAN", INT2NUM(CA_LITTLE_ENDIAN));
|
|
103
|
-
|
|
104
99
|
rb_define_singleton_method(rb_cCArray, "endian", rb_ca_s_endian, 0);
|
|
105
100
|
rb_define_singleton_method(rb_cCArray, "big_endian?",
|
|
106
101
|
rb_ca_s_big_endian_p, 0);
|
data/ext/carray_conversion.c
CHANGED
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
---------------------------------------------------------------------------- */
|
|
13
13
|
|
|
14
14
|
#include "carray.h"
|
|
15
|
-
#include "carray_internal.h" /* ca_attach_window */
|
|
16
15
|
|
|
17
16
|
#include "ruby/io.h"
|
|
18
17
|
#include "ruby/util.h" /* ruby_strtod */
|
|
@@ -133,7 +132,7 @@ to_a_fast_recurse (CArray *ca, int32_t level, const char *base,
|
|
|
133
132
|
slab_bytes = slab_elements * bytes;
|
|
134
133
|
|
|
135
134
|
for (i = 0; i < n; i++) {
|
|
136
|
-
volatile VALUE child =
|
|
135
|
+
volatile VALUE child = rb_ary_new_capa(ca->dim[level + 1]);
|
|
137
136
|
rb_ary_store(dst, i, child);
|
|
138
137
|
to_a_fast_recurse(ca, level + 1,
|
|
139
138
|
base + i * slab_bytes,
|
|
@@ -146,13 +145,13 @@ to_a_fast_recurse (CArray *ca, int32_t level, const char *base,
|
|
|
146
145
|
through the leaf switch is not safe. Walks every cell through
|
|
147
146
|
rb_ca_fetch_index; masked cells return CA_UNDEF via the fetcher. */
|
|
148
147
|
static void
|
|
149
|
-
rb_ca_to_a_loop_universal (VALUE self, int32_t level, ca_size_t *idx, VALUE ary)
|
|
148
|
+
rb_ca_to_a_loop_universal (volatile VALUE self, int32_t level, ca_size_t *idx, VALUE ary)
|
|
150
149
|
{
|
|
151
150
|
volatile VALUE obj;
|
|
152
151
|
CArray *ca;
|
|
153
152
|
ca_size_t i;
|
|
154
153
|
|
|
155
|
-
|
|
154
|
+
GetCArray(self, ca);
|
|
156
155
|
|
|
157
156
|
if ( level == ca->ndim - 1 ) {
|
|
158
157
|
for (i=0; i<ca->dim[level]; i++) {
|
|
@@ -163,7 +162,7 @@ rb_ca_to_a_loop_universal (VALUE self, int32_t level, ca_size_t *idx, VALUE ary)
|
|
|
163
162
|
else {
|
|
164
163
|
for (i=0; i<ca->dim[level]; i++) {
|
|
165
164
|
idx[level] = i;
|
|
166
|
-
obj =
|
|
165
|
+
obj = rb_ary_new_capa(ca->dim[level+1]);
|
|
167
166
|
rb_ca_to_a_loop_universal(self, level+1, idx, obj);
|
|
168
167
|
rb_ary_store(ary, i, obj);
|
|
169
168
|
}
|
|
@@ -180,7 +179,8 @@ rb_ca_to_a_loop_universal (VALUE self, int32_t level, ca_size_t *idx, VALUE ary)
|
|
|
180
179
|
* to the universal fetch_index walk. */
|
|
181
180
|
/* The universal walk reads each cell through the fetch path, which
|
|
182
181
|
decodes -- an object cell, a Face's element -- and can raise, so it runs
|
|
183
|
-
|
|
182
|
+
while the arrays are attached, through ca_attach_window. The fast path
|
|
183
|
+
only copies numbers out. */
|
|
184
184
|
typedef struct {
|
|
185
185
|
VALUE self;
|
|
186
186
|
VALUE ary;
|
|
@@ -195,15 +195,35 @@ ca_to_a_universal (VALUE arg)
|
|
|
195
195
|
return Qnil;
|
|
196
196
|
}
|
|
197
197
|
|
|
198
|
+
/* An array with no cells still has the nesting of its axes up to the first
|
|
199
|
+
zero-length one: (3, 0) is three empty rows, (2, 0, 4) two empty rows. */
|
|
200
|
+
static VALUE
|
|
201
|
+
ca_to_a_empty_nesting (CArray *ca, int32_t level)
|
|
202
|
+
{
|
|
203
|
+
volatile VALUE ary;
|
|
204
|
+
ca_size_t i;
|
|
205
|
+
|
|
206
|
+
ary = rb_ary_new_capa(ca->dim[level]);
|
|
207
|
+
if ( level < ca->ndim - 1 ) {
|
|
208
|
+
for (i=0; i<ca->dim[level]; i++) {
|
|
209
|
+
rb_ary_store(ary, i, ca_to_a_empty_nesting(ca, level + 1));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return ary;
|
|
213
|
+
}
|
|
214
|
+
|
|
198
215
|
VALUE
|
|
199
|
-
rb_ca_to_a (VALUE self)
|
|
216
|
+
rb_ca_to_a (volatile VALUE self)
|
|
200
217
|
{
|
|
201
218
|
volatile VALUE ary;
|
|
202
219
|
CArray *ca;
|
|
203
220
|
int data_type, fast_eligible;
|
|
204
221
|
|
|
205
|
-
|
|
206
|
-
|
|
222
|
+
GetCArray(self, ca);
|
|
223
|
+
if ( ca->elements == 0 && ca->ndim > 0 ) {
|
|
224
|
+
return ca_to_a_empty_nesting(ca, 0);
|
|
225
|
+
}
|
|
226
|
+
ary = rb_ca_is_empty(self) ? rb_ary_new() : rb_ary_new_capa(ca->dim[0]);
|
|
207
227
|
|
|
208
228
|
/* Once attached, ca->ptr is a contig row-major buffer (alias for
|
|
209
229
|
contig views, materialised otherwise) and ca->mask matches, so the
|
|
@@ -230,7 +250,7 @@ rb_ca_to_a (VALUE self)
|
|
|
230
250
|
if ( fast_eligible ) {
|
|
231
251
|
int has_mask;
|
|
232
252
|
const boolean8_t *mask_base;
|
|
233
|
-
ca_attach(ca); /*
|
|
253
|
+
ca_attach(ca); /* attach: nothing raises before the detach */
|
|
234
254
|
has_mask = (ca->mask != NULL);
|
|
235
255
|
mask_base = has_mask ? (const boolean8_t *) ca->mask->ptr : NULL;
|
|
236
256
|
to_a_fast_recurse(ca, 0, ca->ptr, mask_base, ary,
|
|
@@ -251,7 +271,7 @@ rb_ca_to_a (VALUE self)
|
|
|
251
271
|
#undef TO_A_DISPATCH_LEAF
|
|
252
272
|
#undef TO_A_DISPATCH_LEAF_MASKED
|
|
253
273
|
|
|
254
|
-
/* convert yields
|
|
274
|
+
/* convert yields while self and the output are attached, so the loop
|
|
255
275
|
runs through ca_attach_window. An output mask made part way (the first
|
|
256
276
|
UNDEF from the block) is attached inside the loop, and closed by an
|
|
257
277
|
ensure of its own. */
|
|
@@ -303,7 +323,7 @@ ca_convert_loop (VALUE arg)
|
|
|
303
323
|
if ( ret == CA_UNDEF ) {
|
|
304
324
|
if ( ! co->mask ) {
|
|
305
325
|
ca_create_mask(co);
|
|
306
|
-
ca_attach(co->mask); /*
|
|
326
|
+
ca_attach(co->mask); /* attach: closed by the ensure in ca_convert_body */
|
|
307
327
|
c->co_mask_attached = 1;
|
|
308
328
|
dst_mask = (boolean8_t *) co->mask->ptr;
|
|
309
329
|
memset(dst_mask, 0, (size_t) n);
|
|
@@ -344,37 +364,69 @@ ca_convert_body (VALUE arg)
|
|
|
344
364
|
* and the template result are attached symmetrically (R3-compliant) so
|
|
345
365
|
* an overridden #template returning a view stays on the in-ptr path. */
|
|
346
366
|
static VALUE
|
|
347
|
-
rb_ca_convert (int argc, VALUE *argv, VALUE self)
|
|
367
|
+
rb_ca_convert (int argc, VALUE *argv, volatile VALUE self)
|
|
348
368
|
{
|
|
349
369
|
volatile VALUE obj;
|
|
350
370
|
ca_convert_ctx_t c;
|
|
351
371
|
CArray *list[2];
|
|
352
372
|
|
|
373
|
+
/* args: looks at the type only; template reads them all */
|
|
353
374
|
rb_need_block();
|
|
375
|
+
if ( argc >= 1 && RTEST(rb_obj_is_data_class(argv[0])) ) {
|
|
376
|
+
rb_raise(rb_eArgError,
|
|
377
|
+
"convert: a data class (%"PRIsVALUE") is not a data_type of "
|
|
378
|
+
"the cells; convert(:object) and then to_type(%"PRIsVALUE") "
|
|
379
|
+
"makes records", argv[0], argv[0]);
|
|
380
|
+
}
|
|
354
381
|
|
|
355
|
-
|
|
382
|
+
/* template is called as a method, so a Face that overrides it is used */
|
|
383
|
+
obj = rb_funcallv_kw(self, rb_intern("template"), argc, argv,
|
|
384
|
+
RB_PASS_CALLED_KEYWORDS);
|
|
356
385
|
|
|
357
|
-
|
|
358
|
-
|
|
386
|
+
GetCArray(self, c.ca);
|
|
387
|
+
GetCArray(obj, c.co);
|
|
359
388
|
c.self = self;
|
|
360
389
|
c.obj = obj;
|
|
361
390
|
c.has_mask = ca_has_mask(c.ca);
|
|
362
391
|
c.co_mask_attached = 0;
|
|
363
392
|
|
|
364
393
|
/* Attaching an array attaches its mask, so the output's mask is made
|
|
365
|
-
before
|
|
394
|
+
before anything is attached when the source already has one. */
|
|
366
395
|
if ( c.has_mask && ! c.co->mask ) {
|
|
367
396
|
ca_create_mask(c.co);
|
|
368
397
|
}
|
|
369
398
|
|
|
370
399
|
list[0] = c.ca;
|
|
371
400
|
list[1] = c.co;
|
|
372
|
-
ca_attach_window_yielding(
|
|
401
|
+
ca_attach_window_yielding(rb_ary_new_from_args(2, self, obj), 2, list, "rw",
|
|
373
402
|
ca_convert_body, (VALUE) &c);
|
|
374
403
|
|
|
375
404
|
return obj;
|
|
376
405
|
}
|
|
377
406
|
|
|
407
|
+
/* The transfer of dump_binary into a String. The String is locked while
|
|
408
|
+
the bytes go in: a view that runs Ruby to make its cells (a CAObject)
|
|
409
|
+
could otherwise resize it under the pointer being written. */
|
|
410
|
+
typedef struct {
|
|
411
|
+
CArray *ca;
|
|
412
|
+
VALUE io;
|
|
413
|
+
} ca_dump_into_string_t;
|
|
414
|
+
|
|
415
|
+
static VALUE
|
|
416
|
+
ca_dump_into_string_body (VALUE arg)
|
|
417
|
+
{
|
|
418
|
+
ca_dump_into_string_t *d = (ca_dump_into_string_t *) arg;
|
|
419
|
+
/* gc: d lives in the caller's frame, and the caller holds io */
|
|
420
|
+
ca_copy_data(d->ca, RSTRING_PTR(d->io));
|
|
421
|
+
return Qnil;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
static VALUE
|
|
425
|
+
ca_dump_into_string_unlock (VALUE io)
|
|
426
|
+
{
|
|
427
|
+
return rb_str_unlocktmp(io);
|
|
428
|
+
}
|
|
429
|
+
|
|
378
430
|
/* CArray#dump_binary([io]) — write self's raw element bytes to io in
|
|
379
431
|
* row-major order.
|
|
380
432
|
*
|
|
@@ -384,26 +436,20 @@ rb_ca_convert (int argc, VALUE *argv, VALUE self)
|
|
|
384
436
|
* other object must respond to #write. Rejects CA_OBJECT (data is
|
|
385
437
|
* VALUE, not portable). */
|
|
386
438
|
static VALUE
|
|
387
|
-
rb_ca_dump_binary (int argc, VALUE *argv, VALUE self)
|
|
439
|
+
rb_ca_dump_binary (int argc, VALUE *argv, volatile VALUE self)
|
|
388
440
|
{
|
|
389
441
|
volatile VALUE io;
|
|
390
442
|
CArray *ca;
|
|
391
443
|
|
|
392
|
-
|
|
444
|
+
rb_reject_options(rb_pop_options(&argc, &argv));
|
|
445
|
+
rb_check_arity(argc, 0, 1);
|
|
446
|
+
GetCArray(self, ca);
|
|
393
447
|
|
|
394
448
|
if ( ca_is_object_type(ca) ) {
|
|
395
449
|
rb_raise(rb_eCADataTypeError, "don't dump object array");
|
|
396
450
|
}
|
|
397
451
|
|
|
398
|
-
|
|
399
|
-
io = rb_str_new(NULL, 0);
|
|
400
|
-
}
|
|
401
|
-
else if ( argc == 1 ) {
|
|
402
|
-
io = argv[0];
|
|
403
|
-
}
|
|
404
|
-
else {
|
|
405
|
-
rb_raise(rb_eArgError, "invalid # of arguments (%i for 1)", argc);
|
|
406
|
-
}
|
|
452
|
+
io = ( argc == 0 ) ? rb_str_new(NULL, 0) : argv[0];
|
|
407
453
|
|
|
408
454
|
switch ( TYPE(io) ) {
|
|
409
455
|
case T_STRING:
|
|
@@ -411,7 +457,17 @@ rb_ca_dump_binary (int argc, VALUE *argv, VALUE self)
|
|
|
411
457
|
if ( ca_length(ca) != RSTRING_LEN(io) ) {
|
|
412
458
|
rb_str_resize(io, ca_length(ca));
|
|
413
459
|
}
|
|
414
|
-
|
|
460
|
+
/* the bytes are written behind Ruby's back: drop the cached code
|
|
461
|
+
range, or the string keeps answering 7-bit for any contents */
|
|
462
|
+
rb_str_modify(io);
|
|
463
|
+
{
|
|
464
|
+
ca_dump_into_string_t d;
|
|
465
|
+
d.ca = ca;
|
|
466
|
+
d.io = io;
|
|
467
|
+
rb_str_locktmp(io);
|
|
468
|
+
rb_ensure(ca_dump_into_string_body, (VALUE) &d,
|
|
469
|
+
ca_dump_into_string_unlock, io);
|
|
470
|
+
}
|
|
415
471
|
break;
|
|
416
472
|
case T_FILE: {
|
|
417
473
|
rb_io_t *iop;
|
|
@@ -453,11 +509,13 @@ rb_ca_dump_binary (int argc, VALUE *argv, VALUE self)
|
|
|
453
509
|
return io;
|
|
454
510
|
}
|
|
455
511
|
|
|
456
|
-
/* CArray#to_s —
|
|
512
|
+
/* CArray#to_s — the same text as inspect. Ruby calls to_s wherever it puts
|
|
513
|
+
* an object into text (interpolation, puts, error messages), so it is the
|
|
514
|
+
* readable form; the element bytes are dump_binary's. */
|
|
457
515
|
static VALUE
|
|
458
516
|
rb_ca_to_s (VALUE self)
|
|
459
517
|
{
|
|
460
|
-
return
|
|
518
|
+
return rb_inspect(self);
|
|
461
519
|
}
|
|
462
520
|
|
|
463
521
|
/* CArray#load_binary(io) — overwrite self's element bytes with ca_length
|
|
@@ -469,12 +527,12 @@ rb_ca_to_s (VALUE self)
|
|
|
469
527
|
* targets buffer the full read and recurse via T_STRING because
|
|
470
528
|
* ca_sync_data scatters in one pass. */
|
|
471
529
|
static VALUE
|
|
472
|
-
rb_ca_load_binary (VALUE self, VALUE io)
|
|
530
|
+
rb_ca_load_binary (volatile VALUE self, volatile VALUE io)
|
|
473
531
|
{
|
|
474
532
|
CArray *ca;
|
|
475
533
|
|
|
476
534
|
rb_ca_modify(self);
|
|
477
|
-
|
|
535
|
+
GetCArray(self, ca);
|
|
478
536
|
|
|
479
537
|
if ( ca_is_object_type(ca) ) {
|
|
480
538
|
rb_raise(rb_eCADataTypeError, "don't load object array");
|
|
@@ -487,13 +545,14 @@ rb_ca_load_binary (VALUE self, VALUE io)
|
|
|
487
545
|
"%s: data size mismatch (got %" PRId64 " bytes, expected %" PRId64 ")",
|
|
488
546
|
ca_calling_method_name(), (ca_size_t) RSTRING_LEN(io), (ca_size_t) ca_length(ca));
|
|
489
547
|
}
|
|
548
|
+
/* A copy shares the bytes until either side changes: a view that runs
|
|
549
|
+
Ruby while it stores (a CAObject) cannot then move them. */
|
|
550
|
+
io = rb_str_dup(io);
|
|
490
551
|
if ( ca->data_type == CA_BOOLEAN ) {
|
|
491
|
-
io = rb_str_dup(io);
|
|
492
552
|
rb_str_modify(io);
|
|
493
553
|
ca_boolean_normalize_bytes(RSTRING_PTR(io), ca->elements);
|
|
494
554
|
}
|
|
495
555
|
ca_sync_data(ca, RSTRING_PTR(io));
|
|
496
|
-
RB_GC_GUARD(io);
|
|
497
556
|
return self;
|
|
498
557
|
break;
|
|
499
558
|
default:
|
|
@@ -505,10 +564,10 @@ rb_ca_load_binary (VALUE self, VALUE io)
|
|
|
505
564
|
const ca_size_t CHUNK = 65536;
|
|
506
565
|
volatile VALUE scratch = rb_str_buf_new(CHUNK < total ? CHUNK : total);
|
|
507
566
|
ca_size_t off = 0;
|
|
508
|
-
ca_allocate(ca); /*
|
|
567
|
+
ca_allocate(ca); /* attach: an entity, whose allocate holds nothing */
|
|
509
568
|
while ( off < total ) {
|
|
510
569
|
ca_size_t want = (total - off < CHUNK) ? (total - off) : CHUNK;
|
|
511
|
-
VALUE r = rb_funcall(io, id_read, 2, SIZE2NUM(want), scratch);
|
|
570
|
+
volatile VALUE r = rb_funcall(io, id_read, 2, SIZE2NUM(want), scratch);
|
|
512
571
|
if ( NIL_P(r) || RSTRING_LEN(r) != want ) {
|
|
513
572
|
ca_detach(ca);
|
|
514
573
|
rb_raise(rb_eArgError,
|
|
@@ -589,7 +648,7 @@ ca_double_of_shortest_decimal_of_float32 (float x)
|
|
|
589
648
|
double its shortest decimal reads as, for to_csv to write that decimal.
|
|
590
649
|
The mask is kept. */
|
|
591
650
|
static VALUE
|
|
592
|
-
rb_ca_widen_keeping_shortest_decimal (VALUE self)
|
|
651
|
+
rb_ca_widen_keeping_shortest_decimal (volatile VALUE self)
|
|
593
652
|
{
|
|
594
653
|
volatile VALUE out, holder = 0;
|
|
595
654
|
CArray *ca, *co;
|
data/ext/carray_copy.c
CHANGED
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
|
|
11
11
|
#include "carray.h"
|
|
12
12
|
#include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE */
|
|
13
|
-
#include "carray_internal.h" /* ca_gc_hold_push / ca_gc_hold_pop_to */
|
|
14
13
|
#include "ca_sweep_engine.h" /* ca_sweep_same_shape / ca_sweep_refuse_shapes */
|
|
15
14
|
#include <stdarg.h>
|
|
16
15
|
|
|
@@ -61,6 +60,30 @@ ca_fill_or_detach (int n, CArray **list, VALUE (*fill)(VALUE), VALUE arg)
|
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
62
|
|
|
63
|
+
typedef struct {
|
|
64
|
+
CArray **list;
|
|
65
|
+
int32_t n;
|
|
66
|
+
} ca_attach_list_arg;
|
|
67
|
+
|
|
68
|
+
static VALUE
|
|
69
|
+
ca_attach_list_fill (VALUE arg)
|
|
70
|
+
{
|
|
71
|
+
ca_attach_list_arg *a = (ca_attach_list_arg *) arg;
|
|
72
|
+
ca_attach_all(a->list, a->n);
|
|
73
|
+
return Qnil;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
char *
|
|
77
|
+
ca_alloc_cells_then_attach (CArray *ca, int32_t n, CArray **parents)
|
|
78
|
+
{
|
|
79
|
+
char *buf = ca_alloc_cells(ca);
|
|
80
|
+
ca_attach_list_arg a;
|
|
81
|
+
a.list = parents;
|
|
82
|
+
a.n = n;
|
|
83
|
+
ca_fill_or_xfree(buf, ca_attach_list_fill, (VALUE) &a);
|
|
84
|
+
return buf;
|
|
85
|
+
}
|
|
86
|
+
|
|
64
87
|
VALUE
|
|
65
88
|
ca_view_inherit_mask (VALUE view)
|
|
66
89
|
{
|
|
@@ -135,12 +158,12 @@ ca_copy (void *ap)
|
|
|
135
158
|
* Face (e.g. CARecord), lifts the wrapped result so the derived class
|
|
136
159
|
* identity is preserved on the copy. The copy keeps self's attributes. */
|
|
137
160
|
VALUE
|
|
138
|
-
rb_ca_copy (VALUE self)
|
|
161
|
+
rb_ca_copy (volatile VALUE self)
|
|
139
162
|
{
|
|
140
163
|
volatile VALUE obj;
|
|
141
164
|
CArray *ca, *co;
|
|
142
165
|
int guard = -1;
|
|
143
|
-
|
|
166
|
+
GetCArray(self, ca);
|
|
144
167
|
co = ca_copy(ca);
|
|
145
168
|
/* The copy holds VALUEs that so far exist only in co->ptr, and
|
|
146
169
|
ca_wrap_struct allocates. Keep them reachable until the wrapper
|
|
@@ -166,17 +189,11 @@ rb_ca_copy (VALUE self)
|
|
|
166
189
|
int
|
|
167
190
|
ca_to_ca_writable_arg (int argc, VALUE *argv)
|
|
168
191
|
{
|
|
169
|
-
VALUE opts;
|
|
170
|
-
ID kw_ids[1];
|
|
171
|
-
VALUE kw_vals[1];
|
|
192
|
+
VALUE opts, vwritable = Qundef;
|
|
172
193
|
|
|
173
194
|
rb_scan_args(argc, argv, "0:", &opts);
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
kw_ids[0] = rb_intern("writable");
|
|
178
|
-
rb_get_kwargs(opts, kw_ids, 0, 1, kw_vals);
|
|
179
|
-
return ( kw_vals[0] == Qundef ) ? 0 : RTEST(kw_vals[0]);
|
|
195
|
+
rb_scan_options(opts, "writable", &vwritable);
|
|
196
|
+
return ( vwritable == Qundef ) ? 0 : ca_bool_arg(vwritable, "writable");
|
|
180
197
|
}
|
|
181
198
|
|
|
182
199
|
/* Refusal an implementation raises when `writable: true` was asked of a
|
|
@@ -203,11 +220,11 @@ ca_to_ca_refuse_writable (VALUE self)
|
|
|
203
220
|
* entity; both are copies and so refuse `writable: true`. When an
|
|
204
221
|
* independent owned copy is required, callers use `copy`. */
|
|
205
222
|
VALUE
|
|
206
|
-
rb_ca_to_ca (int argc, VALUE *argv, VALUE self)
|
|
223
|
+
rb_ca_to_ca (int argc, VALUE *argv, volatile VALUE self)
|
|
207
224
|
{
|
|
208
225
|
if ( ca_to_ca_writable_arg(argc, argv) ) {
|
|
209
226
|
CArray *ca;
|
|
210
|
-
|
|
227
|
+
GetCArray(self, ca);
|
|
211
228
|
if ( ca_is_readonly(ca) ) {
|
|
212
229
|
ca_refuse_read_only();
|
|
213
230
|
}
|
|
@@ -256,7 +273,7 @@ CArray *
|
|
|
256
273
|
ca_template_safe2 (void *ap, int8_t data_type, ca_size_t bytes)
|
|
257
274
|
{
|
|
258
275
|
CArray *ca = (CArray *) ap;
|
|
259
|
-
|
|
276
|
+
CA_CHECK_DATA_TYPE(data_type);
|
|
260
277
|
if ( ca_is_scalar(ca) ) {
|
|
261
278
|
return (CArray*) cscalar_new(data_type, bytes, NULL);
|
|
262
279
|
}
|
|
@@ -275,23 +292,29 @@ ca_template_safe2 (void *ap, int8_t data_type, ca_size_t bytes)
|
|
|
275
292
|
* The arity dispatch matches `CArray.new` (ca_obj_array.c) so the two
|
|
276
293
|
* factories behave the same for block-driven initialisation. */
|
|
277
294
|
static VALUE
|
|
278
|
-
rb_ca_template_kw (int argc, VALUE *argv, VALUE ropt, VALUE self
|
|
295
|
+
rb_ca_template_kw (int argc, VALUE *argv, VALUE ropt, volatile VALUE self,
|
|
296
|
+
int use_block)
|
|
279
297
|
{
|
|
280
|
-
volatile VALUE obj
|
|
298
|
+
volatile VALUE obj;
|
|
299
|
+
VALUE rtype, rbytes = Qnil;
|
|
281
300
|
CArray *ca, *co;
|
|
282
301
|
int8_t data_type;
|
|
283
302
|
ca_size_t bytes;
|
|
284
303
|
|
|
285
|
-
rb_scan_args(argc, argv, "01",
|
|
304
|
+
rb_scan_args(argc, argv, "01", &rtype);
|
|
286
305
|
rb_scan_options(ropt, "bytes", &rbytes);
|
|
287
306
|
|
|
288
|
-
|
|
307
|
+
GetCArray(self, ca);
|
|
289
308
|
|
|
290
309
|
if ( NIL_P(rtype) ) {
|
|
291
310
|
co = ca_template_safe(ca);
|
|
292
311
|
obj = ca_wrap_struct(co);
|
|
293
312
|
}
|
|
294
313
|
else {
|
|
314
|
+
if ( use_block ) { /* called from Ruby */
|
|
315
|
+
ca_refuse_type_number(rtype);
|
|
316
|
+
ca_refuse_data_class(rtype, "template");
|
|
317
|
+
}
|
|
295
318
|
rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
|
|
296
319
|
co = ca_template_safe2(ca, data_type, bytes);
|
|
297
320
|
obj = ca_wrap_struct(co);
|
|
@@ -310,7 +333,10 @@ rb_ca_template_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
|
|
|
310
333
|
CA_FACE_LIFT_IF_FACE(obj, self, ca);
|
|
311
334
|
}
|
|
312
335
|
|
|
313
|
-
|
|
336
|
+
/* Only the Ruby method `template` reads a block. The C helpers below
|
|
337
|
+
are called from inside other methods, where rb_block_given_p sees the
|
|
338
|
+
caller's block (`a.sort { ... }` would fill sort's scratch with it). */
|
|
339
|
+
if ( use_block && rb_block_given_p() ) {
|
|
314
340
|
if ( rb_proc_arity(rb_block_proc()) == 0 ) {
|
|
315
341
|
volatile VALUE rval = rb_yield_values2(0, NULL);
|
|
316
342
|
if ( rval != self ) {
|
|
@@ -319,10 +345,10 @@ rb_ca_template_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
|
|
|
319
345
|
}
|
|
320
346
|
else {
|
|
321
347
|
CArray *co;
|
|
322
|
-
|
|
348
|
+
GetCArray(obj, co);
|
|
323
349
|
if ( co->ndim > 0 ) {
|
|
324
350
|
ca_size_t idx[CA_RANK_MAX];
|
|
325
|
-
volatile VALUE ridx =
|
|
351
|
+
volatile VALUE ridx = rb_ary_new_capa(co->ndim);
|
|
326
352
|
rb_ca_index_walk(obj, co, 0, idx, ridx, CA_LOOP_STORE);
|
|
327
353
|
}
|
|
328
354
|
else {
|
|
@@ -339,13 +365,13 @@ static VALUE
|
|
|
339
365
|
rb_ca_template_method (int argc, VALUE *argv, VALUE self)
|
|
340
366
|
{
|
|
341
367
|
volatile VALUE ropt = rb_pop_options(&argc, &argv);
|
|
342
|
-
return rb_ca_template_kw(argc, argv, ropt, self);
|
|
368
|
+
return rb_ca_template_kw(argc, argv, ropt, self, 1);
|
|
343
369
|
}
|
|
344
370
|
|
|
345
371
|
VALUE
|
|
346
372
|
rb_ca_template (VALUE self)
|
|
347
373
|
{
|
|
348
|
-
return
|
|
374
|
+
return rb_ca_template_kw(0, NULL, Qnil, self, 0);
|
|
349
375
|
}
|
|
350
376
|
|
|
351
377
|
VALUE
|
|
@@ -353,7 +379,7 @@ rb_ca_template_with_type (VALUE self, VALUE rtype, VALUE rbytes)
|
|
|
353
379
|
{
|
|
354
380
|
volatile VALUE ropt = rb_hash_new();
|
|
355
381
|
rb_set_options(ropt, "bytes", rbytes);
|
|
356
|
-
return rb_ca_template_kw(1, &rtype, ropt, self);
|
|
382
|
+
return rb_ca_template_kw(1, &rtype, ropt, self, 0);
|
|
357
383
|
}
|
|
358
384
|
|
|
359
385
|
/* Returns `template` of the first non-scalar among `n` variadic CArray
|
|
@@ -376,7 +402,7 @@ rb_ca_template_n (int n, ...)
|
|
|
376
402
|
va_end(vargs);
|
|
377
403
|
rb_raise(rb_eRuntimeError, "[BUG] not-carray object given to rb_ca_template_n");
|
|
378
404
|
}
|
|
379
|
-
|
|
405
|
+
GetCArray(varg, ca);
|
|
380
406
|
if ( i == 0 ) {
|
|
381
407
|
obj = varg;
|
|
382
408
|
}
|