]> git.proxmox.com Git - ceph.git/blob - ceph/src/include/interval_set.h
update sources to v12.1.0
[ceph.git] / 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
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7 *
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.
12 *
13 */
14
15
16 #ifndef CEPH_INTERVAL_SET_H
17 #define CEPH_INTERVAL_SET_H
18
19 #include <iterator>
20 #include <map>
21 #include <ostream>
22
23 #include "encoding.h"
24
25 #ifndef MIN
26 # define MIN(a,b) ((a)<=(b) ? (a):(b))
27 #endif
28 #ifndef MAX
29 # define MAX(a,b) ((a)>=(b) ? (a):(b))
30 #endif
31
32
33 template<typename T>
34 class interval_set {
35 public:
36
37 class const_iterator;
38
39 class iterator : public std::iterator <std::forward_iterator_tag, T>
40 {
41 public:
42 explicit iterator(typename std::map<T,T>::iterator iter)
43 : _iter(iter)
44 { }
45
46 // For the copy constructor and assignment operator, the compiler-generated functions, which
47 // perform simple bitwise copying, should be fine.
48
49 bool operator==(const iterator& rhs) const {
50 return (_iter == rhs._iter);
51 }
52
53 bool operator!=(const iterator& rhs) const {
54 return (_iter != rhs._iter);
55 }
56
57 // Dereference this iterator to get a pair.
58 std::pair < T, T > &operator*() {
59 return *_iter;
60 }
61
62 // Return the interval start.
63 T get_start() const {
64 return _iter->first;
65 }
66
67 // Return the interval length.
68 T get_len() const {
69 return _iter->second;
70 }
71
72 // Set the interval length.
73 void set_len(T len) {
74 _iter->second = len;
75 }
76
77 // Preincrement
78 iterator &operator++()
79 {
80 ++_iter;
81 return *this;
82 }
83
84 // Postincrement
85 iterator operator++(int)
86 {
87 iterator prev(_iter);
88 ++_iter;
89 return prev;
90 }
91
92 friend class interval_set<T>::const_iterator;
93
94 protected:
95 typename std::map<T,T>::iterator _iter;
96 friend class interval_set<T>;
97 };
98
99 class const_iterator : public std::iterator <std::forward_iterator_tag, T>
100 {
101 public:
102 explicit const_iterator(typename std::map<T,T>::const_iterator iter)
103 : _iter(iter)
104 { }
105
106 const_iterator(const iterator &i)
107 : _iter(i._iter)
108 { }
109
110 // For the copy constructor and assignment operator, the compiler-generated functions, which
111 // perform simple bitwise copying, should be fine.
112
113 bool operator==(const const_iterator& rhs) const {
114 return (_iter == rhs._iter);
115 }
116
117 bool operator!=(const const_iterator& rhs) const {
118 return (_iter != rhs._iter);
119 }
120
121 // Dereference this iterator to get a pair.
122 std::pair < T, T > operator*() const {
123 return *_iter;
124 }
125
126 // Return the interval start.
127 T get_start() const {
128 return _iter->first;
129 }
130
131 // Return the interval length.
132 T get_len() const {
133 return _iter->second;
134 }
135
136 // Preincrement
137 const_iterator &operator++()
138 {
139 ++_iter;
140 return *this;
141 }
142
143 // Postincrement
144 const_iterator operator++(int)
145 {
146 const_iterator prev(_iter);
147 ++_iter;
148 return prev;
149 }
150
151 protected:
152 typename std::map<T,T>::const_iterator _iter;
153 };
154
155 interval_set() : _size(0) {}
156 interval_set(std::map<T,T>& other) {
157 m.swap(other);
158 _size = 0;
159 for (auto& i : m) {
160 _size += i.second;
161 }
162 }
163
164 int num_intervals() const
165 {
166 return m.size();
167 }
168
169 typename interval_set<T>::iterator begin() {
170 return typename interval_set<T>::iterator(m.begin());
171 }
172
173 typename interval_set<T>::iterator lower_bound(T start) {
174 return typename interval_set<T>::iterator(find_inc_m(start));
175 }
176
177 typename interval_set<T>::iterator end() {
178 return typename interval_set<T>::iterator(m.end());
179 }
180
181 typename interval_set<T>::const_iterator begin() const {
182 return typename interval_set<T>::const_iterator(m.begin());
183 }
184
185 typename interval_set<T>::const_iterator lower_bound(T start) const {
186 return typename interval_set<T>::const_iterator(find_inc(start));
187 }
188
189 typename interval_set<T>::const_iterator end() const {
190 return typename interval_set<T>::const_iterator(m.end());
191 }
192
193 // helpers
194 private:
195 typename std::map<T,T>::const_iterator find_inc(T start) const {
196 typename std::map<T,T>::const_iterator p = m.lower_bound(start); // p->first >= start
197 if (p != m.begin() &&
198 (p == m.end() || p->first > start)) {
199 p--; // might overlap?
200 if (p->first + p->second <= start)
201 p++; // it doesn't.
202 }
203 return p;
204 }
205
206 typename std::map<T,T>::iterator find_inc_m(T start) {
207 typename std::map<T,T>::iterator p = m.lower_bound(start);
208 if (p != m.begin() &&
209 (p == m.end() || p->first > start)) {
210 p--; // might overlap?
211 if (p->first + p->second <= start)
212 p++; // it doesn't.
213 }
214 return p;
215 }
216
217 typename std::map<T,T>::const_iterator find_adj(T start) const {
218 typename std::map<T,T>::const_iterator p = m.lower_bound(start);
219 if (p != m.begin() &&
220 (p == m.end() || p->first > start)) {
221 p--; // might touch?
222 if (p->first + p->second < start)
223 p++; // it doesn't.
224 }
225 return p;
226 }
227
228 typename std::map<T,T>::iterator find_adj_m(T start) {
229 typename std::map<T,T>::iterator p = m.lower_bound(start);
230 if (p != m.begin() &&
231 (p == m.end() || p->first > start)) {
232 p--; // might touch?
233 if (p->first + p->second < start)
234 p++; // it doesn't.
235 }
236 return p;
237 }
238
239 public:
240 bool operator==(const interval_set& other) const {
241 return _size == other._size && m == other.m;
242 }
243
244 int64_t size() const {
245 return _size;
246 }
247
248 void bound_encode(size_t& p) const {
249 denc_traits<std::map<T,T>>::bound_encode(m, p);
250 }
251 void encode(bufferlist::contiguous_appender& p) const {
252 denc(m, p);
253 }
254 void decode(bufferptr::iterator& p) {
255 denc(m, p);
256 _size = 0;
257 for (const auto& i : m) {
258 _size += i.second;
259 }
260 }
261 void decode(bufferlist::iterator& p) {
262 denc(m, p);
263 _size = 0;
264 for (const auto& i : m) {
265 _size += i.second;
266 }
267 }
268
269 void encode_nohead(bufferlist::contiguous_appender& p) const {
270 denc_traits<std::map<T,T>>::encode_nohead(m, p);
271 }
272 void decode_nohead(int n, bufferptr::iterator& p) {
273 denc_traits<std::map<T,T>>::decode_nohead(n, m, p);
274 _size = 0;
275 for (const auto& i : m) {
276 _size += i.second;
277 }
278 }
279
280 void clear() {
281 m.clear();
282 _size = 0;
283 }
284
285 bool contains(T i, T *pstart=0, T *plen=0) const {
286 typename std::map<T,T>::const_iterator p = find_inc(i);
287 if (p == m.end()) return false;
288 if (p->first > i) return false;
289 if (p->first+p->second <= i) return false;
290 assert(p->first <= i && p->first+p->second > i);
291 if (pstart)
292 *pstart = p->first;
293 if (plen)
294 *plen = p->second;
295 return true;
296 }
297 bool contains(T start, T len) const {
298 typename std::map<T,T>::const_iterator p = find_inc(start);
299 if (p == m.end()) return false;
300 if (p->first > start) return false;
301 if (p->first+p->second <= start) return false;
302 assert(p->first <= start && p->first+p->second > start);
303 if (p->first+p->second < start+len) return false;
304 return true;
305 }
306 bool intersects(T start, T len) const {
307 interval_set a;
308 a.insert(start, len);
309 interval_set i;
310 i.intersection_of( *this, a );
311 if (i.empty()) return false;
312 return true;
313 }
314
315 // outer range of set
316 bool empty() const {
317 return m.empty();
318 }
319 T range_start() const {
320 assert(!empty());
321 typename std::map<T,T>::const_iterator p = m.begin();
322 return p->first;
323 }
324 T range_end() const {
325 assert(!empty());
326 typename std::map<T,T>::const_iterator p = m.end();
327 p--;
328 return p->first+p->second;
329 }
330
331 // interval start after p (where p not in set)
332 bool starts_after(T i) const {
333 assert(!contains(i));
334 typename std::map<T,T>::const_iterator p = find_inc(i);
335 if (p == m.end()) return false;
336 return true;
337 }
338 T start_after(T i) const {
339 assert(!contains(i));
340 typename std::map<T,T>::const_iterator p = find_inc(i);
341 return p->first;
342 }
343
344 // interval end that contains start
345 T end_after(T start) const {
346 assert(contains(start));
347 typename std::map<T,T>::const_iterator p = find_inc(start);
348 return p->first+p->second;
349 }
350
351 void insert(T val) {
352 insert(val, 1);
353 }
354
355 void insert(T start, T len, T *pstart=0, T *plen=0) {
356 //cout << "insert " << start << "~" << len << endl;
357 assert(len > 0);
358 _size += len;
359 typename std::map<T,T>::iterator p = find_adj_m(start);
360 if (p == m.end()) {
361 m[start] = len; // new interval
362 if (pstart)
363 *pstart = start;
364 if (plen)
365 *plen = len;
366 } else {
367 if (p->first < start) {
368
369 if (p->first + p->second != start) {
370 //cout << "p is " << p->first << "~" << p->second << ", start is " << start << ", len is " << len << endl;
371 ceph_abort();
372 }
373
374 p->second += len; // append to end
375
376 typename std::map<T,T>::iterator n = p;
377 n++;
378 if (n != m.end() &&
379 start+len == n->first) { // combine with next, too!
380 p->second += n->second;
381 m.erase(n);
382 }
383 if (pstart)
384 *pstart = p->first;
385 if (plen)
386 *plen = p->second;
387 } else {
388 if (start+len == p->first) {
389 m[start] = len + p->second; // append to front
390 if (pstart)
391 *pstart = start;
392 if (plen)
393 *plen = len + p->second;
394 m.erase(p);
395 } else {
396 assert(p->first > start+len);
397 m[start] = len; // new interval
398 if (pstart)
399 *pstart = start;
400 if (plen)
401 *plen = len;
402 }
403 }
404 }
405 }
406
407 void swap(interval_set<T>& other) {
408 m.swap(other.m);
409 std::swap(_size, other._size);
410 }
411
412 void erase(iterator &i) {
413 _size -= i.get_len();
414 assert(_size >= 0);
415 m.erase(i._iter);
416 }
417
418 void erase(T val) {
419 erase(val, 1);
420 }
421
422 void erase(T start, T len) {
423 typename std::map<T,T>::iterator p = find_inc_m(start);
424
425 _size -= len;
426 assert(_size >= 0);
427
428 assert(p != m.end());
429 assert(p->first <= start);
430
431 T before = start - p->first;
432 assert(p->second >= before+len);
433 T after = p->second - before - len;
434
435 if (before)
436 p->second = before; // shorten bit before
437 else
438 m.erase(p);
439 if (after)
440 m[start+len] = after;
441 }
442
443
444 void subtract(const interval_set &a) {
445 for (typename std::map<T,T>::const_iterator p = a.m.begin();
446 p != a.m.end();
447 p++)
448 erase(p->first, p->second);
449 }
450
451 void insert(const interval_set &a) {
452 for (typename std::map<T,T>::const_iterator p = a.m.begin();
453 p != a.m.end();
454 p++)
455 insert(p->first, p->second);
456 }
457
458
459 void intersection_of(const interval_set &a, const interval_set &b) {
460 assert(&a != this);
461 assert(&b != this);
462 clear();
463
464 typename std::map<T,T>::const_iterator pa = a.m.begin();
465 typename std::map<T,T>::const_iterator pb = b.m.begin();
466
467 while (pa != a.m.end() && pb != b.m.end()) {
468 // passing?
469 if (pa->first + pa->second <= pb->first)
470 { pa++; continue; }
471 if (pb->first + pb->second <= pa->first)
472 { pb++; continue; }
473 T start = MAX(pa->first, pb->first);
474 T en = MIN(pa->first+pa->second, pb->first+pb->second);
475 assert(en > start);
476 insert(start, en-start);
477 if (pa->first+pa->second > pb->first+pb->second)
478 pb++;
479 else
480 pa++;
481 }
482 }
483 void intersection_of(const interval_set& b) {
484 interval_set a;
485 swap(a);
486 intersection_of(a, b);
487 }
488
489 void union_of(const interval_set &a, const interval_set &b) {
490 assert(&a != this);
491 assert(&b != this);
492 clear();
493
494 //cout << "union_of" << endl;
495
496 // a
497 m = a.m;
498 _size = a._size;
499
500 // - (a*b)
501 interval_set ab;
502 ab.intersection_of(a, b);
503 subtract(ab);
504
505 // + b
506 insert(b);
507 return;
508 }
509 void union_of(const interval_set &b) {
510 interval_set a;
511 swap(a);
512 union_of(a, b);
513 }
514 void union_insert(T off, T len) {
515 interval_set a;
516 a.insert(off, len);
517 union_of(a);
518 }
519
520 bool subset_of(const interval_set &big) const {
521 for (typename std::map<T,T>::const_iterator i = m.begin();
522 i != m.end();
523 i++)
524 if (!big.contains(i->first, i->second)) return false;
525 return true;
526 }
527
528 /*
529 * build a subset of @other, starting at or after @start, and including
530 * @len worth of values, skipping holes. e.g.,
531 * span_of([5~10,20~5], 8, 5) -> [8~2,20~3]
532 */
533 void span_of(const interval_set &other, T start, T len) {
534 clear();
535 typename std::map<T,T>::const_iterator p = other.find_inc(start);
536 if (p == other.m.end())
537 return;
538 if (p->first < start) {
539 if (p->first + p->second < start)
540 return;
541 if (p->first + p->second < start + len) {
542 T howmuch = p->second - (start - p->first);
543 insert(start, howmuch);
544 len -= howmuch;
545 p++;
546 } else {
547 insert(start, len);
548 return;
549 }
550 }
551 while (p != other.m.end() && len > 0) {
552 if (p->second < len) {
553 insert(p->first, p->second);
554 len -= p->second;
555 p++;
556 } else {
557 insert(p->first, len);
558 return;
559 }
560 }
561 }
562
563 /*
564 * Move contents of m into another std::map<T,T>. Use that instead of
565 * encoding interval_set into bufferlist then decoding it back into std::map.
566 */
567 void move_into(std::map<T,T>& other) {
568 other = std::move(m);
569 }
570
571 private:
572 // data
573 int64_t _size;
574 std::map<T,T> m; // map start -> len
575 };
576
577 // declare traits explicitly because (1) it's templatized, and (2) we
578 // want to include _nohead variants.
579 template<typename T>
580 struct denc_traits<interval_set<T>> {
581 static constexpr bool supported = true;
582 static constexpr bool bounded = false;
583 static constexpr bool featured = false;
584 static constexpr bool need_contiguous = denc_traits<T>::need_contiguous;
585 static void bound_encode(const interval_set<T>& v, size_t& p) {
586 v.bound_encode(p);
587 }
588 static void encode(const interval_set<T>& v,
589 bufferlist::contiguous_appender& p) {
590 v.encode(p);
591 }
592 static void decode(interval_set<T>& v, bufferptr::iterator& p) {
593 v.decode(p);
594 }
595 template<typename U=T>
596 static typename std::enable_if<sizeof(U) && !need_contiguous>::type
597 decode(interval_set<T>& v, bufferlist::iterator& p) {
598 v.decode(p);
599 }
600 static void encode_nohead(const interval_set<T>& v,
601 bufferlist::contiguous_appender& p) {
602 v.encode_nohead(p);
603 }
604 static void decode_nohead(size_t n, interval_set<T>& v,
605 bufferptr::iterator& p) {
606 v.decode_nohead(n, p);
607 }
608 };
609
610
611 template<class T>
612 inline std::ostream& operator<<(std::ostream& out, const interval_set<T> &s) {
613 out << "[";
614 const char *prequel = "";
615 for (typename interval_set<T>::const_iterator i = s.begin();
616 i != s.end();
617 ++i)
618 {
619 out << prequel << i.get_start() << "~" << i.get_len();
620 prequel = ",";
621 }
622 out << "]";
623 return out;
624 }
625
626
627 #endif