Geant4 11.2.2
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 "globals.hh"
40
41#include <vector>
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 */
50template<class T>
51void Set(G4int Elements, T* To, T Value)
52{
53 for (G4int position = 0; position < Elements; position++) {
54 To[position] = Value;
55 }
56}
57
58/** Copy values from one array to another */
59template<class T>
60void Copy(G4int Elements, T* To, T* From)
61{
62 for (G4int position = 0; position < Elements; position++) {
63 To[position] = From[position];
64 }
65}
66
67/** Add two arrays together. If the second array is NULL then the
68 * 'To' array is used as if the function were the += operator.
69 */
70template<class T>
71void Add(G4int Elements, T* To, T* A1, T* A2 = nullptr)
72{
73 if (A2 == nullptr) {
74 A2 = To;
75 }
76
77 for (G4int position = 0; position < Elements; position++) {
78 To[position] = A2[position] + A1[position];
79 }
80}
81
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 the += operator.
84 */
85template<class T>
86void Add(G4int Elements, T* To, T A1, T* A2 = NULL)
87{
88 if (A2 == NULL) {
89 A2 = To;
90 }
91
92 for (G4int position = 0; position < Elements; position++) {
93 To[position] = A1 + A2[position];
94 }
95}
96
97/** Subtract an array from another. If the second array is NULL then the
98 * 'To' array is used as if the function were the -= operator.
99 */
100template<class T>
101void Subtract(G4int Elements, T* To, T* Minuend, T* Subtrahend = NULL)
102{
103 if (Subtrahend == NULL) {
104 Subtrahend = Minuend;
105 Minuend = To;
106 }
107
108 for (G4int position = 0; position < Elements; position++) {
109 To[position] = Minuend[position] - Subtrahend[position];
110 }
111}
112
113/** Multiply two arrays together. If the second array is NULL then the
114 * 'To' array is used as if the function were the *= operator.
115 */
116template<class T>
117void Multiply(G4int Elements, T* To, T* M1, T* M2 = nullptr)
118{
119 if (M2 == nullptr) {
120 M2 = To;
121 }
122
123 for (G4int position = 0; position < Elements; position++) {
124 To[position] = M2[position] * M1[position];
125 }
126}
127
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 the *= operator.
130 */
131template<class T>
132void Multiply(G4int Elements, T* To, T M1, T* M2 = NULL)
133{
134 if (M2 == NULL) {
135 M2 = To;
136 }
137
138 for (G4int position = 0; position < Elements; position++) {
139 To[position] = M2[position] * M1;
140 }
141}
142
143/** Divide an array by another. If the second array is NULL then the
144 * 'To' array is used as if the function were the /= operator.
145 */
146template<class T>
147void Divide(G4int Elements, T* To, T* Numerator, T* Denominator = NULL)
148{
149 if (Denominator == NULL) {
150 Denominator = Numerator;
151 Numerator = To;
152 }
153
154 for (G4int position = 0; position < Elements; position++) {
155 To[position] = Numerator[position] / Denominator[position];
156 }
157}
158
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 the /= operator.
161 */
162template<class T>
163void Divide(G4int Elements, T* To, T Numerator, T* Denominator = NULL)
164{
165 if (Denominator == nullptr) {
166 Denominator = To;
167 }
168
169 for (G4int position = 0; position < Elements; position++) {
170 To[position] = Numerator / Denominator[position];
171 }
172}
173
174template<class T>
175void DeleteVectorOfPointers(std::vector<T>& Vector)
176{
177 for (unsigned int i = 0; i < Vector.size(); i++) {
178 delete Vector[i];
179 }
180
181 delete &Vector;
182}
183} // namespace G4ArrayOps
184
185#endif /** G4ARRAYOPS_HH */
int G4int
Definition G4Types.hh:85
void Add(G4int Elements, T *To, T *A1, T *A2=nullptr)
Definition G4ArrayOps.hh:71
void Subtract(G4int Elements, T *To, T *Minuend, T *Subtrahend=NULL)
void Multiply(G4int Elements, T *To, T *M1, T *M2=nullptr)
void Divide(G4int Elements, T *To, T *Numerator, T *Denominator=NULL)
void DeleteVectorOfPointers(std::vector< T > &Vector)
void Copy(G4int Elements, T *To, T *From)
Definition G4ArrayOps.hh:60
void Set(G4int Elements, T *To, T Value)
Definition G4ArrayOps.hh:51