Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4ArrayOps.hh
Go to the documentation of this file.
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 * File: G4ArrayOps.hh
28 * Author: B. Wendt ([email protected])
29 *
30 * Created on July 28, 2012, 16:08
31 */
32
33// All the arrays that use the functions in this namespace MUST have a
34// [] (bracket) operator for element-wise access
35
36#ifndef G4ARRAYOPS_HH
37#define G4ARRAYOPS_HH
38
39#include <vector>
40
41#include "globals.hh"
42
43/** G4ArrayOps is a namespace that provides template functions for
44 * performing basic arithmatic operations on any data type that
45 * is accessed with the [] operator.
46 */
47namespace G4ArrayOps
48{
49 /** Set's all the values in an array to a constant */
50 template< class T >
51 void Set( G4int Elements,
52 T* To,
53 T Value )
54 {
55 for(G4int position = 0; position < Elements; position++)
56 {
57 To[position] = Value;
58 }
59 }
60
61 /** Copy values from one array to another */
62 template< class T >
63 void Copy( G4int Elements,
64 T* To,
65 T* From )
66 {
67 for(G4int position = 0; position < Elements; position++)
68 {
69 To[position] = From[position];
70 }
71 }
72
73 /** Add two arrays together. If the second array is NULL then the
74 * 'To' array is used as if the function were the += operator.
75 */
76 template< class T >
77 void Add( G4int Elements,
78 T* To,
79 T* A1,
80 T* A2 = NULL )
81 {
82 if(A2 == NULL)
83 {
84 A2 = To;
85 }
86
87 for(G4int position = 0; position < Elements; position++)
88 {
89 To[position] = A2[position] + A1[position];
90 }
91 }
92
93 /** Add a constant to an array. If the second array is NULL then the
94 * 'To' array is used as if the function were the += operator.
95 */
96 template< class T >
97 void Add( G4int Elements,
98 T* To,
99 T A1,
100 T* A2 = NULL )
101 {
102 if(A2 == NULL)
103 {
104 A2 = To;
105 }
106
107 for(G4int position = 0; position < Elements; position++)
108 {
109 To[position] = A1 + A2[position];
110 }
111 }
112
113 /** Subtract an array from another. If the second array is NULL then the
114 * 'To' array is used as if the function were the -= operator.
115 */
116 template< class T >
117 void Subtract( G4int Elements,
118 T* To,
119 T* Minuend,
120 T* Subtrahend = NULL )
121 {
122 if(Subtrahend == NULL)
123 {
124 Subtrahend = Minuend;
125 Minuend = To;
126 }
127
128 for(G4int position = 0; position < Elements; position++)
129 {
130 To[position] = Minuend[position] - Subtrahend[position];
131 }
132 }
133
134 /** Multiply two arrays together. If the second array is NULL then the
135 * 'To' array is used as if the function were the *= operator.
136 */
137 template< class T >
138 void Multiply( G4int Elements,
139 T* To,
140 T* M1,
141 T* M2 = NULL )
142 {
143 if(M2 == NULL)
144 {
145 M2 = To;
146 }
147
148 for(G4int position = 0; position < Elements; position++)
149 {
150 To[position] = M2[position] * M1[position];
151 }
152 }
153
154 /** Multiply an array by a constant. If the second array is NULL then the
155 * 'To' array is used as if the function were the *= operator.
156 */
157 template< class T >
158 void Multiply( G4int Elements,
159 T* To,
160 T M1,
161 T* M2 = NULL )
162 {
163 if(M2 == NULL)
164 {
165 M2 = To;
166 }
167
168 for(G4int position = 0; position < Elements; position++)
169 {
170 To[position] = M2[position] * M1;
171 }
172 }
173
174 /** Divide an array by another. If the second array is NULL then the
175 * 'To' array is used as if the function were the /= operator.
176 */
177 template< class T >
178 void Divide( G4int Elements,
179 T* To,
180 T* Numerator,
181 T* Denominator = NULL )
182 {
183 if(Denominator == NULL)
184 {
185 Denominator = Numerator;
186 Numerator = To;
187 }
188
189 for(G4int position = 0; position < Elements; position++)
190 {
191 To[position] = Numerator[position] / Denominator[position];
192 }
193 }
194
195 /** Divide a constant by an array. If the second array is NULL then the
196 * 'To' array is used as if the function were the /= operator.
197 */
198 template< class T >
199 void Divide( G4int Elements,
200 T* To,
201 T Numerator,
202 T* Denominator = NULL )
203 {
204 if(Denominator == NULL)
205 {
206 Denominator = To;
207 }
208
209 for(G4int position = 0; position < Elements; position++)
210 {
211 To[position] = Numerator / Denominator[position];
212 }
213 }
214
215 template< class T >
216 void DeleteVectorOfPointers( std::vector< T >& Vector )
217 {
218 for(unsigned int i = 0; i < Vector.size(); i++)
219 {
220 delete Vector[i];
221 }
222
223 delete &Vector;
224 }
225}
226
227#endif /** G4ARRAYOPS_HH */
228
int G4int
Definition: G4Types.hh:85
void Add(G4int Elements, T *To, T *A1, T *A2=NULL)
Definition: G4ArrayOps.hh:77
void Multiply(G4int Elements, T *To, T *M1, T *M2=NULL)
Definition: G4ArrayOps.hh:138
void Subtract(G4int Elements, T *To, T *Minuend, T *Subtrahend=NULL)
Definition: G4ArrayOps.hh:117
void Divide(G4int Elements, T *To, T *Numerator, T *Denominator=NULL)
Definition: G4ArrayOps.hh:178
void DeleteVectorOfPointers(std::vector< T > &Vector)
Definition: G4ArrayOps.hh:216
void Copy(G4int Elements, T *To, T *From)
Definition: G4ArrayOps.hh:63
void Set(G4int Elements, T *To, T Value)
Definition: G4ArrayOps.hh:51