Loading...
Searching...
No Matches
G4TAtomicHitsMap.hh
Go to the documentation of this file.
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/// \file parallel/ThreadsafeScorers/include/G4TAtomicHitsMap.hh
27/// \brief Definition of the G4TAtomicHitsMap class
28//
29//
30//
31//
32/// This is an implementation of G4THitsMap<T> where the underlying
33/// type is G4atomic<T>, not just T. A static assert is provided to
34/// ensure that T is fundamental. This class should be used in lieu
35/// of G4THitsMap<T> when memory is a concern. Atomics are
36/// thread-safe and *generally* faster that mutexes (as long as the
37/// STL implementation is lock-free) but the synchronization does
38/// not come without a cost. If performance is the primary concern,
39/// use G4THitsMap<T> in thread-local instances.
40//
41//
42//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
43//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
44
45#ifndef G4TAtomicHitsMap_h
46#define G4TAtomicHitsMap_h 1
47
48#include "G4THitsCollection.hh"
49#include "G4THitsMap.hh"
50#include "globals.hh"
51#include "G4atomic.hh"
52#include "G4Threading.hh"
53#include "G4AutoLock.hh"
54
55#include <map>
56#include <type_traits>
57
58// class description:
59//
60// This is a template class of hits map and parametrized by
61// The concrete class of G4VHit. This is a uniform collection for
62// a particular concrete hit class objects.
63// An intermediate layer class G4HitsMap appeared in this
64// header file is used just for G4Allocator, because G4Allocator
65// cannot be instansiated with a template class. Thus G4HitsMap
66// class MUST NOT be directly used by the user.
67
68template <typename T>
70{
71 protected:
72 static_assert(std::is_fundamental<T>::value,
73 "G4TAtomicHitsMap must use fundamental type");
74
75 public:
78 typedef typename std::map<G4int, mapped_type> container_type;
79 typedef typename container_type::iterator iterator;
80 typedef typename container_type::const_iterator const_iterator;
81
82 public:
84
85 public: // with description
86 G4TAtomicHitsMap(G4String detName, G4String colNam);
87 // constructor.
88
89 public:
90 virtual ~G4TAtomicHitsMap();
91 G4bool operator==(const G4TAtomicHitsMap<T>& right) const;
94
95 public: // with description
96 virtual void DrawAllHits();
97 virtual void PrintAllHits();
98 // These two methods invokes Draw() and Print() methods of all of
99 // hit objects stored in this map, respectively.
100
101 public: // with description
102 inline value_type* operator[](G4int key) const;
103
104 // Returns a pointer to a concrete hit object.
105 inline container_type* GetMap() const { return theCollection; }
106 // Returns a collection map.
107 inline G4int add(const G4int& key, value_type*& aHit) const;
108 inline G4int add(const G4int& key, T& aHit) const;
109 // Insert a hit object. Total number of hit objects stored in this
110 // map is returned.
111 inline G4int set(const G4int& key, value_type*& aHit) const;
112 inline G4int set(const G4int& key, T& aHit) const;
113 // Overwrite a hit object. Total number of hit objects stored in this
114 // map is returned.
115 inline G4int entries() const { return theCollection->size(); }
116 // Returns the number of hit objects stored in this map
117 inline void clear();
118
119 public:
120 virtual G4VHit* GetHit(size_t) const { return 0; }
121 virtual size_t GetSize() const { return theCollection->size(); }
122
123 virtual size_t size() const { return theCollection->size(); }
124
125 public:
126 iterator begin() { return theCollection->begin(); }
127 iterator end() { return theCollection->end(); }
128
129 const_iterator begin() const { return theCollection->begin(); }
130 const_iterator end() const { return theCollection->end(); }
131
132 const_iterator cbegin() const { return theCollection->cbegin(); }
133 const_iterator cend() const { return theCollection->cend(); }
134
135 iterator find(G4int p) { return theCollection->find(p); }
136 const_iterator find(G4int p) const { return theCollection->find(p); }
137
138 private:
140 mutable G4Mutex fMutex;
141};
142
143//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
144template <typename T>
148//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
149template <typename T>
151 : G4VHitsCollection(detName, colNam)
152 , theCollection(new container_type)
153{}
154//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
155template <typename T>
157{
158 for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
159 delete itr->second;
160
161 delete theCollection;
162}
163//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
164template <typename T>
166{
167 return (collectionName == right.collectionName);
168}
169//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
170template <typename T>
172 const G4TAtomicHitsMap<T>& rhs) const
173{
174 for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
175 add(itr->first, *(itr->second));
176
177 return (G4TAtomicHitsMap<T>&) (*this);
178}
179//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
180template <typename T>
182 const G4THitsMap<T>& rhs) const
183{
184 for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
185 add(itr->first, *(itr->second));
186
187 return (G4TAtomicHitsMap<T>&) (*this);
188}
189//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
190template <typename T>
191inline
193 G4int key) const
194{
195 if(theCollection->find(key) != theCollection->end())
196 return theCollection->find(key)->second;
197 else
198 {
199 G4AutoLock l(&fMutex);
200 if(theCollection->find(key) == theCollection->end())
201 {
202 value_type* ptr = new value_type;
203 (*theCollection)[key] = ptr;
204 return ptr;
205 }
206 else
207 return theCollection->find(key)->second;
208 }
209}
210//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
211template <typename T>
212inline G4int G4TAtomicHitsMap<T>::add(const G4int& key, value_type*& aHit) const
213{
214 if(theCollection->find(key) != theCollection->end())
215 *(*theCollection)[key] += *aHit;
216 else
217 {
218 G4AutoLock l(&fMutex);
219 (*theCollection)[key] = aHit;
220 }
221 G4AutoLock l(&fMutex);
222 return theCollection->size();
223}
224//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
225template <typename T>
226inline G4int G4TAtomicHitsMap<T>::add(const G4int& key, T& aHit) const
227{
228 if(theCollection->find(key) != theCollection->end())
229 *(*theCollection)[key] += aHit;
230 else
231 {
232 value_type* hit = new value_type;
233 *hit = aHit;
234 G4AutoLock l(&fMutex);
235 (*theCollection)[key] = hit;
236 }
237 G4AutoLock l(&fMutex);
238 return theCollection->size();
239}
240//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
241template <typename T>
242inline G4int G4TAtomicHitsMap<T>::set(const G4int& key, value_type*& aHit) const
243{
244 if(theCollection->find(key) != theCollection->end())
245 delete(*theCollection)[key]->second;
246
247 (*theCollection)[key] = aHit;
248 G4AutoLock l(&fMutex);
249 return theCollection->size();
250}
251//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
252template <typename T>
253inline G4int G4TAtomicHitsMap<T>::set(const G4int& key, T& aHit) const
254{
255 if(theCollection->find(key) != theCollection->end())
256 *(*theCollection)[key] = aHit;
257 else
258 {
259 value_type* hit = new value_type;
260 *hit = aHit;
261 (*theCollection)[key] = hit;
262 }
263 G4AutoLock l(&fMutex);
264 return theCollection->size();
265}
266//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
267template <typename T>
270//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
271template <typename T>
273{
274 G4cout << "G4TAtomicHitsMap " << SDname << " / " << collectionName << " --- "
275 << entries() << " entries" << G4endl;
276}
277//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
278template <typename T>
280{
281 G4AutoLock l(&fMutex);
282
283 for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
284 delete itr->second;
285
286 theCollection->clear();
287}
288//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
289
290#endif
Definition of the G4atomic class.
This is an implementation of G4THitsMap<T> where the underlying type is G4atomic<T>,...
container_type::const_iterator const_iterator
virtual size_t size() const
const_iterator begin() const
iterator find(G4int p)
G4atomic< T > value_type
container_type * theCollection
virtual G4VHit * GetHit(size_t) const
G4TAtomicHitsMap< T > & operator+=(const G4TAtomicHitsMap< T > &right) const
const_iterator cend() const
container_type * GetMap() const
virtual void DrawAllHits()
virtual size_t GetSize() const
value_type * mapped_type
std::map< G4int, mapped_type > container_type
G4bool operator==(const G4TAtomicHitsMap< T > &right) const
virtual void PrintAllHits()
G4int entries() const
container_type::iterator iterator
const_iterator cbegin() const
const_iterator end() const
G4int set(const G4int &key, value_type *&aHit) const
value_type * operator[](G4int key) const
G4int add(const G4int &key, value_type *&aHit) const
const_iterator find(G4int p) const

Applications | User Support | Publications | Collaboration