BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
array.h
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#ifndef ARRAY_H
19#define ARRAY_H
20
21#include <iostream>
22#include <list>
23#include <string>
24#include <vector>
25
26namespace GMAD
27{
28 class Symtab;
29}
30
31namespace GMAD
32{
39 class Array {
40
41 private:
43 std::vector<std::string> symbols;
45 std::vector<double> data;
46
48 explicit Array(unsigned int n);
49
50 public:
52 friend class Symtab;
54 Array();
56 explicit Array(Symtab*);
58 static Array* Add(Array* a1, Array* a2);
60 static Array* Subtract(Array* a1, Array* a2);
62 static Array* Add(Array* a, double d);
64 static Array* Subtract(Array* a, double d);
66 static Array* Multiply(Array* a, double d);
68 static Array* Divide(Array* a, double d);
70 double Product(Array* a);
71
73 const std::vector<std::string>& GetSymbols() const {return symbols;}
74 std::list<std::string> GetSymbolsList() const;
75 const std::vector<double>& GetData() const {return data;}
76 std::list<double> GetDataList() const;
77
79 void Clear();
81 void Print();
82
84 template<template <typename, typename> class Container>
85 void Copy(Container<std::string, std::allocator<std::string>>& cpy)
86 {
87 for (const std::string& name : cpy)
88 {symbols.push_back(name);}
89 }
90
92 template<typename T, template <typename, typename> class Container>
93 void Copy(Container<T, std::allocator<T>>& cpy)
94 {
95 for (T value : cpy)
96 {data.push_back(value);}
97 }
98
100 template<template <typename, typename> class Container>
101 void set_vector(Container<std::string, std::allocator<std::string>>& dst)const
102 {
103 for (std::string symbol : symbols)
104 {
105 dst.push_back(symbol);
106#ifdef BDSDEBUG
107 std::cout << symbol << " ";
108#endif
109 }
110#ifdef BDSDEBUG
111 std::cout << std::endl;
112#endif
113 }
114
116 template<typename T, template <typename, typename> class Container>
117 void set_vector(Container<T, std::allocator<T>>& dst)const
118 {
119 for (auto value : data)
120 {
121 dst.push_back((T)value);
122
123#ifdef BDSDEBUG
124 std::cout << (T)value << " ";
125#endif
126 }
127#ifdef BDSDEBUG
128 std::cout << std::endl;
129#endif
130 }
131 };
132}
133
134#endif
Representation of arrays used in tokens.
Definition array.h:39
const std::vector< std::string > & GetSymbols() const
Access.
Definition array.h:73
void Copy(Container< std::string, std::allocator< std::string > > &cpy)
Copy STL string containers into symbols.
Definition array.h:85
void set_vector(Container< std::string, std::allocator< std::string > > &dst) const
Copy symbols into STL string containers.
Definition array.h:101
static Array * Subtract(Array *a1, Array *a2)
Constructor from subtracting 2 double arrays.
Definition array.cc:54
static Array * Multiply(Array *a, double d)
Constructor from multiplying an array.
Definition array.cc:87
std::vector< std::string > symbols
Representation of string array.
Definition array.h:43
void Copy(Container< T, std::allocator< T > > &cpy)
Copy STL numerical containers into data.
Definition array.h:93
std::vector< double > data
Representation of double array.
Definition array.h:45
static Array * Divide(Array *a, double d)
Constructor from dividing an array.
Definition array.cc:98
void Clear()
Clear data.
Definition array.cc:123
static Array * Add(Array *a1, Array *a2)
Constructor from adding 2 double arrays.
Definition array.cc:43
double Product(Array *a)
Scalar vector product.
Definition array.cc:109
Array()
Default Constructor.
Definition array.cc:26
void Print()
Print data.
Definition array.cc:129
void set_vector(Container< T, std::allocator< T > > &dst) const
Copy data into STL numerical containers.
Definition array.h:117
Common header for the lexer and the parser to share Symbol table for numeric variables,...
Definition sym_table.h:32
Parser namespace for GMAD language. Combination of Geant4 and MAD.