diff --git a/modules/juce_core/containers/juce_SparseSet.h b/modules/juce_core/containers/juce_SparseSet.h index c7b2a5310d2d..ed08ffa37b42 100644 --- a/modules/juce_core/containers/juce_SparseSet.h +++ b/modules/juce_core/containers/juce_SparseSet.h @@ -277,4 +277,69 @@ class SparseSet } }; +//============================================================================== +/** Iterator for a SparseSet. + You shouldn't ever need to use this class directly - it's used internally by begin() + and end() to allow range-based-for loops on a SparseSet. + */ +template +struct SparseSetIterator +{ + SparseSetIterator (const SparseSet& s, bool isEnd) + : set (s) + { + if (isEnd) + rangeIndex = set.getRanges().size(); + } + + SparseSetIterator& operator++() + { + valueIndex++; + if (valueIndex == set.getRanges()[rangeIndex].getLength()) + { + rangeIndex++; + valueIndex = 0; + } + return *this; + } + + bool operator== (const SparseSetIterator& other) const + { + return rangeIndex == other.rangeIndex && valueIndex == other.valueIndex; + } + + bool operator!= (const SparseSetIterator& other) const + { + return ! (*this == other); + } + + Type operator*() const + { + return set.getRanges()[rangeIndex].getStart() + valueIndex; + } + + using difference_type = std::ptrdiff_t; + using value_type = Type; + using reference = Type&; + using pointer = Type*; + using iterator_category = std::forward_iterator_tag; + +private: + const SparseSet& set; + int rangeIndex = 0; + int valueIndex = 0; +}; + +template +SparseSetIterator begin (const SparseSet& ss) +{ + return SparseSetIterator (ss, false); +} + +template +SparseSetIterator end (const SparseSet& ss) +{ + return SparseSetIterator (ss, true); +} + } // namespace juce