]>
git.proxmox.com Git - ceph.git/blob - ceph/src/include/interval_set.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
16 #ifndef CEPH_INTERVAL_SET_H
17 #define CEPH_INTERVAL_SET_H
28 * This class is written to work with a variety of map-like containers,
29 * *include* ones that invalidate iterators when they are modified (e.g.,
30 * flat_map and btree_map).
33 template<typename T
, typename Map
= std::map
<T
,T
>>
36 using value_type
= typename
Map::value_type
;
37 using offset_type
= T
;
38 using length_type
= T
;
39 using reference
= value_type
&;
40 using const_reference
= const value_type
&;
41 using size_type
= typename
Map::size_type
;
45 class iterator
: public std::iterator
<std::forward_iterator_tag
, T
>
48 explicit iterator(typename
Map::iterator iter
)
52 // For the copy constructor and assignment operator, the compiler-generated functions, which
53 // perform simple bitwise copying, should be fine.
55 bool operator==(const iterator
& rhs
) const {
56 return (_iter
== rhs
._iter
);
59 bool operator!=(const iterator
& rhs
) const {
60 return (_iter
!= rhs
._iter
);
63 // Dereference this iterator to get a pair.
64 reference
operator*() const {
68 // Return the interval start.
69 offset_type
get_start() const {
73 // Return the interval length.
74 length_type
get_len() const {
78 offset_type
get_end() const {
79 return _iter
->first
+ _iter
->second
;
82 // Set the interval length.
83 void set_len(const length_type
& len
) {
88 iterator
& operator++()
95 iterator
operator++(int)
102 friend class interval_set::const_iterator
;
105 typename
Map::iterator _iter
;
106 friend class interval_set
;
109 class const_iterator
: public std::iterator
<std::forward_iterator_tag
, T
>
112 explicit const_iterator(typename
Map::const_iterator iter
)
116 const_iterator(const iterator
&i
)
120 // For the copy constructor and assignment operator, the compiler-generated functions, which
121 // perform simple bitwise copying, should be fine.
123 bool operator==(const const_iterator
& rhs
) const {
124 return (_iter
== rhs
._iter
);
127 bool operator!=(const const_iterator
& rhs
) const {
128 return (_iter
!= rhs
._iter
);
131 // Dereference this iterator to get a pair.
132 const_reference
operator*() const {
136 // Return the interval start.
137 offset_type
get_start() const {
140 offset_type
get_end() const {
141 return _iter
->first
+ _iter
->second
;
144 // Return the interval length.
145 length_type
get_len() const {
146 return _iter
->second
;
150 const_iterator
& operator++()
157 const_iterator
operator++(int)
159 const_iterator
prev(_iter
);
165 typename
Map::const_iterator _iter
;
168 interval_set() = default;
169 interval_set(Map
&& other
) {
171 for (const auto& p
: m
) {
176 size_type
num_intervals() const
182 return iterator(m
.begin());
185 iterator
lower_bound(T start
) {
186 return iterator(find_inc_m(start
));
190 return iterator(m
.end());
193 const_iterator
begin() const {
194 return const_iterator(m
.begin());
197 const_iterator
lower_bound(T start
) const {
198 return const_iterator(find_inc(start
));
201 const_iterator
end() const {
202 return const_iterator(m
.end());
207 auto find_inc(T start
) const {
208 auto p
= m
.lower_bound(start
); // p->first >= start
209 if (p
!= m
.begin() &&
210 (p
== m
.end() || p
->first
> start
)) {
211 --p
; // might overlap?
212 if (p
->first
+ p
->second
<= start
)
218 auto find_inc_m(T start
) {
219 auto p
= m
.lower_bound(start
);
220 if (p
!= m
.begin() &&
221 (p
== m
.end() || p
->first
> start
)) {
222 --p
; // might overlap?
223 if (p
->first
+ p
->second
<= start
)
229 auto find_adj(T start
) const {
230 auto p
= m
.lower_bound(start
);
231 if (p
!= m
.begin() &&
232 (p
== m
.end() || p
->first
> start
)) {
234 if (p
->first
+ p
->second
< start
)
240 auto find_adj_m(T start
) {
241 auto p
= m
.lower_bound(start
);
242 if (p
!= m
.begin() &&
243 (p
== m
.end() || p
->first
> start
)) {
245 if (p
->first
+ p
->second
< start
)
251 void intersection_size_asym(const interval_set
&s
, const interval_set
&l
) {
252 auto ps
= s
.m
.begin();
253 ceph_assert(ps
!= s
.m
.end());
254 auto offset
= ps
->first
;
261 auto pl
= l
.find_inc(offset
);
264 while (ps
!= s
.m
.end() && ps
->first
+ ps
->second
<= pl
->first
)
268 offset
= pl
->first
+ pl
->second
;
269 if (offset
<= ps
->first
) {
276 mi
= m
.insert(mi
, *ps
);
280 } while (ps
!= s
.m
.end() && pl
!= l
.m
.end() && *ps
== *pl
);
287 auto start
= std::max
<T
>(ps
->first
, pl
->first
);
288 auto en
= std::min
<T
>(ps
->first
+ ps
->second
, offset
);
289 ceph_assert(en
> start
);
290 mi
= m
.emplace_hint(mi
, start
, en
- start
);
292 if (ps
->first
+ ps
->second
<= offset
) {
301 bool subset_size_sym(const interval_set
&b
) const {
302 auto pa
= m
.begin(), pb
= b
.m
.begin();
303 const auto a_end
= m
.end(), b_end
= b
.m
.end();
305 while (pa
!= a_end
&& pb
!= b_end
) {
306 while (pb
->first
+ pb
->second
<= pa
->first
) {
316 } while (pa
!= a_end
&& pb
!= b_end
&& *pa
== *pb
);
320 // interval begins before other
321 if (pa
->first
< pb
->first
)
323 // interval is longer than other
324 if (pa
->first
+ pa
->second
> pb
->first
+ pb
->second
)
334 bool operator==(const interval_set
& other
) const {
335 return _size
== other
._size
&& m
== other
.m
;
338 uint64_t size() const {
342 void bound_encode(size_t& p
) const {
343 denc_traits
<Map
>::bound_encode(m
, p
);
345 void encode(ceph::buffer::list::contiguous_appender
& p
) const {
348 void decode(ceph::buffer::ptr::const_iterator
& p
) {
351 for (const auto& p
: m
) {
355 void decode(ceph::buffer::list::iterator
& p
) {
358 for (const auto& p
: m
) {
363 void encode_nohead(ceph::buffer::list::contiguous_appender
& p
) const {
364 denc_traits
<Map
>::encode_nohead(m
, p
);
366 void decode_nohead(int n
, ceph::buffer::ptr::const_iterator
& p
) {
367 denc_traits
<Map
>::decode_nohead(n
, m
, p
);
369 for (const auto& p
: m
) {
379 bool contains(T i
, T
*pstart
=0, T
*plen
=0) const {
380 auto p
= find_inc(i
);
381 if (p
== m
.end()) return false;
382 if (p
->first
> i
) return false;
383 if (p
->first
+p
->second
<= i
) return false;
384 ceph_assert(p
->first
<= i
&& p
->first
+p
->second
> i
);
391 bool contains(T start
, T len
) const {
392 auto p
= find_inc(start
);
393 if (p
== m
.end()) return false;
394 if (p
->first
> start
) return false;
395 if (p
->first
+p
->second
<= start
) return false;
396 ceph_assert(p
->first
<= start
&& p
->first
+p
->second
> start
);
397 if (p
->first
+p
->second
< start
+len
) return false;
400 bool intersects(T start
, T len
) const {
402 a
.insert(start
, len
);
404 i
.intersection_of( *this, a
);
405 if (i
.empty()) return false;
409 // outer range of set
413 offset_type
range_start() const {
414 ceph_assert(!empty());
418 offset_type
range_end() const {
419 ceph_assert(!empty());
422 return p
->first
+p
->second
;
425 // interval start after p (where p not in set)
426 bool starts_after(T i
) const {
427 ceph_assert(!contains(i
));
428 auto p
= find_inc(i
);
429 if (p
== m
.end()) return false;
432 offset_type
start_after(T i
) const {
433 ceph_assert(!contains(i
));
434 auto p
= find_inc(i
);
438 // interval end that contains start
439 offset_type
end_after(T start
) const {
440 ceph_assert(contains(start
));
441 auto p
= find_inc(start
);
442 return p
->first
+p
->second
;
449 void insert(T start
, T len
, T
*pstart
=0, T
*plen
=0) {
450 //cout << "insert " << start << "~" << len << endl;
451 ceph_assert(len
> 0);
453 auto p
= find_adj_m(start
);
455 m
[start
] = len
; // new interval
461 if (p
->first
< start
) {
463 if (p
->first
+ p
->second
!= start
) {
464 //cout << "p is " << p->first << "~" << p->second << ", start is " << start << ", len is " << len << endl;
468 p
->second
+= len
; // append to end
475 start
+len
== n
->first
) { // combine with next, too!
476 p
->second
+= n
->second
;
485 if (start
+len
== p
->first
) {
489 *plen
= len
+ p
->second
;
490 T psecond
= p
->second
;
492 m
[start
] = len
+ psecond
; // append to front
494 ceph_assert(p
->first
> start
+len
);
499 m
[start
] = len
; // new interval
505 void swap(interval_set
& other
) {
507 std::swap(_size
, other
._size
);
510 void erase(iterator
&i
) {
511 _size
-= i
.get_len();
512 ceph_assert(_size
>= 0);
520 void erase(T start
, T len
,
521 std::function
<bool(T
, T
)> claim
= {}) {
522 auto p
= find_inc_m(start
);
525 ceph_assert(_size
>= 0);
527 ceph_assert(p
!= m
.end());
528 ceph_assert(p
->first
<= start
);
530 T before
= start
- p
->first
;
531 ceph_assert(p
->second
>= before
+len
);
532 T after
= p
->second
- before
- len
;
534 if (claim
&& claim(p
->first
, before
)) {
538 p
->second
= before
; // shorten bit before
544 if (claim
&& claim(start
+ len
, after
)) {
547 m
[start
+ len
] = after
;
552 void subtract(const interval_set
&a
) {
553 for (const auto& [start
, len
] : a
.m
) {
558 void insert(const interval_set
&a
) {
559 for (const auto& [start
, len
] : a
.m
) {
565 void intersection_of(const interval_set
&a
, const interval_set
&b
) {
566 ceph_assert(&a
!= this);
567 ceph_assert(&b
!= this);
570 const interval_set
*s
, *l
;
572 if (a
.size() < b
.size()) {
584 * Use the lower_bound algorithm for larger size ratios
585 * where it performs better, but not for smaller size
586 * ratios where sequential search performs better.
588 if (l
->size() / s
->size() >= 10) {
589 intersection_size_asym(*s
, *l
);
593 auto pa
= a
.m
.begin();
594 auto pb
= b
.m
.begin();
597 while (pa
!= a
.m
.end() && pb
!= b
.m
.end()) {
599 if (pa
->first
+ pa
->second
<= pb
->first
)
601 if (pb
->first
+ pb
->second
<= pa
->first
)
606 mi
= m
.insert(mi
, *pa
);
610 } while (pa
!= a
.m
.end() && pb
!= b
.m
.end() && *pa
== *pb
);
614 T start
= std::max(pa
->first
, pb
->first
);
615 T en
= std::min(pa
->first
+pa
->second
, pb
->first
+pb
->second
);
616 ceph_assert(en
> start
);
617 mi
= m
.emplace_hint(mi
, start
, en
- start
);
619 if (pa
->first
+pa
->second
> pb
->first
+pb
->second
)
625 void intersection_of(const interval_set
& b
) {
628 intersection_of(a
, b
);
631 void union_of(const interval_set
&a
, const interval_set
&b
) {
632 ceph_assert(&a
!= this);
633 ceph_assert(&b
!= this);
636 //cout << "union_of" << endl;
644 ab
.intersection_of(a
, b
);
651 void union_of(const interval_set
&b
) {
656 void union_insert(T off
, T len
) {
662 bool subset_of(const interval_set
&big
) const {
665 if (size() > big
.size())
667 if (range_end() > big
.range_end())
671 * Use the lower_bound algorithm for larger size ratios
672 * where it performs better, but not for smaller size
673 * ratios where sequential search performs better.
675 if (big
.size() / size() < 10)
676 return subset_size_sym(big
);
678 for (const auto& [start
, len
] : m
) {
679 if (!big
.contains(start
, len
)) return false;
685 * build a subset of @other, starting at or after @start, and including
686 * @len worth of values, skipping holes. e.g.,
687 * span_of([5~10,20~5], 8, 5) -> [8~2,20~3]
689 void span_of(const interval_set
&other
, T start
, T len
) {
691 auto p
= other
.find_inc(start
);
692 if (p
== other
.m
.end())
694 if (p
->first
< start
) {
695 if (p
->first
+ p
->second
< start
)
697 if (p
->first
+ p
->second
< start
+ len
) {
698 T howmuch
= p
->second
- (start
- p
->first
);
699 insert(start
, howmuch
);
707 while (p
!= other
.m
.end() && len
> 0) {
708 if (p
->second
< len
) {
709 insert(p
->first
, p
->second
);
713 insert(p
->first
, len
);
720 * Move contents of m into another Map. Use that instead of
721 * encoding interval_set into bufferlist then decoding it back into Map.
730 Map m
; // map start -> len
733 // declare traits explicitly because (1) it's templatized, and (2) we
734 // want to include _nohead variants.
735 template<typename T
, typename Map
>
736 struct denc_traits
<interval_set
<T
,Map
>> {
737 static constexpr bool supported
= true;
738 static constexpr bool bounded
= false;
739 static constexpr bool featured
= false;
740 static constexpr bool need_contiguous
= denc_traits
<T
,Map
>::need_contiguous
;
741 static void bound_encode(const interval_set
<T
,Map
>& v
, size_t& p
) {
744 static void encode(const interval_set
<T
,Map
>& v
,
745 ceph::buffer::list::contiguous_appender
& p
) {
748 static void decode(interval_set
<T
,Map
>& v
, ceph::buffer::ptr::const_iterator
& p
) {
751 template<typename U
=T
>
752 static typename
std::enable_if
<sizeof(U
) && !need_contiguous
>::type
753 decode(interval_set
<T
,Map
>& v
, ceph::buffer::list::iterator
& p
) {
756 static void encode_nohead(const interval_set
<T
,Map
>& v
,
757 ceph::buffer::list::contiguous_appender
& p
) {
760 static void decode_nohead(size_t n
, interval_set
<T
,Map
>& v
,
761 ceph::buffer::ptr::const_iterator
& p
) {
762 v
.decode_nohead(n
, p
);
767 template<class T
, typename Map
>
768 inline std::ostream
& operator<<(std::ostream
& out
, const interval_set
<T
,Map
> &s
) {
771 for (const auto& [start
, len
] : s
) {
772 if (!first
) out
<< ",";
773 out
<< start
<< "~" << len
;