Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63614 - in sandbox/SOC/2010/bits_and_ints/boost/integer: . detail
From: muriloufg_at_[hidden]
Date: 2010-07-04 13:12:14


Author: murilov
Date: 2010-07-04 13:12:13 EDT (Sun, 04 Jul 2010)
New Revision: 63614
URL: http://svn.boost.org/trac/boost/changeset/63614

Log:
Added MPL suport on bit_utils.hpp
Added runtime versions for all functions on bit_utils.hpp
Text files modified:
   sandbox/SOC/2010/bits_and_ints/boost/integer/bit_utils.hpp | 82 +++++++++++++++++++++++++++++++--------
   sandbox/SOC/2010/bits_and_ints/boost/integer/detail/static_bit_reversal.hpp | 2
   2 files changed, 66 insertions(+), 18 deletions(-)

Modified: sandbox/SOC/2010/bits_and_ints/boost/integer/bit_utils.hpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/boost/integer/bit_utils.hpp (original)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/bit_utils.hpp 2010-07-04 13:12:13 EDT (Sun, 04 Jul 2010)
@@ -10,7 +10,8 @@
 #ifndef BOOST_BIT_UTILS_INCLUDED
 #define BOOST_BIT_UTILS_INCLUDED
 
-#include <boost/config.hpp> // for BOOST_STATIC_CONSTANT
+#include <boost/mpl/integral_c.hpp>
+#include <boost/mpl/bool.hpp>
 
 /*
  * Some utilities to handle integers.
@@ -18,35 +19,82 @@
 namespace boost
 {
         
+template <typename T>
+inline T set_bit(T data, unsigned char pos)
+{
+ return data | (T(1) << pos);
+}
+
+template <typename T>
+inline T clear_bit(T data, unsigned char pos)
+{
+ return data & ~(T(1) << pos);
+}
+
+template <typename T>
+inline T flip_bit(T data, unsigned char pos)
+{
+ return data ^ (T(1) << pos);
+}
+
+template <typename T>
+inline bool test_bit(T data, unsigned char pos)
+{
+ return bool((data >> pos) & T(1));
+}
+
 /*** Static metafunctions ***/
         
+
+namespace mpl {
+
+/*
+ * MPL compatible metafunctions
+ */
+
+template <typename IC, unsigned char pos>
+struct set_bit : mpl::integral_c<typename IC::value_type,
+ (IC::value | (typename IC::value_type(1) << pos))
+>
+{};
+
+template <typename IC, unsigned char pos>
+struct clear_bit : mpl::integral_c<typename IC::value_type,
+ (IC::value & ~(typename IC::value_type(1) << pos))
+>
+{};
+
+template <typename IC, unsigned char pos>
+struct flip_bit : mpl::integral_c<typename IC::value_type,
+ (IC::value ^ (typename IC::value_type(1) << pos))
+>
+{};
+
+template <typename IC, unsigned char pos>
+struct test_bit : mpl::bool_<((IC::value >> pos) & typename IC::value_type(1))>
+{};
+
+} // mpl
+
 // Sets the bit `pos' in data
 template <typename T, T data, unsigned char pos>
-struct set_bit
-{
- BOOST_STATIC_CONSTANT(T, value = data | (T(1) << pos));
-}; // set_bit
+struct static_set_bit : mpl::set_bit<mpl::integral_c<T, data>, pos>
+{};
 
 // Clear the bit `pos' in data
 template <typename T, T data, unsigned char pos>
-struct clear_bit
-{
- BOOST_STATIC_CONSTANT(T, value = data & ~(T(1) << pos));
-}; // clear_bit
+struct static_clear_bit : mpl::clear_bit<mpl::integral_c<T, data>, pos>
+{};
 
 // If the bit `pos' is 1 then it will be 0 if not the bit will be 1
 template <typename T, T data, unsigned char pos>
-struct flip_bit
-{
- BOOST_STATIC_CONSTANT(T, value = data ^ (T(1) << pos));
-}; // flip_bit
+struct static_flip_bit : mpl::flip_bit<mpl::integral_c<T, data>, pos>
+{};
 
 // Test if the bit in `pos' positon is set or not
 template <typename T, T data, unsigned char pos>
-struct test_bit
-{
- BOOST_STATIC_CONSTANT(bool, value = ((data >> pos) & T(1)) != T(0));
-}; // test_bit
+struct static_test_bit : mpl::test_bit<mpl::integral_c<T, data>, pos>
+{};
         
 } // boost
 

Modified: sandbox/SOC/2010/bits_and_ints/boost/integer/detail/static_bit_reversal.hpp
==============================================================================
--- sandbox/SOC/2010/bits_and_ints/boost/integer/detail/static_bit_reversal.hpp (original)
+++ sandbox/SOC/2010/bits_and_ints/boost/integer/detail/static_bit_reversal.hpp 2010-07-04 13:12:13 EDT (Sun, 04 Jul 2010)
@@ -20,7 +20,7 @@
 template <typename T, T data, std::size_t shift>
 struct static_bit_reversal_impl :
         public integral_constant<T,
- ((T(test_bit<T, data, (sizeof(T) * 8 - shift)>::value) << (shift - 1))
+ ((T(static_test_bit<T, data, (sizeof(T) * 8 - shift)>::value) << (shift - 1))
                  + static_bit_reversal_impl<T, data, shift - 1>::value)
>
 {};


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