summaryrefslogtreecommitdiff
path: root/storage/connect/csort.cpp
blob: 670131b8fd2f6d5bd8b1618e86bce0e2725b50f2 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/*************** CSort C Program Source Code File (.CPP) ***************/
/* PROGRAM NAME: CSORT                                                 */
/* -------------                                                       */
/*  Version 2.2                                                        */
/*                                                                     */
/* COPYRIGHT:                                                          */
/* ----------                                                          */
/*  (C) Copyright to the author Olivier Bertrand 1995-2016             */
/*                                                                     */
/* WHAT THIS PROGRAM DOES:                                             */
/* -----------------------                                             */
/*  This program is the C++ sorting routines that use qsort/insert     */
/*  algorithm and produces an offset/break table while sorting.        */
/*                                                                     */
/* WHAT YOU NEED TO COMPILE THIS PROGRAM:                              */
/* --------------------------------------                              */
/*                                                                     */
/*  REQUIRED FILES:                                                    */
/*  ---------------                                                    */
/*    csort.cpp      - Source code                                     */
/*                                                                     */
/*  REQUIRED LIBRARIES:                                                */
/*  -------------------                                                */
/*    OS2DEF.LIB     - OS2 libray definition subset.                   */
/*                                                                     */
/*  REQUIRED PROGRAMS:                                                 */
/*  ------------------                                                 */
/*    Microsoft C++ Compiler                                           */
/*    or GNU Compiler/Linker                                           */
/*    or BORLAND 4.5 C++ compiler                                      */
/*                                                                     */
/*  NOTE                                                               */
/*  ----                                                               */
/*    These functions are not 64-bits ready.                           */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/*  Include relevant MariaDB header file.                              */
/***********************************************************************/
#include "my_global.h"

/***********************************************************************/
/*  Include application header files                                   */
/***********************************************************************/
#include <stdlib.h>                 /* C standard library              */
#include <string.h>                 /* String manipulation declares    */
#include <stdio.h>                  /* Required for sprintf declare    */
#if defined(_DEBUG)
#include <assert.h>                 /* Assertion routine declares      */
#endif

/***********************************************************************/
/*  Include CSort class header file                                    */
/***********************************************************************/
#include "global.h"
#include "plgdbsem.h"                /* For MBLOCK type definition      */
#include "csort.h"                  /* CSort class definition          */
#include "osutil.h"

#if !defined(BIGSORT)
#define      BIGSORT  200000
#endif   // !BIGSORT

/***********************************************************************/
/*  DB static external variables.                                      */
/***********************************************************************/
extern MBLOCK Nmblk;                /* Used to initialize MBLOCK's     */

/***********************************************************************/
/*  Initialize the CSORT static members.                               */
/***********************************************************************/
int    CSORT::Limit = 0;
double CSORT::Lg2 = log(2.0);
size_t CSORT::Cpn[1000] = {0};          /* Precalculated cmpnum values */

/***********************************************************************/
/*  CSORT constructor.                                                 */
/***********************************************************************/
CSORT::CSORT(bool cns, int th, int mth)
     : Pex((int*&)Index.Memp), Pof((int*&)Offset.Memp)
  {
  G = NULL;
  Dup =NULL;
  Cons = cns;
  Thresh = th;
  Mthresh = mth;
  Nitem = 0;
  Index = Nmblk;
  Offset = Nmblk;
  Swix = NULL;
  Savmax = 0;  
  Savcur = 0;  
  Savstep = NULL;
  } // end of CSORT constructor

/***********************************************************************/
/*  CSORT intialization.                                               */
/***********************************************************************/
int CSORT::Qsort(PGLOBAL g, int nb)
  {
  int rc;

#if defined(_DEBUG)
  assert(Index.Size >= nb * sizeof(int));
#endif

  if (nb > BIGSORT) {
    G = g;
    Dup = (PDBUSER)g->Activityp->Aptr;

    if (Dup->Proginfo) {
      Savstep = Dup->Step;
      Savmax  = Dup->ProgMax;
      Savcur  = Dup->ProgCur;

      // Evaluate the number of comparisons that we will do
      Dup->ProgMax = Cmpnum(nb);
      Dup->ProgCur = 0;
      Dup->Step = (char*)PlugSubAlloc(g, NULL, 32);
      sprintf((char*)Dup->Step, MSG(SORTING_VAL), nb);
    } else
      Dup = NULL;

  } else
    Dup = NULL;

  Nitem = nb;

  for (int n = 0; n < Nitem; n++)
    Pex[n] = n;

  rc = (Cons) ? Qsortc() : Qsortx();

  if (Dup) {
    // Restore any change in progress info settings
//  printf("Progcur=%u\n", Dup->ProgCur);

    Dup->Step    = Savstep;
    Dup->ProgMax = Savmax;
    Dup->ProgCur = Savcur;
    } // endif Subcor

  return rc;
  } // end of QSort

#if defined(DEBTRACE)
/***********************************************************************/
/*  Debug routine to be used by sort for specific data (dummy as now)  */
/***********************************************************************/
void CSORT::DebugSort(int ph, int n, int *base, int *mid, int *tmp)
  {
  htrc("phase=%d n=%d base=%p mid=%p tmp=%p\n",
          ph, n, base, mid, tmp);
  } // end of DebugSort
#endif

/***********************************************************************/
/* Qsortx:   Version adapted from qsortx.c by O.Bertrand               */
/* This version is specialy adapted for Index sorting, meaning that    */
/* the data is not moved, but the Index only is sorted.                */
/* Index array elements are any 4-byte word (a pointer or a int int   */
/* array index), they are not interpreted except by the user provided  */
/* comparison routine which must works accordingly.                    */
/* In addition, this program takes care of data in which there is a    */
/* high rate of repetitions.                                           */
/* CAUTION: the sort algorithm used here is not conservative. Equal    */
/* values will be internally stored in unpredictable order.            */
/* The THRESHold below is the insertion sort threshold, and also the   */
/* threshold for continuing que quicksort partitioning.                */
/* The MTHREShold is where we stop finding a better median.            */
/* These two quantities should be adjusted dynamically depending upon  */
/* the repetition rate of the data.                                    */
/* Algorithm used:                                                     */
/* First, set up some global parameters for Qstx to share.  Then,      */
/* quicksort with Qstx(), and then a cleanup insertion sort ourselves. */
/* Sound simple? It's not...                                           */
/***********************************************************************/
int CSORT::Qsortx(void)
  {
  register int  c;
  register int  lo, hi, min;
  register int  i, j, rc = 0;
  // To do: rc should be checked for being used uninitialized
  int          *top;
#ifdef DEBTRACE
  int           ncp;

  num_comp = 0;
#endif

  /*********************************************************************/
  /* Prepare the Offset array that will be updated during sorts.       */
  /*********************************************************************/
  if (Pof)
    for (Pof[Nitem] = Nitem, j = 0; j < Nitem; j++)
      Pof[j] = 0;
  else
    j = Nitem + 1;

  /*********************************************************************/
  /* Sort on one or zero element is obvious.                           */
  /*********************************************************************/
  if (Nitem <= 1)
    return Nitem;

  /*********************************************************************/
  /* Thresh seems to be good as (10 * n / rep).  But for testing we    */
  /* set it directly as one parameter of the Xset function call.       */
  /* Note: this should be final as the rep parameter is no more used.  */
  /*********************************************************************/
  top = Pex + Nitem;

#ifdef DEBTRACE
 htrc("Qsortx: nitem=%d thresh=%d mthresh=%d\n",
  Nitem, Thresh, Mthresh);
#endif

  /*********************************************************************/
  /*  If applicable, do a rough preliminary quick sort.                */
  /*********************************************************************/
  if (Nitem >= Thresh)
    Qstx(Pex, top);

#ifdef DEBTRACE
 htrc(" after quick sort num_comp=%d\n", num_comp);
 ncp = num_comp;
 num_comp = 0;
#ifdef DEBUG2
 DebugSort((Pof) ? 1 : 4, Nitem, Pex, NULL, NULL);
#endif
#endif

  if (Thresh > 2) {
    if (Pof)
      /*****************************************************************/
      /*  The preliminary search for the smallest element has been     */
      /*  removed so with no sentinel in place, we must check for x    */
      /*  going below the Pof pointer.  For each remaining element    */
      /*  group from [1] to [n-1], set hi to the index of the element  */
      /*  AFTER which this one goes. Then, do the standard insertion   */
      /*  sort shift on an integer at a time basis for each equal      */
      /*  element group in the frob.                                   */
      /*****************************************************************/
      for (min = hi = 0; min < Nitem; min = hi) {
        if (Pof[hi]) {
          hi += Pof[hi];
          continue;
          } // endif Pof

        Pof[min] = 1;

#ifdef DEBUG2
 htrc("insert from min=%d\n", min);
#endif

        for (lo = hi; !Pof[++hi]; lo = hi) {
          while (lo >= min && (rc = Qcompare(Pex + lo, Pex + hi)) > 0)
            if (Pof[lo] > 0)
              lo -= Pof[lo];
            else
              return -2;

          if (++lo != hi) {
            c = Pex[hi];

            for (i = j = hi; i > 0; i = j)
              if (Pof[i - 1] <= 0)
                return -3;
              else if ((j -= Pof[i - 1]) >= lo) {
                Pex[i] = Pex[j];
                Pof[j + 1] = Pof[i] = Pof[j];
              } else
                break;

            Pex[i] = c;
            } // endif lo

          if (rc)
            Pof[lo] = 1;
          else {
            i = lo - Pof[lo - 1];
            Pof[lo] = ++Pof[i];
            } // endelse

#ifdef DEBUG2
 htrc("rc=%d lo=%d hi=%d trx=%d\n", rc, lo, hi, Pof[lo]);
#endif

          } // endfor hi

        } // endfor min

    else
      /*****************************************************************/
      /*  Call conservative insertion sort not using/setting offset.   */
      /*****************************************************************/
      Istc(Pex, Pex + MY_MIN(Nitem, Thresh), top);

    } // endif Thresh

#ifdef DEBTRACE
 htrc(" after insert sort num_comp=%d\n", num_comp);
 num_comp += ncp;
#endif

  if (Pof)
    /*******************************************************************/
    /* Reduce the Offset array.                                        */
    /*******************************************************************/
    for (i = j = 0; i <= Nitem; j++, i += c) {
#ifdef DEBUG2
 htrc(" trxp(%d)=%d trxp(%d)=%d c=%d\n",
  i, Pof[i], j, Pof[j], c);
#endif
      if ((c = Pof[i]))
        Pof[j] = i;
      else
        return -4;

      } // endfor i

  return (j - 1);
  } // end of Qsortx

