]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/boost/math/statistics/univariate_statistics.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / math / statistics / univariate_statistics.hpp
index 9b98d33a987bfb2c15f9d8d9637088c7b6d72179..6dd62d4f80c09e9daa8100f6bd08a342458b5be2 100644 (file)
@@ -10,6 +10,7 @@
 #include <iterator>
 #include <tuple>
 #include <cmath>
+#include <vector>
 #include <boost/assert.hpp>
 
 namespace boost::math::statistics {
@@ -512,6 +513,63 @@ inline auto interquartile_range(RandomAccessContainer & v)
     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