Sat, 17 Mar 2018 11:35:07 +0200
removed the old Add History Line tool
1249 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2017 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (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, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #pragma once | |
20 | #include <type_traits> | |
21 | ||
22 | /* | |
23 | * reverse(container) | |
24 | * | |
25 | * Returns a container class that, when iterated, iterates over the container in reverse order. | |
26 | * Can be used for const and mutable containers, arrays as well as objects. | |
27 | */ | |
28 | ||
29 | template<typename T> | |
30 | class ReverseIterator | |
31 | { | |
32 | public: | |
33 | ReverseIterator(T iterator) : | |
34 | iterator {iterator} {} | |
35 | ||
36 | ReverseIterator& operator++() | |
37 | { | |
38 | --this->iterator; | |
39 | return *this; | |
40 | } | |
41 | ||
42 | decltype(*T{}) operator*() const | |
43 | { | |
44 | return *this->iterator; | |
45 | } | |
46 | ||
47 | bool operator!=(const ReverseIterator<T>& other) | |
48 | { | |
49 | return this->iterator != other.iterator; | |
50 | } | |
51 | ||
52 | private: | |
53 | T iterator; | |
54 | }; | |
55 | ||
56 | template<typename T> | |
57 | struct GenericIterator | |
58 | { | |
59 | using type = decltype(std::begin(T{})); | |
60 | }; | |
61 | ||
62 | template<typename T> | |
63 | struct GenericConstIterator | |
64 | { | |
65 | using type = decltype(std::begin((const T&) T{})); | |
66 | }; | |
67 | ||
68 | template<typename T> | |
69 | class ConstReverser | |
70 | { | |
71 | public: | |
72 | ConstReverser(const T& container) : | |
73 | container {container} {} | |
74 | ||
75 | using Iterator = ReverseIterator<typename GenericConstIterator<T>::type>; | |
76 | ||
77 | Iterator begin() const | |
78 | { | |
79 | auto result = std::end(this->container); | |
80 | return Iterator {--result}; | |
81 | } | |
82 | ||
83 | Iterator end() const | |
84 | { | |
85 | auto result = std::begin(this->container); | |
86 | return Iterator {--result}; | |
87 | } | |
88 | ||
89 | private: | |
90 | const T& container; | |
91 | }; | |
92 | ||
93 | template<typename T> | |
94 | class MutableReverser | |
95 | { | |
96 | public: | |
97 | MutableReverser(T& container) : | |
98 | container {container} {} | |
99 | ||
100 | using Iterator = ReverseIterator<typename GenericIterator<T>::type>; | |
101 | ||
102 | Iterator begin() const | |
103 | { | |
104 | auto result = std::end(this->container); | |
105 | return Iterator {--result}; | |
106 | } | |
107 | ||
108 | Iterator end() const | |
109 | { | |
110 | auto result = std::begin(this->container); | |
111 | return Iterator {--result}; | |
112 | } | |
113 | ||
114 | private: | |
115 | T& container; | |
116 | }; | |
117 | ||
118 | template<typename T> | |
119 | ConstReverser<T> reverse(const T& container) | |
120 | { | |
121 | return {container}; | |
122 | } | |
123 | ||
124 | template<typename T> | |
125 | MutableReverser<T> reverse(T& container) | |
126 | { | |
127 | return {container}; | |
128 | } |