src/functional.h

Mon, 24 Aug 2020 22:51:01 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 24 Aug 2020 22:51:01 +0300
changeset 90
e234edb5e613
parent 63
f7dd937667a5
child 106
128efb9d148b
permissions
-rw-r--r--

remove dependency on glut, fixes

63
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2020 Teemu Piippo
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include <algorithm>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include <functional>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include <QVector>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #include <QSet>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 #include <QList>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 namespace fn
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 template<typename C>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 class InsertIterator;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 /**
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 * Like std::back_inserter but calls x.insert(v) instead
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 */
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 template<typename C>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 class fn::InsertIterator
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 public:
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 InsertIterator(C& container) :
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 container{container} {}
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 auto operator*()
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 return *this;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 auto operator++()
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 return *this;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 auto operator++(int)
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 return *this;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 template<typename T>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 auto operator=(T&& value)
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 this->container.insert(std::forward<T>(value));
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 return *this;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 private:
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 C& container;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 };
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 namespace fn
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 // Constructs a fn::InsertIterator
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 template<typename C>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 InsertIterator<C> makeInsertIterator(C& container)
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 return InsertIterator<C>{container};
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 // Constructs a back_inserter for std::vector
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 template<typename T>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 auto makeDefaultInserter(std::vector<T>& vec)
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 return std::back_inserter(vec);
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 // Constructs a back_inserter for QVector
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 template<typename T>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 auto makeDefaultInserter(QVector<T>& vec)
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 return std::back_inserter(vec);
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 // Constructs a back_inserter for QList
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 template<typename T>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 auto makeDefaultInserter(QList<T>& vec)
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 return std::back_inserter(vec);
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 // Constructs an fn::InsertIterator for QSet
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 template<typename T>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 auto makeDefaultInserter(QSet<T>& vec)
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 return InsertIterator(vec);
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 // Changes the value type of C
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 template<typename C, typename TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 struct ChangeContainerValueType
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 };
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 // Changes the value type of std::vector
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 template<typename T, typename TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 struct ChangeContainerValueType<std::vector<T>, TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 using type = std::vector<TT>;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 };
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 // Changes the value type of QVector
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 template<typename T, typename TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 struct ChangeContainerValueType<QVector<T>, TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
117 using type = QVector<TT>;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118 };
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
119
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120 // Changes the value type of QSet
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
121 template<typename T, typename TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 struct ChangeContainerValueType<QSet<T>, TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
123 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 using type = QSet<TT>;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125 };
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
126
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 // Changes the value type of QList
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 template<typename T, typename TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 struct ChangeContainerValueType<QList<T>, TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
130 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 using type = QList<TT>;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
132 };
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 // Changes the value type of C
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
135 template<typename C, typename TT>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 using ChangeContainerValueType_t = typename ChangeContainerValueType<C, TT>::type;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
137
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
138 /**
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
139 * \brief Applies \c function to all elements of \c container
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
140 * \param container Container to iterate
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
141 * \param function Function to apply
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
142 * \param Rt result type. If not provided, a suitable result type is deduced from inputs
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
143 * \returns mapped result container
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
144 */
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
145 template<typename Rt = void, typename C, typename Fn>
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
146 auto map(C&& container, Fn&& function)
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
147 {
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
148 using value_t = decltype(*std::declval<C>().begin());
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
149 using newvalue_t = std::result_of_t<Fn(value_t)>;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
150 using result_t = std::conditional_t<
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
151 std::is_same_v<Rt, void>,
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
152 ChangeContainerValueType_t<std::remove_reference_t<C>, newvalue_t>,
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
153 Rt>;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
154 result_t result;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
155 result.reserve(std::end(container) - std::begin(container));
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
156 std::transform(std::begin(container), std::end(container), makeDefaultInserter(result), function);
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
157 return result;
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
158 }
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
159 }

mercurial