NCBI C++ ToolKit
cuSort.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #include <ncbi_pch.hpp>
3 
4 #define _SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
5 #define _vsortCBQuickM 7
6 
8 BEGIN_SCOPE(cd_utils)
9 
10 int algIntSORTFunction(void * pVal,int i, int j)
11 {
12  int * iVal=(int * ) pVal;
13  if (iVal[i]>iVal[j])return 1;
14  else if (iVal[i]<iVal[j])return -1;
15  else return 0;
16 }
17 
18 
19 void algSortQuickCallbackIndex(void * pVal,int n,int * istack,int * ind,algSORTFunction isCondFunc)
20 {
21  int i,ir=n-1,j,k,l=0;
22  int jstack=0;
23  int inda,temp;
24  //int * rrd,* ord;
25 
26 
27  /* initilize straight order */
28  for (i=l;i<=ir;i++)ind[i]=i;
29 
30  for (;;) { /*Insertion sort when subarray small enough. */
31  if (ir-l < _vsortCBQuickM) {
32  for (j=l+1;j<=ir;j++) {
33  inda=ind[j];
34  for (i=j-1;i>=l;i--) {
35  if(isCondFunc(pVal,ind[i],inda)<=0)break;
36  ind[i+1]=ind[i];
37  }
38  ind[i+1]=inda;
39  }
40  if (jstack == 0) break;
41  ir=istack[jstack--]; /* Pop stack and begin a new round of partitioning. */
42  l=istack[jstack--];
43  } else {
44 
45  k=(l+ir) >> 1; /* Choose median of left, center, and right elements as partitioning element a. Alsorearrange so that a[l]<=a[l+1]<=a[ir]. */
46  _SWAP(ind[k],ind[l+1])
47  if(isCondFunc(pVal,ind[l],ind[ir])>0){_SWAP(ind[l],ind[ir])}
48  if(isCondFunc(pVal,ind[l+1],ind[ir])>0){_SWAP(ind[l+1],ind[ir])}
49  if(isCondFunc(pVal,ind[l],ind[l+1])>0){_SWAP(ind[l],ind[l+1])}
50 
51  i=l+1; /* Initialize pointers for partitioning. */
52  j=ir;
53  inda=ind[l+1];
54  for (;;) { /* Beginning of innermost loop. */
55  do i++; while (isCondFunc(pVal,ind[i],inda)<0); /* Scan up to ␌nd element > a. */
56  do j--; while (isCondFunc(pVal,ind[j],inda)>0); /* Scan up to ␌nd element > a. */
57  if (j < i) break; /* Pointers crossed. Partitioning complete. */
58  _SWAP(ind[i],ind[j]); /* Exchange elements. */
59  } /* End of innermost loop. */
60  ind[l+1]=ind[j]; /* Insert partitioning element. */
61  ind[j]=inda;
62  jstack += 2;
63  /* Push pointers to larger subarray on stack, process smaller subarray immediately. */
64  if (ir-i+1 >= j-l) {
65  istack[jstack]=ir;
66  istack[jstack-1]=i;
67  ir=j-1;
68  } else {
69  istack[jstack]=j-1;
70  istack[jstack-1]=l;
71  l=i;
72  }
73  }
74  }
75 
76  /* remember order */
77  /*ord=ind;
78  if(toDO&vSORTNUMBER){ord+=n;for(i=0;i<n;i++)ord[ind[i]]=i;}
79  if(toDO&vSORTORDER){
80  ord+=n;rrd=ord+n;
81  rrd[ind[0]]=-1;
82  ord[ind[0]]=ind[1];
83  for(i=1;i<n-1;i++){
84  rrd[ind[i]]=ind[i-1];
85  ord[ind[i]]=ind[i+1];
86  }
87  rrd[ind[n-1]]=ind[n-2];
88  ord[ind[n-1]]=-1;
89  }*/
90 }
91 
92 END_SCOPE(cd_utils)
94 
95 
96 
int algIntSORTFunction(void *pVal, int i, int j)
Definition: cuSort.cpp:10
#define _vsortCBQuickM
Definition: cuSort.cpp:5
#define _SWAP(a, b)
Definition: cuSort.cpp:4
void algSortQuickCallbackIndex(void *pVal, int n, int *istack, int *ind, algSORTFunction isCondFunc)
Definition: cuSort.cpp:19
int(* algSORTFunction)(void *pVal, int i, int j)
Definition: cuSort.hpp:11
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define END_SCOPE(ns)
End the previously defined scope.
Definition: ncbistl.hpp:75
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define BEGIN_SCOPE(ns)
Define a new scope.
Definition: ncbistl.hpp:72
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
yy_size_t n
Modified on Sat Dec 02 09:23:38 2023 by modify_doxy.py rev. 669887