Documentation of CSL
librarytensor.h
Go to the documentation of this file.
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #ifndef CSL_LIBRARYTENSOR_H_INCLUDED
24 #define CSL_LIBRARYTENSOR_H_INCLUDED
25 
26 #ifdef THIS_FILE_IS_NOT_MEANT_TO_BE_COMPILED_SO_PLEASE_IGNORE_IT_DEAR_COMPILER
27 
28 #include <vector>
29 #include <iostream>
30 #include "csl/default_move_cpy.h"
31 
32 namespace csl {
33 
34  template<class Type>
35  class LibraryTensor {
36 
37  public:
38 
39  DEFINE_DEFAULT_CPY_MV(LibraryTensor)
40 
41  using iterator = typename std::vector<Type>::iterator;
42  using const_iterator = typename std::vector<Type>::const_iterator;
43  using reverse_iterator
44  = typename std::vector<Type>::reverse_iterator;
45  using const_reverse_iterator
46  = typename std::vector<Type>::const_reverse_iterator;
47 
48  static size_t getTotalDimension(std::vector<size_t> const& dimensions)
49  {
50  size_t tot = 1;
51  for (size_t d : dimensions)
52  tot *= d;
53  return tot;
54  }
55 
56  LibraryTensor(std::vector<size_t> const& t_dimensions)
57  :dimensions(t_dimensions),
58  data(getTotalDimension(dimensions))
59  {
60 
61  }
62 
63  LibraryTensor(std::vector<size_t> const& t_dimensions,
64  Type const& filler)
65  :dimensions(t_dimensions),
66  data(getTotalDimension(dimensions), filler)
67  {
68 
69  }
70 
71  LibraryTensor(std::vector<size_t> const& t_dimensions,
72  std::vector<Type> const& t_data)
73  :dimensions(t_dimensions),
74  data(t_data)
75  {
76  if (getTotalDimension(dimensions) != data.size()) {
77  std::cerr << "Bad initialization of LibraryTensor in file "
78  << __FILE__ << " (l. " << __LINE__ << "): expected "
79  << getTotalDimension(dimensions) << " elements, "
80  << data.size() << " given.\n";
81  exit(123);
82  }
83  }
84 
85  std::vector<size_t> const& getDimensions() const {
86  return dimensions;
87  }
88  std::vector<Type> const& toStdVector() const {
89  return data;
90  }
91 
92  size_t getIndex(std::vector<size_t> const& indices) const {
93  if (dimensions.size() == 1)
94  return indices[0];
95  size_t index = 0;
96  auto iter_index = indices.begin();
97  auto iter_dim = dimensions.begin();
98  for (; iter_dim != dimensions.end(); ++iter_index, ++iter_dim) {
99  index *= *iter_dim;
100  index += *iter_index;
101  }
102 
103  return index;
104  }
105 
106  bool empty() const {
107  return data.empty();
108  }
109  size_t size() const {
110  return data.size();
111  }
112 
113 #ifndef CSL_LT_DISABLE_ITERATOR
114  iterator begin() {
115  return data.begin();
116  }
117  iterator end() {
118  return data.end();
119  }
120  const_iterator begin() const {
121  return data.begin();
122  }
123  const_iterator end() const {
124  return data.end();
125  }
126 
127  reverse_iterator rbegin() {
128  return data.rbegin();
129  }
130  reverse_iterator rend() {
131  return data.rend();
132  }
133  const_reverse_iterator rbegin() const {
134  return data.rbegin();
135  }
136  const_reverse_iterator rend() const {
137  return data.rend();
138  }
139 #endif
140 
141  Type& operator[](size_t pos) {
142  return data[pos];
143  }
144  Type const& operator[](size_t pos) const {
145  return data[pos];
146  }
147 
148  Type& operator[](std::vector<size_t> const& indices) {
149  return data[getIndex(indices)];
150  }
151  Type const& operator[](std::vector<size_t> const& indices) const {
152  return data[getIndex(indices)];
153  }
154 
155  public:
156 
157  std::vector<size_t> dimensions;
158 
159  std::vector<Type> data;
160  };
161 
162 } // End of namespace csl
163 
164 #endif
165 
166 #endif // ifndef CSL_LIBRARYTENSOR_H_INCLUDED
Namespace for csl library.
Definition: abreviation.h:34
Type
Enum of the different types of Abstract (i.e. list of all possible specializations).
Definition: enum.h:47