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;
83 auto size() const ->
size_t
85 auto range =
static_cast<const RangeT*
>(
this);
86 return std::distance(range->begin(), range->end());
95 template <
typename Functor>
96 auto sum(Functor&& f)
const ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
98 auto range =
static_cast<const RangeT*
>(
this);
99 auto begin = range->begin();
100 auto end = range->end();
101 assert(begin != end);
102 typename std::decay<
decltype (f(*begin))>::type result = f(*begin);
105 for (; it != end; ++it)
116 template <
typename Functor>
117 auto avg(Functor&& f)
const ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
119 auto range =
static_cast<const RangeT*
>(
this);
120 auto begin = range->begin();
121 auto end = range->end();
122 assert(begin != end);
123 typename std::decay<
decltype (f(*begin))>::type result = f(*begin);
127 for (; it != end; ++it)
132 return (1.0 / n_elements) * result;
142 template <
typename Functor,
typename WeightFunctor>
143 auto avg(Functor&& f, WeightFunctor&& w)
const ->
typename std::decay<
decltype ((1.0/(w(std::declval<HandleT>())+w(std::declval<HandleT>())))*f(std::declval<HandleT>()))>::type
145 auto range =
static_cast<const RangeT*
>(
this);
146 auto begin = range->begin();
147 auto end = range->end();
148 assert(begin != end);
149 typename std::decay<
decltype (w(*begin))>::type weight = w(*begin);
150 typename std::decay<
decltype (w(*begin)*f(*begin))>::type result = weight * f(*begin);
151 typename std::decay<
decltype (w(*begin)+w(*begin))>::type weight_sum = weight;
154 for (; it != end; ++it)
157 result += weight*f(*it);
158 weight_sum += weight;
160 return (1.0 / weight_sum) * result;
170 template <
typename Functor>
173 auto range =
static_cast<const RangeT*
>(
this);
174 for (
auto e : *range)
187 template <
typename Functor>
190 auto range =
static_cast<const RangeT*
>(
this);
191 for (
auto e : *range)
206 template <
int n,
typename Functor = Identity>
207 auto to_array(Functor&& f = {})
const -> std::array<
typename std::decay<
decltype (f(std::declval<HandleT>()))>::type, n>
209 auto range =
static_cast<const RangeT*
>(
this);
210 std::array<typename std::decay<decltype (f(std::declval<HandleT>()))>::type, n> res;
211 auto it = range->begin();
212 auto end = range->end();
214 while (i < n && it != end)
215 res[i++] = f(*(it++));
226 template <
typename Functor = Identity>
227 auto to_vector(Functor&& f = {})
const -> std::vector<
typename std::decay<
decltype (f(std::declval<HandleT>()))>::type>
229 auto range =
static_cast<const RangeT*
>(
this);
230 std::vector<typename std::decay<decltype (f(std::declval<HandleT>()))>::type> res;
231 for (
const auto& e : *range)
243 template <
typename Functor = Identity>
244 auto to_set(Functor&& f = {})
const -> std::set<
typename std::decay<
decltype (f(std::declval<HandleT>()))>::type>
246 auto range =
static_cast<const RangeT*
>(
this);
247 std::set<typename std::decay<decltype (f(std::declval<HandleT>()))>::type> res;
248 for (
const auto& e : *range)
261 template <
typename Functor>
262 auto first(Functor&& f = {})
const -> HandleT
264 auto range =
static_cast<const RangeT*
>(
this);
265 for (
const auto& e : *range)
277 template <
typename Functor>
278 auto min(Functor&& f)
const ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
282 auto range =
static_cast<const RangeT*
>(
this);
283 auto it = range->begin();
284 auto end = range->end();
287 typename std::decay<decltype (f(std::declval<HandleT>()))>::type res = f(*it);
290 for (; it != end; ++it)
291 res =
min(res, f(*it));
302 template <
typename Functor>
303 auto argmin(Functor&& f)
const -> HandleT
305 auto range =
static_cast<const RangeT*
>(
this);
306 auto it = range->begin();
308 auto end = range->end();
311 typename std::decay<decltype (f(std::declval<HandleT>()))>::type curr_min = f(*it);
314 for (; it != end; ++it)
333 template <
typename Functor>
334 auto max(Functor&& f)
const ->
typename std::decay<decltype (f(std::declval<HandleT>()))>::type
338 auto range =
static_cast<const RangeT*
>(
this);
339 auto it = range->begin();
340 auto end = range->end();
343 typename std::decay<decltype (f(std::declval<HandleT>()))>::type res = f(*it);
346 for (; it != end; ++it)
347 res =
max(res, f(*it));
359 template <
typename Functor>
360 auto argmax(Functor&& f)
const -> HandleT
362 auto range =
static_cast<const RangeT*
>(
this);
363 auto it = range->begin();
365 auto end = range->end();
368 typename std::decay<decltype (f(std::declval<HandleT>()))>::type curr_max = f(*it);
371 for (; it != end; ++it)
391 template <
typename Functor>
392 auto minmax(Functor&& f)
const -> std::pair<typename std::decay<decltype (f(std::declval<HandleT>()))>::type,
393 typename std::decay<decltype (f(std::declval<HandleT>()))>::type>
395 return std::make_pair(this->
min(f), this->
max(f));
405 template <
typename Functor>
409 auto range =
static_cast<const RangeT*
>(
this);
410 for (
const auto& e : *range)
423 template <
typename Functor>
426 auto range =
static_cast<const RangeT*
>(
this);
427 for (
const auto& e : *range)
438 template <
typename Functor>
441 auto range =
static_cast<const RangeT*
>(
this);
448template <
typename RangeT,
typename HandleT,
typename Functor>
452 using BaseIterator =
decltype((std::declval<typename RangeT::Range>().begin()));
457 FilteredIterator(Functor f, BaseIterator it, BaseIterator end): BaseIterator(it), f_(f), end_(end)
459 if (!BaseIterator::operator==(end_) && !f_(*(*
this)))
467 BaseIterator::operator=(other);
474 if (BaseIterator::operator==(end_))
479 BaseIterator::operator++();
480 while (BaseIterator::operator!=(end_) && !f_(*(*
this)));
489 FilteredSmartRangeT(Functor&& f, BaseIterator begin, BaseIterator end) : f_(std::forward<Functor>(f)), begin_(std::move(begin)), end_(std::move(end)){}
490 FilteredIterator begin()
const {
return FilteredIterator(f_, begin_, end_); }
491 FilteredIterator end()
const {
return FilteredIterator(f_, end_, end_); }
Class which applies a filter when iterating over elements.
Base class for all smart range types.
auto to_set(Functor &&f={}) const -> std::set< typename std::decay< decltype(f(std::declval< HandleT >()))>::type >
Convert range to set.
auto sum(Functor &&f) const -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Computes the sum of elements.
auto argmin(Functor &&f) const -> HandleT
Compute minimal element.
auto count_if(Functor &&f) const -> int
Compute number of elements that satisfy a given predicate.
auto to_vector(Functor &&f={}) const -> std::vector< typename std::decay< decltype(f(std::declval< HandleT >()))>::type >
Convert range to vector.
auto argmax(Functor &&f) const -> HandleT
Compute maximal element.
auto filtered(Functor &&f) const -> FilteredSmartRangeT< SmartRange, Handle, Functor >
Only iterate over a subset of elements.
auto minmax(Functor &&f) const -> std::pair< typename std::decay< decltype(f(std::declval< HandleT >()))>::type, typename std::decay< decltype(f(std::declval< HandleT >()))>::type >
Computes minimum and maximum.
auto all_of(Functor &&f) const -> bool
Check if all elements fulfil condition.
auto first(Functor &&f={}) const -> HandleT
Get the first element that fulfills a condition.
auto min(Functor &&f) const -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Compute minimum.
auto avg(Functor &&f, WeightFunctor &&w) const -> 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.
auto to_array(Functor &&f={}) const -> std::array< typename std::decay< decltype(f(std::declval< HandleT >()))>::type, n >
Convert range to array.
auto avg(Functor &&f) const -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Computes the average of elements.
auto for_each(Functor &&f) const -> void
Apply a functor to each element.
auto size() const -> size_t
Computes number of elements.
auto any_of(Functor &&f) const -> bool
Check if any element fulfils condition.
auto max(Functor &&f) const -> typename std::decay< decltype(f(std::declval< HandleT >()))>::type
Compute maximum.