I want to define n-dimensional data structure using std::vector. I have a problem with definition of operator(). Lets have a look at a sample 2D structure
class my_data {
public:
my_data (size_t N, size_t M) : tab(N*M), _N(N), _M(M) {}
const double& operator() (size_t i, size_t j) const {
return tab.at(i * M + j);
}
double& operator() (size_t i, size_t j) {
return tab.at(i * M + j);
}
private:
std::vector<double> tab;
size_t _N;
size_t _M;
};
I would like to define such a structures for any dimension using templates , but I don't know if it is possible. So basically what I would like to have is something like that
my_data<4> vec (1,2,3,4);
defines 4D 'tensor' with sizes 1,2,3,4 (number of all elements 1*2*3*4). You can now access and change values by typing
vec (0,0,0,0) = 2.0;
The below solution uses some of the C++14 and C++1z features, but they can be easily ported to C++11:
#include <vector>
#include <utility>
#include <array>
#include <cstddef>
namespace detail
{
template <typename T, typename S>
class my_data;
template <typename T, std::size_t... Is>
class my_data<T, std::index_sequence<Is...>>
{
public:
my_data(decltype(Is)... size)
: t((size * ...)), s{{ size... }}
{}
T& operator()(decltype(Is)... i)
{
return t.at(index({{ i... }}));
}
const T& operator()(decltype(Is)... i) const
{
return t.at(index({{ i... }}));
}
private:
std::size_t index(const std::array<std::size_t, sizeof...(Is)>& a) const
{
std::size_t ind = a[0];
for (std::size_t i = 1; i < a.size(); ++i)
{
ind = ind * s[i] + a[i];
}
return ind;
}
std::vector<T> t;
const std::array<std::size_t, sizeof...(Is)> s;
};
}
template <typename T, std::size_t N>
using my_data = detail::my_data<T, std::make_index_sequence<N>>;
Test:
int main()
{
my_data<double, 4> vec(1,2,3,4);
vec(0,0,0,0) = 2.0;
}
DEMO
DEMO (C++11)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With