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 Hash, class KeyEqual, class Allocator>
0038 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0039   const G4String& name,
0040   G4MergeMode mergeMode)
0041 : G4VAccumulable(name, mergeMode),
0042   fUMap(),
0043   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0044 {
0045   if (G4Accumulables::VerboseLevel > 1 ) {
0046     G4cout << "G4AccUnorderedMap ctor1" << G4endl;
0047   }
0048 }
0049 
0050 // Constructor (2)
0051 //_____________________________________________________________________________
0052 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0053 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0054   std::size_t bucket_count,
0055   G4MergeMode mergeMode,
0056   const Allocator& allocator)
0057 : G4VAccumulable(mergeMode),
0058   fUMap(bucket_count, allocator),
0059   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0060 {
0061   if (G4Accumulables::VerboseLevel > 1 ) {
0062     G4cout << "G4AccUnorderedMap ctor2" << G4endl;
0063   }
0064 }
0065 
0066 // Constructor (2) with name
0067 //_____________________________________________________________________________
0068 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0069 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0070   const G4String& name,
0071   std::size_t bucket_count,
0072   G4MergeMode mergeMode,
0073   const Allocator& allocator)
0074 : G4VAccumulable(name, mergeMode),
0075   fUMap(bucket_count, allocator),
0076   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0077 {
0078   if (G4Accumulables::VerboseLevel > 1 ) {
0079     G4cout << "G4AccUnorderedMap ctor2n" << G4endl;
0080   }
0081 }
0082 
0083 // Constructor (3)
0084 //_____________________________________________________________________________
0085 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0086 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0087   std::size_t bucket_count,
0088   const Allocator& allocator,
0089   G4MergeMode mergeMode)
0090 : G4VAccumulable(mergeMode),
0091   fUMap(bucket_count, allocator),
0092   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0093 {
0094   if (G4Accumulables::VerboseLevel > 1 ) {
0095     G4cout << "G4AccUnorderedMap ctor3" << G4endl;
0096   }
0097 }
0098 
0099 // Constructor (3) with name
0100 //_____________________________________________________________________________
0101 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0102 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0103   const G4String& name,
0104   std::size_t bucket_count,
0105   const Allocator& allocator,
0106   G4MergeMode mergeMode)
0107 : G4VAccumulable(name, mergeMode),
0108   fUMap(bucket_count, allocator),
0109   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0110 {
0111   if (G4Accumulables::VerboseLevel > 1 ) {
0112     G4cout << "G4AccUnorderedMap ctor3n" << G4endl;
0113   }
0114 }
0115 
0116 // Constructor (4)
0117 //_____________________________________________________________________________
0118 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0119 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0120   std::size_t bucket_count,
0121   const Hash& hash,
0122   const Allocator& allocator,
0123   G4MergeMode mergeMode)
0124 : G4VAccumulable(mergeMode),
0125   fUMap(bucket_count, hash, allocator),
0126   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0127 {
0128   if (G4Accumulables::VerboseLevel > 1 ) {
0129     G4cout << "G4AccUnorderedMap ctor4" << G4endl;
0130   }
0131 }
0132 
0133 // Constructor (4) with name
0134 //_____________________________________________________________________________
0135 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0136 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0137   const G4String& name,
0138   std::size_t bucket_count,
0139   const Hash& hash,
0140   const Allocator& allocator,
0141   G4MergeMode mergeMode)
0142 : G4VAccumulable(name, mergeMode),
0143   fUMap(bucket_count, hash, allocator),
0144   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0145 {
0146   if (G4Accumulables::VerboseLevel > 1 ) {
0147     G4cout << "G4AccUnorderedMap ctor4n" << G4endl;
0148   }
0149 }
0150 
0151 // Constructor (5)
0152 //_____________________________________________________________________________
0153 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0154 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0155   const Allocator& allocator,
0156   G4MergeMode mergeMode)
0157 : G4VAccumulable(mergeMode),
0158   fUMap(allocator),
0159   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0160 {
0161   if (G4Accumulables::VerboseLevel > 1 ) {
0162     G4cout << "G4AccUnorderedMap ctor5" << G4endl;
0163   }
0164 }
0165 
0166 // Constructor (5) with name
0167 //_____________________________________________________________________________
0168 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0169 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0170   const G4String& name,
0171   const Allocator& allocator,
0172   G4MergeMode mergeMode)
0173 : G4VAccumulable(name, mergeMode),
0174   fUMap(allocator),
0175   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0176 {
0177   if (G4Accumulables::VerboseLevel > 1 ) {
0178     G4cout << "G4AccUnorderedMap ctor5n" << G4endl;
0179   }
0180 }
0181 
0182 // Constructor (13)
0183 //_____________________________________________________________________________
0184 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0185 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0186   std::initializer_list<std::pair<const Key,T>> init,
0187   G4MergeMode mergeMode,
0188   std::size_t bucket_count,
0189   const Hash& hash,
0190   const KeyEqual& equal,
0191   const Allocator& allocator)
0192 : G4VAccumulable(mergeMode),
0193   fUMap(init, bucket_count, hash, equal, allocator),
0194   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0195 {
0196   if (G4Accumulables::VerboseLevel > 1 ) {
0197     G4cout << "G4AccUnorderedMap ctor13" << G4endl;
0198   }
0199 }
0200 
0201 // Constructor (13) with name
0202 //_____________________________________________________________________________
0203 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0204 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0205   const G4String& name,
0206   std::initializer_list<std::pair<const Key,T>> init,
0207   G4MergeMode mergeMode,
0208   std::size_t bucket_count,
0209   const Hash& hash,
0210   const KeyEqual& equal,
0211   const Allocator& allocator)
0212 : G4VAccumulable(name, mergeMode),
0213   fUMap(init, bucket_count, hash, equal, allocator),
0214   fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0215 {
0216   if (G4Accumulables::VerboseLevel > 1 ) {
0217     G4cout << "G4AccUnorderedMap ctor13n" << G4endl;
0218   }
0219 }
0220 
0221 // Copy ctor
0222 //_____________________________________________________________________________
0223 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0224 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0225   const G4AccUnorderedMap& rhs,
0226   const Allocator& allocator)
0227 : G4VAccumulable(rhs),
0228   fUMap(rhs, allocator),
0229   fMergeFunction(rhs.fMergeFunction)
0230 {}
0231 
0232 // Move ctor
0233 //_____________________________________________________________________________
0234 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0235 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap(
0236   G4AccUnorderedMap&& rhs,
0237   const Allocator& allocator)
0238 : G4VAccumulable(std::move(rhs)),
0239   fUMap(std::move(rhs), allocator),
0240   fMergeFunction(rhs.fMergeFunction)
0241 {}
0242 
0243 //_____________________________________________________________________________
0244 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0245 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Merge(const G4VAccumulable& other)
0246 {
0247   const auto& otherMap = static_cast<const G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>&>(other);
0248 
0249   if (G4Accumulables::VerboseLevel > 2 ) {
0250     G4cout << "G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Merge: " << G4endl;
0251     G4cout << "destination: ";
0252     for (const auto& [key, v] : fUMap) {
0253       G4cout << "[ " << key << ", " << v << " ], ";
0254     }
0255     G4cout << G4endl;
0256     G4cout << "merged data: ";
0257     for (const auto& [key, v] : otherMap.fUMap) {
0258       G4cout << "[ " << key << ", " << v << " ], ";
0259     }
0260     G4cout << G4endl;
0261   }
0262 
0263   for (const auto& [key, value] : otherMap.fUMap) {
0264     if ( fUMap.find(key) == fUMap.end()) {
0265       (fUMap)[key] = value;
0266     }
0267     else {
0268       (fUMap)[key] = fMergeFunction((fUMap)[key], value);
0269     }
0270   }
0271 }
0272 
0273 //_____________________________________________________________________________
0274 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0275 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Reset()
0276 {
0277   for (auto& [key, value] : fUMap) {
0278     fUMap[key] = fInitValue;
0279   }
0280 }
0281 
0282 //_____________________________________________________________________________
0283 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0284 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Print(
0285   G4PrintOptions options) const
0286 {
0287   if (options.Has(G4PrintOptions::kType)) {
0288     G4cout << "unordered_map<" << typeid(Key).name() << ", " << typeid(T).name() << ">: ";
0289   }
0290 
0291   PrintBase(options);
0292 
0293   bool first = true;
0294   for (const auto& [key, value] : fUMap) {
0295     if (! first) { G4cout << ", "; }
0296     G4cout << "[ " << key << ", " << value << "] ";
0297     first = false;
0298   }
0299   G4cout << G4endl;
0300 }
0301 
0302 //_____________________________________________________________________________
0303 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0304 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::SetMergeMode(G4MergeMode value)
0305 {
0306   G4VAccumulable::SetMergeMode(value);
0307   fMergeFunction = G4Accumulables::GetMergeFunction<T>(fMergeMode);
0308 }
0309 
0310 //_____________________________________________________________________________
0311 template <class Key, class T, class Hash, class KeyEqual, class Allocator>
0312 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::SetInitValue(const T& value)
0313 {
0314   fInitValue = value;
0315 }