bitset 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // Debugging bitset implementation -*- C++ -*-
  2. // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
  3. // Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. /** @file debug/bitset
  22. * This file is a GNU debug extension to the Standard C++ Library.
  23. */
  24. #ifndef _GLIBCXX_DEBUG_BITSET
  25. #define _GLIBCXX_DEBUG_BITSET
  26. #include <bitset>
  27. #include <debug/safe_sequence.h>
  28. #include <debug/safe_iterator.h>
  29. namespace std
  30. {
  31. namespace __debug
  32. {
  33. template<size_t _Nb>
  34. class bitset
  35. : public _GLIBCXX_STD_D::bitset<_Nb>,
  36. public __gnu_debug::_Safe_sequence_base
  37. {
  38. typedef _GLIBCXX_STD_D::bitset<_Nb> _Base;
  39. typedef __gnu_debug::_Safe_sequence_base _Safe_base;
  40. public:
  41. // bit reference:
  42. class reference
  43. : private _Base::reference, public __gnu_debug::_Safe_iterator_base
  44. {
  45. typedef typename _Base::reference _Base_ref;
  46. friend class bitset;
  47. reference();
  48. reference(const _Base_ref& __base, bitset* __seq)
  49. : _Base_ref(__base), _Safe_iterator_base(__seq, false)
  50. { }
  51. public:
  52. reference(const reference& __x)
  53. : _Base_ref(__x), _Safe_iterator_base(__x, false)
  54. { }
  55. reference&
  56. operator=(bool __x)
  57. {
  58. _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
  59. _M_message(__gnu_debug::__msg_bad_bitset_write)
  60. ._M_iterator(*this));
  61. *static_cast<_Base_ref*>(this) = __x;
  62. return *this;
  63. }
  64. reference&
  65. operator=(const reference& __x)
  66. {
  67. _GLIBCXX_DEBUG_VERIFY(! __x._M_singular(),
  68. _M_message(__gnu_debug::__msg_bad_bitset_read)
  69. ._M_iterator(__x));
  70. _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
  71. _M_message(__gnu_debug::__msg_bad_bitset_write)
  72. ._M_iterator(*this));
  73. *static_cast<_Base_ref*>(this) = __x;
  74. return *this;
  75. }
  76. bool
  77. operator~() const
  78. {
  79. _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
  80. _M_message(__gnu_debug::__msg_bad_bitset_read)
  81. ._M_iterator(*this));
  82. return ~(*static_cast<const _Base_ref*>(this));
  83. }
  84. operator bool() const
  85. {
  86. _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
  87. _M_message(__gnu_debug::__msg_bad_bitset_read)
  88. ._M_iterator(*this));
  89. return *static_cast<const _Base_ref*>(this);
  90. }
  91. reference&
  92. flip()
  93. {
  94. _GLIBCXX_DEBUG_VERIFY(! this->_M_singular(),
  95. _M_message(__gnu_debug::__msg_bad_bitset_flip)
  96. ._M_iterator(*this));
  97. _Base_ref::flip();
  98. return *this;
  99. }
  100. };
  101. // 23.3.5.1 constructors:
  102. bitset() : _Base() { }
  103. bitset(unsigned long __val) : _Base(__val) { }
  104. template<typename _CharT, typename _Traits, typename _Alloc>
  105. explicit
  106. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
  107. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  108. __pos = 0,
  109. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  110. __n = (std::basic_string<_CharT, _Traits, _Alloc>::npos))
  111. : _Base(__str, __pos, __n) { }
  112. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  113. // 396. what are characters zero and one.
  114. template<class _CharT, class _Traits, class _Alloc>
  115. bitset(const std::basic_string<_CharT, _Traits, _Alloc>& __str,
  116. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  117. __pos,
  118. typename std::basic_string<_CharT, _Traits, _Alloc>::size_type
  119. __n,
  120. _CharT __zero, _CharT __one = _CharT('1'))
  121. : _Base(__str, __pos, __n, __zero, __one) { }
  122. bitset(const _Base& __x) : _Base(__x), _Safe_base() { }
  123. // 23.3.5.2 bitset operations:
  124. bitset<_Nb>&
  125. operator&=(const bitset<_Nb>& __rhs)
  126. {
  127. _M_base() &= __rhs;
  128. return *this;
  129. }
  130. bitset<_Nb>&
  131. operator|=(const bitset<_Nb>& __rhs)
  132. {
  133. _M_base() |= __rhs;
  134. return *this;
  135. }
  136. bitset<_Nb>&
  137. operator^=(const bitset<_Nb>& __rhs)
  138. {
  139. _M_base() ^= __rhs;
  140. return *this;
  141. }
  142. bitset<_Nb>&
  143. operator<<=(size_t __pos)
  144. {
  145. _M_base() <<= __pos;
  146. return *this;
  147. }
  148. bitset<_Nb>&
  149. operator>>=(size_t __pos)
  150. {
  151. _M_base() >>= __pos;
  152. return *this;
  153. }
  154. bitset<_Nb>&
  155. set()
  156. {
  157. _Base::set();
  158. return *this;
  159. }
  160. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  161. // 186. bitset::set() second parameter should be bool
  162. bitset<_Nb>&
  163. set(size_t __pos, bool __val = true)
  164. {
  165. _Base::set(__pos, __val);
  166. return *this;
  167. }
  168. bitset<_Nb>&
  169. reset()
  170. {
  171. _Base::reset();
  172. return *this;
  173. }
  174. bitset<_Nb>&
  175. reset(size_t __pos)
  176. {
  177. _Base::reset(__pos);
  178. return *this;
  179. }
  180. bitset<_Nb> operator~() const { return bitset(~_M_base()); }
  181. bitset<_Nb>&
  182. flip()
  183. {
  184. _Base::flip();
  185. return *this;
  186. }
  187. bitset<_Nb>&
  188. flip(size_t __pos)
  189. {
  190. _Base::flip(__pos);
  191. return *this;
  192. }
  193. // element access:
  194. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  195. // 11. Bitset minor problems
  196. reference
  197. operator[](size_t __pos)
  198. {
  199. __glibcxx_check_subscript(__pos);
  200. return reference(_M_base()[__pos], this);
  201. }
  202. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  203. // 11. Bitset minor problems
  204. bool
  205. operator[](size_t __pos) const
  206. {
  207. __glibcxx_check_subscript(__pos);
  208. return _M_base()[__pos];
  209. }
  210. using _Base::to_ulong;
  211. template <typename _CharT, typename _Traits, typename _Alloc>
  212. std::basic_string<_CharT, _Traits, _Alloc>
  213. to_string() const
  214. { return _M_base().template to_string<_CharT, _Traits, _Alloc>(); }
  215. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  216. // 396. what are characters zero and one.
  217. template<class _CharT, class _Traits, class _Alloc>
  218. std::basic_string<_CharT, _Traits, _Alloc>
  219. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  220. {
  221. return _M_base().template
  222. to_string<_CharT, _Traits, _Alloc>(__zero, __one);
  223. }
  224. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  225. // 434. bitset::to_string() hard to use.
  226. template<typename _CharT, typename _Traits>
  227. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  228. to_string() const
  229. { return to_string<_CharT, _Traits, std::allocator<_CharT> >(); }
  230. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  231. // 853. to_string needs updating with zero and one.
  232. template<class _CharT, class _Traits>
  233. std::basic_string<_CharT, _Traits, std::allocator<_CharT> >
  234. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  235. { return to_string<_CharT, _Traits,
  236. std::allocator<_CharT> >(__zero, __one); }
  237. template<typename _CharT>
  238. std::basic_string<_CharT, std::char_traits<_CharT>,
  239. std::allocator<_CharT> >
  240. to_string() const
  241. {
  242. return to_string<_CharT, std::char_traits<_CharT>,
  243. std::allocator<_CharT> >();
  244. }
  245. template<class _CharT>
  246. std::basic_string<_CharT, std::char_traits<_CharT>,
  247. std::allocator<_CharT> >
  248. to_string(_CharT __zero, _CharT __one = _CharT('1')) const
  249. {
  250. return to_string<_CharT, std::char_traits<_CharT>,
  251. std::allocator<_CharT> >(__zero, __one);
  252. }
  253. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  254. to_string() const
  255. {
  256. return to_string<char,std::char_traits<char>,std::allocator<char> >();
  257. }
  258. std::basic_string<char, std::char_traits<char>, std::allocator<char> >
  259. to_string(char __zero, char __one = '1') const
  260. {
  261. return to_string<char, std::char_traits<char>,
  262. std::allocator<char> >(__zero, __one);
  263. }
  264. using _Base::count;
  265. using _Base::size;
  266. bool
  267. operator==(const bitset<_Nb>& __rhs) const
  268. { return _M_base() == __rhs; }
  269. bool
  270. operator!=(const bitset<_Nb>& __rhs) const
  271. { return _M_base() != __rhs; }
  272. using _Base::test;
  273. using _Base::all;
  274. using _Base::any;
  275. using _Base::none;
  276. bitset<_Nb>
  277. operator<<(size_t __pos) const
  278. { return bitset<_Nb>(_M_base() << __pos); }
  279. bitset<_Nb>
  280. operator>>(size_t __pos) const
  281. { return bitset<_Nb>(_M_base() >> __pos); }
  282. _Base&
  283. _M_base() { return *this; }
  284. const _Base&
  285. _M_base() const { return *this; }
  286. };
  287. template<size_t _Nb>
  288. bitset<_Nb>
  289. operator&(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
  290. { return bitset<_Nb>(__x) &= __y; }
  291. template<size_t _Nb>
  292. bitset<_Nb>
  293. operator|(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
  294. { return bitset<_Nb>(__x) |= __y; }
  295. template<size_t _Nb>
  296. bitset<_Nb>
  297. operator^(const bitset<_Nb>& __x, const bitset<_Nb>& __y)
  298. { return bitset<_Nb>(__x) ^= __y; }
  299. template<typename _CharT, typename _Traits, size_t _Nb>
  300. std::basic_istream<_CharT, _Traits>&
  301. operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
  302. { return __is >> __x._M_base(); }
  303. template<typename _CharT, typename _Traits, size_t _Nb>
  304. std::basic_ostream<_CharT, _Traits>&
  305. operator<<(std::basic_ostream<_CharT, _Traits>& __os,
  306. const bitset<_Nb>& __x)
  307. { return __os << __x._M_base(); }
  308. } // namespace __debug
  309. } // namespace std
  310. #endif