Allow computing the combined weight of any proto-palette set

This commit is contained in:
ISSOtm
2022-03-07 22:44:41 +01:00
committed by Eldred Habert
parent c98d92a4c4
commit fdfedc45a6

View File

@@ -158,13 +158,15 @@ public:
bool empty() const { return std::distance(begin(), end()) == 0; } bool empty() const { return std::distance(begin(), end()) == 0; }
private: private:
static void addUniqueColors(std::unordered_set<uint16_t> &colors, AssignedProtos const &pal) { template<typename Iter>
for (ProtoPalAttrs const &attrs : pal) { static void addUniqueColors(std::unordered_set<uint16_t> &colors, Iter iter, Iter const &end,
for (uint16_t color : (*pal._protoPals)[attrs.palIndex]) { std::vector<ProtoPalette> const &protoPals) {
colors.insert(color); for (; iter != end; ++iter) {
} ProtoPalette const &protoPal = protoPals[iter->palIndex];
colors.insert(protoPal.begin(), protoPal.end());
} }
} }
// This function should stay private because it returns a reference to a unique object
std::unordered_set<uint16_t> &uniqueColors() const { std::unordered_set<uint16_t> &uniqueColors() const {
// We check for *distinct* colors by stuffing them into a `set`; this should be // We check for *distinct* colors by stuffing them into a `set`; this should be
// faster than "back-checking" on every element (O(n²)) // faster than "back-checking" on every element (O(n²))
@@ -181,7 +183,7 @@ private:
static std::unordered_set<uint16_t> colors; static std::unordered_set<uint16_t> colors;
colors.clear(); colors.clear();
addUniqueColors(colors, *this); addUniqueColors(colors, begin(), end(), *_protoPals);
return colors; return colors;
} }
public: public:
@@ -195,7 +197,6 @@ public:
return colors.size() <= options.maxPalSize(); return colors.size() <= options.maxPalSize();
} }
public:
/** /**
* Computes the "relative size" of a proto-palette on this palette * Computes the "relative size" of a proto-palette on this palette
*/ */
@@ -217,11 +218,13 @@ public:
} }
/** /**
* Computes the "relative size" of a palette on this one * Computes the "relative size" of a set of proto-palettes on this palette
*/ */
double combinedVolume(AssignedProtos const &pal) const { template<typename Iter>
auto combinedVolume(Iter &&begin, Iter const &end,
std::vector<ProtoPalette> const &protoPals) const {
auto &colors = uniqueColors(); auto &colors = uniqueColors();
addUniqueColors(colors, pal); addUniqueColors(colors, std::forward<Iter>(begin), end, protoPals);
return colors.size(); return colors.size();
} }
}; };
@@ -271,7 +274,7 @@ static void decant(std::vector<AssignedProtos> &assignments) {
// Decant on palettes // Decant on palettes
decantOn([](AssignedProtos &to, AssignedProtos &from) { decantOn([](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) <= options.maxPalSize()) { if (to.combinedVolume(from.begin(), from.end(), protoPalettes) <= options.maxPalSize()) {
for (ProtoPalAttrs &protoPal : from) { for (ProtoPalAttrs &protoPal : from) {
to.assign(std::move(protoPal)); to.assign(std::move(protoPal));
} }