Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-19 09:21:44

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 // G4NavigationHistoryPool
0027 //
0028 // Class description:
0029 //
0030 // Thread-local pool for navigation history levels collections being
0031 // allocated by G4NavigationHistory. Allows for reuse of the vectors
0032 // allocated according to lifetime of G4NavigationHistory objects.
0033 
0034 // Author: Gabriele Cosmo (CERN), 07.05.2014 - Initial version
0035 // --------------------------------------------------------------------
0036 #ifndef G4NAVIGATIONHISTORYPOOL_HH
0037 #define G4NAVIGATIONHISTORYPOOL_HH
0038 
0039 #include <vector>
0040 
0041 #include "G4NavigationLevel.hh"
0042 
0043 /**
0044  * @brief G4NavigationHistoryPool is a thread-local pool for navigation history
0045  * levels collections being allocated by G4NavigationHistory. It allows for
0046  * reuse of the vectors allocated according to lifetime of G4NavigationHistory
0047  * objects.
0048  */
0049 
0050 class G4NavigationHistoryPool
0051 {
0052   public:
0053 
0054     /**
0055      * Destructor: takes care to delete the allocated levels.
0056      */
0057     ~G4NavigationHistoryPool();
0058 
0059     /**
0060      * Returns the unique instance of G4NavigationHistoryPool.
0061      */
0062     static G4NavigationHistoryPool* GetInstance();
0063 
0064     /**
0065      * Returns the pointer to a new collection of levels being allocated.
0066      */
0067     inline std::vector<G4NavigationLevel> * GetNewLevels();
0068 
0069     /**
0070      * Returns the pointer of the first available collection of levels
0071      * If none are available (i.e. empty free vector) allocates the collection.
0072      */
0073     inline std::vector<G4NavigationLevel> * GetLevels();
0074 
0075     /**
0076      * Deactivates the levels collection in pool.
0077      */
0078     inline void DeRegister(std::vector<G4NavigationLevel> * pLevels);
0079 
0080     /**
0081      * Deletes all levels stored in the pool.
0082      */
0083     void Clean();
0084 
0085     /**
0086      * Prints the number of entries.
0087      */
0088     void Print() const;
0089 
0090   private:
0091 
0092     /**
0093      * Private default Constructor.
0094      */
0095     G4NavigationHistoryPool();
0096 
0097     /**
0098      * Registers the levels collection to the pool and activates it.
0099      */
0100     inline void Register(std::vector<G4NavigationLevel> * pLevels);
0101 
0102     /**
0103      * Sets internal vectors content to zero.
0104      */
0105     void Reset();
0106 
0107   private:
0108 
0109     static G4ThreadLocal G4NavigationHistoryPool* fgInstance;
0110 
0111     std::vector<std::vector<G4NavigationLevel> *> fPool;
0112     std::vector<std::vector<G4NavigationLevel> *> fFree;
0113 };
0114 
0115 // ***************************************************************************
0116 // Register levels collection to pool (add and/or activate)
0117 // ***************************************************************************
0118 //
0119 inline void G4NavigationHistoryPool::
0120 Register(std::vector<G4NavigationLevel> * pLevels)
0121 {
0122   fPool.push_back(pLevels);
0123 }
0124 
0125 // ***************************************************************************
0126 // Deactivate levels collection in pool
0127 // ***************************************************************************
0128 //
0129 inline void G4NavigationHistoryPool::
0130 DeRegister(std::vector<G4NavigationLevel> * pLevels)
0131 {
0132   fFree.push_back(pLevels);
0133 }
0134 
0135 // ***************************************************************************
0136 // Return the pointer of a new collection of levels allocated
0137 // ***************************************************************************
0138 //
0139 inline std::vector<G4NavigationLevel> * G4NavigationHistoryPool::GetNewLevels()
0140 {
0141   auto aLevelVec = new std::vector<G4NavigationLevel>(kHistoryMax);
0142   Register(aLevelVec);
0143 
0144   return aLevelVec;
0145 }
0146 
0147 // ***************************************************************************
0148 // Return the pointer of the first available collection of levels
0149 // If none are available (i.e. non active) allocate collection
0150 // ***************************************************************************
0151 //
0152 inline std::vector<G4NavigationLevel> * G4NavigationHistoryPool::GetLevels()
0153 {
0154   std::vector<G4NavigationLevel> * levels = nullptr;
0155 
0156   if (!fFree.empty())
0157   {
0158     levels = fFree.back();
0159     fFree.pop_back();
0160   }
0161   else
0162   {
0163     levels = GetNewLevels();
0164   }
0165 
0166   return levels;
0167 }
0168 
0169 #endif