Skip to content

Instantly share code, notes, and snippets.

@cynthia2006
Created September 13, 2023 09:47
Show Gist options
  • Save cynthia2006/750cc962058ce16ca37538a35765e807 to your computer and use it in GitHub Desktop.
Save cynthia2006/750cc962058ce16ca37538a35765e807 to your computer and use it in GitHub Desktop.
#include <array>
#include <vector>
/* This idea was inspired by how std::tuple<...> is implemented, that is, using std::pair<K, V>
* as linked-lists. The same idea here is applied in a different fashion to create multidimensional
* std::array<T, N> of homogenous types. A std::vector<T> variant is also made.
*/
template<typename T, int N, int ...dims>
class ndarray {
public:
typedef std::array<typename ndarray<T, dims...>::type, N> type;
};
template<typename T, int N>
class ndarray<T, N> {
public:
typedef std::array<T, N> type;
};
template<typename T, int ...dims>
using ndarray_t = typename ndarray<T, dims...>::type;
template<typename T, int D>
class dyn_ndarray {
typedef std::vector<typename dyn_narray<T, D-1>::type> type;
};
template<typename T>
class dyn_ndarray<T, 0> {
typedef T type;
};
template<typename T, int D>
using dyn_ndarray_t = dyn_ndarray<T, D>::type;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment