Commit 26187bf3
26187bf304e4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +3 added · ~1 modified
Description
codegen: Array#combination block + Hash#delete_if/select!/reject!
-
int_array.combination(k) { |c| ... } calls the blockless sp_IntArray_combination helper to materialise all combinations, then iterates the resulting PtrArray and binds each sub-array to the block param.
-
Hash#delete_if / #reject! / #select! on sym_int_hash and str_int_hash: walk the hash, evaluate the block on each (k, v), collect keys to drop, then delete in a second pass (open-addressing layout doesn't tolerate modify-during-iter). select! inverts the predicate.
Fixes #940 Fixes #944
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 26187bf304e4
- Parent
- a668764f7a40
spinel_codegen.rbmodifiedView file
@@ -33709,6 +33709,105 @@
3370933709
end3371033710
end3371133711
33712+
# Hash#delete_if / #select! / #reject! on typed hashes —33713+
# walk the hash, evaluate block on each (k, v), collect keys33714+
# to drop, then delete in a second pass (modify-during-iter is33715+
# not safe with the open-addressing layout). select! inverts33716+
# the predicate (keep iff true).33717+
if (mname == "delete_if" || mname == "reject!" || mname == "select!") && @nd_block[nid] >= 0 && recv >= 033718+
rt_dh = infer_type(recv)33719+
pfx_dh = ""33720+
key_ty_dh = ""33721+
val_ty_dh = ""33722+
key_arr_pfx = "IntArray" # default sym ids stored in int array33723+
if rt_dh == "sym_int_hash"33724+
pfx_dh = "SymIntHash"; key_ty_dh = "symbol"; val_ty_dh = "int"33725+
elsif rt_dh == "str_int_hash"33726+
pfx_dh = "StrIntHash"; key_ty_dh = "string"; val_ty_dh = "int"; key_arr_pfx = "StrArray"33727+
end33728+
if pfx_dh != ""33729+
old_dh = @in_loop33730+
@in_loop = 133731+
rc_dh = compile_expr_gc_rooted(recv)33732+
bp_k = get_block_param(nid, 0)33733+
bp_k = "_k" if bp_k == ""33734+
bp_v = get_block_param(nid, 1)33735+
bp_v = "_v" if bp_v == ""33736+
rm_arr = new_temp33737+
idx_dh = new_temp33738+
@needs_int_array = 133739+
@needs_str_array = 1 if key_arr_pfx == "StrArray"33740+
@needs_gc = 133741+
emit(" sp_" + key_arr_pfx + " *" + rm_arr + " = sp_" + key_arr_pfx + "_new();")33742+
emit(" SP_GC_ROOT(" + rm_arr + ");")33743+
emit(" for (mrb_int " + idx_dh + " = 0; " + idx_dh + " < " + rc_dh + "->len; " + idx_dh + "++) {")33744+
key_c_decl = (key_ty_dh == "symbol") ? "sp_sym" : "const char *"33745+
emit(" " + key_c_decl + " lv_" + bp_k + " = " + rc_dh + "->order[" + idx_dh + "];")33746+
emit(" mrb_int lv_" + bp_v + " = sp_" + pfx_dh + "_get(" + rc_dh + ", lv_" + bp_k + ");")33747+
@indent = @indent + 133748+
push_scope33749+
declare_var(bp_k, key_ty_dh)33750+
declare_var(bp_v, val_ty_dh)33751+
bbody_dh = @nd_body[@nd_block[nid]]33752+
bexpr_dh = "FALSE"33753+
if bbody_dh >= 033754+
bs_dh = get_stmts(bbody_dh)33755+
kk = 033756+
while kk < bs_dh.length - 133757+
compile_stmt(bs_dh[kk])33758+
kk = kk + 133759+
end33760+
if bs_dh.length > 033761+
bexpr_dh = compile_expr(bs_dh.last)33762+
end33763+
end33764+
cond_dh = (mname == "select!") ? "!(" + bexpr_dh + ")" : "(" + bexpr_dh + ")"33765+
push_arg = (key_ty_dh == "symbol") ? "(mrb_int)lv_" + bp_k : "lv_" + bp_k33766+
emit(" if (" + cond_dh + ") sp_" + key_arr_pfx + "_push(" + rm_arr + ", " + push_arg + ");")33767+
pop_scope33768+
@indent = @indent - 133769+
emit(" }")33770+
rm_idx_dh = new_temp33771+
get_call = (key_ty_dh == "symbol") ? "(sp_sym)sp_IntArray_get(" + rm_arr + ", " + rm_idx_dh + ")" : "sp_StrArray_get(" + rm_arr + ", " + rm_idx_dh + ")"33772+
emit(" for (mrb_int " + rm_idx_dh + " = 0; " + rm_idx_dh + " < " + rm_arr + "->len; " + rm_idx_dh + "++) {")33773+
emit(" sp_" + pfx_dh + "_delete(" + rc_dh + ", " + get_call + ");")33774+
emit(" }")33775+
@in_loop = old_dh33776+
return 133777+
end33778+
end33779+
33780+
# `arr.combination(k) { |c| ... }` on int_array — call the33781+
# blockless helper to materialise all combinations, then loop33782+
# them and bind each sub-array to the block param.33783+
if mname == "combination" && @nd_block[nid] >= 0 && recv >= 033784+
rt_co = infer_type(recv)33785+
if rt_co == "int_array"33786+
old_co = @in_loop33787+
@in_loop = 133788+
rc_co = compile_expr_gc_rooted(recv)33789+
bp_co = get_block_param(nid, 0)33790+
bp_co = "_c" if bp_co == ""33791+
@needs_int_array = 133792+
@needs_gc = 133793+
combos_co = new_temp33794+
iter_co = new_temp33795+
emit(" sp_PtrArray *" + combos_co + " = sp_IntArray_combination(" + rc_co + ", " + compile_arg0_as_int(nid) + ");")33796+
emit(" SP_GC_ROOT(" + combos_co + ");")33797+
emit(" for (mrb_int " + iter_co + " = 0; " + iter_co + " < " + combos_co + "->len; " + iter_co + "++) {")33798+
emit(" sp_IntArray * lv_" + bp_co + " = (sp_IntArray *)" + combos_co + "->data[" + iter_co + "];")33799+
@indent = @indent + 133800+
push_scope33801+
declare_var(bp_co, "int_array")33802+
compile_stmts_body(@nd_body[@nd_block[nid]])33803+
pop_scope33804+
@indent = @indent - 133805+
emit(" }")33806+
@in_loop = old_co33807+
return 133808+
end33809+
end33810+
3371233811
# `reverse_each` on a typed array: walk indices backwards.3371333812
if mname == "reverse_each" && @nd_block[nid] >= 0 && recv >= 03371433813
rt_re = infer_type(recv)test/array_combination_block.rbaddedView file
Loading diff…
test/hash_delete_if_select_reject_bang.rbaddedView file
Loading diff…
test/hash_delete_if_select_reject_bang.rb.expectedaddedView file
Loading diff…