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
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "G4SurfBits.hh"
00040 #include "G4ios.hh"
00041
00042
00043 G4SurfBits::G4SurfBits(unsigned int nBits) : fNBits(nBits)
00044 {
00045
00046
00047 if (fNBits <= 0) fNBits = 0;
00048 fNBytes = fNBits ? ((fNBits-1)/8) + 1 : 1;
00049 fAllBits = new unsigned char[fNBytes];
00050
00051 std::memset(fAllBits,0,fNBytes);
00052 }
00053
00054
00055 G4SurfBits::G4SurfBits(const G4SurfBits &original) : fNBits(original.fNBits),
00056 fNBytes(original.fNBytes)
00057 {
00058
00059
00060 fAllBits = new unsigned char[fNBytes];
00061 std::memcpy(fAllBits,original.fAllBits,fNBytes);
00062 }
00063
00064
00065 G4SurfBits& G4SurfBits::operator=(const G4SurfBits& rhs)
00066 {
00067
00068 if (this != &rhs) {
00069
00070 fNBits = rhs.fNBits;
00071 fNBytes = rhs.fNBytes;
00072 delete [] fAllBits;
00073 if (fNBytes != 0) {
00074 fAllBits = new unsigned char[fNBytes];
00075 std::memcpy(fAllBits,rhs.fAllBits,fNBytes);
00076 } else {
00077 fAllBits = 0;
00078 }
00079 }
00080 return *this;
00081 }
00082
00083
00084 G4SurfBits::~G4SurfBits()
00085 {
00086
00087
00088 delete [] fAllBits;
00089 }
00090
00091
00092 void G4SurfBits::Clear()
00093 {
00094
00095
00096 delete [] fAllBits;
00097 fAllBits = 0;
00098 fNBits = 0;
00099 fNBytes = 0;
00100 }
00101
00102
00103 void G4SurfBits::Compact()
00104 {
00105
00106
00107 if (!fNBits || !fAllBits) return;
00108 unsigned int needed;
00109 for(needed=fNBytes-1;
00110 needed > 0 && fAllBits[needed]==0; ) { needed--; };
00111 needed++;
00112
00113 if (needed!=fNBytes) {
00114 unsigned char *old_location = fAllBits;
00115 fAllBits = new unsigned char[needed];
00116
00117 std::memcpy(fAllBits,old_location,needed);
00118 delete [] old_location;
00119
00120 fNBytes = needed;
00121 fNBits = 8*fNBytes;
00122 }
00123 }
00124
00125
00126 void G4SurfBits::Output(std::ostream &os) const
00127 {
00128
00129 for(unsigned int i=0; i<fNBytes; ++i) {
00130 unsigned char val = fAllBits[fNBytes - 1 - i];
00131 for (unsigned int j=0; j<8; ++j) {
00132 os << (G4bool)(val&0x80);
00133 val <<= 1;
00134 }
00135 }
00136 }
00137
00138
00139 void G4SurfBits::Print() const
00140 {
00141
00142 G4int count = 0;
00143 for(unsigned int i=0; i<fNBytes; ++i) {
00144 unsigned char val = fAllBits[i];
00145 for (unsigned int j=0; j<8; ++j) {
00146 if (val & 1) G4cout << " bit:" << count << " = 1" << G4endl;
00147 count++;
00148 val = val >> 1;
00149 }
00150 }
00151 }
00152
00153
00154 void G4SurfBits::ResetAllBits(G4bool value)
00155 {
00156 if (fAllBits) std::memset(fAllBits, value ? 0xFF : 0,fNBytes);
00157 }
00158
00159
00160 void G4SurfBits::ReserveBytes(unsigned int nbytes)
00161 {
00162
00163
00164 if (nbytes > fNBytes) {
00165
00166 unsigned char *newBits=new unsigned char[nbytes];
00167 delete[] fAllBits;
00168 fNBytes=nbytes;
00169 fAllBits=newBits;
00170 }
00171 }
00172
00173
00174 void G4SurfBits::set(unsigned int nBits, const char *array)
00175 {
00176
00177 unsigned int nbytes=(nBits+7)>>3;
00178
00179 ReserveBytes(nbytes);
00180
00181 fNBits=nBits;
00182 std::memcpy(fAllBits, array, nbytes);
00183 }
00184
00185
00186 void G4SurfBits::Get(char *array) const
00187 {
00188
00189 std::memcpy(array, fAllBits, (fNBits+7)>>3);
00190 }
00191
00192
00193
00194
00195
00196 void G4SurfBits::set(unsigned int nBits, const G4int *array)
00197 {
00198
00199
00200 set(nBits, (const char*)array);
00201 }
00202
00203
00204 void G4SurfBits::Get(G4int *array) const
00205 {
00206
00207
00208 Get((char*)array);
00209 }