Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4TAtomicHitsMap< T > Class Template Reference

#include <G4TAtomicHitsMap.hh>

Inheritance diagram for G4TAtomicHitsMap< T >:
Collaboration diagram for G4TAtomicHitsMap< T >:

Public Types

typedef G4atomic< T > value_type
 
typedef value_typemapped_type
 
typedef std::map< G4int,
mapped_type
container_type
 
typedef container_type::iterator iterator
 
typedef
container_type::const_iterator 
const_iterator
 

Public Member Functions

 G4TAtomicHitsMap ()
 
 G4TAtomicHitsMap (G4String detName, G4String colNam)
 
virtual ~G4TAtomicHitsMap ()
 
G4int operator== (const G4TAtomicHitsMap< T > &right) const
 
G4TAtomicHitsMap< T > & operator+= (const G4TAtomicHitsMap< T > &right) const
 
G4TAtomicHitsMap< T > & operator+= (const G4THitsMap< T > &right) const
 
virtual void DrawAllHits ()
 
virtual void PrintAllHits ()
 
value_typeoperator[] (G4int key) const
 
container_typeGetMap () const
 
G4int add (const G4int &key, value_type *&aHit) const
 
G4int add (const G4int &key, T &aHit) const
 
G4int set (const G4int &key, value_type *&aHit) const
 
G4int set (const G4int &key, T &aHit) const
 
G4int entries () const
 
void clear ()
 
virtual G4VHitGetHit (size_t) const
 
virtual size_t GetSize () const
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
iterator find (G4int p)
 
const_iterator find (G4int p) const
 
- Public Member Functions inherited from G4VHitsCollection
 G4VHitsCollection ()
 
 G4VHitsCollection (G4String detName, G4String colNam)
 
virtual ~G4VHitsCollection ()
 
G4int operator== (const G4VHitsCollection &right) const
 
G4String GetName ()
 
G4String GetSDname ()
 
 G4VHitsCollection ()
 
 G4VHitsCollection (G4String detName, G4String colNam)
 
virtual ~G4VHitsCollection ()
 
G4int operator== (const G4VHitsCollection &right) const
 
G4String GetName ()
 
G4String GetSDname ()
 
 G4VHitsCollection ()
 
 G4VHitsCollection (G4String detName, G4String colNam)
 
virtual ~G4VHitsCollection ()
 
G4int operator== (const G4VHitsCollection &right) const
 
G4StringGetName ()
 
G4StringGetSDname ()
 
void SetColID (G4int i)
 
G4int GetColID () const
 

Additional Inherited Members

- Protected Attributes inherited from G4VHitsCollection
G4String collectionName
 
G4String SDname
 
G4int colID
 

Detailed Description

template<typename T>
class G4TAtomicHitsMap< T >

This is an implementation of G4THitsMap<T> where the underlying type is G4atomic<T>, not just T. A static assert is provided to ensure that T is fundamental. This class should be used in lieu of G4THitsMap<T> 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<T> in thread-local instances.

Definition at line 73 of file G4TAtomicHitsMap.hh.

Member Typedef Documentation

template<typename T>
typedef container_type::const_iterator G4TAtomicHitsMap< T >::const_iterator

Definition at line 84 of file G4TAtomicHitsMap.hh.

template<typename T>
typedef std::map<G4int, mapped_type> G4TAtomicHitsMap< T >::container_type

Definition at line 82 of file G4TAtomicHitsMap.hh.

template<typename T>
typedef container_type::iterator G4TAtomicHitsMap< T >::iterator

Definition at line 83 of file G4TAtomicHitsMap.hh.

template<typename T>
typedef value_type* G4TAtomicHitsMap< T >::mapped_type

Definition at line 81 of file G4TAtomicHitsMap.hh.

template<typename T>
typedef G4atomic<T> G4TAtomicHitsMap< T >::value_type

Definition at line 77 of file G4TAtomicHitsMap.hh.

Constructor & Destructor Documentation

template<typename T >
G4TAtomicHitsMap< T >::G4TAtomicHitsMap ( )

Definition at line 155 of file G4TAtomicHitsMap.hh.

156  : theCollection(new container_type),
157  fMutex(G4MUTEX_INITIALIZER)
158 { }
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
std::map< G4int, mapped_type > container_type
template<typename T >
G4TAtomicHitsMap< T >::G4TAtomicHitsMap ( G4String  detName,
G4String  colNam 
)

