File size: 2,625 Bytes
806d4b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

U0 QSortI64(I64 *base,I64 num, I64 (*fp_compare)(I64 e1,I64 e2))
{/*Quick Sort for width==8.
fp_compare() passes by value instead of ref.

For ascending strings: return StrCmp(e1,e2);
For ascending ints   : return e1-e2;

Maybe, look at ::/Demo/MultiCore/MPRadix.HC.
*/
  I64 i,*left,*right,pivot;
  if (num>1) {
    left =base;
    right=base+num-1;
    pivot=base[num/2];
    do {
      while ((*fp_compare)(*left,pivot)<0)
        left++;
      while ((*fp_compare)(*right,pivot)>0)
        right--;
      if (left<=right)
        SwapI64(left++,right--);
    } while (left<=right);
    i=right+1-base;
    if (1<i<num)
      QSortI64(base,i,fp_compare);
    i=base+num-left;
    if (1<i<num)
      QSortI64(left,i,fp_compare);
  }
}

U0 QSort2a(U8 **base,I64 num,I64 (*fp_compare)(U8 **_e1,U8 **_e2))
{//Not public.For case of width==size(U8 *)==8.
//fp_compare() passes by ref.
  I64 i;
  U8 **left,**right,*pivot;
  left =base;
  right=base+num-1;
  pivot=base[num/2];
  do {
    while ((*fp_compare)(left,&pivot)<0)
      left++;
    while ((*fp_compare)(right,&pivot)>0)
      right--;
    if (left<=right)
      SwapI64(left++,right--);
  } while (left<=right);
  i=right+1-base;
  if (1<i<num)
    QSort2a(base,i,fp_compare);
  i=base+num-left;
  if (1<i<num)
    QSort2a(left,i,fp_compare);
}
U0 QSort2b(U8 *base,I64 num, I64 width,
        I64 (*fp_compare)(U8 *e1,U8 *e2),U8 *tmp)
{//Not public
  I64 i;
  U8 *left,*right,*pivot=tmp+width;
  left =base;
  right=base+(num-1)*width;
  MemCpy(pivot,base+num/2*width,width);
  do {
    while ((*fp_compare)(left,pivot)<0)
      left+=width;
    while ((*fp_compare)(right,pivot)>0)
      right-=width;
    if (left<=right) {
      if (left!=right) {
        MemCpy(tmp,right,width);
        MemCpy(right,left,width);
        MemCpy(left,tmp,width);
      }
      left+=width;
      right-=width;
    }
  } while (left<=right);
  i=1+(right-base)/width;
  if (1<i<num)
    QSort2b(base,i,width,fp_compare,tmp);
  i=num+(base-left)/width;
  if (1<i<num)
    QSort2b(left,i,width,fp_compare,tmp);
}
U0 QSort(U8 *base,I64 num, I64 width, I64 (*fp_compare)(U8 *e1,U8 *e2))
{/*Quick Sort: fp_compare() passes by ref.

For ascending strings: return StrCmp(*e1,*e2);
For ascending ints   : return *e1-*e2;
Don't return e1-e2 if numbers can overflow, return -1,0 or 1.

Maybe, look at ::/Demo/MultiCore/MPRadix.HC.
*/
  U8 *tmp;
  if (width && num>1) {
    if (width==sizeof(U8 *))    //assign instead of MemCpy for width 8
      QSort2a(base,num,fp_compare);
    else {
      tmp=MAlloc(width*2);
      QSort2b(base,num,width,fp_compare,tmp);
      Free(tmp);
    }
  }
}