dynamic_bitset.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // -----------------------------------------------------------
  2. //
  3. // Copyright (c) 2001-2002 Chuck Allison and Jeremy Siek
  4. // Copyright (c) 2003-2006, 2008, 2025 Gennaro Prota
  5. // Copyright (c) 2014 Glen Joseph Fernandes
  6. // (glenjofe@gmail.com)
  7. // Copyright (c) 2018 Evgeny Shulgin
  8. // Copyright (c) 2019 Andrey Semashev
  9. //
  10. // Distributed under the Boost Software License, Version 1.0.
  11. // (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. //
  14. // -----------------------------------------------------------
  15. #ifndef BOOST_DETAIL_DYNAMIC_BITSET_HPP
  16. #define BOOST_DETAIL_DYNAMIC_BITSET_HPP
  17. #include <cstddef>
  18. #include <memory>
  19. #include <type_traits>
  20. #include <utility>
  21. namespace boost {
  22. namespace detail {
  23. namespace dynamic_bitset_impl {
  24. template< typename AllocatorOrContainer, typename Block >
  25. class is_container
  26. {
  27. private:
  28. template< typename U >
  29. static decltype( std::declval< U >().resize( std::size_t{} ), std::declval< U >()[ 0 ], typename U::value_type(), std::is_same< typename U::value_type, Block >{}, std::true_type{} ) test( int );
  30. template< typename >
  31. static std::false_type test( ... );
  32. public:
  33. static constexpr bool value = decltype( test< AllocatorOrContainer >( 0 ) )::value;
  34. };
  35. template< typename AllocatorOrContainer, bool IsContainer >
  36. class allocator_type_extractor_impl;
  37. template< typename AllocatorOrContainer >
  38. class allocator_type_extractor_impl< AllocatorOrContainer, false >
  39. {
  40. public:
  41. typedef AllocatorOrContainer type;
  42. };
  43. template< typename AllocatorOrContainer >
  44. class allocator_type_extractor_impl< AllocatorOrContainer, true >
  45. {
  46. public:
  47. typedef typename AllocatorOrContainer::allocator_type type;
  48. };
  49. template< typename AllocatorOrContainer, typename Block >
  50. class allocator_type_extractor
  51. {
  52. public:
  53. typedef typename allocator_type_extractor_impl<
  54. AllocatorOrContainer,
  55. is_container< AllocatorOrContainer, Block >::value >::type type;
  56. };
  57. template< typename T, int amount, int width /* = default */ >
  58. struct shifter
  59. {
  60. static BOOST_DYNAMIC_BITSET_CONSTEXPR20 void
  61. left_shift( T & v )
  62. {
  63. amount >= width ? ( v = 0 )
  64. : ( v >>= BOOST_DYNAMIC_BITSET_WRAP_CONSTANT( amount ) );
  65. }
  66. };
  67. template< bool value >
  68. struct value_to_type
  69. {
  70. };
  71. // for static_asserts
  72. template< typename T >
  73. struct allowed_block_type
  74. {
  75. enum
  76. {
  77. value = T( -1 ) > 0 // ensure T has no sign
  78. };
  79. };
  80. template<>
  81. struct allowed_block_type< bool >
  82. {
  83. enum
  84. {
  85. value = false
  86. };
  87. };
  88. template< typename T >
  89. struct is_numeric
  90. {
  91. enum
  92. {
  93. value = false
  94. };
  95. };
  96. #define BOOST_dynamic_bitset_is_numeric( x ) \
  97. template<> \
  98. struct is_numeric< x > \
  99. { \
  100. enum \
  101. { \
  102. value = true \
  103. }; \
  104. } /**/
  105. BOOST_dynamic_bitset_is_numeric( bool );
  106. BOOST_dynamic_bitset_is_numeric( char );
  107. #if ! defined( BOOST_NO_INTRINSIC_WCHAR_T )
  108. BOOST_dynamic_bitset_is_numeric( wchar_t );
  109. #endif
  110. BOOST_dynamic_bitset_is_numeric( signed char );
  111. BOOST_dynamic_bitset_is_numeric( short );
  112. BOOST_dynamic_bitset_is_numeric( int );
  113. BOOST_dynamic_bitset_is_numeric( long );
  114. BOOST_dynamic_bitset_is_numeric( long long );
  115. BOOST_dynamic_bitset_is_numeric( unsigned char );
  116. BOOST_dynamic_bitset_is_numeric( unsigned short );
  117. BOOST_dynamic_bitset_is_numeric( unsigned int );
  118. BOOST_dynamic_bitset_is_numeric( unsigned long );
  119. BOOST_dynamic_bitset_is_numeric( unsigned long long );
  120. // intentionally omitted
  121. // BOOST_dynamic_bitset_is_numeric(float);
  122. // BOOST_dynamic_bitset_is_numeric(double);
  123. // BOOST_dynamic_bitset_is_numeric(long double);
  124. #undef BOOST_dynamic_bitset_is_numeric
  125. } // dynamic_bitset_impl
  126. } // namespace detail
  127. } // namespace boost
  128. #endif // include guard