Commit 12f1c5c4

12f1c5c42d26Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #150 from ryanseys/rs-array-tally-sym

feat(codegen): Array#tally for sym_array

Details

Commit
12f1c5c42d26
Parent
bee7e89d5b64
Merge parent
8abd9145428e

spinel_codegen.rbmodifiedView file

@@ -2596,6 +2596,9 @@
25962596 return "int"
25972597 end
25982598 if mname == "tally"
2599+ if recv >= 0 && infer_type(recv) == "sym_array"
2600+ return "sym_int_hash"
2601+ end
25992602 return "str_int_hash"
26002603 end
26012604 if mname == "values"
@@ -8550,6 +8553,14 @@
85508553 end
85518554 end
85528555 end
8556+ # `sym_array.tally` returns a sym_int_hash. The runtime helper
8557+ # sp_SymArray_tally lives next to the sp_SymIntHash typedef which
8558+ # is gated on @needs_sym_int_hash, so flag the dependency.
8559+ if mname == "tally"
8560+ if @nd_receiver[nid] >= 0 && infer_type(@nd_receiver[nid]) == "sym_array"
8561+ @needs_sym_int_hash = 1
8562+ end
8563+ end
85538564 # Methods that need string helpers only when receiver is string
85548565 if mname == "+" || mname == "*" || mname == "reverse"
85558566 if @nd_receiver[nid] >= 0
@@ -10114,6 +10125,13 @@
1011410125 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;}")
1011510126 emit_raw("static mrb_int sp_SymIntHash_length(sp_SymIntHash*h){return h->len;}")
1011610127 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;}}")
10128+ # sym_array.tally — emitted alongside sp_SymIntHash because the
10129+ # helper depends on the typedef. sym_array storage is sp_IntArray
10130+ # (sym ids stored as mrb_int); cast each element to sp_sym for the
10131+ # hash key. sp_SymIntHash_get returns 0 for missing keys (line 10176)
10132+ # so the has_key? guard is redundant — drop it for two lookups per
10133+ # element instead of three.
10134+ 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;}")
1011710135 emit_raw("")
1011810136 end
1011910137
@@ -17414,6 +17432,12 @@
1741417432 pop_scope
1741517433 return tmp
1741617434 end
17435+ # tally: sym_array only — int_array would need an int_int_hash
17436+ # variant which doesn't exist yet. Result is sym_int_hash.
17437+ if mname == "tally" && recv_type == "sym_array"
17438+ @needs_sym_int_hash = 1
17439+ return "sp_SymArray_tally(" + rc + ")"
17440+ end
1741717441 if mname == "first"
1741817442 return "sp_IntArray_get(" + rc + ", 0)"
1741917443 end