From ea1358bbe6038c96dced33a1b22806e0b06d5242 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Wed, 13 Aug 2025 20:48:54 -0400 Subject: [PATCH] Predef `std::pair` to two-element `std::tuple` --- include/asm/fstack.hpp | 2 -- include/gfx/pal_packing.hpp | 4 ++-- include/link/fstack.hpp | 1 - src/asm/fstack.cpp | 40 +++++++++++++++++++----------------- src/gfx/pal_packing.cpp | 2 +- src/gfx/process.cpp | 19 +++++++---------- src/link/fstack.cpp | 41 +++++++++++++++++++------------------ src/link/sdas_obj.cpp | 4 ++-- 8 files changed, 54 insertions(+), 59 deletions(-) diff --git a/include/asm/fstack.hpp b/include/asm/fstack.hpp index 9c87b23e..8ee60720 100644 --- a/include/asm/fstack.hpp +++ b/include/asm/fstack.hpp @@ -44,8 +44,6 @@ struct FileStackNode { : type(type_), data(data_) {} void printBacktrace(uint32_t curLineNo) const; - std::vector> backtrace(uint32_t curLineNo) const; - std::string reptChain() const; }; struct MacroArgs; diff --git a/include/gfx/pal_packing.hpp b/include/gfx/pal_packing.hpp index 2af5de11..ec00c5fb 100644 --- a/include/gfx/pal_packing.hpp +++ b/include/gfx/pal_packing.hpp @@ -4,13 +4,13 @@ #define RGBDS_GFX_PAL_PACKING_HPP #include -#include +#include #include struct Palette; class ColorSet; // Returns which palette each color set maps to, and how many palettes are necessary -std::tuple, size_t> overloadAndRemove(std::vector const &colorSets); +std::pair, size_t> overloadAndRemove(std::vector const &colorSets); #endif // RGBDS_GFX_PAL_PACKING_HPP diff --git a/include/link/fstack.hpp b/include/link/fstack.hpp index f5036b58..6b2e530b 100644 --- a/include/link/fstack.hpp +++ b/include/link/fstack.hpp @@ -32,7 +32,6 @@ struct FileStackNode { std::string const &name() const { return std::get(data); } void printBacktrace(uint32_t curLineNo) const; - std::vector> backtrace(uint32_t curLineNo) const; }; #endif // RGBDS_LINK_FSTACK_HPP diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index 9c77d203..081cead8 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -50,9 +50,9 @@ static std::vector includePaths = {""}; // -I static std::deque preIncludeNames; // -P static bool failedOnMissingInclude = false; -std::string FileStackNode::reptChain() const { +static std::string reptChain(FileStackNode const &node) { std::string chain; - std::vector const &nodeIters = iters(); + std::vector const &nodeIters = node.iters(); for (uint32_t i = nodeIters.size(); i--;) { chain.append("::REPT~"); chain.append(std::to_string(nodeIters[i])); @@ -60,29 +60,31 @@ std::string FileStackNode::reptChain() const { return chain; } -std::vector> FileStackNode::backtrace(uint32_t curLineNo) const { - if (std::holds_alternative>(data)) { - assume(parent); // REPT nodes use their parent's name - std::vector> nodes = parent->backtrace(lineNo); - assume(!nodes.empty()); - nodes.emplace_back(nodes.back().first + reptChain(), curLineNo); - return nodes; - } else if (parent) { - std::vector> nodes = parent->backtrace(lineNo); - nodes.emplace_back(name(), curLineNo); - return nodes; - } else { +using TraceNode = std::pair; + +static std::vector backtrace(FileStackNode const &node, uint32_t curLineNo) { + if (!node.parent) { + assume(node.type != NODE_REPT && std::holds_alternative(node.data)); return { - {name(), curLineNo} + {node.name(), curLineNo} }; } + + std::vector traceNodes = backtrace(*node.parent, node.lineNo); + if (std::holds_alternative>(node.data)) { + assume(!traceNodes.empty()); // REPT nodes use their parent's name + traceNodes.emplace_back(traceNodes.back().first + reptChain(node), curLineNo); + } else { + traceNodes.emplace_back(node.name(), curLineNo); + } + return traceNodes; } void FileStackNode::printBacktrace(uint32_t curLineNo) const { trace_PrintBacktrace( - backtrace(curLineNo), - [](std::pair const &node) { return node.first.c_str(); }, - [](std::pair const &node) { return node.second; } + backtrace(*this, curLineNo), + [](TraceNode const &node) { return node.first.c_str(); }, + [](TraceNode const &node) { return node.second; } ); } @@ -280,7 +282,7 @@ static void newMacroContext(Symbol const ¯o, std::shared_ptr macr } } if (macro.src->type == NODE_REPT) { - fileInfoName.append(macro.src->reptChain()); + fileInfoName.append(reptChain(*macro.src)); } fileInfoName.append("::"); fileInfoName.append(macro.name); diff --git a/src/gfx/pal_packing.cpp b/src/gfx/pal_packing.cpp index b73c18d1..b9c9f715 100644 --- a/src/gfx/pal_packing.cpp +++ b/src/gfx/pal_packing.cpp @@ -363,7 +363,7 @@ static void decant(std::vector &assignments, std::vector verbosePrint(VERB_DEBUG, "%zu palettes after decanting on color sets\n", assignments.size()); } -std::tuple, size_t> overloadAndRemove(std::vector const &colorSets) { +std::pair, size_t> overloadAndRemove(std::vector const &colorSets) { verbosePrint(VERB_NOTICE, "Paginating palettes using \"overload-and-remove\" strategy...\n"); // Sort the color sets by size, which improves the packing algorithm's efficiency diff --git a/src/gfx/process.cpp b/src/gfx/process.cpp index 58cfc507..ee524e95 100644 --- a/src/gfx/process.cpp +++ b/src/gfx/process.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -330,7 +329,7 @@ static void generatePalSpec(Image const &image) { } } -static std::tuple, std::vector> +static std::pair, std::vector> generatePalettes(std::vector const &colorSets, Image const &image) { // Run a "pagination" problem solver auto [mappings, nbPalettes] = overloadAndRemove(colorSets); @@ -382,7 +381,7 @@ static std::tuple, std::vector> return {mappings, palettes}; } -static std::tuple, std::vector> +static std::pair, std::vector> makePalsAsSpecified(std::vector const &colorSets) { // Convert the palette spec to actual palettes std::vector palettes(options.palSpec.size()); @@ -726,19 +725,15 @@ struct UniqueTiles { UniqueTiles(UniqueTiles &&) = default; // Adds a tile to the collection, and returns its ID - std::tuple addTile(TileData newTile) { - auto [tileData, inserted] = tileset.insert(newTile); - - TileData::MatchType matchType = TileData::NOPE; - if (inserted) { + std::pair addTile(TileData newTile) { + if (auto [tileData, inserted] = tileset.insert(newTile); inserted) { // Give the new tile the next available unique ID tileData->tileID = static_cast(tiles.size()); - // Pointers are never invalidated! - tiles.emplace_back(&*tileData); + tiles.emplace_back(&*tileData); // Pointers are never invalidated! + return {tileData->tileID, TileData::NOPE}; } else { - matchType = tileData->tryMatching(newTile); + return {tileData->tileID, tileData->tryMatching(newTile)}; } - return {tileData->tileID, matchType}; } size_t size() const { return tiles.size(); } diff --git a/src/link/fstack.cpp b/src/link/fstack.cpp index f37f6db6..2b28bb8f 100644 --- a/src/link/fstack.cpp +++ b/src/link/fstack.cpp @@ -1,39 +1,40 @@ #include "link/fstack.hpp" #include -#include #include "backtrace.hpp" #include "link/warning.hpp" -std::vector> FileStackNode::backtrace(uint32_t curLineNo) const { - if (std::holds_alternative>(data)) { - assume(parent); // REPT nodes use their parent's name - std::vector> nodes = parent->backtrace(lineNo); - assume(!nodes.empty()); - std::string reptChain = nodes.back().first; - for (uint32_t iter : iters()) { +using TraceNode = std::pair; + +static std::vector backtrace(FileStackNode const &node, uint32_t curLineNo) { + if (!node.parent) { + assume(node.type != NODE_REPT && std::holds_alternative(node.data)); + return { + {node.name(), curLineNo} + }; + } + + std::vector traceNodes = backtrace(*node.parent, node.lineNo); + if (std::holds_alternative>(node.data)) { + assume(!traceNodes.empty()); // REPT nodes use their parent's name + std::string reptChain = traceNodes.back().first; + for (uint32_t iter : node.iters()) { reptChain.append("::REPT~"); reptChain.append(std::to_string(iter)); } - nodes.emplace_back(reptChain, curLineNo); - return nodes; - } else if (parent) { - std::vector> nodes = parent->backtrace(lineNo); - nodes.emplace_back(name(), curLineNo); - return nodes; + traceNodes.emplace_back(reptChain, curLineNo); } else { - return { - {name(), curLineNo} - }; + traceNodes.emplace_back(node.name(), curLineNo); } + return traceNodes; } void FileStackNode::printBacktrace(uint32_t curLineNo) const { trace_PrintBacktrace( - backtrace(curLineNo), - [](std::pair const &node) { return node.first.c_str(); }, - [](std::pair const &node) { return node.second; } + backtrace(*this, curLineNo), + [](TraceNode const &node) { return node.first.c_str(); }, + [](TraceNode const &node) { return node.second; } ); } diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index 8bb1d6e8..91a344ea 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include "helpers.hpp" // assume, literal_strlen #include "linkdefs.hpp" @@ -409,7 +409,7 @@ void sdobj_ReadFile(FileStackNode const &src, FILE *file, std::vector &f if (other) { // The same symbol can only be defined twice if neither // definition is in a floating section - auto checkSymbol = [](Symbol const &sym) -> std::tuple
{ + auto checkSymbol = [](Symbol const &sym) -> std::pair
{ if (std::holds_alternative