You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

430 lines
12KB

  1. // Debugging bitset implementation -*- C++ -*-
  2. // Copyright (C) 2003-2020 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 debug/bitset
  21. * This file is a GNU debug extension to the Standard C++ Library.
  22. */
  23. #ifndef _GLIBCXX_DEBUG_BITSET
  24. #define _GLIBCXX_DEBUG_BITSET
  25. #pragma GCC system_header
  26. #include <bitset>
  27. #include <debug/safe_sequence.h>
  28. #include <debug/safe_iterator.h>
  29. namespace std _GLIBCXX_VISIBILITY(default)
  30. {
  31. namespace __debug
  32. {
  33. /// Class std::bitset with additional safety/checking/debug instrumentation.
  34. template<size_t _Nb>
  35. class bitset
  36. : public _GLIBCXX_STD_C::bitset<_Nb>
  37. #if __cplusplus < 201103L
  38. , public __gnu_debug::_Safe_sequence_base
  39. #endif
  40. {
  41. typedef _GLIBCXX_STD_C::bitset<_Nb> _Base;
  42. public:
  43. // In C++11 we rely on normal reference type to preserve the property
  44. // of bitset to be use as a literal.
  45. // TODO: Find another solution.
  46. #if __cplusplus >= 201103L
  47. typedef typename _Base::reference reference;
  48. #else
  49. // bit reference:
  50. class reference
  51. : private _Base::reference
  52. , public __gnu_debug::_Safe_iterator_base
  53. {
  54. typedef typename _Base::reference _Base_ref;
  55. friend class bitset;
  56. reference();
  57. reference(const _Base_ref& __base, bitset* __seq) _GLIBCXX_NOEXCEPT
  58. : _Base_ref(__base)
  59. , _Safe_iterator_base(__seq, false)
  60. { }
  61. public:
  62. reference(const reference& __x) _GLIBCXX_NOEXCEPT
  63. : _Base_ref(__x)
  64. , _Safe_iterator_base(__x, false)
  65. { }
  66. reference&
  67. operator=(bool __x) _GLIBCXX_NOEXCEPT
  68. {
  69. _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
  70. _M_message(__gnu_debug::__msg_bad_bitset_write)
  71. ._M_iterator(*this));
  72. *static_cast<_Base_ref*>(this) = __x;
  73. return *this;
  74. }
  75. reference&
  76. operator=(const reference& __x) _GLIBCXX_NOEXCEPT
  77. {
  78. _GLIBCXX_DEBUG_VERIFY(!__x._M_singular(),
  79. _M_message(__gnu_debug::__msg_bad_bitset_read)
  80. ._M_iterator(__x));
  81. _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
  82. _M_message(__gnu_debug::__msg_bad_bitset_write)
  83. ._M_iterator(*this));
  84. *static_cast<_Base_ref*>(this) = __x;
  85. return *this;
  86. }
  87. bool
  88. operator~() const _GLIBCXX_NOEXCEPT
  89. {
  90. _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
  91. _M_message(__gnu_debug::__msg_bad_bitset_read)
  92. ._M_iterator(*this));
  93. return ~(*static_cast<const _Base_ref*>(this));
  94. }
  95. operator bool() const _GLIBCXX_NOEXCEPT
  96. {
  97. _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
  98. _M_message(__gnu_debug::__msg_bad_bitset_read)
  99. ._M_iterator(*this));
  100. return *static_cast<const _Base_ref*>(this);
  101. }
  102. reference&
  103. flip() _GLIBCXX_NOEXCEPT
  104. {
  105. _GLIBCXX_DEBUG_VERIFY(!this->_M_singular(),
  106. _M_message(__gnu_debug::__msg_bad_bitset_flip)
  107. ._M_iterator(*this));
  108. _Base_ref::flip();
  109. return *this;
  110. }
  111. };
  112. #endif
  113. // 23.3.5.1 constructors:
  114. _GLIBCXX_CONSTEXPR bitset() _GLIBCXX_NOEXCEPT
  115. : _Base() { }
  116. #if __cplusplus >= 201103L
  117. constexpr bitset(unsigned long long __val) noexcept
  118. #else
  119. bitset(unsigned long __val)
  120. #endif
  121. : _Base(__val) { }
  122. template<typename _CharT, typename _Traits, typename _Alloc>
  123. explicit
  124. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
  125. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  126. __pos = 0,
  127. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  128. __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
  129. : _Base(__str, __pos, __n) { }
  130. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  131. // 396. what are characters zero and one.
  132. template<class _CharT, class _Traits, class _Alloc>
  133. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
  134. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  135. __pos,
  136. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  137. __n,
  138. _CharT __zero, _CharT __one = _CharT('1'))
  139. : _Base(__str, __pos, __n, __zero, __one) { }
  140. bitset(const _Base& __x) : _Base(__x) { }
  141. #if __cplusplus >= 201103L
  142. template<typename _CharT>
  143. explicit
  144. bitset(const _CharT* __str,
  145. typename std::basic_string<_CharT>::size_type __n
  146. = std::basic_string<_CharT>::npos,
  147. _CharT __zero = _CharT('0'), _CharT __one = _CharT('1'))
  148. : _Base(__str, __n, __zero, __one) { }
  149. #endif
  150. // 23.3.5.2 bitset operations:
  151. bitset<_Nb>&
  152. operator&=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  153. {
  154. _M_base() &= __rhs;
  155. return *this;
  156. }
  157. bitset<_Nb>&
  158. operator|=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  159. {
  160. _M_base() |= __rhs;
  161. return *this;
  162. }
  163. bitset<_Nb>&
  164. operator^=(const bitset<_Nb>& __rhs) _GLIBCXX_NOEXCEPT
  165. {
  166. _M_base() ^= __rhs;
  167. return *this;
  168. }
  169. bitset<_Nb>&
  170. operator<<=(size_t __pos) _GLIBCXX_NOEXCEPT
  171. {
  172. _M_base() <<= __pos;
  173. return *this;
  174. }
  175. bitset<_Nb>&
  176. operator>>=(size_t __pos) _GLIBCXX_NOEXCEPT
  177. {
  178. _M_base() >>= __pos;
  179. return *this;
  180. }
  181. bitset<_Nb>&
  182. set() _GLIBCXX_NOEXCEPT
  183. {
  184. _Base::set();
  185. return *this;
  186. }
  187. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  188. // 186. bitset::set() second parameter should be bool
  189. bitset<_Nb>&
  190. set(size_t __pos, bool __val = true)
  191. {
  192. _Base::set(__pos, __val);
  193. return *this;
  194. }
  195. bitset<_Nb>&
  196. reset() _GLIBCXX_NOEXCEPT
  197. {
  198. _Base::reset();
  199. return *this;
  200. }
  201. bitset<_Nb>&
  202. reset(size_t __pos)
  203. {
  204. _Base::reset(__pos);
  205. return *this;
  206. }
  207. bitset<_Nb>
  208. operator~() const _GLIBCXX_NOEXCEPT
  209. { return bitset(~_M_base()); }
  210. bitset<_Nb>&
  211. flip() _GLIBCXX_NOEXCEPT
  212. {
  213. _Base::flip();
  214. return *this;
  215. }
  216. bitset<_Nb>&
  217. flip(size_t __pos)
  218. {
  219. _Base::flip(__pos);
  220. return *this;
  221. }
  222. // element access:
  223. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  224. // 11. Bitset minor problems
  225. reference
  226. operator[](size_t __pos)
  227. {
  228. __glibcxx_check_subscript(__pos);
  229. #if __cplusplus >= 201103L
  230. return _M_base()[__pos];
  231. #else
  232. return reference(_M_base()[__pos], this);
  233. #endif
  234. }
  235. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  236. // 11. Bitset minor problems
  237. _GLIBCXX_CONSTEXPR bool
  238. operator[](size_t __pos) const
  239. {
  240. #if __cplusplus < 201103L
  241. // TODO: Check in debug-mode too.
  242. __glibcxx_check_subscript(__pos);
  243. #endif
  244. return _Base::operator[](__pos);
  245. }
  246. using _Base::to_ulong;
  247. #if __cplusplus >= 201103L
  248. using _Base::to_ullong;
  249. #endif
  250. template <typename _CharT, typename _Traits, typename _Alloc>
  251. std::basic_string<_CharT, _Traits, _Alloc>
  252. to_string() const
  253. { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
  254. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  255. // 396. what are characters zero and one.
  256. template<class _CharT, class _Traits, class _Alloc>
  257. std::basic_string<_CharT, _Traits, _Alloc>
  258. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  259. {
  260. return _M_base().template
  261. to_string<_CharT, _Traits, _Alloc>(__zero, __one);
  262. }
  263. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  264. // 434. bitset::to_string() hard to use.
  265. template<typename _CharT, typename _Traits>
  266. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  267. to_string() const
  268. { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
  269. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  270. // 853. to_string needs updating with zero and one.
  271. template<class _CharT, class _Traits>
  272. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  273. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  274. { return to_string<_CharT, _Traits,
  275. std::allocator<_CharT> >(__zero, __one); }
  276. template<typename _CharT>
  277. std::basic_string<_CharT, std::char_traits<_CharT>,
  278. std::allocator<_CharT> >
  279. to_string() const
  280. {
  281. return to_string<_CharT, std::char_traits<_CharT>,
  282. std::allocator<_CharT> >();
  283. }
  284. template<class _CharT>
  285. std::basic_string<_CharT, std::char_traits<_CharT>,
  286. std::allocator<_CharT> >
  287. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  288. {
  289. return to_string<_CharT, std::char_traits<_CharT>,
  290. std::allocator<_CharT> >(__zero, __one);
  291. }
  292. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  293. to_string() const
  294. {
  295. return to_string<char,std::char_traits<char>,std::allocator<char> >();
  296. }
  297. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  298. to_string(char __zero, char __one = '1') const
  299. {
  300. return to_string<char, std::char_traits<char>,
  301. std::allocator<char> >(__zero, __one);
  302. }
  303. using _Base::count;
  304. using _Base::size;
  305. bool
  306. operator==(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
  307. { return _M_base() == __rhs._M_base(); }
  308. #if __cpp_impl_three_way_comparison < 201907L
  309. bool
  310. operator!=(const bitset<_Nb>& __rhs) const _GLIBCXX_NOEXCEPT
  311. { return _M_base() != __rhs._M_base(); }
  312. #endif
  313. using _Base::test;
  314. using _Base::all;
  315. using _Base::any;
  316. using _Base::none;
  317. bitset<_Nb>
  318. operator<<(size_t __pos) const _GLIBCXX_NOEXCEPT
  319. { return bitset<_Nb>(_M_base() << __pos); }
  320. bitset<_Nb>
  321. operator>>(size_t __pos) const _GLIBCXX_NOEXCEPT
  322. { return bitset<_Nb>(_M_base() >> __pos); }
  323. _Base&
  324. _M_base() _GLIBCXX_NOEXCEPT
  325. { return *this; }
  326. const _Base&
  327. _M_base() const _GLIBCXX_NOEXCEPT
  328. { return *this; }
  329. };
  330. template<size_t _Nb>
  331. bitset<_Nb>
  332. operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  333. { return bitset<_Nb>(__x) &= __y; }
  334. template<size_t _Nb>
  335. bitset<_Nb>
  336. operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  337. { return bitset<_Nb>(__x) |= __y; }
  338. template<size_t _Nb>
  339. bitset<_Nb>
  340. operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y) _GLIBCXX_NOEXCEPT
  341. { return bitset<_Nb>(__x) ^= __y; }
  342. template<typename _CharT, typename _Traits, size_t _Nb>
  343. std::basic_istream<_CharT, _Traits>&
  344. operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
  345. { return __is >> __x._M_base(); }
  346. template<typename _CharT, typename _Traits, size_t _Nb>
  347. std::basic_ostream<_CharT, _Traits>&
  348. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  349. const bitset<_Nb>& __x)
  350. { return __os << __x._M_base(); }
  351. } // namespace __debug
  352. #if __cplusplus >= 201103L
  353. // DR 1182.
  354. /// std::hash specialization for bitset.
  355. template<size_t _Nb>
  356. struct hash<__debug::bitset<_Nb>>
  357. : public __hash_base<size_t, __debug::bitset<_Nb>>
  358. {
  359. size_t
  360. operator()(const __debug::bitset<_Nb>& __b) const noexcept
  361. { return std::hash<_GLIBCXX_STD_C::bitset<_Nb>>()(__b._M_base()); }
  362. };
  363. #endif
  364. } // namespace std
  365. #endif