/***********************************************************************/
/* Qstx:  Do a quicksort on index elements (just one int int).        */
/* First, find the median element, and put that one in the first place */
/* as the discriminator.  (This "median" is just the median of the     */
/* first, last and middle elements).  (Using this median instead of    */
/* the first element is a big win).  Then, the usual partitioning/     */
/* swapping, followed by moving the discriminator into the right place.*/
/* Element equal to the discriminator are placed against it, so the    */
/* mid (discriminator) block grows when equal elements exist. This is  */
/* a huge win in case of repartitions with few different elements.     */
/* The mid block being at its final position, its first and last       */
/* elements are marked in the offset list (used to make break list).   */
/* Then, figure out the sizes of the two partitions, do the smaller    */
/* one recursively and the larger one via a repeat of this code.       */
/* Stopping when there are less than THRESH elements in a partition    */
/* and cleaning up with an insertion sort (in our caller) is a huge    */
/* win(?). All data swaps are done in-line, which is space-losing but  */
/* time-saving. (And there are only three places where this is done).  */
/***********************************************************************/
void CSORT::Qstx(int *base, int *max)
  {
  register int *i, *j, *jj, *mid, *him, c;
  int          *tmp;
  int           lo, hi, rc;
  size_t        zlo, zhi, cnm;

  zlo = zhi = cnm = 0;                  // Avoid warning message

  lo = (int)(max - base);                      // Number of elements as longs

  if (Dup)
    cnm = Cmpnum(lo);

  do {
    /*******************************************************************/
    /* At the top here, lo is the number of integers of elements in    */
    /* the current partition.  (Which should be max - base).           */
    /* Find the median of the first, last, and middle element and make */
    /* that the middle element.  Set j to largest of first and middle. */
    /* If max is larger than that guy, then it's that guy, else        */
    /* compare max with loser of first and take larger.  Things are    */
    /* set up to prefer the middle, then the first in case of ties.    */
    /* In addition, hi and rc are set to comparison results. So if hi  */
    /* is null, the two high values are equal and if rc is null, the   */
    /* two low values are equal. This was used to set which test will  */
    /* be made by LE and which one by LT (does not apply anymore).     */
    /*******************************************************************/
    him = mid = i = base + (lo >> 1);
    hi = rc = 0;

#ifdef DEBTRACE
 tmp = max - 1;
 htrc("--> block base=%d size=%d\n", base - Pex, lo);
 DebugSort(2, 0, base, mid, tmp);
#endif

    if (lo >= Mthresh) {
      rc = Qcompare((jj = base), i);
      j = (rc > 0) ? jj : i;
      hi = Qcompare(j, (tmp = max - 1));

      if (hi > 0 && rc) {
        j = (j == jj) ? i : jj;               // switch to first loser

        if ((rc = Qcompare(j, tmp)) < 0)
          j = tmp;

        } // endif

      if (j != i) {
        c = *i;
        *i = *j;
        *j = c;
        } // endif j

    } else if (lo == 2) {
      /*****************************************************************/
      /*  Small group. Do special quicker processing.                  */
      /*****************************************************************/
      if ((rc = Qcompare(base, (him = base + 1))) > 0)
        c = *base, *base = *him, *him = c;

      if (Pof)
        Pof[base - Pex] = Pof[him - Pex] = (rc) ? 1 : 2;

      break;
    } // endif lo

#ifdef DEBTRACE
 DebugSort(3, hi, NULL, mid, &rc);
#endif

    /*******************************************************************/
    /*  Semi-standard quicksort partitioning/swapping.  Added here is  */
    /*  a test on equality.  All values equal to the mid element are   */
    /*  placed under or over it.  Mid block can be also moved when it  */
    /*  is necessary because the other partition is full.  At the end  */
    /*  of the for loop the mid block is definitely positionned.       */
    /*******************************************************************/
    for (i = base, j = max - 1; ;) {
     CONT:
      while (i < mid)
        if ((rc = Qcompare(i, mid)) < 0)
          i++;
        else if (!rc) {
          c = *i;
          *i = *(--mid);
          *mid = c;
        } else
          break;

      while (j > him)
        if ((rc = Qcompare(him, j)) < 0)
          j--;
        else if (!rc) {
          c = *j;
          *j = *(++him);
          *him = c;
        } else if (i == mid) {              // Triple move:
          c = *j;                           // j goes under mid block
          *j = *(++him);                    // val over mid block -> j
          *him = *mid++;                    // and mid block goes one
          *i++ = c;                         // position higher.
        } else {                            // i <-> j
          c = *i;
          *i++ = *j;
          *j-- = c;
          goto CONT;
        } // endif's

      if (i == mid)
        break;
      else {                                // Triple move:
        c = *i;                             // i goes over mid block
        *i = *(--mid);                      // val under mid block -> i
        *mid = *him--;                      // and mid block goes one
        *j-- = c;                           // position lower.
        } // endelse

      } // endfor i

    /*******************************************************************/
    /* The mid block being placed at its final position we can now set */
    /* the offset array values indicating break point and block size.  */
    /*******************************************************************/
    j = mid;
    i = him + 1;

    if (Pof)
      Pof[him - Pex] = Pof[mid - Pex] = (int)(i - j);

    /*******************************************************************/
    /* Look at sizes of the two partitions, do the smaller one first   */
    /* by recursion, then do the larger one by making sure lo is its   */
    /* size, base and max are update correctly, and branching back.    */
    /* But only repeat (recursively or by branching) if the partition  */
    /* is of at least size THRESH.                                     */
    /*******************************************************************/
    lo = (int)(j - base);
    hi = (int)(max - i);

    if (Dup) {                         // Update progress information
      zlo = Cmpnum(lo);
      zhi = Cmpnum(hi);
      Dup->ProgCur += cnm - (zlo + zhi);
      } // endif Dup

#ifdef DEBTRACE
 htrc(" done lo=%d sep=%d hi=%d\n", lo, i - j, hi);
#endif

    if (lo <= hi) {
      if (lo >= Thresh)
        Qstx(base, j);
      else if (lo == 1 && Pof)
        Pof[base - Pex] = 1;

      base = i;
      lo = hi;
      cnm = zhi;
    } else {
      if (hi >= Thresh)
        Qstx(i, max);
      else if (hi == 1 && Pof)
        Pof[i - Pex] = 1;

      max = j;
      cnm = zlo;
    } // endif

    if (lo == 1 && Pof)
      Pof[base - Pex] = 1;

    } while (lo >= Thresh); // enddo

  } // end of Qstx

