Commit 014d77b4

014d77b472f4Merged

carlosdanielpohlod <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Add Array#intersection for core typed arrays (int/sym/str/float)

This adds #intersection support to the main typed array variants.

Instead of reinventing the wheel, I'm leveraging existing include? runtime functions for most types. For float_array, I went with inline membership loops to keep NaN logic consistent with Ruby's expected behavior.

I've left poly_array and ptr_array out for now since we don't have a reliable way to check element equality at the codegen level yet. Also, this sticks to the single-argument form to keep the initial PR focused.

Tests covering edge cases (duplicates, empty arrays, no matches) are in test/array_intersection.rb.

Details

Commit
014d77b472f4
Parent
ba4335ae239b

spinel_codegen.rbmodifiedView file

@@ -2773,6 +2773,13 @@
27732773 end
27742774 return "int"
27752775 end
2776+ if mname == "intersection"
2777+ if recv >= 0
2778+ rt = infer_type(recv)
2779+ return rt if rt == "int_array" || rt == "sym_array" || rt == "str_array" || rt == "float_array"
2780+ end
2781+ return ""
2782+ end
27762783 ""
27772784 end
27782785
@@ -16058,6 +16065,20 @@
1605816065 if mname == "count"
1605916066 return "sp_IntArray_length(" + rc + ")"
1606016067 end
16068+ # intersection: supported for int/sym/str/float arrays.
16069+ # poly_array and ptr_array fall through (element equality not available at codegen level).
16070+ # Only the first argument is compiled; multi-argument form (Ruby 2.7+) is not supported.
16071+ if mname == "intersection"
16072+ arg = compile_arg0(nid)
16073+ tmp = new_temp
16074+ itmp = new_temp
16075+ emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
16076+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_IntArray_length(" + rc + "); " + itmp + "++) {")
16077+ emit(" mrb_int _v = sp_IntArray_get(" + rc + ", " + itmp + ");")
16078+ emit(" if (sp_IntArray_include(" + arg + ", _v) && !sp_IntArray_include(" + tmp + ", _v)) sp_IntArray_push(" + tmp + ", _v);")
16079+ emit(" }")
16080+ return tmp
16081+ end
1606116082 if mname == "min_by"
1606216083 if @nd_block[nid] >= 0
1606316084 blk = @nd_block[nid]
@@ -16187,6 +16208,22 @@
1618716208 if mname == "last"
1618816209 return "sp_FloatArray_get(" + rc + ", -1)"
1618916210 end
16211+ if mname == "intersection"
16212+ arg = compile_arg0(nid)
16213+ tmp = new_temp
16214+ itmp = new_temp
16215+ jtmp = new_temp
16216+ ktmp = new_temp
16217+ emit(" sp_FloatArray *" + tmp + " = sp_FloatArray_new();")
16218+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_FloatArray_length(" + rc + "); " + itmp + "++) {")
16219+ emit(" mrb_float _v = sp_FloatArray_get(" + rc + ", " + itmp + ");")
16220+ # == matches Ruby Float#eql? semantics (exact bitwise equality; NaN != NaN in both C and Ruby)
16221+ emit(" mrb_int _in_b = 0; for (mrb_int " + jtmp + " = 0; " + jtmp + " < sp_FloatArray_length(" + arg + "); " + jtmp + "++) { if (sp_FloatArray_get(" + arg + ", " + jtmp + ") == _v) { _in_b = 1; break; } }")
16222+ emit(" mrb_int _in_r = 0; for (mrb_int " + ktmp + " = 0; " + ktmp + " < sp_FloatArray_length(" + tmp + "); " + ktmp + "++) { if (sp_FloatArray_get(" + tmp + ", " + ktmp + ") == _v) { _in_r = 1; break; } }")
16223+ emit(" if (_in_b && !_in_r) sp_FloatArray_push(" + tmp + ", _v);")
16224+ emit(" }")
16225+ return tmp
16226+ end
1619016227 end
1619116228 if is_ptr_array_type(recv_type) == 1
1619216229 elem_type = ptr_array_elem_type(recv_type)
@@ -16294,6 +16331,17 @@
1629416331 if mname == "count"
1629516332 return "sp_StrArray_length(" + rc + ")"
1629616333 end
16334+ if mname == "intersection"
16335+ arg = compile_arg0(nid)
16336+ tmp = new_temp
16337+ itmp = new_temp
16338+ emit(" sp_StrArray *" + tmp + " = sp_StrArray_new();")
16339+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_StrArray_length(" + rc + "); " + itmp + "++) {")
16340+ emit(" const char *_v = sp_StrArray_get(" + rc + ", " + itmp + ");")
16341+ emit(" if (sp_StrArray_include(" + arg + ", _v) && !sp_StrArray_include(" + tmp + ", _v)) sp_StrArray_push(" + tmp + ", _v);")
16342+ emit(" }")
16343+ return tmp
16344+ end
1629716345 end
1629816346
1629916347 # PolyArray methods