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

@@ -50,9 +50,9 @@ static std::vector<std::string> includePaths = {""}; // -I
static std::deque<std::string> preIncludeNames; // -P
static bool failedOnMissingInclude = false;
std::string FileStackNode::reptChain() const {
static std::string reptChain(FileStackNode const &node) {
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--;) {
chain.append("::REPT~");
chain.append(std::to_string(nodeIters[i]));
@@ -60,29 +60,31 @@ std::string FileStackNode::reptChain() const {
return chain;
}
std::vector<std::pair<std::string, uint32_t>> FileStackNode::backtrace(uint32_t curLineNo) const {
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
assume(parent); // REPT nodes use their parent's name
std::vector<std::pair<std::string, uint32_t>> nodes = parent->backtrace(lineNo);
assume(!nodes.empty());
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 {
using TraceNode = std::pair<std::string, uint32_t>;
static std::vector<TraceNode> backtrace(FileStackNode const &node, uint32_t curLineNo) {
if (!node.parent) {
assume(node.type != NODE_REPT && std::holds_alternative<std::string>(node.data));
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 {
trace_PrintBacktrace(
backtrace(curLineNo),
[](std::pair<std::string, uint32_t> const &node) { return node.first.c_str(); },
[](std::pair<std::string, uint32_t> 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 &macro, std::shared_ptr<MacroArgs> macr
}
}
if (macro.src->type == NODE_REPT) {
fileInfoName.append(macro.src->reptChain());
fileInfoName.append(reptChain(*macro.src));
}
fileInfoName.append("::");
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());
}
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");
// Sort the color sets by size, which improves the packing algorithm's efficiency

View File

@@ -10,7 +10,6 @@
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <tuple>
#include <unordered_set>
#include <utility>
#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) {
// Run a "pagination" problem solver
auto [mappings, nbPalettes] = overloadAndRemove(colorSets);
@@ -382,7 +381,7 @@ static std::tuple<std::vector<size_t>, std::vector<Palette>>
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) {
// Convert the palette spec to actual palettes
std::vector<Palette> 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<uint16_t, TileData::MatchType> addTile(TileData newTile) {
auto [tileData, inserted] = tileset.insert(newTile);
TileData::MatchType matchType = TileData::NOPE;
if (inserted) {
std::pair<uint16_t, TileData::MatchType> addTile(TileData newTile) {
if (auto [tileData, inserted] = tileset.insert(newTile); inserted) {
// Give the new tile the next available unique ID
tileData->tileID = static_cast<uint16_t>(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(); }

View File

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

View File

@@ -7,7 +7,7 @@
#include <memory>
#include <stdint.h>
#include <string.h>
#include <tuple>
#include <utility>
#include "helpers.hpp" // assume, literal_strlen
#include "linkdefs.hpp"
@@ -409,7 +409,7 @@ void sdobj_ReadFile(FileStackNode const &src, FILE *file, std::vector<Symbol> &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<Section *, int32_t> {
auto checkSymbol = [](Symbol const &sym) -> std::pair<Section *, int32_t> {
if (std::holds_alternative<Label>(sym.data)) {
Label const &label = std::get<Label>(sym.data);
return {label.section, label.offset};