| 8 | namespace nd::impl { |
| 9 | |
| 10 | inline nd::array flatten(nd::array a) |
| 11 | { |
| 12 | if (a.dimensions() <= 1 && !a.is_dynamic()) { |
| 13 | return a; |
| 14 | } |
| 15 | return nd::switch_numeric_dtype(a.dtype(), [&a]<typename T>() { |
| 16 | icm::vector<T> v; |
| 17 | v.reserve(a.volume()); |
| 18 | for (int i = 0; i < a.size(); i++) { |
| 19 | auto arr = flatten(a[i]); |
| 20 | if (!arr.has_data()) { |
| 21 | for (int j = 0; j < arr.volume(); ++j) { |
| 22 | v.emplace_back(arr.template value<T>(j)); |
| 23 | } |
| 24 | } else { |
| 25 | ASSERT(arr.dtype() == a.dtype()); |
| 26 | auto d = arr.template data<T>(); |
| 27 | v.insert(v.end(), d.begin(), d.end()); |
| 28 | } |
| 29 | } |
| 30 | return adapt(std::move(v)); |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | template <typename T> |
| 35 | class flattened_array |
no test coverage detected