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/ca_obj_const_string.c
CHANGED
|
@@ -32,23 +32,13 @@
|
|
|
32
32
|
|
|
33
33
|
#include "carray.h"
|
|
34
34
|
#include "ruby/util.h"
|
|
35
|
-
#include "carray_internal.h"
|
|
36
35
|
#include "ca_obj_face.h"
|
|
37
36
|
#include <ruby/encoding.h>
|
|
38
37
|
|
|
39
38
|
/* === struct layout (identical to the CATime prefix + tail) === */
|
|
40
39
|
typedef struct {
|
|
41
40
|
/* === CAView prefix === */
|
|
42
|
-
|
|
43
|
-
int8_t data_type; /* CA_FIXLEN surface (numeric gate); storage is fixlen-16 */
|
|
44
|
-
int8_t ndim;
|
|
45
|
-
int32_t flags; /* CA_FLAG_IS_FACE set */
|
|
46
|
-
ca_size_t bytes; /* 2 * sizeof(int64_t) = 16 (one (start,end) pair per element) */
|
|
47
|
-
ca_size_t elements;
|
|
48
|
-
ca_size_t *dim;
|
|
49
|
-
char *ptr;
|
|
50
|
-
CArray *mask;
|
|
51
|
-
char *_pool; /* framework-managed pool buffer (NULL = legacy ALLOC_N path). */
|
|
41
|
+
CA_ARRAY_HEADER
|
|
52
42
|
CArray *parent; /* fixlen-16 (start,end) pair entity (offset-source) */
|
|
53
43
|
uint32_t attach;
|
|
54
44
|
uint8_t nosync;
|
|
@@ -104,7 +94,7 @@ const rb_data_type_t catext_data_type = {
|
|
|
104
94
|
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
105
95
|
};
|
|
106
96
|
|
|
107
|
-
static
|
|
97
|
+
static int16_t CA_OBJ_CONST_STRING;
|
|
108
98
|
static VALUE rb_cCAConstString;
|
|
109
99
|
|
|
110
100
|
/* yard:
|
|
@@ -236,6 +226,7 @@ ca_operation_function_t ca_const_string_func = {
|
|
|
236
226
|
static VALUE
|
|
237
227
|
ca_const_string_decode (CAConstString *ca, int64_t start, int64_t end)
|
|
238
228
|
{
|
|
229
|
+
/* gc: the CAConstString marks (and so pins) its buffer */
|
|
239
230
|
const char *buf = RSTRING_PTR(ca->buffer);
|
|
240
231
|
long buflen = RSTRING_LEN(ca->buffer);
|
|
241
232
|
int64_t len = end - start;
|
|
@@ -256,7 +247,7 @@ ca_const_string_decode (CAConstString *ca, int64_t start, int64_t end)
|
|
|
256
247
|
16-byte raw String = the (start,end) int64 pair. Decode the buffer slice
|
|
257
248
|
→ frozen Ruby String (plain frozen String, no Scalar wrapper). */
|
|
258
249
|
static VALUE
|
|
259
|
-
rb_ca_const_string_storage_to_element (VALUE self, VALUE raw)
|
|
250
|
+
rb_ca_const_string_storage_to_element (volatile VALUE self, volatile VALUE raw)
|
|
260
251
|
{
|
|
261
252
|
CAConstString *ca;
|
|
262
253
|
int64_t pair[2];
|
|
@@ -278,13 +269,13 @@ rb_ca_const_string_storage_to_element (VALUE self, VALUE raw)
|
|
|
278
269
|
/* CAConstString.wrap(int64_offsets, buffer:, encoding:) — low-level Face wrap of an
|
|
279
270
|
existing fixlen-16 (start,end) pair entity + pure-concatenation buffer. */
|
|
280
271
|
static VALUE
|
|
281
|
-
rb_ca_const_string_wrap (VALUE parent_val, VALUE buffer, int encoding_id)
|
|
272
|
+
rb_ca_const_string_wrap (volatile VALUE parent_val, VALUE buffer, int encoding_id)
|
|
282
273
|
{
|
|
283
274
|
CArray *parent;
|
|
284
275
|
CAConstString *ca;
|
|
285
276
|
VALUE obj;
|
|
286
277
|
|
|
287
|
-
|
|
278
|
+
GetCArray(parent_val, parent);
|
|
288
279
|
if ( parent->data_type != CA_FIXLEN || parent->bytes != 2 * (ca_size_t) sizeof(int64_t) ) {
|
|
289
280
|
rb_raise(rb_eTypeError,
|
|
290
281
|
"CAConstString.wrap requires a fixlen-16 (start,end) pair entity "
|
|
@@ -313,7 +304,7 @@ rb_ca_const_string_wrap (VALUE parent_val, VALUE buffer, int encoding_id)
|
|
|
313
304
|
boolean8_t *m;
|
|
314
305
|
|
|
315
306
|
ca_update_mask(parent);
|
|
316
|
-
ca_attach(parent); /*
|
|
307
|
+
ca_attach(parent); /* attach: detached before its one raise */
|
|
317
308
|
n = parent->elements;
|
|
318
309
|
pair = (int64_t *) parent->ptr;
|
|
319
310
|
m = parent->mask ? (boolean8_t *) parent->mask->ptr : NULL;
|
|
@@ -372,7 +363,7 @@ rb_ca_const_string_wrap_method (int argc, VALUE *argv, VALUE klass)
|
|
|
372
363
|
per-element Ruby build loop -- no `String#b` copy, no `pack`, no per-row
|
|
373
364
|
method dispatch. `renc` is the target Encoding (nil = UTF-8). */
|
|
374
365
|
static VALUE
|
|
375
|
-
rb_ca_const_string_build (VALUE klass, VALUE values, VALUE renc)
|
|
366
|
+
rb_ca_const_string_build (VALUE klass, volatile VALUE values, VALUE renc)
|
|
376
367
|
{
|
|
377
368
|
long i, n;
|
|
378
369
|
int encoding_id;
|
|
@@ -385,13 +376,15 @@ rb_ca_const_string_build (VALUE klass, VALUE values, VALUE renc)
|
|
|
385
376
|
long hint = 0;
|
|
386
377
|
|
|
387
378
|
Check_Type(values, T_ARRAY);
|
|
379
|
+
/* A to_s below can change the caller's Array; read a copy of it. */
|
|
380
|
+
values = rb_ary_dup(values);
|
|
388
381
|
n = RARRAY_LEN(values);
|
|
389
382
|
encoding_id = NIL_P(renc) ? rb_utf8_encindex() : rb_to_encoding_index(renc);
|
|
390
383
|
enc = rb_enc_from_index(encoding_id);
|
|
391
384
|
|
|
392
385
|
/* size hint: sum String lengths cheaply (skip non-String coercion here) */
|
|
393
386
|
for ( i = 0; i < n; i++ ) {
|
|
394
|
-
VALUE s = RARRAY_AREF(values, i);
|
|
387
|
+
volatile VALUE s = RARRAY_AREF(values, i);
|
|
395
388
|
if ( RB_TYPE_P(s, T_STRING) ) hint += RSTRING_LEN(s);
|
|
396
389
|
}
|
|
397
390
|
buffer = rb_str_buf_new(hint);
|
|
@@ -399,11 +392,11 @@ rb_ca_const_string_build (VALUE klass, VALUE values, VALUE renc)
|
|
|
399
392
|
|
|
400
393
|
dim[0] = (ca_size_t) n;
|
|
401
394
|
vpair = rb_carray_new(CA_FIXLEN, 1, dim, 2 * sizeof(int64_t), NULL);
|
|
402
|
-
|
|
395
|
+
GetCArray(vpair, pe);
|
|
403
396
|
pair = (int64_t *) pe->ptr;
|
|
404
397
|
|
|
405
398
|
for ( i = 0; i < n; i++ ) {
|
|
406
|
-
VALUE s = RARRAY_AREF(values, i);
|
|
399
|
+
volatile VALUE s = RARRAY_AREF(values, i);
|
|
407
400
|
if ( NIL_P(s) ) {
|
|
408
401
|
if ( ! mask ) {
|
|
409
402
|
ca_create_mask(pe);
|
|
@@ -434,7 +427,7 @@ rb_ca_const_string_build (VALUE klass, VALUE values, VALUE renc)
|
|
|
434
427
|
|
|
435
428
|
/* CAConstString#encoding → Encoding */
|
|
436
429
|
static VALUE
|
|
437
|
-
rb_ca_const_string_encoding (VALUE self)
|
|
430
|
+
rb_ca_const_string_encoding (volatile VALUE self)
|
|
438
431
|
{
|
|
439
432
|
CAConstString *ca;
|
|
440
433
|
TypedData_Get_Struct(self, CAConstString, &catext_data_type, ca);
|
|
@@ -449,7 +442,7 @@ rb_ca_const_string_encoding (VALUE self)
|
|
|
449
442
|
element; `wrap` is where a hand-built pair array is checked against a
|
|
450
443
|
buffer. */
|
|
451
444
|
static VALUE
|
|
452
|
-
rb_ca_const_string_buffer (VALUE self)
|
|
445
|
+
rb_ca_const_string_buffer (volatile VALUE self)
|
|
453
446
|
{
|
|
454
447
|
CAConstString *ca;
|
|
455
448
|
TypedData_Get_Struct(self, CAConstString, &catext_data_type, ca);
|
|
@@ -461,7 +454,7 @@ rb_ca_const_string_buffer (VALUE self)
|
|
|
461
454
|
/* to_string: the cells as a CAString over an object array of Strings, in
|
|
462
455
|
one pass over the buffer (shape and mask carried). */
|
|
463
456
|
static VALUE
|
|
464
|
-
rb_ca_const_string_to_string (VALUE self)
|
|
457
|
+
rb_ca_const_string_to_string (volatile VALUE self)
|
|
465
458
|
{
|
|
466
459
|
CAConstString *ca;
|
|
467
460
|
volatile VALUE vpairs, out;
|
|
@@ -475,19 +468,20 @@ rb_ca_const_string_to_string (VALUE self)
|
|
|
475
468
|
|
|
476
469
|
TypedData_Get_Struct(self, CAConstString, &catext_data_type, ca);
|
|
477
470
|
vpairs = rb_ca_parent(self);
|
|
478
|
-
|
|
471
|
+
GetCArray(vpairs, pe);
|
|
479
472
|
if ( ! ca_is_entity(pe) ) {
|
|
480
473
|
vpairs = rb_ca_copy(vpairs);
|
|
481
|
-
|
|
474
|
+
GetCArray(vpairs, pe);
|
|
482
475
|
}
|
|
483
476
|
enc = rb_enc_from_index(ca->encoding_id);
|
|
484
477
|
out = rb_carray_new(CA_OBJECT, pe->ndim, pe->dim, 0, NULL);
|
|
485
|
-
|
|
478
|
+
GetCArray(out, co);
|
|
486
479
|
m = ca_has_mask(pe) ? (boolean8_t *) pe->mask->ptr : NULL;
|
|
487
480
|
if ( m ) {
|
|
488
481
|
ca_create_mask(co);
|
|
489
482
|
memcpy(co->mask->ptr, m, pe->elements);
|
|
490
483
|
}
|
|
484
|
+
/* gc: the CAConstString marks (and so pins) its buffer */
|
|
491
485
|
buf = RSTRING_PTR(ca->buffer);
|
|
492
486
|
pair = (const int64_t *) pe->ptr;
|
|
493
487
|
cells = (VALUE *) co->ptr;
|
|
@@ -500,12 +494,82 @@ rb_ca_const_string_to_string (VALUE self)
|
|
|
500
494
|
cells[i] = rb_enc_str_new(buf + pair[2 * i],
|
|
501
495
|
(long) ( pair[2 * i + 1] - pair[2 * i] ), enc);
|
|
502
496
|
}
|
|
503
|
-
RB_GC_GUARD(vpairs);
|
|
504
|
-
RB_GC_GUARD(self);
|
|
505
497
|
return rb_funcall(rb_const_get(rb_cObject, rb_intern("CAString")),
|
|
506
498
|
rb_intern("wrap"), 1, out);
|
|
507
499
|
}
|
|
508
500
|
|
|
501
|
+
/* A scan reads the (start,end) pairs and the bytes of a column. */
|
|
502
|
+
typedef struct {
|
|
503
|
+
CAConstString *ca;
|
|
504
|
+
int64_t *off;
|
|
505
|
+
boolean8_t *m;
|
|
506
|
+
const char *buf;
|
|
507
|
+
long buflen;
|
|
508
|
+
ca_size_t n;
|
|
509
|
+
} ca_const_string_scan_t;
|
|
510
|
+
|
|
511
|
+
/* What a scan's body runs while the columns are attached. s holds one
|
|
512
|
+
scan, or two for an operation on a pair of arrays. */
|
|
513
|
+
typedef VALUE (*ca_const_string_scan_body_t) (ca_const_string_scan_t *s, void *arg);
|
|
514
|
+
|
|
515
|
+
typedef struct {
|
|
516
|
+
int32_t n;
|
|
517
|
+
ca_const_string_scan_t *s;
|
|
518
|
+
ca_const_string_scan_body_t body;
|
|
519
|
+
void *arg;
|
|
520
|
+
} ca_const_string_run_t;
|
|
521
|
+
|
|
522
|
+
static VALUE
|
|
523
|
+
ca_const_string_run_body (VALUE varg)
|
|
524
|
+
{
|
|
525
|
+
ca_const_string_run_t *r = (ca_const_string_run_t *) varg;
|
|
526
|
+
int32_t k;
|
|
527
|
+
for ( k = 0; k < r->n; k++ ) {
|
|
528
|
+
ca_const_string_scan_t *s = &r->s[k];
|
|
529
|
+
s->off = (int64_t *) s->ca->ptr;
|
|
530
|
+
s->m = s->ca->mask ? (boolean8_t *) s->ca->mask->ptr : NULL;
|
|
531
|
+
/* gc: the CAConstString marks (and so pins) its buffer */
|
|
532
|
+
s->buf = RSTRING_PTR(s->ca->buffer);
|
|
533
|
+
s->buflen = RSTRING_LEN(s->ca->buffer);
|
|
534
|
+
s->n = s->ca->elements;
|
|
535
|
+
}
|
|
536
|
+
return r->body(r->s, r->arg);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/* Runs body(s, arg) with the offsets of the n (1 or 2) columns in cols
|
|
540
|
+
attached, s[k] scanning cols[k], and detaches them however body
|
|
541
|
+
leaves: what body allocates can fail, and body may raise. */
|
|
542
|
+
static VALUE
|
|
543
|
+
ca_const_string_scan_cols (int32_t n, const VALUE *cols,
|
|
544
|
+
ca_const_string_scan_t *s,
|
|
545
|
+
ca_const_string_scan_body_t body, void *arg)
|
|
546
|
+
{
|
|
547
|
+
ca_const_string_run_t r;
|
|
548
|
+
CArray *list[2];
|
|
549
|
+
int32_t k;
|
|
550
|
+
|
|
551
|
+
r.n = n;
|
|
552
|
+
r.s = s;
|
|
553
|
+
r.body = body;
|
|
554
|
+
r.arg = arg;
|
|
555
|
+
for ( k = 0; k < n; k++ ) {
|
|
556
|
+
/* gc: the caller holds the columns */
|
|
557
|
+
TypedData_Get_Struct(cols[k], CAConstString, &catext_data_type, s[k].ca);
|
|
558
|
+
ca_update_mask((CArray *) s[k].ca);
|
|
559
|
+
list[k] = (CArray *) s[k].ca;
|
|
560
|
+
}
|
|
561
|
+
return ca_attach_window(n, list, n == 2 ? "rr" : "r",
|
|
562
|
+
ca_const_string_run_body, (VALUE) &r);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/* The scan of one column. */
|
|
566
|
+
static VALUE
|
|
567
|
+
ca_const_string_scan_run (VALUE self, ca_const_string_scan_t *s,
|
|
568
|
+
ca_const_string_scan_body_t body, void *arg)
|
|
569
|
+
{
|
|
570
|
+
return ca_const_string_scan_cols(1, &self, s, body, arg);
|
|
571
|
+
}
|
|
572
|
+
|
|
509
573
|
/* copy = compacting deep copy — the only chain-descent point in
|
|
510
574
|
CAConstString. Produces a standalone CAConstString:
|
|
511
575
|
- a fresh contiguous fixlen-16 (start,end) pair entity
|
|
@@ -515,73 +579,77 @@ rb_ca_const_string_to_string (VALUE self)
|
|
|
515
579
|
- fully detached from the original buffer / pair-source
|
|
516
580
|
Cost is O(live bytes); a near-full copy is a near-full rebuild (accepted).
|
|
517
581
|
Done at the byte level (no per-element Ruby String boxing). */
|
|
582
|
+
typedef struct {
|
|
583
|
+
volatile VALUE pairs; /* the new (start,end) pair entity */
|
|
584
|
+
volatile VALUE buffer; /* the compacted bytes */
|
|
585
|
+
} ca_const_string_copy_t;
|
|
586
|
+
|
|
587
|
+
/* Repacks each live record's bytes into a new buffer in the view's order,
|
|
588
|
+
writing the new pairs and carrying the mask. */
|
|
518
589
|
static VALUE
|
|
519
|
-
|
|
590
|
+
ca_const_string_copy_body (ca_const_string_scan_t *scan, void *arg)
|
|
520
591
|
{
|
|
521
|
-
|
|
522
|
-
|
|
592
|
+
ca_const_string_scan_t s0 = *scan, *s = &s0; /* a local copy: the writes below cannot alias it */
|
|
593
|
+
ca_const_string_copy_t *c = (ca_const_string_copy_t *) arg;
|
|
523
594
|
CArray *co;
|
|
524
|
-
ca_size_t i
|
|
525
|
-
int64_t *
|
|
526
|
-
boolean8_t *m = NULL;
|
|
527
|
-
const char *oldbuf;
|
|
528
|
-
long oldbuflen;
|
|
529
|
-
|
|
530
|
-
TypedData_Get_Struct(self, CAConstString, &catext_data_type, ca);
|
|
531
|
-
|
|
532
|
-
/* materialize logical-order (start,end) pairs + mask */
|
|
533
|
-
ca_update_mask((CArray *) ca);
|
|
534
|
-
ca_attach((CArray *) ca); /* window: detached before its one raise */
|
|
535
|
-
|
|
536
|
-
n = ca->elements;
|
|
537
|
-
src_pair = (int64_t *) ca->ptr;
|
|
538
|
-
m = ca->mask ? (boolean8_t *) ca->mask->ptr : NULL;
|
|
539
|
-
oldbuf = RSTRING_PTR(ca->buffer);
|
|
540
|
-
oldbuflen = RSTRING_LEN(ca->buffer);
|
|
595
|
+
ca_size_t i;
|
|
596
|
+
int64_t *dst_pair;
|
|
541
597
|
|
|
542
598
|
/* fresh fixlen-16 pair entity, same shape (N-D preserved) */
|
|
543
|
-
|
|
544
|
-
|
|
599
|
+
c->pairs = rb_carray_new(CA_FIXLEN, s->ca->ndim, s->ca->dim,
|
|
600
|
+
2 * sizeof(int64_t), NULL);
|
|
601
|
+
/* gc: c lives in the caller's frame, and its fields are volatile */
|
|
602
|
+
GetCArray(c->pairs, co);
|
|
545
603
|
dst_pair = (int64_t *) co->ptr;
|
|
546
604
|
|
|
547
605
|
/* growable compacted buffer (BINARY) */
|
|
548
|
-
|
|
549
|
-
rb_enc_associate_index(
|
|
606
|
+
c->buffer = rb_str_buf_new(s->buflen);
|
|
607
|
+
rb_enc_associate_index(c->buffer, rb_ascii8bit_encindex());
|
|
550
608
|
|
|
551
|
-
for ( i = 0; i < n; i++ ) {
|
|
609
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
552
610
|
int64_t start, end, len;
|
|
553
|
-
if ( m && m[i] ) {
|
|
611
|
+
if ( s->m && s->m[i] ) {
|
|
554
612
|
dst_pair[2 * i] = 0;
|
|
555
613
|
dst_pair[2 * i + 1] = 0;
|
|
556
614
|
continue;
|
|
557
615
|
}
|
|
558
|
-
start =
|
|
559
|
-
end =
|
|
616
|
+
start = s->off[2 * i];
|
|
617
|
+
end = s->off[2 * i + 1];
|
|
560
618
|
len = end - start;
|
|
561
|
-
if ( start < 0 || len < 0 || end > (int64_t)
|
|
562
|
-
ca_detach((CArray *) ca);
|
|
619
|
+
if ( start < 0 || len < 0 || end > (int64_t) s->buflen ) {
|
|
563
620
|
rb_raise(rb_eIndexError,
|
|
564
621
|
"CAConstString#copy: record [%lld,%lld) out of buffer range",
|
|
565
622
|
(long long) start, (long long) end);
|
|
566
623
|
}
|
|
567
|
-
dst_pair[2 * i] = (int64_t) RSTRING_LEN(
|
|
568
|
-
rb_str_cat(
|
|
569
|
-
dst_pair[2 * i + 1] = (int64_t) RSTRING_LEN(
|
|
624
|
+
dst_pair[2 * i] = (int64_t) RSTRING_LEN(c->buffer);
|
|
625
|
+
rb_str_cat(c->buffer, s->buf + start, (long) len);
|
|
626
|
+
dst_pair[2 * i + 1] = (int64_t) RSTRING_LEN(c->buffer);
|
|
570
627
|
}
|
|
571
628
|
|
|
572
629
|
/* carry mask onto the new pair entity */
|
|
573
|
-
if ( m ) {
|
|
630
|
+
if ( s->m ) {
|
|
574
631
|
boolean8_t *nm;
|
|
575
632
|
ca_create_mask(co);
|
|
576
633
|
nm = (boolean8_t *) co->mask->ptr;
|
|
577
|
-
for ( i = 0; i < n; i++ ) {
|
|
578
|
-
nm[i] = m[i] ? 1 : 0;
|
|
634
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
635
|
+
nm[i] = s->m[i] ? 1 : 0;
|
|
579
636
|
}
|
|
580
637
|
}
|
|
638
|
+
return Qnil;
|
|
639
|
+
}
|
|
581
640
|
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
641
|
+
static VALUE
|
|
642
|
+
rb_ca_const_string_copy (VALUE self)
|
|
643
|
+
{
|
|
644
|
+
ca_const_string_scan_t s;
|
|
645
|
+
ca_const_string_copy_t c;
|
|
646
|
+
volatile VALUE vout;
|
|
647
|
+
|
|
648
|
+
c.pairs = Qnil;
|
|
649
|
+
c.buffer = Qnil;
|
|
650
|
+
ca_const_string_scan_run(self, &s, ca_const_string_copy_body, &c);
|
|
651
|
+
vout = rb_ca_const_string_wrap(c.pairs, rb_str_freeze(c.buffer),
|
|
652
|
+
s.ca->encoding_id);
|
|
585
653
|
rb_ca_inherit_attr(vout, self);
|
|
586
654
|
return vout;
|
|
587
655
|
}
|
|
@@ -604,67 +672,6 @@ rb_ca_const_string_to_ca (int argc, VALUE *argv, VALUE self)
|
|
|
604
672
|
the layer where CAConstString outperforms `to_object` followed by
|
|
605
673
|
per-cell Ruby String work. */
|
|
606
674
|
|
|
607
|
-
typedef struct {
|
|
608
|
-
CAConstString *ca;
|
|
609
|
-
int64_t *off;
|
|
610
|
-
boolean8_t *m;
|
|
611
|
-
const char *buf;
|
|
612
|
-
long buflen;
|
|
613
|
-
ca_size_t n;
|
|
614
|
-
} ca_const_string_scan_t;
|
|
615
|
-
|
|
616
|
-
/* Opens the scan's window; ca_const_string_scan_end closes it. What runs
|
|
617
|
-
between them in this file reads bytes and raises only after closing. */
|
|
618
|
-
static void
|
|
619
|
-
ca_const_string_scan_begin (VALUE self, ca_const_string_scan_t *s)
|
|
620
|
-
{
|
|
621
|
-
TypedData_Get_Struct(self, CAConstString, &catext_data_type, s->ca);
|
|
622
|
-
ca_update_mask((CArray *) s->ca);
|
|
623
|
-
ca_attach((CArray *) s->ca); /* window: closed by ca_const_string_scan_end */
|
|
624
|
-
s->off = (int64_t *) s->ca->ptr;
|
|
625
|
-
s->m = s->ca->mask ? (boolean8_t *) s->ca->mask->ptr : NULL;
|
|
626
|
-
s->buf = RSTRING_PTR(s->ca->buffer);
|
|
627
|
-
s->buflen = RSTRING_LEN(s->ca->buffer);
|
|
628
|
-
s->n = s->ca->elements;
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
static void
|
|
632
|
-
ca_const_string_scan_end (ca_const_string_scan_t *s)
|
|
633
|
-
{
|
|
634
|
-
ca_detach((CArray *) s->ca);
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
typedef struct {
|
|
638
|
-
VALUE self;
|
|
639
|
-
ca_const_string_scan_t *s;
|
|
640
|
-
} ca_const_string_scan_arg_t;
|
|
641
|
-
|
|
642
|
-
static VALUE
|
|
643
|
-
ca_const_string_scan_begin_body (VALUE arg)
|
|
644
|
-
{
|
|
645
|
-
ca_const_string_scan_arg_t *x = (ca_const_string_scan_arg_t *) arg;
|
|
646
|
-
ca_const_string_scan_begin(x->self, x->s);
|
|
647
|
-
return Qnil;
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
/* Begins the scans of two arrays, both or neither: when the second one's
|
|
651
|
-
attach raises, the first is closed before the raise goes on. */
|
|
652
|
-
static void
|
|
653
|
-
ca_const_string_scan_begin_pair (VALUE va, ca_const_string_scan_t *a,
|
|
654
|
-
VALUE vb, ca_const_string_scan_t *b)
|
|
655
|
-
{
|
|
656
|
-
ca_const_string_scan_arg_t x;
|
|
657
|
-
int tag = 0;
|
|
658
|
-
ca_const_string_scan_begin(va, a);
|
|
659
|
-
x.self = vb;
|
|
660
|
-
x.s = b;
|
|
661
|
-
/* protect: ends the first scan, which is not an array */
|
|
662
|
-
rb_protect(ca_const_string_scan_begin_body, (VALUE) &x, &tag);
|
|
663
|
-
if ( tag ) {
|
|
664
|
-
ca_const_string_scan_end(a);
|
|
665
|
-
rb_jump_tag(tag);
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
675
|
|
|
669
676
|
/* Resolve element i to its record bytes; returns 0 if masked.
|
|
670
677
|
Storage is (start,end) int64 pairs: s->off holds 2 int64 per element,
|
|
@@ -688,10 +695,10 @@ ca_const_string_record (ca_const_string_scan_t *s, ca_size_t i, const char **pp,
|
|
|
688
695
|
static VALUE
|
|
689
696
|
ca_const_string_bool_out (ca_const_string_scan_t *s, boolean8_t **op)
|
|
690
697
|
{
|
|
691
|
-
VALUE vout;
|
|
698
|
+
volatile VALUE vout;
|
|
692
699
|
CArray *co;
|
|
693
700
|
vout = rb_carray_new(CA_BOOLEAN, s->ca->ndim, s->ca->dim, 0, NULL);
|
|
694
|
-
|
|
701
|
+
GetCArray(vout, co);
|
|
695
702
|
*op = (boolean8_t *) co->ptr;
|
|
696
703
|
if ( s->m ) {
|
|
697
704
|
ca_size_t i;
|
|
@@ -720,65 +727,72 @@ ca_const_string_memmem (const char *hay, long hlen, const char *need, long nlen)
|
|
|
720
727
|
return NULL;
|
|
721
728
|
}
|
|
722
729
|
|
|
723
|
-
/* CAConstString#byte_length → int64 CArray of per-element byte lengths. */
|
|
724
730
|
static VALUE
|
|
725
|
-
|
|
731
|
+
ca_const_string_bytesize_body (ca_const_string_scan_t *scan, void *arg)
|
|
726
732
|
{
|
|
727
|
-
ca_const_string_scan_t s;
|
|
728
|
-
VALUE vout;
|
|
733
|
+
ca_const_string_scan_t s0 = *scan, *s = &s0; /* a local copy: the writes below cannot alias it */
|
|
734
|
+
volatile VALUE vout;
|
|
729
735
|
CArray *co;
|
|
730
|
-
|
|
736
|
+
int32_t *op;
|
|
731
737
|
const char *p;
|
|
732
738
|
int64_t len;
|
|
733
739
|
ca_size_t i;
|
|
734
740
|
|
|
735
|
-
|
|
736
|
-
vout
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
op[i] = ca_const_string_record(&s, i, &p, &len) ? (int64_t) len : 0;
|
|
741
|
+
vout = rb_carray_new(CA_INT32, s->ca->ndim, s->ca->dim, 0, NULL);
|
|
742
|
+
GetCArray(vout, co);
|
|
743
|
+
op = (int32_t *) co->ptr;
|
|
744
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
745
|
+
op[i] = ca_const_string_record(s, i, &p, &len) ? (int32_t) len : 0;
|
|
741
746
|
}
|
|
742
|
-
if ( s
|
|
747
|
+
if ( s->m ) {
|
|
743
748
|
boolean8_t *nm;
|
|
744
749
|
ca_create_mask(co);
|
|
745
750
|
nm = (boolean8_t *) co->mask->ptr;
|
|
746
|
-
for ( i = 0; i < s
|
|
747
|
-
nm[i] = s
|
|
751
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
752
|
+
nm[i] = s->m[i] ? 1 : 0;
|
|
748
753
|
}
|
|
749
754
|
}
|
|
750
|
-
ca_const_string_scan_end(&s);
|
|
751
755
|
return vout;
|
|
752
756
|
}
|
|
753
757
|
|
|
758
|
+
/* CAConstString#bytesize → int64 CArray of per-element byte lengths. */
|
|
759
|
+
static VALUE
|
|
760
|
+
rb_ca_const_string_bytesize (VALUE self)
|
|
761
|
+
{
|
|
762
|
+
ca_const_string_scan_t s;
|
|
763
|
+
return ca_const_string_scan_run(self, &s,
|
|
764
|
+
ca_const_string_bytesize_body, NULL);
|
|
765
|
+
}
|
|
766
|
+
|
|
754
767
|
/* op codes for the shared predicate scan. */
|
|
755
768
|
enum { CA_TEXT_EQ, CA_TEXT_START, CA_TEXT_END, CA_TEXT_INCLUDE };
|
|
756
769
|
|
|
757
770
|
/* CAConstString#eq / start_with? / end_with? / include? — boolean mask vs a scalar
|
|
758
771
|
String query, byte-native. */
|
|
772
|
+
typedef struct {
|
|
773
|
+
const char *qp;
|
|
774
|
+
long qlen;
|
|
775
|
+
int op;
|
|
776
|
+
} ca_const_string_query_t;
|
|
777
|
+
|
|
759
778
|
static VALUE
|
|
760
|
-
|
|
779
|
+
ca_const_string_predicate_body (ca_const_string_scan_t *scan, void *arg)
|
|
761
780
|
{
|
|
762
|
-
ca_const_string_scan_t s;
|
|
763
|
-
|
|
781
|
+
ca_const_string_scan_t s0 = *scan, *s = &s0; /* a local copy: the writes below cannot alias it */
|
|
782
|
+
ca_const_string_query_t *q = (ca_const_string_query_t *) arg;
|
|
783
|
+
const char *qp = q->qp, *p;
|
|
784
|
+
long qlen = q->qlen;
|
|
785
|
+
int op = q->op;
|
|
786
|
+
volatile VALUE vout;
|
|
764
787
|
boolean8_t *out;
|
|
765
|
-
const char *qp, *p;
|
|
766
|
-
long qlen;
|
|
767
788
|
int64_t len;
|
|
768
789
|
ca_size_t i;
|
|
769
790
|
|
|
770
|
-
|
|
771
|
-
query = rb_obj_as_string(query);
|
|
772
|
-
}
|
|
773
|
-
qp = RSTRING_PTR(query);
|
|
774
|
-
qlen = RSTRING_LEN(query);
|
|
775
|
-
|
|
776
|
-
ca_const_string_scan_begin(self, &s);
|
|
777
|
-
vout = ca_const_string_bool_out(&s, &out);
|
|
791
|
+
vout = ca_const_string_bool_out(scan, &out);
|
|
778
792
|
|
|
779
|
-
for ( i = 0; i < s
|
|
793
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
780
794
|
boolean8_t r = 0;
|
|
781
|
-
if ( ca_const_string_record(
|
|
795
|
+
if ( ca_const_string_record(s, i, &p, &len) ) {
|
|
782
796
|
switch ( op ) {
|
|
783
797
|
case CA_TEXT_EQ:
|
|
784
798
|
r = ( len == qlen && memcmp(p, qp, qlen) == 0 );
|
|
@@ -796,13 +810,93 @@ ca_const_string_predicate (VALUE self, VALUE query, int op)
|
|
|
796
810
|
}
|
|
797
811
|
out[i] = r;
|
|
798
812
|
}
|
|
799
|
-
ca_const_string_scan_end(&s);
|
|
800
813
|
return vout;
|
|
801
814
|
}
|
|
802
815
|
|
|
803
|
-
static VALUE
|
|
804
|
-
|
|
805
|
-
|
|
816
|
+
static VALUE
|
|
817
|
+
ca_const_string_predicate (VALUE self, VALUE query, int op)
|
|
818
|
+
{
|
|
819
|
+
ca_const_string_scan_t s;
|
|
820
|
+
ca_const_string_query_t q;
|
|
821
|
+
volatile VALUE vquery = query;
|
|
822
|
+
|
|
823
|
+
if ( TYPE(vquery) != T_STRING ) {
|
|
824
|
+
vquery = rb_obj_as_string(vquery);
|
|
825
|
+
}
|
|
826
|
+
q.qp = RSTRING_PTR(vquery);
|
|
827
|
+
q.qlen = RSTRING_LEN(vquery);
|
|
828
|
+
q.op = op;
|
|
829
|
+
{
|
|
830
|
+
/* the scan reads q.qp, a pointer into vquery */
|
|
831
|
+
volatile VALUE out = ca_const_string_scan_run(self, &s,
|
|
832
|
+
ca_const_string_predicate_body, &q);
|
|
833
|
+
return out;
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/* The native scans read one String. Anything else (a Regexp, several
|
|
838
|
+
prefixes, a value that is no String) is asked of the Strings themselves. */
|
|
839
|
+
static VALUE
|
|
840
|
+
ca_const_string_ask_strings (VALUE self, const char *name, int argc, VALUE *argv)
|
|
841
|
+
{
|
|
842
|
+
return rb_funcallv(rb_funcall(self, rb_intern("to_string"), 0),
|
|
843
|
+
rb_intern(name), argc, argv);
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
static VALUE
|
|
847
|
+
rb_ca_const_string_start_with (int argc, VALUE *argv, VALUE self)
|
|
848
|
+
{
|
|
849
|
+
/* args: a fast path for one String; anything else goes to String#start_with? as given */
|
|
850
|
+
if ( argc == 1 && RB_TYPE_P(argv[0], T_STRING) ) {
|
|
851
|
+
return ca_const_string_predicate(self, argv[0], CA_TEXT_START);
|
|
852
|
+
}
|
|
853
|
+
return ca_const_string_ask_strings(self, "start_with?", argc, argv);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
static VALUE
|
|
857
|
+
rb_ca_const_string_end_with (int argc, VALUE *argv, VALUE self)
|
|
858
|
+
{
|
|
859
|
+
/* args: a fast path for one String; anything else goes to String#end_with? as given */
|
|
860
|
+
if ( argc == 1 && RB_TYPE_P(argv[0], T_STRING) ) {
|
|
861
|
+
return ca_const_string_predicate(self, argv[0], CA_TEXT_END);
|
|
862
|
+
}
|
|
863
|
+
return ca_const_string_ask_strings(self, "end_with?", argc, argv);
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
static VALUE
|
|
867
|
+
rb_ca_const_string_include (VALUE self, VALUE q)
|
|
868
|
+
{
|
|
869
|
+
if ( RB_TYPE_P(q, T_STRING) ) {
|
|
870
|
+
return ca_const_string_predicate(self, q, CA_TEXT_INCLUDE);
|
|
871
|
+
}
|
|
872
|
+
return ca_const_string_ask_strings(self, "include?", 1, &q);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
static VALUE
|
|
876
|
+
ca_const_string_eq_pair_body (ca_const_string_scan_t *scan, void *arg)
|
|
877
|
+
{
|
|
878
|
+
ca_const_string_scan_t a0 = scan[0], b0 = scan[1], *a = &a0, *b = &b0; /* local copies: the writes below cannot alias them */
|
|
879
|
+
volatile VALUE vout;
|
|
880
|
+
boolean8_t *out;
|
|
881
|
+
/* record() leaves these unset for masked cells; short-circuit eval means
|
|
882
|
+
they are read only when both records are valid, but GCC cannot prove
|
|
883
|
+
that -- initialize to keep -Wmaybe-uninitialized quiet and defensive. */
|
|
884
|
+
const char *pa = NULL, *pb = NULL;
|
|
885
|
+
int64_t la = 0, lb = 0;
|
|
886
|
+
ca_size_t i;
|
|
887
|
+
|
|
888
|
+
if ( a->n != b->n ) {
|
|
889
|
+
rb_raise(rb_eArgError, "CAConstString#eq: element count mismatch (%lld vs %lld)",
|
|
890
|
+
(long long) a->n, (long long) b->n);
|
|
891
|
+
}
|
|
892
|
+
vout = ca_const_string_bool_out(&scan[0], &out); /* mask carried from self */
|
|
893
|
+
for ( i = 0; i < a->n; i++ ) {
|
|
894
|
+
int va = ca_const_string_record(a, i, &pa, &la);
|
|
895
|
+
int vb = ca_const_string_record(b, i, &pb, &lb);
|
|
896
|
+
out[i] = ( va && vb && la == lb && memcmp(pa, pb, la) == 0 );
|
|
897
|
+
}
|
|
898
|
+
return vout;
|
|
899
|
+
}
|
|
806
900
|
|
|
807
901
|
/* CAConstString#eq(value): value is a scalar String (broadcast) or a CAConstString
|
|
808
902
|
(element-wise). Returns a boolean CArray. */
|
|
@@ -810,32 +904,12 @@ static VALUE
|
|
|
810
904
|
rb_ca_const_string_eq (VALUE self, VALUE other)
|
|
811
905
|
{
|
|
812
906
|
if ( rb_obj_is_kind_of(other, rb_cCAConstString) ) {
|
|
813
|
-
ca_const_string_scan_t
|
|
814
|
-
VALUE
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
const char *pa = NULL, *pb = NULL;
|
|
820
|
-
int64_t la = 0, lb = 0;
|
|
821
|
-
ca_size_t i;
|
|
822
|
-
|
|
823
|
-
ca_const_string_scan_begin_pair(self, &a, other, &b);
|
|
824
|
-
if ( a.n != b.n ) {
|
|
825
|
-
ca_const_string_scan_end(&a);
|
|
826
|
-
ca_const_string_scan_end(&b);
|
|
827
|
-
rb_raise(rb_eArgError, "CAConstString#eq: element count mismatch (%lld vs %lld)",
|
|
828
|
-
(long long) a.n, (long long) b.n);
|
|
829
|
-
}
|
|
830
|
-
vout = ca_const_string_bool_out(&a, &out); /* mask carried from self */
|
|
831
|
-
for ( i = 0; i < a.n; i++ ) {
|
|
832
|
-
int va = ca_const_string_record(&a, i, &pa, &la);
|
|
833
|
-
int vb = ca_const_string_record(&b, i, &pb, &lb);
|
|
834
|
-
out[i] = ( va && vb && la == lb && memcmp(pa, pb, la) == 0 );
|
|
835
|
-
}
|
|
836
|
-
ca_const_string_scan_end(&a);
|
|
837
|
-
ca_const_string_scan_end(&b);
|
|
838
|
-
return vout;
|
|
907
|
+
ca_const_string_scan_t s[2];
|
|
908
|
+
VALUE cols[2];
|
|
909
|
+
cols[0] = self;
|
|
910
|
+
cols[1] = other;
|
|
911
|
+
return ca_const_string_scan_cols(2, cols, s,
|
|
912
|
+
ca_const_string_eq_pair_body, NULL);
|
|
839
913
|
}
|
|
840
914
|
/* Another array is compared cell by cell as Strings, where a CAString
|
|
841
915
|
reconciles it (an object array of Strings, a fixlen or a CAString). */
|
|
@@ -843,6 +917,9 @@ rb_ca_const_string_eq (VALUE self, VALUE other)
|
|
|
843
917
|
return rb_funcall(rb_ca_const_string_to_string(self), rb_intern("eq"), 1,
|
|
844
918
|
other);
|
|
845
919
|
}
|
|
920
|
+
if ( ! RB_TYPE_P(other, T_STRING) ) {
|
|
921
|
+
return ca_const_string_ask_strings(self, "eq", 1, &other);
|
|
922
|
+
}
|
|
846
923
|
return ca_const_string_predicate(self, other, CA_TEXT_EQ);
|
|
847
924
|
}
|
|
848
925
|
|
|
@@ -850,32 +927,77 @@ rb_ca_const_string_eq (VALUE self, VALUE other)
|
|
|
850
927
|
value. With no value, the number of non-masked elements, as CArray#count
|
|
851
928
|
answers. */
|
|
852
929
|
static VALUE
|
|
853
|
-
|
|
930
|
+
ca_const_string_count_body (ca_const_string_scan_t *scan, void *arg)
|
|
854
931
|
{
|
|
855
|
-
ca_const_string_scan_t s;
|
|
856
|
-
|
|
857
|
-
|
|
932
|
+
ca_const_string_scan_t s0 = *scan, *s = &s0; /* a local copy: the writes below cannot alias it */
|
|
933
|
+
ca_const_string_query_t *q = (ca_const_string_query_t *) arg;
|
|
934
|
+
const char *p;
|
|
858
935
|
int64_t len;
|
|
859
936
|
ca_size_t i, c = 0;
|
|
860
|
-
|
|
937
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
938
|
+
if ( ca_const_string_record(s, i, &p, &len) && len == q->qlen &&
|
|
939
|
+
memcmp(p, q->qp, q->qlen) == 0 ) {
|
|
940
|
+
c++;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return SIZET2NUM(c);
|
|
944
|
+
}
|
|
861
945
|
|
|
862
|
-
|
|
863
|
-
|
|
946
|
+
/* The first (non-masked) cell byte-equal to the query, as a flat index, or
|
|
947
|
+
nil when none matches. */
|
|
948
|
+
static VALUE
|
|
949
|
+
ca_const_string_search_body (ca_const_string_scan_t *scan, void *arg)
|
|
950
|
+
{
|
|
951
|
+
ca_const_string_scan_t s0 = *scan, *s = &s0; /* a local copy: the writes below cannot alias it */
|
|
952
|
+
ca_const_string_query_t *q = (ca_const_string_query_t *) arg;
|
|
953
|
+
const char *p;
|
|
954
|
+
int64_t len;
|
|
955
|
+
ca_size_t i;
|
|
956
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
957
|
+
if ( ca_const_string_record(s, i, &p, &len) && len == q->qlen &&
|
|
958
|
+
memcmp(p, q->qp, (size_t) q->qlen) == 0 ) {
|
|
959
|
+
return SIZET2NUM(i);
|
|
960
|
+
}
|
|
864
961
|
}
|
|
865
|
-
|
|
866
|
-
|
|
962
|
+
return Qnil;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/* Runs a scan that compares each cell with a query String. */
|
|
966
|
+
static VALUE
|
|
967
|
+
ca_const_string_query_run (VALUE self, VALUE query,
|
|
968
|
+
ca_const_string_scan_body_t body)
|
|
969
|
+
{
|
|
970
|
+
ca_const_string_scan_t s;
|
|
971
|
+
ca_const_string_query_t q;
|
|
972
|
+
volatile VALUE vquery = query;
|
|
973
|
+
volatile VALUE out; /* the scan reads q.qp, a pointer into vquery */
|
|
974
|
+
|
|
975
|
+
if ( TYPE(vquery) != T_STRING ) {
|
|
976
|
+
vquery = rb_obj_as_string(vquery);
|
|
867
977
|
}
|
|
868
|
-
qp = RSTRING_PTR(
|
|
869
|
-
qlen = RSTRING_LEN(
|
|
978
|
+
q.qp = RSTRING_PTR(vquery);
|
|
979
|
+
q.qlen = RSTRING_LEN(vquery);
|
|
980
|
+
q.op = CA_TEXT_EQ;
|
|
981
|
+
out = ca_const_string_scan_run(self, &s, body, &q);
|
|
982
|
+
return out;
|
|
983
|
+
}
|
|
870
984
|
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
985
|
+
static VALUE
|
|
986
|
+
rb_ca_const_string_count (int argc, VALUE *argv, VALUE self)
|
|
987
|
+
{
|
|
988
|
+
VALUE query;
|
|
989
|
+
if ( rb_keyword_given_p() ) {
|
|
990
|
+
/* axis: and the other reduction keywords: the scan below is flat */
|
|
991
|
+
return rb_funcallv_kw(rb_funcall(self, rb_intern("to_string"), 0),
|
|
992
|
+
rb_intern("count"), argc, argv, RB_PASS_KEYWORDS);
|
|
876
993
|
}
|
|
877
|
-
|
|
878
|
-
|
|
994
|
+
if ( rb_scan_args(argc, argv, "01", &query) == 0 ) {
|
|
995
|
+
return rb_funcall(self, rb_intern("count_not_masked"), 0);
|
|
996
|
+
}
|
|
997
|
+
if ( ! RB_TYPE_P(query, T_STRING) ) {
|
|
998
|
+
return ca_const_string_ask_strings(self, "count", 1, &query);
|
|
999
|
+
}
|
|
1000
|
+
return ca_const_string_query_run(self, query, ca_const_string_count_body);
|
|
879
1001
|
}
|
|
880
1002
|
|
|
881
1003
|
/* CAConstString#search(query) / #find_value_index(query): flat index of the
|
|
@@ -884,30 +1006,26 @@ rb_ca_const_string_count (int argc, VALUE *argv, VALUE self)
|
|
|
884
1006
|
offset entity, not the string content), so this is a native memcmp scan --
|
|
885
1007
|
the exact-lookup counterpart of the native sort / min / max below. */
|
|
886
1008
|
static VALUE
|
|
887
|
-
|
|
1009
|
+
rb_ca_const_string_find_value_index (VALUE self, VALUE query)
|
|
888
1010
|
{
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
VALUE result = Qnil;
|
|
1011
|
+
if ( ! RB_TYPE_P(query, T_STRING) ) {
|
|
1012
|
+
return Qnil;
|
|
1013
|
+
}
|
|
1014
|
+
return ca_const_string_query_run(self, query, ca_const_string_search_body);
|
|
1015
|
+
}
|
|
895
1016
|
|
|
896
|
-
|
|
897
|
-
|
|
1017
|
+
static VALUE
|
|
1018
|
+
rb_ca_const_string_search (int argc, VALUE *argv, VALUE self)
|
|
1019
|
+
{
|
|
1020
|
+
/* args: a fast path for one String; anything else goes to the string array's search as given */
|
|
1021
|
+
if ( rb_keyword_given_p() ) {
|
|
1022
|
+
return rb_funcallv_kw(rb_funcall(self, rb_intern("to_string"), 0),
|
|
1023
|
+
rb_intern("search"), argc, argv, RB_PASS_KEYWORDS);
|
|
898
1024
|
}
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
ca_const_string_scan_begin(self, &s);
|
|
903
|
-
for ( i = 0; i < s.n; i++ ) {
|
|
904
|
-
if ( ca_const_string_record(&s, i, &p, &len) && len == qlen && memcmp(p, qp, (size_t) qlen) == 0 ) {
|
|
905
|
-
result = SIZET2NUM(i);
|
|
906
|
-
break;
|
|
907
|
-
}
|
|
1025
|
+
if ( argc == 1 && RB_TYPE_P(argv[0], T_STRING) ) {
|
|
1026
|
+
return rb_ca_const_string_find_value_index(self, argv[0]);
|
|
908
1027
|
}
|
|
909
|
-
|
|
910
|
-
return result;
|
|
1028
|
+
return ca_const_string_ask_strings(self, "search", argc, argv);
|
|
911
1029
|
}
|
|
912
1030
|
|
|
913
1031
|
/* ------------------------------------------------------------------- */
|
|
@@ -944,18 +1062,20 @@ ca_const_string_index_cmp (const void *a, const void *b)
|
|
|
944
1062
|
return (ia > ib) - (ia < ib);
|
|
945
1063
|
}
|
|
946
1064
|
|
|
947
|
-
/*
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
ca_const_string_sort_perm (ca_const_string_scan_t *s)
|
|
1065
|
+
/* raises on masked input (matches base CArray#sort) */
|
|
1066
|
+
static void
|
|
1067
|
+
ca_const_string_refuse_masked_sort (ca_const_string_scan_t *s)
|
|
951
1068
|
{
|
|
952
|
-
ca_size_t *idx;
|
|
953
|
-
ca_size_t i;
|
|
954
1069
|
if ( s->m ) {
|
|
955
|
-
ca_const_string_scan_end(s);
|
|
956
1070
|
rb_raise(rb_eArgError, "CAConstString#sort: cannot sort an array with masked elements");
|
|
957
1071
|
}
|
|
958
|
-
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
/* computes the ascending sort permutation into idx[0..n-1] */
|
|
1075
|
+
static void
|
|
1076
|
+
ca_const_string_sort_perm (ca_const_string_scan_t *s, ca_size_t *idx)
|
|
1077
|
+
{
|
|
1078
|
+
ca_size_t i;
|
|
959
1079
|
for ( i = 0; i < s->n; i++ ) idx[i] = i;
|
|
960
1080
|
ruby_qsort(idx, (size_t) s->n, sizeof(ca_size_t), ca_const_string_byte_cmp, s);
|
|
961
1081
|
/* qsort is not stable. Equal strings keep their order, as CArray's own
|
|
@@ -970,31 +1090,38 @@ ca_const_string_sort_perm (ca_const_string_scan_t *s)
|
|
|
970
1090
|
}
|
|
971
1091
|
i = j;
|
|
972
1092
|
}
|
|
973
|
-
return idx;
|
|
974
1093
|
}
|
|
975
1094
|
|
|
976
1095
|
/* CAConstString#sort_index → int64 CArray of the ascending sort permutation. */
|
|
977
1096
|
static VALUE
|
|
978
|
-
|
|
1097
|
+
ca_const_string_sort_index_body (ca_const_string_scan_t *s, void *arg)
|
|
979
1098
|
{
|
|
980
|
-
|
|
1099
|
+
volatile VALUE holder = 0; /* 0, not Qnil: ALLOCV_END reads a non-zero holder */
|
|
1100
|
+
volatile VALUE vout;
|
|
981
1101
|
ca_size_t *idx;
|
|
982
|
-
VALUE vout;
|
|
983
1102
|
CArray *co;
|
|
984
1103
|
int64_t *op;
|
|
985
1104
|
ca_size_t i;
|
|
986
1105
|
|
|
987
|
-
|
|
988
|
-
idx =
|
|
989
|
-
|
|
990
|
-
|
|
1106
|
+
ca_const_string_refuse_masked_sort(s);
|
|
1107
|
+
idx = ALLOCV_N(ca_size_t, holder, s->n > 0 ? s->n : 1);
|
|
1108
|
+
ca_const_string_sort_perm(s, idx);
|
|
1109
|
+
vout = rb_carray_new(CA_INT64, s->ca->ndim, s->ca->dim, 0, NULL);
|
|
1110
|
+
GetCArray(vout, co);
|
|
991
1111
|
op = (int64_t *) co->ptr;
|
|
992
|
-
for ( i = 0; i < s
|
|
993
|
-
|
|
994
|
-
ca_const_string_scan_end(&s);
|
|
1112
|
+
for ( i = 0; i < s->n; i++ ) op[i] = (int64_t) idx[i];
|
|
1113
|
+
ALLOCV_END(holder);
|
|
995
1114
|
return vout;
|
|
996
1115
|
}
|
|
997
1116
|
|
|
1117
|
+
static VALUE
|
|
1118
|
+
rb_ca_const_string_sort_index (VALUE self)
|
|
1119
|
+
{
|
|
1120
|
+
ca_const_string_scan_t s;
|
|
1121
|
+
return ca_const_string_scan_run(self, &s,
|
|
1122
|
+
ca_const_string_sort_index_body, NULL);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
998
1125
|
/* The ordering surface is defined in Ruby (lib/carray/const_string.rb) on
|
|
999
1126
|
top of these three: a storage cell is a byte range, so storage order is
|
|
1000
1127
|
insertion order rather than string order, and every member has to read
|
|
@@ -1008,18 +1135,18 @@ rb_ca_const_string_sort_index (VALUE self)
|
|
|
1008
1135
|
/* CAConstString#min / #max → the byte-min / byte-max element as a frozen String,
|
|
1009
1136
|
skipping masked elements; nil if empty or all masked. */
|
|
1010
1137
|
static VALUE
|
|
1011
|
-
|
|
1138
|
+
ca_const_string_extremum_body (ca_const_string_scan_t *scan, void *arg)
|
|
1012
1139
|
{
|
|
1013
|
-
ca_const_string_scan_t s;
|
|
1140
|
+
ca_const_string_scan_t s0 = *scan, *s = &s0; /* a local copy: the writes below cannot alias it */
|
|
1141
|
+
int want_max = *(int *) arg;
|
|
1014
1142
|
const char *p, *bestp = NULL;
|
|
1015
1143
|
int64_t len, bestlen = 0;
|
|
1016
1144
|
ca_size_t i;
|
|
1017
1145
|
int found = 0;
|
|
1018
1146
|
|
|
1019
|
-
|
|
1020
|
-
for ( i = 0; i < s.n; i++ ) {
|
|
1147
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
1021
1148
|
int c;
|
|
1022
|
-
if ( ! ca_const_string_record(
|
|
1149
|
+
if ( ! ca_const_string_record(s, i, &p, &len) ) continue;
|
|
1023
1150
|
if ( ! found ) {
|
|
1024
1151
|
bestp = p; bestlen = len; found = 1;
|
|
1025
1152
|
continue;
|
|
@@ -1031,14 +1158,18 @@ ca_const_string_extremum (VALUE self, int want_max)
|
|
|
1031
1158
|
}
|
|
1032
1159
|
}
|
|
1033
1160
|
if ( ! found ) {
|
|
1034
|
-
ca_const_string_scan_end(&s);
|
|
1035
1161
|
return Qnil;
|
|
1036
1162
|
}
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1163
|
+
return rb_str_freeze(rb_enc_str_new(bestp, bestlen,
|
|
1164
|
+
rb_enc_from_index(s->ca->encoding_id)));
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
static VALUE
|
|
1168
|
+
ca_const_string_extremum (VALUE self, int want_max)
|
|
1169
|
+
{
|
|
1170
|
+
ca_const_string_scan_t s;
|
|
1171
|
+
return ca_const_string_scan_run(self, &s,
|
|
1172
|
+
ca_const_string_extremum_body, &want_max);
|
|
1042
1173
|
}
|
|
1043
1174
|
|
|
1044
1175
|
static VALUE rb_ca_const_string_min (VALUE self) { return ca_const_string_extremum(self, 0); }
|
|
@@ -1048,7 +1179,7 @@ static VALUE rb_ca_const_string_max (VALUE self) { return ca_const_string_extrem
|
|
|
1048
1179
|
/* Native factorize: categorize's discovery pass, read from the bytes. The
|
|
1049
1180
|
storage cells are byte ranges, so the fixlen lane of
|
|
1050
1181
|
CArray#__factorize_appearance__ would tell ranges apart, not strings. A
|
|
1051
|
-
String is made per distinct value only, after the scan
|
|
1182
|
+
String is made per distinct value only, after the scan detaches. */
|
|
1052
1183
|
|
|
1053
1184
|
static uint64_t
|
|
1054
1185
|
ca_const_string_hash_bytes (const char *p, int64_t len)
|
|
@@ -1062,12 +1193,58 @@ ca_const_string_hash_bytes (const char *p, int64_t len)
|
|
|
1062
1193
|
return h;
|
|
1063
1194
|
}
|
|
1064
1195
|
|
|
1196
|
+
typedef struct {
|
|
1197
|
+
uint32_t *codes, *slot;
|
|
1198
|
+
int64_t *lev_start, *lev_len;
|
|
1199
|
+
size_t mask_bits;
|
|
1200
|
+
ca_size_t k;
|
|
1201
|
+
} ca_const_string_factorize_t;
|
|
1202
|
+
|
|
1203
|
+
/* Codes the cells by first appearance into an open-addressing table of
|
|
1204
|
+
(start, length) records. Reads bytes only. */
|
|
1205
|
+
static VALUE
|
|
1206
|
+
ca_const_string_factorize_body (ca_const_string_scan_t *scan, void *arg)
|
|
1207
|
+
{
|
|
1208
|
+
ca_const_string_scan_t s0 = *scan, *s = &s0; /* a local copy: the writes below cannot alias it */
|
|
1209
|
+
ca_const_string_factorize_t *f = (ca_const_string_factorize_t *) arg;
|
|
1210
|
+
ca_size_t i, k = f->k;
|
|
1211
|
+
for ( i = 0; i < s->n; i++ ) {
|
|
1212
|
+
const char *p;
|
|
1213
|
+
int64_t len;
|
|
1214
|
+
size_t j;
|
|
1215
|
+
if ( ! ca_const_string_record(s, i, &p, &len) ) {
|
|
1216
|
+
f->codes[i] = 0xFFFFFFFFu;
|
|
1217
|
+
continue;
|
|
1218
|
+
}
|
|
1219
|
+
j = (size_t) ca_const_string_hash_bytes(p, len) & f->mask_bits;
|
|
1220
|
+
for (;;) {
|
|
1221
|
+
uint32_t c = f->slot[j];
|
|
1222
|
+
if ( c == 0 ) {
|
|
1223
|
+
f->lev_start[k] = (int64_t) (p - s->buf);
|
|
1224
|
+
f->lev_len[k] = len;
|
|
1225
|
+
f->slot[j] = (uint32_t) (k + 1);
|
|
1226
|
+
f->codes[i] = (uint32_t) k;
|
|
1227
|
+
k++;
|
|
1228
|
+
break;
|
|
1229
|
+
}
|
|
1230
|
+
if ( f->lev_len[c - 1] == len &&
|
|
1231
|
+
memcmp(s->buf + f->lev_start[c - 1], p, (size_t) len) == 0 ) {
|
|
1232
|
+
f->codes[i] = c - 1;
|
|
1233
|
+
break;
|
|
1234
|
+
}
|
|
1235
|
+
j = (j + 1) & f->mask_bits;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
f->k = k;
|
|
1239
|
+
return Qnil;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1065
1242
|
/* CAConstString#__factorize_appearance__ -> [codes, levels], as the CArray
|
|
1066
1243
|
method answers: codes are the narrowest unsigned type holding the k codes
|
|
1067
1244
|
and the type-max sentinel, which masked cells store; levels are the k
|
|
1068
1245
|
distinct Strings in first-appearance order. */
|
|
1069
1246
|
static VALUE
|
|
1070
|
-
rb_ca_const_string_factorize_appearance (VALUE self)
|
|
1247
|
+
rb_ca_const_string_factorize_appearance (volatile VALUE self)
|
|
1071
1248
|
{
|
|
1072
1249
|
ca_const_string_scan_t s;
|
|
1073
1250
|
CAConstString *ca;
|
|
@@ -1078,7 +1255,7 @@ rb_ca_const_string_factorize_appearance (VALUE self)
|
|
|
1078
1255
|
size_t cap = 16, mask_bits;
|
|
1079
1256
|
ca_size_t n, i, k = 0;
|
|
1080
1257
|
int8_t code_dt;
|
|
1081
|
-
VALUE
|
|
1258
|
+
volatile VALUE slot_holder = 0, start_holder = 0, len_holder = 0;
|
|
1082
1259
|
|
|
1083
1260
|
TypedData_Get_Struct(self, CAConstString, &catext_data_type, ca);
|
|
1084
1261
|
n = ca->elements;
|
|
@@ -1089,50 +1266,34 @@ rb_ca_const_string_factorize_appearance (VALUE self)
|
|
|
1089
1266
|
cap <<= 1;
|
|
1090
1267
|
}
|
|
1091
1268
|
mask_bits = cap - 1;
|
|
1092
|
-
/* Everything the scan writes is allocated before
|
|
1093
|
-
|
|
1094
|
-
|
|
1269
|
+
/* Everything the scan writes is allocated before it attaches, and held
|
|
1270
|
+
by the collector: attaching reads the offsets, which can raise for a
|
|
1271
|
+
parent that answers in Ruby. */
|
|
1095
1272
|
vcodes = rb_carray_new(CA_UINT32, ca->ndim, ca->dim, 0, NULL);
|
|
1096
|
-
|
|
1273
|
+
GetCArray(vcodes, co);
|
|
1097
1274
|
codes = (uint32_t *) co->ptr;
|
|
1098
1275
|
slot = ALLOCV_N(uint32_t, slot_holder, cap); /* code + 1; 0 is empty */
|
|
1099
1276
|
MEMZERO(slot, uint32_t, cap);
|
|
1100
1277
|
lev_start = ALLOCV_N(int64_t, start_holder, n > 0 ? n : 1);
|
|
1101
1278
|
lev_len = ALLOCV_N(int64_t, len_holder, n > 0 ? n : 1);
|
|
1102
1279
|
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
for (;;) {
|
|
1114
|
-
uint32_t c = slot[j];
|
|
1115
|
-
if ( c == 0 ) {
|
|
1116
|
-
lev_start[k] = (int64_t) (p - s.buf);
|
|
1117
|
-
lev_len[k] = len;
|
|
1118
|
-
slot[j] = (uint32_t) (k + 1);
|
|
1119
|
-
codes[i] = (uint32_t) k;
|
|
1120
|
-
k++;
|
|
1121
|
-
break;
|
|
1122
|
-
}
|
|
1123
|
-
if ( lev_len[c - 1] == len && memcmp(s.buf + lev_start[c - 1], p, (size_t) len) == 0 ) {
|
|
1124
|
-
codes[i] = c - 1;
|
|
1125
|
-
break;
|
|
1126
|
-
}
|
|
1127
|
-
j = (j + 1) & mask_bits;
|
|
1128
|
-
}
|
|
1280
|
+
{
|
|
1281
|
+
ca_const_string_factorize_t f;
|
|
1282
|
+
f.codes = codes;
|
|
1283
|
+
f.slot = slot;
|
|
1284
|
+
f.lev_start = lev_start;
|
|
1285
|
+
f.lev_len = lev_len;
|
|
1286
|
+
f.mask_bits = mask_bits;
|
|
1287
|
+
f.k = 0;
|
|
1288
|
+
ca_const_string_scan_run(self, &s, ca_const_string_factorize_body, &f);
|
|
1289
|
+
k = f.k;
|
|
1129
1290
|
}
|
|
1130
|
-
ca_const_string_scan_end(&s);
|
|
1131
1291
|
ALLOCV_END(slot_holder);
|
|
1132
1292
|
|
|
1133
|
-
/* The buffer is the column's frozen String, alive
|
|
1293
|
+
/* The buffer is the column's frozen String, alive after the detach. */
|
|
1134
1294
|
levels = rb_ary_new_capa(k);
|
|
1135
1295
|
{
|
|
1296
|
+
/* gc: the CAConstString marks (and so pins) its buffer */
|
|
1136
1297
|
const char *buf = RSTRING_PTR(ca->buffer);
|
|
1137
1298
|
rb_encoding *enc = rb_enc_from_index(ca->encoding_id);
|
|
1138
1299
|
for ( i = 0; i < k; i++ ) {
|
|
@@ -1146,9 +1307,9 @@ rb_ca_const_string_factorize_appearance (VALUE self)
|
|
|
1146
1307
|
else if ( k <= 0xFFFF ) { code_dt = CA_UINT16; }
|
|
1147
1308
|
else { code_dt = CA_UINT32; }
|
|
1148
1309
|
if ( code_dt != CA_UINT32 ) {
|
|
1149
|
-
VALUE vnarrow = rb_carray_new(code_dt, ca->ndim, ca->dim, 0, NULL);
|
|
1310
|
+
volatile VALUE vnarrow = rb_carray_new(code_dt, ca->ndim, ca->dim, 0, NULL);
|
|
1150
1311
|
CArray *cn;
|
|
1151
|
-
|
|
1312
|
+
GetCArray(vnarrow, cn);
|
|
1152
1313
|
if ( code_dt == CA_UINT8 ) {
|
|
1153
1314
|
uint8_t *cp = (uint8_t *) cn->ptr;
|
|
1154
1315
|
for ( i = 0; i < n; i++ ) {
|
|
@@ -1163,7 +1324,7 @@ rb_ca_const_string_factorize_appearance (VALUE self)
|
|
|
1163
1324
|
}
|
|
1164
1325
|
vcodes = vnarrow;
|
|
1165
1326
|
}
|
|
1166
|
-
return
|
|
1327
|
+
return rb_ary_new_from_args(2, vcodes, levels);
|
|
1167
1328
|
}
|
|
1168
1329
|
|
|
1169
1330
|
/* ------------------------------------------------------------------- */
|
|
@@ -1176,7 +1337,7 @@ rb_ca_const_string_s_allocate (VALUE klass)
|
|
|
1176
1337
|
}
|
|
1177
1338
|
|
|
1178
1339
|
static VALUE
|
|
1179
|
-
rb_ca_const_string_initialize_copy (VALUE self, VALUE other)
|
|
1340
|
+
rb_ca_const_string_initialize_copy (volatile VALUE self, volatile VALUE other)
|
|
1180
1341
|
{
|
|
1181
1342
|
CAConstString *ca, *cs;
|
|
1182
1343
|
TypedData_Get_Struct(self, CAConstString, &catext_data_type, ca);
|
|
@@ -1207,7 +1368,6 @@ Init_ca_obj_const_string (void)
|
|
|
1207
1368
|
rb_cCArrayMask,
|
|
1208
1369
|
&carray_mask_data_type,
|
|
1209
1370
|
&ca_const_string_func, sizeof(ca_const_string_func));
|
|
1210
|
-
rb_define_const(rb_cObject, "CA_OBJ_CONST_STRING", INT2NUM(CA_OBJ_CONST_STRING));
|
|
1211
1371
|
|
|
1212
1372
|
rb_define_alloc_func(rb_cCAConstString, rb_ca_const_string_s_allocate);
|
|
1213
1373
|
rb_define_method(rb_cCAConstString, "initialize_copy",
|
|
@@ -1217,7 +1377,7 @@ Init_ca_obj_const_string (void)
|
|
|
1217
1377
|
rb_define_singleton_method(rb_cCAConstString, "__build__", rb_ca_const_string_build, 2);
|
|
1218
1378
|
rb_define_method(rb_cCAConstString, "encoding", rb_ca_const_string_encoding, 0);
|
|
1219
1379
|
rb_define_method(rb_cCAConstString, "buffer", rb_ca_const_string_buffer, 0);
|
|
1220
|
-
|
|
1380
|
+
rb_define_private_method(rb_cCAConstString, "storage_to_element",
|
|
1221
1381
|
rb_ca_const_string_storage_to_element, 1);
|
|
1222
1382
|
|
|
1223
1383
|
/* copy = compacting deep copy; to_ca is the same descent point. */
|
|
@@ -1226,18 +1386,18 @@ Init_ca_obj_const_string (void)
|
|
|
1226
1386
|
rb_define_method(rb_cCAConstString, "to_ca", rb_ca_const_string_to_ca, -1);
|
|
1227
1387
|
|
|
1228
1388
|
/* native byte ops (§3.7) */
|
|
1229
|
-
rb_define_method(rb_cCAConstString, "
|
|
1389
|
+
rb_define_method(rb_cCAConstString, "bytesize", rb_ca_const_string_bytesize, 0);
|
|
1230
1390
|
rb_define_method(rb_cCAConstString, "eq", rb_ca_const_string_eq, 1);
|
|
1231
1391
|
rb_define_method(rb_cCAConstString, "count", rb_ca_const_string_count, -1);
|
|
1232
|
-
rb_define_method(rb_cCAConstString, "start_with?", rb_ca_const_string_start_with, 1);
|
|
1233
|
-
rb_define_method(rb_cCAConstString, "end_with?", rb_ca_const_string_end_with, 1);
|
|
1392
|
+
rb_define_method(rb_cCAConstString, "start_with?", rb_ca_const_string_start_with, -1);
|
|
1393
|
+
rb_define_method(rb_cCAConstString, "end_with?", rb_ca_const_string_end_with, -1);
|
|
1234
1394
|
rb_define_method(rb_cCAConstString, "include?", rb_ca_const_string_include, 1);
|
|
1235
1395
|
|
|
1236
1396
|
/* native exact-lookup search: flat index of the first byte-equal element,
|
|
1237
1397
|
or nil. `search` is what str_matches consumes; find_value_index is the
|
|
1238
1398
|
*_index-named surface (both exact, mask-skipping). */
|
|
1239
|
-
rb_define_method(rb_cCAConstString, "search", rb_ca_const_string_search, 1);
|
|
1240
|
-
rb_define_method(rb_cCAConstString, "find_value_index",
|
|
1399
|
+
rb_define_method(rb_cCAConstString, "search", rb_ca_const_string_search, -1);
|
|
1400
|
+
rb_define_method(rb_cCAConstString, "find_value_index", rb_ca_const_string_find_value_index, 1);
|
|
1241
1401
|
|
|
1242
1402
|
/* Native flat sort permutation / min / max (§3.7), byte-memcmp
|
|
1243
1403
|
comparator. The public ordering surface is built on these in
|