From 4ec83e51a69625ae6f36975171cdc2534168f761 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sat, 27 Aug 2022 10:48:21 -0400 Subject: [PATCH] Merge sort.h into grade.h --- src/builtins/grade.h | 171 +++++++++++++++++++++++++++++++++++++++++-- src/builtins/sort.c | 11 +-- src/builtins/sort.h | 156 --------------------------------------- 3 files changed, 168 insertions(+), 170 deletions(-) delete mode 100644 src/builtins/sort.h diff --git a/src/builtins/grade.h b/src/builtins/grade.h index d0e6a3d4..7d78ea1e 100644 --- a/src/builtins/grade.h +++ b/src/builtins/grade.h @@ -1,4 +1,11 @@ #define GRADE_CAT(N) CAT(GRADE_UD(gradeUp,gradeDown),N) +#define GRADE_NEG GRADE_UD(,-) + +// Timsort +#define SORT_CMP(W, X) GRADE_NEG compare(W, X) +#define SORT_NAME GRADE_UD(bA,bD) +#define SORT_TYPE B +#include "sortTemplate.h" #define SORT_CMP(W, X) GRADE_NEG compare((W).k, (X).k) #define SORT_NAME GRADE_CAT(BP) @@ -10,6 +17,153 @@ #define SORT_TYPE I32I32p #include "sortTemplate.h" + +#define LT GRADE_UD(<,>) +#define FOR(I,MAX) GRADE_UD(for (usz I=0; I>=), PRE_UD(K,>>,<<=)) + +#define INSERTION_SORT(T) \ + rp[0] = xp[0]; \ + for (usz i=0; ie; j--) { i+=c0[j]; rp[i]--; } \ + ) \ + for (usz i=1; i>8)]++; } \ + PRE_SUM; \ + i16 *r0 = (i16*)(c0+2*256); \ + for (usz i=0; i>8)]++; rp[c]=v; } \ + TFREE(alloc) + RADIX2(u8, + u64 s0=0; u64 s1=0; + FOR(j, 256/8) { PRE64(0); PRE64(1); } + ); + } else if (n < 1<<15) { // Radix sort + RADIX2(u32, + u32 s0=0; u32 s1=0; + FOR(j, 256) { + u32 p0=s0; s0+=c0[j]; c0[j]=p0; + u32 p1=s1; s1+=c1[j]; c1[j]=p1; + } + ); + #undef RADIX2 + } else { + COUNTING_SORT(i16); + } + } else if (xe==el_i32) { + i32* xp = i32any_ptr(x); + i32* rp; r = m_i32arrv(&rp, n); + if (n < 40) { + INSERTION_SORT(i32); + } else if (n <= 256) { + #define RADIX4(T, PRE_SUM) \ + TALLOC(u8, alloc, 4*256*sizeof(T) + n*4); \ + T *c0=(T*)alloc, *c1=c0+256, *c2=c1+256, *c3=c2+256, *c3o=c3+128; \ + for (usz j=0; j<4*256; j++) c0[j]=0; \ + for (usz i=0; i> 8)]++; \ + c2 [(u8)(v>>16)]++; c3o[(i8)(v>>24)]++; } \ + PRE_SUM; \ + i32 *r0 = (i32*)(c0+4*256); \ + for (usz i=0; i> 8)]++; rp[c]=v; } \ + for (usz i=0; i>16)]++; r0[c]=v; } \ + for (usz i=0; i>24)]++; rp[c]=v; } \ + TFREE(alloc) + RADIX4(u8, + u64 s0=0; u64 s1=0; u64 s2=0; u64 s3=0; + FOR(j, 256/8) { PRE64(0); PRE64(1); PRE64(2); PRE64(3); } + ); + } else { + RADIX4(usz, + usz s0=0; usz s1=0; usz s2=0; usz s3=0; + FOR(j, 256) { + u32 p0=s0; s0+=c0[j]; c0[j]=p0; + u32 p1=s1; s1+=c1[j]; c1[j]=p1; + u32 p2=s2; s2+=c2[j]; c2[j]=p2; + u32 p3=s3; s3+=c3[j]; c3[j]=p3; + } + ); + #undef RADIX4 + } + } else { + B xf = getFillQ(x); + HArr_p ra = m_harrUv(n); + SGet(x) + for (usz i = 0; i < n; i++) ra.a[i] = Get(x,i); + CAT(GRADE_UD(bA,bD),tim_sort)(ra.a, n); + r = withFill(ra.b,xf); + } + decG(x); + return FL_SET(r, CAT(fl,GRADE_UD(asc,dsc))); +} +#undef SORT_C1 +#undef INSERTION_SORT +#undef COUNTING_SORT + + +#define GRADE_CHR GRADE_UD("⍋","⍒") B GRADE_CAT(c1)(B t, B x) { if (isAtm(x) || RNK(x)==0) thrM(GRADE_CHR": Argument cannot be a unit"); if (RNK(x)>1) x = toCells(x); @@ -82,7 +236,7 @@ B GRADE_CAT(c1)(B t, B x) { return r; } - SLOW1(GRADE_UD("⍋","⍒")"𝕩", x); + SLOW1(GRADE_CHR"𝕩", x); TALLOC(BI32p, tmp, ia); SGetU(x) for (usz i = 0; i < ia; i++) { @@ -94,6 +248,8 @@ B GRADE_CAT(c1)(B t, B x) { TFREE(tmp); decG(x); return r; } + + B GRADE_CAT(c2)(B t, B w, B x) { if (isAtm(w) || RNK(w)==0) thrM(GRADE_CHR": 𝕨 must have rank≥1"); if (isAtm(x)) x = m_atomUnit(x); @@ -128,14 +284,14 @@ B GRADE_CAT(c2)(B t, B w, B x) { usz s = 0, e = wia+1; while (e-s > 1) { usz m = (s+(i64)e)/2; - if (c GRADE_UD(<,>) wi[m-1]) e = m; + if (c LT wi[m-1]) e = m; else s = m; } rp[i] = s; } } else { SGetU(x) - SLOW2("𝕨"GRADE_UD("⍋","⍒")"𝕩", w, x); + SLOW2("𝕨"GRADE_CHR"𝕩", w, x); B* wp = arr_bptr(w); if (wp==NULL) { HArr* a = toHArr(w); @@ -152,7 +308,7 @@ B GRADE_CAT(c2)(B t, B w, B x) { usz s = 0, e = wia+1; while (e-s > 1) { usz m = (s+e) / 2; - if (compare(c, wp[m-1]) GRADE_UD(<,>) 0) e = m; + if (compare(c, wp[m-1]) LT 0) e = m; else s = m; } rp[i] = s; @@ -161,7 +317,12 @@ B GRADE_CAT(c2)(B t, B w, B x) { decG(w);decG(x); return r; } -#undef GRADE_CAT #undef GRADE_CHR + +#undef LT +#undef FOR +#undef PRE_UD +#undef PRE64 +#undef GRADE_CAT #undef GRADE_NEG #undef GRADE_UD diff --git a/src/builtins/sort.c b/src/builtins/sort.c index d270c073..2371dad7 100644 --- a/src/builtins/sort.c +++ b/src/builtins/sort.c @@ -1,21 +1,14 @@ #include "../core.h" #include "../utils/talloc.h" +// Defines Sort, Grade, and Bins + #define CAT0(A,B) A##_##B #define CAT(A,B) CAT0(A,B) typedef struct BI32p { B k; i32 v; } BI32p; typedef struct I32I32p { i32 k; i32 v; } I32I32p; #define GRADE_UD(U,D) U -#define GRADE_NEG -#define GRADE_CHR "⍋" #include "grade.h" #define GRADE_UD(U,D) D -#define GRADE_NEG - -#define GRADE_CHR "⍒" #include "grade.h" - -#define SORT_UD(U,D) U -#include "sort.h" -#define SORT_UD(U,D) D -#include "sort.h" diff --git a/src/builtins/sort.h b/src/builtins/sort.h deleted file mode 100644 index a2195dba..00000000 --- a/src/builtins/sort.h +++ /dev/null @@ -1,156 +0,0 @@ -#define SORT_C1 CAT(SORT_UD(and,or),c1) -#define LT SORT_UD(<,>) - -#define TIM_B SORT_UD(bA,bD) - -#define SORT_CMP(W, X) SORT_UD(compare(W, X), compare(X, W)) -#define SORT_NAME TIM_B -#define SORT_TYPE B -#include "sortTemplate.h" - -#define FOR(I,MAX) SORT_UD(for (usz I=0; I>=), PRE_UD(K,>>,<<=)) - -#define INSERTION_SORT(T) \ - rp[0] = xp[0]; \ - for (usz i=0; ie; j--) { i+=c0[j]; rp[i]--; } \ - ) \ - for (usz i=1; i>8)]++; } \ - PRE_SUM; \ - i16 *r0 = (i16*)(c0+2*256); \ - for (usz i=0; i>8)]++; rp[c]=v; } \ - TFREE(alloc) - RADIX2(u8, - u64 s0=0; u64 s1=0; - FOR(j, 256/8) { PRE64(0); PRE64(1); } - ); - } else if (n < 1<<15) { // Radix sort - RADIX2(u32, - u32 s0=0; u32 s1=0; - FOR(j, 256) { - u32 p0=s0; s0+=c0[j]; c0[j]=p0; - u32 p1=s1; s1+=c1[j]; c1[j]=p1; - } - ); - #undef RADIX2 - } else { - COUNTING_SORT(i16); - } - } else if (xe==el_i32) { - i32* xp = i32any_ptr(x); - i32* rp; r = m_i32arrv(&rp, n); - if (n < 40) { - INSERTION_SORT(i32); - } else if (n <= 256) { - #define RADIX4(T, PRE_SUM) \ - TALLOC(u8, alloc, 4*256*sizeof(T) + n*4); \ - T *c0=(T*)alloc, *c1=c0+256, *c2=c1+256, *c3=c2+256, *c3o=c3+128; \ - for (usz j=0; j<4*256; j++) c0[j]=0; \ - for (usz i=0; i> 8)]++; \ - c2 [(u8)(v>>16)]++; c3o[(i8)(v>>24)]++; } \ - PRE_SUM; \ - i32 *r0 = (i32*)(c0+4*256); \ - for (usz i=0; i> 8)]++; rp[c]=v; } \ - for (usz i=0; i>16)]++; r0[c]=v; } \ - for (usz i=0; i>24)]++; rp[c]=v; } \ - TFREE(alloc) - RADIX4(u8, - u64 s0=0; u64 s1=0; u64 s2=0; u64 s3=0; - FOR(j, 256/8) { PRE64(0); PRE64(1); PRE64(2); PRE64(3); } - ); - } else { - RADIX4(usz, - usz s0=0; usz s1=0; usz s2=0; usz s3=0; - FOR(j, 256) { - u32 p0=s0; s0+=c0[j]; c0[j]=p0; - u32 p1=s1; s1+=c1[j]; c1[j]=p1; - u32 p2=s2; s2+=c2[j]; c2[j]=p2; - u32 p3=s3; s3+=c3[j]; c3[j]=p3; - } - ); - #undef RADIX4 - } - } else { - B xf = getFillQ(x); - HArr_p ra = m_harrUv(n); - SGet(x) - for (usz i = 0; i < n; i++) ra.a[i] = Get(x,i); - CAT(TIM_B,tim_sort)(ra.a, n); - r = withFill(ra.b,xf); - } - decG(x); - return FL_SET(r, CAT(fl,SORT_UD(asc,dsc))); -} -#undef SORT_C1 -#undef LT -#undef TIM_B -#undef FOR -#undef PRE_UD -#undef PRE64 -#undef INSERTION_SORT -#undef COUNTING_SORT -#undef SORT_UD