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] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // ********************************************************************
 25 //
 26 // G4SurfBits
 27 //
 28 // Class description:
 29 //
 30 // This class provides a simple container of bits, to be used for
 31 // optimization of tessellated surfaces (G4TessellatedSolid).
 32 // Each bit can be set and tested via the functions SetBitNumber and
 33 // TestBitNumber.
 34 // The default value of all bits is false.
 35 // The size of the container is automatically extended when a bit
 36 // number is either set or tested.  To reduce the memory size of the
 37 // container use the Compact function, this will discard the memory
 38 // occupied by the upper bits that are 0.
 39 
 40 // 19.10.12 - Marek Gayer, created and adapted from original implementation
 41 //                         of Root's TBits class by P.Canal
 42 // --------------------------------------------------------------------
 43 #ifndef G4SURFBITS_HH
 44 #define G4SURFBITS_HH
 45 
 46 #include <cstring>
 47 
 48 #include "G4Types.hh"
 49 
 50 class G4SurfBits
 51 {
 52   public:
 53 
 54     G4SurfBits(unsigned int nbits = 0);
 55     G4SurfBits(const G4SurfBits&);
 56     G4SurfBits& operator=(const G4SurfBits&);
 57    ~G4SurfBits();
 58 
 59     //----- Bit manipulation
 60     void ResetAllBits(G4bool value = false);  // if value=1 set all bits to 1
 61     void ResetBitNumber(unsigned int bitnumber);
 62     void SetBitNumber(unsigned int bitnumber, G4bool value = true);
 63     G4bool TestBitNumber(unsigned int bitnumber) const;
 64 
 65     //----- Accessors and operator
 66     G4bool operator[](unsigned int bitnumber) const;
 67 
 68     //----- Optimized setters
 69     // Each of these will replace the contents of the receiver with the
 70     // bitvector in the parameter array. The number of bits is changed
 71     // to nbits. If nbits is smaller than fNBits, the receiver will NOT
 72     // be compacted.
 73     void set(unsigned int nbits, const char* array);
 74     void set(unsigned int nbits, const G4int* array);
 75 
 76     //----- Optimized getters
 77     // Each of these will replace the contents of the parameter array with the
 78     // bits in the receiver.  The parameter array must be large enough to hold
 79     // all of the bits in the receiver.
 80     // Note on semantics: any bits in the parameter array that go beyond the
 81     // number of the bits in the receiver will have an unspecified value. For
 82     // example, if you call Get(Int*) with an array of one integer and the
 83     // G4SurfBits object has less than 32 bits, then the remaining bits in the
 84     // integer will have an unspecified value.
 85     void Get(char* array) const;
 86     void Get(G4int* array) const;
 87 
 88     //----- Utilities
 89     void Clear();
 90     void Compact();               // Reduce the space used.
 91 
 92     unsigned int GetNbits()      const { return fNBits; }
 93     unsigned int GetNbytes()     const { return fNBytes; }
 94 
 95     void Print() const;  // to show the list of active bits
 96     void Output(std::ostream &) const;
 97 
 98   protected:
 99 
100     void ReserveBytes(unsigned int nbytes);
101 
102   public:
103 
104     unsigned char* fAllBits = nullptr; // [fNBytes] array of UChars
105 
106   protected:
107 
108     unsigned int fNBits;         // Highest bit set + 1
109     unsigned int fNBytes;        // Number of UChars in fAllBits
110 };
111 
112 // inline functions...
113 
114 inline void G4SurfBits::SetBitNumber(unsigned int bitnumber, G4bool value)
115 {
116   // set bit number 'bitnumber' to be value
117 
118   if (bitnumber >= fNBits)
119   {
120     unsigned int new_size = (bitnumber/8) + 1;
121     if (new_size > fNBytes)
122     {
123       if (new_size < 100 * 1024 * 1024) new_size *= 2;
124       unsigned char *old_location = fAllBits;
125       fAllBits = new unsigned char[new_size];
126       std::memcpy(fAllBits,old_location,fNBytes);
127       std::memset(fAllBits+fNBytes ,0, new_size-fNBytes);
128       fNBytes = new_size;
129       delete [] old_location;
130     }
131     fNBits = bitnumber+1;
132   }
133   unsigned int loc = bitnumber/8;
134   unsigned char bit = bitnumber%8;
135   if (value)
136     fAllBits[loc] |= (1<<bit);
137   else
138     fAllBits[loc] &= (0xFF ^ (1<<bit));
139 }
140 
141 inline G4bool G4SurfBits::TestBitNumber(unsigned int bitnumber) const
142 {
143   // Return the current value of the bit
144 
145   if (bitnumber >= fNBits) return false;
146   unsigned int loc = bitnumber/8;
147   unsigned char value = fAllBits[loc];
148   unsigned char bit = bitnumber%8;
149   G4bool result = (value & (1<<bit)) != 0;
150   return result;
151   // short: return 0 != (fAllBits[bitnumber/8] & (1<< (bitnumber%8)));
152 }
153 
154 inline void G4SurfBits::ResetBitNumber(unsigned int bitnumber)
155 {
156   SetBitNumber(bitnumber,false);
157 }
158 
159 inline G4bool G4SurfBits::operator[](unsigned int bitnumber) const
160 {
161   return TestBitNumber(bitnumber);
162 }
163 
164 #endif
165