Skip to content

Instantly share code, notes, and snippets.

@posobin
Last active August 29, 2015 14:21
Show Gist options
  • Save posobin/f52623ba3f69b3e720b5 to your computer and use it in GitHub Desktop.
Save posobin/f52623ba3f69b3e720b5 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
template <size_t N, class ElementType>
class Tensor
{
std::vector<Tensor<N - 1, ElementType>> data_;
public:
Tensor<N - 1, ElementType>& operator[](size_t index)
{
return data_[index];
}
const Tensor<N - 1, ElementType>& operator[](size_t index) const
{
return data_[index];
}
Tensor(const std::initializer_list<Tensor<N - 1, ElementType>>& list) :
data_(list)
{ }
template <class...Arg2> Tensor(size_t size, Arg2...list) :
data_(size, Tensor<N - 1, ElementType>(list...))
{ }
};
template <class ElementType>
class Tensor<0, ElementType>
{
ElementType element_;
public:
Tensor()
{ }
Tensor(const ElementType& element) : element_(element)
{ }
operator ElementType() { return element_; }
};
int main()
{
Tensor<2, int> q({{1, 2, 3}});
Tensor<5, int> haha(1, 2, 3, 4, 5);
Tensor<0, int> v(0);
Tensor<3, int> cube =
{
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
}, {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
}, {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
}
};
v = 10;
std::cout << cube[0][1][2] << std::endl;
q[0] = { 100, 100, 100 };
std::cout << q[0][1] + v << ' ' << haha[0][1][2][3][4] << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment