diff --git a/modules/juce_graphics/detail/juce_Ranges.h b/modules/juce_graphics/detail/juce_Ranges.h index b27f2ac360..18edc3a00c 100644 --- a/modules/juce_graphics/detail/juce_Ranges.h +++ b/modules/juce_graphics/detail/juce_Ranges.h @@ -331,19 +331,6 @@ struct Ranges final return result; } - std::optional getIndexForEnclosingRange (int64 positionInTextRange) const - { - auto it = std::lower_bound (ranges.begin(), - ranges.end(), - positionInTextRange, - [] (auto& elem, auto& value) { return elem.getEnd() <= value; }); - - if (it != ranges.end() && it->getStart() <= positionInTextRange) - return getIndex (it); - - return std::nullopt; - } - //============================================================================== size_t size() const { @@ -381,6 +368,23 @@ struct Ranges final return ranges.cend(); } + /* Returns an iterator for the Range element which includes the provided value. */ + auto find (int64 i) const + { + const auto it = std::lower_bound (cbegin(), + cend(), + i, + [] (auto& elem, auto& value) { return elem.getEnd() <= value; }); + + return it != cend() && it->getStart() <= i ? it : cend(); + } + + std::optional getIndexForEnclosingRange (int64 positionInTextRange) const + { + const auto iter = find (positionInTextRange); + return iter != ranges.end() ? std::make_optional (getIndex (iter)) : std::nullopt; + } + private: size_t getIndex (std::vector>::const_iterator it) const { @@ -671,6 +675,17 @@ public: return getItemWithEnclosingRangeImpl (*this, i); } + // Finds the item whose range encloses the provided value + template + static auto findImpl (Self& self, int64 i) + { + return iteratorWithAdvance (self.begin(), + std::distance (self.ranges.cbegin(), self.ranges.find (i))); + } + + auto find (int64 i) { return findImpl (*this, i); } + auto find (int64 i) const { return findImpl (*this, i); } + Item getItem (size_t i) { jassert (i < values.size());