Commit 3e40da57
3e40da571ba7Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +1 added · ~3 modified
Description
Transpose an untyped nested array (poly_array), preserving element types
Array#transpose on a poly_array -- a nested array built dynamically or
returned from a method, so its static type is poly_array rather than a
typed *_array_ptr_array -- errored with "cannot resolve call to
'transpose' on poly_array". Only the statically-typed int/str/float
nested-array shapes were specialized (inline codegen).
Add the poly fallback: sp_poly_array_transpose builds a poly_array of poly_array columns, reading each element through sp_poly_int_get (extended here to str_array / float_array rows). analyze types the result as poly_array; codegen dispatches poly_array#transpose to it.
a = []; a.push([1, 2]); a.push([3, 4]); p a.transpose # [[1, 3], [2, 4]]
works for int / float / str / mixed rows, matching CRuby.
This covers the transpose case of #1369's secondary facet (methods
unresolved on a genuinely-poly array); the primary PtrArray->poly_array
degradation and poly_array#delete_at remain.
Refs #1369
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 3e40da571ba7
- Parent
- 931b98e6276c
lib/sp_runtime.hmodifiedView file
for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_flatten_into_n(b, a->data[i], depth); return b;}/* Transpose a poly-array of int-arrays (or int-arrays boxed in poly). Used by `Array#transpose` on a matrix whose rows are IntArrays. *//* sp_poly_array_transpose is defined below, after the sp_poly_length / sp_poly_int_get element accessors it relies on. *//* Runtime dispatch: .length/.size on a boxed array value (sp_RbVal). */static mrb_int sp_poly_length(sp_RbVal v) { if (v.tag != SP_TAG_OBJ) return 0; if (v.cls_id == SP_BUILTIN_INT_ARRAY) return sp_IntArray_length((sp_IntArray *)v.v.p); if (v.cls_id == SP_BUILTIN_STR_ARRAY) return ((sp_StrArray *)v.v.p)->len; if (v.cls_id == SP_BUILTIN_POLY_ARRAY) return ((sp_PolyArray *)v.v.p)->len; if (v.cls_id == SP_BUILTIN_FLT_ARRAY) return ((sp_FloatArray *)v.v.p)->len; return 0;}/* Runtime dispatch: [] on a boxed array value with integer index -> boxed element. */static sp_RbVal sp_poly_int_get(sp_RbVal v, mrb_int i) { if (v.tag != SP_TAG_OBJ) return sp_box_nil(); if (v.cls_id == SP_BUILTIN_INT_ARRAY) { mrb_int val = sp_IntArray_get((sp_IntArray *)v.v.p, i); return val == SP_INT_NIL ? sp_box_nil() : sp_box_int(val); } if (v.cls_id == SP_BUILTIN_POLY_ARRAY) { sp_PolyArray *a = (sp_PolyArray *)v.v.p; if (i < 0 || i >= a->len) return sp_box_nil(); return a->data[i]; } if (v.cls_id == SP_BUILTIN_STR_ARRAY) { sp_StrArray *a = (sp_StrArray *)v.v.p; if (i < 0 || i >= a->len) return sp_box_nil(); return sp_box_str(sp_StrArray_get(a, i)); } if (v.cls_id == SP_BUILTIN_FLT_ARRAY) { sp_FloatArray *a = (sp_FloatArray *)v.v.p; if (i < 0 || i >= a->len) return sp_box_nil(); return sp_box_float(sp_FloatArray_get(a, i)); } return sp_box_nil();}/* Array#transpose on a poly_array (untyped nested array). Swaps rows and columns, preserving each element's runtime type via sp_poly_int_get (int / float / str / poly rows). Returns a poly_array of poly_array columns; the statically-typed nested-array paths are handled inline by codegen. Row length is taken from the first row (CRuby raises on a ragged matrix; here a short row reads nil past its end). */static sp_PolyArray *sp_poly_array_transpose(sp_PolyArray *rows) { SP_GC_SAVE(); if (!rows || rows->len == 0) return sp_PolyArray_new(); mrb_int ncols = sp_poly_length(rows->data[0]); sp_PolyArray *result = sp_PolyArray_new(); SP_GC_ROOT(result); for (mrb_int j = 0; j < ncols; j++) { sp_PolyArray *col = sp_PolyArray_new(); SP_GC_ROOT(col); for (mrb_int i = 0; i < rows->len; i++) { sp_PolyArray_push(col, sp_poly_int_get(rows->data[i], j)); } sp_PolyArray_push(result, sp_box_poly_array(col)); } return result;}/* Sum the integer-tagged elements of a poly_array. Used by `Array#sum` on a poly_array whose runtime tags are uniform int (e.g. the result of `arr.map { _1[:int_key] }`). Non-int tagsspinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/array_transpose_poly_elements.rbaddedView file
Loading diff…