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

@@ -3167,6 +3167,65 @@
31673167 for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_flatten_into_n(b, a->data[i], depth);
31683168 return b;
31693169 }
3170+/* Transpose a poly-array of int-arrays (or int-arrays boxed in poly).
3171+ Used by `Array#transpose` on a matrix whose rows are IntArrays. */
3172+/* sp_poly_array_transpose is defined below, after the sp_poly_length /
3173+ sp_poly_int_get element accessors it relies on. */
3174+/* Runtime dispatch: .length/.size on a boxed array value (sp_RbVal). */
3175+static mrb_int sp_poly_length(sp_RbVal v) {
3176+ if (v.tag != SP_TAG_OBJ) return 0;
3177+ if (v.cls_id == SP_BUILTIN_INT_ARRAY) return sp_IntArray_length((sp_IntArray *)v.v.p);
3178+ if (v.cls_id == SP_BUILTIN_STR_ARRAY) return ((sp_StrArray *)v.v.p)->len;
3179+ if (v.cls_id == SP_BUILTIN_POLY_ARRAY) return ((sp_PolyArray *)v.v.p)->len;
3180+ if (v.cls_id == SP_BUILTIN_FLT_ARRAY) return ((sp_FloatArray *)v.v.p)->len;
3181+ return 0;
3182+}
3183+/* Runtime dispatch: [] on a boxed array value with integer index -> boxed element. */
3184+static sp_RbVal sp_poly_int_get(sp_RbVal v, mrb_int i) {
3185+ if (v.tag != SP_TAG_OBJ) return sp_box_nil();
3186+ if (v.cls_id == SP_BUILTIN_INT_ARRAY) {
3187+ mrb_int val = sp_IntArray_get((sp_IntArray *)v.v.p, i);
3188+ return val == SP_INT_NIL ? sp_box_nil() : sp_box_int(val);
3189+ }
3190+ if (v.cls_id == SP_BUILTIN_POLY_ARRAY) {
3191+ sp_PolyArray *a = (sp_PolyArray *)v.v.p;
3192+ if (i < 0 || i >= a->len) return sp_box_nil();
3193+ return a->data[i];
3194+ }
3195+ if (v.cls_id == SP_BUILTIN_STR_ARRAY) {
3196+ sp_StrArray *a = (sp_StrArray *)v.v.p;
3197+ if (i < 0 || i >= a->len) return sp_box_nil();
3198+ return sp_box_str(sp_StrArray_get(a, i));
3199+ }
3200+ if (v.cls_id == SP_BUILTIN_FLT_ARRAY) {
3201+ sp_FloatArray *a = (sp_FloatArray *)v.v.p;
3202+ if (i < 0 || i >= a->len) return sp_box_nil();
3203+ return sp_box_float(sp_FloatArray_get(a, i));
3204+ }
3205+ return sp_box_nil();
3206+}
3207+/* Array#transpose on a poly_array (untyped nested array). Swaps rows and
3208+ columns, preserving each element's runtime type via sp_poly_int_get
3209+ (int / float / str / poly rows). Returns a poly_array of poly_array
3210+ columns; the statically-typed nested-array paths are handled inline by
3211+ codegen. Row length is taken from the first row (CRuby raises on a
3212+ ragged matrix; here a short row reads nil past its end). */
3213+static sp_PolyArray *sp_poly_array_transpose(sp_PolyArray *rows) {
3214+ SP_GC_SAVE();
3215+ if (!rows || rows->len == 0) return sp_PolyArray_new();
3216+ mrb_int ncols = sp_poly_length(rows->data[0]);
3217+ sp_PolyArray *result = sp_PolyArray_new();
3218+ SP_GC_ROOT(result);
3219+ for (mrb_int j = 0; j < ncols; j++) {
3220+ sp_PolyArray *col = sp_PolyArray_new();
3221+ SP_GC_ROOT(col);
3222+ for (mrb_int i = 0; i < rows->len; i++) {
3223+ sp_PolyArray_push(col, sp_poly_int_get(rows->data[i], j));
3224+ }
3225+ sp_PolyArray_push(result, sp_box_poly_array(col));
3226+ }
3227+ return result;
3228+}
31703229 /* Sum the integer-tagged elements of a poly_array. Used by
31713230 `Array#sum` on a poly_array whose runtime tags are uniform int
31723231 (e.g. the result of `arr.map { _1[:int_key] }`). Non-int tags