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 #include <limits.h>
00040 #include <string.h>
00041 #include "bool_array.h"
00042
00043 BYTE bool_array::_S_bit_count[256] =
00044 {
00045 0, 1, 1, 2, 1,
00046 2, 2, 3, 1, 2,
00047 2, 3, 2, 3, 3,
00048 4, 1, 2, 2, 3,
00049 2, 3, 3, 4, 2,
00050 3, 3, 4, 3, 4,
00051 4, 5, 1, 2, 2,
00052 3, 2, 3, 3, 4,
00053 2, 3, 3, 4, 3,
00054 4, 4, 5, 2, 3,
00055 3, 4, 3, 4, 4,
00056 5, 3, 4, 4, 5,
00057 4, 5, 5, 6, 1,
00058 2, 2, 3, 2, 3,
00059 3, 4, 2, 3, 3,
00060 4, 3, 4, 4, 5,
00061 2, 3, 3, 4, 3,
00062 4, 4, 5, 3, 4,
00063 4, 5, 4, 5, 5,
00064 6, 2, 3, 3, 4,
00065 3, 4, 4, 5, 3,
00066 4, 4, 5, 4, 5,
00067 5, 6, 3, 4, 4,
00068 5, 4, 5, 5, 6,
00069 4, 5, 5, 6, 5,
00070 6, 6, 7, 1, 2,
00071 2, 3, 2, 3, 3,
00072 4, 2, 3, 3, 4,
00073 3, 4, 4, 5, 2,
00074 3, 3, 4, 3, 4,
00075 4, 5, 3, 4, 4,
00076 5, 4, 5, 5, 6,
00077 2, 3, 3, 4, 3,
00078 4, 4, 5, 3, 4,
00079 4, 5, 4, 5, 5,
00080 6, 3, 4, 4, 5,
00081 4, 5, 5, 6, 4,
00082 5, 5, 6, 5, 6,
00083 6, 7, 2, 3, 3,
00084 4, 3, 4, 4, 5,
00085 3, 4, 4, 5, 4,
00086 5, 5, 6, 3, 4,
00087 4, 5, 4, 5, 5,
00088 6, 4, 5, 5, 6,
00089 5, 6, 6, 7, 3,
00090 4, 4, 5, 4, 5,
00091 5, 6, 4, 5, 5,
00092 6, 5, 6, 6, 7,
00093 4, 5, 5, 6, 5,
00094 6, 6, 7, 5, 6,
00095 6, 7, 6, 7, 7,
00096 8
00097 };
00098
00108 bool bool_array::create(unsigned long __size)
00109 {
00110 if (__size == 0)
00111 return false;
00112
00113 if (ULONG_MAX > UINT_MAX && ((__size - 1) / 8 + 1) > UINT_MAX)
00114 return false;
00115
00116 size_t __byte_cnt = (size_t)((__size - 1) / 8 + 1);
00117 if (_M_byte_ptr)
00118 free(_M_byte_ptr);
00119 _M_length = 0;
00120
00121
00122
00123 if (!(_M_byte_ptr = (BYTE*)malloc(__byte_cnt)))
00124 return false;
00125
00126 _M_length = __size;
00127 return true;
00128 }
00129
00135 void bool_array::initialize(bool __value)
00136 {
00137 assert(_M_byte_ptr);
00138 size_t __byte_cnt = (size_t)((_M_length - 1) / 8) + 1;
00139 memset(_M_byte_ptr, __value ? ~0 : 0, __byte_cnt);
00140 if (__value)
00141 {
00142 int __valid_bits_in_last_byte = (_M_length - 1) % 8 + 1;
00143 _M_byte_ptr[__byte_cnt - 1] &= ~(~0 << __valid_bits_in_last_byte);
00144 }
00145 }
00146
00152 unsigned long bool_array::count() const
00153 {
00154 assert(_M_byte_ptr);
00155 unsigned long __true_cnt = 0;
00156 size_t __byte_cnt = (size_t)((_M_length - 1) / 8) + 1;
00157 for (size_t __i = 0; __i < __byte_cnt; ++__i)
00158 __true_cnt += _S_bit_count[_M_byte_ptr[__i]];
00159 return __true_cnt;
00160 }
00161
00169 unsigned long bool_array::count(unsigned long __beg, unsigned long __end) const
00170 {
00171 assert(_M_byte_ptr);
00172 unsigned long __true_cnt = 0;
00173 size_t __byte_idx_beg, __byte_idx_end;
00174 BYTE __byte_val;
00175
00176 if (__beg >= __end)
00177 return 0;
00178 if (__end > _M_length)
00179 throw std::out_of_range("invalid bool_array subscript");
00180 --__end;
00181
00182 __byte_idx_beg = (size_t)(__beg / 8);
00183 __byte_val = _M_byte_ptr[__byte_idx_beg];
00184 __byte_val &= ~0 << (__beg % 8);
00185
00186 __byte_idx_end = (size_t)(__end / 8);
00187 if (__byte_idx_beg < __byte_idx_end)
00188 {
00189 __true_cnt = _S_bit_count[__byte_val];
00190 __byte_val = _M_byte_ptr[__byte_idx_end];
00191 }
00192 __byte_val &= ~(~0 << (__end % 8 + 1));
00193 __true_cnt += _S_bit_count[__byte_val];
00194
00195 for (++__byte_idx_beg; __byte_idx_beg < __byte_idx_end; ++__byte_idx_beg)
00196 __true_cnt += _S_bit_count[_M_byte_ptr[__byte_idx_beg]];
00197 return __true_cnt;
00198 }
00199
00203 void bool_array::flip()
00204 {
00205 assert(_M_byte_ptr);
00206 size_t __byte_cnt = (size_t)((_M_length - 1) / 8) + 1;
00207 for (size_t __i = 0; __i < __byte_cnt; ++__i)
00208 _M_byte_ptr[__i] = ~_M_byte_ptr[__i];
00209 int __valid_bits_in_last_byte = (_M_length - 1) % 8 + 1;
00210 _M_byte_ptr[__byte_cnt - 1] &= ~(~0 << __valid_bits_in_last_byte);
00211 }