dynamic_bitset.hpp

Go to the documentation of this file.
00001 // --------------------------------------------------
00002 //
00003 // (C) Copyright Chuck Allison and Jeremy Siek 2001 - 2002.
00004 // (C) Copyright Gennaro Prota                 2003 - 2004.
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 //    (See accompanying file LICENSE_1_0.txt or copy at
00008 //          http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 // -----------------------------------------------------------
00011 
00012 //  See http://www.boost.org/libs/dynamic_bitset for documentation.
00013 
00014 
00015 #ifndef BOOST_DETAIL_DYNAMIC_BITSET_HPP
00016 #define BOOST_DETAIL_DYNAMIC_BITSET_HPP
00017 
00018 #include <cstddef> // for std::size_t
00019 #include "sysc/packages/boost/config.hpp"
00020 #include "sysc/packages/boost/detail/workaround.hpp"
00021 //#include "sysc/packages/boost/static_assert.hpp" // gps
00022 
00023 
00024 namespace boost {
00025 
00026   namespace detail {
00027 
00028     // Gives (read-)access to the object representation
00029     // of an object of type T (3.9p4). CANNOT be used
00030     // on a base sub-object
00031     //
00032     template <typename T>
00033     inline const unsigned char * object_representation (T* p)
00034     {
00035         return static_cast<const unsigned char *>(static_cast<const void *>(p));
00036     }
00037 
00038 
00039     // ------- count function implementation --------------
00040 
00041     namespace dynamic_bitset_count_impl {
00042 
00043     typedef unsigned char byte_type;
00044 
00045     enum mode { access_by_bytes, access_by_blocks };
00046 
00047     template <mode> struct mode_to_type {};
00048 
00049     // the table: wrapped in a class template, so
00050     // that it is only instantiated if/when needed
00051     //
00052     template <bool dummy_name = true>
00053     struct count_table { static const byte_type table[]; };
00054 
00055     template <>
00056     struct count_table<false> { /* no table */ };
00057 
00058 
00059      const unsigned int table_width = 8;
00060      template <bool b>
00061      const byte_type count_table<b>::table[] =
00062      {
00063        // Automatically generated by GPTableGen.exe v.1.0
00064        //
00065      0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00066      1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00067      1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00068      2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00069      1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00070      2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00071      2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00072      3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
00073      };
00074 
00075 
00076      // overload for access by bytes
00077      //
00078 
00079      template <typename Iterator>
00080      inline std::size_t do_count(Iterator first, std::size_t length,
00081                                  int /*dummy param*/,
00082                                  mode_to_type<access_by_bytes>* )
00083      {
00084          std::size_t num = 0;
00085          if (length)
00086          {
00087              const byte_type * p = object_representation(&*first);
00088              length *= sizeof(*first);
00089 
00090               do {
00091                  num += count_table<>::table[*p];
00092                  ++p;
00093                  --length;
00094 
00095              } while (length);
00096          }
00097 
00098          return num;
00099      }
00100 
00101 
00102      // overload for access by blocks
00103      //
00104      template <typename Iterator, typename ValueType>
00105      inline std::size_t do_count(Iterator first, std::size_t length, ValueType,
00106                                  mode_to_type<access_by_blocks>*)
00107      {
00108          std::size_t num = 0;
00109          while (length){
00110 
00111              ValueType value = *first;
00112              while (value) {
00113                  num += count_table<>::table[value & ((1u<<table_width) - 1)];
00114                  value >>= table_width;
00115              }
00116 
00117              ++first;
00118              --length;
00119          }
00120 
00121          return num;
00122      }
00123 
00124 
00125     } // dynamic_bitset_count_impl
00126     // -------------------------------------------------------
00127 
00128 
00129     // Some library implementations simply return a dummy
00130     // value such as
00131     //
00132     //   size_type(-1) / sizeof(T)
00133     //
00134     // from vector<>::max_size. This tries to get out more
00135     // meaningful info.
00136     //
00137     template <typename T>
00138     typename T::size_type vector_max_size_workaround(const T & v) {
00139 
00140       typedef typename T::allocator_type allocator_type;
00141 
00142       const typename allocator_type::size_type alloc_max =
00143                                                   v.get_allocator().max_size();
00144       const typename T::size_type container_max = v.max_size();
00145 
00146       return alloc_max < container_max?
00147                     alloc_max :
00148                     container_max;
00149     }
00150 
00151     // for static_asserts
00152     template <typename T>
00153     struct dynamic_bitset_allowed_block_type {
00154         enum { value = T(-1) > 0 }; // ensure T has no sign
00155     };
00156 
00157     template <>
00158     struct dynamic_bitset_allowed_block_type<bool> {
00159         enum { value = false };
00160     };
00161 
00162 
00163   } // namespace detail
00164 
00165 } // namespace boost
00166 
00167 #endif // include guard
00168 

Generated on Wed Jan 21 15:32:09 2009 for SystemC by  doxygen 1.5.5