Use std::tie for sort comparator functions

This commit is contained in:
Rangi42
2025-07-13 14:47:10 -04:00
parent 21a6d35b8b
commit 2adeda0318
3 changed files with 16 additions and 32 deletions

View File

@@ -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 bool dumpEquConstants(FILE *file) {
static std::vector<Symbol *> equConstants; // `static` so `sym_ForEach` callback can see it static std::vector<Symbol *> equConstants; // `static` so `sym_ForEach` callback can see it
equConstants.clear(); equConstants.clear();
@@ -414,10 +419,7 @@ static bool dumpEquConstants(FILE *file) {
equConstants.push_back(&sym); equConstants.push_back(&sym);
} }
}); });
// Constants are ordered by file, then by definition order std::sort(RANGE(equConstants), compareSymbols);
std::sort(RANGE(equConstants), [](Symbol *sym1, Symbol *sym2) -> bool {
return sym1->defIndex < sym2->defIndex;
});
for (Symbol const *sym : equConstants) { for (Symbol const *sym : equConstants) {
uint32_t value = static_cast<uint32_t>(sym->getOutputValue()); uint32_t value = static_cast<uint32_t>(sym->getOutputValue());
@@ -436,10 +438,7 @@ static bool dumpVariables(FILE *file) {
variables.push_back(&sym); variables.push_back(&sym);
} }
}); });
// Variables are ordered by file, then by definition order std::sort(RANGE(variables), compareSymbols);
std::sort(RANGE(variables), [](Symbol *sym1, Symbol *sym2) -> bool {
return sym1->defIndex < sym2->defIndex;
});
for (Symbol const *sym : variables) { for (Symbol const *sym : variables) {
uint32_t value = static_cast<uint32_t>(sym->getOutputValue()); uint32_t value = static_cast<uint32_t>(sym->getOutputValue());
@@ -458,10 +457,7 @@ static bool dumpEqusConstants(FILE *file) {
equsConstants.push_back(&sym); equsConstants.push_back(&sym);
} }
}); });
// Constants are ordered by file, then by definition order std::sort(RANGE(equsConstants), compareSymbols);
std::sort(RANGE(equsConstants), [](Symbol *sym1, Symbol *sym2) -> bool {
return sym1->defIndex < sym2->defIndex;
});
for (Symbol const *sym : equsConstants) { for (Symbol const *sym : equsConstants) {
fprintf(file, "def %s equs \"", sym->name.c_str()); fprintf(file, "def %s equs \"", sym->name.c_str());
@@ -500,10 +496,7 @@ static bool dumpMacros(FILE *file) {
macros.push_back(&sym); macros.push_back(&sym);
} }
}); });
// Macros are ordered by file, then by definition order std::sort(RANGE(macros), compareSymbols);
std::sort(RANGE(macros), [](Symbol *sym1, Symbol *sym2) -> bool {
return sym1->defIndex < sym2->defIndex;
});
for (Symbol const *sym : macros) { for (Symbol const *sym : macros) {
auto const &body = sym->getMacro(); auto const &body = sym->getMacro();

View File

@@ -75,6 +75,7 @@ void sortRgb(std::vector<Palette> &palettes) {
options.verbosePrint(Options::VERB_LOG_ACT, "Sorting palettes by luminance...\n"); options.verbosePrint(Options::VERB_LOG_ACT, "Sorting palettes by luminance...\n");
for (Palette &pal : palettes) { for (Palette &pal : palettes) {
// Sort from lightest to darkest
std::sort(RANGE(pal), [](uint16_t lhs, uint16_t rhs) { std::sort(RANGE(pal), [](uint16_t lhs, uint16_t rhs) {
return luminance(lhs) > luminance(rhs); return luminance(lhs) > luminance(rhs);
}); });

View File

@@ -293,27 +293,17 @@ static void writeSymName(std::string const &name, FILE *file) {
// Comparator function for `std::stable_sort` to sort symbols // Comparator function for `std::stable_sort` to sort symbols
static bool compareSymbols(SortedSymbol const &sym1, SortedSymbol const &sym2) { 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 &sym1_name = sym1.sym->name;
std::string const &sym2_name = sym2.sym->name; std::string const &sym2_name = sym2.sym->name;
bool sym1_local = sym1_name.find('.') != std::string::npos; bool sym1_local = sym1_name.find('.') != std::string::npos;
bool sym2_local = sym2_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 // Third, sort by parent address
if (sym1.parentAddr != sym2.parentAddr) {
return sym1.parentAddr < sym2.parentAddr;
}
// Fourth, sort by name // 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<typename F> template<typename F>
@@ -577,8 +567,8 @@ static void writeSym() {
}); });
// Numeric constants are ordered by value, then by name // Numeric constants are ordered by value, then by name
std::sort(RANGE(constants), [](Symbol *sym1, Symbol *sym2) -> bool { std::sort(RANGE(constants), [](Symbol *sym1, Symbol *sym2) -> bool {
int32_t val1 = std::get<int32_t>(sym1->data), val2 = std::get<int32_t>(sym2->data); return std::tie(std::get<int32_t>(sym1->data), sym1->name)
return val1 != val2 ? val1 < val2 : sym1->name < sym2->name; < std::tie(std::get<int32_t>(sym2->data), sym2->name);
}); });
for (Symbol *sym : constants) { for (Symbol *sym : constants) {
int32_t val = std::get<int32_t>(sym->data); int32_t val = std::get<int32_t>(sym->data);