src/generics/reverse.h

Sun, 10 Jun 2018 17:17:42 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 10 Jun 2018 17:17:42 +0300
changeset 1399
f52ea4078f5d
parent 1326
69a90bd2dba2
permissions
-rw-r--r--

added the disc and disc negative to the circular primitive type

1249
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
1 /*
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1326
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1249
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
1249
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
4 *
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
8 * (at your option) any later version.
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
9 *
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
13 * GNU General Public License for more details.
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
14 *
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
17 */
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
18
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
19 #pragma once
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
20 #include <type_traits>
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
21
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
22 /*
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
23 * reverse(container)
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
24 *
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
25 * Returns a container class that, when iterated, iterates over the container in reverse order.
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
26 * Can be used for const and mutable containers, arrays as well as objects.
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
27 */
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
28
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
29 template<typename T>
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
30 class ReverseIterator
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
31 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
32 public:
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
33 ReverseIterator(T iterator) :
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
34 iterator {iterator} {}
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
35
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
36 ReverseIterator& operator++()
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
37 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
38 --this->iterator;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
39 return *this;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
40 }
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
41
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
42 decltype(*T{}) operator*() const
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
43 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
44 return *this->iterator;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
45 }
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
46
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
47 bool operator!=(const ReverseIterator<T>& other)
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
48 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
49 return this->iterator != other.iterator;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
50 }
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
51
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
52 private:
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
53 T iterator;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
54 };
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
55
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
56 template<typename T>
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
57 struct GenericIterator
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
58 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
59 using type = decltype(std::begin(T{}));
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
60 };
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
61
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
62 template<typename T>
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
63 struct GenericConstIterator
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
64 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
65 using type = decltype(std::begin((const T&) T{}));
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
66 };
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
67
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
68 template<typename T>
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
69 class ConstReverser
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
70 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
71 public:
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
72 ConstReverser(const T& container) :
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
73 container {container} {}
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
74
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
75 using Iterator = ReverseIterator<typename GenericConstIterator<T>::type>;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
76
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
77 Iterator begin() const
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
78 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
79 auto result = std::end(this->container);
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
80 return Iterator {--result};
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
81 }
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
82
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
83 Iterator end() const
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
84 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
85 auto result = std::begin(this->container);
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
86 return Iterator {--result};
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
87 }
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
88
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
89 private:
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
90 const T& container;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
91 };
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
92
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
93 template<typename T>
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
94 class MutableReverser
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
95 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
96 public:
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
97 MutableReverser(T& container) :
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
98 container {container} {}
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
99
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
100 using Iterator = ReverseIterator<typename GenericIterator<T>::type>;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
101
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
102 Iterator begin() const
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
103 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
104 auto result = std::end(this->container);
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
105 return Iterator {--result};
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
106 }
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
107
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
108 Iterator end() const
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
109 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
110 auto result = std::begin(this->container);
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
111 return Iterator {--result};
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
112 }
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
113
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
114 private:
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
115 T& container;
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
116 };
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
117
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
118 template<typename T>
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
119 ConstReverser<T> reverse(const T& container)
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
120 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
121 return {container};
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
122 }
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
123
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
124 template<typename T>
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
125 MutableReverser<T> reverse(T& container)
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
126 {
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
127 return {container};
6a8f76f0f4d1 convert MainWindow::deleteSelection to mvc
Santeri Piippo
parents:
diff changeset
128 }

mercurial