Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:56

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /*
0027  * File:   G4ArrayOps.hh
0028  * Author: B. Wendt (wendbryc@isu.edu)
0029  *
0030  * Created on July 28, 2012, 16:08
0031  */
0032 
0033 // All the arrays that use the functions in this namespace MUST have a
0034 // [] (bracket) operator for element-wise access
0035 
0036 #ifndef G4ARRAYOPS_HH
0037 #define G4ARRAYOPS_HH
0038 
0039 #include "globals.hh"
0040 
0041 #include <vector>
0042 
0043 /** G4ArrayOps is a namespace that provides template functions for
0044  *  performing basic arithmatic operations on any data type that
0045  *  is accessed with the [] operator.
0046  */
0047 namespace G4ArrayOps
0048 {
0049 /** Set's all the values in an array to a constant */
0050 template<class T>
0051 void Set(G4int Elements, T* To, T Value)
0052 {
0053   for (G4int position = 0; position < Elements; position++) {
0054     To[position] = Value;
0055   }
0056 }
0057 
0058 /** Copy values from one array to another */
0059 template<class T>
0060 void Copy(G4int Elements, T* To, T* From)
0061 {
0062   for (G4int position = 0; position < Elements; position++) {
0063     To[position] = From[position];
0064   }
0065 }
0066 
0067 /** Add two arrays together. If the second array is NULL then the
0068  *  'To' array is used as if the function were the += operator.
0069  */
0070 template<class T>
0071 void Add(G4int Elements, T* To, T* A1, T* A2 = nullptr)
0072 {
0073   if (A2 == nullptr) {
0074     A2 = To;
0075   }
0076 
0077   for (G4int position = 0; position < Elements; position++) {
0078     To[position] = A2[position] + A1[position];
0079   }
0080 }
0081 
0082 /** Add a constant to an array. If the second array is NULL then the
0083  *  'To' array is used as if the function were the += operator.
0084  */
0085 template<class T>
0086 void Add(G4int Elements, T* To, T A1, T* A2 = NULL)
0087 {
0088   if (A2 == NULL) {
0089     A2 = To;
0090   }
0091 
0092   for (G4int position = 0; position < Elements; position++) {
0093     To[position] = A1 + A2[position];
0094   }
0095 }
0096 
0097 /** Subtract an array from another. If the second array is NULL then the
0098  *  'To' array is used as if the function were the -= operator.
0099  */
0100 template<class T>
0101 void Subtract(G4int Elements, T* To, T* Minuend, T* Subtrahend = NULL)
0102 {
0103   if (Subtrahend == NULL) {
0104     Subtrahend = Minuend;
0105     Minuend = To;
0106   }
0107 
0108   for (G4int position = 0; position < Elements; position++) {
0109     To[position] = Minuend[position] - Subtrahend[position];
0110   }
0111 }
0112 
0113 /** Multiply two arrays together. If the second array is NULL then the
0114  *  'To' array is used as if the function were the *= operator.
0115  */
0116 template<class T>
0117 void Multiply(G4int Elements, T* To, T* M1, T* M2 = nullptr)
0118 {
0119   if (M2 == nullptr) {
0120     M2 = To;
0121   }
0122 
0123   for (G4int position = 0; position < Elements; position++) {
0124     To[position] = M2[position] * M1[position];
0125   }
0126 }
0127 
0128 /** Multiply an array by a constant. If the second array is NULL then the
0129  *  'To' array is used as if the function were the *= operator.
0130  */
0131 template<class T>
0132 void Multiply(G4int Elements, T* To, T M1, T* M2 = NULL)
0133 {
0134   if (M2 == NULL) {
0135     M2 = To;
0136   }
0137 
0138   for (G4int position = 0; position < Elements; position++) {
0139     To[position] = M2[position] * M1;
0140   }
0141 }
0142 
0143 /** Divide an array by another. If the second array is NULL then the
0144  *  'To' array is used as if the function were the /= operator.
0145  */
0146 template<class T>
0147 void Divide(G4int Elements, T* To, T* Numerator, T* Denominator = NULL)
0148 {
0149   if (Denominator == NULL) {
0150     Denominator = Numerator;
0151     Numerator = To;
0152   }
0153 
0154   for (G4int position = 0; position < Elements; position++) {
0155     To[position] = Numerator[position] / Denominator[position];
0156   }
0157 }
0158 
0159 /** Divide a constant by an array. If the second array is NULL then the
0160  *  'To' array is used as if the function were the /= operator.
0161  */
0162 template<class T>
0163 void Divide(G4int Elements, T* To, T Numerator, T* Denominator = NULL)
0164 {
0165   if (Denominator == nullptr) {
0166     Denominator = To;
0167   }
0168 
0169   for (G4int position = 0; position < Elements; position++) {
0170     To[position] = Numerator / Denominator[position];
0171   }
0172 }
0173 
0174 template<class T>
0175 void DeleteVectorOfPointers(std::vector<T>& Vector)
0176 {
0177   for (unsigned int i = 0; i < Vector.size(); i++) {
0178     delete Vector[i];
0179   }
0180 
0181   delete &Vector;
0182 }
0183 }  // namespace G4ArrayOps
0184 
0185 #endif /** G4ARRAYOPS_HH */