Predef std::pair to two-element std::tuple

This commit is contained in:
Rangi42
2025-08-13 20:48:54 -04:00
parent 2bdf61da70
commit ea1358bbe6
8 changed files with 54 additions and 59 deletions

View File

@@ -44,8 +44,6 @@ struct FileStackNode {
: type(type_), data(data_) {} : type(type_), data(data_) {}
void printBacktrace(uint32_t curLineNo) const; void printBacktrace(uint32_t curLineNo) const;
std::vector<std::pair<std::string, uint32_t>> backtrace(uint32_t curLineNo) const;
std::string reptChain() const;
}; };
struct MacroArgs; struct MacroArgs;

View File

@@ -4,13 +4,13 @@
#define RGBDS_GFX_PAL_PACKING_HPP #define RGBDS_GFX_PAL_PACKING_HPP
#include <stddef.h> #include <stddef.h>
#include <tuple> #include <utility>
#include <vector> #include <vector>
struct Palette; struct Palette;
class ColorSet; class ColorSet;
// Returns which palette each color set maps to, and how many palettes are necessary // Returns which palette each color set maps to, and how many palettes are necessary
std::tuple<std::vector<size_t>, size_t> overloadAndRemove(std::vector<ColorSet> const &colorSets); std::pair<std::vector<size_t>, size_t> overloadAndRemove(std::vector<ColorSet> const &colorSets);
#endif // RGBDS_GFX_PAL_PACKING_HPP #endif // RGBDS_GFX_PAL_PACKING_HPP

View File

@@ -32,7 +32,6 @@ struct FileStackNode {
std::string const &name() const { return std::get<std::string>(data); } std::string const &name() const { return std::get<std::string>(data); }
void printBacktrace(uint32_t curLineNo) const; void printBacktrace(uint32_t curLineNo) const;
std::vector<std::pair<std::string, uint32_t>> backtrace(uint32_t curLineNo) const;
}; };
#endif // RGBDS_LINK_FSTACK_HPP #endif // RGBDS_LINK_FSTACK_HPP

View File

