Commit 4a35427f

4a35427f40daMerged

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

Description

feat(codegen): Array#union for core typed arrays (int/sym/str/float)

Mirrors Array#intersection (c31b618) for the four typed array variants. union returns a new array containing every unique element from self, followed by every unique element from other, in order. Like intersection, this is the single-argument form only — multi-argument a.union(b, c, d) is deferred.

Layer 1 (infer_method_call_return_type): when the receiver is one of the four supported variants, the result type is the same. poly_array and ptr_array fall through.

Layer 2 (compile_array_method_expr) adds three dispatch branches that mirror their intersection siblings:

  • int_array / sym_array (shared sp_IntArray storage): two passes, pushing only when sp_IntArray_include returns false against the growing result. No new runtime helpers — sp_IntArray_include / _new / _push / _get / _length are already there.
  • float_array: inline membership loop matching intersection's NaN handling (== matches Float#eql?, NaN != NaN consistently in C and Ruby).
  • str_array: same shape as int_array, using sp_StrArray_include (strcmp-based) for membership.

Test exercises:

  • int: basic, no overlap, all-overlap, empty self, empty other, duplicates in self, both-empty
  • str: basic, no overlap, all-overlap, duplicates in self
  • float: basic, no overlap, duplicates in self
  • sym: basic, no overlap, duplicates in self

All cases compile to the same C as Ruby's expected output.

Out of scope: poly_array / ptr_array (no element-equality at codegen level — same restriction as intersection); multi-argument form; Set-typed receivers.

Bootstrap: gen2.c == gen3.c. Tests: 204 pass.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
4a35427f40da
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -2984,6 +2984,13 @@
29842984 end
29852985 return ""
29862986 end
2987+ if mname == "union"
2988+ if recv >= 0
2989+ rt = infer_type(recv)
2990+ return rt if rt == "int_array" || rt == "sym_array" || rt == "str_array" || rt == "float_array"
2991+ end
2992+ return ""
2993+ end
29872994 ""
29882995 end
29892996
@@ -17100,6 +17107,25 @@
1710017107 emit(" }")
1710117108 return tmp
1710217109 end
17110+ # union: dedup-merge self followed by other. Same single-argument
17111+ # restriction as intersection. Both inputs are int_array/sym_array
17112+ # backed by sp_IntArray, so a single helper covers them.
17113+ if mname == "union"
17114+ arg = compile_arg0(nid)
17115+ tmp = new_temp
17116+ itmp = new_temp
17117+ jtmp = new_temp
17118+ emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
17119+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_IntArray_length(" + rc + "); " + itmp + "++) {")
17120+ emit(" mrb_int _v = sp_IntArray_get(" + rc + ", " + itmp + ");")
17121+ emit(" if (!sp_IntArray_include(" + tmp + ", _v)) sp_IntArray_push(" + tmp + ", _v);")
17122+ emit(" }")
17123+ emit(" for (mrb_int " + jtmp + " = 0; " + jtmp + " < sp_IntArray_length(" + arg + "); " + jtmp + "++) {")
17124+ emit(" mrb_int _v = sp_IntArray_get(" + arg + ", " + jtmp + ");")
17125+ emit(" if (!sp_IntArray_include(" + tmp + ", _v)) sp_IntArray_push(" + tmp + ", _v);")
17126+ emit(" }")
17127+ return tmp
17128+ end
1710317129 if mname == "min_by"
1710417130 if @nd_block[nid] >= 0
1710517131 blk = @nd_block[nid]
@@ -17245,6 +17271,28 @@
1724517271 emit(" }")
1724617272 return tmp
1724717273 end
17274+ # union: dedup-merge for floats. No sp_FloatArray_include helper —
17275+ # inline membership matches the intersection arm's NaN handling.
17276+ if mname == "union"
17277+ arg = compile_arg0(nid)
17278+ tmp = new_temp
17279+ itmp = new_temp
17280+ jtmp = new_temp
17281+ ktmp = new_temp
17282+ ltmp = new_temp
17283+ emit(" sp_FloatArray *" + tmp + " = sp_FloatArray_new();")
17284+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_FloatArray_length(" + rc + "); " + itmp + "++) {")
17285+ emit(" mrb_float _v = sp_FloatArray_get(" + rc + ", " + itmp + ");")
17286+ emit(" mrb_int _in_r = 0; for (mrb_int " + jtmp + " = 0; " + jtmp + " < sp_FloatArray_length(" + tmp + "); " + jtmp + "++) { if (sp_FloatArray_get(" + tmp + ", " + jtmp + ") == _v) { _in_r = 1; break; } }")
17287+ emit(" if (!_in_r) sp_FloatArray_push(" + tmp + ", _v);")
17288+ emit(" }")
17289+ emit(" for (mrb_int " + ktmp + " = 0; " + ktmp + " < sp_FloatArray_length(" + arg + "); " + ktmp + "++) {")
17290+ emit(" mrb_float _v = sp_FloatArray_get(" + arg + ", " + ktmp + ");")
17291+ emit(" mrb_int _in_r2 = 0; for (mrb_int " + ltmp + " = 0; " + ltmp + " < sp_FloatArray_length(" + tmp + "); " + ltmp + "++) { if (sp_FloatArray_get(" + tmp + ", " + ltmp + ") == _v) { _in_r2 = 1; break; } }")
17292+ emit(" if (!_in_r2) sp_FloatArray_push(" + tmp + ", _v);")
17293+ emit(" }")
17294+ return tmp
17295+ end
1724817296 end
1724917297 if is_ptr_array_type(recv_type) == 1
1725017298 elem_type = ptr_array_elem_type(recv_type)
@@ -17363,6 +17411,24 @@
1736317411 emit(" }")
1736417412 return tmp
1736517413 end
17414+ # union: dedup-merge for str_array. Reuses sp_StrArray_include
17415+ # (strcmp-based) for membership.
17416+ if mname == "union"
17417+ arg = compile_arg0(nid)
17418+ tmp = new_temp
17419+ itmp = new_temp
17420+ jtmp = new_temp
17421+ emit(" sp_StrArray *" + tmp + " = sp_StrArray_new();")
17422+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_StrArray_length(" + rc + "); " + itmp + "++) {")
17423+ emit(" const char *_v = sp_StrArray_get(" + rc + ", " + itmp + ");")
17424+ emit(" if (!sp_StrArray_include(" + tmp + ", _v)) sp_StrArray_push(" + tmp + ", _v);")
17425+ emit(" }")
17426+ emit(" for (mrb_int " + jtmp + " = 0; " + jtmp + " < sp_StrArray_length(" + arg + "); " + jtmp + "++) {")
17427+ emit(" const char *_v = sp_StrArray_get(" + arg + ", " + jtmp + ");")
17428+ emit(" if (!sp_StrArray_include(" + tmp + ", _v)) sp_StrArray_push(" + tmp + ", _v);")
17429+ emit(" }")
17430+ return tmp
17431+ end
1736617432 end
1736717433
1736817434 # PolyArray methods