/***********************************************************************/
/*  Qsortc.c:   Version adapted from qsort.c by O.Bertrand             */
/*  This version is specialy adapted for Index sorting, meaning that   */
/*  the data is not moved, but the Index only is sorted.               */
/*  Index array elements are any 4-byte word (a pointer or a int int  */
/*  array index), they are not interpreted except by the user provided */
/*  comparison routine which must works accordingly.                   */
/*  In addition, this program takes care of data in which there is a   */
/*  high rate of repetitions.                                          */
/*  NOTE: the sort algorithm used here is conservative. Equal and      */
/*  greater than values are internally stored in additional work area. */
/*  The THRESHold below is the insertion sort threshold, and also the  */
/*  threshold for continuing que quicksort partitioning.               */
/*  The MTHREShold is where we stop finding a better median.           */
/*  These two quantities should be adjusted dynamically depending upon */
/*  the repetition rate of the data.                                   */
/*  Algorithm used:                                                    */
/*  First, set up some global parameters for Qstc to share.  Then,     */
/*  quicksort with Qstc(), and then a cleanup insertion sort ourselves.*/
/*  Sound simple? It's not...                                          */
/***********************************************************************/
int CSORT::Qsortc(void)
  {
  register int  c;
  register int  lo, hi, min;
  register int  i, j, k, m, rc = 0;
  // To do: rc should be checked for being used uninitialized
  int          *max;
#ifdef DEBTRACE
  int           ncp;

  num_comp = 0;
#endif

  /*********************************************************************/
  /* Prepare the Offset array that will be updated during sorts.       */
  /*********************************************************************/
  if (Pof)
    for (Pof[Nitem] = Nitem, j = 0; j < Nitem; j++)
      Pof[j] = 0;
  else
    j = Nitem + 1;

  /*********************************************************************/
  /* Sort on one or zero element is obvious.                           */
  /*********************************************************************/
  if (Nitem <= 1)
    return Nitem;

  /*********************************************************************/
  /* Thresh seems to be good as (10 * n / rep).  But for testing we    */
  /* set it directly as one parameter of the Xset function call.       */
  /* Note: this should be final as the rep parameter is no more used.  */
  /*********************************************************************/
  max = Pex + Nitem;

#ifdef DEBTRACE
 htrc("Qsortc: nitem=%d thresh=%d mthresh=%d\n",
  Nitem, Thresh, Mthresh);
#endif

  /*********************************************************************/
  /*  If applicable, do a rough preliminary conservative quick sort.   */
  /*********************************************************************/
  if (Nitem >= Thresh) {
    if (!(Swix = (int *)malloc(Nitem * sizeof(int))))
      return -1;

    Qstc(Pex, max);

    free(Swix);
    Swix = NULL;
    } // endif n

#ifdef DEBTRACE
 htrc(" after quick sort num_comp=%d\n", num_comp);
 ncp = num_comp;
 num_comp = 0;
#ifdef DEBUG2
 DebugSort((Pof) ? 1 : 4, Nitem, Pex, NULL, NULL);
#endif
#endif

  if (Thresh > 2) {
    if (Pof)
      /*****************************************************************/
      /*  The preliminary search for the smallest element has been     */
      /*  removed so with no sentinel in place, we must check for x    */
      /*  going below the Pof pointer.  For each remaining element    */
      /*  group from [1] to [n-1], set hi to the index of the element  */
      /*  AFTER which this one goes. Then, do the standard insertion   */
      /*  sort shift on an integer at a time basis for each equal      */
      /*  element group in the frob.                                   */
      /*****************************************************************/
      for (min = hi = 0; min < Nitem; min = hi) {
        if (Pof[hi]) {
          hi += Pof[hi];
          continue;
          } // endif

        Pof[min] = 1;

#ifdef DEBUG2
 htrc("insert from min=%d\n", min);
#endif

        for (lo = hi; !Pof[++hi]; lo = hi) {
          while (lo >= min && (rc = Qcompare(Pex + lo, Pex + hi)) > 0)
            if (Pof[lo] > 0)
              lo -= Pof[lo];
            else
              return -2;

          if (++lo != hi) {
            c = Pex[hi];

            for (i = j = hi; i > 0; i = j)
              if (Pof[i - 1] <= 0)
                return -3;
              else if ((j -= Pof[i - 1]) >= lo) {
                for (k = m = i; --m >= j; k--)    // Move intermediate
                  Pex[k] = Pex[m];              // for conservation.

                Pof[j + 1] = Pof[i] = Pof[j];
              } else
                break;

            Pex[i] = c;
            } // endif

          if (rc)
            Pof[lo] = 1;
          else {
            i = lo - Pof[lo - 1];
            Pof[lo] = ++Pof[i];
            } // endelse

#ifdef DEBUG2
 htrc("rc=%d lo=%d hi=%d ofx=%d\n", rc, lo, hi, Pof[lo]);
#endif

          } // endfor hi

        } // endfor min

    else
      /*****************************************************************/
      /*  Call conservative insertion sort not using/setting offset.   */
      /*****************************************************************/
      Istc(Pex, Pex + MY_MIN(Nitem, Thresh), max);

    } // endif Thresh

#ifdef DEBTRACE
 htrc(" after insert sort num_comp=%d\n", num_comp);
 num_comp += ncp;
#endif

  if (Pof)
    /*******************************************************************/
    /* Reduce the Offset array.                                        */
    /*******************************************************************/
    for (i = j = 0; i <= Nitem; j++, i += c) {
#ifdef DEBUG2
 htrc(" Pof(%d)=%d Pof(%d)=%d c=%d\n",
  i, Pof[i], j, Pof[j], c);
#endif
      if ((c = Pof[i]))
        Pof[j] = i;
      else
        return -4;

      } // endfor i

  return (j - 1);
  } // end of Qsortc

