Disk ARchive 2.7.16
Full featured and portable backup and archiving tool
range.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2024 Denis Corbin
4//
5// This program is free software; you can redistribute it and/or
6// modify it under the terms of the GNU General Public License
7// as published by the Free Software Foundation; either version 2
8// of the License, or (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18//
19// to contact the author, see the AUTHOR file
20/*********************************************************************/
21
25
26
27#ifndef RANGE_HPP
28#define RANGE_HPP
29
30#include <string>
31#include <set>
32#include <list>
33
34#include "../my_config.h"
35#include "infinint.hpp"
36
37namespace libdar
38{
41
43 class range
44 {
45 public:
46 range() { clear(); };
47 range(const infinint & low, const infinint & high) { parts.push_back(segment(low, high)); };
48 range(const range & ref) = default;
49 range(range && ref) noexcept = default;
50 range & operator = (const range & ref) = default;
51 range & operator = (range && ref) noexcept = default;
52 ~range() = default;
53
54 void operator += (const range & ref);
55 range operator + (const range & ref) const { range ret = *this; ret += ref; return ret; };
56 std::string display() const;
57
59
64 void reset_read() const { read_cursor = parts.begin(); };
65
67
72 bool read_next_segment(infinint & low, infinint & high) const;
73
74 void clear() { parts.clear(); };
75
76 private:
77 class segment
78 {
79 public:
80 segment(const infinint & x_low, const infinint & x_high) { low = x_low; high = x_high; };
81
82 const infinint & get_low() const { return low; };
83 const infinint & get_high() const { return high; };
84
85 bool overlaps_with(const segment & ref) const { return !(ref < *this) && !(ref > *this); };
86 void merge_with(const segment & ref); // only possible with a segment that overlaps with the current object
87
88 // if two segment make < or > true they cannot be replaced by a single segment
89 bool operator < (const segment & ref) const { return high + 1 < ref.low; };
90 bool operator > (const segment & ref) const { return ref < *this; };
91 bool operator == (const segment & ref) const { return ref.high == high && ref.low == low; };
92 bool operator != (const segment & ref) const { return ! (*this == ref); };
93
94 // if two segment make <= or >= true they can be replaced by a single (larger) segment
95 bool operator <= (const segment & ref) const { return ref.low < low && low <= ref.high + 1 && ref.high < high; };
96 bool operator >= (const segment &ref) const { return ref <= *this; };
97 bool contains(const segment & ref) const { return low <= ref.low && ref.high <= high; };
98
99 std::string display() const;
100
101 private:
102 infinint low, high;
103 };
104
105 std::list<segment> parts;
106 mutable std::list<segment>::const_iterator read_cursor;
107
108 };
109
111
112} // end of namespace
113
114#endif
the arbitrary large positive integer class
stores a range of integers or a set of ranges
Definition: range.hpp:44
void reset_read() const
provides a way to read range contents segment by segment
Definition: range.hpp:64
bool read_next_segment(infinint &low, infinint &high) const
read the next available segment
switch module to limitint (32 ou 64 bits integers) or infinint
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47