Definition at line 161 of file G4TAtomicHitsMap.hh.

163  : G4VHitsCollection(detName,colNam),
164  theCollection(new container_type),
165  fMutex(G4MUTEX_INITIALIZER)
166 { }
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:175
std::map< G4int, mapped_type > container_type
template<typename T >
G4TAtomicHitsMap< T >::~G4TAtomicHitsMap ( )
virtual

Definition at line 169 of file G4TAtomicHitsMap.hh.

170 {
171  for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
172  delete itr->second;
173 
174  delete theCollection;
175  G4MUTEXDESTROY(fMutex);
176 }
#define G4MUTEXDESTROY(mutex)
Definition: G4Threading.hh:178

Member Function Documentation

template<typename T >
G4int G4TAtomicHitsMap< T >::add ( const G4int key,
value_type *&  aHit 
) const
inline

Definition at line 225 of file G4TAtomicHitsMap.hh.

226 {
227  if(theCollection->find(key) != theCollection->end())
228  *(*theCollection)[key] += *aHit;
229  else
230  {
231  G4AutoLock l(&fMutex);
232  (*theCollection)[key] = aHit;
233  }
234  G4AutoLock l(&fMutex);
235  return theCollection->size();
236 }
template<typename T >
G4int G4TAtomicHitsMap< T >::add ( const G4int key,
T &  aHit 
) const
inline

Definition at line 240 of file G4TAtomicHitsMap.hh.

241 {
242 
243  if(theCollection->find(key) != theCollection->end())
244  *(*theCollection)[key] += aHit;
245  else
246  {
247  value_type* hit = new value_type;
248  *hit = aHit;
249  G4AutoLock l(&fMutex);
250  (*theCollection)[key] = hit;
251  }
252  G4AutoLock l(&fMutex);
253  return theCollection->size();
254 }
G4atomic< T > value_type
template<typename T>
iterator G4TAtomicHitsMap< T >::begin ( )
inline

Definition at line 135 of file G4TAtomicHitsMap.hh.

135 { return theCollection->begin(); }
template<typename T>
const_iterator G4TAtomicHitsMap< T >::begin ( ) const
inline

Definition at line 138 of file G4TAtomicHitsMap.hh.

138 { return theCollection->begin(); }
template<typename T>
const_iterator G4TAtomicHitsMap< T >::cbegin ( ) const
inline

Definition at line 141 of file G4TAtomicHitsMap.hh.

141 { return theCollection->cbegin(); }
template<typename T>
const_iterator G4TAtomicHitsMap< T >::cend ( ) const
inline

Definition at line 142 of file G4TAtomicHitsMap.hh.

142 { return theCollection->cend(); }
template<typename T >
void G4TAtomicHitsMap< T >::clear ( void  )
inline

Definition at line 296 of file G4TAtomicHitsMap.hh.

297 {
298  G4AutoLock l(&fMutex);
299 
300  for(auto itr = theCollection->begin(); itr != theCollection->end(); itr++)
301  delete itr->second;
302 
303  theCollection->clear();
304 
305 }
template<typename T >
void G4TAtomicHitsMap< T >::DrawAllHits ( )
virtual

Reimplemented from G4VHitsCollection.

Definition at line 285 of file G4TAtomicHitsMap.hh.

286 { }
template<typename T>
iterator G4TAtomicHitsMap< T >::end ( )
inline

Definition at line 136 of file G4TAtomicHitsMap.hh.

136 { return theCollection->end(); }

Here is the caller graph for this function:

template<typename T>
const_iterator G4TAtomicHitsMap< T >::end ( ) const
inline

Definition at line 139 of file G4TAtomicHitsMap.hh.

139 { return theCollection->end(); }
template<typename T>
G4int G4TAtomicHitsMap< T >::entries ( ) const
inline

Definition at line 120 of file G4TAtomicHitsMap.hh.

121  {
122  return theCollection->size();
123  }
template<typename T>
iterator G4TAtomicHitsMap< T >::find ( G4int  p)
inline

Definition at line 144 of file G4TAtomicHitsMap.hh.

144 { return theCollection->find(p); }
const char * p
Definition: xmltok.h:285
template<typename T>
const_iterator G4TAtomicHitsMap< T >::find ( G4int  p) const
inline

Definition at line 145 of file G4TAtomicHitsMap.hh.

