From 5f8a76d4336f69a27cd06e9e4805d43e569ee44f Mon Sep 17 00:00:00 2001 From: Alex Ainscow Date: Wed, 8 Jan 2025 22:04:56 +0000 Subject: [PATCH] interval_map: Add interfaces for getting start/end and lower ranges. These new utilities add the ability to: get_lower_range: Return the lowest interval iterator which covers the specified range get_start_off(): return the first offset in the interval. get_end_off(): Return the end offset of the last interval contains(): Return true if specified is entirely contained within the interval map. Signed-off-by: Alex Ainscow --- src/common/interval_map.h | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/common/interval_map.h b/src/common/interval_map.h index 65a89e211f2..e57fac539a9 100644 --- a/src/common/interval_map.h +++ b/src/common/interval_map.h @@ -61,6 +61,16 @@ class interval_map { auto lst = m.lower_bound(off + len); return std::make_pair(fst, lst); } + cmapiter get_range_fst(K off) const { + // fst is first iterator with end after off (may be end) + auto fst = m.upper_bound(off); + if (fst != m.begin()) + --fst; + if (fst != m.end() && off >= (fst->first + fst->second.first)) + ++fst; + + return fst; + } void try_merge(mapiter niter) { if (niter != m.begin()) { auto prev = niter; @@ -258,6 +268,32 @@ public: auto rng = get_range(off, len); return std::make_pair(const_iterator(rng.first), const_iterator(rng.second)); } + + const_iterator get_lower_range( + K off, + K len) const { + return const_iterator(get_range_fst(off, len)); + } + K get_start_off() const + { + auto i = m.begin(); + ceph_assert(i != m.end()); + return i->first; + } + K get_end_off() const + { + auto i = m.rbegin(); + ceph_assert(i != m.rend()); + return i->first + i->second.first; + } + bool contains(K off, K len) const { + auto it = get_range_fst(off, len); + if (it == m.end()) return false; + + K _off = it->first; + K _len = it->second.first; + return _off <= off && _off + _len >= off + len; + } unsigned ext_count() const { return m.size(); } -- 2.39.5