#include <iterator>
#include <tuple>
#include <cmath>
+#include <vector>
#include <boost/assert.hpp>
namespace boost::math::statistics {
return interquartile_range(v.begin(), v.end());
}
+template<class ForwardIterator, class OutputIterator>
+auto sorted_mode(ForwardIterator first, ForwardIterator last, OutputIterator output) -> decltype(output)
+{
+ using Z = typename std::iterator_traits<ForwardIterator>::value_type;
+ static_assert(std::is_integral<Z>::value, "Floating point values have not yet been implemented.");
+ using Size = typename std::iterator_traits<ForwardIterator>::difference_type;
+
+ std::vector<Z> modes {};
+ modes.reserve(16);
+ Size max_counter {0};
+
+ while(first != last)
+ {
+ Size current_count {0};
+ auto end_it {first};
+ while(end_it != last && *end_it == *first)
+ {
+ ++current_count;
+ ++end_it;
+ }
+
+ if(current_count > max_counter)
+ {
+ modes.resize(1);
+ modes[0] = *first;
+ max_counter = current_count;
+ }
+
+ else if(current_count == max_counter)
+ {
+ modes.emplace_back(*first);
+ }
+
+ first = end_it;
+ }
+
+ return std::move(modes.begin(), modes.end(), output);
+}
+
+template<class Container, class OutputIterator>
+inline auto sorted_mode(Container & v, OutputIterator output) -> decltype(output)
+{
+ return sorted_mode(v.begin(), v.end(), output);
+}
+
+template<class RandomAccessIterator, class OutputIterator>
+auto mode(RandomAccessIterator first, RandomAccessIterator last, OutputIterator output) -> decltype(output)
+{
+ std::sort(first, last);
+ return sorted_mode(first, last, output);
+}
+
+template<class RandomAccessContainer, class OutputIterator>
+inline auto mode(RandomAccessContainer & v, OutputIterator output) -> decltype(output)
+{
+ return mode(v.begin(), v.end(), output);
+}
}
#endif