qBittorrent
indexrange.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2016 Eugene Shalygin
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  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #pragma once
30 
31 #include <QtGlobal>
32 
33 // Interval is defined via [first;last]
34 template <typename Index>
36 {
37 public:
38  using IndexType = Index;
39 
40  constexpr IndexInterval(const IndexType first, const IndexType last)
41  : m_first {first}
42  , m_last {last}
43  {
44  Q_ASSERT(first <= last);
45  }
46 
47  constexpr IndexType first() const
48  {
49  return m_first;
50  }
51 
52  constexpr IndexType last() const
53  {
54  return m_last;
55  }
56 
57 private:
60 };
61 
62 template <typename T>
63 constexpr IndexInterval<T> makeInterval(const T first, const T last)
64 {
65  return {first, last};
66 }
67 
68 // range is defined via first index and size
69 template <typename Index, typename IndexDiff = Index>
71 {
72 public:
73  using IndexType = Index;
74  using IndexDiffType = IndexDiff;
75 
76  class Iterator
77  {
78  public:
79  explicit constexpr Iterator(const IndexType index)
80  : m_index {index}
81  {
82  }
83 
84  constexpr Iterator(const Iterator &) = default;
85 
86  constexpr IndexType operator*() const
87  {
88  return m_index;
89  }
90 
91  constexpr Iterator &operator++()
92  {
93  ++m_index;
94  return *this;
95  }
96 
97  constexpr Iterator operator++(int)
98  {
99  const Iterator iter {*this};
100  ++(*this);
101  return iter;
102  }
103 
104  constexpr bool operator==(const Iterator &other) const
105  {
106  return (*(*this) == *other);
107  }
108 
109  constexpr bool operator!=(const Iterator &other) const
110  {
111  return !(*this == other);
112  }
113 
114  private:
116  };
117 
118  constexpr IndexRange()
119  : m_first {0}
120  , m_size {0}
121  {
122  }
123 
124  constexpr IndexRange(const IndexType first, const IndexDiffType size)
125  : m_first {first}
126  , m_size {size}
127  {
128  }
129 
130  constexpr IndexRange(const IndexInterval<IndexType> &interval)
131  : m_first {interval.first()}
132  , m_size {interval.last() - interval.first() + 1}
133  {
134  }
135 
136  constexpr Iterator begin() const
137  {
138  return Iterator {m_first};
139  }
140 
141  constexpr Iterator end() const
142  {
143  return Iterator {m_first + m_size};
144  }
145 
146  constexpr IndexDiffType size() const
147  {
148  return m_size;
149  }
150 
151  constexpr IndexType first() const
152  {
153  return m_first;
154  }
155 
156  constexpr IndexType last() const
157  {
158  return (m_first + m_size - 1);
159  }
160 
161  constexpr bool isEmpty() const
162  {
163  return (m_size == 0);
164  }
165 
166 private:
169 };
Index IndexType
Definition: indexrange.h:38
constexpr IndexType last() const
Definition: indexrange.h:52
IndexType m_first
Definition: indexrange.h:58
constexpr IndexType first() const
Definition: indexrange.h:47
constexpr IndexInterval(const IndexType first, const IndexType last)
Definition: indexrange.h:40
IndexType m_last
Definition: indexrange.h:59
constexpr Iterator & operator++()
Definition: indexrange.h:91
constexpr bool operator==(const Iterator &other) const
Definition: indexrange.h:104
constexpr Iterator(const IndexType index)
Definition: indexrange.h:79
constexpr bool operator!=(const Iterator &other) const
Definition: indexrange.h:109
constexpr IndexType operator*() const
Definition: indexrange.h:86
constexpr Iterator operator++(int)
Definition: indexrange.h:97
constexpr Iterator(const Iterator &)=default
IndexType m_first
Definition: indexrange.h:167
constexpr IndexDiffType size() const
Definition: indexrange.h:146
constexpr IndexRange(const IndexInterval< IndexType > &interval)
Definition: indexrange.h:130
IndexDiff IndexDiffType
Definition: indexrange.h:74
constexpr bool isEmpty() const
Definition: indexrange.h:161
constexpr IndexRange()
Definition: indexrange.h:118
constexpr Iterator begin() const
Definition: indexrange.h:136
constexpr IndexType last() const
Definition: indexrange.h:156
Index IndexType
Definition: indexrange.h:73
IndexDiffType m_size
Definition: indexrange.h:168
constexpr Iterator end() const
Definition: indexrange.h:141
constexpr IndexType first() const
Definition: indexrange.h:151
constexpr IndexRange(const IndexType first, const IndexDiffType size)
Definition: indexrange.h:124
constexpr IndexInterval< T > makeInterval(const T first, const T last)
Definition: indexrange.h:63