Sync Changelog entries between branches
[qBittorrent.git] / src / base / indexrange.h
blob0960067a2b6907c2f50c5ec79ee069dd990b8599
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Eugene Shalygin
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.
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.
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.
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.
29 #pragma once
31 #include <QtGlobal>
33 // Interval is defined via [first;last]
34 template <typename Index>
35 class IndexInterval
37 public:
38 using IndexType = Index;
40 constexpr IndexInterval(const IndexType first, const IndexType last)
41 : m_first {first}
42 , m_last {last}
44 Q_ASSERT(first <= last);
47 constexpr IndexType first() const
49 return m_first;
52 constexpr IndexType last() const
54 return m_last;
57 private:
58 IndexType m_first;
59 IndexType m_last;
62 template <typename T>
63 constexpr IndexInterval<T> makeInterval(const T first, const T last)
65 return {first, last};
68 // range is defined via first index and size
69 template <typename Index, typename IndexDiff = Index>
70 class IndexRange
72 public:
73 using IndexType = Index;
74 using IndexDiffType = IndexDiff;
76 class Iterator
78 public:
79 explicit constexpr Iterator(const IndexType index)
80 : m_index {index}
84 constexpr Iterator(const Iterator &) = default;
86 constexpr IndexType operator*() const
88 return m_index;
91 constexpr Iterator &operator++()
93 ++m_index;
94 return *this;
97 constexpr Iterator operator++(int)
99 const Iterator iter {*this};
100 ++(*this);
101 return iter;
104 constexpr bool operator==(const Iterator &other) const
106 return (*(*this) == *other);
109 constexpr bool operator!=(const Iterator &other) const
111 return !(*this == other);
114 private:
115 IndexType m_index;
118 constexpr IndexRange()
119 : m_first {0}
120 , m_size {0}
124 constexpr IndexRange(const IndexType first, const IndexDiffType size)
125 : m_first {first}
126 , m_size {size}
130 constexpr IndexRange(const IndexInterval<IndexType> &interval)
131 : m_first {interval.first()}
132 , m_size {interval.last() - interval.first() + 1}
136 constexpr Iterator begin() const
138 return Iterator {m_first};
141 constexpr Iterator end() const
143 return Iterator {m_first + m_size};
146 constexpr IndexDiffType size() const
148 return m_size;
151 constexpr IndexType first() const
153 return m_first;
156 constexpr IndexType last() const
158 return (m_first + m_size - 1);
161 constexpr bool isEmpty() const
163 return (m_size == 0);
166 private:
167 IndexType m_first;
168 IndexDiffType m_size;