29 #define G4THitsMap_h 1
35 #include <unordered_map>
47 template <
typename T,
typename Map_t = std::map<G4
int, T*> >
51 typedef std::multimap<G4int, T*>
mmap_t;
53 typedef std::unordered_multimap<G4int, T*>
uommap_t;
55 #define is_same_t(_Tp, _Up) std::is_same<_Tp, _Up>::value
56 #define is_multimap_t(_Mp) std::is_same<_Mp, mmap_t>::value
57 #define is_uommap_t(_Mp) std::is_same<_Mp, uommap_t>::value
58 #define is_mmap_t(_Mp) (is_multimap_t(_Mp) || is_uommap_t(_Mp))
59 #define is_fundamental_t(_Tp) std::is_fundamental<_Tp>::value
61 template <
bool _Bp,
typename _Tp =
void>
65 template <
typename U = T, enable_if_t<is_fundamental_t(U),
int> = 0>
71 template <
typename U = T, enable_if_t<! is_fundamental_t(U),
int> = 0>
96 template <
typename U,
typename MapU_t>
99 MapU_t* aHitsMap = right.
GetMap();
100 for(
auto itr = aHitsMap->begin(); itr != aHitsMap->end(); ++itr)
101 add<U, map_type>(itr->first, *(itr->second));
117 inline typename Map_t::size_type
size()
121 {
return itr->first; }
124 {
return itr->first; }
126 template <
typename MapU_t = Map_t, enable_if_t< !is_mmap_t(MapU_t),
int> = 0>
130 template <
typename MapU_t = Map_t, enable_if_t< is_mmap_t(MapU_t),
int> = 0>
137 {
return itr->second; }
140 {
return itr->second; }
174 template <
typename U =
T,
175 typename MapU_t = Map_t,
176 enable_if_t<is_same_t(U, T) && ! is_mmap_t(MapU_t), int> = 0>
180 if(theHitsMap->find(key) == theHitsMap->end())
181 theHitsMap->insert(
pair_t(key,
new T(*aHit)));
183 *theHitsMap->find(key)->second += *aHit;
184 return theHitsMap->size();
189 template <
typename U =
T,
190 typename MapU_t = Map_t,
191 enable_if_t<(is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
195 theHitsMap->insert(
pair_t(key, aHit));
196 return theHitsMap->size();
202 template <
typename U =
T,
203 typename MapU_t = Map_t,
204 enable_if_t<(!is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
210 theHitsMap->insert(
pair_t(key, hit));
211 return theHitsMap->size();
221 template <
typename U =
T,
222 typename MapU_t = Map_t,
223 enable_if_t<(is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
227 if(theHitsMap->find(key) == theHitsMap->end())
228 theHitsMap->insert(
pair_t(key,
new T(aHit)));
230 *theHitsMap->find(key)->second += aHit;
231 return theHitsMap->size();
237 template <
typename U =
T,
238 typename MapU_t = Map_t,
239 enable_if_t<(! is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
243 if(theHitsMap->find(key) == theHitsMap->end())
245 *theHitsMap->find(key)->second += aHit;
246 return theHitsMap->size();
251 template <
typename U =
T,
252 typename MapU_t = Map_t,
253 enable_if_t<(is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
257 theHitsMap->insert(
pair_t(key,
new T(aHit)));
258 return theHitsMap->size();
264 template <
typename U =
T,
265 typename MapU_t = Map_t,
266 enable_if_t<(!is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
272 theHitsMap->insert(
pair_t(key, hit));
273 return theHitsMap->size();
284 template <
typename U =
T,
285 typename MapU_t = Map_t,
286 enable_if_t<(is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
287 inline size_t set(
const G4int& key, U*& aHit)
const
290 if(theHitsMap->find(key) != theHitsMap->end())
291 delete theHitsMap->find(key)->second;
292 theHitsMap->find(key)->second = aHit;
293 return theHitsMap->size();
298 template <
typename U =
T,
299 typename MapU_t = Map_t,
300 enable_if_t<(is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
301 inline size_t set(
const G4int& key, U*& aHit)
const
304 if(theHitsMap->find(key) != theHitsMap->end())
305 theHitsMap->insert(
pair_t(key, aHit));
308 delete theHitsMap->find(key)->second;
309 theHitsMap->find(key)->second = aHit;
311 return theHitsMap->size();
316 template <
typename U =
T,
317 typename MapU_t = Map_t,
318 enable_if_t<(!is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
319 inline size_t set(
const G4int & key, U*& aHit)
const
323 if(theHitsMap->find(key) == theHitsMap->end())
324 theHitsMap->insert(std::make_pair(key, hit =
allocate()));
326 hit = theHitsMap->find(key)->second;
328 return theHitsMap->size();
333 template <
typename U =
T,
334 typename MapU_t = Map_t,
335 enable_if_t<(!is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
336 inline size_t set(
const G4int& key, U*& aHit)
const
341 if(theHitsMap->find(key) != theHitsMap->end())
342 theHitsMap->insert(
pair_t(key, hit));
345 delete theHitsMap->find(key)->second;
346 theHitsMap->find(key)->second = hit;
348 return theHitsMap->size();
359 template <
typename U =
T,
360 typename MapU_t = Map_t,
361 enable_if_t<(is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
362 inline size_t set(
const G4int& key, U& aHit)
const
366 if(theHitsMap->find(key) != theHitsMap->end())
367 hit = theHitsMap->find(key)->second;
371 return theHitsMap->size();
376 template <
typename U =
T,
377 typename MapU_t = Map_t,
378 enable_if_t<(is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
379 inline size_t set(
const G4int& key, U& aHit)
const
382 if(theHitsMap->find(key) != theHitsMap->end())
383 *theHitsMap->find(key)->second = aHit;
385 theHitsMap->insert(
pair_t(key,
new T(aHit)));
386 return theHitsMap->size();
391 template <
typename U =
T,
392 typename MapU_t = Map_t,
393 enable_if_t<(!is_same_t(U, T) && ! is_mmap_t(MapU_t)), int> = 0>
394 inline size_t set(
const G4int & key, U& aHit)
const
398 if(theHitsMap->find(key) == theHitsMap->end())
399 theHitsMap->insert(std::make_pair(key, hit =
allocate()));
401 hit = theHitsMap->find(key)->second;
403 return theHitsMap->size();
408 template <
typename U =
T,
409 typename MapU_t = Map_t,
410 enable_if_t<(!is_same_t(U, T) && is_mmap_t(MapU_t)), int> = 0>
411 inline size_t set(
const G4int& key, U& aHit)
const
416 if(theHitsMap->find(key) != theHitsMap->end())
417 *theHitsMap->find(key)->second = *hit;
419 theHitsMap->insert(
pair_t(key, hit));
420 return theHitsMap->size();
430 template <
typename MapU_t = Map_t,
431 enable_if_t<! is_mmap_t(MapU_t), int> = 0>
435 if(theHitsMap->find(key) != theHitsMap->end())
436 return theHitsMap->find(key)->second;
440 template <
typename MapU_t = Map_t,
441 enable_if_t<is_mmap_t(MapU_t), int> = 0>
445 static bool _first =
true;
449 G4Exception(
"G4THitsMap operator[]",
"calling [] on multimap",
454 iterator itr = theHitsMap->find(key);
455 if(itr != theHitsMap->end())
457 std::advance(itr, theHitsMap->count(key)-1);
468 #undef is_fundamental_t
473 template <
typename T,
typename Map_t>
476 theCollection = (
void*)
new Map_t;
481 template <
typename T,
typename Map_t>
490 template <
typename T,
typename Map_t>
494 for(
iterator itr = theHitsMap->begin(); itr != theHitsMap->end(); itr++)
501 template <
typename T,
typename Map_t>
509 template <
typename T,
typename Map_t>
515 template <
typename T,
typename Map_t>
518 G4cout <<
"G4THitsMap " << SDname <<
" / " << collectionName <<
" --- "
519 << entries() <<
" entries" <<
G4endl;
536 template <
typename T,
typename Map_t>
539 Map_t * theHitsMap = GetMap();
540 for(
iterator itr = theHitsMap->begin(); itr != theHitsMap->end(); itr++)
553 template <
typename _Tp>
564 using parent_type::operator +=;
565 using parent_type::operator ==;
566 using parent_type::operator [];
584 template <
typename _Tp>
595 using parent_type::operator +=;
596 using parent_type::operator ==;
597 using parent_type::operator [];
615 template <
typename _Tp>
617 :
public G4VTHitsMap<_Tp, std::unordered_map<G4int, _Tp*>>
627 using parent_type::operator +=;
628 using parent_type::operator ==;
629 using parent_type::operator [];
647 template <
typename _Tp>
649 :
public G4VTHitsMap<_Tp, std::unordered_multimap<G4int, _Tp*>>
659 using parent_type::operator +=;
660 using parent_type::operator ==;
661 using parent_type::operator [];