Commit 9aafc320
9aafc3203f07Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add Array#each_slice(n) and Array#each_cons(n)
each_slice iterates over non-overlapping n-element chunks. each_cons iterates over sliding n-element windows. Block parameter is a sub-array of the same element type. Works for int_array, str_array, and float_array.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 9aafc3203f07
- Parent
- bb3ab402289b
spinel_codegen.rbmodifiedView file
@@ -9356,6 +9356,13 @@
93569356
# Index93579357
types.push("int")93589358
end9359+
elsif mname == "each_slice" || mname == "each_cons"9360+
# Block param is a sub-array of the same type9361+
if recv_type == "str_array" || recv_type == "float_array" || recv_type == "int_array"9362+
types.push(recv_type)9363+
else9364+
types.push("int_array")9365+
end93599366
elsif mname == "reduce" || mname == "inject"93609367
if bk == 093619368
# Accumulator: infer from initial value argument@@ -14725,6 +14732,20 @@
1472514732
end1472614733
end1472714734
14735+
if mname == "each_slice"14736+
if @nd_block[nid] >= 014737+
compile_each_slice_block(nid)14738+
return 114739+
end14740+
end14741+
14742+
if mname == "each_cons"14743+
if @nd_block[nid] >= 014744+
compile_each_cons_block(nid)14745+
return 114746+
end14747+
end14748+
1472814749
# scan with block: str.scan(/re/) { |m| ... }1472914750
if mname == "scan"1473014751
if @nd_block[nid] >= 0@@ -16136,6 +16157,62 @@
1613616157
""1613716158
end1613816159
16160+
def compile_each_slice_block(nid)16161+
old = @in_loop16162+
@in_loop = 116163+
rt = infer_type(@nd_receiver[nid])16164+
rc = compile_expr(@nd_receiver[nid])16165+
n = compile_arg0(nid)16166+
bp1 = get_block_param(nid, 0)16167+
if bp1 == ""16168+
bp1 = "_slice"16169+
end16170+
tmp_i = new_temp16171+
tmp_j = new_temp16172+
tmp_len = new_temp16173+
pfx = array_c_prefix(rt)16174+
declare_var(bp1, rt)16175+
@needs_gc = 116176+
emit(" mrb_int " + tmp_len + " = sp_" + pfx + "_length(" + rc + ");")16177+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < " + tmp_len + "; " + tmp_i + " += " + n + ") {")16178+
emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")16179+
emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < " + n + " && " + tmp_i + " + " + tmp_j + " < " + tmp_len + "; " + tmp_j + "++)")16180+
emit(" sp_" + pfx + "_push(lv_" + bp1 + ", sp_" + pfx + "_get(" + rc + ", " + tmp_i + " + " + tmp_j + "));")16181+
@indent = @indent + 116182+
compile_stmts_body(@nd_body[@nd_block[nid]])16183+
@indent = @indent - 116184+
emit(" }")16185+
@in_loop = old16186+
end16187+
16188+
def compile_each_cons_block(nid)16189+
old = @in_loop16190+
@in_loop = 116191+
rt = infer_type(@nd_receiver[nid])16192+
rc = compile_expr(@nd_receiver[nid])16193+
n = compile_arg0(nid)16194+
bp1 = get_block_param(nid, 0)16195+
if bp1 == ""16196+
bp1 = "_cons"16197+
end16198+
tmp_i = new_temp16199+
tmp_j = new_temp16200+
tmp_len = new_temp16201+
pfx = array_c_prefix(rt)16202+
declare_var(bp1, rt)16203+
@needs_gc = 116204+
emit(" mrb_int " + tmp_len + " = sp_" + pfx + "_length(" + rc + ");")16205+
emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " + " + n + " <= " + tmp_len + "; " + tmp_i + "++) {")16206+
emit(" lv_" + bp1 + " = sp_" + pfx + "_new();")16207+
emit(" for (mrb_int " + tmp_j + " = 0; " + tmp_j + " < " + n + "; " + tmp_j + "++)")16208+
emit(" sp_" + pfx + "_push(lv_" + bp1 + ", sp_" + pfx + "_get(" + rc + ", " + tmp_i + " + " + tmp_j + "));")16209+
@indent = @indent + 116210+
compile_stmts_body(@nd_body[@nd_block[nid]])16211+
@indent = @indent - 116212+
emit(" }")16213+
@in_loop = old16214+
end16215+
1613916216
def compile_each_with_index_block(nid)1614016217
old = @in_loop1614116218
@in_loop = 1