22 #ifndef Rcpp__sugar__median_h
23 #define Rcpp__sugar__median_h
27 namespace median_detail {
55 inline bool less(T lhs, T rhs) {
61 if (lhs.r < rhs.r)
return true;
62 if (lhs.i < rhs.i)
return true;
69 inline double half(
double lhs) {
73 inline double half(
int lhs) {
74 return static_cast<double>(lhs) / 2.0;
77 inline Rcomplex
half(Rcomplex lhs) {
86 template <
int RTYPE,
bool NA,
typename T,
bool NA_RM = false>
103 return Rcpp::traits::get_na<RESULT_RTYPE>();
107 return Rcpp::traits::get_na<RESULT_RTYPE>();
110 R_xlen_t n =
x.size() / 2;
112 x.begin(),
x.begin() + n,
x.end(),
113 median_detail::less<stored_type>);
115 if (
x.size() % 2)
return x[n];
117 x[n] + *std::max_element(
118 x.begin(),
x.begin() + n,
119 median_detail::less<stored_type>));
124 template <
int RTYPE,
bool NA,
typename T>
141 return Rcpp::traits::get_na<RESULT_RTYPE>();
144 R_xlen_t n =
x.size() / 2;
146 x.begin(),
x.begin() + n,
x.end(),
147 median_detail::less<stored_type>);
149 if (
x.size() % 2)
return x[n];
151 x[n] + *std::max_element(
152 x.begin(),
x.begin() + n,
153 median_detail::less<stored_type>));
158 template <
int RTYPE,
typename T,
bool NA_RM>
175 return Rcpp::traits::get_na<RESULT_RTYPE>();
178 R_xlen_t n =
x.size() / 2;
180 x.begin(),
x.begin() + n,
x.end(),
181 median_detail::less<stored_type>);
183 if (
x.size() % 2)
return x[n];
185 x[n] + *std::max_element(
186 x.begin(),
x.begin() + n,
187 median_detail::less<stored_type>));
195 template <
bool NA,
typename T,
bool NA_RM>
210 if (!(
x.size() % 2)) {
218 R_xlen_t n =
x.size() / 2;
226 template <
bool NA,
typename T>
241 if (!(
x.size() % 2)) {
245 R_xlen_t n =
x.size() / 2;
253 template <
typename T>
268 if (!(
x.size() % 2)) {
272 R_xlen_t n =
x.size() / 2;
281 template <
int RTYPE,
bool NA,
typename T>
sugar::IsNa< RTYPE, NA, T > is_na(const Rcpp::VectorBase< RTYPE, NA, T > &t)
bool any(InputIterator first, InputIterator last, const T &value)
sugar::median_detail::result< RTYPE >::type median(const Rcpp::VectorBase< RTYPE, NA, T > &x, bool na_rm=false)
Vector< RTYPE > na_omit(const VectorBase< RTYPE, NA, T > &t)