Tensor
A C++ expression template library for computations on N dimensional tensors
tensor_container.hpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------------------------------------
2 /// @file Header file for TensorConainer class to allow both static and dynamic containers
3 // ----------------------------------------------------------------------------------------------------------
4 
5 /*
6 * ----------------------------------------------------------------------------------------------------------
7 * Tensor is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * Tensor is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with tensor; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * ----------------------------------------------------------------------------------------------------------
21 */
22 
23 #ifndef FTL_TENSOR_CONTAINER_HPP
24 #define FTL_TENSOR_CONTAINER_HPP
25 
26 #include <nano/nano.hpp>
27 
28 #include <array>
29 #include <vector>
30 
31 namespace ftl {
32 
33 // ----------------------------------------------------------------------------------------------------------
34 /// @struct TensorContainer
35 /// @brief Container for tensor data depending on if the tensor is static (dimension sizes, and hence the
36 /// total number of elements, are known at compile time -- uses std::array) or dynamic (dimension
37 /// sizes are not known at compile time -- uses std::vector).
38 /// @tparam Dtype The type of data used by the tensor
39 /// @tparam Sizes The sizes of the dimensions (optional)
40 // ----------------------------------------------------------------------------------------------------------
41 template <typename Dtype, size_t... Sizes>
43 
44 // Specialize for static containers -- for when the sizes of the dimensions are given at compile-time
45 template <typename Dtype, size_t SizeFirst, size_t... SizeRest>
46 class TensorContainer<Dtype, SizeFirst, SizeRest...> {
47 public:
48  // ---------------------------------------- ALIAS'S -----------------------------------------------------
49  using data_type = Dtype;
50  using dimension_sizes = nano::list<nano::size_t<SizeFirst>, nano::size_t<SizeRest>...>;
51  using dimension_product = nano::multiplies<dimension_sizes>;
52  using data_container = std::array<data_type, dimension_product::result>;
53  using dim_container = typename nano::runtime_converter<dimension_sizes>::array_type;
54  using size_type = typename data_container::size_type;
55  using iterator = typename data_container::iterator;
56  // ------------------------------------------------------------------------------------------------------
57 
58  // ------------------------------------------------------------------------------------------------------
59  /// @brief Default constructor
60  // ------------------------------------------------------------------------------------------------------
62 
63  // ------------------------------------------------------------------------------------------------------
64  /// @brief Contructor when given an array with the data for the container
65  /// @param[in] data The data to use to initalize the container data with
66  // ------------------------------------------------------------------------------------------------------
68  : _data(data) {}
69 
70  // ------------------------------------------------------------------------------------------------------
71  /// @brief Move constructor for when the data is given as a literal list
72  /// @param[in] values The first value in the literal list
73  /// @tparam TR The type of the rest of the parameters
74  // ------------------------------------------------------------------------------------------------------
75  template <typename... TR>
76  constexpr TensorContainer(TR&&... values)
77  : _data{{std::forward<TR>(values)...}} {}
78 
79  // ------------------------------------------------------------------------------------------------------
80  /// @brief Gets the size (total number of elements) in the container
81  /// @return The size of the container
82  // ------------------------------------------------------------------------------------------------------
83  constexpr size_t size() const { return dimension_product::result; }
84 
85  // ------------------------------------------------------------------------------------------------------
86  /// @brief Gets an element from the container
87  /// @param[in] i The index of the element in the container
88  /// @return A reference to the element at the index i in the container
89  // ------------------------------------------------------------------------------------------------------
90  inline data_type& operator[](size_t i) { return _data[i]; }
91 
92  // ------------------------------------------------------------------------------------------------------
93  /// @brief Gets an element from the container
94  /// @param[in] i The index of the element in the container
95  /// @return A reference to the element at the index i in the container
96  // ------------------------------------------------------------------------------------------------------
97  inline const data_type& operator[](size_t i) const { return _data[i]; }
98 
99  // ------------------------------------------------------------------------------------------------------
100  /// @brief Returns an iterator to the first element of the container
101  /// @return An iterator to the first element of the container
102  // ------------------------------------------------------------------------------------------------------
103  iterator begin() { return _data.begin(); }
104 
105  // ------------------------------------------------------------------------------------------------------
106  /// @brief Returns an iterator to the element following the last element
107  /// @return An iterator to the element following the last element
108  // ------------------------------------------------------------------------------------------------------
109  iterator end() { return _data.end(); }
110 private:
111  data_container _data; //!< Static data for a tensor
112 };
113 
114 // Specialization for dynamic container which the dimension sizes
115 // (and hence the number of elements) are not known at compile time
116 template <typename Dtype>
117 class TensorContainer<Dtype> {
118 public:
119  // ----------------------------------------- ALIAS'S ----------------------------------------------------
120  using data_type = Dtype;
121  using data_container = std::vector<data_type>;
122  using size_type = typename data_container::size_type;
123  using dim_container = std::vector<size_type>;
124  using iterator = typename data_container::iterator;
125  // ------------------------------------------------------------------------------------------------------
126 
127  // ------------------------------------------------------------------------------------------------------
128  /// @brief Default constructor
129  // ------------------------------------------------------------------------------------------------------
130  TensorContainer() : _size(0), _data(0) {}
131 
132  // ------------------------------------------------------------------------------------------------------
133  /// @brief Contructor when given an array with the data for the container
134  /// @param[in] data The data to use to initalize the container data with
135  // ------------------------------------------------------------------------------------------------------
137  : _size(data.size()), _data(data){}
138 
139  // ------------------------------------------------------------------------------------------------------
140  /// @brief Gets the size (total number of elements) in the container
141  /// @return The size of the container
142  // ------------------------------------------------------------------------------------------------------
143  inline size_t size() const { return _size; }
144 
145  // ------------------------------------------------------------------------------------------------------
146  /// @brief Gets an element from the container
147  /// @param[in] i The index of the element in the container
148  /// @return A reference to the element at the index i in the container
149  // ------------------------------------------------------------------------------------------------------
150  inline data_type& operator[](size_t i) { return _data[i]; }
151 
152  // ------------------------------------------------------------------------------------------------------
153  /// @brief Gets an element from the container
154  /// @param[in] i The index of the element in the container
155  /// @return A reference to the element at the index i in the container
156  // ------------------------------------------------------------------------------------------------------
157  inline const data_type& operator[](size_t i) const { return _data[i]; }
158 
159  // ------------------------------------------------------------------------------------------------------
160  /// @brief Returns an iterator to the first element of the container
161  /// @return An iterator to the first element of the container
162  // ------------------------------------------------------------------------------------------------------
163  iterator begin() { return _data.begin(); }
164 
165  // ------------------------------------------------------------------------------------------------------
166  /// @brief Returns an iterator to the element following the last element
167  /// @return An iterator to the element following the last element
168  // ------------------------------------------------------------------------------------------------------
169  iterator end() { return _data.end(); }
170 private:
171  data_container _data; //!< Dynamic data container for a tensor
172  size_t _size; //!< Number of elements in the container
173 };
174 
175 } // End namespace ftl
176 
177 #endif // FTL_TENSOR_CONTAINER_HPP
typename data_container::size_type size_type
Definition: tensor_container.hpp:122
TensorContainer()
Default constructor.
Definition: tensor_container.hpp:130
typename data_container::iterator iterator
Definition: tensor_container.hpp:55
iterator begin()
Returns an iterator to the first element of the container.
Definition: tensor_container.hpp:163
typename data_container::iterator iterator
Definition: tensor_container.hpp:124
std::vector< size_type > dim_container
Definition: tensor_container.hpp:123
size_t size() const
Gets the size (total number of elements) in the container.
Definition: tensor_container.hpp:143
Container for tensor data depending on if the tensor is static (dimension sizes, and hence the total ...
Definition: tensor_container.hpp:42
Dtype data_type
Definition: tensor_container.hpp:120
Definition: mapper.hpp:37
nano::multiplies< dimension_sizes > dimension_product
Definition: tensor_container.hpp:51
iterator end()
Returns an iterator to the element following the last element.
Definition: tensor_container.hpp:169
TensorContainer()
Default constructor.
Definition: tensor_container.hpp:61
typename nano::runtime_converter< dimension_sizes >::array_type dim_container
Definition: tensor_container.hpp:53
constexpr TensorContainer(data_container &data)
Contructor when given an array with the data for the container.
Definition: tensor_container.hpp:67
data_type & operator[](size_t i)
Gets an element from the container.
Definition: tensor_container.hpp:150
typename data_container::size_type size_type
Definition: tensor_container.hpp:54
nano::list< nano::size_t< SizeFirst >, nano::size_t< SizeRest >...> dimension_sizes
Definition: tensor_container.hpp:50
std::array< data_type, dimension_product::result > data_container
Definition: tensor_container.hpp:52
TensorContainer(data_container &data)
Contructor when given an array with the data for the container.
Definition: tensor_container.hpp:136
const data_type & operator[](size_t i) const
Gets an element from the container.
Definition: tensor_container.hpp:157
constexpr TensorContainer(TR &&...values)
Move constructor for when the data is given as a literal list.
Definition: tensor_container.hpp:76
std::vector< data_type > data_container
Definition: tensor_container.hpp:121
Dtype data_type
Definition: tensor_container.hpp:49