VCCC  2024.05
VisualCamp Common C++ library
average.hpp
Go to the documentation of this file.
1 # /*
2 # * Created by YongGyu Lee on 2020/12/08.
3 # */
4 #
5 # ifndef VCCC_NUMERIC_AVERAGE_HPP
6 # define VCCC_NUMERIC_AVERAGE_HPP
7 #
8 # include <cassert>
9 # include <iterator>
10 #
11 # include "vccc/type_traits.hpp"
12 # include "vccc/__numeric/sum.hpp"
13 
14 namespace vccc {
15 
21 
28 constexpr auto average(InputIterator first, InputIterator last) {
29  assert(((void)"input size muse be larger than 0", first != last));
30  using return_type = decltype(*first);
31  if(first == last) return impl::default_value<decltype(*first)>() / static_cast<decay_if_float_t<return_type>>(1);
32  return sum(first, last) / static_cast<decay_if_float_t<return_type>>(std::distance(first, last));
33 }
34 
41 template<typename InputIterator, typename UnaryOperation,
43 constexpr auto
44 average(InputIterator first, InputIterator last, UnaryOperation unary_op)
45 {
46  assert(((void)"input size muse be larger than 0", first != last));
47  using return_type = decltype(unary_op(*first));
48  if(first == last) return static_cast<decay_if_float_t<return_type>>(sum(first, last, std::move(unary_op)));
49  return sum(first, last, unary_op) / static_cast<decay_if_float_t<return_type>>(std::distance(first, last));
50 }
51 
62 template<typename ...Numbers,
63  std::enable_if_t<conjunction<std::is_arithmetic<Numbers>...>::value, int> = 0>
64 constexpr inline auto
65 average(Numbers... numbers)
66 {
67  return sum(numbers...) / static_cast<decay_if_float_t<Numbers...>>(sizeof...(numbers));
68 }
69 
70 
77 template<typename ...Args,
78  std::enable_if_t<
79  conjunction<
80  negation<disjunction<is_iterable<Args>...>>,
81  negation<disjunction<std::is_arithmetic<Args>...>>
82  >::value, int> = 0>
83 constexpr inline
84 auto
85 average(const Args&... args)
86 {
87  return sum(args...) / static_cast<double>(sizeof...(args));
88 }
89 
91 
92 
98 template<typename ...Ints,
99  std::enable_if_t<conjunction<std::is_integral<Ints>...>::value, int> = 0>
100 constexpr inline
101 auto
102 int_average(Ints... ints)
103 {
104  return sum(ints...) / static_cast<signed_bigger_type_t<Ints...>>(sizeof...(Ints));
105 }
106 
108 
109 } // namespace vccc
110 
111 # endif // VCCC_NUMERIC_AVERAGE_HPP
typename signed_bigger_type< Ts... >::type signed_bigger_type_t
Definition: bigger_type.hpp:99
constexpr auto average(InputIterator first, InputIterator last)
get average of iterator values [first, last)
Definition: average.hpp:28
constexpr auto sum(InputIterator first, InputIterator last)
sum of iterator [first, last)
Definition: sum.hpp:57
constexpr auto int_average(Ints... ints)
get average value of integers(result is floored)
Definition: average.hpp:102
constexpr VCCC_INLINE_OR_STATIC detail::distance_niebloid distance
Definition: distance.hpp:72
typename std::conditional_t< conjunction< std::is_same< Types, float >... >::value, float, double > decay_if_float_t
Definition: lossless_type.hpp:20
Definition: directory.h:12
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 0 > first
Definition: key_value.hpp:34
constexpr VCCC_INLINE_OR_STATIC detail::element_niebloid< 1 > value
Definition: key_value.hpp:35