Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
util-foreach.hpp
Go to the documentation of this file.
1/*
2 UTIL-FOREACH.hpp - helpers for doing something for each element
3
4 Copyright (C)
5 2009, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12*/
13
14
42#ifndef UTIL_FOREACH_H
43#define UTIL_FOREACH_H
44
45#include "lib/util.hpp"
46#include "lib/meta/trait.hpp"
47
48#include <functional>
49#include <algorithm>
50
51
52
53namespace util {
54
57
60
61
62
67 template <typename IT, typename FUN>
68 inline bool
69 and_all (IT i, IT end, FUN predicate)
70 {
71 for ( ; i!=end; ++i )
72 if (!predicate(*i))
73 return false;
74
75 return true;
76 }
77
78
83 template <typename IT, typename FUN>
84 inline bool
85 has_any (IT i, IT end, FUN predicate)
86 {
87 for ( ; i!=end; ++i )
88 if (predicate(*i))
89 return true;
90
91 return false;
92 }
93
94
95 /* === specialisations for STL containers and Lumiera Forward Iterators === */
96
108 template <typename Container
109 ,typename FUN
110 >
111 inline disable_if< can_IterForEach<Container>,
112 FUN >
113 for_each (Container const& coll, FUN doIt)
114 {
115 using lib::meta::unwrap;
116
117 return std::for_each (unwrap(coll).begin()
118 ,unwrap(coll).end()
119 , doIt);
120 }
121
122
124 template <typename IT
125 ,typename FUN
126 >
127 inline enable_if< can_IterForEach<IT>,
128 FUN >
129 for_each (IT const& ii, FUN doIt)
130 {
131 return std::for_each (ii, IT(), doIt);
132 }
133
134
135
136
137 template <typename Container
138 ,typename FUN
139 >
140 inline enable_if< can_STL_ForEach<Container>,
141 bool >
142 and_all (Container const& coll, FUN predicate)
143 {
144 using lib::meta::unwrap;
145
146 return and_all (unwrap(coll).begin()
147 ,unwrap(coll).end()
148 , predicate);
149 }
150
151
152 template <typename IT
153 ,typename FUN
154 >
155 inline enable_if< can_IterForEach<IT>,
156 bool >
157 and_all (IT const& ii, FUN predicate)
158 {
159 return and_all (ii, IT(), predicate);
160 }
161
162
163
164 template <typename Container
165 ,typename FUN
166 >
167 inline enable_if< can_STL_ForEach<Container>,
168 bool >
169 has_any (Container const& coll, FUN predicate)
170 {
171 using lib::meta::unwrap;
172
173 return has_any (unwrap(coll).begin()
174 ,unwrap(coll).end()
175 , predicate);
176 }
177
178
179 template <typename IT
180 ,typename FUN
181 >
182 inline enable_if< can_IterForEach<IT>,
183 bool >
184 has_any (IT const& ii, FUN predicate)
185 {
186 return has_any (ii, IT(), predicate);
187 }
188
189
190
191
192 /* === allow creating argument binders on-the-fly === */
193
194
197 template <typename CON, typename FUN, typename P1, typename...ARGS>
198 inline void
199 for_each (CON const& elements, FUN function, P1&& bind1, ARGS&& ...args)
200 {
201 for_each (elements, std::bind (function, std::forward<P1>(bind1), std::forward<ARGS> (args)...));
202 }
203
204
205
206
207
210 template <typename CON, typename FUN, typename P1, typename...ARGS>
211 inline bool
212 and_all (CON const& elements, FUN function, P1&& bind1, ARGS&& ...args)
213 {
214 return and_all (elements, std::bind<bool> (function, std::forward<P1>(bind1), std::forward<ARGS> (args)...));
215 }
216
217
218
219
220
223 template <typename CON, typename FUN, typename P1, typename...ARGS>
224 inline bool
225 has_any (CON const& elements, FUN function, P1&& bind1, ARGS&& ...args)
226 {
227 return has_any (elements, std::bind<bool> (function, std::forward<P1>(bind1), std::forward<ARGS> (args)...));
228 }
229
230
231
232} // namespace util
233#endif /*UTIL_FOREACH_H*/
Trait template to detect a type usable immediately as "Lumiera Forward Iterator" in a specialised for...
Definition trait.hpp:511
Trait template to detect a type usable with the STL for-each loop.
Definition trait.hpp:556
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
enable_if_c< not Cond::value, T >::type disable_if
Definition meta/util.hpp:90
Unwrap< X >::Type & unwrap(X const &wrapped)
convenience shortcut: unwrapping free function.
Definition trait.hpp:240
bool has_any(IT i, IT end, FUN predicate)
Existential quantification: check if any element of a collection satisfies the given predicate.
bool and_all(IT i, IT end, FUN predicate)
All quantification: check if all elements of a collection satisfy the given predicate.
disable_if< can_IterForEach< Container >, FUN > for_each(Container const &coll, FUN doIt)
operate on all elements of a STL container.
Helpers for type detection, type rewriting and metaprogramming.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...