Skip to content

Instantly share code, notes, and snippets.

@ucasfl
Created December 28, 2019 04:29
Show Gist options
  • Save ucasfl/4b7e6ca2b5936746df98e98b12850d30 to your computer and use it in GitHub Desktop.
Save ucasfl/4b7e6ca2b5936746df98e98b12850d30 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
using namespace std;
template <typename T, size_t dim1, size_t dim2>
class array2dref
{
public:
array2dref(T * data_) : data(data_) {}
T * operator[](size_t y) const { return data + y * dim2; }
private:
T * data;
};
template <typename T, size_t dim1, size_t dim2, size_t dim3>
class array3d
{
public:
array3d() { data = new T[dim1 * dim2 * dim3]; }
auto operator[](size_t y) const { return array2dref<T, dim2, dim3>(data + y * dim2 * dim3); }
~array3d() { delete[] data; }
private:
T * data;
};
int main()
{
vector<int> d;
for (auto i = 0ul; i < 8; ++i)
{
d.push_back(i);
}
array3d<int, 2, 2, 2> a;
for (auto i = 0ul; i < 2; ++i)
{
for (auto j = 0ul; j < 2; ++j)
{
for (auto k = 0ul; k < 2; ++k)
{
a[i][j][k] = d[i * 4 + j * 2 + k];
}
}
}
for (auto i = 0ul; i < 2; ++i)
{
for (auto j = 0ul; j < 2; ++j)
{
for (auto k = 0ul; k < 2; ++k)
{
std::cout << a[i][j][k] << std::endl;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment