Commit 56c54669
56c546695745Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified
Description
Array#drop/#take, poly-array #max/#min with int-array tuple comparison
- drop(n)/take(n) on all array kinds (incl. poly) lower to slice; analyze types them as the receiver's array kind.
- max/min on a poly array compare boxed elements at runtime: numerics/strings
via sp_poly_cmp, boxed int arrays lexicographically (Array#<=> over int
elements) via the new sp_poly_cmp_int_arrays -- covering the
.map { [delta, idx] }.maxtuple idiom.
Completes the each_cons.with_index.map.drop.take.max[i] enumerator pipeline; fixes bundle_misc_c_23.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 56c546695745
- Parent
- 6f0c5e1985e2
lib/sp_runtime.hmodifiedView file
@@ -3052,6 +3052,21 @@
30523052
static mrb_bool sp_poly_eq(sp_RbVal a, sp_RbVal b) { if (sp_poly_numeric_p(a) && sp_poly_numeric_p(b)) return sp_poly_to_f(a) == sp_poly_to_f(b); if (a.tag != b.tag) return FALSE; switch (a.tag) { case SP_TAG_INT: return a.v.i == b.v.i; case SP_TAG_STR: return (a.v.s == NULL || b.v.s == NULL) ? (a.v.s == b.v.s) : (strcmp(a.v.s, b.v.s) == 0); case SP_TAG_FLT: return a.v.f == b.v.f; case SP_TAG_BOOL: return a.v.b == b.v.b; case SP_TAG_NIL: return TRUE; case SP_TAG_SYM: return a.v.i == b.v.i; case SP_TAG_ENCODING: return (a.v.s == NULL || b.v.s == NULL) ? (a.v.s == b.v.s) : (strcmp(a.v.s, b.v.s) == 0); case SP_TAG_OBJ: if (a.cls_id != b.cls_id) return FALSE; if (a.v.p == b.v.p) return TRUE; switch (a.cls_id) { case SP_BUILTIN_INT_ARRAY: return sp_IntArray_eq((sp_IntArray*)a.v.p,(sp_IntArray*)b.v.p); case SP_BUILTIN_STR_ARRAY: return sp_StrArray_eq((sp_StrArray*)a.v.p,(sp_StrArray*)b.v.p); case SP_BUILTIN_FLT_ARRAY: return sp_FloatArray_eq((sp_FloatArray*)a.v.p,(sp_FloatArray*)b.v.p); case SP_BUILTIN_POLY_ARRAY: return sp_PolyArray_eq((sp_PolyArray*)a.v.p,(sp_PolyArray*)b.v.p); default: return FALSE; } case SP_TAG_CLASS: return a.v.i == b.v.i; default: return FALSE; } }30533053
static const char *(*sp_sym_name_fn)(sp_sym) = NULL;30543054
static mrb_int sp_poly_cmp(sp_RbVal a, sp_RbVal b, mrb_bool *comparable) { if (sp_poly_numeric_p(a) && sp_poly_numeric_p(b)) { mrb_float af = sp_poly_to_f(a), bf = sp_poly_to_f(b); *comparable = TRUE; return (af > bf) - (af < bf); } if (a.tag == SP_TAG_STR && b.tag == SP_TAG_STR) { if (a.v.s == NULL || b.v.s == NULL) { *comparable = (a.v.s == b.v.s); return 0; } *comparable = TRUE; return strcmp(a.v.s, b.v.s); } if (a.tag == SP_TAG_SYM && b.tag == SP_TAG_SYM) { *comparable = TRUE; if (sp_sym_name_fn) { int _r = strcmp(sp_sym_name_fn((sp_sym)a.v.i), sp_sym_name_fn((sp_sym)b.v.i)); return _r; } return (a.v.i > b.v.i) - (a.v.i < b.v.i); } *comparable = FALSE; return 0; }3055+
/* Lexicographic <=> between two boxed int arrays (Array#<=> over int elems),3056+
so Array#max/min/sort work on an array of int pairs ([delta, idx] tuples). */3057+
static mrb_int sp_poly_cmp_int_arrays(sp_RbVal a, sp_RbVal b, mrb_bool *comparable) {3058+
if (a.tag != SP_TAG_OBJ || b.tag != SP_TAG_OBJ ||3059+
a.cls_id != SP_BUILTIN_INT_ARRAY || b.cls_id != SP_BUILTIN_INT_ARRAY) { *comparable = FALSE; return 0; }3060+
sp_IntArray *x = (sp_IntArray *)a.v.p, *y = (sp_IntArray *)b.v.p;3061+
if (!x || !y) { *comparable = FALSE; return 0; }3062+
mrb_int n = x->len < y->len ? x->len : y->len;3063+
for (mrb_int i = 0; i < n; i++) {3064+
mrb_int xe = x->data[x->start + i], ye = y->data[y->start + i];3065+
if (xe != ye) { *comparable = TRUE; return xe < ye ? -1 : 1; }3066+
}3067+
*comparable = TRUE;3068+
return (x->len > y->len) - (x->len < y->len);3069+
}30553070
static mrb_bool sp_poly_lt(sp_RbVal a, sp_RbVal b) { mrb_bool comparable; mrb_int cmp = sp_poly_cmp(a, b, &comparable); return comparable ? (cmp < 0) : FALSE; }30563071
static mrb_bool sp_poly_le(sp_RbVal a, sp_RbVal b) { mrb_bool comparable; mrb_int cmp = sp_poly_cmp(a, b, &comparable); return comparable ? (cmp <= 0) : FALSE; }30573072
static mrb_bool sp_poly_gt(sp_RbVal a, sp_RbVal b) { mrb_bool comparable; mrb_int cmp = sp_poly_cmp(a, b, &comparable); return comparable ? (cmp > 0) : FALSE; }@@ -3641,6 +3656,30 @@
36413656
static void sp_PolyArray_rotate_bang(sp_PolyArray*a,mrb_int n){if(!a)return;if(a->frozen){sp_raise_frozen_array();return;}if(a->len<=0)return;n=((n%a->len)+a->len)%a->len;if(n==0)return;sp_RbVal*d=a->data;mrb_int lo=0,hi=n-1;while(lo<hi){sp_RbVal t=d[lo];d[lo]=d[hi];d[hi]=t;lo++;hi--;}lo=n;hi=a->len-1;while(lo<hi){sp_RbVal t=d[lo];d[lo]=d[hi];d[hi]=t;lo++;hi--;}lo=0;hi=a->len-1;while(lo<hi){sp_RbVal t=d[lo];d[lo]=d[hi];d[hi]=t;lo++;hi--;}}36423657
static sp_PolyArray *sp_PolyArray_shuffle(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_dup(a); sp_PolyArray_shuffle_bang(b); return b; }36433658
static int _sp_poly_cmp_qsort(const void *pa, const void *pb) { mrb_bool ok = FALSE; mrb_int r = sp_poly_cmp(*(const sp_RbVal *)pa, *(const sp_RbVal *)pb, &ok); return ok ? (int)r : 0; }3659+
/* max/min over boxed elements: numerics/strings via sp_poly_cmp, int arrays3660+
lexicographically. Returns nil for an empty array. */3661+
static sp_RbVal sp_PolyArray_max(sp_PolyArray *a) {3662+
if (!a || a->len == 0) return sp_box_nil();3663+
sp_RbVal best = a->data[0];3664+
for (mrb_int i = 1; i < a->len; i++) {3665+
mrb_bool ok = FALSE;3666+
mrb_int r = sp_poly_cmp(a->data[i], best, &ok);3667+
if (!ok) r = sp_poly_cmp_int_arrays(a->data[i], best, &ok);3668+
if (ok && r > 0) best = a->data[i];3669+
}3670+
return best;3671+
}3672+
static sp_RbVal sp_PolyArray_min(sp_PolyArray *a) {3673+
if (!a || a->len == 0) return sp_box_nil();3674+
sp_RbVal best = a->data[0];3675+
for (mrb_int i = 1; i < a->len; i++) {3676+
mrb_bool ok = FALSE;3677+
mrb_int r = sp_poly_cmp(a->data[i], best, &ok);3678+
if (!ok) r = sp_poly_cmp_int_arrays(a->data[i], best, &ok);3679+
if (ok && r < 0) best = a->data[i];3680+
}3681+
return best;3682+
}36443683
static void sp_PolyArray_sort_bang(sp_PolyArray *a) { if (!a || a->frozen) { if (a && a->frozen) sp_raise_frozen_array(); return; } if (a->len > 1) qsort(a->data, (size_t)a->len, sizeof(sp_RbVal), _sp_poly_cmp_qsort); }36453684
static sp_PolyArray *sp_PolyArray_sort(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_dup(a); sp_PolyArray_sort_bang(b); return b; }36463685
static void sp_PolyArray_uniq_bang(sp_PolyArray*a){if(!a||a->frozen){if(a&&a->frozen)sp_raise_frozen_array();return;}for(mrb_int i=0;i<a->len;){int dup=0;for(mrb_int j=0;j<i;j++){if(sp_poly_eq(a->data[j],a->data[i])){dup=1;break;}}if(dup){for(mrb_int k2=i;k2<a->len-1;k2++)a->data[k2]=a->data[k2+1];a->len--;}else i++;}}src/analyze.cmodifiedView file
Loading diff…
src/codegen.cmodifiedView file
Loading diff…