os/ossrv/ossrv_pub/boost_apis/boost/detail/dynamic_bitset.hpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// --------------------------------------------------
sl@0
     2
//
sl@0
     3
// (C) Copyright Chuck Allison and Jeremy Siek 2001 - 2002.
sl@0
     4
// (C) Copyright Gennaro Prota                 2003 - 2004.
sl@0
     5
//
sl@0
     6
// Distributed under the Boost Software License, Version 1.0.
sl@0
     7
//    (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     8
//          http://www.boost.org/LICENSE_1_0.txt)
sl@0
     9
//
sl@0
    10
// -----------------------------------------------------------
sl@0
    11
sl@0
    12
//  See http://www.boost.org/libs/dynamic_bitset for documentation.
sl@0
    13
sl@0
    14
sl@0
    15
#ifndef BOOST_DETAIL_DYNAMIC_BITSET_HPP
sl@0
    16
#define BOOST_DETAIL_DYNAMIC_BITSET_HPP
sl@0
    17
sl@0
    18
#include <cstddef> // for std::size_t
sl@0
    19
#include "boost/config.hpp"
sl@0
    20
#include "boost/detail/workaround.hpp"
sl@0
    21
//#include "boost/static_assert.hpp" // gps
sl@0
    22
sl@0
    23
sl@0
    24
namespace boost {
sl@0
    25
sl@0
    26
  namespace detail {
sl@0
    27
sl@0
    28
    // Gives (read-)access to the object representation
sl@0
    29
    // of an object of type T (3.9p4). CANNOT be used
sl@0
    30
    // on a base sub-object
sl@0
    31
    //
sl@0
    32
    template <typename T>
sl@0
    33
    inline const unsigned char * object_representation (T* p)
sl@0
    34
    {
sl@0
    35
        return static_cast<const unsigned char *>(static_cast<const void *>(p));
sl@0
    36
    }
sl@0
    37
sl@0
    38
sl@0
    39
    // ------- count function implementation --------------
sl@0
    40
sl@0
    41
    namespace dynamic_bitset_count_impl {
sl@0
    42
sl@0
    43
    typedef unsigned char byte_type;
sl@0
    44
sl@0
    45
    enum mode { access_by_bytes, access_by_blocks };
sl@0
    46
sl@0
    47
    template <mode> struct mode_to_type {};
sl@0
    48
sl@0
    49
    // the table: wrapped in a class template, so
sl@0
    50
    // that it is only instantiated if/when needed
sl@0
    51
    //
sl@0
    52
    template <bool dummy_name = true>
sl@0
    53
    struct count_table { static const byte_type table[]; };
sl@0
    54
sl@0
    55
    template <>
sl@0
    56
    struct count_table<false> { /* no table */ };
sl@0
    57
sl@0
    58
sl@0
    59
     const unsigned int table_width = 8;
sl@0
    60
     template <bool b>
sl@0
    61
     const byte_type count_table<b>::table[] =
sl@0
    62
     {
sl@0
    63
       // Automatically generated by GPTableGen.exe v.1.0
sl@0
    64
       //
sl@0
    65
     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,
sl@0
    66
     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,
sl@0
    67
     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,
sl@0
    68
     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,
sl@0
    69
     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,
sl@0
    70
     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,
sl@0
    71
     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,
sl@0
    72
     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
sl@0
    73
     };
sl@0
    74
sl@0
    75
sl@0
    76
     // overload for access by bytes
sl@0
    77
     //
sl@0
    78
sl@0
    79
     template <typename Iterator>
sl@0
    80
     inline std::size_t do_count(Iterator first, std::size_t length,
sl@0
    81
                                 int /*dummy param*/,
sl@0
    82
                                 mode_to_type<access_by_bytes>* )
sl@0
    83
     {
sl@0
    84
         std::size_t num = 0;
sl@0
    85
         if (length)
sl@0
    86
         {
sl@0
    87
             const byte_type * p = object_representation(&*first);
sl@0
    88
             length *= sizeof(*first);
sl@0
    89
sl@0
    90
              do {
sl@0
    91
                 num += count_table<>::table[*p];
sl@0
    92
                 ++p;
sl@0
    93
                 --length;
sl@0
    94
sl@0
    95
             } while (length);
sl@0
    96
         }
sl@0
    97
sl@0
    98
         return num;
sl@0
    99
     }
sl@0
   100
sl@0
   101
sl@0
   102
     // overload for access by blocks
sl@0
   103
     //
sl@0
   104
     template <typename Iterator, typename ValueType>
sl@0
   105
     inline std::size_t do_count(Iterator first, std::size_t length, ValueType,
sl@0
   106
                                 mode_to_type<access_by_blocks>*)
sl@0
   107
     {
sl@0
   108
         std::size_t num = 0;
sl@0
   109
         while (length){
sl@0
   110
sl@0
   111
             ValueType value = *first;
sl@0
   112
             while (value) {
sl@0
   113
                 num += count_table<>::table[value & ((1u<<table_width) - 1)];
sl@0
   114
                 value >>= table_width;
sl@0
   115
             }
sl@0
   116
sl@0
   117
             ++first;
sl@0
   118
             --length;
sl@0
   119
         }
sl@0
   120
sl@0
   121
         return num;
sl@0
   122
     }
sl@0
   123
sl@0
   124
sl@0
   125
    } // dynamic_bitset_count_impl
sl@0
   126
    // -------------------------------------------------------
sl@0
   127
sl@0
   128
sl@0
   129
    // Some library implementations simply return a dummy
sl@0
   130
    // value such as
sl@0
   131
    //
sl@0
   132
    //   size_type(-1) / sizeof(T)
sl@0
   133
    //
sl@0
   134
    // from vector<>::max_size. This tries to get out more
sl@0
   135
    // meaningful info.
sl@0
   136
    //
sl@0
   137
    template <typename T>
sl@0
   138
    typename T::size_type vector_max_size_workaround(const T & v) {
sl@0
   139
sl@0
   140
      typedef typename T::allocator_type allocator_type;
sl@0
   141
sl@0
   142
      const typename allocator_type::size_type alloc_max =
sl@0
   143
                                                  v.get_allocator().max_size();
sl@0
   144
      const typename T::size_type container_max = v.max_size();
sl@0
   145
sl@0
   146
      return alloc_max < container_max?
sl@0
   147
                    alloc_max :
sl@0
   148
                    container_max;
sl@0
   149
    }
sl@0
   150
sl@0
   151
    // for static_asserts
sl@0
   152
    template <typename T>
sl@0
   153
    struct dynamic_bitset_allowed_block_type {
sl@0
   154
        enum { value = (T(-1) > 0) }; // ensure T has no sign
sl@0
   155
    };
sl@0
   156
sl@0
   157
    template <>
sl@0
   158
    struct dynamic_bitset_allowed_block_type<bool> {
sl@0
   159
        enum { value = false };
sl@0
   160
    };
sl@0
   161
sl@0
   162
sl@0
   163
  } // namespace detail
sl@0
   164
sl@0
   165
} // namespace boost
sl@0
   166
sl@0
   167
#endif // include guard
sl@0
   168