/***********************************************************************/
/*  Qstc:  Do a quicksort on index elements (just one int int).       */
/*  First, find the median element, and set it as the discriminator.   */
/*  (This "median" is just the median of the first, last and middle    */
/*  elements).  (Using this median instead of the first element is a   */
/*  big win).  Then, the special partitioning/swapping, where elements */
/*  smaller than the discriminator are placed in the sorted block,     */
/*  elements equal to the discriminator are placed backward from the   */
/*  top of the work area and elements greater than *j (discriminator)  */
/*  are placed in the work area from its bottom. Then the elements in  */
/*  the work area are placed back in the sort area in natural order,   */
/*  making the sort conservative. Non equal blocks shrink faster when  */
/*  equal elements exist. This is a huge win in case of repartitions   */
/*  with few different elements. The mid block being at its final      */
/*  position, its first and last elements are marked in the offset     */
/*  list (used to make break list). Then, figure out the sizes of the  */
/*  two partitions, do the smaller one recursively and the larger one  */
/*  via a repeat of this code. Stopping when there are less than       */
/*  THRESH elements in a partition and cleaning up with an insertion   */
/*  sort (in our caller) is a huge win (yet to be proved?).            */
/***********************************************************************/
void CSORT::Qstc(int *base, int *max)
  {
  register int *i, *j, *jj, *lt, *eq, *gt, *mid;
  int           c = 0, lo, hi, rc;
  size_t        zlo, zhi, cnm;

  zlo = zhi = cnm = 0;                  // Avoid warning message

  lo = (int)(max - base);                      // Number of elements as longs

  if (Dup)
    cnm = Cmpnum(lo);

  do {
    /*******************************************************************/
    /*  At the top here, lo is the number of integers of elements in   */
    /*  the current partition. (Which should be max - base). Find the  */
    /*  median of the first, last, and middle element and make that    */
    /*  the compare element. Set jj to smallest of middle and last.    */
    /*  If base is smaller or equal than that guy, then it's that guy, */
    /*  else compare base with loser of first and take smaller. Things */
    /*  are set up to prefer the top, then the middle in case of ties. */
    /*******************************************************************/
    i = base + (lo >> 1);
    jj = mid = max - 1;

#ifdef DEBTRACE
 htrc("--> block base=%d size=%d\n", base - Pex, lo);
 DebugSort(2, 0, base, i, mid);
#endif

    if (lo >= Mthresh) {
      jj = ((rc = Qcompare(i, mid)) < 0) ? i : mid;

      if (rc && Qcompare(base, jj) > 0) {
        jj = (jj == mid) ? i : mid;           // switch to first loser

        if (Qcompare(base, jj) < 0)
          jj = base;

        } // endif

      if (jj != mid) {
        /***************************************************************/
        /*  The compare element must be at the top of the block so it  */
        /*  cannot be overwritten while making the partitioning.  So   */
        /*  save the last block value which will be compared later.    */
        /***************************************************************/
        c = *mid;
        *mid = *jj;
        } // endif

    } else if (lo == 2) {
      /*****************************************************************/
      /*  Small group. Do special quicker processing.                  */
      /*****************************************************************/
			if ((rc = Qcompare(base, (i = base + 1))) > 0) {
				c = *base;
				*base = *i;
				*i = c;
			}	// endif rc

      if (Pof)
        Pof[base - Pex] = Pof[i - Pex] = (rc) ? 1 : 2;

      break;
    } // endif lo

#ifdef DEBTRACE
 DebugSort(3, lo, NULL, jj, &rc);
#endif

    /*******************************************************************/
    /*  Non-standard quicksort partitioning using additional storage   */
    /*  to store values less than, equal or greater than the middle    */
    /*  element. This uses more memory but provides conservation of    */
    /*  the equal elements order.                                      */
    /*******************************************************************/
    lt = base;
    eq = Swix + lo;
    gt = Swix;

    if (jj == mid) {
      /*****************************************************************/
      /*  Compare element was last.  No problem.                       */
      /*****************************************************************/
      for (i = base; i < max; i++)
        if ((rc = Qcompare(i, mid)) < 0)
          *lt++ = *i;
        else if (rc > 0)
          *gt++ = *i;
        else
          *--eq = *i;

    } else {
      /*****************************************************************/
      /*  Compare element was not last and was copied to top of block. */
      /*****************************************************************/
      for (i = base; i < mid; i++)
        if ((rc = Qcompare(i, mid)) < 0)
          *lt++ = *i;
        else if (rc > 0)
          *gt++ = *i;
        else
          *--eq = *i;

      /*****************************************************************/
      /*  Restore saved last value and do the comparison from there.   */
      /*****************************************************************/
      *--i = c;

      if ((rc = Qcompare(i, mid)) < 0)
        *lt++ = *i;
      else if (rc > 0)
        *gt++ = *i;
      else
        *--eq = *i;

    } // endif

    /*******************************************************************/
    /* Now copy the equal and greater values back in the main array in */
    /* the same order they have been placed in the work area.          */
    /*******************************************************************/
    for (j = Swix + lo, i = lt; j > eq; )
      *i++ = *--j;

    for (j = Swix, jj = i; j < gt; )
      *i++ = *j++;

    /*******************************************************************/
    /* The mid block being placed at its final position we can now set */
    /* the offset array values indicating break point and block size.  */
    /*******************************************************************/
    if (Pof)
      Pof[lt - Pex] = Pof[(jj - 1) - Pex] = (int)(jj - lt);

    /*******************************************************************/
    /* Look at sizes of the two partitions, do the smaller one first   */
    /* by recursion, then do the larger one by making sure lo is its   */
    /* size, base and max are update correctly, and branching back.    */
    /* But only repeat (recursively or by branching) if the partition  */
    /* is of at least size THRESH.                                     */
    /*******************************************************************/
    lo = (int)(lt - base);
    hi = (int)(gt - Swix);

    if (Dup) {                         // Update progress information
      zlo = Cmpnum(lo);
      zhi = Cmpnum(hi);
      Dup->ProgCur += cnm - (zlo + zhi);
      } // endif Dup

#ifdef DEBTRACE
 htrc(" done lo=%d hi=%d\n",
   lo, /*Swix + lt - base - eq,*/ hi);
#endif

    if (lo <= hi) {
      if (lo >= Thresh)
        Qstc(base, lt);
      else if (lo == 1 && Pof)
        Pof[base - Pex] = 1;

      base = jj;
      lo = hi;
      cnm = zhi;
    } else {
      if (hi >= Thresh)
        Qstc(jj, max);
      else if (hi == 1 && Pof)
        Pof[jj - Pex] = 1;

      max = lt;
      cnm = zlo;
    } // endif

    if (lo == 1 && Pof)
      Pof[base - Pex] = 1;

    } while (lo >= Thresh); // enddo

  } // end of Qstc

