File indexing completed on 2025-08-02 08:28:13
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include <algorithm>
0030
0031
0032
0033
0034
0035
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
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
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
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
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
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
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
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
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 }