Fine tune translations loading for Chinese locales
[qBittorrent.git] / src / base / indexrange.h
blob65129f5ea94f5bdd37b98cffa10069bda3f50496
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 // 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);
110 friend constexpr bool operator!=(const Iterator &left, const Iterator &right)
112 return !(left == right);
115 private:
116 IndexType m_index;
119 constexpr IndexRange()
120 : m_first {0}
121 , m_size {0}
125 constexpr IndexRange(const IndexType first, const IndexDiffType size)
126 : m_first {first}
127 , m_size {size}
131 constexpr IndexRange(const IndexInterval<IndexType> &interval)
132 : m_first {interval.first()}
133 , m_size {interval.last() - interval.first() + 1}
137 constexpr Iterator begin() const
139 return Iterator {m_first};
142 constexpr Iterator end() const
144 return Iterator {m_first + m_size};
147 constexpr IndexDiffType size() const
149 return m_size;
152 constexpr IndexType first() const
154 return m_first;
157 constexpr IndexType last() const
159 return (m_first + m_size - 1);
162 constexpr bool isEmpty() const
164 return (m_size == 0);
167 private:
168 IndexType m_first;
169 IndexDiffType m_size;