/***********************************************************************/
/*  Conservative insertion sort not using/setting offset array.        */
/***********************************************************************/
void CSORT::Istc(int *base, int *hi, int *max)
  {
  register int  c = 0;
  register int *lo;
  register int *i, *j;

  /*********************************************************************/
  /*  First put smallest element, which must be in the first THRESH,   */
  /*  in the first position as a sentinel.  This is done just by       */
  /*  searching the 1st THRESH elements (or the 1st n if n < THRESH)   */
  /*  finding the min, and shifting it into the first position.        */
  /*********************************************************************/
  for (j = lo = base; ++lo < hi; )
    if (Qcompare(j, lo) > 0)
      j = lo;

  if (j != base) {                               // shift j into place
    c = *j;

    for (i = j; --j >= base; i = j)
      *i = *j;

    *base = c;
    } // endif j

#ifdef DEBTRACE
 htrc("sentinel %d in place, base=%p hi=%p max=%p\n",
  c, base, hi, max);
#endif

  /*********************************************************************/
  /*  With our sentinel in place, we now run the following hyper-      */
  /*  fast insertion sort.  For each remaining element, lo, from [1]   */
  /*  to [n-1], set hi to the index of the element AFTER which this    */
  /*  one goes. Then, do the standard insertion sort shift for each    */
  /*  element in the frob.                                             */
  /*********************************************************************/
  for (lo = base; (hi = ++lo) < max;) {
    while (Qcompare(--hi, lo) > 0) ;

#ifdef DEBUG2
 htrc("after while: hi(%p)=%d lo(%p)=%d\n",
  hi, *hi, lo, *lo);
#endif

    if (++hi != lo) {
      c = *lo;

      for (i = j = lo; --j >= hi; i = j)
        *i = *j;

      *i = c;
      } // endif hi

    } // endfor lo

  } // end of Istc

/* -------------------------- End of CSort --------------------------- */