Replace DefaultInitVec with std::vector (#1732)

This commit is contained in:
Rangi
2025-07-08 14:55:28 -04:00
committed by GitHub
parent fda54fd0c3
commit 5de05e2e4b
6 changed files with 30 additions and 75 deletions

View File

@@ -1,38 +0,0 @@
// SPDX-License-Identifier: MIT
#ifndef RGBDS_DEFAULT_INIT_ALLOC_HPP
#define RGBDS_DEFAULT_INIT_ALLOC_HPP
#include <memory>
#include <vector>
// Allocator adaptor that interposes construct() calls to convert value-initialization
// (which is what you get with e.g. `vector::resize`) into default-initialization (which does not
// zero out non-class types).
// From
// https://stackoverflow.com/questions/21028299/is-this-behavior-of-vectorresizesize-type-n-under-c11-and-boost-container/21028912#21028912
template<typename T, typename A = std::allocator<T>>
class default_init_allocator : public A {
using a_t = std::allocator_traits<A>;
public:
template<typename U>
struct rebind {
using other = default_init_allocator<U, typename a_t::template rebind_alloc<U>>;
};
using A::A; // Inherit the allocator's constructors
template<typename U>
void construct(U *ptr) noexcept(std::is_nothrow_default_constructible_v<U>) {
::new (static_cast<void *>(ptr)) U;
}
template<typename U, typename... Args>
void construct(U *ptr, Args &&...args) {
a_t::construct(static_cast<A &>(*this), ptr, std::forward<Args>(args)...);
}
};
template<typename T>
using DefaultInitVec = std::vector<T, default_init_allocator<T>>;
#endif // RGBDS_DEFAULT_INIT_ALLOC_HPP

View File

@@ -3,16 +3,15 @@
#ifndef RGBDS_GFX_PAL_PACKING_HPP #ifndef RGBDS_GFX_PAL_PACKING_HPP
#define RGBDS_GFX_PAL_PACKING_HPP #define RGBDS_GFX_PAL_PACKING_HPP
#include <stddef.h>
#include <tuple> #include <tuple>
#include <vector> #include <vector>
#include "defaultinitvec.hpp"
struct Palette; struct Palette;
class ProtoPalette; class ProtoPalette;
// Returns which palette each proto-palette maps to, and how many palettes are necessary // Returns which palette each proto-palette maps to, and how many palettes are necessary
std::tuple<DefaultInitVec<size_t>, size_t> std::tuple<std::vector<size_t>, size_t>
overloadAndRemove(std::vector<ProtoPalette> const &protoPalettes); overloadAndRemove(std::vector<ProtoPalette> const &protoPalettes);
#endif // RGBDS_GFX_PAL_PACKING_HPP #endif // RGBDS_GFX_PAL_PACKING_HPP

View File

@@ -349,7 +349,7 @@ static void decant(
); );
} }
std::tuple<DefaultInitVec<size_t>, size_t> std::tuple<std::vector<size_t>, size_t>
overloadAndRemove(std::vector<ProtoPalette> const &protoPalettes) { overloadAndRemove(std::vector<ProtoPalette> const &protoPalettes) {
options.verbosePrint( options.verbosePrint(
Options::VERB_LOG_ACT, "Paginating palettes using \"overload-and-remove\" strategy...\n" Options::VERB_LOG_ACT, "Paginating palettes using \"overload-and-remove\" strategy...\n"
@@ -361,8 +361,8 @@ std::tuple<DefaultInitVec<size_t>, size_t>
ProtoPalette const &rhs = protoPalettes[right]; ProtoPalette const &rhs = protoPalettes[right];
return lhs.size() > rhs.size(); // We want the proto-pals to be sorted *largest first*! return lhs.size() > rhs.size(); // We want the proto-pals to be sorted *largest first*!
}; };
DefaultInitVec<size_t> sortedProtoPalIDs(protoPalettes.size()); std::vector<size_t> sortedProtoPalIDs;
sortedProtoPalIDs.clear(); sortedProtoPalIDs.reserve(protoPalettes.size());
for (size_t i = 0; i < protoPalettes.size(); ++i) { for (size_t i = 0; i < protoPalettes.size(); ++i) {
sortedProtoPalIDs.insert( sortedProtoPalIDs.insert(
std::lower_bound(RANGE(sortedProtoPalIDs), i, indexOfLargestProtoPalFirst), i std::lower_bound(RANGE(sortedProtoPalIDs), i, indexOfLargestProtoPalFirst), i
@@ -582,7 +582,7 @@ std::tuple<DefaultInitVec<size_t>, size_t>
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
DefaultInitVec<size_t> mappings(protoPalettes.size()); std::vector<size_t> mappings(protoPalettes.size());
for (size_t i = 0; i < assignments.size(); ++i) { for (size_t i = 0; i < assignments.size(); ++i) {
for (ProtoPalAttrs const &attrs : assignments[i]) { for (ProtoPalAttrs const &attrs : assignments[i]) {
mappings[attrs.protoPalIndex] = i; mappings[attrs.protoPalIndex] = i;

View File

@@ -15,7 +15,6 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "defaultinitvec.hpp"
#include "error.hpp" #include "error.hpp"
#include "file.hpp" #include "file.hpp"
#include "helpers.hpp" #include "helpers.hpp"
@@ -76,7 +75,7 @@ class Png {
// These are cached for speed // These are cached for speed
uint32_t width, height; uint32_t width, height;
DefaultInitVec<Rgba> pixels; std::vector<Rgba> pixels;
ImagePalette colors; ImagePalette colors;
int colorType; int colorType;
int nbColors; int nbColors;
@@ -357,7 +356,7 @@ public:
size_t nbRowBytes = png_get_rowbytes(png, info); size_t nbRowBytes = png_get_rowbytes(png, info);
assume(nbRowBytes != 0); assume(nbRowBytes != 0);
DefaultInitVec<png_byte> row(nbRowBytes); std::vector<png_byte> row(nbRowBytes);
// Holds known-conflicting color pairs to avoid warning about them twice. // Holds known-conflicting color pairs to avoid warning about them twice.
// We don't need to worry about transitivity, as ImagePalette slots are immutable once // We don't need to worry about transitivity, as ImagePalette slots are immutable once
// assigned, and conflicts always occur between that and another color. // assigned, and conflicts always occur between that and another color.
@@ -546,7 +545,7 @@ struct AttrmapEntry {
static constexpr size_t background = static_cast<size_t>(-2); static constexpr size_t background = static_cast<size_t>(-2);
bool isBackgroundTile() const { return protoPaletteID == background; } bool isBackgroundTile() const { return protoPaletteID == background; }
size_t getPalID(DefaultInitVec<size_t> const &mappings) const { size_t getPalID(std::vector<size_t> const &mappings) const {
return mappings[isBackgroundTile() || protoPaletteID == transparent ? 0 : protoPaletteID]; return mappings[isBackgroundTile() || protoPaletteID == transparent ? 0 : protoPaletteID];
} }
}; };
@@ -575,7 +574,7 @@ static void generatePalSpec(Png const &png) {
} }
} }
static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>> static std::tuple<std::vector<size_t>, std::vector<Palette>>
generatePalettes(std::vector<ProtoPalette> const &protoPalettes, Png const &png) { generatePalettes(std::vector<ProtoPalette> const &protoPalettes, Png const &png) {
// Run a "pagination" problem solver // Run a "pagination" problem solver
auto [mappings, nbPalettes] = overloadAndRemove(protoPalettes); auto [mappings, nbPalettes] = overloadAndRemove(protoPalettes);
@@ -625,7 +624,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
return {mappings, palettes}; return {mappings, palettes};
} }
static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>> static std::tuple<std::vector<size_t>, std::vector<Palette>>
makePalsAsSpecified(std::vector<ProtoPalette> const &protoPalettes) { makePalsAsSpecified(std::vector<ProtoPalette> const &protoPalettes) {
// 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());
@@ -648,7 +647,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
}; };
// Iterate through proto-palettes, and try mapping them to the specified palettes // Iterate through proto-palettes, and try mapping them to the specified palettes
DefaultInitVec<size_t> mappings(protoPalettes.size()); std::vector<size_t> mappings(protoPalettes.size());
bool bad = false; bool bad = false;
for (size_t i = 0; i < protoPalettes.size(); ++i) { for (size_t i = 0; i < protoPalettes.size(); ++i) {
ProtoPalette const &protoPal = protoPalettes[i]; ProtoPalette const &protoPal = protoPalettes[i];
@@ -857,9 +856,9 @@ struct std::hash<TileData> {
static void outputUnoptimizedTileData( static void outputUnoptimizedTileData(
Png const &png, Png const &png,
DefaultInitVec<AttrmapEntry> const &attrmap, std::vector<AttrmapEntry> const &attrmap,
std::vector<Palette> const &palettes, std::vector<Palette> const &palettes,
DefaultInitVec<size_t> const &mappings std::vector<size_t> const &mappings
) { ) {
File output; File output;
if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) { if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
@@ -900,7 +899,7 @@ static void outputUnoptimizedTileData(
} }
static void outputUnoptimizedMaps( static void outputUnoptimizedMaps(
DefaultInitVec<AttrmapEntry> const &attrmap, DefaultInitVec<size_t> const &mappings std::vector<AttrmapEntry> const &attrmap, std::vector<size_t> const &mappings
) { ) {
std::optional<File> tilemapOutput, attrmapOutput, palmapOutput; std::optional<File> tilemapOutput, attrmapOutput, palmapOutput;
auto autoOpenPath = [](std::string const &path, std::optional<File> &file) { auto autoOpenPath = [](std::string const &path, std::optional<File> &file) {
@@ -983,9 +982,9 @@ struct UniqueTiles {
// twice) // twice)
static UniqueTiles dedupTiles( static UniqueTiles dedupTiles(
Png const &png, Png const &png,
DefaultInitVec<AttrmapEntry> &attrmap, std::vector<AttrmapEntry> &attrmap,
std::vector<Palette> const &palettes, std::vector<Palette> const &palettes,
DefaultInitVec<size_t> const &mappings std::vector<size_t> const &mappings
) { ) {
// Iterate throughout the image, generating tile data as we go // Iterate throughout the image, generating tile data as we go
// (We don't need the full tile data to be able to dedup tiles, but we don't lose anything // (We don't need the full tile data to be able to dedup tiles, but we don't lose anything
@@ -1088,7 +1087,7 @@ static void outputTileData(UniqueTiles const &tiles) {
} }
} }
static void outputTilemap(DefaultInitVec<AttrmapEntry> const &attrmap) { static void outputTilemap(std::vector<AttrmapEntry> const &attrmap) {
File output; File output;
if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) { if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START // LCOV_EXCL_START
@@ -1101,9 +1100,8 @@ static void outputTilemap(DefaultInitVec<AttrmapEntry> const &attrmap) {
} }
} }
static void outputAttrmap( static void
DefaultInitVec<AttrmapEntry> const &attrmap, DefaultInitVec<size_t> const &mappings outputAttrmap(std::vector<AttrmapEntry> const &attrmap, std::vector<size_t> const &mappings) {
) {
File output; File output;
if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) { if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START // LCOV_EXCL_START
@@ -1119,9 +1117,8 @@ static void outputAttrmap(
} }
} }
static void outputPalmap( static void
DefaultInitVec<AttrmapEntry> const &attrmap, DefaultInitVec<size_t> const &mappings outputPalmap(std::vector<AttrmapEntry> const &attrmap, std::vector<size_t> const &mappings) {
) {
File output; File output;
if (!output.open(options.palmap, std::ios_base::out | std::ios_base::binary)) { if (!output.open(options.palmap, std::ios_base::out | std::ios_base::binary)) {
// LCOV_EXCL_START // LCOV_EXCL_START
@@ -1184,7 +1181,7 @@ void process() {
// perform even if no output is requested), and because it's necessary to generate any // perform even if no output is requested), and because it's necessary to generate any
// output (with the exception of an un-duplicated tilemap, but that's an acceptable loss.) // output (with the exception of an un-duplicated tilemap, but that's an acceptable loss.)
std::vector<ProtoPalette> protoPalettes; std::vector<ProtoPalette> protoPalettes;
DefaultInitVec<AttrmapEntry> attrmap{}; std::vector<AttrmapEntry> attrmap{};
for (auto tile : png.visitAsTiles()) { for (auto tile : png.visitAsTiles()) {
AttrmapEntry &attrs = attrmap.emplace_back(); AttrmapEntry &attrs = attrmap.emplace_back();

View File

@@ -12,7 +12,6 @@
#include <string.h> #include <string.h>
#include <vector> #include <vector>
#include "defaultinitvec.hpp"
#include "error.hpp" #include "error.hpp"
#include "file.hpp" #include "file.hpp"
#include "helpers.hpp" // assume #include "helpers.hpp" // assume
@@ -20,12 +19,12 @@
#include "gfx/main.hpp" #include "gfx/main.hpp"
#include "gfx/warning.hpp" #include "gfx/warning.hpp"
static DefaultInitVec<uint8_t> readInto(std::string const &path) { static std::vector<uint8_t> readInto(std::string const &path) {
File file; File file;
if (!file.open(path, std::ios::in | std::ios::binary)) { if (!file.open(path, std::ios::in | std::ios::binary)) {
fatal("Failed to open \"%s\": %s", file.c_str(path), strerror(errno)); fatal("Failed to open \"%s\": %s", file.c_str(path), strerror(errno));
} }
DefaultInitVec<uint8_t> data(128 * 16); // Begin with some room pre-allocated std::vector<uint8_t> data(128 * 16); // Begin with some room pre-allocated
size_t curSize = 0; size_t curSize = 0;
for (;;) { for (;;) {
@@ -143,7 +142,7 @@ void reverse() {
size_t const nbTiles = tiles.size() / tileSize; size_t const nbTiles = tiles.size() / tileSize;
options.verbosePrint(Options::VERB_INTERM, "Read %zu tiles.\n", nbTiles); options.verbosePrint(Options::VERB_INTERM, "Read %zu tiles.\n", nbTiles);
size_t mapSize = nbTiles + options.trim; // Image size in tiles size_t mapSize = nbTiles + options.trim; // Image size in tiles
std::optional<DefaultInitVec<uint8_t>> tilemap; std::optional<std::vector<uint8_t>> tilemap;
if (!options.tilemap.empty()) { if (!options.tilemap.empty()) {
tilemap = readInto(options.tilemap); tilemap = readInto(options.tilemap);
mapSize = tilemap->size(); mapSize = tilemap->size();
@@ -271,7 +270,7 @@ void reverse() {
palettes = std::move(options.palSpec); // We won't be using it again. palettes = std::move(options.palSpec); // We won't be using it again.
} }
std::optional<DefaultInitVec<uint8_t>> attrmap; std::optional<std::vector<uint8_t>> attrmap;
uint16_t nbTilesInBank[2] = {0, 0}; // Only used if there is an attrmap. uint16_t nbTilesInBank[2] = {0, 0}; // Only used if there is an attrmap.
if (!options.attrmap.empty()) { if (!options.attrmap.empty()) {
attrmap = readInto(options.attrmap); attrmap = readInto(options.attrmap);
@@ -393,7 +392,7 @@ void reverse() {
requireZeroErrors(); requireZeroErrors();
} }
std::optional<DefaultInitVec<uint8_t>> palmap; std::optional<std::vector<uint8_t>> palmap;
if (!options.palmap.empty()) { if (!options.palmap.empty()) {
palmap = readInto(options.palmap); palmap = readInto(options.palmap);
if (palmap->size() != mapSize) { if (palmap->size() != mapSize) {

View File

@@ -14,8 +14,6 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "defaultinitvec.hpp" // Reused from RGBDS
#include "gfx/rgba.hpp" // Reused from RGBGFX #include "gfx/rgba.hpp" // Reused from RGBGFX
// For `execProg` (Windows and POSIX spawn child processes differently) // For `execProg` (Windows and POSIX spawn child processes differently)
@@ -90,7 +88,7 @@ class Png {
// These are cached for speed // These are cached for speed
uint32_t width, height; uint32_t width, height;
DefaultInitVec<Rgba> pixels; std::vector<Rgba> pixels;
int colorType; int colorType;
int nbColors; int nbColors;
png_colorp embeddedPal = nullptr; png_colorp embeddedPal = nullptr;
@@ -239,7 +237,7 @@ public:
size_t nbRowBytes = png_get_rowbytes(png, info); size_t nbRowBytes = png_get_rowbytes(png, info);
assert(nbRowBytes != 0); assert(nbRowBytes != 0);
DefaultInitVec<png_byte> row(nbRowBytes); std::vector<png_byte> row(nbRowBytes);
if (interlaceType == PNG_INTERLACE_NONE) { if (interlaceType == PNG_INTERLACE_NONE) {
for (png_uint_32 y = 0; y < height; ++y) { for (png_uint_32 y = 0; y < height; ++y) {