bool_array.cpp

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 // vim:tabstop=4:shiftwidth=4:expandtab:
00003 
00004 /*
00005  * Copyright (C) 2004-2005 Wu Yongwei <adah at users dot sourceforge dot net>
00006  *
00007  * This software is provided 'as-is', without any express or implied
00008  * warranty.  In no event will the authors be held liable for any
00009  * damages arising from the use of this software.
00010  *
00011  * Permission is granted to anyone to use this software for any purpose,
00012  * including commercial applications, and to alter it and redistribute
00013  * it freely, subject to the following restrictions:
00014  *
00015  * 1. The origin of this software must not be misrepresented; you must
00016  *    not claim that you wrote the original software. If you use this
00017  *    software in a product, an acknowledgment in the product
00018  *    documentation would be appreciated but is not required.
00019  * 2. Altered source versions must be plainly marked as such, and must
00020  *    not be misrepresented as being the original software.
00021  * 3. This notice may not be removed or altered from any source
00022  *    distribution.
00023  *
00024  * This file is part of Stones of Nvwa:
00025  *      http://sourceforge.net/projects/nvwa
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, /*   0 */ 1, /*   1 */ 1, /*   2 */ 2, /*   3 */ 1, /*   4 */
00046     2, /*   5 */ 2, /*   6 */ 3, /*   7 */ 1, /*   8 */ 2, /*   9 */
00047     2, /*  10 */ 3, /*  11 */ 2, /*  12 */ 3, /*  13 */ 3, /*  14 */
00048     4, /*  15 */ 1, /*  16 */ 2, /*  17 */ 2, /*  18 */ 3, /*  19 */
00049     2, /*  20 */ 3, /*  21 */ 3, /*  22 */ 4, /*  23 */ 2, /*  24 */
00050     3, /*  25 */ 3, /*  26 */ 4, /*  27 */ 3, /*  28 */ 4, /*  29 */
00051     4, /*  30 */ 5, /*  31 */ 1, /*  32 */ 2, /*  33 */ 2, /*  34 */
00052     3, /*  35 */ 2, /*  36 */ 3, /*  37 */ 3, /*  38 */ 4, /*  39 */
00053     2, /*  40 */ 3, /*  41 */ 3, /*  42 */ 4, /*  43 */ 3, /*  44 */
00054     4, /*  45 */ 4, /*  46 */ 5, /*  47 */ 2, /*  48 */ 3, /*  49 */
00055     3, /*  50 */ 4, /*  51 */ 3, /*  52 */ 4, /*  53 */ 4, /*  54 */
00056     5, /*  55 */ 3, /*  56 */ 4, /*  57 */ 4, /*  58 */ 5, /*  59 */
00057     4, /*  60 */ 5, /*  61 */ 5, /*  62 */ 6, /*  63 */ 1, /*  64 */
00058     2, /*  65 */ 2, /*  66 */ 3, /*  67 */ 2, /*  68 */ 3, /*  69 */
00059     3, /*  70 */ 4, /*  71 */ 2, /*  72 */ 3, /*  73 */ 3, /*  74 */
00060     4, /*  75 */ 3, /*  76 */ 4, /*  77 */ 4, /*  78 */ 5, /*  79 */
00061     2, /*  80 */ 3, /*  81 */ 3, /*  82 */ 4, /*  83 */ 3, /*  84 */
00062     4, /*  85 */ 4, /*  86 */ 5, /*  87 */ 3, /*  88 */ 4, /*  89 */
00063     4, /*  90 */ 5, /*  91 */ 4, /*  92 */ 5, /*  93 */ 5, /*  94 */
00064     6, /*  95 */ 2, /*  96 */ 3, /*  97 */ 3, /*  98 */ 4, /*  99 */
00065     3, /* 100 */ 4, /* 101 */ 4, /* 102 */ 5, /* 103 */ 3, /* 104 */
00066     4, /* 105 */ 4, /* 106 */ 5, /* 107 */ 4, /* 108 */ 5, /* 109 */
00067     5, /* 110 */ 6, /* 111 */ 3, /* 112 */ 4, /* 113 */ 4, /* 114 */
00068     5, /* 115 */ 4, /* 116 */ 5, /* 117 */ 5, /* 118 */ 6, /* 119 */
00069     4, /* 120 */ 5, /* 121 */ 5, /* 122 */ 6, /* 123 */ 5, /* 124 */
00070     6, /* 125 */ 6, /* 126 */ 7, /* 127 */ 1, /* 128 */ 2, /* 129 */
00071     2, /* 130 */ 3, /* 131 */ 2, /* 132 */ 3, /* 133 */ 3, /* 134 */
00072     4, /* 135 */ 2, /* 136 */ 3, /* 137 */ 3, /* 138 */ 4, /* 139 */
00073     3, /* 140 */ 4, /* 141 */ 4, /* 142 */ 5, /* 143 */ 2, /* 144 */
00074     3, /* 145 */ 3, /* 146 */ 4, /* 147 */ 3, /* 148 */ 4, /* 149 */
00075     4, /* 150 */ 5, /* 151 */ 3, /* 152 */ 4, /* 153 */ 4, /* 154 */
00076     5, /* 155 */ 4, /* 156 */ 5, /* 157 */ 5, /* 158 */ 6, /* 159 */
00077     2, /* 160 */ 3, /* 161 */ 3, /* 162 */ 4, /* 163 */ 3, /* 164 */
00078     4, /* 165 */ 4, /* 166 */ 5, /* 167 */ 3, /* 168 */ 4, /* 169 */
00079     4, /* 170 */ 5, /* 171 */ 4, /* 172 */ 5, /* 173 */ 5, /* 174 */
00080     6, /* 175 */ 3, /* 176 */ 4, /* 177 */ 4, /* 178 */ 5, /* 179 */
00081     4, /* 180 */ 5, /* 181 */ 5, /* 182 */ 6, /* 183 */ 4, /* 184 */
00082     5, /* 185 */ 5, /* 186 */ 6, /* 187 */ 5, /* 188 */ 6, /* 189 */
00083     6, /* 190 */ 7, /* 191 */ 2, /* 192 */ 3, /* 193 */ 3, /* 194 */
00084     4, /* 195 */ 3, /* 196 */ 4, /* 197 */ 4, /* 198 */ 5, /* 199 */
00085     3, /* 200 */ 4, /* 201 */ 4, /* 202 */ 5, /* 203 */ 4, /* 204 */
00086     5, /* 205 */ 5, /* 206 */ 6, /* 207 */ 3, /* 208 */ 4, /* 209 */
00087     4, /* 210 */ 5, /* 211 */ 4, /* 212 */ 5, /* 213 */ 5, /* 214 */
00088     6, /* 215 */ 4, /* 216 */ 5, /* 217 */ 5, /* 218 */ 6, /* 219 */
00089     5, /* 220 */ 6, /* 221 */ 6, /* 222 */ 7, /* 223 */ 3, /* 224 */
00090     4, /* 225 */ 4, /* 226 */ 5, /* 227 */ 4, /* 228 */ 5, /* 229 */
00091     5, /* 230 */ 6, /* 231 */ 4, /* 232 */ 5, /* 233 */ 5, /* 234 */
00092     6, /* 235 */ 5, /* 236 */ 6, /* 237 */ 6, /* 238 */ 7, /* 239 */
00093     4, /* 240 */ 5, /* 241 */ 5, /* 242 */ 6, /* 243 */ 5, /* 244 */
00094     6, /* 245 */ 6, /* 246 */ 7, /* 247 */ 5, /* 248 */ 6, /* 249 */
00095     6, /* 250 */ 7, /* 251 */ 6, /* 252 */ 7, /* 253 */ 7, /* 254 */
00096     8  /* 255 */
00097 }; // End _S_bit_count
00098 
00108 bool bool_array::create(unsigned long __size)
00109 {
00110     if (__size == 0)
00111         return false;
00112     // Will be optimized away by a decent compiler if ULONG_MAX == UINT_MAX
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     // Uses malloc/free instead of new/delete to avoid exception handling
00122     // differences between compilers
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 }

Generated on Wed May 5 00:06:28 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6