Skip to content

Commit 3ece61b

Browse files
committed
Use fewer templates in RGBGFX pal_packing.cpp
1 parent a82fd17 commit 3ece61b

1 file changed

Lines changed: 18 additions & 13 deletions

File tree

src/gfx/pal_packing.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ struct ColorSetAttrs {
4242
std::vector<bool> bannedPages;
4343

4444
explicit ColorSetAttrs(size_t index) : colorSetIndex(index) {}
45+
4546
bool isBannedFrom(size_t index) const {
4647
return index < bannedPages.size() && bannedPages[index];
4748
}
49+
4850
void banFrom(size_t index) {
4951
if (bannedPages.size() <= index) {
5052
bannedPages.resize(index + 1);
@@ -62,9 +64,8 @@ class AssignedSets {
6264
std::vector<ColorSet> const *_colorSets;
6365

6466
public:
65-
template<typename... Ts>
66-
AssignedSets(std::vector<ColorSet> const &colorSets, Ts &&...elems)
67-
: _assigned{std::forward<Ts>(elems)...}, _colorSets{&colorSets} {}
67+
AssignedSets(std::vector<ColorSet> const &colorSets, std::optional<ColorSetAttrs> &&attrs)
68+
: _assigned{attrs}, _colorSets{&colorSets} {}
6869

6970
private:
7071
template<typename Inner, template<typename> typename Constness>
@@ -119,34 +120,34 @@ class AssignedSets {
119120
std::swap(lhs._iter, rhs._iter);
120121
}
121122
};
123+
122124
public:
123125
using iterator = Iter<decltype(_assigned)::iterator, std::remove_const_t>;
124126
iterator begin() { return iterator{&_assigned, _assigned.begin()}.skipEmpty(); }
125127
iterator end() { return iterator{&_assigned, _assigned.end()}; }
128+
126129
using const_iterator = Iter<decltype(_assigned)::const_iterator, std::add_const_t>;
127130
const_iterator begin() const {
128131
return const_iterator{&_assigned, _assigned.begin()}.skipEmpty();
129132
}
130133
const_iterator end() const { return const_iterator{&_assigned, _assigned.end()}; }
131134

132-
// Assigns a new ColorSetAttrs in a free slot, assuming there is one
133-
// Args are passed to the `ColorSetAttrs`'s constructor
134-
template<typename... Ts>
135-
void assign(Ts &&...args) {
135+
void assign(ColorSetAttrs const &&attrs) {
136136
auto freeSlot =
137137
std::find_if_not(RANGE(_assigned), [](std::optional<ColorSetAttrs> const &slot) {
138138
return slot.has_value();
139139
});
140-
141-
if (freeSlot == _assigned.end()) { // We are full, use a new slot
142-
_assigned.emplace_back(std::forward<Ts>(args)...);
143-
} else { // Reuse a free slot
144-
freeSlot->emplace(std::forward<Ts>(args)...);
140+
if (freeSlot == _assigned.end()) {
141+
_assigned.emplace_back(attrs); // We are full, use a new slot
142+
} else {
143+
freeSlot->emplace(attrs); // Reuse a free slot
145144
}
146145
}
146+
147147
void remove(iterator const &iter) {
148148
iter._iter->reset(); // This time, we want to access the `optional` itself
149149
}
150+
150151
void clear() { _assigned.clear(); }
151152

152153
bool empty() const {
@@ -171,6 +172,7 @@ class AssignedSets {
171172
colors.insert(RANGE(colorSet));
172173
}
173174
}
175+
174176
// This function should stay private because it returns a reference to a unique object
175177
std::unordered_set<uint16_t> &uniqueColors() const {
176178
// We check for *distinct* colors by stuffing them into a `set`; this should be
@@ -181,9 +183,11 @@ class AssignedSets {
181183
addUniqueColors(colors, RANGE(*this), *_colorSets);
182184
return colors;
183185
}
186+
184187
public:
185188
// Returns the number of distinct colors
186189
size_t volume() const { return uniqueColors().size(); }
190+
187191
bool canFit(ColorSet const &colorSet) const {
188192
std::unordered_set<uint16_t> &colors = uniqueColors();
189193
colors.insert(RANGE(colorSet));
@@ -239,6 +243,7 @@ class AssignedSets {
239243
addUniqueColors(colors, std::forward<Iter>(begin), end, colorSets);
240244
return colors.size();
241245
}
246+
242247
// Computes the "relative size" of a set of colors on this palette
243248
template<typename Iter>
244249
size_t combinedVolume(Iter &&begin, Iter &&end) const {
@@ -302,7 +307,7 @@ static void decant(std::vector<AssignedSets> &assignments, std::vector<ColorSet>
302307
// If the entire palettes can be merged, move all of `from`'s color sets
303308
if (to.combinedVolume(RANGE(from), colorSets) <= options.maxOpaqueColors()) {
304309
for (ColorSetAttrs &attrs : from) {
305-
to.assign(attrs.colorSetIndex);
310+
to.assign(std::move(attrs));
306311
}
307312
from.clear();
308313
}

0 commit comments

Comments
 (0)