implement division by scalar for Time and container ops
[libi2ncommon] / src / timefunc.hxx
index 43c5f20..4b4e3c9 100644 (file)
@@ -30,6 +30,7 @@ on this file might be covered by the GNU General Public License.
 #include <errno.h>
 #include <list>
 #include <string>
+#include <cstdlib>
 
 #include <boost/optional.hpp>
 
@@ -445,7 +446,9 @@ namespace clock {
             inline Time &subtract (const time_t t2) NOEXCEPT
             { return this->subtract (t2, 0L); };
 
-            Time &scale (const time_t factor) NOEXCEPT;
+            Time &scale (const int64_t factor) NOEXCEPT;
+
+            Time &divide (const int64_t divisor) NOEXCEPT;
 
             friend int compare (const Time &t1, const Time &t2) NOEXCEPT;
 
@@ -489,13 +492,21 @@ namespace clock {
             { return this->subtract (t2); }
 
             inline Time
-            operator* (const time_t factor) const NOEXCEPT
+            operator* (const int64_t factor) const NOEXCEPT
             { return Time (*this).scale (factor); }
 
             inline Time &
-            operator*= (const time_t factor) NOEXCEPT
+            operator*= (const int64_t factor) NOEXCEPT
             { return this->scale (factor); }
 
+            inline Time
+            operator/ (const int64_t divisor) const NOEXCEPT
+            { return Time (*this).divide (divisor); }
+
+            inline Time &
+            operator/= (const int64_t divisor) NOEXCEPT
+            { return this->divide (divisor); }
+
             friend CONSTEXPR bool
             operator== (const Time &t1, const Time &t2) NOEXCEPT;
 
@@ -531,6 +542,10 @@ namespace clock {
     operator- (const time_t t1, const Time &t2) NOEXCEPT
     { return Time (t1) - t2; }
 
+    inline Time
+    operator* (const time_t t1, const Time &t2) NOEXCEPT
+    { return t2 * t1; }
+
     int compare (const Time &t1, const Time &t2) NOEXCEPT;
 
     /*
@@ -591,6 +606,43 @@ namespace clock {
                      const enum type::id       id   = type::real,
                      const enum type::variant  var  = type::dflt) NOEXCEPT;
 
+    template <typename ContT>
+    Time
+    mean (const ContT &data)
+    {
+        Time sum (0, 0);
+
+        if (data.size () == 0) {
+            return sum;
+        }
+
+        for (typename ContT::const_iterator it = data.begin ();
+             it != data.end (); ++it)
+        {
+            sum += *it;
+        };
+
+        return sum.divide (static_cast<int64_t> (data.size ()));
+    };
+
+    template <typename ContT>
+    Time
+    median (const ContT &data)
+    {
+        if (data.size () == 0) {
+            return zero ();
+        }
+        if (data.size () == 1) {
+            return *data.begin ();
+        }
+
+        std::vector<typename ContT::value_type> sorted;
+        std::copy (data.begin (), data.end (), std::back_inserter (sorted));
+        std::sort (sorted.begin (), sorted.end ());
+
+        return sorted [data.size () / 2];
+    };
+
 } /* [namespace clock] */
 
 } /* [namespace I2n] */