From: Rui Ueyama Date: Tue, 3 Oct 2017 03:09:05 +0000 (+0000) Subject: Rewrite a function so that it doesn't use pointers to pointers. NFC. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ee23d30e7a770da4c2a1563563d9de791e1b00f5;p=llvm 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314756 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/MC/StringTableBuilder.cpp b/lib/MC/StringTableBuilder.cpp index 6025a20a9c1..484a4f922ef 100644 --- a/lib/MC/StringTableBuilder.cpp +++ b/lib/MC/StringTableBuilder.cpp @@ -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 &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::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();