@@ -50,9 +50,9 @@ static std::vector<std::string> includePaths = {""}; // -I
static std::deque<std::string> preIncludeNames; // -P static std::deque<std::string> preIncludeNames; // -P
static bool failedOnMissingInclude = false; static bool failedOnMissingInclude = false;
std::string FileStackNode::reptChain() const { static std::string reptChain(FileStackNode const &node) {
std::string chain; std::string chain;
std::vector<uint32_t> const &nodeIters = iters(); std::vector<uint32_t> const &nodeIters = node.iters();
for (uint32_t i = nodeIters.size(); i--;) { for (uint32_t i = nodeIters.size(); i--;) {
chain.append("::REPT~"); chain.append("::REPT~");
chain.append(std::to_string(nodeIters[i])); chain.append(std::to_string(nodeIters[i]));
@@ -60,29 +60,31 @@ std::string FileStackNode::reptChain() const {
return chain; return chain;
} }
std::vector<std::pair<std::string, uint32_t>> FileStackNode::backtrace(uint32_t curLineNo) const { using TraceNode = std::pair<std::string, uint32_t>;
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
assume(parent); // REPT nodes use their parent's name static std::vector<TraceNode> backtrace(FileStackNode const &node, uint32_t curLineNo) {
std::vector<std::pair<std::string, uint32_t>> nodes = parent->backtrace(lineNo); if (!node.parent) {
assume(!nodes.empty()); assume(node.type != NODE_REPT && std::holds_alternative<std::string>(node.data));
nodes.emplace_back(nodes.back().first + reptChain(), curLineNo);
return nodes;
} else if (parent) {
std::vector<std::pair<std::string, uint32_t>> nodes = parent->backtrace(lineNo);
nodes.emplace_back(name(), curLineNo);
return nodes;
} else {
return { return {
{name(), curLineNo} {node.name(), curLineNo}
}; };
} }
std::vector<TraceNode> traceNodes = backtrace(*node.parent, node.lineNo);
if (std::holds_alternative<std::vector<uint32_t>>(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 { void FileStackNode::printBacktrace(uint32_t curLineNo) const {
trace_PrintBacktrace( trace_PrintBacktrace(
backtrace(curLineNo), backtrace(*this, curLineNo),
[](std::pair<std::string, uint32_t> const &node) { return node.first.c_str(); }, [](TraceNode const &node) { return node.first.c_str(); },
[](std::pair<std::string, uint32_t> const &node) { return node.second; } [](TraceNode const &node) { return node.second; }
); );
} }
@@ -280,7 +282,7 @@ static void newMacroContext(Symbol const &macro, std::shared_ptr<MacroArgs> macr
} }
} }
if (macro.src->type == NODE_REPT) { if (macro.src->type == NODE_REPT) {
fileInfoName.append(macro.src->reptChain()); fileInfoName.append(reptChain(*macro.src));
} }
fileInfoName.append("::"); fileInfoName.append("::");
fileInfoName.append(macro.name); fileInfoName.append(macro.name);

View File

@@ -363,7 +363,7 @@ static void decant(std::vector<AssignedSets> &assignments, std::vector<ColorSet>
verbosePrint(VERB_DEBUG, "%zu palettes after decanting on color sets\n", assignments.size()); verbosePrint(VERB_DEBUG, "%zu palettes after decanting on color sets\n", assignments.size());
} }
std::tuple<std::vector<size_t>, size_t> overloadAndRemove(std::vector<ColorSet> const &colorSets) { std::pair<std::vector<size_t>, size_t> overloadAndRemove(std::vector<ColorSet> const &colorSets) {
verbosePrint(VERB_NOTICE, "Paginating palettes using \"overload-and-remove\" strategy...\n"); verbosePrint(VERB_NOTICE, "Paginating palettes using \"overload-and-remove\" strategy...\n");
// Sort the color sets by size, which improves the packing algorithm's efficiency // Sort the color sets by size, which improves the packing algorithm's efficiency

View File

@@ -10,7 +10,6 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <tuple>
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
#include <vector> #include <vector>
@@ -330,7 +329,7 @@ static void generatePalSpec(Image const &image) {
} }
} }
static std::tuple<std::vector<size_t>, std::vector<Palette>> static std::pair<std::vector<size_t>, std::vector<Palette>>
generatePalettes(std::vector<ColorSet> const &colorSets, Image const &image) { generatePalettes(std::vector<ColorSet> const &colorSets, Image const &image) {
// Run a "pagination" problem solver // Run a "pagination" problem solver
auto [mappings, nbPalettes] = overloadAndRemove(colorSets); auto [mappings, nbPalettes] = overloadAndRemove(colorSets);
@@ -382,7 +381,7 @@ static std::tuple<std::vector<size_t>, std::vector<Palette>>
return {mappings, palettes}; return {mappings, palettes};
} }
static std::tuple<std::vector<size_t>, std::vector<Palette>> static std::pair<std::vector<size_t>, std::vector<Palette>>
makePalsAsSpecified(std::vector<ColorSet> const &colorSets) { makePalsAsSpecified(std::vector<ColorSet> const &colorSets) {
// Convert the palette spec to actual palettes // Convert the palette spec to actual palettes
std::vector<Palette> palettes(options.palSpec.size()); std::vector<Palette> palettes(options.palSpec.size());
@@ -726,19 +725,15 @@ struct UniqueTiles {
UniqueTiles(UniqueTiles &&) = default; UniqueTiles(UniqueTiles &&) = default;
// Adds a tile to the collection, and returns its ID // Adds a tile to the collection, and returns its ID
std::tuple<uint16_t, TileData::MatchType> addTile(TileData newTile) { std::pair<uint16_t, TileData::MatchType> addTile(TileData newTile) {
auto [tileData, inserted] = tileset.insert(newTile); if (auto [tileData, inserted] = tileset.insert(newTile); inserted) {
TileData::MatchType matchType = TileData::NOPE;
if (inserted) {
// Give the new tile the next available unique ID // Give the new tile the next available unique ID
tileData->tileID = static_cast<uint16_t>(tiles.size()); tileData->tileID = static_cast<uint16_t>(tiles.size());
// Pointers are never invalidated! tiles.emplace_back(&*tileData); // Pointers are never invalidated!
tiles.emplace_back(&*tileData); return {tileData->tileID, TileData::NOPE};
} else { } else {
matchType = tileData->tryMatching(newTile); return {tileData->tileID, tileData->tryMatching(newTile)};
} }
return {tileData->tileID, matchType};
} }
size_t size() const { return tiles.size(); } size_t size() const { return tiles.size(); }

View File

@@ -1,39 +1,40 @@
#include "link/fstack.hpp" #include "link/fstack.hpp"
#include <inttypes.h> #include <inttypes.h>
#include <utility>
#include "backtrace.hpp" #include "backtrace.hpp"
#include "link/warning.hpp" #include "link/warning.hpp"
std::vector<std::pair<std::string, uint32_t>> FileStackNode::backtrace(uint32_t curLineNo) const { using TraceNode = std::pair<std::string, uint32_t>;
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
assume(parent); // REPT nodes use their parent's name static std::vector<TraceNode> backtrace(FileStackNode const &node, uint32_t curLineNo) {
std::vector<std::pair<std::string, uint32_t>> nodes = parent->backtrace(lineNo); if (!node.parent) {
assume(!nodes.empty()); assume(node.type != NODE_REPT && std::holds_alternative<std::string>(node.data));
std::string reptChain = nodes.back().first; return {
for (uint32_t iter : iters()) { {node.name(), curLineNo}
};
}
std::vector<TraceNode> traceNodes = backtrace(*node.parent, node.lineNo);
if (std::holds_alternative<std::vector<uint32_t>>(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("::REPT~");
reptChain.append(std::to_string(iter)); reptChain.append(std::to_string(iter));
} }
nodes.emplace_back(reptChain, curLineNo); traceNodes.emplace_back(reptChain, curLineNo);
return nodes;
} else if (parent) {
std::vector<std::pair<std::string, uint32_t>> nodes = parent->backtrace(lineNo);
nodes.emplace_back(name(), curLineNo);
return nodes;
} else { } else {
return { traceNodes.emplace_back(node.name(), curLineNo);
{name(), curLineNo}
};
} }
return traceNodes;
} }
void FileStackNode::printBacktrace(uint32_t curLineNo) const { void FileStackNode::printBacktrace(uint32_t curLineNo) const {
trace_PrintBacktrace( trace_PrintBacktrace(
backtrace(curLineNo), backtrace(*this, curLineNo),
[](std::pair<std::string, uint32_t> const &node) { return node.first.c_str(); }, [](TraceNode const &node) { return node.first.c_str(); },
[](std::pair<std::string, uint32_t> const &node) { return node.second; } [](TraceNode const &node) { return node.second; }
); );
} }

View File

@@ -7,7 +7,7 @@
#include <memory> #include <memory>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <tuple> #include <utility>
#include "helpers.hpp" // assume, literal_strlen #include "helpers.hpp" // assume, literal_strlen
#include "linkdefs.hpp" #include "linkdefs.hpp"
@@ -409,7 +409,7 @@ void sdobj_ReadFile(FileStackNode const &src, FILE *file, std::vector<Symbol> &f
if (other) { if (other) {
// The same symbol can only be defined twice if neither // The same symbol can only be defined twice if neither
// definition is in a floating section // definition is in a floating section
auto checkSymbol = [](Symbol const &sym) -> std::tuple<Section *, int32_t> { auto checkSymbol = [](Symbol const &sym) -> std::pair<Section *, int32_t> {
if (std::holds_alternative<Label>(sym.data)) { if (std::holds_alternative<Label>(sym.data)) {
Label const &label = std::get<Label>(sym.data); Label const &label = std::get<Label>(sym.data);
return {label.section, label.offset}; return {label.section, label.offset};