From 13676f06e5398b83f05e13d9f60288f1c0eba1ef Mon Sep 17 00:00:00 2001 From: chros Date: Sat, 11 Mar 2017 11:40:08 +0000 Subject: [PATCH] Add median command (See #14) --- rak/algorithm.h | 20 ++++++++++++++++++++ src/command_ui.cc | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/rak/algorithm.h b/rak/algorithm.h index ad8e7158..98613820 100644 --- a/rak/algorithm.h +++ b/rak/algorithm.h @@ -176,6 +176,26 @@ inline int popcount_wrapper(T t) { #endif } +// Get the median of an unordered set of numbers of arbitrary +// type by modifing the underlying dataset +template +T median(_InputIter __first, _InputIter __last) { + T __med; + + unsigned int __size = __last - __first; + unsigned int __middle = __size / 2; + _InputIter __target1 = __first + __middle; + std::nth_element(__first, __target1, __last); + __med = *__target1; + + if (__size % 2 == 0) { + _InputIter __target2 = std::max_element(__first, __target1); + __med = (__med + *__target2) / 2.0; + } + + return __med; +} + } #endif diff --git a/src/command_ui.cc b/src/command_ui.cc index a558de64..d0646bd1 100644 --- a/src/command_ui.cc +++ b/src/command_ui.cc @@ -39,6 +39,7 @@ #include #include +#include #include #include @@ -519,6 +520,31 @@ apply_elapsed_greater(const torrent::Object::list_type& args) { return (int64_t)(start_time != 0 && rak::timer::current_seconds() - start_time > rpc::convert_to_value(args.back())); } +inline std::vector +as_vector(const torrent::Object::list_type& args) { + if (args.size() == 0) + throw torrent::input_error("Wrong argument count in as_list."); + + std::vector result; + + for (torrent::Object::list_const_iterator itr = args.begin(), last = args.end(); itr != last; itr++) { + + if (itr->is_value()) { + result.push_back(itr->as_value()); + } else if (itr->is_string()) { + result.push_back(rpc::convert_to_value(itr->as_string())); + } else if (itr->is_list()) { + std::vector subResult = as_vector(itr->as_list()); + result.insert(result.end(), subResult.begin(), subResult.end()); + } else { + throw torrent::input_error("Wrong type supplied to as_list."); + } + + } + + return result; +} + int64_t apply_math_basic(const std::function op, const torrent::Object::list_type& args) { if (args.size() == 0) @@ -600,11 +626,14 @@ apply_arith_other(const char* op, const torrent::Object::list_type& args) { if (args.size() == 0) throw torrent::input_error("Wrong argument count in apply_arith_other."); - if (op == "average") + if (op == "average") { return (int64_t)(apply_math_basic(std::plus(), args) / apply_arith_count(args)); - else + } else if (op == "median") { + std::vector result = as_vector(args); + return (int64_t)rak::median(result.begin(), result.end()); + } else { throw torrent::input_error("Wrong operation supplied to apply_arith_other."); - + } } void @@ -682,6 +711,7 @@ initialize_command_ui() { CMD2_ANY_LIST("math.max", std::bind(&apply_arith_basic, std::greater(), std::placeholders::_2)); CMD2_ANY_LIST("math.cnt", std::bind(&apply_arith_count, std::placeholders::_2)); CMD2_ANY_LIST("math.avg", std::bind(&apply_arith_other, "average", std::placeholders::_2)); + CMD2_ANY_LIST("math.med", std::bind(&apply_arith_other, "median", std::placeholders::_2)); CMD2_ANY_LIST ("elapsed.less", std::bind(&apply_elapsed_less, std::placeholders::_2)); CMD2_ANY_LIST ("elapsed.greater", std::bind(&apply_elapsed_greater, std::placeholders::_2));