Use RANGE macro to abbreviate begin/end pairs (#1269)

This commit is contained in:
Rangi
2023-12-11 14:10:20 -05:00
committed by GitHub
parent b886b7e611
commit 495d701022
7 changed files with 40 additions and 43 deletions

View File

@@ -82,4 +82,7 @@
// (Having two instances of `arr` is OK because the contents of `sizeof` are not evaluated.) // (Having two instances of `arr` is OK because the contents of `sizeof` are not evaluated.)
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof *(arr)) #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof *(arr))
// For lack of <ranges>, this adds some more brevity
#define RANGE(s) std::begin(s), std::end(s)
#endif // HELPERS_H #endif // HELPERS_H

View File

@@ -16,6 +16,7 @@
#include <vector> #include <vector>
#include "defaultinitalloc.hpp" #include "defaultinitalloc.hpp"
#include "helpers.hpp"
#include "gfx/main.hpp" #include "gfx/main.hpp"
#include "gfx/proto_palette.hpp" #include "gfx/proto_palette.hpp"
@@ -140,7 +141,7 @@ public:
template<typename... Ts> template<typename... Ts>
void assign(Ts &&...args) { void assign(Ts &&...args) {
auto freeSlot = std::find_if_not( auto freeSlot = std::find_if_not(
_assigned.begin(), _assigned.end(), RANGE(_assigned),
[](std::optional<ProtoPalAttrs> const &slot) { return slot.has_value(); }); [](std::optional<ProtoPalAttrs> const &slot) { return slot.has_value(); });
if (freeSlot == _assigned.end()) { // We are full, use a new slot if (freeSlot == _assigned.end()) { // We are full, use a new slot
@@ -156,11 +157,11 @@ public:
bool empty() const { bool empty() const {
return std::find_if( return std::find_if(
_assigned.begin(), _assigned.end(), RANGE(_assigned),
[](std::optional<ProtoPalAttrs> const &slot) { return slot.has_value(); }) [](std::optional<ProtoPalAttrs> const &slot) { return slot.has_value(); })
== _assigned.end(); == _assigned.end();
} }
size_t nbProtoPals() const { return std::distance(begin(), end()); } size_t nbProtoPals() const { return std::distance(RANGE(*this)); }
private: private:
template<typename Iter> template<typename Iter>
@@ -168,7 +169,7 @@ private:
std::vector<ProtoPalette> const &protoPals) { std::vector<ProtoPalette> const &protoPals) {
for (; iter != end; ++iter) { for (; iter != end; ++iter) {
ProtoPalette const &protoPal = protoPals[iter->protoPalIndex]; ProtoPalette const &protoPal = protoPals[iter->protoPalIndex];
colors.insert(protoPal.begin(), protoPal.end()); colors.insert(RANGE(protoPal));
} }
} }
// This function should stay private because it returns a reference to a unique object // This function should stay private because it returns a reference to a unique object
@@ -188,7 +189,7 @@ private:
static std::unordered_set<uint16_t> colors; static std::unordered_set<uint16_t> colors;
colors.clear(); colors.clear();
addUniqueColors(colors, begin(), end(), *_protoPals); addUniqueColors(colors, RANGE(*this), *_protoPals);
return colors; return colors;
} }
public: public:
@@ -198,7 +199,7 @@ public:
size_t volume() const { return uniqueColors().size(); } size_t volume() const { return uniqueColors().size(); }
bool canFit(ProtoPalette const &protoPal) const { bool canFit(ProtoPalette const &protoPal) const {
auto &colors = uniqueColors(); auto &colors = uniqueColors();
colors.insert(protoPal.begin(), protoPal.end()); colors.insert(RANGE(protoPal));
return colors.size() <= options.maxOpaqueColors(); return colors.size() <= options.maxOpaqueColors();
} }
@@ -209,9 +210,9 @@ public:
// NOTE: this function must not call `uniqueColors`, or one of its callers will break! // NOTE: this function must not call `uniqueColors`, or one of its callers will break!
double relSize = 0.; double relSize = 0.;
for (uint16_t color : protoPal) { for (uint16_t color : protoPal) {
auto n = std::count_if(begin(), end(), [this, &color](ProtoPalAttrs const &attrs) { auto n = std::count_if(RANGE(*this), [this, &color](ProtoPalAttrs const &attrs) {
ProtoPalette const &pal = (*_protoPals)[attrs.protoPalIndex]; ProtoPalette const &pal = (*_protoPals)[attrs.protoPalIndex];
return std::find(pal.begin(), pal.end(), color) != pal.end(); return std::find(RANGE(pal), color) != pal.end();
}); });
// NOTE: The paper and the associated code disagree on this: the code has // NOTE: The paper and the associated code disagree on this: the code has
// this `1 +`, whereas the paper does not; its lack causes a division by 0 // this `1 +`, whereas the paper does not; its lack causes a division by 0
@@ -273,8 +274,7 @@ static void decant(std::vector<AssignedProtos> &assignments,
// Decant on palettes // Decant on palettes
decantOn([&protoPalettes](AssignedProtos &to, AssignedProtos &from) { decantOn([&protoPalettes](AssignedProtos &to, AssignedProtos &from) {
// If the entire palettes can be merged, move all of `from`'s proto-palettes // If the entire palettes can be merged, move all of `from`'s proto-palettes
if (to.combinedVolume(from.begin(), from.end(), protoPalettes) if (to.combinedVolume(RANGE(from), protoPalettes) <= options.maxOpaqueColors()) {
<= options.maxOpaqueColors()) {
for (ProtoPalAttrs &attrs : from) { for (ProtoPalAttrs &attrs : from) {
to.assign(attrs.protoPalIndex); to.assign(attrs.protoPalIndex);
} }
@@ -294,7 +294,7 @@ static void decant(std::vector<AssignedProtos> &assignments,
std::unordered_set<uint16_t> colors; std::unordered_set<uint16_t> colors;
std::vector<size_t> members; std::vector<size_t> members;
while (true) { while (true) {
auto iter = std::find(processed.begin(), processed.end(), true); auto iter = std::find(RANGE(processed), true);
if (iter == processed.end()) { // Processed everything! if (iter == processed.end()) { // Processed everything!
break; break;
} }
@@ -309,10 +309,8 @@ static void decant(std::vector<AssignedProtos> &assignments,
ProtoPalette const &protoPal = protoPalettes[attrs->protoPalIndex]; ProtoPalette const &protoPal = protoPalettes[attrs->protoPalIndex];
// If this is the first proto-pal, or if at least one color matches, add it // If this is the first proto-pal, or if at least one color matches, add it
if (members.empty() if (members.empty()
|| std::find_first_of(colors.begin(), colors.end(), protoPal.begin(), || std::find_first_of(RANGE(colors), RANGE(protoPal)) != colors.end()) {
protoPal.end()) colors.insert(RANGE(protoPal));
!= colors.end()) {
colors.insert(protoPal.begin(), protoPal.end());
members.push_back(iter - processed.begin()); members.push_back(iter - processed.begin());
*iter = true; // Mark that proto-pal as processed *iter = true; // Mark that proto-pal as processed
} }
@@ -320,7 +318,7 @@ static void decant(std::vector<AssignedProtos> &assignments,
++attrs; ++attrs;
} while (iter != processed.end()); } while (iter != processed.end());
if (to.combinedVolume(colors.begin(), colors.end()) <= options.maxOpaqueColors()) { if (to.combinedVolume(RANGE(colors)) <= options.maxOpaqueColors()) {
// Iterate through the component's proto-palettes, and transfer them // Iterate through the component's proto-palettes, and transfer them
auto member = from.begin(); auto member = from.begin();
size_t curIndex = 0; size_t curIndex = 0;
@@ -358,12 +356,10 @@ std::tuple<DefaultInitVec<size_t>, size_t>
DefaultInitVec<size_t> sortedProtoPalIDs(protoPalettes.size()); DefaultInitVec<size_t> sortedProtoPalIDs(protoPalettes.size());
sortedProtoPalIDs.clear(); sortedProtoPalIDs.clear();
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), i);
std::lower_bound(sortedProtoPalIDs.begin(), sortedProtoPalIDs.end(), i), i);
} }
// Begin with all proto-palettes queued up for insertion // Begin with all proto-palettes queued up for insertion
std::queue<ProtoPalAttrs> queue( std::queue<ProtoPalAttrs> queue(std::deque<ProtoPalAttrs>(RANGE(sortedProtoPalIDs)));
std::deque<ProtoPalAttrs>(sortedProtoPalIDs.begin(), sortedProtoPalIDs.end()));
// Begin with no pages // Begin with no pages
std::vector<AssignedProtos> assignments{}; std::vector<AssignedProtos> assignments{};
@@ -410,7 +406,7 @@ std::tuple<DefaultInitVec<size_t>, size_t>
return pal.size() / bestPal.relSizeOf(pal); return pal.size() / bestPal.relSizeOf(pal);
}; };
auto [minEfficiencyIter, maxEfficiencyIter] = auto [minEfficiencyIter, maxEfficiencyIter] =
std::minmax_element(bestPal.begin(), bestPal.end(), std::minmax_element(RANGE(bestPal),
[&efficiency, &protoPalettes](ProtoPalAttrs const &lhs, [&efficiency, &protoPalettes](ProtoPalAttrs const &lhs,
ProtoPalAttrs const &rhs) { ProtoPalAttrs const &rhs) {
return efficiency(protoPalettes[lhs.protoPalIndex]) return efficiency(protoPalettes[lhs.protoPalIndex])
@@ -448,7 +444,7 @@ std::tuple<DefaultInitVec<size_t>, size_t>
ProtoPalAttrs const &attrs = queue.front(); ProtoPalAttrs const &attrs = queue.front();
ProtoPalette const &protoPal = protoPalettes[attrs.protoPalIndex]; ProtoPalette const &protoPal = protoPalettes[attrs.protoPalIndex];
auto iter = auto iter =
std::find_if(assignments.begin(), assignments.end(), std::find_if(RANGE(assignments),
[&protoPal](AssignedProtos const &pal) { return pal.canFit(protoPal); }); [&protoPal](AssignedProtos const &pal) { return pal.canFit(protoPal); });
if (iter == assignments.end()) { // No such page, create a new one if (iter == assignments.end()) { // No such page, create a new one
options.verbosePrint(Options::VERB_DEBUG, options.verbosePrint(Options::VERB_DEBUG,

View File

@@ -23,7 +23,7 @@ void indexed(std::vector<Palette> &palettes, int palSize, png_color const *palRG
}; };
for (Palette &pal : palettes) { for (Palette &pal : palettes) {
std::sort(pal.begin(), pal.end(), [&](uint16_t lhs, uint16_t rhs) { std::sort(RANGE(pal), [&](uint16_t lhs, uint16_t rhs) {
// Iterate through the PNG's palette, looking for either of the two // Iterate through the PNG's palette, looking for either of the two
for (int i = 0; i < palSize; ++i) { for (int i = 0; i < palSize; ++i) {
uint16_t color = pngToRgb(i).cgbColor(); uint16_t color = pngToRgb(i).cgbColor();
@@ -52,7 +52,7 @@ void grayscale(std::vector<Palette> &palettes,
assert(palettes.size() == 1); assert(palettes.size() == 1);
Palette &palette = palettes[0]; Palette &palette = palettes[0];
std::fill(palette.colors.begin(), palette.colors.end(), Rgba::transparent); std::fill(RANGE(palette.colors), Rgba::transparent);
for (auto const &slot : colors) { for (auto const &slot : colors) {
if (!slot.has_value() || slot->isTransparent()) { if (!slot.has_value() || slot->isTransparent()) {
continue; continue;
@@ -72,7 +72,7 @@ void rgb(std::vector<Palette> &palettes) {
options.verbosePrint(Options::VERB_LOG_ACT, "Sorting palettes by \"\"\"luminance\"\"\"...\n"); options.verbosePrint(Options::VERB_LOG_ACT, "Sorting palettes by \"\"\"luminance\"\"\"...\n");
for (Palette &pal : palettes) { for (Palette &pal : palettes) {
std::sort(pal.begin(), pal.end(), [](uint16_t lhs, uint16_t rhs) { std::sort(RANGE(pal), [](uint16_t lhs, uint16_t rhs) {
return legacyLuminance(lhs) > legacyLuminance(rhs); return legacyLuminance(lhs) > legacyLuminance(rhs);
}); });
} }

View File

@@ -572,7 +572,7 @@ void parseExternalPalSpec(char const *arg) {
std::tuple{"GBC", &parseGBCFile, std::ios::binary}, std::tuple{"GBC", &parseGBCFile, std::ios::binary},
}; };
auto iter = std::find_if(parsers.begin(), parsers.end(), auto iter = std::find_if(RANGE(parsers),
[&arg, &ptr](decltype(parsers)::value_type const &parser) { [&arg, &ptr](decltype(parsers)::value_type const &parser) {
return strncasecmp(arg, std::get<0>(parser), ptr - arg) == 0; return strncasecmp(arg, std::get<0>(parser), ptr - arg) == 0;
}); });

View File

@@ -60,7 +60,7 @@ public:
} }
size_t size() const { size_t size() const {
return std::count_if(_colors.begin(), _colors.end(), return std::count_if(RANGE(_colors),
[](decltype(_colors)::value_type const &slot) { [](decltype(_colors)::value_type const &slot) {
return slot.has_value() && !slot->isTransparent(); return slot.has_value() && !slot->isTransparent();
}); });
@@ -333,7 +333,7 @@ public:
Rgba &&color) { Rgba &&color) {
if (!color.isTransparent() && !color.isOpaque()) { if (!color.isTransparent() && !color.isOpaque()) {
uint32_t css = color.toCSS(); uint32_t css = color.toCSS();
if (std::find(indeterminates.begin(), indeterminates.end(), css) if (std::find(RANGE(indeterminates), css)
== indeterminates.end()) { == indeterminates.end()) {
error("Color #%08x is neither transparent (alpha < %u) nor opaque (alpha >= " error("Color #%08x is neither transparent (alpha < %u) nor opaque (alpha >= "
"%u) [first seen at x: %" PRIu32 ", y: %" PRIu32 "]", "%u) [first seen at x: %" PRIu32 ", y: %" PRIu32 "]",
@@ -343,7 +343,7 @@ public:
} else if (Rgba const *other = colors.registerColor(color); other) { } else if (Rgba const *other = colors.registerColor(color); other) {
std::tuple conflicting{color.toCSS(), other->toCSS()}; std::tuple conflicting{color.toCSS(), other->toCSS()};
// Do not report combinations twice // Do not report combinations twice
if (std::find(conflicts.begin(), conflicts.end(), conflicting) == conflicts.end()) { if (std::find(RANGE(conflicts), conflicting) == conflicts.end()) {
warning("Fusing colors #%08x and #%08x into Game Boy color $%04x [first seen " warning("Fusing colors #%08x and #%08x into Game Boy color $%04x [first seen "
"at x: %" PRIu32 ", y: %" PRIu32 "]", "at x: %" PRIu32 ", y: %" PRIu32 "]",
std::get<0>(conflicting), std::get<1>(conflicting), color.cgbColor(), x, std::get<0>(conflicting), std::get<1>(conflicting), color.cgbColor(), x,
@@ -597,10 +597,10 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
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];
// Find the palette... // Find the palette...
auto iter = std::find_if(palettes.begin(), palettes.end(), [&protoPal](Palette const &pal) { auto iter = std::find_if(RANGE(palettes), [&protoPal](Palette const &pal) {
// ...which contains all colors in this proto-pal // ...which contains all colors in this proto-pal
return std::all_of(protoPal.begin(), protoPal.end(), [&pal](uint16_t color) { return std::all_of(RANGE(protoPal), [&pal](uint16_t color) {
return std::find(pal.begin(), pal.end(), color) != pal.end(); return std::find(RANGE(pal), color) != pal.end();
}); });
}); });
@@ -731,7 +731,7 @@ public:
} }
// Check if we have horizontal mirroring, which scans the array forward again // Check if we have horizontal mirroring, which scans the array forward again
if (std::equal(_data.begin(), _data.end(), other._data.begin(), if (std::equal(RANGE(_data), other._data.begin(),
[](uint8_t lhs, uint8_t rhs) { return lhs == flipTable[rhs]; })) { [](uint8_t lhs, uint8_t rhs) { return lhs == flipTable[rhs]; })) {
return MatchType::HFLIP; return MatchType::HFLIP;
} }

View File

@@ -1,5 +1,7 @@
/* SPDX-License-Identifier: MIT */ /* SPDX-License-Identifier: MIT */
#include "helpers.hpp"
#include "gfx/proto_palette.hpp" #include "gfx/proto_palette.hpp"
#include <algorithm> #include <algorithm>
@@ -42,8 +44,8 @@ bool ProtoPalette::add(uint16_t color) {
ProtoPalette::ComparisonResult ProtoPalette::compare(ProtoPalette const &other) const { ProtoPalette::ComparisonResult ProtoPalette::compare(ProtoPalette const &other) const {
// This works because the sets are sorted numerically // This works because the sets are sorted numerically
assert(std::is_sorted(_colorIndices.begin(), _colorIndices.end())); assert(std::is_sorted(RANGE(_colorIndices)));
assert(std::is_sorted(other._colorIndices.begin(), other._colorIndices.end())); assert(std::is_sorted(RANGE(other._colorIndices)));
auto ours = _colorIndices.begin(), theirs = other._colorIndices.begin(); auto ours = _colorIndices.begin(), theirs = other._colorIndices.begin();
bool weBigger = true, theyBigger = true; bool weBigger = true, theyBigger = true;
@@ -67,7 +69,7 @@ ProtoPalette::ComparisonResult ProtoPalette::compare(ProtoPalette const &other)
} }
size_t ProtoPalette::size() const { size_t ProtoPalette::size() const {
return std::distance(begin(), end()); return std::distance(RANGE(*this));
} }
bool ProtoPalette::empty() const { bool ProtoPalette::empty() const {
@@ -78,5 +80,5 @@ auto ProtoPalette::begin() const -> decltype(_colorIndices)::const_iterator {
return _colorIndices.begin(); return _colorIndices.begin();
} }
auto ProtoPalette::end() const -> decltype(_colorIndices)::const_iterator { auto ProtoPalette::end() const -> decltype(_colorIndices)::const_iterator {
return std::find(_colorIndices.begin(), _colorIndices.end(), UINT16_MAX); return std::find(RANGE(_colorIndices), UINT16_MAX);
} }

View File

@@ -281,17 +281,13 @@ try_again: // Can't use a `do {} while(0)` loop, otherwise compilers (wrongly) t
} }
for (SectionType type : EnumSeq(SECTTYPE_INVALID)) { for (SectionType type : EnumSeq(SECTTYPE_INVALID)) {
if (std::equal(ident.begin(), ident.end(), if (std::equal(RANGE(ident), RANGE(sectionTypeInfo[type].name), strUpperCmp)) {
sectionTypeInfo[type].name.begin(), sectionTypeInfo[type].name.end(),
strUpperCmp)) {
return yy::parser::make_section_type(type); return yy::parser::make_section_type(type);
} }
} }
for (Keyword const &keyword : keywords) { for (Keyword const &keyword : keywords) {
if (std::equal(ident.begin(), ident.end(), if (std::equal(RANGE(ident), RANGE(keyword.name), strUpperCmp)) {
keyword.name.begin(), keyword.name.end(),
strUpperCmp)) {
return keyword.tokenGen(); return keyword.tokenGen();
} }
} }