Commit c73573c2
c73573c268b2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified
Description
Merge pull request #199 from mame/pr/array-slice-bang
feat(codegen+runtime): Array#slice!(from, n) for every typed array
Details
- Commit
- c73573c268b2
- Parent
- aeb3b363b43f
- Merge parent
- b1f7cf6aa4d0
lib/sp_runtime.hmodifiedView file
@@ -720,6 +720,18 @@
720720
static mrb_int sp_PolyArray_length(sp_PolyArray *a) { return a->len; }721721
static sp_RbVal sp_PolyArray_get(sp_PolyArray *a, mrb_int i) { if (i < 0) i += a->len; return a->data[i]; }722722
static sp_PolyArray *sp_PolyArray_slice(sp_PolyArray *a, mrb_int start, mrb_int len) { if (start < 0) start += a->len; if (start < 0) start = 0; sp_PolyArray *b = sp_PolyArray_new(); if (start >= a->len || len <= 0) return b; if (start + len > a->len) len = a->len - start; for (mrb_int i = 0; i < len; i++) sp_PolyArray_push(b, a->data[start + i]); return b; }723+
static sp_PolyArray *sp_PolyArray_slice_bang(sp_PolyArray *a, mrb_int from, mrb_int n) {724+
if (from < 0) from += a->len;725+
if (from < 0) from = 0;726+
if (from > a->len) from = a->len;727+
if (n < 0) n = 0;728+
if (from + n > a->len) n = a->len - from;729+
sp_PolyArray *r = sp_PolyArray_new();730+
for (mrb_int i = 0; i < n; i++) sp_PolyArray_push(r, a->data[from + i]);731+
for (mrb_int i = from; i + n < a->len; i++) a->data[i] = a->data[i + n];732+
a->len -= n;733+
return r;734+
}723735
static sp_PolyArray *sp_PolyArray_dup(sp_PolyArray *a) { sp_PolyArray *b = sp_PolyArray_new(); for (mrb_int i = 0; i < a->len; i++) sp_PolyArray_push(b, a->data[i]); return b; }724736
static void sp_PolyArray_shuffle_bang(sp_PolyArray *a) { for (mrb_int i = a->len - 1; i > 0; i--) { mrb_int j = (mrb_int)(rand() % (i + 1)); sp_RbVal t = a->data[i]; a->data[i] = a->data[j]; a->data[j] = t; } }725737
static void sp_PolyArray_rotate_bang(sp_PolyArray*a,mrb_int n){if(a->len<=0)return;n=((n%a->len)+a->len)%a->len;if(n==0)return;sp_RbVal*tmp=(sp_RbVal*)malloc(sizeof(sp_RbVal)*a->len);for(mrb_int i=0;i<a->len;i++)tmp[i]=a->data[(i+n)%a->len];for(mrb_int i=0;i<a->len;i++)a->data[i]=tmp[i];free(tmp);}@@ -850,6 +862,64 @@
850862
return a;851863
}852864
865+
/* `arr.slice!(from, n)` — returns a fresh array of `n` elements866+
starting at `from` and removes them from `a`. IntArray uses its867+
`start` field for an O(1) head peel (from == 0); the others868+
shift the tail down to fill the hole. */869+
static sp_IntArray *sp_IntArray_slice_bang(sp_IntArray *a, mrb_int from, mrb_int n) {870+
if (from < 0) from += a->len;871+
if (from < 0) from = 0;872+
if (from > a->len) from = a->len;873+
if (n < 0) n = 0;874+
if (from + n > a->len) n = a->len - from;875+
sp_IntArray *r = sp_IntArray_new();876+
for (mrb_int i = 0; i < n; i++) sp_IntArray_push(r, a->data[a->start + from + i]);877+
if (from == 0) {878+
a->start += n;879+
a->len -= n;880+
} else {881+
for (mrb_int i = from; i + n < a->len; i++) a->data[a->start + i] = a->data[a->start + i + n];882+
a->len -= n;883+
}884+
return r;885+
}886+
static sp_FloatArray *sp_FloatArray_slice_bang(sp_FloatArray *a, mrb_int from, mrb_int n) {887+
if (from < 0) from += a->len;888+
if (from < 0) from = 0;889+
if (from > a->len) from = a->len;890+
if (n < 0) n = 0;891+
if (from + n > a->len) n = a->len - from;892+
sp_FloatArray *r = sp_FloatArray_new();893+
for (mrb_int i = 0; i < n; i++) sp_FloatArray_push(r, a->data[from + i]);894+
for (mrb_int i = from; i + n < a->len; i++) a->data[i] = a->data[i + n];895+
a->len -= n;896+
return r;897+
}898+
static sp_StrArray *sp_StrArray_slice_bang(sp_StrArray *a, mrb_int from, mrb_int n) {899+
if (from < 0) from += a->len;900+
if (from < 0) from = 0;901+
if (from > a->len) from = a->len;902+
if (n < 0) n = 0;903+
if (from + n > a->len) n = a->len - from;904+
sp_StrArray *r = sp_StrArray_new();905+
for (mrb_int i = 0; i < n; i++) sp_StrArray_push(r, a->data[from + i]);906+
for (mrb_int i = from; i + n < a->len; i++) a->data[i] = a->data[i + n];907+
a->len -= n;908+
return r;909+
}910+
static sp_PtrArray *sp_PtrArray_slice_bang(sp_PtrArray *a, mrb_int from, mrb_int n) {911+
if (from < 0) from += a->len;912+
if (from < 0) from = 0;913+
if (from > a->len) from = a->len;914+
if (n < 0) n = 0;915+
if (from + n > a->len) n = a->len - from;916+
sp_PtrArray *r = sp_PtrArray_new_scan(a->scan_elem);917+
for (mrb_int i = 0; i < n; i++) sp_PtrArray_push(r, a->data[from + i]);918+
for (mrb_int i = from; i + n < a->len; i++) a->data[i] = a->data[i + n];919+
a->len -= n;920+
return r;921+
}922+
853923
typedef struct sp_Proc { void *fn; void *cap; void (*cap_scan)(void *); } sp_Proc;854924
static void sp_Proc_scan(void *p) { sp_Proc *pr = (sp_Proc *)p; if (pr->cap && pr->cap_scan) pr->cap_scan(pr->cap); }855925
static sp_Proc *sp_proc_new(void *fn, void *cap, void (*cap_scan)(void *)) { sp_Proc *p = (sp_Proc *)sp_gc_alloc(sizeof(sp_Proc), NULL, sp_Proc_scan); p->fn = fn; p->cap = cap; p->cap_scan = cap_scan; return p; }spinel_codegen.rbmodifiedView file
Loading diff…
test/array_slice_bang.rbaddedView file
Loading diff…