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.
33 // Interval is defined via [first;last]
34 template <typename Index
>
38 using IndexType
= Index
;
40 constexpr IndexInterval(const IndexType first
, const IndexType last
)
44 Q_ASSERT(first
<= last
);
47 constexpr IndexType
first() const
52 constexpr IndexType
last() const
63 constexpr IndexInterval
<T
> makeInterval(const T first
, const T last
)
68 // range is defined via first index and size
69 template <typename Index
, typename IndexDiff
= Index
>
73 using IndexType
= Index
;
74 using IndexDiffType
= IndexDiff
;
79 explicit constexpr Iterator(const IndexType index
)
84 constexpr Iterator(const Iterator
&) = default;
86 constexpr IndexType
operator*() const
91 constexpr Iterator
&operator++()
97 constexpr Iterator
operator++(int)
99 const Iterator iter
{*this};
104 // comparing iterators from different containers is undefined behavior in C++ standard library
105 friend constexpr bool operator==(const Iterator
&left
, const Iterator
&right
)
107 return (*left
== *right
);
114 constexpr IndexRange()
120 constexpr IndexRange(const IndexType first
, const IndexDiffType size
)
126 constexpr IndexRange(const IndexInterval
<IndexType
> &interval
)
127 : m_first
{interval
.first()}
128 , m_size
{interval
.last() - interval
.first() + 1}
132 constexpr Iterator
begin() const
134 return Iterator
{m_first
};
137 constexpr Iterator
end() const
139 return Iterator
{m_first
+ m_size
};
142 constexpr IndexDiffType
size() const
147 constexpr IndexType
first() const
152 constexpr IndexType
last() const
154 return (m_first
+ m_size
- 1);
157 constexpr bool isEmpty() const
159 return (m_size
== 0);
164 IndexDiffType m_size
;