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 #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> // assert 00048 #include <stdlib.h> // exit, free, and NULL 00049 #include <new> // std::bad_alloc 00050 #include <stdexcept> // std::out_of_range 00051 #include <string> // for exception constructors 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 // Using unsigned type here can increase performance! 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 /* Inline functions */ 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
1.5.6