Skip to content

Commit

Permalink
Rewrite a function so that it doesn't use pointers to pointers. NFC.
Browse files Browse the repository at this point in the history
Previous code was a bit puzzling because of its use of pointers.
In this patch, we pass a vector and its offsets, instead of pointers to
vector elements.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314756 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rui314 committed Oct 3, 2017
1 parent eb5fecf commit ee23d30
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/MC/StringTableBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,28 +82,29 @@ static int charTailAt(StringPair *P, size_t Pos) {

// 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.
static void multikey_qsort(StringPair **Begin, StringPair **End, int Pos) {
static void multikey_qsort(std::vector<StringPair *> &Vec, size_t Begin,
size_t End, int Pos) {
tailcall:
if (End - Begin <= 1)
return;

// Partition items. Items in [Begin, P) are greater than the pivot,
// Partition items so that items in [Begin, P) are greater than the pivot,
// [P, Q) are the same as the pivot, and [Q, End) are less than the pivot.
int Pivot = charTailAt(*Begin, Pos);
StringPair **P = Begin;
StringPair **Q = End;
for (StringPair **R = Begin + 1; R < Q;) {
int C = charTailAt(*R, Pos);
int Pivot = charTailAt(Vec[Begin], Pos);
size_t P = Begin;
size_t Q = End;
for (size_t R = Begin + 1; R < Q;) {
int C = charTailAt(Vec[R], Pos);
if (C > Pivot)
std::swap(*P++, *R++);
std::swap(Vec[P++], Vec[R++]);
else if (C < Pivot)
std::swap(*--Q, *R);
std::swap(Vec[--Q], Vec[R]);
else
R++;
}

multikey_qsort(Begin, P, Pos);
multikey_qsort(Q, End, Pos);
multikey_qsort(Vec, Begin, P, Pos);
multikey_qsort(Vec, Q, End, Pos);
if (Pivot != -1) {
// qsort(P, Q, Pos + 1), but with tail call optimization.
Begin = P;
Expand Down Expand Up @@ -133,7 +134,7 @@ void StringTableBuilder::finalizeStringTable(bool Optimize) {
if (!Strings.empty()) {
// If we're optimizing, sort by name. If not, sort by previously assigned
// offset.
multikey_qsort(&Strings[0], &Strings[0] + Strings.size(), 0);
multikey_qsort(Strings, 0, Strings.size(), 0);
}

initSize();
Expand Down

0 comments on commit ee23d30

Please sign in to comment.