G4SurfBits.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id: G4SurfBits.cc 67011 2013-01-29 16:17:41Z gcosmo $
00028 //
00029 // --------------------------------------------------------------------
00030 // GEANT 4 class source file
00031 //
00032 // G4SurfBits implementation
00033 //
00034 // History:
00035 // 19.10.12 Marek Gayer, created and adapted from original implementation
00036 //                       of Root's TBits class by P.Canal
00037 // --------------------------------------------------------------------
00038 
00039 #include "G4SurfBits.hh"
00040 #include "G4ios.hh"
00041 
00042 //______________________________________________________________________________
00043 G4SurfBits::G4SurfBits(unsigned int nBits) : fNBits(nBits)
00044 {
00045   // G4SurfBits constructor.  All bits set to 0
00046 
00047   if (fNBits <= 0) fNBits = 0;
00048   fNBytes  = fNBits ? ((fNBits-1)/8) + 1 : 1;
00049   fAllBits = new unsigned char[fNBytes];
00050   // this is redundant only with libNew
00051   std::memset(fAllBits,0,fNBytes);
00052 }
00053 
00054 //______________________________________________________________________________
00055 G4SurfBits::G4SurfBits(const G4SurfBits &original) : fNBits(original.fNBits),
00056   fNBytes(original.fNBytes)
00057 {
00058   // G4SurfBits copy constructor
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   // G4SurfBits assignment operator
00068   if (this != &rhs) {
00069     //      TObject::operator=(rhs);
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   // G4SurfBits destructor
00087 
00088   delete [] fAllBits;
00089 }
00090 
00091 //______________________________________________________________________________
00092 void G4SurfBits::Clear()
00093 {
00094   // Clear the value.
00095 
00096   delete [] fAllBits;
00097   fAllBits = 0;
00098   fNBits   = 0;
00099   fNBytes  = 0;
00100 }
00101 
00102 //______________________________________________________________________________
00103 void G4SurfBits::Compact()
00104 {
00105   // Reduce the storage used by the object to a minimun
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   // Print the value to the std::ostream
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   // Print the list of active bits
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   // Reverse each bytes.
00163 
00164   if (nbytes > fNBytes) {
00165     // do it in this order to remain exception-safe.
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   // set all the bytes
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   // Copy all the byes.
00189   std::memcpy(array, fAllBits, (fNBits+7)>>3);
00190 }
00191 
00192 // If we are on a little endian machine, a bitvector represented using
00193 // any integer type is identical to a bitvector represented using bytes.
00194 
00195 //______________________________________________________________________________
00196 void G4SurfBits::set(unsigned int nBits, const G4int *array)
00197 {
00198   // set all the bytes.
00199 
00200   set(nBits, (const char*)array);
00201 }
00202 
00203 //______________________________________________________________________________
00204 void G4SurfBits::Get(G4int *array) const
00205 {
00206   // Get all the bytes.
00207 
00208   Get((char*)array);
00209 }

Generated on Mon May 27 17:49:56 2013 for Geant4 by  doxygen 1.4.7