Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74208 - trunk/boost/chrono
From: vicente.botet_at_[hidden]
Date: 2011-09-03 11:45:52


Author: viboes
Date: 2011-09-03 11:45:51 EDT (Sat, 03 Sep 2011)
New Revision: 74208
URL: http://svn.boost.org/trac/boost/changeset/74208

Log:
Chrono: Added rounding utilities
Added:
   trunk/boost/chrono/ceil.hpp (contents, props changed)
   trunk/boost/chrono/floor.hpp (contents, props changed)
   trunk/boost/chrono/round.hpp (contents, props changed)

Added: trunk/boost/chrono/ceil.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/ceil.hpp 2011-09-03 11:45:51 EDT (Sat, 03 Sep 2011)
@@ -0,0 +1,36 @@
+// boost/chrono/round.hpp ------------------------------------------------------------//
+
+// (C) Copyright Howard Hinnant
+// Copyright 2011 Vicente J. Botet Escriba
+
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/libs/chrono for documentation.
+
+#ifndef BOOST_CHRONO_CEIL_HPP
+#define BOOST_CHRONO_CEIL_HPP
+
+#include <boost/chrono/duration.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ /**
+ * round up
+ */
+ template <class To, class Rep, class Period>
+ To ceil(const duration<Rep, Period>& d)
+ {
+ To t = duration_cast<To>(d);
+ if (t < d)
+ ++t;
+ return t;
+ }
+
+ } // namespace chrono
+} // namespace boost
+
+#endif

Added: trunk/boost/chrono/floor.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/floor.hpp 2011-09-03 11:45:51 EDT (Sat, 03 Sep 2011)
@@ -0,0 +1,34 @@
+// boost/chrono/round.hpp ------------------------------------------------------------//
+
+// (C) Copyright Howard Hinnant
+// Copyright 2011 Vicente J. Botet Escriba
+
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/libs/chrono for documentation.
+
+#ifndef BOOST_CHRONO_FLOOR_HPP
+#define BOOST_CHRONO_FLOOR_HPP
+
+#include <boost/chrono/duration.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ /**
+ * round down
+ */
+ template <class To, class Rep, class Period>
+ To floor(const duration<Rep, Period>& d)
+ {
+ return duration_cast<To>(d);
+ }
+
+
+ } // namespace chrono
+} // namespace boost
+
+#endif

Added: trunk/boost/chrono/round.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/round.hpp 2011-09-03 11:45:51 EDT (Sat, 03 Sep 2011)
@@ -0,0 +1,47 @@
+// boost/chrono/round.hpp ------------------------------------------------------------//
+
+// (C) Copyright Howard Hinnant
+// Copyright 2011 Vicente J. Botet Escriba
+
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/libs/chrono for documentation.
+
+#ifndef BOOST_CHRONO_ROUND_HPP
+#define BOOST_CHRONO_ROUND_HPP
+
+#include <boost/chrono/duration.hpp>
+#include <boost/chrono/typeof/boost/chrono/chrono.hpp>
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ /**
+ * round to nearest, to even on tie
+ */
+ template <class To, class Rep, class Period>
+ To round(const duration<Rep, Period>& d)
+ {
+ To t0 = duration_cast<To>(d);
+ To t1 = t0;
+ ++t1;
+ BOOST_AUTO(diff0, d - t0);
+ BOOST_AUTO(diff1, t1 - d);
+ if (diff0 == diff1)
+ {
+ if (t0.count() & 1)
+ return t1;
+ return t0;
+ }
+ else if (diff0 < diff1)
+ return t0;
+ return t1;
+ }
+
+ } // namespace chrono
+} // namespace boost
+
+#endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk