Last active
November 26, 2021 07:19
-
-
Save GoBigorGoHome/adceff68d2373ed193a0efa0a9584c04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file presents some implementations of ndarray function/class. | |
// Function | |
// #1 | |
template<typename T> T get_vector() { | |
return T(); | |
} | |
template<typename T, int dimension, int... Dimensions> auto get_vector() { | |
using value_type = decltype(get_vector<T, Dimensions...>()); | |
return std::vector<value_type>(dimension, get_vector<T, Dimensions...>()); | |
} | |
// #2 (by cuiaoxiang) | |
template<class T> auto vect(const T& v, int n) { | |
return std::vector<T>(n, v); | |
} | |
template<class T, class... D> auto vect(const T& v, int n, D... m) { | |
return std::vector<decltype(vect(v, m...))>(n, vect(v, m...)); | |
} | |
// Class | |
// arbitrary-dimensional array that allows non-constexpr extents. | |
// Usage: ndarray<dimension, value_type> arr(extents..., init_value); | |
// All extensions but one for the last dimension are required. An initial value | |
// for all array items can be specified if all extensions are present. | |
// Examples: | |
// ndarray<2, int> a(2, 3, -1); | |
// ndarray<3, int> b(2, 3, 4); | |
// ndarray<3, int> c(2, 3); | |
template<unsigned dimension, typename T> | |
class ndarray : public std::vector<ndarray<dimension - 1, T>> { | |
public: | |
using std::vector<ndarray<dimension - 1, T>>::vector; | |
template<typename... Args> | |
ndarray(unsigned d, Args... args) | |
: std::vector<ndarray<dimension - 1, T>>( | |
d, ndarray<dimension - 1, T>(args...)) {} | |
}; | |
template<typename T> class ndarray<1, T> : public std::vector<T> { | |
public: | |
using std::vector<T>::vector; | |
template<typename... Args> | |
explicit ndarray(Args&&... args) | |
: std::vector<T>(std::forward<Args>(args)...) {} | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment