Commit f5973caf
f5973caf6713Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
codegen: Array#compact! on poly_array (in-place compaction) + int_array no-op
poly_array compact! shifts non-nil elements to the front and truncates len, returning the receiver. CRuby returns nil when nothing was removed; the typed array can't carry that signal so the subset always returns self.
int_array compact / compact! become no-op self-return since int_array elements can't be nil.
Fixes #988
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- f5973caf6713
- Parent
- 75f5d853da80
spinel_codegen.rbmodifiedView file
@@ -22299,6 +22299,12 @@
2229922299
if mname == "uniq"2230022300
return "sp_IntArray_uniq(" + rc + ")"2230122301
end22302+
if mname == "compact" || mname == "compact!"22303+
# int_array elements can't be nil, so compact is a no-op that22304+
# returns self (compact! would return nil in CRuby when nothing22305+
# was removed; the typed array can't carry that signal).22306+
return rc22307+
end2230222308
if mname == "uniq!"2230322309
# Mutate in place. sp_IntArray_uniq returns a fresh array; copy2230422310
# its contents back. The return is the (now-deduped) receiver,@@ -22927,6 +22933,15 @@
2292722933
# tag-table walk. Issue #725.2292822934
return "sp_PolyArray_compact(" + rc + ")"2292922935
end22936+
if mname == "compact!"22937+
# In-place compaction: shift non-nil elements to the front and22938+
# truncate. Returns the receiver (CRuby returns nil when nothing22939+
# was removed; the typed poly_array can't carry that signal so22940+
# we always return self).22941+
tmp_ca = new_temp22942+
emit(" { sp_PolyArray *" + tmp_ca + " = " + rc + "; mrb_int _w = 0; for (mrb_int _i = 0; _i < " + tmp_ca + "->len; _i++) if (" + tmp_ca + "->data[_i].tag != SP_TAG_NIL) " + tmp_ca + "->data[_w++] = " + tmp_ca + "->data[_i]; " + tmp_ca + "->len = _w; }")22943+
return rc22944+
end2293022945
if mname == "flatten"2293122946
# Recursive flatten into nested array-typed elements. With a2293222947
# depth arg the depth-bounded variant runs; without, fullytest/poly_array_compact_bang.rbaddedView file
Loading diff…
test/poly_array_compact_bang.rb.expectedaddedView file
Loading diff…