20#include "../utils/tools.hpp"
40bool Sorter::qSort(
int* nIndex,
int nElements,
int nColumn,
long long int nLeft,
long long int nRight,
int nSign)
43 if (!nIndex || !nElements || nLeft < 0 || nRight > nElements || nRight < nLeft)
50 while (nRight >= nLeft && !
isValue(nIndex[nRight], nColumn))
64 if (!
isValue(nIndex[nPos], nColumn))
66 int nTemp = nIndex[nPos];
67 nIndex[nPos] = nIndex[nRight];
68 nIndex[nRight] = nTemp;
96 if (!nIndex || !nElements || nLeft < 0 || nRight > nElements || nRight < nLeft)
108 if (nRight - nLeft <= 1 && (nSign *
compare(nIndex[nLeft], nIndex[nRight], nColumn) <= 0))
110 else if (nRight - nLeft <= 1 && (nSign *
compare(nIndex[nLeft], nIndex[nRight], nColumn) >= 0))
112 int nTemp = nIndex[nLeft];
113 nIndex[nLeft] = nIndex[nRight];
114 nIndex[nRight] = nTemp;
119 int nTemp = nIndex[nRight];
120 nIndex[nRight] = nIndex[(nRight+nLeft)/2];
121 nIndex[(nRight+nLeft)/2] = nTemp;
134 while (i < nRight && (nSign *
compare(nIndex[i], nIndex[nPivot], nColumn) <= 0))
138 while (j > nLeft && (nSign *
compare(nIndex[j], nIndex[nPivot], nColumn) >= 0))
145 int nTemp = nIndex[i];
146 nIndex[i] = nIndex[j];
153 if (nSign *
compare(nIndex[i], nIndex[nPivot], nColumn) > 0)
155 int nTemp = nIndex[i];
156 nIndex[i] = nIndex[nRight];
157 nIndex[nRight] = nTemp;
196bool Sorter::sortSubList(
int* nIndex,
int nElements,
ColumnKeys* KeyList,
long long int i1,
long long int i2,
long long int j1,
int nSign,
long long int nColumns)
200 int nTopColumn = KeyList->
nKey[0];
207 if (subKeyList && subKeyList->
subkeys && j1 + nTopColumn < nColumns)
209 for (
int k = i1 + 1; k <= i2 && k < nElements; k++)
213 if (
compare(nIndex[k], nIndex[nStart], j1 + nTopColumn) != 0)
220 if (!
qSort(nIndex, nElements, j1 + subKeyList->
nKey[0], nStart, k - 1, nSign))
227 if (!
sortSubList(nIndex, nElements, subKeyList, nStart, k - 1, j1, nSign, nColumns))
259 if (sKeyList.find(
':') == string::npos && sKeyList.find(
'[') == string::npos)
268 unsigned int nLastIndex = 0;
272 for (
unsigned int n = 0; n < sKeyList.length(); n++)
274 if (sKeyList[n] ==
':')
278 keys->
nKey[0] =
StrToInt(sKeyList.substr(nLastIndex, n - nLastIndex)) - 1;
281 if (n + 1 == sKeyList.length())
282 keys->
nKey[1] = nColumnCount;
285 for (
size_t i = n + 1; i < sKeyList.length(); i++)
287 if (sKeyList[i] ==
'[' || sKeyList[i] ==
':' || sKeyList[i] ==
',')
289 keys->
nKey[1] =
StrToInt(sKeyList.substr(n + 1, i - n - 1));
290 sKeyList.erase(0, i + 1);
293 else if (i + 1 == sKeyList.length())
296 keys->
nKey[1] = nColumnCount;
307 else if (sKeyList[n] ==
'[' && sKeyList.find(
']', n) != string::npos)
311 keys->
nKey[0] =
StrToInt(sKeyList.substr(nLastIndex, n - nLastIndex)) - 1;
316 if (i != string::npos)
318 sColArray = sKeyList.substr(n + 1, i - 1);
319 sKeyList.erase(0, i + n + 1);
328 else if (sKeyList[n] ==
'[')
ColumnKeys * evaluateKeyList(std::string &sKeyList, long long int nColumnCount)
This public member function creates a ColumnKeys object from a string containing the hierarchical sor...
bool qSort(int *nIndex, int nElements, int nColumn, long long int nLeft, long long int nRight, int nSign)
This public member function is the interface to the quicksort algorithm, which itself is implemented ...
bool qSortImplementation(int *nIndex, int nElements, int nColumn, long long int nLeft, long long int nRight, int nSign)
This private member function is the actual implementation of the quicksort algorithm.
virtual bool isValue(int line, int col)=0
virtual int compare(int i, int j, int col)=0
bool sortSubList(int *nIndex, int nElements, ColumnKeys *KeyList, long long int i1, long long int i2, long long int j1, int nSign, long long int nColumns)
This public member function handles the hierarchical sorting process of many columns together....
unsigned int getMatchingParenthesis(const StringView &)
Returns the position of the closing parenthesis.
int StrToInt(const std::string &)
Converts a string into an integer.
Structure for the sorting functionality: used for the recursive definition of the index columns for s...