Commit b2d2d56e

b2d2d56e9773Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · ~3 modified

Description

fix(runtime,codegen,analyze): Array#combination + #permutation for int_array (#742)

[1,2,3].combination(2) and [1,2,3].permutation(2) used to fall through to the unresolved-call warning.

Three pieces:

  • lib/sp_runtime.h: sp_IntArray_combination (recursive k-element selection) and sp_IntArray_permutation (k-element ordered, with a used[] bitmap). Both return sp_PtrArray of fresh IntArrays.
  • spinel_codegen.rb: int_array recv arm routes combination / permutation to the runtime helpers. is_ptr_array_type recv arm gets a to_a identity branch so chained .to_a.inspect works.
  • spinel_analyze.rb: infer_type returns int_array_ptr_array for combination/permutation on int_array/sym_array recv. to_a on ptr_array / str_array / sym_array / float_array returns the recv type as identity (was always int_array fallback).

values_at was the third method in this issue and was handled in 6e6417f.

Closes #742

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
b2d2d56e9773
Parent
f8c852cd0f2c

lib/sp_runtime.hmodifiedView file

@@ -1392,6 +1392,13 @@
13921392 type is one of the four built-in T_array shapes, recurse into the
13931393 matching primitive inspect . */
13941394 static const char*sp_IntArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_IntArray_inspect((sp_IntArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
1395+/* Issue #742: Array#combination(k) on int_array -- emit all
1396+ k-element ordered combinations as a PtrArray of IntArrays. */
1397+static void sp_int_combination_recur(sp_IntArray*src,mrb_int start,mrb_int k,sp_IntArray*acc,sp_PtrArray*out){if(k==0){sp_IntArray*cp=sp_IntArray_new();for(mrb_int i=0;i<acc->len;i++)sp_IntArray_push(cp,acc->data[acc->start+i]);sp_PtrArray_push(out,cp);return;}for(mrb_int i=start;i<=src->len-k;i++){sp_IntArray_push(acc,src->data[src->start+i]);sp_int_combination_recur(src,i+1,k-1,acc,out);acc->len--;}}
1398+static sp_PtrArray*sp_IntArray_combination(sp_IntArray*a,mrb_int k){sp_PtrArray*out=sp_PtrArray_new();if(!a||k<0||k>a->len)return out;sp_IntArray*acc=sp_IntArray_new();sp_int_combination_recur(a,0,k,acc,out);return out;}
1399+/* Array#permutation(k) -- ordered k-permutations. */
1400+static void sp_int_permutation_recur(sp_IntArray*src,mrb_int k,sp_IntArray*used,sp_IntArray*acc,sp_PtrArray*out){if(k==0){sp_IntArray*cp=sp_IntArray_new();for(mrb_int i=0;i<acc->len;i++)sp_IntArray_push(cp,acc->data[acc->start+i]);sp_PtrArray_push(out,cp);return;}for(mrb_int i=0;i<src->len;i++){if(used->data[used->start+i])continue;used->data[used->start+i]=1;sp_IntArray_push(acc,src->data[src->start+i]);sp_int_permutation_recur(src,k-1,used,acc,out);acc->len--;used->data[used->start+i]=0;}}
1401+static sp_PtrArray*sp_IntArray_permutation(sp_IntArray*a,mrb_int k){sp_PtrArray*out=sp_PtrArray_new();if(!a||k<0||k>a->len)return out;sp_IntArray*used=sp_IntArray_new();for(mrb_int i=0;i<a->len;i++)sp_IntArray_push(used,0);sp_IntArray*acc=sp_IntArray_new();sp_int_permutation_recur(a,k,used,acc,out);return out;}
13951402 static const char*sp_FloatArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_FloatArray_inspect((sp_FloatArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
13961403 static const char*sp_StrArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_StrArray_inspect((sp_StrArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
13971404 static const char*sp_SymArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_SymArray_inspect((sp_IntArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}