Commit 80de1883

80de18839213Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

feat: add Array#count(val) for value counting

Counts occurrences of a specific value in an array. Uses strcmp for str_array, == for int_array/float_array. Works alongside the existing block and no-arg versions.

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

Details

Commit
80de18839213
Parent
ccffc61c679f

spinel_codegen.rbmodifiedView file

@@ -12354,6 +12354,23 @@
1235412354 pfx = array_c_prefix(recv_type)
1235512355 return "(sp_" + pfx + "_length(" + rc + ") == 0)"
1235612356 end
12357+ if mname == "count" && @nd_arguments[nid] >= 0 && @nd_block[nid] < 0
12358+ # count(val) — count occurrences of a specific value
12359+ pfx = array_c_prefix(recv_type)
12360+ val = compile_arg0(nid)
12361+ tmp_c = new_temp
12362+ tmp_i = new_temp
12363+ if recv_type == "str_array"
12364+ emit(" mrb_int " + tmp_c + " = 0;")
12365+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++)")
12366+ emit(" if (strcmp(sp_" + pfx + "_get(" + rc + ", " + tmp_i + "), " + val + ") == 0) " + tmp_c + "++;")
12367+ else
12368+ emit(" mrb_int " + tmp_c + " = 0;")
12369+ emit(" for (mrb_int " + tmp_i + " = 0; " + tmp_i + " < sp_" + pfx + "_length(" + rc + "); " + tmp_i + "++)")
12370+ emit(" if (sp_" + pfx + "_get(" + rc + ", " + tmp_i + ") == " + val + ") " + tmp_c + "++;")
12371+ end
12372+ return tmp_c
12373+ end
1235712374 if (mname == "any?" || mname == "all?" || mname == "none?" || mname == "one?") && @nd_block[nid] >= 0
1235812375 return compile_array_predicate_block(nid, rc, recv_type, mname)
1235912376 end