Warning, /include/Geant4/tools/array is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef tools_array
0005 #define tools_array
0006
0007 #include <vector>
0008 #include <string>
0009
0010 namespace tools {
0011
0012 // array handles an hyperparallelepiped of cells of class T.
0013
0014 template <class T>
0015 class array {
0016 public:
0017 static const std::string& s_class() {
0018 static const std::string s_v("tools::array");
0019 return s_v;
0020 }
0021 public:
0022 typedef typename std::vector<unsigned int> uints_t;
0023 public:
0024 array() {
0025 }
0026 array(const uints_t& a_orders) {
0027 configure(a_orders);
0028 }
0029 array(unsigned int a_dimension,unsigned int a_order) {
0030 // A hypercube of dimension "a_dimension" and size "a_order".
0031 uints_t _orders(a_dimension);
0032 for(unsigned int index=0;index<a_dimension;index++)
0033 _orders[index] = a_order;
0034 configure(_orders);
0035 }
0036 virtual ~array() {
0037 }
0038 public:
0039 array(const array& a_from)
0040 :m_orders(a_from.m_orders)
0041 ,m_offsets(a_from.m_offsets)
0042 ,m_vector(a_from.m_vector)
0043 ,m_is(a_from.m_is){
0044 }
0045 array& operator=(const array& a_from) {
0046 m_orders = a_from.m_orders;
0047 m_offsets = a_from.m_offsets;
0048 m_vector = a_from.m_vector;
0049 m_is = a_from.m_is;
0050 return *this;
0051 }
0052 public:
0053 void clear() {
0054 m_orders.clear();
0055 m_offsets.clear();
0056 m_vector.clear();
0057 m_is.clear();
0058 }
0059 bool configure(const uints_t& a_orders) {
0060 m_orders = a_orders;
0061 size_t dim = m_orders.size();
0062 if(dim==0) {
0063 clear();
0064 return false;
0065 }
0066 unsigned int _size = 1;
0067 for(size_t index=0;index<dim;index++) {
0068 _size *= m_orders[index];
0069 }
0070 m_vector.resize(_size);
0071 m_vector.assign(_size,zero());
0072 m_offsets.resize(dim,0);
0073 m_offsets[0] = 1;
0074 for(size_t iaxis=1;iaxis<dim;iaxis++)
0075 m_offsets[iaxis] = m_offsets[iaxis-1] * m_orders[iaxis-1];
0076 m_is.resize(dim);
0077 m_is.assign(dim,0);
0078 return true;
0079 }
0080 size_t dimension() const { return m_orders.size();}
0081 const uints_t& orders() const { return m_orders;}
0082 size_t size() const {return m_vector.size();}
0083 const std::vector<T>& vector() const { return m_vector;}
0084 std::vector<T>& vector() { return m_vector;}
0085 bool fill(const std::vector<T>& a_values) {
0086 size_t dsize = a_values.size();
0087 size_t di = 0;
0088 unsigned int index = 0;
0089
0090 for(auto it=m_vector.begin();it!=m_vector.end();++it,index++) {
0091 if(di>=dsize) return false; //a_values exhausted too early
0092 *it = a_values[di];
0093 di++;
0094 }
0095 return true;
0096 }
0097 public:
0098 static T zero() {
0099 return T();
0100 }
0101 protected:
0102 uints_t m_orders;
0103 uints_t m_offsets;
0104 std::vector<T> m_vector;
0105 uints_t m_is;
0106 };
0107
0108 }
0109
0110 #endif