Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 /* 26 /* 27 * File: G4ArrayOps.hh 27 * File: G4ArrayOps.hh 28 * Author: B. Wendt (wendbryc@isu.edu) 28 * Author: B. Wendt (wendbryc@isu.edu) 29 * 29 * 30 * Created on July 28, 2012, 16:08 30 * Created on July 28, 2012, 16:08 31 */ 31 */ 32 32 33 // All the arrays that use the functions in th 33 // All the arrays that use the functions in this namespace MUST have a 34 // [] (bracket) operator for element-wise acce 34 // [] (bracket) operator for element-wise access 35 35 36 #ifndef G4ARRAYOPS_HH 36 #ifndef G4ARRAYOPS_HH 37 #define G4ARRAYOPS_HH 37 #define G4ARRAYOPS_HH 38 38 39 #include "globals.hh" 39 #include "globals.hh" 40 40 41 #include <vector> 41 #include <vector> 42 42 43 /** G4ArrayOps is a namespace that provides te 43 /** G4ArrayOps is a namespace that provides template functions for 44 * performing basic arithmatic operations on 44 * performing basic arithmatic operations on any data type that 45 * is accessed with the [] operator. 45 * is accessed with the [] operator. 46 */ 46 */ 47 namespace G4ArrayOps 47 namespace G4ArrayOps 48 { 48 { 49 /** Set's all the values in an array to a cons 49 /** Set's all the values in an array to a constant */ 50 template<class T> 50 template<class T> 51 void Set(G4int Elements, T* To, T Value) 51 void Set(G4int Elements, T* To, T Value) 52 { 52 { 53 for (G4int position = 0; position < Elements 53 for (G4int position = 0; position < Elements; position++) { 54 To[position] = Value; 54 To[position] = Value; 55 } 55 } 56 } 56 } 57 57 58 /** Copy values from one array to another */ 58 /** Copy values from one array to another */ 59 template<class T> 59 template<class T> 60 void Copy(G4int Elements, T* To, T* From) 60 void Copy(G4int Elements, T* To, T* From) 61 { 61 { 62 for (G4int position = 0; position < Elements 62 for (G4int position = 0; position < Elements; position++) { 63 To[position] = From[position]; 63 To[position] = From[position]; 64 } 64 } 65 } 65 } 66 66 67 /** Add two arrays together. If the second arr 67 /** Add two arrays together. If the second array is NULL then the 68 * 'To' array is used as if the function were 68 * 'To' array is used as if the function were the += operator. 69 */ 69 */ 70 template<class T> 70 template<class T> 71 void Add(G4int Elements, T* To, T* A1, T* A2 = 71 void Add(G4int Elements, T* To, T* A1, T* A2 = nullptr) 72 { 72 { 73 if (A2 == nullptr) { 73 if (A2 == nullptr) { 74 A2 = To; 74 A2 = To; 75 } 75 } 76 76 77 for (G4int position = 0; position < Elements 77 for (G4int position = 0; position < Elements; position++) { 78 To[position] = A2[position] + A1[position] 78 To[position] = A2[position] + A1[position]; 79 } 79 } 80 } 80 } 81 81 82 /** Add a constant to an array. If the second 82 /** Add a constant to an array. If the second array is NULL then the 83 * 'To' array is used as if the function were 83 * 'To' array is used as if the function were the += operator. 84 */ 84 */ 85 template<class T> 85 template<class T> 86 void Add(G4int Elements, T* To, T A1, T* A2 = 86 void Add(G4int Elements, T* To, T A1, T* A2 = NULL) 87 { 87 { 88 if (A2 == NULL) { 88 if (A2 == NULL) { 89 A2 = To; 89 A2 = To; 90 } 90 } 91 91 92 for (G4int position = 0; position < Elements 92 for (G4int position = 0; position < Elements; position++) { 93 To[position] = A1 + A2[position]; 93 To[position] = A1 + A2[position]; 94 } 94 } 95 } 95 } 96 96 97 /** Subtract an array from another. If the sec 97 /** Subtract an array from another. If the second array is NULL then the 98 * 'To' array is used as if the function were 98 * 'To' array is used as if the function were the -= operator. 99 */ 99 */ 100 template<class T> 100 template<class T> 101 void Subtract(G4int Elements, T* To, T* Minuen 101 void Subtract(G4int Elements, T* To, T* Minuend, T* Subtrahend = NULL) 102 { 102 { 103 if (Subtrahend == NULL) { 103 if (Subtrahend == NULL) { 104 Subtrahend = Minuend; 104 Subtrahend = Minuend; 105 Minuend = To; 105 Minuend = To; 106 } 106 } 107 107 108 for (G4int position = 0; position < Elements 108 for (G4int position = 0; position < Elements; position++) { 109 To[position] = Minuend[position] - Subtrah 109 To[position] = Minuend[position] - Subtrahend[position]; 110 } 110 } 111 } 111 } 112 112 113 /** Multiply two arrays together. If the secon 113 /** Multiply two arrays together. If the second array is NULL then the 114 * 'To' array is used as if the function were 114 * 'To' array is used as if the function were the *= operator. 115 */ 115 */ 116 template<class T> 116 template<class T> 117 void Multiply(G4int Elements, T* To, T* M1, T* 117 void Multiply(G4int Elements, T* To, T* M1, T* M2 = nullptr) 118 { 118 { 119 if (M2 == nullptr) { 119 if (M2 == nullptr) { 120 M2 = To; 120 M2 = To; 121 } 121 } 122 122 123 for (G4int position = 0; position < Elements 123 for (G4int position = 0; position < Elements; position++) { 124 To[position] = M2[position] * M1[position] 124 To[position] = M2[position] * M1[position]; 125 } 125 } 126 } 126 } 127 127 128 /** Multiply an array by a constant. If the se 128 /** Multiply an array by a constant. If the second array is NULL then the 129 * 'To' array is used as if the function were 129 * 'To' array is used as if the function were the *= operator. 130 */ 130 */ 131 template<class T> 131 template<class T> 132 void Multiply(G4int Elements, T* To, T M1, T* 132 void Multiply(G4int Elements, T* To, T M1, T* M2 = NULL) 133 { 133 { 134 if (M2 == NULL) { 134 if (M2 == NULL) { 135 M2 = To; 135 M2 = To; 136 } 136 } 137 137 138 for (G4int position = 0; position < Elements 138 for (G4int position = 0; position < Elements; position++) { 139 To[position] = M2[position] * M1; 139 To[position] = M2[position] * M1; 140 } 140 } 141 } 141 } 142 142 143 /** Divide an array by another. If the second 143 /** Divide an array by another. If the second array is NULL then the 144 * 'To' array is used as if the function were 144 * 'To' array is used as if the function were the /= operator. 145 */ 145 */ 146 template<class T> 146 template<class T> 147 void Divide(G4int Elements, T* To, T* Numerato 147 void Divide(G4int Elements, T* To, T* Numerator, T* Denominator = NULL) 148 { 148 { 149 if (Denominator == NULL) { 149 if (Denominator == NULL) { 150 Denominator = Numerator; 150 Denominator = Numerator; 151 Numerator = To; 151 Numerator = To; 152 } 152 } 153 153 154 for (G4int position = 0; position < Elements 154 for (G4int position = 0; position < Elements; position++) { 155 To[position] = Numerator[position] / Denom 155 To[position] = Numerator[position] / Denominator[position]; 156 } 156 } 157 } 157 } 158 158 159 /** Divide a constant by an array. If the seco 159 /** Divide a constant by an array. If the second array is NULL then the 160 * 'To' array is used as if the function were 160 * 'To' array is used as if the function were the /= operator. 161 */ 161 */ 162 template<class T> 162 template<class T> 163 void Divide(G4int Elements, T* To, T Numerator 163 void Divide(G4int Elements, T* To, T Numerator, T* Denominator = NULL) 164 { 164 { 165 if (Denominator == nullptr) { 165 if (Denominator == nullptr) { 166 Denominator = To; 166 Denominator = To; 167 } 167 } 168 168 169 for (G4int position = 0; position < Elements 169 for (G4int position = 0; position < Elements; position++) { 170 To[position] = Numerator / Denominator[pos 170 To[position] = Numerator / Denominator[position]; 171 } 171 } 172 } 172 } 173 173 174 template<class T> 174 template<class T> 175 void DeleteVectorOfPointers(std::vector<T>& Ve 175 void DeleteVectorOfPointers(std::vector<T>& Vector) 176 { 176 { 177 for (unsigned int i = 0; i < Vector.size(); 177 for (unsigned int i = 0; i < Vector.size(); i++) { 178 delete Vector[i]; 178 delete Vector[i]; 179 } 179 } 180 180 181 delete &Vector; 181 delete &Vector; 182 } 182 } 183 } // namespace G4ArrayOps 183 } // namespace G4ArrayOps 184 184 185 #endif /** G4ARRAYOPS_HH */ 185 #endif /** G4ARRAYOPS_HH */ 186 186