Commit 885048a6
885048a6e745Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
codegen,runtime: Range#overlap?, Array#flatten(n) depth-bounded
-
Range#overlap? for two numeric ranges reduces to `a.first <= b.last && b.first <= a.last`. Analyzer marks the return type as bool so the puts side formats true/false rather than 1/0.
-
Array#flatten(n) on poly_array routes through a new sp_PolyArray_flatten_n that takes a max depth (-1 = unlimited, matching the no-arg flatten). The non-depth call still uses the existing helper.
Fixes #933 Fixes #943
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 885048a6e745
- Parent
- fbf02ef3725c
lib/sp_runtime.hmodifiedView file
@@ -2630,6 +2630,24 @@
26302630
sp_PolyArray_push(dst, v);26312631
}26322632
static sp_PolyArray *sp_PolyArray_flatten(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); if (!a) return b; for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_flatten_into(b, a->data[i]); return b; }2633+
/* Depth-bounded variant. depth==0 returns a shallow copy; each2634+
recursive step decrements depth, and a negative depth means2635+
"unlimited" (same as flatten without arg). Used by2636+
`Array#flatten(n)`. */2637+
static void sp_PolyArray_flatten_into_n(sp_PolyArray *dst, sp_RbVal v, mrb_int depth) {2638+
if (depth == 0 || v.tag != SP_TAG_OBJ) { sp_PolyArray_push(dst, v); return; }2639+
if (v.cls_id == SP_BUILTIN_INT_ARRAY) { sp_IntArray *ia = (sp_IntArray *)v.v.p; for (mrb_int i = 0; i < ia->len; i++) sp_PolyArray_push(dst, sp_box_int(ia->data[ia->start + i])); return; }2640+
if (v.cls_id == SP_BUILTIN_STR_ARRAY) { sp_StrArray *sa = (sp_StrArray *)v.v.p; for (mrb_int i = 0; i < sa->len; i++) sp_PolyArray_push(dst, sp_box_str(sa->data[i])); return; }2641+
if (v.cls_id == SP_BUILTIN_POLY_ARRAY) { sp_PolyArray *pa = (sp_PolyArray *)v.v.p; for (mrb_int i = 0; i < pa->len; i++) sp_PolyArray_flatten_into_n(dst, pa->data[i], depth - 1); return; }2642+
sp_PolyArray_push(dst, v);2643+
}2644+
static sp_PolyArray *sp_PolyArray_flatten_n(sp_PolyArray *a, mrb_int depth) {2645+
sp_PolyArray *b = sp_PolyArray_new();2646+
if (!a) return b;2647+
if (depth < 0) depth = INT64_MAX;2648+
for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_flatten_into_n(b, a->data[i], depth);2649+
return b;2650+
}26332651
/* Sum the integer-tagged elements of a poly_array. Used by26342652
`Array#sum` on a poly_array whose runtime tags are uniform int26352653
(e.g. the result of `arr.map { _1[:int_key] }`). Non-int tagsspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/range_overlap_array_flatten_depth.rbaddedView file
Loading diff…
test/range_overlap_array_flatten_depth.rb.expectedaddedView file
Loading diff…