From 2adeda031814a33a4ff7a926ea1066fa938a5793 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Sun, 13 Jul 2025 14:47:10 -0400 Subject: [PATCH] Use `std::tie` for sort comparator functions --- src/asm/output.cpp | 25 +++++++++---------------- src/gfx/pal_sorting.cpp | 1 + src/link/output.cpp | 22 ++++++---------------- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/asm/output.cpp b/src/asm/output.cpp index a8c42963..4b8e550a 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -405,6 +405,11 @@ static void dumpString(std::string const &escape, FILE *file) { } } +// Symbols are ordered by file, then by definition order +static bool compareSymbols(Symbol const *sym1, Symbol const *sym2) { + return sym1->defIndex < sym2->defIndex; +} + static bool dumpEquConstants(FILE *file) { static std::vector equConstants; // `static` so `sym_ForEach` callback can see it equConstants.clear(); @@ -414,10 +419,7 @@ static bool dumpEquConstants(FILE *file) { equConstants.push_back(&sym); } }); - // Constants are ordered by file, then by definition order - std::sort(RANGE(equConstants), [](Symbol *sym1, Symbol *sym2) -> bool { - return sym1->defIndex < sym2->defIndex; - }); + std::sort(RANGE(equConstants), compareSymbols); for (Symbol const *sym : equConstants) { uint32_t value = static_cast(sym->getOutputValue()); @@ -436,10 +438,7 @@ static bool dumpVariables(FILE *file) { variables.push_back(&sym); } }); - // Variables are ordered by file, then by definition order - std::sort(RANGE(variables), [](Symbol *sym1, Symbol *sym2) -> bool { - return sym1->defIndex < sym2->defIndex; - }); + std::sort(RANGE(variables), compareSymbols); for (Symbol const *sym : variables) { uint32_t value = static_cast(sym->getOutputValue()); @@ -458,10 +457,7 @@ static bool dumpEqusConstants(FILE *file) { equsConstants.push_back(&sym); } }); - // Constants are ordered by file, then by definition order - std::sort(RANGE(equsConstants), [](Symbol *sym1, Symbol *sym2) -> bool { - return sym1->defIndex < sym2->defIndex; - }); + std::sort(RANGE(equsConstants), compareSymbols); for (Symbol const *sym : equsConstants) { fprintf(file, "def %s equs \"", sym->name.c_str()); @@ -500,10 +496,7 @@ static bool dumpMacros(FILE *file) { macros.push_back(&sym); } }); - // Macros are ordered by file, then by definition order - std::sort(RANGE(macros), [](Symbol *sym1, Symbol *sym2) -> bool { - return sym1->defIndex < sym2->defIndex; - }); + std::sort(RANGE(macros), compareSymbols); for (Symbol const *sym : macros) { auto const &body = sym->getMacro(); diff --git a/src/gfx/pal_sorting.cpp b/src/gfx/pal_sorting.cpp index 9c8420c8..a5351e12 100644 --- a/src/gfx/pal_sorting.cpp +++ b/src/gfx/pal_sorting.cpp @@ -75,6 +75,7 @@ void sortRgb(std::vector &palettes) { options.verbosePrint(Options::VERB_LOG_ACT, "Sorting palettes by luminance...\n"); for (Palette &pal : palettes) { + // Sort from lightest to darkest std::sort(RANGE(pal), [](uint16_t lhs, uint16_t rhs) { return luminance(lhs) > luminance(rhs); }); diff --git a/src/link/output.cpp b/src/link/output.cpp index 484b2a02..6e453b67 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -293,27 +293,17 @@ static void writeSymName(std::string const &name, FILE *file) { // Comparator function for `std::stable_sort` to sort symbols static bool compareSymbols(SortedSymbol const &sym1, SortedSymbol const &sym2) { - // First, sort by address - if (sym1.addr != sym2.addr) { - return sym1.addr < sym2.addr; - } - - // Second, sort by locality (global before local) std::string const &sym1_name = sym1.sym->name; std::string const &sym2_name = sym2.sym->name; bool sym1_local = sym1_name.find('.') != std::string::npos; bool sym2_local = sym2_name.find('.') != std::string::npos; - if (sym1_local != sym2_local) { - return sym1_local < sym2_local; - } + // First, sort by address + // Second, sort by locality (global before local) // Third, sort by parent address - if (sym1.parentAddr != sym2.parentAddr) { - return sym1.parentAddr < sym2.parentAddr; - } - // Fourth, sort by name - return sym1_name < sym2_name; + return std::tie(sym1.addr, sym1_local, sym1.parentAddr, sym1_name) + < std::tie(sym2.addr, sym2_local, sym2.parentAddr, sym2_name); } template @@ -577,8 +567,8 @@ static void writeSym() { }); // Numeric constants are ordered by value, then by name std::sort(RANGE(constants), [](Symbol *sym1, Symbol *sym2) -> bool { - int32_t val1 = std::get(sym1->data), val2 = std::get(sym2->data); - return val1 != val2 ? val1 < val2 : sym1->name < sym2->name; + return std::tie(std::get(sym1->data), sym1->name) + < std::tie(std::get(sym2->data), sym2->name); }); for (Symbol *sym : constants) { int32_t val = std::get(sym->data);