22#ifndef Rcpp__sugar__median_h
23#define Rcpp__sugar__median_h
27namespace median_detail {
55inline bool less(T lhs, T rhs) {
61 if (lhs.r < rhs.r)
return true;
62 if (lhs.i < rhs.i)
return true;
69inline double half(
double lhs) {
73inline double half(
int lhs) {
74 return static_cast<double>(lhs) / 2.0;
86template <
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>();
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>));
124template <
int RTYPE,
bool NA,
typename T>
141 return Rcpp::traits::get_na<RESULT_RTYPE>();
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>));
158template <
int RTYPE,
typename T,
bool NA_RM>
175 return Rcpp::traits::get_na<RESULT_RTYPE>();
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>));
195template <
bool NA,
typename T,
bool NA_RM>
210 if (!(
x.size() % 2)) {
226template <
bool NA,
typename T>
241 if (!(
x.size() % 2)) {
268 if (!(
x.size() % 2)) {
281template <
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)