Commit 38476767

3847676723d4Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · +1 added · ~1 modified

Description

Honour the comparator block in Array#minmax

minmax emitted a fixed _v < min / _v > max (or strcmp) loop and ignored any block, so [3,1,2].minmax { |a,b| b <=> a } returned [1,3] instead of [3,1].

When a 2-param comparator block is present, route minmax to a new compile_array_minmax_block that walks the array once keeping a running min and max, compiling the block against each running best (the same comparator shape Array#min/#max-with-block already use) and returning the [min, max] tuple. Works for int and string element arrays; float elements are still gated by the unimplemented float <=>.

Closes #1081

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
3847676723d4
Parent
ae57c944a913

spinel_codegen.rbmodifiedView file

@@ -23018,6 +23018,10 @@
2301823018 return tmp_res
2301923019 end
2302023020 if mname == "minmax"
23021+ # minmax with a 2-param comparator block honours the block ordering.
23022+ if @nd_block[nid] >= 0 && get_block_param(nid, 1) != ""
23023+ return compile_array_minmax_block(nid, rc, recv_type)
23024+ end
2302123025 pfx = array_c_prefix(recv_type)
2302223026 et = elem_type_of_array(recv_type)
2302323027 tt = "tuple:" + et + "," + et
@@ -42504,6 +42508,74 @@
4250442508 tmp_res
4250542509 end
4250642510
42511+ # `arr.minmax { |a, b| ... }` with a 2-param comparator block. Walks
42512+ # the array once keeping a running min and max; the block (compiled
42513+ # twice per element, once against each running best) returns the
42514+ # spaceship result. Mirrors compile_array_min_max_block's comparator
42515+ # arm but produces both ends as a tuple. Issue #1081.
42516+ def compile_array_minmax_block(nid, rc, recv_type)
42517+ bp1 = get_block_param(nid, 0)
42518+ bp1 = "_a" if bp1 == ""
42519+ bp2 = get_block_param(nid, 1)
42520+ bp2 = "_b" if bp2 == ""
42521+ et = elem_type_of_array(recv_type)
42522+ tt = "tuple:" + et + "," + et
42523+ register_tuple_type(tt)
42524+ @needs_gc = 1
42525+ name = tuple_c_name(tt)
42526+ pfx = array_c_prefix(recv_type)
42527+ tmp_min = new_temp
42528+ tmp_max = new_temp
42529+ tmp_i = new_temp
42530+ tmp = new_temp
42531+ emit(" " + c_type(et) + " " + tmp_min + " = " + c_default_val(et) + ";")
42532+ emit(" " + c_type(et) + " " + tmp_max + " = " + c_default_val(et) + ";")
42533+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++) {")
42534+ emit(" " + c_type(et) + " lv_" + bp1 + " = sp_" + pfx + "_get(" + rc + ", " + tmp_i + ");")
42535+ push_scope
42536+ declare_var(bp1, et)
42537+ declare_var(bp2, et)
42538+ emit(" if (" + tmp_i + " == 0) { " + tmp_min + " = lv_" + bp1 + "; " + tmp_max + " = lv_" + bp1 + "; } else {")
42539+ emit(" " + c_type(et) + " lv_" + bp2 + " = " + tmp_min + ";")
42540+ blk_mm = @nd_block[nid]
42541+ bexpr_min = "0"
42542+ if @nd_body[blk_mm] >= 0
42543+ bs_mm = get_stmts(@nd_body[blk_mm])
42544+ if bs_mm.length > 0
42545+ k_mm = 0
42546+ while k_mm < bs_mm.length - 1
42547+ compile_stmt(bs_mm[k_mm])
42548+ k_mm = k_mm + 1
42549+ end
42550+ bexpr_min = compile_expr(bs_mm.last)
42551+ end
42552+ end
42553+ emit(" mrb_int _cmpmin = " + bexpr_min + ";")
42554+ emit(" if (_cmpmin < 0) " + tmp_min + " = lv_" + bp1 + ";")
42555+ emit(" lv_" + bp2 + " = " + tmp_max + ";")
42556+ bexpr_max = "0"
42557+ if @nd_body[blk_mm] >= 0
42558+ bs_mx = get_stmts(@nd_body[blk_mm])
42559+ if bs_mx.length > 0
42560+ k_mx = 0
42561+ while k_mx < bs_mx.length - 1
42562+ compile_stmt(bs_mx[k_mx])
42563+ k_mx = k_mx + 1
42564+ end
42565+ bexpr_max = compile_expr(bs_mx.last)
42566+ end
42567+ end
42568+ emit(" mrb_int _cmpmax = " + bexpr_max + ";")
42569+ emit(" if (_cmpmax > 0) " + tmp_max + " = lv_" + bp1 + ";")
42570+ emit(" }")
42571+ pop_scope
42572+ emit(" }")
42573+ emit(" " + name + " *" + tmp + " = (" + name + " *)sp_gc_alloc(sizeof(" + name + "), NULL, " + tuple_scan_name(tt) + ");")
42574+ emit(" " + tmp + "->_0 = " + tmp_min + ";")
42575+ emit(" " + tmp + "->_1 = " + tmp_max + ";")
42576+ return tmp
42577+ end
42578+
4250742579 def compile_array_filter_map(nid, rc, recv_type)
4250842580 bp1 = get_block_param(nid, 0)
4250942581 if bp1 == ""