145 { return theCollection->find(p); }
const char * p
Definition: xmltok.h:285
template<typename T>
virtual G4VHit* G4TAtomicHitsMap< T >::GetHit ( size_t  ) const
inlinevirtual

Reimplemented from G4VHitsCollection.

Definition at line 128 of file G4TAtomicHitsMap.hh.

128 {return 0;}
template<typename T>
container_type* G4TAtomicHitsMap< T >::GetMap ( ) const
inline

Definition at line 109 of file G4TAtomicHitsMap.hh.

110  { return theCollection; }

Here is the caller graph for this function:

template<typename T>
virtual size_t G4TAtomicHitsMap< T >::GetSize ( ) const
inlinevirtual

Reimplemented from G4VHitsCollection.

Definition at line 129 of file G4TAtomicHitsMap.hh.

130  {
131  return theCollection->size();
132  }
template<typename T >
G4TAtomicHitsMap< T > & G4TAtomicHitsMap< T >::operator+= ( const G4TAtomicHitsMap< T > &  right) const

Definition at line 186 of file G4TAtomicHitsMap.hh.

187 {
188  for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
189  add(itr->first, *(itr->second));
190 
191  return (G4TAtomicHitsMap<T>&)(*this);
192 }
G4int add(const G4int &key, value_type *&aHit) const

Here is the call graph for this function:

template<typename T >
G4TAtomicHitsMap< T > & G4TAtomicHitsMap< T >::operator+= ( const G4THitsMap< T > &  right) const

Definition at line 196 of file G4TAtomicHitsMap.hh.

197 {
198  for(auto itr = rhs.GetMap()->begin(); itr != rhs.GetMap()->end(); itr++)
199  add(itr->first, *(itr->second));
200 
201  return (G4TAtomicHitsMap<T>&)(*this);
202 }
G4int add(const G4int &key, value_type *&aHit) const

Here is the call graph for this function:

template<typename T >
G4int G4TAtomicHitsMap< T >::operator== ( const G4TAtomicHitsMap< T > &  right) const

Definition at line 179 of file G4TAtomicHitsMap.hh.

180 {
181  return (collectionName == right.collectionName);
182 }
template<typename T >
G4TAtomicHitsMap< T >::value_type * G4TAtomicHitsMap< T >::operator[] ( G4int  key) const
inline

Definition at line 206 of file G4TAtomicHitsMap.hh.

207 {
208  if(theCollection->find(key) != theCollection->end())
209  return theCollection->find(key)->second;
210  else
211  {
212  G4AutoLock l(&fMutex);
213  if(theCollection->find(key) == theCollection->end())
214  {
215  value_type* ptr = new value_type;
216  (*theCollection)[key] = ptr;
217  return ptr;
218  } else
219  return theCollection->find(key)->second;
220  }
221 }
G4atomic< T > value_type
template<typename T >
void G4TAtomicHitsMap< T >::PrintAllHits ( )
virtual

Reimplemented from G4VHitsCollection.

Definition at line 289 of file G4TAtomicHitsMap.hh.

290 {
291  G4cout << "G4TAtomicHitsMap " << SDname << " / " << collectionName << " --- "
292  << entries() << " entries" << G4endl;
293 }
G4int entries() const
G4GLOB_DLL std::ostream G4cout
#define G4endl
Definition: G4ios.hh:61
template<typename T >
G4int G4TAtomicHitsMap< T >::set ( const G4int key,
value_type *&  aHit 
) const
inline

Definition at line 258 of file G4TAtomicHitsMap.hh.

259 {
260  if(theCollection->find(key) != theCollection->end())
261  delete (*theCollection)[key]->second;
262 
263  (*theCollection)[key] = aHit;
264  G4AutoLock l(&fMutex);
265  return theCollection->size();
266 }
template<typename T >
G4int G4TAtomicHitsMap< T >::set ( const G4int key,
T &  aHit 
) const
inline

Definition at line 270 of file G4TAtomicHitsMap.hh.

271 {
272  if(theCollection->find(key) != theCollection->end())
273  *(*theCollection)[key] = aHit;
274  else
275  {
276  value_type* hit = new value_type;
277  *hit = aHit;
278  (*theCollection)[key] = hit;
279  }
280  G4AutoLock l(&fMutex);
281  return theCollection->size();
282 }
G4atomic< T > value_type

The documentation for this class was generated from the following file: