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 template <class T, std::size_t N>
0037 G4AccArray<T, N>::G4AccArray(
0038 const G4String& name, const T& value, G4MergeMode mergeMode)
0039 : G4VAccumulable(name, mergeMode),
0040 fInitValue(value),
0041 fMergeFunction(G4Accumulables::GetMergeFunction<T>(mergeMode))
0042 {
0043 if (G4Accumulables::VerboseLevel > 1 ) {
0044 G4cout << "G4AccArray ctor1" << G4endl;
0045 }
0046
0047 for (auto& element : fArray ) {
0048 element = fInitValue;
0049 }
0050 }
0051
0052
0053 template <class T, std::size_t N>
0054 template <typename... Args>
0055 G4AccArray<T, N>::G4AccArray(Args&&... args)
0056 : G4VAccumulable(),
0057 fArray{{std::forward<Args>(args)...}}, fMergeFunction(G4Accumulables::GetMergeFunction<T>(fMergeMode))
0058 {
0059 if (G4Accumulables::VerboseLevel > 1 ) {
0060 G4cout << "G4AccArray ctor2" << G4endl;
0061 }
0062 }
0063
0064
0065 template <class T, std::size_t N>
0066 template <typename First, typename... Args>
0067 G4AccArray<T, N>::G4AccArray(const First& firstArg, Args&&... args)
0068 : G4VAccumulable(firstArg),
0069 fArray{{std::forward<Args>(args)...}},
0070 fMergeFunction(G4Accumulables::GetMergeFunction<T>(fMergeMode))
0071 {
0072 if (G4Accumulables::VerboseLevel > 1 ) {
0073 G4cout << "G4AccArray ctor3" << G4endl;
0074 }
0075 }
0076
0077
0078 template <class T, std::size_t N>
0079 void G4AccArray<T, N>::Merge(const G4VAccumulable& other)
0080 {
0081 const auto& otherArrayAcc = static_cast<const G4AccArray<T,N>&>(other);
0082 const auto& otherArray = otherArrayAcc.GetArray();
0083
0084 if (G4Accumulables::VerboseLevel > 2 ) {
0085 G4cout << "G4AccArray<T, N>::Merge: " << G4endl;
0086 G4cout << "destination: ";
0087 for (auto v : this->fArray) {
0088 G4cout << v << ", ";
0089 }
0090 G4cout << G4endl;
0091 G4cout << "merged data: ";
0092 for (auto v : otherArray) {
0093 G4cout << v << ", ";
0094 }
0095 G4cout << G4endl;
0096 }
0097
0098 std::transform(fArray.begin(), fArray.end(), otherArray.begin(),
0099 fArray.begin(), fMergeFunction);
0100
0101 if (G4Accumulables::VerboseLevel > 1 ) {
0102 G4cout << "G4AccArray<T, N>::Merge: done" << G4endl;
0103 }
0104 }
0105
0106
0107 template <class T, std::size_t N>
0108 void G4AccArray<T, N>::Reset()
0109 {
0110 for (auto& value : fArray) {
0111 value = fInitValue;
0112 }
0113 }
0114
0115
0116 template <class T, std::size_t N>
0117 void G4AccArray<T, N>::Print(G4PrintOptions options) const
0118 {
0119 if (options.Has(G4PrintOptions::kType)) {
0120 G4cout << "array<" << typeid(fInitValue).name() << ">: ";
0121 }
0122
0123 PrintBase(options);
0124
0125 bool first = true;
0126 for (auto& value : fArray) {
0127 if (! first) { G4cout << ", "; }
0128 G4cout << value;
0129 first = false;
0130 }
0131 G4cout << G4endl;
0132 }
0133
0134
0135 template <class T, std::size_t N>
0136 void G4AccArray<T, N>::SetMergeMode(G4MergeMode value)
0137 {
0138 G4VAccumulable::SetMergeMode(value);
0139 fMergeFunction = G4Accumulables::GetMergeFunction<T>(value);
0140 }
0141
0142
0143 template <class T, std::size_t N>
0144 std::array<T,N>& G4AccArray<T, N>::GetArray()
0145 {
0146 return fArray;
0147 }
0148
0149
0150 template <class T, std::size_t N>
0151 const std::array<T,N>& G4AccArray<T, N>::GetArray() const
0152 {
0153 return fArray;
0154 }