1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Add Ranges::find and RangedValues::find

This commit is contained in:
attila 2024-11-26 10:46:00 +01:00
parent 0dc97de993
commit c69119a7b7

View file

@ -331,19 +331,6 @@ struct Ranges final
return result;
}
std::optional<size_t> 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<size_t> 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<Range<int64>>::const_iterator it) const
{
@ -671,6 +675,17 @@ public:
return getItemWithEnclosingRangeImpl (*this, i);
}
// Finds the item whose range encloses the provided value
template <typename Self>
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());