Commit 183a8010
183a80101d37Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(codegen): Array#tally for sym_array
str_array#tally already shipped (sp_StrArray_tally lives next to sp_StrArray in sp_runtime.h). Add the symbol-array variant via a new sp_SymArray_tally helper that emits a sym_int_hash mapping each unique symbol element to its occurrence count.
The helper is emitted alongside sp_SymIntHash (in emit_sym_int_hash_runtime, gated on @needs_sym_int_hash) rather than in lib/sp_runtime.h, because sp_SymIntHash is one of the on-demand-emitted hash variants, not a baseline runtime type. sym_array storage is sp_IntArray under the covers, so the helper takes sp_IntArray * and casts each element to sp_sym for the hash key:
static sp_SymIntHash *sp_SymArray_tally(sp_IntArray *a) {
sp_SymIntHash *h = sp_SymIntHash_new();
for (mrb_int i = 0; i < a->len; i++) {
sp_sym k = (sp_sym)a->data[a->start + i];
mrb_int c = sp_SymIntHash_has_key(h, k) ? sp_SymIntHash_get(h, k) : 0;
sp_SymIntHash_set(h, k, c + 1);
}
return h;
}
compile_call_expr's int_array/sym_array dispatch arm now branches on recv_type — only sym_array routes to sp_SymArray_tally; int_array falls through (would need a new int_int_hash variant which doesn't exist yet). The "tally" return-type rule in infer_method_name_type likewise branches: sym_array receivers infer sym_int_hash, others keep the existing str_int_hash default.
scan_features' new tally arm sets @needs_sym_int_hash when the receiver is sym_array, so emit_sym_int_hash_runtime fires and the typedef + sp_SymArray_tally helper land in the generated C ahead of any call site.
Test exercises:
- repeated symbols across a 6-element array, asserting count for each distinct key
- single-element tally
- all-same-element tally (count == array length)
- tally.length matches the unique-element count
- has_key? confirms presence and absence
Out of scope: int_array#tally, float_array#tally — both would need new int_int_hash / float_int_hash variants. Listed as the same family but deferred here.
Bootstrap: gen2.c == gen3.c. Tests: 212 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 183a80101d37
- Parent
- 02005f26deba
spinel_codegen.rbmodifiedView file
return "int" end if mname == "tally" if recv >= 0 && infer_type(recv) == "sym_array" return "sym_int_hash" end return "str_int_hash" end if mname == "values" end end end # `sym_array.tally` returns a sym_int_hash. The runtime helper # sp_SymArray_tally lives next to the sp_SymIntHash typedef which # is gated on @needs_sym_int_hash, so flag the dependency. if mname == "tally" if @nd_receiver[nid] >= 0 && infer_type(@nd_receiver[nid]) == "sym_array" @needs_sym_int_hash = 1 end end # Methods that need string helpers only when receiver is string if mname == "+" || mname == "*" || mname == "reverse" if @nd_receiver[nid] >= 0 emit_raw("static mrb_bool sp_SymIntHash_has_key(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k)return TRUE;idx=(idx+1)&h->mask;}return FALSE;}") emit_raw("static mrb_int sp_SymIntHash_length(sp_SymIntHash*h){return h->len;}") emit_raw("static void sp_SymIntHash_delete(sp_SymIntHash*h,sp_sym k){mrb_int idx=(mrb_int)(((mrb_int)k)&h->mask);while(h->keys[idx]>=0){if(h->keys[idx]==k){h->keys[idx]=-1;h->vals[idx]=0;h->len--;mrb_int j=(idx+1)&h->mask;while(h->keys[j]>=0){mrb_int nj=(mrb_int)(((mrb_int)h->keys[j])&h->mask);if((j>idx&&(nj<=idx||nj>j))||(j<idx&&nj<=idx&&nj>j)){h->keys[idx]=h->keys[j];h->vals[idx]=h->vals[j];h->keys[j]=-1;h->vals[j]=0;idx=j;}j=(j+1)&h->mask;}return;}idx=(idx+1)&h->mask;}}") # sym_array.tally — emitted alongside sp_SymIntHash because the # helper depends on the typedef. sym_array storage is sp_IntArray # (sym ids stored as mrb_int); cast each element to sp_sym for the # hash key. sp_SymIntHash_get returns 0 for missing keys (line 10176) # so the has_key? guard is redundant — drop it for two lookups per # element instead of three. emit_raw("static sp_SymIntHash*sp_SymArray_tally(sp_IntArray*a){sp_SymIntHash*h=sp_SymIntHash_new();for(mrb_int i=0;i<a->len;i++){sp_sym k=(sp_sym)a->data[a->start+i];sp_SymIntHash_set(h,k,sp_SymIntHash_get(h,k)+1);}return h;}") emit_raw("") end end return "sp_IntArray_sort(" + rc + ")" end # tally: sym_array only — int_array would need an int_int_hash # variant which doesn't exist yet. Result is sym_int_hash. if mname == "tally" && recv_type == "sym_array" @needs_sym_int_hash = 1 return "sp_SymArray_tally(" + rc + ")" end if mname == "first" return "sp_IntArray_get(" + rc + ", 0)" endtest/array_tally_sym.rbaddedView file
Loading diff…