Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-02 08:28:13

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 
0027 // Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 19/07/2024
0028 
0029 #include <algorithm>
0030 
0031 //
0032 // public functions
0033 //
0034 
0035 // Default constructor (1)
0036 //_____________________________________________________________________________
0037 template <class Key, class T, class Compare, class Allocator>
0038 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0039   const G4String& name,
0040   G4MergeMode mergeMode)
0041 : G4VAccumulable(name, mergeMode),
0042   fMap(),
0043   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0044 {
0045   if (G4Accumulables::VerboseLevel > 1 ) {
0046     G4cout << "G4AccMap ctor1" << G4endl;
0047   }
0048 }
0049 
0050 // Constructor (2)
0051 //_____________________________________________________________________________
0052 template <class Key, class T, class Compare, class Allocator>
0053 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0054   const Compare& comp,
0055   G4MergeMode mergeMode,
0056   const Allocator& allocator)
0057 : G4VAccumulable(mergeMode),
0058   fMap(comp, allocator),
0059   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0060 {
0061   if (G4Accumulables::VerboseLevel > 1 ) {
0062     G4cout << "G4AccMap ctor2" << G4endl;
0063   }
0064 }
0065 
0066 // Constructor (2) with name
0067 //_____________________________________________________________________________
0068 template <class Key, class T, class Compare, class Allocator>
0069 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0070   const G4String& name,
0071   const Compare& comp,
0072   G4MergeMode mergeMode,
0073   const Allocator& allocator)
0074 : G4VAccumulable(name, mergeMode),
0075   fMap(comp, allocator),
0076   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0077 {
0078   if (G4Accumulables::VerboseLevel > 1 ) {
0079     G4cout << "G4AccMap ctor2n" << G4endl;
0080   }
0081 }
0082 
0083 // Constructor (3)
0084 //_____________________________________________________________________________
0085 template <class Key, class T, class Compare, class Allocator>
0086 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0087   const Allocator& allocator,
0088   G4MergeMode mergeMode)
0089 : G4VAccumulable(mergeMode),
0090   fMap(allocator),
0091   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0092 {
0093   if (G4Accumulables::VerboseLevel > 1 ) {
0094     G4cout << "G4AccMap ctor3" << G4endl;
0095   }
0096 }
0097 
0098 // Constructor (3) with name
0099 //_____________________________________________________________________________
0100 template <class Key, class T, class Compare, class Allocator>
0101 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0102   const G4String& name,
0103   const Allocator& allocator,
0104   G4MergeMode mergeMode)
0105 : G4VAccumulable(name, mergeMode),
0106   fMap(allocator),
0107   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0108 {
0109   if (G4Accumulables::VerboseLevel > 1 ) {
0110     G4cout << "G4AccMap ctor3n" << G4endl;
0111   }
0112 }
0113 
0114 // Constructor (10)
0115 //_____________________________________________________________________________
0116 template <class Key, class T, class Compare, class Allocator>
0117 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0118   std::initializer_list<std::pair<const Key,T>> init,
0119   G4MergeMode mergeMode,
0120   const Compare& comp,
0121   const Allocator& allocator)
0122 : G4VAccumulable(mergeMode),
0123   fMap(init, comp, allocator),
0124   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0125 {
0126   if (G4Accumulables::VerboseLevel > 1 ) {
0127     G4cout << "G4AccMap ctor10" << G4endl;
0128   }
0129 }
0130 
0131 // Constructor (10) with name
0132 //_____________________________________________________________________________
0133 template <class Key, class T, class Compare, class Allocator>
0134 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0135   const G4String& name,
0136   std::initializer_list<std::pair<const Key,T>> init,
0137   G4MergeMode mergeMode,
0138   const Compare& comp,
0139   const Allocator& allocator)
0140 : G4VAccumulable(name, mergeMode),
0141   fMap(init, comp, allocator),
0142   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0143 {
0144   if (G4Accumulables::VerboseLevel > 1 ) {
0145     G4cout << "G4AccMap ctor10n" << G4endl;
0146   }
0147 }
0148 
0149 // Copy ctor
0150 //_____________________________________________________________________________
0151 template <class Key, class T, class Compare, class Allocator>
0152 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0153   const G4AccMap& rhs,
0154   const Allocator& allocator)
0155 : G4VAccumulable(rhs),
0156   fMap(rhs, allocator),
0157   fMergeFunction(rhs.fMergeFunction)
0158 {}
0159 
0160 // Move ctor
0161 //_____________________________________________________________________________
0162 template <class Key, class T, class Compare, class Allocator>
0163 G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
0164   G4AccMap&& rhs,
0165   const Allocator& allocator)
0166 : G4VAccumulable(std::move(rhs)),
0167   fMap(std::move(rhs), allocator),
0168   fMergeFunction(rhs.fMergeFunction)
0169 {}
0170 
0171 //_____________________________________________________________________________
0172 template <class Key, class T, class Compare, class Allocator>
0173 void G4AccMap<Key, T, Compare, Allocator>::Merge(const G4VAccumulable& other)
0174 {
0175   const auto& otherMap = static_cast<const G4AccMap<Key, T, Compare, Allocator>&>(other);
0176 
0177   if (G4Accumulables::VerboseLevel > 2 ) {
0178     G4cout << "G4AccMap<Key, T, Compare, Allocator>::Merge: " << G4endl;
0179     G4cout << "destination: ";
0180     for (const auto& [key, v] : fMap) {
0181       G4cout << "[ " << key << ", " << v << " ], ";
0182     }
0183     G4cout << G4endl;
0184     G4cout << "merged data: ";
0185     for (const auto& [key, v] : otherMap.fMap) {
0186       G4cout << "[ " << key << ", " << v << " ], ";
0187     }
0188     G4cout << G4endl;
0189   }
0190 
0191   for (const auto& [key, value] : otherMap.fMap) {
0192     if ( fMap.find(key) == fMap.end()) {
0193       fMap[key] = value;
0194     }
0195     else {
0196       fMap[key] = fMergeFunction(fMap[key], value);
0197     }
0198   }
0199 }
0200 
0201 //_____________________________________________________________________________
0202 template <class Key, class T, class Compare, class Allocator>
0203 void G4AccMap<Key, T, Compare, Allocator>::Reset()
0204 {
0205   for (auto& [key, value] :fMap) {
0206     fMap[key] = fInitValue;
0207   }
0208 }
0209 
0210 //_____________________________________________________________________________
0211 template <class Key, class T, class Compare, class Allocator>
0212 void G4AccMap<Key, T, Compare, Allocator>::Print(
0213   G4PrintOptions options) const
0214 {
0215   if (options.Has(G4PrintOptions::kType)) {
0216     G4cout << "map<" << typeid(Key).name() << ", " << typeid(T).name() << ">: ";
0217   }
0218 
0219   PrintBase(options);
0220 
0221   bool first = true;
0222   for (const auto& [key, value] : fMap) {
0223     if (! first) { G4cout << ", "; }
0224     G4cout << "[ " << key << ", " << value << "] ";
0225     first = false;
0226   }
0227   G4cout << G4endl;
0228 }
0229 
0230 //_____________________________________________________________________________
0231 template <class Key, class T, class Compare, class Allocator>
0232 void G4AccMap<Key, T, Compare, Allocator>::SetMergeMode(G4MergeMode value)
0233 {
0234   G4VAccumulable::SetMergeMode(value);
0235   fMergeFunction = G4Accumulables::GetMergeFunction<T>(fMergeMode);
0236 }
0237 
0238 //_____________________________________________________________________________
0239 template <class Key, class T, class Compare, class Allocator>
0240 void G4AccMap<Key, T, Compare, Allocator>::SetInitValue(const T& value)
0241 {
0242   fInitValue = value;
0243 }