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 end
3371033710 end
3371133711
33712+ # Hash#delete_if / #select! / #reject! on typed hashes —
33713+ # walk the hash, evaluate block on each (k, v), collect keys
33714+ # to drop, then delete in a second pass (modify-during-iter is
33715+ # not safe with the open-addressing layout). select! inverts
33716+ # the predicate (keep iff true).
33717+ if (mname == "delete_if" || mname == "reject!" || mname == "select!") && @nd_block[nid] >= 0 && recv >= 0
33718+ 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 array
33723+ 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+ end
33728+ if pfx_dh != ""
33729+ old_dh = @in_loop
33730+ @in_loop = 1
33731+ 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_temp
33737+ idx_dh = new_temp
33738+ @needs_int_array = 1
33739+ @needs_str_array = 1 if key_arr_pfx == "StrArray"
33740+ @needs_gc = 1
33741+ 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 + 1
33748+ push_scope
33749+ 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 >= 0
33754+ bs_dh = get_stmts(bbody_dh)
33755+ kk = 0
33756+ while kk < bs_dh.length - 1
33757+ compile_stmt(bs_dh[kk])
33758+ kk = kk + 1
33759+ end
33760+ if bs_dh.length > 0
33761+ bexpr_dh = compile_expr(bs_dh.last)
33762+ end
33763+ end
33764+ cond_dh = (mname == "select!") ? "!(" + bexpr_dh + ")" : "(" + bexpr_dh + ")"
33765+ push_arg = (key_ty_dh == "symbol") ? "(mrb_int)lv_" + bp_k : "lv_" + bp_k
33766+ emit(" if (" + cond_dh + ") sp_" + key_arr_pfx + "_push(" + rm_arr + ", " + push_arg + ");")
33767+ pop_scope
33768+ @indent = @indent - 1
33769+ emit(" }")
33770+ rm_idx_dh = new_temp
33771+ 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_dh
33776+ return 1
33777+ end
33778+ end
33779+
33780+ # `arr.combination(k) { |c| ... }` on int_array — call the
33781+ # blockless helper to materialise all combinations, then loop
33782+ # them and bind each sub-array to the block param.
33783+ if mname == "combination" && @nd_block[nid] >= 0 && recv >= 0
33784+ rt_co = infer_type(recv)
33785+ if rt_co == "int_array"
33786+ old_co = @in_loop
33787+ @in_loop = 1
33788+ 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 = 1
33792+ @needs_gc = 1
33793+ combos_co = new_temp
33794+ iter_co = new_temp
33795+ 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 + 1
33800+ push_scope
33801+ declare_var(bp_co, "int_array")
33802+ compile_stmts_body(@nd_body[@nd_block[nid]])
33803+ pop_scope
33804+ @indent = @indent - 1
33805+ emit(" }")
33806+ @in_loop = old_co
33807+ return 1
33808+ end
33809+ end
33810+
3371233811 # `reverse_each` on a typed array: walk indices backwards.
3371333812 if mname == "reverse_each" && @nd_block[nid] >= 0 && recv >= 0
3371433813 rt_re = infer_type(recv)