Commit fa89ee62
fa89ee6269b0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Array#take_while, #drop_while, and PolyArray#tally
take_while/drop_while: implement for all array kinds (typed and poly) with a dedicated handler before the if(k) guard. Empty array literals (TY_UNKNOWN receiver) return an empty poly array immediately.
PolyArray#tally: count symbol occurrences in a poly array, returning a TY_SYM_POLY_HASH (sp_SymPolyHash with boxed integer values). Adds the sp_PolyArray_tally runtime helper and wires up type inference and codegen.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- fa89ee6269b0
- Parent
- e1d310237f59
lib/sp_runtime.hmodifiedView file
@@ -3645,6 +3645,7 @@
36453645
key from the insertion-order list. Issue #510. */36463646
static void sp_SymPolyHash_delete(sp_SymPolyHash*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]=sp_box_nil();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]=sp_box_nil();idx=j;}j=(j+1)&h->mask;}{mrb_int oi=0;while(oi<=h->len){if(h->order[oi]==k){while(oi<h->len){h->order[oi]=h->order[oi+1];oi++;}break;}oi++;}}return;}idx=(idx+1)&h->mask;}}36473647
static sp_SymPolyHash*sp_SymPolyHash_dup(sp_SymPolyHash*h){sp_SymPolyHash*r=sp_SymPolyHash_new();r->default_v=h->default_v;for(mrb_int i=0;i<h->len;i++)sp_SymPolyHash_set(r,h->order[i],sp_SymPolyHash_get(h,h->order[i]));return r;}3648+
static sp_SymPolyHash *sp_PolyArray_tally(sp_PolyArray *a) { sp_SymPolyHash *h = sp_SymPolyHash_new(); if (!a) return h; for (mrb_int i = 0; i < a->len; i++) { sp_RbVal v = a->data[i]; if (v.tag != SP_TAG_SYM) continue; sp_sym k = (sp_sym)v.v.i; sp_RbVal cur = sp_SymPolyHash_get(h, k); mrb_int c = (cur.tag == SP_TAG_INT) ? cur.v.i : 0; sp_SymPolyHash_set(h, k, sp_box_int(c + 1)); } return h; }36483649
static sp_SymPolyHash*sp_SymPolyHash_replace(sp_SymPolyHash*h,sp_SymPolyHash*o){if(!h)return h;for(mrb_int i=0;i<h->cap;i++)h->keys[i]=-1;h->len=0;if(o)for(mrb_int i=0;i<o->len;i++)sp_SymPolyHash_set(h,o->order[i],sp_SymPolyHash_get(o,o->order[i]));return h;}36493650
static mrb_bool sp_SymPolyHash_eq(sp_SymPolyHash*a,sp_SymPolyHash*b){if(!a||!b)return a==b;if(a->len!=b->len)return FALSE;for(mrb_int i=0;i<a->len;i++){sp_sym k=a->order[i];if(!sp_SymPolyHash_has_key(b,k))return FALSE;if(!sp_poly_eq(sp_SymPolyHash_get(a,k),sp_SymPolyHash_get(b,k)))return FALSE;}return TRUE;}36503651
/* Hash#inspect for sym_poly_hash. CRuby 4.0 renders symbol keyssrc/analyze.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…