From c8d22d87448f0e8c05d664e4f3d3f67dcd6cb035 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Sun, 21 Sep 2025 10:54:27 -0400 Subject: [PATCH] Refactor some iterator template code in RGBGFX pal_packing.cpp --- src/gfx/pal_packing.cpp | 94 +++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/src/gfx/pal_packing.cpp b/src/gfx/pal_packing.cpp index 859eb4c7..0f5dbe74 100644 --- a/src/gfx/pal_packing.cpp +++ b/src/gfx/pal_packing.cpp @@ -68,23 +68,27 @@ public: : _assigned{attrs}, _colorSets{&colorSets} {} private: - template typename Constness> - class Iter { + // Template class for both const and non-const iterators over the non-empty `_assigned` slots + template typename Constness> + class AssignedSetsIter { public: friend class AssignedSets; + // For `iterator_traits` - using difference_type = typename std::iterator_traits::difference_type; using value_type = ColorSetAttrs; - using pointer = Constness *; + using difference_type = ptrdiff_t; using reference = Constness &; + using pointer = Constness *; using iterator_category = std::forward_iterator_tag; private: Constness *_array = nullptr; - Inner _iter{}; + I _iter{}; - Iter(decltype(_array) array, decltype(_iter) &&iter) : _array(array), _iter(iter) {} - Iter &skipEmpty() { + AssignedSetsIter(decltype(_array) array, decltype(_iter) &&iter) + : _array(array), _iter(iter) {} + + AssignedSetsIter &skipEmpty() { while (_iter != _array->end() && !_iter->has_value()) { ++_iter; } @@ -92,17 +96,17 @@ private: } public: - Iter() = default; + AssignedSetsIter() = default; - bool operator==(Iter const &rhs) const { return _iter == rhs._iter; } + bool operator==(AssignedSetsIter const &rhs) const { return _iter == rhs._iter; } - Iter &operator++() { + AssignedSetsIter &operator++() { ++_iter; skipEmpty(); return *this; } - Iter operator++(int) { - Iter it = *this; + AssignedSetsIter operator++(int) { + AssignedSetsIter it = *this; ++(*this); return it; } @@ -115,18 +119,18 @@ private: return &(**this); // Invokes the operator above, not quite a no-op! } - friend void swap(Iter &lhs, Iter &rhs) { + friend void swap(AssignedSetsIter &lhs, AssignedSetsIter &rhs) { std::swap(lhs._array, rhs._array); std::swap(lhs._iter, rhs._iter); } }; public: - using iterator = Iter; + using iterator = AssignedSetsIter; iterator begin() { return iterator{&_assigned, _assigned.begin()}.skipEmpty(); } iterator end() { return iterator{&_assigned, _assigned.end()}; } - using const_iterator = Iter; + using const_iterator = AssignedSetsIter; const_iterator begin() const { return const_iterator{&_assigned, _assigned.begin()}.skipEmpty(); } @@ -151,20 +155,19 @@ public: void clear() { _assigned.clear(); } bool empty() const { - return std::find_if( - RANGE(_assigned), - [](std::optional const &slot) { return slot.has_value(); } - ) - == _assigned.end(); + return std::none_of(RANGE(_assigned), [](std::optional const &slot) { + return slot.has_value(); + }); } + size_t nbColorSets() const { return std::distance(RANGE(*this)); } private: - template + template static void addUniqueColors( std::unordered_set &colors, - Iter iter, - Iter const &end, + I iter, + I const &end, std::vector const &colorSets ) { for (; iter != end; ++iter) { @@ -236,19 +239,18 @@ public: } // Computes the "relative size" of a set of color sets on this palette - template - size_t combinedVolume(Iter &&begin, Iter const &end, std::vector const &colorSets) - const { + template + size_t combinedVolume(I &&begin, I const &end, std::vector const &colorSets) const { std::unordered_set &colors = uniqueColors(); - addUniqueColors(colors, std::forward(begin), end, colorSets); + addUniqueColors(colors, std::forward(begin), end, colorSets); return colors.size(); } // Computes the "relative size" of a set of colors on this palette - template - size_t combinedVolume(Iter &&begin, Iter &&end) const { + template + size_t combinedVolume(I &&begin, I &&end) const { std::unordered_set &colors = uniqueColors(); - colors.insert(std::forward(begin), std::forward(end)); + colors.insert(std::forward(begin), std::forward(end)); return colors.size(); } }; @@ -321,23 +323,23 @@ static void decant(std::vector &assignments, std::vector // We do this by adding the first available color set, and then looking for palettes with // common colors. (As an optimization, we know we can skip palettes already scanned.) std::vector processed(from.nbColorSets(), false); - for (std::vector::iterator iter; - (iter = std::find(RANGE(processed), false)) != processed.end();) { + for (std::vector::iterator wasProcessed; + (wasProcessed = std::find(RANGE(processed), false)) != processed.end();) { auto attrs = from.begin(); - std::advance(attrs, iter - processed.begin()); + std::advance(attrs, wasProcessed - processed.begin()); std::unordered_set colors(RANGE(colorSets[attrs->colorSetIndex])); - std::vector members = {static_cast(iter - processed.begin())}; - *iter = true; // Mark the first color set as processed + std::vector members = {static_cast(wasProcessed - processed.begin())}; + *wasProcessed = true; // Mark the first color set as processed // Build up the "component"... - for (; ++iter != processed.end(); ++attrs) { + for (; ++wasProcessed != processed.end(); ++attrs) { // If at least one color matches, add it if (ColorSet const &colorSet = colorSets[attrs->colorSetIndex]; std::find_first_of(RANGE(colors), RANGE(colorSet)) != colors.end()) { colors.insert(RANGE(colorSet)); - members.push_back(iter - processed.begin()); - *iter = true; // Mark that color set as processed + members.push_back(wasProcessed - processed.begin()); + *wasProcessed = true; // Mark that color set as processed } } @@ -362,10 +364,10 @@ static void decant(std::vector &assignments, std::vector // Decant on individual color sets decantOn([&colorSets](AssignedSets &to, AssignedSets &from) { - for (auto iter = from.begin(); iter != from.end(); ++iter) { - if (to.canFit(colorSets[iter->colorSetIndex])) { - to.assign(std::move(*iter)); - from.remove(iter); + for (auto it = from.begin(); it != from.end(); ++it) { + if (to.canFit(colorSets[it->colorSetIndex])) { + to.assign(std::move(*it)); + from.remove(it); } } }); @@ -530,10 +532,10 @@ std::pair, size_t> overloadAndRemove(std::vector c // Place back any color sets now in the queue via first-fit for (ColorSetAttrs const &attrs : overloadQueue) { ColorSet const &colorSet = colorSets[attrs.colorSetIndex]; - auto iter = std::find_if(RANGE(assignments), [&colorSet](AssignedSets const &pal) { + auto palette = std::find_if(RANGE(assignments), [&colorSet](AssignedSets const &pal) { return pal.canFit(colorSet); }); - if (iter == assignments.end()) { // No such page, create a new one + if (palette == assignments.end()) { // No such page, create a new one verbosePrint( VERB_DEBUG, "Adding new palette (%zu) for overflowing color set %zu\n", @@ -546,9 +548,9 @@ std::pair, size_t> overloadAndRemove(std::vector c VERB_DEBUG, "Assigning overflowing color set %zu to palette %zu\n", attrs.colorSetIndex, - iter - assignments.begin() + palette - assignments.begin() ); - iter->assign(std::move(attrs)); + palette->assign(std::move(attrs)); } }