Simplify multikey_qsort function.

This function implements the three-way radix quicksort algorithm.
This patch simplifies the implementation by using MutableArrayRef.

llvm-svn: 314858
This commit is contained in:
Rui Ueyama 2017-10-03 23:12:01 +00:00
parent e0c43152b5
commit 15b8327963
1 changed files with 20 additions and 24 deletions

View File

@ -82,33 +82,34 @@ static int charTailAt(StringPair *P, size_t Pos) {
// Three-way radix quicksort. This is much faster than std::sort with strcmp // Three-way radix quicksort. This is much faster than std::sort with strcmp
// because it does not compare characters that we already know the same. // because it does not compare characters that we already know the same.
static void multikey_qsort(std::vector<StringPair *> &Vec, size_t Begin, static void multikeySort(MutableArrayRef<StringPair *> Vec, int Pos) {
size_t End, int Pos) {
tailcall: tailcall:
if (End - Begin <= 1) if (Vec.size() <= 1)
return; return;
// Partition items so that items in [Begin, P) are greater than the pivot, // Partition items so that items in [0, I) are greater than the pivot,
// [P, Q) are the same as the pivot, and [Q, End) are less than the pivot. // [I, J) are the same as the pivot, and [J, Vec.size()) are less than
int Pivot = charTailAt(Vec[Begin], Pos); // the pivot.
size_t P = Begin; int Pivot = charTailAt(Vec[0], Pos);
size_t Q = End; size_t I = 0;
for (size_t R = Begin + 1; R < Q;) { size_t J = Vec.size();
int C = charTailAt(Vec[R], Pos); for (size_t K = 1; K < J;) {
int C = charTailAt(Vec[K], Pos);
if (C > Pivot) if (C > Pivot)
std::swap(Vec[P++], Vec[R++]); std::swap(Vec[I++], Vec[K++]);
else if (C < Pivot) else if (C < Pivot)
std::swap(Vec[--Q], Vec[R]); std::swap(Vec[--J], Vec[K]);
else else
R++; K++;
} }
multikey_qsort(Vec, Begin, P, Pos); multikeySort(Vec.slice(0, I), Pos);
multikey_qsort(Vec, Q, End, Pos); multikeySort(Vec.slice(J), Pos);
// multikeySort(Vec.slice(I, J - I), Pos + 1), but with
// tail call optimization.
if (Pivot != -1) { if (Pivot != -1) {
// qsort(P, Q, Pos + 1), but with tail call optimization. Vec = Vec.slice(I, J - I);
Begin = P;
End = Q;
++Pos; ++Pos;
goto tailcall; goto tailcall;
} }
@ -131,12 +132,7 @@ void StringTableBuilder::finalizeStringTable(bool Optimize) {
for (StringPair &P : StringIndexMap) for (StringPair &P : StringIndexMap)
Strings.push_back(&P); Strings.push_back(&P);
if (!Strings.empty()) { multikeySort(Strings, 0);
// If we're optimizing, sort by name. If not, sort by previously assigned
// offset.
multikey_qsort(Strings, 0, Strings.size(), 0);
}
initSize(); initSize();
StringRef Previous; StringRef Previous;