1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <config.h>
#ifdef DMALLOC
#include <dmalloc.h>
#endif
#include "suck_config.h"
#include "suck.h"
#include "suckutils.h"
#include "ssort.h"
/* THREE-WAY RADIX QUICKSORT, faster version */
/* From Dr Dobb's Journal Nov 1999 */
/* modified to work with PList vice strings */
/* prototypes */
void swap(PList *, int, int);
void vecswap(PList *, int, int, int);
int med3func(PList *, int, int, int, int);
void inssort(PList *, int, int);
/* Support functions */
#ifndef min
#define min(a, b) ((a)<=(b) ? (a) : (b))
#endif
/*-------------------------------------------------------------*/
void swap(PList *a, int i, int j) {
PList t = a[i];
a[i] = a[j];
a[j] = t;
}
/*--------------------------------------------------------------*/
void vecswap(PList *a, int i, int j, int n) {
while (n-- > 0) {
swap(a, i++, j++);
}
}
/*---------------------------------------------------------------*/
int med3func(PList *a, int ia, int ib, int ic, int depth) {
int va, vb, vc;
if ((va=a[ia]->msgnr[depth]) == (vb=a[ib]->msgnr[depth])) {
return ia;
}
if ((vc=a[ic]->msgnr[depth]) == va || vc == vb) {
return ic;
}
return va < vb ?
(vb < vc ? ib : (va < vc ? ic : ia ) )
: (vb > vc ? ib : (va < vc ? ia : ic ) );
}
/*-----------------------------------------------------------------*/
void inssort(PList *a, int n, int depth) {
int i, j;
for (i = 1; i < n; i++) {
for (j = i; j > 0; j--) {
if (strcmp(&(a[j-1]->msgnr[depth]), &(a[j]->msgnr[depth])) <= 0) {
break;
}
swap(a, j, j-1);
}
}
}
/*-----------------------------------------------------------------*/
void ssort(PList *a, int n, int depth) {
int le, lt, gt, ge, r, v;
int pl, pm, pn, d;
if (n <= 10) {
inssort(a, n, depth);
return;
}
pl = 0;
pm = n/2;
pn = n-1;
if (n > 50) {
d = n/8;
pl = med3func(a, pl, pl+d, pl+2*d,depth);
pm = med3func(a, pm-d, pm, pm+d,depth);
pn = med3func(a, pn-2*d, pn-d, pn,depth);
}
pm = med3func(a, pl, pm, pn,depth);
swap(a, 0, pm);
v = a[0]->msgnr[depth];
for (le = 1; le < n && a[le]->msgnr[depth] == v; le++) {
;
}
if (le == n) {
if (v != 0) {
ssort(a, n, depth+1);
}
return;
}
lt = le;
gt = ge = n-1;
for (;;) {
for ( ; lt <= gt && a[lt]->msgnr[depth] <= v; lt++) {
if (a[lt]->msgnr[depth] == v) {
swap(a, le++, lt);
}
}
for ( ; lt <= gt && a[gt]->msgnr[depth] >= v; gt--) {
if (a[gt]->msgnr[depth] == v) {
swap(a, gt, ge--);
}
}
if (lt > gt) {
break;
}
swap(a, lt++, gt--);
}
r = min(le, lt-le);
vecswap(a, 0, lt-r, r);
r = min(ge-gt, n-ge-1);
vecswap(a, lt, n-r, r);
ssort(a, lt-le, depth);
if (v != 0) {
ssort(a + lt-le, le + n-ge-1, depth+1);
}
ssort(a + n-(ge-gt), ge-gt, depth);
}
/*-------------------------------------------------------------------------------*/
PList my_bsearch(PList *arr, char *matchnr, int nrin) {
int val, index, left = 0, right = nrin ;
PList retval = NULL;
while( left < right ) {
index = ( right + left) / 2; /* find halfway pt */
val = qcmp_msgid(matchnr, arr[index]->msgnr);
if(val < 0) {
right = index ;
}
else if(val > 0) {
left = index + 1;
}
else {
retval = arr[index];
break;
}
}
return retval;
}
|