G4SurfBits.hh

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.hh 67011 2013-01-29 16:17:41Z gcosmo $
00028 //
00029 // --------------------------------------------------------------------
00030 // GEANT 4 class header file
00031 //
00032 // G4SurfBits
00033 //
00034 // Class description:
00035 //
00036 // This class provides a simple container of bits, to be used for
00037 // optimization of tessellated surfaces (G4TessellatedSolid).
00038 // Each bit can be set and tested via the functions SetBitNumber and
00039 // TestBitNumber.
00040 // The default value of all bits is false.
00041 // The size of the container is automatically extended when a bit
00042 // number is either set or tested.  To reduce the memory size of the
00043 // container use the Compact function, this will discard the memory
00044 // occupied by the upper bits that are 0.
00045 //
00046 
00047 // History:
00048 // 19.10.12 Marek Gayer, created and adapted from original implementation
00049 //                       of Root's TBits class by P.Canal
00050 // --------------------------------------------------------------------
00051 
00052 #ifndef G4SurfBits_HH
00053 #define G4SurfBits_HH
00054 
00055 #include <cstring>
00056 
00057 #include "G4Types.hh"
00058 
00059 class G4SurfBits
00060 {
00061   public:
00062 
00063     G4SurfBits(unsigned int nbits = 0);
00064     G4SurfBits(const G4SurfBits&);
00065     G4SurfBits& operator=(const G4SurfBits&);
00066    ~G4SurfBits();
00067 
00068     //----- Bit manipulation
00069     void ResetAllBits(G4bool value=false);  // if value=1 set all bits to 1
00070     void ResetBitNumber(unsigned int bitnumber);
00071     void SetBitNumber(unsigned int bitnumber, G4bool value = true);
00072     G4bool TestBitNumber(unsigned int bitnumber) const;
00073 
00074     //----- Accessors and operator
00075     G4bool operator[](unsigned int bitnumber) const;
00076 
00077     //----- Optimized setters
00078     // Each of these will replace the contents of the receiver with the
00079     // bitvector in the parameter array. The number of bits is changed
00080     // to nbits. If nbits is smaller than fNBits, the receiver will NOT
00081     // be compacted.
00082     void   set(unsigned int nbits, const char *array);
00083     void   set(unsigned int nbits, const G4int *array);
00084 
00085     //----- Optimized getters
00086     // Each of these will replace the contents of the parameter array with the
00087     // bits in the receiver.  The parameter array must be large enough to hold
00088     // all of the bits in the receiver.
00089     // Note on semantics: any bits in the parameter array that go beyond the
00090     // number of the bits in the receiver will have an unspecified value. For
00091     // example, if you call Get(Int*) with an array of one integer and the
00092     // G4SurfBits object has less than 32 bits, then the remaining bits in the
00093     // integer will have an unspecified value.
00094     void   Get(char *array) const;
00095     void   Get(G4int *array) const;
00096 
00097     //----- Utilities
00098     void    Clear();
00099     void    Compact();               // Reduce the space used.
00100 
00101     unsigned int  GetNbits()      const { return fNBits; }
00102     unsigned int  GetNbytes()     const { return fNBytes; }
00103 
00104     void    Print() const;  // to show the list of active bits
00105     void    Output(std::ostream &) const;
00106 
00107   protected:
00108 
00109     void ReserveBytes(unsigned int nbytes);
00110 
00111   public:
00112 
00113     unsigned char *fAllBits;       // [fNBytes] array of UChars
00114 
00115   protected:
00116 
00117     unsigned int   fNBits;         // Highest bit set + 1
00118     unsigned int   fNBytes;        // Number of UChars in fAllBits
00119 };
00120 
00121 // inline functions...
00122 
00123 inline void G4SurfBits::SetBitNumber(unsigned int bitnumber, G4bool value)
00124 {
00125   // set bit number 'bitnumber' to be value
00126   if (bitnumber >= fNBits) {
00127     unsigned int new_size = (bitnumber/8) + 1;
00128     if (new_size > fNBytes) {
00129       if (new_size < 100 * 1024 * 1024)
00130         new_size *= 2;
00131       unsigned char *old_location = fAllBits;
00132       fAllBits = new unsigned char[new_size];
00133       std::memcpy(fAllBits,old_location,fNBytes);
00134       std::memset(fAllBits+fNBytes ,0, new_size-fNBytes);
00135       fNBytes = new_size;
00136       delete [] old_location;
00137     }
00138     fNBits = bitnumber+1;
00139   }
00140   unsigned int  loc = bitnumber/8;
00141   unsigned char bit = bitnumber%8;
00142   if (value)
00143     fAllBits[loc] |= (1<<bit);
00144   else
00145     fAllBits[loc] &= (0xFF ^ (1<<bit));
00146 }
00147 
00148 inline G4bool G4SurfBits::TestBitNumber(unsigned int bitnumber) const
00149 {
00150   // Return the current value of the bit
00151 
00152   if (bitnumber >= fNBits) return false;
00153   unsigned int  loc = bitnumber/8;
00154   unsigned char value = fAllBits[loc];
00155   unsigned char bit = bitnumber%8;
00156   G4bool  result = (value & (1<<bit)) != 0;
00157   return result;
00158   // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
00159 }
00160 
00161 inline void G4SurfBits::ResetBitNumber(unsigned int bitnumber)
00162 {
00163   SetBitNumber(bitnumber,false);
00164 }
00165 
00166 inline G4bool G4SurfBits::operator[](unsigned int bitnumber) const
00167 {
00168   return TestBitNumber(bitnumber);
00169 }
00170 
00171 #endif

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