Commit 6d7b2ae1
6d7b2ae1edc4Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +1 added · ~3 modified
Description
runtime,codegen: Array#product two-array Cartesian for int_array
`[1, 2].product([3, 4])` produces `[[1,3], [1,4], [2,3], [2,4]]`. sp_IntArray_product nested-loops the two arrays and emits an sp_PtrArray of sp_IntArray pairs; analyzer marks the return type as int_array_ptr_array so .inspect routes correctly.
Variadic (3+ arrays) is left for a follow-up — needs a recursive helper and the spinel use cases so far are all 2-array.
Fixes #939
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 6d7b2ae1edc4
- Parent
- 10746b571fe3
lib/sp_runtime.hmodifiedView file
@@ -1846,6 +1846,22 @@
18461846
k-element ordered combinations as a PtrArray of IntArrays. */18471847
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--;}}18481848
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;}1849+
1850+
/* Cartesian product of two int arrays. Returns a PtrArray of1851+
2-element IntArrays. */1852+
static sp_PtrArray *sp_IntArray_product(sp_IntArray *a, sp_IntArray *b) {1853+
sp_PtrArray *out = sp_PtrArray_new();1854+
if (!a || !b) return out;1855+
for (mrb_int i = 0; i < a->len; i++) {1856+
for (mrb_int j = 0; j < b->len; j++) {1857+
sp_IntArray *pair = sp_IntArray_new();1858+
sp_IntArray_push(pair, a->data[a->start + i]);1859+
sp_IntArray_push(pair, b->data[b->start + j]);1860+
sp_PtrArray_push(out, pair);1861+
}1862+
}1863+
return out;1864+
}18491865
/* Array#permutation(k) -- ordered k-permutations. */18501866
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;}}18511867
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;}spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/array_product.rbaddedView file
Loading diff…