Files
geant4/source/analysis/accumulables/include/G4AccMap.icc
T
2024-12-06 11:11:40 +01:00

244 lines
8.2 KiB
Plaintext

//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The Geant4 software is copyright of the Copyright Holders of *
// * the Geant4 Collaboration. It is provided under the terms and *
// * conditions of the Geant4 Software License, included in the file *
// * LICENSE and available at http://cern.ch/geant4/license . These *
// * include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GEANT4 collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the Geant4 Software license. *
// ********************************************************************
//
// Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 19/07/2024
#include <algorithm>
//
// public functions
//
// Default constructor (1)
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4String& name,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fMap(),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor1" << G4endl;
}
}
// Constructor (2)
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const Compare& comp,
G4MergeMode mergeMode,
const Allocator& allocator)
: G4VAccumulable(mergeMode),
fMap(comp, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor2" << G4endl;
}
}
// Constructor (2) with name
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4String& name,
const Compare& comp,
G4MergeMode mergeMode,
const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fMap(comp, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor2n" << G4endl;
}
}
// Constructor (3)
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(mergeMode),
fMap(allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor3" << G4endl;
}
}
// Constructor (3) with name
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4String& name,
const Allocator& allocator,
G4MergeMode mergeMode)
: G4VAccumulable(name, mergeMode),
fMap(allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor3n" << G4endl;
}
}
// Constructor (10)
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode,
const Compare& comp,
const Allocator& allocator)
: G4VAccumulable(mergeMode),
fMap(init, comp, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor10" << G4endl;
}
}
// Constructor (10) with name
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4String& name,
std::initializer_list<std::pair<const Key,T>> init,
G4MergeMode mergeMode,
const Compare& comp,
const Allocator& allocator)
: G4VAccumulable(name, mergeMode),
fMap(init, comp, allocator),
fMergeFunction(GetMergeFunction<T>(mergeMode))
{
if (G4Accumulables::VerboseLevel > 1 ) {
G4cout << "G4AccMap ctor10n" << G4endl;
}
}
// Copy ctor
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
const G4AccMap& rhs,
const Allocator& allocator)
: G4VAccumulable(rhs),
fMap(rhs, allocator),
fMergeFunction(rhs.fMergeFunction)
{}
// Move ctor
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
G4AccMap<Key, T, Compare, Allocator>::G4AccMap(
G4AccMap&& rhs,
const Allocator& allocator)
: G4VAccumulable(std::move(rhs)),
fMap(std::move(rhs), allocator),
fMergeFunction(rhs.fMergeFunction)
{}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::Merge(const G4VAccumulable& other)
{
const auto& otherMap = static_cast<const G4AccMap<Key, T, Compare, Allocator>&>(other);
if (G4Accumulables::VerboseLevel > 2 ) {
G4cout << "G4AccMap<Key, T, Compare, Allocator>::Merge: " << G4endl;
G4cout << "destination: ";
for (const auto& [key, v] : fMap) {
G4cout << "[ " << key << ", " << v << " ], ";
}
G4cout << G4endl;
G4cout << "merged data: ";
for (const auto& [key, v] : otherMap.fMap) {
G4cout << "[ " << key << ", " << v << " ], ";
}
G4cout << G4endl;
}
for (const auto& [key, value] : otherMap.fMap) {
if ( fMap.find(key) == fMap.end()) {
fMap[key] = value;
}
else {
fMap[key] = fMergeFunction(fMap[key], value);
}
}
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::Reset()
{
for (auto& [key, value] :fMap) {
fMap[key] = fInitValue;
}
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::Print(
G4PrintOptions options) const
{
if (options.Has(G4PrintOptions::kType)) {
G4cout << "map<" << typeid(Key).name() << ", " << typeid(T).name() << ">: ";
}
PrintBase(options);
bool first = true;
for (const auto& [key, value] : fMap) {
if (! first) { G4cout << ", "; }
G4cout << "[ " << key << ", " << value << "] ";
first = false;
}
G4cout << G4endl;
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::SetMergeMode(G4MergeMode value)
{
G4VAccumulable::SetMergeMode(value);
fMergeFunction = GetMergeFunction<T>(fMergeMode);
}
//_____________________________________________________________________________
template <class Key, class T, class Compare, class Allocator>
void G4AccMap<Key, T, Compare, Allocator>::SetInitValue(const T& value)
{
fInitValue = value;
}