mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Replace DefaultInitVec with std::vector (#1732)
This commit is contained in:
@@ -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
|
||||
@@ -3,16 +3,15 @@
|
||||
#ifndef RGBDS_GFX_PAL_PACKING_HPP
|
||||
#define RGBDS_GFX_PAL_PACKING_HPP
|
||||
|
||||
#include <stddef.h>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include "defaultinitvec.hpp"
|
||||
|
||||
struct Palette;
|
||||
class ProtoPalette;
|
||||
|
||||
// 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);
|
||||
|
||||
#endif // RGBDS_GFX_PAL_PACKING_HPP
|
||||
|
||||
@@ -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) {
|
||||
options.verbosePrint(
|
||||
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];
|
||||
return lhs.size() > rhs.size(); // We want the proto-pals to be sorted *largest first*!
|
||||
};
|
||||
DefaultInitVec<size_t> sortedProtoPalIDs(protoPalettes.size());
|
||||
sortedProtoPalIDs.clear();
|
||||
std::vector<size_t> 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<DefaultInitVec<size_t>, size_t>
|
||||
}
|
||||
// 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 (ProtoPalAttrs const &attrs : assignments[i]) {
|
||||
mappings[attrs.protoPalIndex] = i;
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#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<Rgba> pixels;
|
||||
std::vector<Rgba> pixels;
|
||||
ImagePalette colors;
|
||||
int colorType;
|
||||
int nbColors;
|
||||
@@ -357,7 +356,7 @@ public:
|
||||
|
||||
size_t nbRowBytes = png_get_rowbytes(png, info);
|
||||
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.
|
||||
// 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<size_t>(-2);
|
||||
|
||||
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];
|
||||
}
|
||||
};
|
||||
@@ -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) {
|
||||
// Run a "pagination" problem solver
|
||||
auto [mappings, nbPalettes] = overloadAndRemove(protoPalettes);
|
||||
@@ -625,7 +624,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
|
||||
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) {
|
||||
// Convert the palette spec to actual palettes
|
||||
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
|
||||
DefaultInitVec<size_t> mappings(protoPalettes.size());
|
||||
std::vector<size_t> 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<TileData> {
|
||||
|
||||
static void outputUnoptimizedTileData(
|
||||
Png const &png,
|
||||
DefaultInitVec<AttrmapEntry> const &attrmap,
|
||||
std::vector<AttrmapEntry> const &attrmap,
|
||||
std::vector<Palette> const &palettes,
|
||||
DefaultInitVec<size_t> const &mappings
|
||||
std::vector<size_t> 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<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;
|
||||
auto autoOpenPath = [](std::string const &path, std::optional<File> &file) {
|
||||
@@ -983,9 +982,9 @@ struct UniqueTiles {
|
||||
// twice)
|
||||
static UniqueTiles dedupTiles(
|
||||
Png const &png,
|
||||
DefaultInitVec<AttrmapEntry> &attrmap,
|
||||
std::vector<AttrmapEntry> &attrmap,
|
||||
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
|
||||
// (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;
|
||||
if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) {
|
||||
// LCOV_EXCL_START
|
||||
@@ -1101,9 +1100,8 @@ static void outputTilemap(DefaultInitVec<AttrmapEntry> const &attrmap) {
|
||||
}
|
||||
}
|
||||
|
||||
static void outputAttrmap(
|
||||
DefaultInitVec<AttrmapEntry> const &attrmap, DefaultInitVec<size_t> const &mappings
|
||||
) {
|
||||
static void
|
||||
outputAttrmap(std::vector<AttrmapEntry> const &attrmap, std::vector<size_t> 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<AttrmapEntry> const &attrmap, DefaultInitVec<size_t> const &mappings
|
||||
) {
|
||||
static void
|
||||
outputPalmap(std::vector<AttrmapEntry> const &attrmap, std::vector<size_t> 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<ProtoPalette> protoPalettes;
|
||||
DefaultInitVec<AttrmapEntry> attrmap{};
|
||||
std::vector<AttrmapEntry> attrmap{};
|
||||
|
||||
for (auto tile : png.visitAsTiles()) {
|
||||
AttrmapEntry &attrs = attrmap.emplace_back();
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
#include <string.h>
|
||||
#include <vector>
|
||||
|
||||
#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<uint8_t> readInto(std::string const &path) {
|
||||
static std::vector<uint8_t> 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<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;
|
||||
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<DefaultInitVec<uint8_t>> tilemap;
|
||||
std::optional<std::vector<uint8_t>> 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<DefaultInitVec<uint8_t>> attrmap;
|
||||
std::optional<std::vector<uint8_t>> 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<DefaultInitVec<uint8_t>> palmap;
|
||||
std::optional<std::vector<uint8_t>> palmap;
|
||||
if (!options.palmap.empty()) {
|
||||
palmap = readInto(options.palmap);
|
||||
if (palmap->size() != mapSize) {
|
||||
|
||||
@@ -14,8 +14,6 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<Rgba> pixels;
|
||||
std::vector<Rgba> 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<png_byte> row(nbRowBytes);
|
||||
std::vector<png_byte> row(nbRowBytes);
|
||||
|
||||
if (interlaceType == PNG_INTERLACE_NONE) {
|
||||
for (png_uint_32 y = 0; y < height; ++y) {
|
||||
|
||||
Reference in New Issue
Block a user