Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 27 // Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 19/07/2024 28 29 #include <algorithm> 30 31 // 32 // public functions 33 // 34 35 // Default constructor (1) 36 //_____________________________________________________________________________ 37 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 38 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 39 const G4String& name, 40 G4MergeMode mergeMode) 41 : G4VAccumulable(name, mergeMode), 42 fUMap(), 43 fMergeFunction(GetMergeFunction<T>(mergeMode)) 44 { 45 if (G4Accumulables::VerboseLevel > 1 ) { 46 G4cout << "G4AccUnorderedMap ctor1" << G4endl; 47 } 48 } 49 50 // Constructor (2) 51 //_____________________________________________________________________________ 52 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 53 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 54 std::size_t bucket_count, 55 G4MergeMode mergeMode, 56 const Allocator& allocator) 57 : G4VAccumulable(mergeMode), 58 fUMap(bucket_count, allocator), 59 fMergeFunction(GetMergeFunction<T>(mergeMode)) 60 { 61 if (G4Accumulables::VerboseLevel > 1 ) { 62 G4cout << "G4AccUnorderedMap ctor2" << G4endl; 63 } 64 } 65 66 // Constructor (2) with name 67 //_____________________________________________________________________________ 68 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 69 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 70 const G4String& name, 71 std::size_t bucket_count, 72 G4MergeMode mergeMode, 73 const Allocator& allocator) 74 : G4VAccumulable(name, mergeMode), 75 fUMap(bucket_count, allocator), 76 fMergeFunction(GetMergeFunction<T>(mergeMode)) 77 { 78 if (G4Accumulables::VerboseLevel > 1 ) { 79 G4cout << "G4AccUnorderedMap ctor2n" << G4endl; 80 } 81 } 82 83 // Constructor (3) 84 //_____________________________________________________________________________ 85 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 86 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 87 std::size_t bucket_count, 88 const Allocator& allocator, 89 G4MergeMode mergeMode) 90 : G4VAccumulable(mergeMode), 91 fUMap(bucket_count, allocator), 92 fMergeFunction(GetMergeFunction<T>(mergeMode)) 93 { 94 if (G4Accumulables::VerboseLevel > 1 ) { 95 G4cout << "G4AccUnorderedMap ctor3" << G4endl; 96 } 97 } 98 99 // Constructor (3) with name 100 //_____________________________________________________________________________ 101 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 102 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 103 const G4String& name, 104 std::size_t bucket_count, 105 const Allocator& allocator, 106 G4MergeMode mergeMode) 107 : G4VAccumulable(name, mergeMode), 108 fUMap(bucket_count, allocator), 109 fMergeFunction(GetMergeFunction<T>(mergeMode)) 110 { 111 if (G4Accumulables::VerboseLevel > 1 ) { 112 G4cout << "G4AccUnorderedMap ctor3n" << G4endl; 113 } 114 } 115 116 // Constructor (4) 117 //_____________________________________________________________________________ 118 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 119 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 120 std::size_t bucket_count, 121 const Hash& hash, 122 const Allocator& allocator, 123 G4MergeMode mergeMode) 124 : G4VAccumulable(mergeMode), 125 fUMap(bucket_count, hash, allocator), 126 fMergeFunction(GetMergeFunction<T>(mergeMode)) 127 { 128 if (G4Accumulables::VerboseLevel > 1 ) { 129 G4cout << "G4AccUnorderedMap ctor4" << G4endl; 130 } 131 } 132 133 // Constructor (4) with name 134 //_____________________________________________________________________________ 135 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 136 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 137 const G4String& name, 138 std::size_t bucket_count, 139 const Hash& hash, 140 const Allocator& allocator, 141 G4MergeMode mergeMode) 142 : G4VAccumulable(name, mergeMode), 143 fUMap(bucket_count, hash, allocator), 144 fMergeFunction(GetMergeFunction<T>(mergeMode)) 145 { 146 if (G4Accumulables::VerboseLevel > 1 ) { 147 G4cout << "G4AccUnorderedMap ctor4n" << G4endl; 148 } 149 } 150 151 // Constructor (5) 152 //_____________________________________________________________________________ 153 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 154 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 155 const Allocator& allocator, 156 G4MergeMode mergeMode) 157 : G4VAccumulable(mergeMode), 158 fUMap(allocator), 159 fMergeFunction(GetMergeFunction<T>(mergeMode)) 160 { 161 if (G4Accumulables::VerboseLevel > 1 ) { 162 G4cout << "G4AccUnorderedMap ctor5" << G4endl; 163 } 164 } 165 166 // Constructor (5) with name 167 //_____________________________________________________________________________ 168 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 169 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 170 const G4String& name, 171 const Allocator& allocator, 172 G4MergeMode mergeMode) 173 : G4VAccumulable(name, mergeMode), 174 fUMap(allocator), 175 fMergeFunction(GetMergeFunction<T>(mergeMode)) 176 { 177 if (G4Accumulables::VerboseLevel > 1 ) { 178 G4cout << "G4AccUnorderedMap ctor5n" << G4endl; 179 } 180 } 181 182 // Constructor (13) 183 //_____________________________________________________________________________ 184 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 185 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 186 std::initializer_list<std::pair<const Key,T>> init, 187 G4MergeMode mergeMode, 188 std::size_t bucket_count, 189 const Hash& hash, 190 const KeyEqual& equal, 191 const Allocator& allocator) 192 : G4VAccumulable(mergeMode), 193 fUMap(init, bucket_count, hash, equal, allocator), 194 fMergeFunction(GetMergeFunction<T>(mergeMode)) 195 { 196 if (G4Accumulables::VerboseLevel > 1 ) { 197 G4cout << "G4AccUnorderedMap ctor13" << G4endl; 198 } 199 } 200 201 // Constructor (13) with name 202 //_____________________________________________________________________________ 203 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 204 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 205 const G4String& name, 206 std::initializer_list<std::pair<const Key,T>> init, 207 G4MergeMode mergeMode, 208 std::size_t bucket_count, 209 const Hash& hash, 210 const KeyEqual& equal, 211 const Allocator& allocator) 212 : G4VAccumulable(name, mergeMode), 213 fUMap(init, bucket_count, hash, equal, allocator), 214 fMergeFunction(GetMergeFunction<T>(mergeMode)) 215 { 216 if (G4Accumulables::VerboseLevel > 1 ) { 217 G4cout << "G4AccUnorderedMap ctor13n" << G4endl; 218 } 219 } 220 221 // Copy ctor 222 //_____________________________________________________________________________ 223 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 224 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 225 const G4AccUnorderedMap& rhs, 226 const Allocator& allocator) 227 : G4VAccumulable(rhs), 228 fUMap(rhs, allocator), 229 fMergeFunction(rhs.fMergeFunction) 230 {} 231 232 // Move ctor 233 //_____________________________________________________________________________ 234 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 235 G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::G4AccUnorderedMap( 236 G4AccUnorderedMap&& rhs, 237 const Allocator& allocator) 238 : G4VAccumulable(std::move(rhs)), 239 fUMap(std::move(rhs), allocator), 240 fMergeFunction(rhs.fMergeFunction) 241 {} 242 243 //_____________________________________________________________________________ 244 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 245 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Merge(const G4VAccumulable& other) 246 { 247 const auto& otherMap = static_cast<const G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>&>(other); 248 249 if (G4Accumulables::VerboseLevel > 2 ) { 250 G4cout << "G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Merge: " << G4endl; 251 G4cout << "destination: "; 252 for (const auto& [key, v] : fUMap) { 253 G4cout << "[ " << key << ", " << v << " ], "; 254 } 255 G4cout << G4endl; 256 G4cout << "merged data: "; 257 for (const auto& [key, v] : otherMap.fUMap) { 258 G4cout << "[ " << key << ", " << v << " ], "; 259 } 260 G4cout << G4endl; 261 } 262 263 for (const auto& [key, value] : otherMap.fUMap) { 264 if ( fUMap.find(key) == fUMap.end()) { 265 (fUMap)[key] = value; 266 } 267 else { 268 (fUMap)[key] = fMergeFunction((fUMap)[key], value); 269 } 270 } 271 } 272 273 //_____________________________________________________________________________ 274 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 275 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Reset() 276 { 277 for (auto& [key, value] : fUMap) { 278 fUMap[key] = fInitValue; 279 } 280 } 281 282 //_____________________________________________________________________________ 283 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 284 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::Print( 285 G4PrintOptions options) const 286 { 287 if (options.Has(G4PrintOptions::kType)) { 288 G4cout << "unordered_map<" << typeid(Key).name() << ", " << typeid(T).name() << ">: "; 289 } 290 291 PrintBase(options); 292 293 bool first = true; 294 for (const auto& [key, value] : fUMap) { 295 if (! first) { G4cout << ", "; } 296 G4cout << "[ " << key << ", " << value << "] "; 297 first = false; 298 } 299 G4cout << G4endl; 300 } 301 302 //_____________________________________________________________________________ 303 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 304 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::SetMergeMode(G4MergeMode value) 305 { 306 G4VAccumulable::SetMergeMode(value); 307 fMergeFunction = GetMergeFunction<T>(fMergeMode); 308 } 309 310 //_____________________________________________________________________________ 311 template <class Key, class T, class Hash, class KeyEqual, class Allocator> 312 void G4AccUnorderedMap<Key, T, Hash, KeyEqual, Allocator>::SetInitValue(const T& value) 313 { 314 fInitValue = value; 315 } 316