00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00039 #ifndef _BOOL_ARRAY_H
00040 #define _BOOL_ARRAY_H
00041
00042 #ifndef _BYTE_DEFINED
00043 #define _BYTE_DEFINED
00044 typedef unsigned char BYTE;
00045 #endif // !_BYTE_DEFINED
00046
00047 #include <assert.h>
00048 #include <stdlib.h>
00049 #include <new>
00050 #include <stdexcept>
00051 #include <string>
00052
00067 class bool_array
00068 {
00070 class _Element
00071 {
00072 public:
00073 _Element(BYTE* __ptr, unsigned long __idx);
00074 bool operator=(bool __value);
00075 operator bool() const;
00076 private:
00077 BYTE* _M_byte_ptr;
00078 size_t _M_byte_idx;
00079 size_t _M_bit_idx;
00080 };
00081
00082 public:
00083 bool_array() : _M_byte_ptr(NULL), _M_length(0) {}
00084 explicit bool_array(unsigned long __size);
00085 ~bool_array() { if (_M_byte_ptr != NULL) free(_M_byte_ptr); }
00086
00087 bool create(unsigned long __size);
00088 void initialize(bool __value);
00089
00090
00091 _Element operator[](unsigned long __idx);
00092 bool at(unsigned long __idx) const;
00093 void reset(unsigned long __idx);
00094 void set(unsigned long __idx);
00095
00096 unsigned long size() const { return _M_length; }
00097 unsigned long count() const;
00098 unsigned long count(unsigned long __beg, unsigned long __end) const;
00099 void flip();
00100
00101 private:
00102 BYTE* _M_byte_ptr;
00103 unsigned long _M_length;
00104 static BYTE _S_bit_count[256];
00105 };
00106
00107
00108
00109
00116 inline bool_array::_Element::_Element(BYTE* __ptr, unsigned long __idx)
00117 {
00118 _M_byte_ptr = __ptr;
00119 _M_byte_idx = (size_t)(__idx / 8);
00120 _M_bit_idx = (size_t)(__idx % 8);
00121 }
00122
00129 inline bool bool_array::_Element::operator=(bool __value)
00130 {
00131 if (__value)
00132 *(_M_byte_ptr + _M_byte_idx) |= 1 << _M_bit_idx;
00133 else
00134 *(_M_byte_ptr + _M_byte_idx) &= ~(1 << _M_bit_idx);
00135 return __value;
00136 }
00137
00143 inline bool_array::_Element::operator bool() const
00144 {
00145 return *(_M_byte_ptr + _M_byte_idx) & (1 << _M_bit_idx) ? true : false;
00146 }
00147
00155 inline bool_array::bool_array(unsigned long __size)
00156 : _M_byte_ptr(NULL), _M_length(0)
00157 {
00158 if (__size == 0)
00159 throw std::out_of_range("invalid bool_array size");
00160
00161 if (!create(__size))
00162 throw std::bad_alloc();
00163 }
00164
00170 inline bool_array::_Element bool_array::operator[](unsigned long __idx)
00171 {
00172 assert(_M_byte_ptr);
00173 assert(__idx < _M_length);
00174 return _Element(_M_byte_ptr, __idx);
00175 }
00176
00184 inline bool bool_array::at(unsigned long __idx) const
00185 {
00186 size_t __byte_idx, __bit_idx;
00187 if (__idx >= _M_length)
00188 throw std::out_of_range("invalid bool_array subscript");
00189 __byte_idx = (size_t)(__idx / 8);
00190 __bit_idx = (size_t)(__idx % 8);
00191 return *(_M_byte_ptr + __byte_idx) & (1 << __bit_idx) ? true : false;
00192 }
00193
00200 inline void bool_array::reset(unsigned long __idx)
00201 {
00202 size_t __byte_idx, __bit_idx;
00203 if (__idx >= _M_length)
00204 throw std::out_of_range("invalid bool_array subscript");
00205 __byte_idx = (size_t)(__idx / 8);
00206 __bit_idx = (size_t)(__idx % 8);
00207 *(_M_byte_ptr + __byte_idx) &= ~(1 << __bit_idx);
00208 }
00209
00216 inline void bool_array::set(unsigned long __idx)
00217 {
00218 size_t __byte_idx, __bit_idx;
00219 if (__idx >= _M_length)
00220 throw std::out_of_range("invalid bool_array subscript");
00221 __byte_idx = (size_t)(__idx / 8);
00222 __bit_idx = (size_t)(__idx % 8);
00223 *(_M_byte_ptr + __byte_idx) |= 1 << __bit_idx;
00224 }
00225
00226 #endif // _BOOL_ARRAY_H