diff --git a/include/defaultinitvec.hpp b/include/defaultinitvec.hpp deleted file mode 100644 index 949b243f..00000000 --- a/include/defaultinitvec.hpp +++ /dev/null @@ -1,38 +0,0 @@ -// SPDX-License-Identifier: MIT - -#ifndef RGBDS_DEFAULT_INIT_ALLOC_HPP -#define RGBDS_DEFAULT_INIT_ALLOC_HPP - -#include -#include - -// 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> -class default_init_allocator : public A { - using a_t = std::allocator_traits; -public: - template - struct rebind { - using other = default_init_allocator>; - }; - - using A::A; // Inherit the allocator's constructors - - template - void construct(U *ptr) noexcept(std::is_nothrow_default_constructible_v) { - ::new (static_cast(ptr)) U; - } - template - void construct(U *ptr, Args &&...args) { - a_t::construct(static_cast(*this), ptr, std::forward(args)...); - } -}; - -template -using DefaultInitVec = std::vector>; - -#endif // RGBDS_DEFAULT_INIT_ALLOC_HPP diff --git a/include/gfx/pal_packing.hpp b/include/gfx/pal_packing.hpp index 7b1aefd9..fa0339fd 100644 --- a/include/gfx/pal_packing.hpp +++ b/include/gfx/pal_packing.hpp @@ -3,16 +3,15 @@ #ifndef RGBDS_GFX_PAL_PACKING_HPP #define RGBDS_GFX_PAL_PACKING_HPP +#include #include #include -#include "defaultinitvec.hpp" - struct Palette; class ProtoPalette; // Returns which palette each proto-palette maps to, and how many palettes are necessary -std::tuple, size_t> +std::tuple, size_t> overloadAndRemove(std::vector const &protoPalettes); #endif // RGBDS_GFX_PAL_PACKING_HPP diff --git a/src/gfx/pal_packing.cpp b/src/gfx/pal_packing.cpp index a08c829e..8d57624d 100644 --- a/src/gfx/pal_packing.cpp +++ b/src/gfx/pal_packing.cpp @@ -349,7 +349,7 @@ static void decant( ); } -std::tuple, size_t> +std::tuple, size_t> overloadAndRemove(std::vector const &protoPalettes) { options.verbosePrint( Options::VERB_LOG_ACT, "Paginating palettes using \"overload-and-remove\" strategy...\n" @@ -361,8 +361,8 @@ std::tuple, size_t> ProtoPalette const &rhs = protoPalettes[right]; return lhs.size() > rhs.size(); // We want the proto-pals to be sorted *largest first*! }; - DefaultInitVec sortedProtoPalIDs(protoPalettes.size()); - sortedProtoPalIDs.clear(); + std::vector sortedProtoPalIDs; + sortedProtoPalIDs.reserve(protoPalettes.size()); for (size_t i = 0; i < protoPalettes.size(); ++i) { sortedProtoPalIDs.insert( std::lower_bound(RANGE(sortedProtoPalIDs), i, indexOfLargestProtoPalFirst), i @@ -582,7 +582,7 @@ std::tuple, size_t> } // LCOV_EXCL_STOP - DefaultInitVec mappings(protoPalettes.size()); + std::vector mappings(protoPalettes.size()); for (size_t i = 0; i < assignments.size(); ++i) { for (ProtoPalAttrs const &attrs : assignments[i]) { mappings[attrs.protoPalIndex] = i; diff --git a/src/gfx/process.cpp b/src/gfx/process.cpp index 8be732fe..dfeeba18 100644 --- a/src/gfx/process.cpp +++ b/src/gfx/process.cpp @@ -15,7 +15,6 @@ #include #include -#include "defaultinitvec.hpp" #include "error.hpp" #include "file.hpp" #include "helpers.hpp" @@ -76,7 +75,7 @@ class Png { // These are cached for speed uint32_t width, height; - DefaultInitVec pixels; + std::vector pixels; ImagePalette colors; int colorType; int nbColors; @@ -357,7 +356,7 @@ public: size_t nbRowBytes = png_get_rowbytes(png, info); assume(nbRowBytes != 0); - DefaultInitVec row(nbRowBytes); + std::vector row(nbRowBytes); // 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 // assigned, and conflicts always occur between that and another color. @@ -546,7 +545,7 @@ struct AttrmapEntry { static constexpr size_t background = static_cast(-2); bool isBackgroundTile() const { return protoPaletteID == background; } - size_t getPalID(DefaultInitVec const &mappings) const { + size_t getPalID(std::vector const &mappings) const { return mappings[isBackgroundTile() || protoPaletteID == transparent ? 0 : protoPaletteID]; } }; @@ -575,7 +574,7 @@ static void generatePalSpec(Png const &png) { } } -static std::tuple, std::vector> +static std::tuple, std::vector> generatePalettes(std::vector const &protoPalettes, Png const &png) { // Run a "pagination" problem solver auto [mappings, nbPalettes] = overloadAndRemove(protoPalettes); @@ -625,7 +624,7 @@ static std::tuple, std::vector> return {mappings, palettes}; } -static std::tuple, std::vector> +static std::tuple, std::vector> makePalsAsSpecified(std::vector const &protoPalettes) { // Convert the palette spec to actual palettes std::vector palettes(options.palSpec.size()); @@ -648,7 +647,7 @@ static std::tuple, std::vector> }; // Iterate through proto-palettes, and try mapping them to the specified palettes - DefaultInitVec mappings(protoPalettes.size()); + std::vector mappings(protoPalettes.size()); bool bad = false; for (size_t i = 0; i < protoPalettes.size(); ++i) { ProtoPalette const &protoPal = protoPalettes[i]; @@ -857,9 +856,9 @@ struct std::hash { static void outputUnoptimizedTileData( Png const &png, - DefaultInitVec const &attrmap, + std::vector const &attrmap, std::vector const &palettes, - DefaultInitVec const &mappings + std::vector const &mappings ) { File output; if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) { @@ -900,7 +899,7 @@ static void outputUnoptimizedTileData( } static void outputUnoptimizedMaps( - DefaultInitVec const &attrmap, DefaultInitVec const &mappings + std::vector const &attrmap, std::vector const &mappings ) { std::optional tilemapOutput, attrmapOutput, palmapOutput; auto autoOpenPath = [](std::string const &path, std::optional &file) { @@ -983,9 +982,9 @@ struct UniqueTiles { // twice) static UniqueTiles dedupTiles( Png const &png, - DefaultInitVec &attrmap, + std::vector &attrmap, std::vector const &palettes, - DefaultInitVec const &mappings + std::vector const &mappings ) { // 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 @@ -1088,7 +1087,7 @@ static void outputTileData(UniqueTiles const &tiles) { } } -static void outputTilemap(DefaultInitVec const &attrmap) { +static void outputTilemap(std::vector const &attrmap) { File output; if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START @@ -1101,9 +1100,8 @@ static void outputTilemap(DefaultInitVec const &attrmap) { } } -static void outputAttrmap( - DefaultInitVec const &attrmap, DefaultInitVec const &mappings -) { +static void + outputAttrmap(std::vector const &attrmap, std::vector const &mappings) { File output; if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START @@ -1119,9 +1117,8 @@ static void outputAttrmap( } } -static void outputPalmap( - DefaultInitVec const &attrmap, DefaultInitVec const &mappings -) { +static void + outputPalmap(std::vector const &attrmap, std::vector const &mappings) { File output; if (!output.open(options.palmap, std::ios_base::out | std::ios_base::binary)) { // LCOV_EXCL_START @@ -1184,7 +1181,7 @@ void process() { // 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.) std::vector protoPalettes; - DefaultInitVec attrmap{}; + std::vector attrmap{}; for (auto tile : png.visitAsTiles()) { AttrmapEntry &attrs = attrmap.emplace_back(); diff --git a/src/gfx/reverse.cpp b/src/gfx/reverse.cpp index 35ca53d7..e0d41e1d 100644 --- a/src/gfx/reverse.cpp +++ b/src/gfx/reverse.cpp @@ -12,7 +12,6 @@ #include #include -#include "defaultinitvec.hpp" #include "error.hpp" #include "file.hpp" #include "helpers.hpp" // assume @@ -20,12 +19,12 @@ #include "gfx/main.hpp" #include "gfx/warning.hpp" -static DefaultInitVec readInto(std::string const &path) { +static std::vector readInto(std::string const &path) { File file; if (!file.open(path, std::ios::in | std::ios::binary)) { fatal("Failed to open \"%s\": %s", file.c_str(path), strerror(errno)); } - DefaultInitVec data(128 * 16); // Begin with some room pre-allocated + std::vector data(128 * 16); // Begin with some room pre-allocated size_t curSize = 0; for (;;) { @@ -143,7 +142,7 @@ void reverse() { size_t const nbTiles = tiles.size() / tileSize; options.verbosePrint(Options::VERB_INTERM, "Read %zu tiles.\n", nbTiles); size_t mapSize = nbTiles + options.trim; // Image size in tiles - std::optional> tilemap; + std::optional> tilemap; if (!options.tilemap.empty()) { tilemap = readInto(options.tilemap); mapSize = tilemap->size(); @@ -271,7 +270,7 @@ void reverse() { palettes = std::move(options.palSpec); // We won't be using it again. } - std::optional> attrmap; + std::optional> attrmap; uint16_t nbTilesInBank[2] = {0, 0}; // Only used if there is an attrmap. if (!options.attrmap.empty()) { attrmap = readInto(options.attrmap); @@ -393,7 +392,7 @@ void reverse() { requireZeroErrors(); } - std::optional> palmap; + std::optional> palmap; if (!options.palmap.empty()) { palmap = readInto(options.palmap); if (palmap->size() != mapSize) { diff --git a/test/gfx/rgbgfx_test.cpp b/test/gfx/rgbgfx_test.cpp index 1c0fd1d8..45f52d7e 100644 --- a/test/gfx/rgbgfx_test.cpp +++ b/test/gfx/rgbgfx_test.cpp @@ -14,8 +14,6 @@ #include #include -#include "defaultinitvec.hpp" // Reused from RGBDS - #include "gfx/rgba.hpp" // Reused from RGBGFX // For `execProg` (Windows and POSIX spawn child processes differently) @@ -90,7 +88,7 @@ class Png { // These are cached for speed uint32_t width, height; - DefaultInitVec pixels; + std::vector pixels; int colorType; int nbColors; png_colorp embeddedPal = nullptr; @@ -239,7 +237,7 @@ public: size_t nbRowBytes = png_get_rowbytes(png, info); assert(nbRowBytes != 0); - DefaultInitVec row(nbRowBytes); + std::vector row(nbRowBytes); if (interlaceType == PNG_INTERLACE_NONE) { for (png_uint_32 y = 0; y < height; ++y) {