void ssort2(char **a, int n, int depth,int* index) { int d, r, partval; int Diff=0; char **pa, **pb, **pc, **pd, **pl, **pm, **pn, *t; //~ if (n < 10) { //~ inssort(a, n, depth); //~ return; //~ } pl = a; pm = a + (n/2); pn = a + (n-1); if (n > 30) { // On big arrays, pseudomedian of 9 d = (n/8); pl = med3(pl, pl+d, pl+2*d); pm = med3(pm-d, pm, pm+d); pn = med3(pn-2*d, pn-d, pn); } pm = med3(pl, pm, pn); Diff=pm-a; int *IndexA,*IndexB,*IndexC,*IndexD,*IndexN; swap2(a, pm); swap2Index(index,index+Diff); partval = ptr2char(a); pa = pb = a + 1; pc = pd = a + n-1; IndexA=IndexB=index+1; IndexC=IndexD=index+n-1; for (;;) { while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) { if (r == 0) { swap2(pa, pb);swap2Index(IndexA, IndexB);pa++;IndexA++; } pb++; IndexB++; } while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) { if (r == 0) { swap2(pc, pd); swap2Index(IndexC, IndexD);pd--;IndexD-- ;} pc--; IndexC--; } if (pb > pc) break; swap2(pb, pc); swap2Index(IndexB,IndexC); pb++; IndexB++; pc--; IndexC--; } pn = a + n; IndexN=index+n; r = min(pa-a, pb-pa); vecswap2(a, pb-r, r); vecswap2Index(index,IndexB-r,r); r = min(pd-pc, pn-pd-1); vecswap2(pb, pn-r, r); vecswap2Index(IndexB,IndexN-r,r); if ((r = pb-pa) > 1) ssort2(a, r, depth,index); if (ptr2char(a + r) != 0) ssort2(a + r, pa-a + pn-pd-1, depth+1,index+r); if ((r = pd-pc) > 1) ssort2(a + n-r, r, depth,index+n-r); }
__inline__ Int32 *med3func(Int32 *a, Int32 *b, Int32 *c, UChar *text_depth) { int va, vb, vc; if ((va=ptr2char(a)) == (vb=ptr2char(b))) return a; if ((vc=ptr2char(c)) == va || vc == vb) return c; return va < vb ? (vb < vc ? b : (va < vc ? c : a ) ) : (vb > vc ? b : (va < vc ? a : c ) ); }
static string *med3func(string *a, string *b, string *c, int depth) { int va, vb, vc; if ((va=ptr2char(a)) == (vb=ptr2char(b))) return a; if ((vc=ptr2char(c)) == va || vc == vb) return c; return va < vb ? (vb < vc ? b : (va < vc ? c : a ) ) : (vb > vc ? b : (va < vc ? a : c ) ); }
static Int32 split_group(UInt32 *a, int n, int depth,int offset,Int32 pivot,int *first) { int r, partval; UInt32 *pa, *pb, *pc, *pd, *pa_old, *pd_old; Int32 pivot_pos, t; UChar *text_depth,*text_limit; /* --------- initialization ------------------------------------ */ pivot_pos = a[pivot]; /* starting position in T[] of pivot */ text_depth = Text+depth; text_limit = text_depth+offset; /* ------------------------------------------------------------- */ /* In the following for() loop: */ /* [pa ... pd] is the current working region, */ /* pb moves from pa towards pd */ /* pc moves from pd towards pa */ /* ------------------------------------------------------------- */ pa = a; pd = a + n-1; for( ; pa!=pd && (text_depth<text_limit); text_depth++) { assert(pa<pd); /* ------ the pivot char is Text[pivot_pos+depth] where */ /* depth = text_depth-Text. This is text_depth[pivot_pos] */ partval = text_depth[pivot_pos]; /* ----- partition ------------ */ pb = pa_old = pa; pc = pd_old = pd; for (;;) { while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) { if (r == 0) { swap2(pa, pb); pa++; } pb++; } while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) { if (r == 0) { swap2(pc, pd); pd--; } pc--; } if (pb > pc) break; swap2(pb, pc); pb++; pc--; } r = min((Int32*)pa-(Int32*)pa_old, (Int32*)pb-(Int32*)pa); vecswap2(pa_old, pb-r, r); r = min((Int32*)pd-(Int32*)pc, (Int32*)pd_old-(Int32*)pd); vecswap2(pb, pd_old+1-r, r); /* ------ compute new boundaries ----- */ pa = pa_old + (pb-pa); /* there are pb-pa chars < partval */ pd = pd_old - (pd-pc); /* there are pd-pc chars > partval */ } *first=pa-a; /* index in a[] of the first suf. equal to pivot */ assert(pd-pa>=0); return pd-pa+1; /* return number of suffixes equal to pivot */ }
static void do_test_run(struct code_chunks *chunk) { /* type-1 function */ char df1[] = { 0x64, 0x00, 0x00, 0x00, /* 100 */ 'x', 0x00, 0x00, 0x00, /* 'x' */ 0xC8, 0x00, 0x00, 0x00, /* 200 */ }; /* type-2 function */ char *name = "mygirl"; char df2[] = { 0x00, 0x00, 0x00, 0x00, /* name */ 0x04, 0x00, 0x00, 0x00 /* 4 */ }; /* type-3 function */ int fibo; char df3[] = { 0x0f, 0x00, 0x00, 0x00, /* 15 */ 0x00, 0x00, 0x00, 0x00 /* fibo */ }; char number[15]; char df4[] = { 0xde, 0xad, 0xbe, 0xef, 0x00, 0x00, 0x00, 0x00 /* number */ }; char df5[] = { }; ptr2char(df2, name); ptr2char((char *)df3 + 4, &fibo); ptr2char((char *)df4 + 4, number); call_function(chunk, FUNC_TYPE1, df1); call_function(chunk, FUNC_TYPE2, df2); call_function(chunk, FUNC_TYPE3, df3); printf("%s: fibo value returned: %d\n", __func__, fibo); call_function(chunk, FUNC_TYPE5, df5); call_function(chunk, FUNC_TYPE4, df4); printf("%s: original: %d, reversed: %s\n", __func__, *(int *)df4, number); }
void ssort2(unsigned char **a, int n, int depth) { int d, r, partval; unsigned char **pa, **pb, **pc, **pd, **pl, **pm, **pn, *t; if (n < 10) { inssort(a, n, depth); return; } pl = a; pm = a + (n/2); pn = a + (n-1); if (n > 30) { d = (n/8); pl = med3(pl, pl+d, pl+2*d); pm = med3(pm-d, pm, pm+d); pn = med3(pn-2*d, pn-d, pn); } pm = med3(pl, pm, pn); swap2(a, pm); partval = ptr2char(a); pa = pb = a + 1; pc = pd = a + n-1; for (;;) { while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) { if (r == 0) { swap2(pa, pb); pa++; } pb++; } while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) { if (r == 0) { swap2(pc, pd); pd--; } pc--; } if (pb > pc) break; swap2(pb, pc); pb++; pc--; } pn = a + n; r = min(pa-a, pb-pa); vecswap2(a, pb-r, r); r = min(pd-pc, pn-pd-1); vecswap2(pb, pn-r, r); if ((r = pb-pa) > 1) ssort2(a, r, depth); if (ptr2char(a + r) != 0) ssort2(a + r, pa-a + pn-pd-1, depth+1); if ((r = pd-pc) > 1) ssort2(a + n-r, r, depth); }
/* ******************************************************** recursive multikey quicksort from Bentley-Sedgewick stops when text_depth reaches Shallow_depth_limit that is when we have found that the current set of strings have Shallow_limit chars in common ******************************************************** */ static void shallow_mkq(Int32 *a, int n, UChar *text_depth) { __inline__ void vecswap2(Int32 *a, Int32 *b, int n); int d, r, partval; Int32 *pa, *pb, *pc, *pd, *pl, *pm, *pn, t; UChar *next_depth; // ---- On small arrays use insertions sort if (n < Mk_qs_thresh) { shallow_inssort_lcp(a, n, text_depth); return; } // ----------- choose pivot -------------- repeat: pl = a; pm = a + (n/2); pn = a + (n-1); if (n > 30) { // On big arrays, pseudomedian of 9 d = (n/8); pl = med3(pl, pl+d, pl+2*d); pm = med3(pm-d, pm, pm+d); pn = med3(pn-2*d, pn-d, pn); } pm = med3(pl, pm, pn); swap2(a, pm); partval = ptr2char(a); pa = pb = a + 1; pc = pd = a + n-1; // -------- partition ----------------- for (;;) { while (pb <= pc && (r = ptr2char(pb)-partval) <= 0) { if (r == 0) { swap2(pa, pb); pa++; } pb++; } while (pb <= pc && (r = ptr2char(pc)-partval) >= 0) { if (r == 0) { swap2(pc, pd); pd--; } pc--; } if (pb > pc) break; swap2(pb, pc); pb++; pc--; } #if UNROLL if(pa>pd) { // all values were equal to partval: make it simpler if( (next_depth = text_depth+1) >= Shallow_text_limit) { helped_sort(a, n, next_depth-Text, Shallow_limit); return; } else { text_depth = next_depth; goto repeat; } } #endif // partition a[] into the values smaller, equal, and larger that partval pn = a + n; r = min(pa-a, pb-pa); vecswap2(a, pb-r, r); r = min(pd-pc, pn-pd-1); vecswap2(pb, pn-r, r); // --- sort smaller strings ------- if ((r = pb-pa) > 1) shallow_mkq(a, r, text_depth); // --- sort strings starting with partval ----- if( (next_depth = text_depth+1) < Shallow_text_limit) shallow_mkq(a + r, pa-pd+n-1, next_depth); else helped_sort(a + r, pa-pd+n-1, next_depth-Text, Shallow_limit); if ((r = pd-pc) > 1) shallow_mkq(a + n-r, r, text_depth); }