mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Use std::tie for sort comparator functions
This commit is contained in:
@@ -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<Symbol *> 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<uint32_t>(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<uint32_t>(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();
|
||||
|
||||
@@ -75,6 +75,7 @@ void sortRgb(std::vector<Palette> &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);
|
||||
});
|
||||
|
||||
@@ -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<typename F>
|
||||
@@ -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<int32_t>(sym1->data), val2 = std::get<int32_t>(sym2->data);
|
||||
return val1 != val2 ? val1 < val2 : sym1->name < sym2->name;
|
||||
return std::tie(std::get<int32_t>(sym1->data), sym1->name)
|
||||
< std::tie(std::get<int32_t>(sym2->data), sym2->name);
|
||||
});
|
||||
for (Symbol *sym : constants) {
|
||||
int32_t val = std::get<int32_t>(sym->data);
|
||||
|
||||
Reference in New Issue
Block a user