bool_array.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  2. // vim:tabstop=4:shiftwidth=4:expandtab:
  3. /*
  4. * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
  5. *
  6. * This software is provided 'as-is', without any express or implied
  7. * warranty. In no event will the authors be held liable for any
  8. * damages arising from the use of this software.
  9. *
  10. * Permission is granted to anyone to use this software for any purpose,
  11. * including commercial applications, and to alter it and redistribute
  12. * it freely, subject to the following restrictions:
  13. *
  14. * 1. The origin of this software must not be misrepresented; you must
  15. * not claim that you wrote the original software. If you use this
  16. * software in a product, an acknowledgement in the product
  17. * documentation would be appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must
  19. * not be misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source
  21. * distribution.
  22. *
  23. * This file is part of Stones of Nvwa:
  24. * http://sourceforge.net/projects/nvwa
  25. *
  26. */
  27. /*!
  28. * \file bool_array.h
  29. * \ingroup QxMemLeak
  30. *
  31. * Header file for class bool_array (packed boolean array).
  32. *
  33. * \version 3.1, 2005/08/25
  34. * \author Wu Yongwei
  35. *
  36. */
  37. #ifndef QT_NO_DEBUG
  38. #ifndef _QX_MODE_RELEASE
  39. #if _QX_USE_MEM_LEAK_DETECTION
  40. #ifndef _BOOL_ARRAY_H
  41. #define _BOOL_ARRAY_H
  42. #ifdef _MSC_VER
  43. #pragma once
  44. #endif
  45. #include <assert.h> // assert
  46. #include <stdlib.h> // exit, free, and NULL
  47. #include <new> // std::bad_alloc
  48. #include <stdexcept> // std::out_of_range
  49. #include <string> // for exception constructors
  50. namespace qx {
  51. namespace memory {
  52. #ifndef _BYTE_DEFINED
  53. #define _BYTE_DEFINED
  54. typedef unsigned char BYTE;
  55. #endif // !_BYTE_DEFINED
  56. /**
  57. * Class to represent a packed boolean array.
  58. *
  59. * This was first written in April 1995, before I knew of any existing
  60. * implementation of this kind of classes. Of course, the C++ Standard
  61. * Template Library now demands an implementation of packed boolean
  62. * array as `vector&lt;bool>', but the code here should still be useful
  63. * for the following three reasons: (1) STL support of MSVC 6 did not
  64. * implement this specialization (nor did it have a `bit_vector'); (2) I
  65. * incorporated some useful member functions from the STL bitset into
  66. * this `bool_array', including `reset', `set', `flip', and `count';
  67. * (3) In my tests under MSVC 6 and GCC 2.95.3/3.2.3 my code is really
  68. * FASTER than vector&lt;bool> or the normal boolean array.
  69. */
  70. class QX_DLL_EXPORT bool_array
  71. {
  72. /** Class to represent a reference to an array element. */
  73. class QX_DLL_EXPORT _Element
  74. {
  75. public:
  76. _Element(BYTE* __ptr, unsigned long __idx);
  77. bool operator=(bool ___value);
  78. operator bool() const;
  79. private:
  80. BYTE* _M_byte_ptr;
  81. size_t _M_byte_idx;
  82. size_t _M_bit_idx;
  83. };
  84. public:
  85. bool_array() : _M_byte_ptr(NULL), _M_length(0) {}
  86. explicit bool_array(unsigned long __size);
  87. ~bool_array() { if (_M_byte_ptr != NULL) free(_M_byte_ptr); }
  88. bool create(unsigned long __size);
  89. void initialize(bool ___value);
  90. // Using unsigned type here can increase performance!
  91. _Element operator[](unsigned long __idx);
  92. bool at(unsigned long __idx) const;
  93. void reset(unsigned long __idx);
  94. void set(unsigned long __idx);
  95. unsigned long size() const { return _M_length; }
  96. unsigned long count() const;
  97. unsigned long count(unsigned long __beg, unsigned long __end) const;
  98. void flip();
  99. private:
  100. BYTE* _M_byte_ptr;
  101. unsigned long _M_length;
  102. static BYTE _S_bit_count[256];
  103. };
  104. /* Inline functions */
  105. /**
  106. * Constructs a reference to an array element.
  107. *
  108. * @param __ptr pointer to the interal boolean data
  109. * @param __idx index of the array element to access
  110. */
  111. inline bool_array::_Element::_Element(BYTE* __ptr, unsigned long __idx)
  112. {
  113. _M_byte_ptr = __ptr;
  114. _M_byte_idx = (size_t)(__idx / 8);
  115. _M_bit_idx = (size_t)(__idx % 8);
  116. }
  117. /**
  118. * Assigns a new boolean value to an array element.
  119. *
  120. * @param ___value the new boolean value
  121. * @return the assigned boolean value
  122. */
  123. inline bool bool_array::_Element::operator=(bool ___value)
  124. {
  125. if (___value)
  126. *(_M_byte_ptr + _M_byte_idx) |= 1 << _M_bit_idx;
  127. else
  128. *(_M_byte_ptr + _M_byte_idx) &= ~(1 << _M_bit_idx);
  129. return ___value;
  130. }
  131. /**
  132. * Reads the boolean value from an array element.
  133. *
  134. * @return the boolean value of the accessed array element
  135. */
  136. inline bool_array::_Element::operator bool() const
  137. {
  138. return *(_M_byte_ptr + _M_byte_idx) & (1 << _M_bit_idx) ? true : false;
  139. }
  140. /**
  141. * Constructs the packed boolean array with a specific size.
  142. *
  143. * @param __size size of the array
  144. * @throw std::out_of_range if \a __size equals \c 0
  145. * @throw std::bad_alloc if memory is insufficient
  146. */
  147. inline bool_array::bool_array(unsigned long __size)
  148. : _M_byte_ptr(NULL), _M_length(0)
  149. {
  150. if (__size == 0)
  151. throw std::out_of_range("invalid bool_array size");
  152. if (!create(__size))
  153. throw std::bad_alloc();
  154. }
  155. /**
  156. * Creates a reference to an array element.
  157. *
  158. * @param __idx index of the array element to access
  159. */
  160. inline bool_array::_Element bool_array::operator[](unsigned long __idx)
  161. {
  162. assert(_M_byte_ptr);
  163. assert(__idx < _M_length);
  164. return _Element(_M_byte_ptr, __idx);
  165. }
  166. /**
  167. * Reads the boolean value of an array element via an index.
  168. *
  169. * @param __idx index of the array element to access
  170. * @return the boolean value of the accessed array element
  171. * @throw std::out_of_range when the index is too big
  172. */
  173. inline bool bool_array::at(unsigned long __idx) const
  174. {
  175. size_t __byte_idx, __bit_idx;
  176. if (__idx >= _M_length)
  177. throw std::out_of_range("invalid bool_array subscript");
  178. __byte_idx = (size_t)(__idx / 8);
  179. __bit_idx = (size_t)(__idx % 8);
  180. return *(_M_byte_ptr + __byte_idx) & (1 << __bit_idx) ? true : false;
  181. }
  182. /**
  183. * Resets an array element to \c false via an index.
  184. *
  185. * @param __idx index of the array element to access
  186. * @throw std::out_of_range when the index is too big
  187. */
  188. inline void bool_array::reset(unsigned long __idx)
  189. {
  190. size_t __byte_idx, __bit_idx;
  191. if (__idx >= _M_length)
  192. throw std::out_of_range("invalid bool_array subscript");
  193. __byte_idx = (size_t)(__idx / 8);
  194. __bit_idx = (size_t)(__idx % 8);
  195. *(_M_byte_ptr + __byte_idx) &= ~(1 << __bit_idx);
  196. }
  197. /**
  198. * Sets an array element to \c true via an index.
  199. *
  200. * @param __idx index of the array element to access
  201. * @throw std::out_of_range when the index is too big
  202. */
  203. inline void bool_array::set(unsigned long __idx)
  204. {
  205. size_t __byte_idx, __bit_idx;
  206. if (__idx >= _M_length)
  207. throw std::out_of_range("invalid bool_array subscript");
  208. __byte_idx = (size_t)(__idx / 8);
  209. __bit_idx = (size_t)(__idx % 8);
  210. *(_M_byte_ptr + __byte_idx) |= 1 << __bit_idx;
  211. }
  212. } // namespace memory
  213. } // namespace qx
  214. #endif // _BOOL_ARRAY_H
  215. #endif // _QX_USE_MEM_LEAK_DETECTION
  216. #endif // _QX_MODE_RELEASE
  217. #endif // QT_NO_DEBUG