[llvm] r314756 - Rewrite a function so that it doesn't use pointers to pointers. NFC.
Rui Ueyama via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 2 20:09:05 PDT 2017
Author: ruiu
Date: Mon Oct 2 20:09:05 2017
New Revision: 314756
URL: http://llvm.org/viewvc/llvm-project?rev=314756&view=rev
Log:
Rewrite a function so that it doesn't use pointers to pointers. NFC.
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.
Modified:
llvm/trunk/lib/MC/StringTableBuilder.cpp
Modified: llvm/trunk/lib/MC/StringTableBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/StringTableBuilder.cpp?rev=314756&r1=314755&r2=314756&view=diff
==============================================================================
--- llvm/trunk/lib/MC/StringTableBuilder.cpp (original)
+++ llvm/trunk/lib/MC/StringTableBuilder.cpp Mon Oct 2 20:09:05 2017
@@ -82,28 +82,29 @@ static int charTailAt(StringPair *P, siz
// 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;
@@ -133,7 +134,7 @@ void StringTableBuilder::finalizeStringT
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();
More information about the llvm-commits
mailing list