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
_vonly when!sp_IntArray_include(other, _v) && !sp_IntArray_include(result, _v). - float_array: inline membership against both
argand 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
end return "" end if mname == "difference" if recv >= 0 rt = infer_type(recv) return rt if rt == "int_array" || rt == "sym_array" || rt == "str_array" || rt == "float_array" end return "" end "" end emit(" }") return tmp end # difference: elements of self NOT in other (deduplicated). # Inverse of intersection. if mname == "difference" arg = compile_arg0(nid) tmp = new_temp itmp = new_temp emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_IntArray_length(" + rc + "); " + itmp + "++) {") emit(" mrb_int _v = sp_IntArray_get(" + rc + ", " + itmp + ");") emit(" if (!sp_IntArray_include(" + arg + ", _v) && !sp_IntArray_include(" + tmp + ", _v)) sp_IntArray_push(" + tmp + ", _v);") emit(" }") return tmp end if mname == "min_by" if @nd_block[nid] >= 0 blk = @nd_block[nid] emit(" }") return tmp end # difference: float elements of self not in other, dedup. Inline # membership for the NaN-aware == semantics; mirror intersection. if mname == "difference" arg = compile_arg0(nid) tmp = new_temp itmp = new_temp jtmp = new_temp ktmp = new_temp emit(" sp_FloatArray *" + tmp + " = sp_FloatArray_new();") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_FloatArray_length(" + rc + "); " + itmp + "++) {") emit(" mrb_float _v = sp_FloatArray_get(" + rc + ", " + itmp + ");") 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; } }") 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; } }") emit(" if (!_in_b && !_in_r) sp_FloatArray_push(" + tmp + ", _v);") emit(" }") return tmp end end if is_ptr_array_type(recv_type) == 1 elem_type = ptr_array_elem_type(recv_type) emit(" }") return tmp end # difference: str elements of self not in other, dedup. Mirrors # int_array's branch with strcmp-backed membership. if mname == "difference" arg = compile_arg0(nid) tmp = new_temp itmp = new_temp emit(" sp_StrArray *" + tmp + " = sp_StrArray_new();") emit(" for (mrb_int " + itmp + " = 0; " + itmp + " < sp_StrArray_length(" + rc + "); " + itmp + "++) {") emit(" const char *_v = sp_StrArray_get(" + rc + ", " + itmp + ");") emit(" if (!sp_StrArray_include(" + arg + ", _v) && !sp_StrArray_include(" + tmp + ", _v)) sp_StrArray_push(" + tmp + ", _v);") emit(" }") return tmp end end # PolyArray methodstest/array_difference.rbaddedView file
Loading diff…