49 #ifndef G4TAtomicHitsMap_h
50 #define G4TAtomicHitsMap_h 1
52 #include "G4THitsCollection.hh"
60 #include <type_traits>
76 static_assert(std::is_fundamental<T>::value,
77 "G4TAtomicHitsMap must use fundamental type");
81 typedef value_type* mapped_type;
82 typedef typename std::map<G4int, mapped_type> container_type;
83 typedef typename container_type::iterator iterator;
84 typedef typename container_type::const_iterator const_iterator;
106 inline value_type* operator[](
G4int key)
const;
109 inline container_type* GetMap()
const
110 {
return theCollection; }
112 inline G4int add(
const G4int & key, value_type*& aHit)
const;
113 inline G4int add(
const G4int & key, T& aHit)
const;
116 inline G4int set(
const G4int & key, value_type*& aHit)
const;
117 inline G4int set(
const G4int & key, T& aHit)
const;
120 inline G4int entries()
const
122 return theCollection->size();
131 return theCollection->size();
135 iterator begin() {
return theCollection->begin(); }
136 iterator end() {
return theCollection->end(); }
138 const_iterator begin()
const {
return theCollection->begin(); }
139 const_iterator end()
const {
return theCollection->end(); }
141 const_iterator cbegin()
const {
return theCollection->cbegin(); }
142 const_iterator cend()
const {
return theCollection->cend(); }
144 iterator find(
G4int p) {
return theCollection->find(p); }
145 const_iterator find(
G4int p)
const {
return theCollection->find(p); }
148 container_type* theCollection;
154 template <
typename T>
156 : theCollection(new container_type),
160 template <
typename T>
164 theCollection(new container_type),
168 template <
typename T>
171 for(
auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
174 delete theCollection;
178 template <
typename T>
184 template <
typename T>
188 for(
auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
189 add(itr->first, *(itr->second));
194 template <
typename T>
198 for(
auto itr = rhs.
GetMap()->begin(); itr != rhs.
GetMap()->end(); itr++)
199 add(itr->first, *(itr->second));
204 template <
typename T>
208 if(theCollection->find(key) != theCollection->end())
209 return theCollection->find(key)->second;
213 if(theCollection->find(key) == theCollection->end())
215 value_type* ptr =
new value_type;
216 (*theCollection)[key] = ptr;
219 return theCollection->find(key)->second;
223 template <
typename T>
227 if(theCollection->find(key) != theCollection->end())
228 *(*theCollection)[key] += *aHit;
232 (*theCollection)[key] = aHit;
235 return theCollection->size();
238 template <
typename T>
243 if(theCollection->find(key) != theCollection->end())
244 *(*theCollection)[key] += aHit;
247 value_type* hit =
new value_type;
250 (*theCollection)[key] = hit;
253 return theCollection->size();
256 template <
typename T>
260 if(theCollection->find(key) != theCollection->end())
261 delete (*theCollection)[key]->second;
263 (*theCollection)[key] = aHit;
265 return theCollection->size();
268 template <
typename T>
272 if(theCollection->find(key) != theCollection->end())
273 *(*theCollection)[key] = aHit;
276 value_type* hit =
new value_type;
278 (*theCollection)[key] = hit;
281 return theCollection->size();
284 template <
typename T>
288 template <
typename T>
291 G4cout <<
"G4TAtomicHitsMap " << SDname <<
" / " << collectionName <<
" --- "
292 << entries() <<
" entries" <<
G4endl;
295 template <
typename T>
300 for(
auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
303 theCollection->clear();
virtual size_t GetSize() const
virtual void PrintAllHits()
G4int operator==(const G4VHitsCollection &right) const
virtual G4VHit * GetHit(size_t) const
virtual void DrawAllHits()
#define G4MUTEX_INITIALIZER
G4GLOB_DLL std::ostream G4cout
This is an implementation of G4THitsMap where the underlying type is G4atomic, not just T. A static assert is provided to ensure that T is fundamental. This class should be used in lieu of G4THitsMap when memory is a concern. Atomics are thread-safe and generally faster that mutexes (as long as the STL implementation is lock-free) but the synchronization does not come without a cost. If performance is the primary concern, use G4THitsMap in thread-local instances.
std::map< G4int, T * > * GetMap() const
Definition of the G4atomic class.
#define G4MUTEXDESTROY(mutex)
_Tp G4atomic
This is an friendly implementation of the STL atomic class. This class has the same interface as the ...