Commit af47e609

af47e6090f51Merged

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

Description

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

Mirrors Array#intersection (c31b618) and the just-landed Array#union for the same four typed array variants. difference returns a new array with the elements of self that do NOT appear in other, deduplicated. Single-argument form only — multi-argument a.difference(b, c) is deferred.

Layer 1: when receiver is one of the supported variants, return type is the same. poly_array / ptr_array fall through.

Layer 2 dispatch branches sit alongside intersection in each typed arm:

  • int_array / sym_array (sp_IntArray storage): one-pass loop pushing _v only when !sp_IntArray_include(other, _v) && !sp_IntArray_include(result, _v).
  • float_array: inline membership against both arg and result, with the same NaN-aware == as intersection.
  • str_array: same shape as int_array using sp_StrArray_include.

No new runtime helpers — reuses sp_*Array_include / _new / _push / _get / _length already present.

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

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

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

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

Details

Commit
af47e6090f51
Parent
02005f26deba

spinel_codegen.rbmodifiedView file

@@ -2984,6 +2984,13 @@
29842984 end
29852985 return ""
29862986 end
2987+ if mname == "difference"
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,19 @@
1710017107 emit(" }")
1710117108 return tmp
1710217109 end
17110+ # difference: elements of self NOT in other (deduplicated).
17111+ # Inverse of intersection.
17112+ if mname == "difference"
17113+ arg = compile_arg0(nid)
17114+ tmp = new_temp
17115+ itmp = new_temp
17116+ emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
17117+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_IntArray_length(" + rc + "); " + itmp + "++) {")
17118+ emit(" mrb_int _v = sp_IntArray_get(" + rc + ", " + itmp + ");")
17119+ emit(" if (!sp_IntArray_include(" + arg + ", _v) && !sp_IntArray_include(" + tmp + ", _v)) sp_IntArray_push(" + tmp + ", _v);")
17120+ emit(" }")
17121+ return tmp
17122+ end
1710317123 if mname == "min_by"
1710417124 if @nd_block[nid] >= 0
1710517125 blk = @nd_block[nid]
@@ -17245,6 +17265,23 @@
1724517265 emit(" }")
1724617266 return tmp
1724717267 end
17268+ # difference: float elements of self not in other, dedup. Inline
17269+ # membership for the NaN-aware == semantics; mirror intersection.
17270+ if mname == "difference"
17271+ arg = compile_arg0(nid)
17272+ tmp = new_temp
17273+ itmp = new_temp
17274+ jtmp = new_temp
17275+ ktmp = new_temp
17276+ emit(" sp_FloatArray *" + tmp + " = sp_FloatArray_new();")
17277+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_FloatArray_length(" + rc + "); " + itmp + "++) {")
17278+ emit(" mrb_float _v = sp_FloatArray_get(" + rc + ", " + itmp + ");")
17279+ 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; } }")
17280+ 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; } }")
17281+ emit(" if (!_in_b && !_in_r) sp_FloatArray_push(" + tmp + ", _v);")
17282+ emit(" }")
17283+ return tmp
17284+ end
1724817285 end
1724917286 if is_ptr_array_type(recv_type) == 1
1725017287 elem_type = ptr_array_elem_type(recv_type)
@@ -17363,6 +17400,19 @@
1736317400 emit(" }")
1736417401 return tmp
1736517402 end
17403+ # difference: str elements of self not in other, dedup. Mirrors
17404+ # int_array's branch with strcmp-backed membership.
17405+ if mname == "difference"
17406+ arg = compile_arg0(nid)
17407+ tmp = new_temp
17408+ itmp = new_temp
17409+ emit(" sp_StrArray *" + tmp + " = sp_StrArray_new();")
17410+ emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_StrArray_length(" + rc + "); " + itmp + "++) {")
17411+ emit(" const char *_v = sp_StrArray_get(" + rc + ", " + itmp + ");")
17412+ emit(" if (!sp_StrArray_include(" + arg + ", _v) && !sp_StrArray_include(" + tmp + ", _v)) sp_StrArray_push(" + tmp + ", _v);")
17413+ emit(" }")
17414+ return tmp
17415+ end
1736617416 end
1736717417
1736817418 # PolyArray methods