63 T operator()(
const T& _t)
const {
return _t; }
68 template <
typename RangeT,
typename HandleT,
typename Functor>
72 template <
typename RangeT,
typename HandleT>
75 using Handle = HandleT;
87 template <
typename Functor>
88 auto sum(Functor&& f) ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
90 auto range =
static_cast<const RangeT*
>(
this);
91 auto begin = range->begin();
92 auto end = range->end();
94 typename std::decay<decltype (f(*begin))>::type result = f(*begin);
97 for (; it != end; ++it)
108 template <
typename Functor>
109 auto avg(Functor&& f) ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
111 auto range =
static_cast<const RangeT*
>(
this);
112 auto begin = range->begin();
113 auto end = range->end();
114 assert(begin != end);
115 typename std::decay<decltype (f(*begin))>::type result = f(*begin);
119 for (; it != end; ++it)
124 return (1.0 / n_elements) * result;
134 template <
typename Functor>
137 auto range =
static_cast<const RangeT*
>(
this);
138 for (
auto e : *range)
151 template <
typename Functor>
154 auto range =
static_cast<const RangeT*
>(
this);
155 for (
auto e : *range)
170 template <
int n,
typename Functor = Identity>
171 auto to_array(Functor&& f = {}) -> std::array<
typename std::decay<decltype (f(std::declval<HandleT>()))>::type, n>
173 auto range =
static_cast<const RangeT*
>(
this);
174 std::array<typename std::decay<decltype (f(std::declval<HandleT>()))>::type, n> res;
175 auto it = range->begin();
176 auto end = range->end();
178 while (i < n && it != end)
179 res[i++] = f(*(it++));
190 template <
typename Functor = Identity>
191 auto to_vector(Functor&& f = {}) -> std::vector<
typename std::decay<decltype (f(std::declval<HandleT>()))>::type>
193 auto range =
static_cast<const RangeT*
>(
this);
194 std::vector<typename std::decay<decltype (f(std::declval<HandleT>()))>::type> res;
195 for (
const auto& e : *range)
207 template <
typename Functor = Identity>
208 auto to_set(Functor&& f = {}) -> std::set<
typename std::decay<decltype (f(std::declval<HandleT>()))>::type>
210 auto range =
static_cast<const RangeT*
>(
this);
211 std::set<typename std::decay<decltype (f(std::declval<HandleT>()))>::type> res;
212 for (
const auto& e : *range)
225 template <
typename Functor>
226 auto first(Functor&& f = {}) -> HandleT
228 auto range =
static_cast<const RangeT*
>(
this);
229 for (
const auto& e : *range)
241 template <
typename Functor>
242 auto min(Functor&& f) ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
246 auto range =
static_cast<const RangeT*
>(
this);
247 auto it = range->begin();
248 auto end = range->end();
251 typename std::decay<decltype (f(std::declval<HandleT>()))>::type res = f(*it);
254 for (; it != end; ++it)
255 res = min(res, f(*it));
266 template <
typename Functor>
269 auto range =
static_cast<const RangeT*
>(
this);
270 auto it = range->begin();
272 auto end = range->end();
275 typename std::decay<decltype (f(std::declval<HandleT>()))>::type curr_min = f(*it);
278 for (; it != end; ++it)
297 template <
typename Functor>
298 auto max(Functor&& f) ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
302 auto range =
static_cast<const RangeT*
>(
this);
303 auto it = range->begin();
304 auto end = range->end();
307 typename std::decay<decltype (f(std::declval<HandleT>()))>::type res = f(*it);
310 for (; it != end; ++it)
311 res = max(res, f(*it));
323 template <
typename Functor>
326 auto range =
static_cast<const RangeT*
>(
this);
327 auto it = range->begin();
329 auto end = range->end();
332 typename std::decay<decltype (f(std::declval<HandleT>()))>::type curr_max = f(*it);
335 for (; it != end; ++it)
355 template <
typename Functor>
356 auto minmax(Functor&& f) -> std::pair<typename std::decay<decltype (f(std::declval<HandleT>()))>::type,
357 typename std::decay<decltype (f(std::declval<HandleT>()))>::type>
359 return std::make_pair(this->min(f), this->max(f));
369 template <
typename Functor>
373 auto range =
static_cast<const RangeT*
>(
this);
374 for (
const auto& e : *range)
387 template <
typename Functor>
390 auto range =
static_cast<const RangeT*
>(
this);
391 for (
const auto& e : *range)
402 template <
typename Functor>
405 auto range =
static_cast<const RangeT*
>(
this);
406 auto b = (*range).begin();
407 auto e = (*range).end();
415 template <
typename RangeT,
typename HandleT,
typename Functor>
419 using BaseIterator = decltype((std::declval<typename RangeT::Range>().begin()));
424 FilteredIterator(Functor f, BaseIterator it, BaseIterator end): BaseIterator(it), f_(f), end_(end)
432 if (BaseIterator::operator==(end_))
437 BaseIterator::operator++();
438 while (BaseIterator::operator!=(end_) && !f_(*(*
this)));
446 FilteredSmartRangeT(Functor f, BaseIterator begin, BaseIterator end) : f_(f), begin_(begin), end_(end){}
auto any_of(Functor &&f) -> bool
Check if any element fulfils condition.
Definition: SmartRange.hh:135
Definition: SmartRange.hh:421
auto to_vector(Functor &&f={}) -> std::vector< typename std::decay< decltype(f(std::declval< HandleT >()))>::type >
Convert range to vector.
Definition: SmartRange.hh:191
auto to_set(Functor &&f={}) -> std::set< typename std::decay< decltype(f(std::declval< HandleT >()))>::type >
Convert range to set.
Definition: SmartRange.hh:208
auto min(Functor &&f) -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Compute minimum.
Definition: SmartRange.hh:242
auto filtered(Functor &&f) -> FilteredSmartRangeT< SmartRange, Handle, typename std::decay< Functor >::type >
Only iterate over a subset of elements.
Definition: SmartRange.hh:403
auto for_each(Functor &&f) -> void
Apply a functor to each element.
Definition: SmartRange.hh:388
auto argmin(Functor &&f) -> HandleT
Compute minimal element.
Definition: SmartRange.hh:267
auto sum(Functor &&f) -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Computes the sum of elements.
Definition: SmartRange.hh:88
auto minmax(Functor &&f) -> std::pair< typename std::decay< decltype(f(std::declval< HandleT >()))>::type, typename std::decay< decltype(f(std::declval< HandleT >()))>::type >
Computes minimum and maximum.
Definition: SmartRange.hh:356
auto max(Functor &&f) -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Compute maximum.
Definition: SmartRange.hh:298
auto first(Functor &&f={}) -> HandleT
Get the first element that fulfills a condition.
Definition: SmartRange.hh:226
auto avg(Functor &&f) -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Computes the average of elements.
Definition: SmartRange.hh:109
Base class for all smart range types.
Definition: SmartRange.hh:73
auto count_if(Functor &&f) -> int
Compute number of elements that satisfy a given predicate.
Definition: SmartRange.hh:370
auto to_array(Functor &&f={}) -> std::array< typename std::decay< decltype(f(std::declval< HandleT >()))>::type, n >
Convert range to array.
Definition: SmartRange.hh:171
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
Class which applies a filter when iterating over elements.
Definition: SmartRange.hh:69
auto all_of(Functor &&f) -> bool
Check if all elements fulfil condition.
Definition: SmartRange.hh:152
auto argmax(Functor &&f) -> HandleT
Compute maximal element.
Definition: SmartRange.hh:324