63 T operator()(
const T& _t)
const {
return _t; }
68template <
typename RangeT,
typename HandleT,
typename Functor>
69struct FilteredSmartRangeT;
72template <
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,
typename WeightFunctor>
135 auto avg(Functor&& f, WeightFunctor&& w) ->
typename std::decay<
decltype ((1.0/(w(std::declval<HandleT>())+w(std::declval<HandleT>())))*f(std::declval<HandleT>()))>::type
137 auto range =
static_cast<const RangeT*
>(
this);
138 auto begin = range->begin();
139 auto end = range->end();
140 assert(begin != end);
141 typename std::decay<
decltype (w(*begin))>::type weight = w(*begin);
142 typename std::decay<
decltype (w(*begin)*f(*begin))>::type result = weight * f(*begin);
143 typename std::decay<
decltype (w(*begin)+w(*begin))>::type weight_sum = weight;
146 for (; it != end; ++it)
149 result += weight*f(*it);
150 weight_sum += weight;
152 return (1.0 / weight_sum) * result;
162 template <
typename Functor>
165 auto range =
static_cast<const RangeT*
>(
this);
166 for (
auto e : *range)
179 template <
typename Functor>
182 auto range =
static_cast<const RangeT*
>(
this);
183 for (
auto e : *range)
198 template <
int n,
typename Functor = Identity>
199 auto to_array(Functor&& f = {}) -> std::array<
typename std::decay<
decltype (f(std::declval<HandleT>()))>::type, n>
201 auto range =
static_cast<const RangeT*
>(
this);
202 std::array<typename std::decay<decltype (f(std::declval<HandleT>()))>::type, n> res;
203 auto it = range->begin();
204 auto end = range->end();
206 while (i < n && it != end)
207 res[i++] = f(*(it++));
218 template <
typename Functor = Identity>
219 auto to_vector(Functor&& f = {}) -> std::vector<
typename std::decay<
decltype (f(std::declval<HandleT>()))>::type>
221 auto range =
static_cast<const RangeT*
>(
this);
222 std::vector<typename std::decay<decltype (f(std::declval<HandleT>()))>::type> res;
223 for (
const auto& e : *range)
235 template <
typename Functor = Identity>
236 auto to_set(Functor&& f = {}) -> std::set<
typename std::decay<
decltype (f(std::declval<HandleT>()))>::type>
238 auto range =
static_cast<const RangeT*
>(
this);
239 std::set<typename std::decay<decltype (f(std::declval<HandleT>()))>::type> res;
240 for (
const auto& e : *range)
253 template <
typename Functor>
254 auto first(Functor&& f = {}) -> HandleT
256 auto range =
static_cast<const RangeT*
>(
this);
257 for (
const auto& e : *range)
269 template <
typename Functor>
270 auto min(Functor&& f) ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
274 auto range =
static_cast<const RangeT*
>(
this);
275 auto it = range->begin();
276 auto end = range->end();
279 typename std::decay<decltype (f(std::declval<HandleT>()))>::type res = f(*it);
282 for (; it != end; ++it)
283 res =
min(res, f(*it));
294 template <
typename Functor>
297 auto range =
static_cast<const RangeT*
>(
this);
298 auto it = range->begin();
300 auto end = range->end();
303 typename std::decay<decltype (f(std::declval<HandleT>()))>::type curr_min = f(*it);
306 for (; it != end; ++it)
325 template <
typename Functor>
326 auto max(Functor&& f) ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
330 auto range =
static_cast<const RangeT*
>(
this);
331 auto it = range->begin();
332 auto end = range->end();
335 typename std::decay<decltype (f(std::declval<HandleT>()))>::type res = f(*it);
338 for (; it != end; ++it)
339 res =
max(res, f(*it));
351 template <
typename Functor>
354 auto range =
static_cast<const RangeT*
>(
this);
355 auto it = range->begin();
357 auto end = range->end();
360 typename std::decay<decltype (f(std::declval<HandleT>()))>::type curr_max = f(*it);
363 for (; it != end; ++it)
383 template <
typename Functor>
384 auto minmax(Functor&& f) -> std::pair<typename std::decay<decltype (f(std::declval<HandleT>()))>::type,
385 typename std::decay<decltype (f(std::declval<HandleT>()))>::type>
387 return std::make_pair(this->
min(f), this->
max(f));
397 template <
typename Functor>
401 auto range =
static_cast<const RangeT*
>(
this);
402 for (
const auto& e : *range)
415 template <
typename Functor>
418 auto range =
static_cast<const RangeT*
>(
this);
419 for (
const auto& e : *range)
430 template <
typename Functor>
433 auto range =
static_cast<const RangeT*
>(
this);
440template <
typename RangeT,
typename HandleT,
typename Functor>
444 using BaseIterator =
decltype((std::declval<typename RangeT::Range>().begin()));
449 FilteredIterator(Functor f, BaseIterator it, BaseIterator end): BaseIterator(it), f_(f), end_(end)
451 if (!BaseIterator::operator==(end_) && !f_(*(*
this)))
459 BaseIterator::operator=(other);
466 if (BaseIterator::operator==(end_))
471 BaseIterator::operator++();
472 while (BaseIterator::operator!=(end_) && !f_(*(*
this)));
481 FilteredSmartRangeT(Functor&& f, BaseIterator begin, BaseIterator end) : f_(std::forward<Functor>(f)), begin_(std::move(begin)), end_(std::move(end)){}
482 FilteredIterator begin()
const {
return FilteredIterator(f_, begin_, end_); }
483 FilteredIterator end()
const {
return FilteredIterator(f_, end_, end_); }
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:442
Base class for all smart range types.
Definition: SmartRange.hh:74
auto for_each(Functor &&f) -> void
Apply a functor to each element.
Definition: SmartRange.hh:416
auto to_set(Functor &&f={}) -> std::set< typename std::decay< decltype(f(std::declval< HandleT >()))>::type >
Convert range to set.
Definition: SmartRange.hh:236
auto sum(Functor &&f) -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Computes the sum of elements.
Definition: SmartRange.hh:88
auto to_array(Functor &&f={}) -> std::array< typename std::decay< decltype(f(std::declval< HandleT >()))>::type, n >
Convert range to array.
Definition: SmartRange.hh:199
auto argmax(Functor &&f) -> HandleT
Compute maximal element.
Definition: SmartRange.hh:352
auto avg(Functor &&f) -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Computes the average of elements.
Definition: SmartRange.hh:109
auto min(Functor &&f) -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Compute minimum.
Definition: SmartRange.hh:270
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:384
auto any_of(Functor &&f) -> bool
Check if any element fulfils condition.
Definition: SmartRange.hh:163
auto first(Functor &&f={}) -> HandleT
Get the first element that fulfills a condition.
Definition: SmartRange.hh:254
auto all_of(Functor &&f) -> bool
Check if all elements fulfil condition.
Definition: SmartRange.hh:180
auto filtered(Functor &&f) -> FilteredSmartRangeT< SmartRange, Handle, Functor >
Only iterate over a subset of elements.
Definition: SmartRange.hh:431
auto to_vector(Functor &&f={}) -> std::vector< typename std::decay< decltype(f(std::declval< HandleT >()))>::type >
Convert range to vector.
Definition: SmartRange.hh:219
auto avg(Functor &&f, WeightFunctor &&w) -> typename std::decay< decltype((1.0/(w(std::declval< HandleT >())+w(std::declval< HandleT >()))) *f(std::declval< HandleT >()))>::type
Computes the weighted average of elements.
Definition: SmartRange.hh:135
auto argmin(Functor &&f) -> HandleT
Compute minimal element.
Definition: SmartRange.hh:295
auto max(Functor &&f) -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Compute maximum.
Definition: SmartRange.hh:326
auto count_if(Functor &&f) -> int
Compute number of elements that satisfy a given predicate.
Definition: SmartRange.hh:398
Definition: SmartRange.hh:447