src/functional.h

Tue, 28 Sep 2021 23:07:23 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 23:07:23 +0300
changeset 145
4dea24d3eda0
parent 123
e3fe3617b631
child 259
c27612f0eac0
permissions
-rw-r--r--

Use QSaveFile to save the file more safely

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 }
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
160
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
161 template<typename T, typename Fn>
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
162 bool any(T&& container, Fn&& f)
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
163 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
164 for (auto&& x : container)
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
165 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
166 if (f(x))
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
167 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
168 return true;
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
169 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
170 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
171 return false;
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
172 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
173
123
e3fe3617b631 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 106
diff changeset
174
106
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
175 template<typename T, typename Fn>
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
176 bool all(T&& container, Fn&& f)
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
177 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
178 for (auto&& x : container)
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
179 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
180 if (not f(x))
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
181 {
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
182 return false;
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
183 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
184 }
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
185 return true;
128efb9d148b work on draw preview
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
186 }

mercurial