bitset 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Profiling bitset implementation -*- C++ -*-
  2. // Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file profile/bitset
  21. * This file is a GNU profile extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_PROFILE_BITSET
  24. #define _GLIBCXX_PROFILE_BITSET
  25. #include <bitset>
  26. namespace std _GLIBCXX_VISIBILITY(default)
  27. {
  28. namespace __profile
  29. {
  30. /// Class std::bitset wrapper with performance instrumentation, none at the
  31. /// moment.
  32. template<size_t _Nb>
  33. class bitset
  34. : public _GLIBCXX_STD_C::bitset<_Nb>
  35. {
  36. typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
  37. public:
  38. // 23.3.5.1 constructors:
  39. #if __cplusplus < 201103L
  40. bitset()
  41. : _Base() { }
  42. #else
  43. constexpr bitset() = default;
  44. #endif
  45. #if __cplusplus >= 201103L
  46. constexpr bitset(unsigned long long __val) noexcept
  47. #else
  48. bitset(unsigned long __val)
  49. #endif
  50. : _Base(__val) { }
  51. template<typename _CharT, typename _Traits, typename _Alloc>
  52. explicit
  53. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
  54. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  55. __pos = 0,
  56. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  57. __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
  58. : _Base(__str, __pos, __n) { }
  59. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  60. // 396. what are characters zero and one.
  61. template<class _CharT, class _Traits, class _Alloc>
  62. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
  63. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  64. __pos,
  65. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  66. __n,
  67. _CharT __zero, _CharT __one = _CharT('1'))
  68. : _Base(__str, __pos, __n, __zero, __one) { }
  69. bitset(const _Base& __x) : _Base(__x) { }
  70. #if __cplusplus >= 201103L
  71. template<typename _CharT>
  72. explicit
  73. bitset(const _CharT* __str,
  74. typename std::basic_string<_CharT>::size_type __n
  75. = std::basic_string<_CharT>::npos,
  76. _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
  77. : _Base(__str, __n, __zero, __one) { }
  78. #endif
  79. // 23.3.5.2 bitset operations:
  80. bitset<_Nb>&
  81. operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  82. {
  83. _M_base() &= __rhs;
  84. return *this;
  85. }
  86. bitset<_Nb>&
  87. operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  88. {
  89. _M_base() |= __rhs;
  90. return *this;
  91. }
  92. bitset<_Nb>&
  93. operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  94. {
  95. _M_base() ^= __rhs;
  96. return *this;
  97. }
  98. bitset<_Nb>&
  99. operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
  100. {
  101. _M_base() <<= __pos;
  102. return *this;
  103. }
  104. bitset<_Nb>&
  105. operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
  106. {
  107. _M_base() >>= __pos;
  108. return *this;
  109. }
  110. bitset<_Nb>&
  111. set() _GLIBCXX_NOEXCEPT
  112. {
  113. _Base::set();
  114. return *this;
  115. }
  116. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  117. // 186. bitset::set() second parameter should be bool
  118. bitset<_Nb>&
  119. set(size_t __pos, bool __val = true)
  120. {
  121. _Base::set(__pos, __val);
  122. return *this;
  123. }
  124. bitset<_Nb>&
  125. reset() _GLIBCXX_NOEXCEPT
  126. {
  127. _Base::reset();
  128. return *this;
  129. }
  130. bitset<_Nb>&
  131. reset(size_t __pos)
  132. {
  133. _Base::reset(__pos);
  134. return *this;
  135. }
  136. bitset<_Nb>
  137. operator~() const _GLIBCXX_NOEXCEPT
  138. { return bitset(~_M_base()); }
  139. bitset<_Nb>&
  140. flip() _GLIBCXX_NOEXCEPT
  141. {
  142. _Base::flip();
  143. return *this;
  144. }
  145. bitset<_Nb>&
  146. flip(size_t __pos)
  147. {
  148. _Base::flip(__pos);
  149. return *this;
  150. }
  151. bool
  152. operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
  153. { return _M_base() == __rhs; }
  154. bool
  155. operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
  156. { return _M_base() != __rhs; }
  157. bitset<_Nb>
  158. operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
  159. { return bitset<_Nb>(_M_base() << __pos); }
  160. bitset<_Nb>
  161. operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
  162. { return bitset<_Nb>(_M_base() >> __pos); }
  163. _Base&
  164. _M_base() _GLIBCXX_NOEXCEPT
  165. { return *this; }
  166. const _Base&
  167. _M_base() const _GLIBCXX_NOEXCEPT
  168. { return *this; }
  169. };
  170. template<size_t _Nb>
  171. bitset<_Nb>
  172. operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  173. { return bitset<_Nb>(__x) &= __y; }
  174. template<size_t _Nb>
  175. bitset<_Nb>
  176. operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  177. { return bitset<_Nb>(__x) |= __y; }
  178. template<size_t _Nb>
  179. bitset<_Nb>
  180. operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  181. { return bitset<_Nb>(__x) ^= __y; }
  182. template<typename _CharT, typename _Traits, size_t _Nb>
  183. std::basic_istream<_CharT, _Traits>&
  184. operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
  185. { return __is >> __x._M_base(); }
  186. template<typename _CharT, typename _Traits, size_t _Nb>
  187. std::basic_ostream<_CharT, _Traits>&
  188. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  189. const bitset<_Nb>& __x)
  190. { return __os << __x._M_base(); }
  191. } // namespace __profile
  192. #if __cplusplus >= 201103L
  193. // DR 1182.
  194. /// std::hash specialization for bitset.
  195. template<size_t _Nb>
  196. struct hash<__profile::bitset<_Nb>>
  197. : public __hash_base<size_t, __profile::bitset<_Nb>>
  198. {
  199. size_t
  200. operator()(const __profile::bitset<_Nb>& __b) const noexcept
  201. { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
  202. };
  203. #endif
  204. } // namespace std
  205. #endif