|
bitary2d.h00001 // A two-dimensional array of bits. 00002 // 00003 // Copyright 2000 Andrew Kirmse. All rights reserved. 00004 // 00005 // Permission is granted to use this code for any purpose, as long as this 00006 // copyright message remains intact. 00007 00008 #ifndef _csBitArray2D_H 00009 #define _csBitArray2D_H 00010 00011 #include "bitarray.h" 00012 00013 class csBitArray2D : public csBitArray 00014 { 00015 typedef csBitArray super; 00016 00017 private: 00018 unsigned mWidth; 00019 00020 public: 00021 00022 // 00023 // Array proxy (for operator[]) 00024 // 00025 00026 class ArrayProxy 00027 { 00028 private: 00029 csBitArray2D &mArray; 00030 unsigned mPos; // We are a proxy for this row of the array 00031 public: 00032 ArrayProxy(csBitArray2D &array, unsigned pos): 00033 mArray(array), mPos(pos) 00034 {} 00035 00036 super::BitProxy operator[](unsigned pos) const 00037 { 00038 return super::BitProxy(mArray, mPos * mArray.mWidth + pos); 00039 } 00040 }; 00041 00042 friend class ArrayProxy; 00043 00044 // 00045 // Constructors 00046 // 00047 00048 csBitArray2D(unsigned dim1, unsigned dim2) : super(dim1 * dim2) 00049 { 00050 mWidth = dim2; 00051 } 00052 00053 csBitArray2D(const csBitArray2D &that) : super(that) 00054 { 00055 mWidth = that.mWidth; 00056 } 00057 00058 // 00059 // Operators 00060 // 00061 00062 csBitArray2D &operator=(const csBitArray2D &that) 00063 { 00064 super::operator=(that); 00065 mWidth = that.mWidth; 00066 return *this; 00067 } 00068 00069 ArrayProxy operator[](unsigned pos) 00070 { 00071 return ArrayProxy(*this, pos); 00072 } 00073 00074 const ArrayProxy operator[](unsigned pos) const 00075 { 00076 return ArrayProxy(CONST_CAST(csBitArray2D&,*this), pos); 00077 } 00078 00079 bool operator==(const csBitArray2D &that) const 00080 { 00081 return super::operator==(that); 00082 } 00083 00084 bool operator!=(const csBitArray2D &that) const 00085 { 00086 return !(*this == that); 00087 } 00088 00089 csBitArray2D &operator&=(const csBitArray2D &that) 00090 { 00091 super::operator&=(that); 00092 return *this; 00093 } 00094 00095 csBitArray2D operator|=(const csBitArray2D &that) 00096 { 00097 super::operator|=(that); 00098 return *this; 00099 } 00100 00101 csBitArray2D operator^=(const csBitArray2D &that) 00102 { 00103 super::operator^=(that); 00104 return *this; 00105 } 00106 00107 csBitArray2D operator~() const 00108 { 00109 return csBitArray2D(*this).FlipAllBits(); 00110 } 00111 00112 friend csBitArray2D operator&(const csBitArray2D &a1, const csBitArray2D &a2) 00113 { 00114 return csBitArray2D(a1) &= a2; 00115 } 00116 00117 friend csBitArray2D operator|(const csBitArray2D &a1, const csBitArray2D &a2) 00118 { 00119 return csBitArray2D(a1) |= a2; 00120 } 00121 00122 friend csBitArray2D operator^(const csBitArray2D &a1, const csBitArray2D &a2) 00123 { 00124 return csBitArray2D(a1) ^= a2; 00125 } 00126 00127 // 00128 // Plain English interface 00129 // 00130 00132 void Clear() 00133 { 00134 super::Clear(); 00135 } 00136 00138 void SetBit(unsigned pos1, unsigned pos2) 00139 { 00140 super::SetBit(pos1 * mWidth + pos2); 00141 } 00142 00144 void ClearBit(unsigned pos1, unsigned pos2) 00145 { 00146 super::ClearBit(pos1 * mWidth + pos2); 00147 } 00148 00150 void FlipBit(unsigned pos1, unsigned pos2) 00151 { 00152 super::FlipBit(pos1 * mWidth + pos2); 00153 } 00154 00156 void Set(unsigned pos1, unsigned pos2, bool val) 00157 { 00158 val ? SetBit(pos1, pos2) : ClearBit(pos1, pos2); 00159 } 00160 00162 bool IsBitSet(unsigned pos1, unsigned pos2) const 00163 { 00164 return super::IsBitSet(pos1 * mWidth + pos2); 00165 } 00166 00168 bool AllBitsFalse() const 00169 { 00170 return super::AllBitsFalse(); 00171 } 00172 00174 csBitArray2D &FlipAllBits() 00175 { 00176 super::FlipAllBits(); 00177 return *this; 00178 } 00179 }; 00180 00181 #endif Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000 |