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) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#ifndef ARRAY_H
20#define ARRAY_H
21
22#include <iostream>
23#include <list>
24#include <string>
25#include <vector>
26
27namespace GMAD
28{
29 class Symtab;
30}
31
32namespace GMAD
33{
40 class Array {
41
42 private:
44 std::vector<std::string> symbols;
46 std::vector<double> data;
47
49 explicit Array(unsigned int n);
50
51 public:
53 friend class Symtab;
55 Array();
57 explicit Array(Symtab*);
59 static Array* Add(Array* a1, Array* a2);
61 static Array* Subtract(Array* a1, Array* a2);
63 static Array* Add(Array* a, double d);
65 static Array* Subtract(Array* a, double d);
67 static Array* Multiply(Array* a, double d);
69 static Array* Divide(Array* a, double d);
71 double Product(Array* a);
72
74 const std::vector<std::string>& GetSymbols() const {return symbols;}
75 std::list<std::string> GetSymbolsList() const;
76 const std::vector<double>& GetData() const {return data;}
77 std::list<double> GetDataList() const;
78
80 void Clear();
82 void Print();
83
85 template<template <typename, typename> class Container>
86 void Copy(Container<std::string, std::allocator<std::string>>& cpy)
87 {
88 for (const std::string& name : cpy)
89 {symbols.push_back(name);}
90 }
91
93 template<typename T, template <typename, typename> class Container>
94 void Copy(Container<T, std::allocator<T>>& cpy)
95 {
96 for (T value : cpy)
97 {data.push_back(value);}
98 }
99
101 template<template <typename, typename> class Container>
102 void set_vector(Container<std::string, std::allocator<std::string>>& dst)const
103 {
104 for (std::string symbol : symbols)
105 {
106 dst.push_back(symbol);
107#ifdef BDSDEBUG
108 std::cout << symbol << " ";
109#endif
110 }
111#ifdef BDSDEBUG
112 std::cout << std::endl;
113#endif
114 }
115
117 template<typename T, template <typename, typename> class Container>
118 void set_vector(Container<T, std::allocator<T>>& dst)const
119 {
120 for (auto value : data)
121 {
122 dst.push_back((T)value);
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:40
const std::vector< std::string > & GetSymbols() const
Access.
Definition: array.h:74
void Copy(Container< std::string, std::allocator< std::string > > &cpy)
Copy STL string containers into symbols.
Definition: array.h:86
void set_vector(Container< std::string, std::allocator< std::string > > &dst) const
Copy symbols into STL string containers.
Definition: array.h:102
static Array * Subtract(Array *a1, Array *a2)
Constructor from subtracting 2 double arrays.
Definition: array.cc:55
static Array * Multiply(Array *a, double d)
Constructor from multiplying an array.
Definition: array.cc:88
std::vector< std::string > symbols
Representation of string array.
Definition: array.h:44
void Copy(Container< T, std::allocator< T > > &cpy)
Copy STL numerical containers into data.
Definition: array.h:94
std::vector< double > data
Representation of double array.
Definition: array.h:46
static Array * Divide(Array *a, double d)
Constructor from dividing an array.
Definition: array.cc:99
void Clear()
Clear data.
Definition: array.cc:124
static Array * Add(Array *a1, Array *a2)
Constructor from adding 2 double arrays.
Definition: array.cc:44
double Product(Array *a)
Scalar vector product.
Definition: array.cc:110
Array()
Default Constructor.
Definition: array.cc:27
void Print()
Print data.
Definition: array.cc:130
void set_vector(Container< T, std::allocator< T > > &dst) const
Copy data into STL numerical containers.
Definition: array.h:118
Common header for the lexer and the parser to share Symbol table for numeric variables,...
Definition: sym_table.h:33
Parser namespace for GMAD language. Combination of Geant4 and MAD.