Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Geant4/G4SurfBits.hh was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4SurfBits
0027 //
0028 // Class description:
0029 //
0030 // This class provides a simple container of bits, to be used for
0031 // optimization of tessellated surfaces (G4TessellatedSolid).
0032 // Each bit can be set and tested via the functions SetBitNumber and
0033 // TestBitNumber.
0034 // The default value of all bits is false.
0035 // The size of the container is automatically extended when a bit
0036 // number is either set or tested.  To reduce the memory size of the
0037 // container use the Compact function, this will discard the memory
0038 // occupied by the upper bits that are 0.
0039 
0040 // 19.10.12 - Marek Gayer, created and adapted from original implementation
0041 //                         of Root's TBits class by P.Canal
0042 // --------------------------------------------------------------------
0043 #ifndef G4SURFBITS_HH
0044 #define G4SURFBITS_HH
0045 
0046 #include <cstring>
0047 
0048 #include "G4Types.hh"
0049 
0050 class G4SurfBits
0051 {
0052   public:
0053 
0054     G4SurfBits(unsigned int nbits = 0);
0055     G4SurfBits(const G4SurfBits&);
0056     G4SurfBits& operator=(const G4SurfBits&);
0057    ~G4SurfBits();
0058 
0059     //----- Bit manipulation
0060     void ResetAllBits(G4bool value = false);  // if value=1 set all bits to 1
0061     void ResetBitNumber(unsigned int bitnumber);
0062     void SetBitNumber(unsigned int bitnumber, G4bool value = true);
0063     G4bool TestBitNumber(unsigned int bitnumber) const;
0064 
0065     //----- Accessors and operator
0066     G4bool operator[](unsigned int bitnumber) const;
0067 
0068     //----- Optimized setters
0069     // Each of these will replace the contents of the receiver with the
0070     // bitvector in the parameter array. The number of bits is changed
0071     // to nbits. If nbits is smaller than fNBits, the receiver will NOT
0072     // be compacted.
0073     void set(unsigned int nbits, const char* array);
0074     void set(unsigned int nbits, const G4int* array);
0075 
0076     //----- Optimized getters
0077     // Each of these will replace the contents of the parameter array with the
0078     // bits in the receiver.  The parameter array must be large enough to hold
0079     // all of the bits in the receiver.
0080     // Note on semantics: any bits in the parameter array that go beyond the
0081     // number of the bits in the receiver will have an unspecified value. For
0082     // example, if you call Get(Int*) with an array of one integer and the
0083     // G4SurfBits object has less than 32 bits, then the remaining bits in the
0084     // integer will have an unspecified value.
0085     void Get(char* array) const;
0086     void Get(G4int* array) const;
0087 
0088     //----- Utilities
0089     void Clear();
0090     void Compact();               // Reduce the space used.
0091 
0092     unsigned int GetNbits()      const { return fNBits; }
0093     unsigned int GetNbytes()     const { return fNBytes; }
0094 
0095     void Print() const;  // to show the list of active bits
0096     void Output(std::ostream &) const;
0097 
0098   protected:
0099 
0100     void ReserveBytes(unsigned int nbytes);
0101 
0102   public:
0103 
0104     unsigned char* fAllBits = nullptr; // [fNBytes] array of UChars
0105 
0106   protected:
0107 
0108     unsigned int fNBits;         // Highest bit set + 1
0109     unsigned int fNBytes;        // Number of UChars in fAllBits
0110 };
0111 
0112 // inline functions...
0113 
0114 inline void G4SurfBits::SetBitNumber(unsigned int bitnumber, G4bool value)
0115 {
0116   // set bit number 'bitnumber' to be value
0117 
0118   if (bitnumber >= fNBits)
0119   {
0120     unsigned int new_size = (bitnumber/8) + 1;
0121     if (new_size > fNBytes)
0122     {
0123       if (new_size < 100 * 1024 * 1024) new_size *= 2;
0124       unsigned char *old_location = fAllBits;
0125       fAllBits = new unsigned char[new_size];
0126       std::memcpy(fAllBits,old_location,fNBytes);
0127       std::memset(fAllBits+fNBytes ,0, new_size-fNBytes);
0128       fNBytes = new_size;
0129       delete [] old_location;
0130     }
0131     fNBits = bitnumber+1;
0132   }
0133   unsigned int loc = bitnumber/8;
0134   unsigned char bit = bitnumber%8;
0135   if (value)
0136     fAllBits[loc] |= (1<<bit);
0137   else
0138     fAllBits[loc] &= (0xFF ^ (1<<bit));
0139 }
0140 
0141 inline G4bool G4SurfBits::TestBitNumber(unsigned int bitnumber) const
0142 {
0143   // Return the current value of the bit
0144 
0145   if (bitnumber >= fNBits) return false;
0146   unsigned int loc = bitnumber/8;
0147   unsigned char value = fAllBits[loc];
0148   unsigned char bit = bitnumber%8;
0149   G4bool result = (value & (1<<bit)) != 0;
0150   return result;
0151   // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
0152 }
0153 
0154 inline void G4SurfBits::ResetBitNumber(unsigned int bitnumber)
0155 {
0156   SetBitNumber(bitnumber,false);
0157 }
0158 
0159 inline G4bool G4SurfBits::operator[](unsigned int bitnumber) const
0160 {
0161   return TestBitNumber(bitnumber);
0162 }
0163 
0164 #endif