forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_array.hpp
More file actions
86 lines (72 loc) · 1.68 KB
/
Copy pathflat_array.hpp
File metadata and controls
86 lines (72 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include "../adapt.hpp"
#include "../array.hpp"
#include <icm/vector.hpp>
namespace nd::impl {
inline nd::array flatten(nd::array a)
{
if (a.dimensions() <= 1 && !a.is_dynamic()) {
return a;
}
return nd::switch_numeric_dtype(a.dtype(), [&a]<typename T>() {
icm::vector<T> v;
v.reserve(a.volume());
for (int i = 0; i < a.size(); i++) {
auto arr = flatten(a[i]);
if (!arr.has_data()) {
for (int j = 0; j < arr.volume(); ++j) {
v.emplace_back(arr.template value<T>(j));
}
} else {
ASSERT(arr.dtype() == a.dtype());
auto d = arr.template data<T>();
v.insert(v.end(), d.begin(), d.end());
}
}
return adapt(std::move(v));
});
}
template <typename T>
class flattened_array
{
public:
using value_type = T;
public:
explicit flattened_array(nd::array arr)
: a_(std::move(arr))
, shape_(a_.volume())
{
}
public:
enum dtype dtype() const
{
return a_.dtype();
}
const icm::shape& shape() const
{
return shape_;
}
value_type value(int64_t index) const
{
return a_.value<value_type>(index);
}
array get(int64_t index) const
{
return nd::adapt(value(index));
}
nd::array eval() const
{
if (a_.is_dynamic() || a_.dimensions() > 1) {
return flatten(a_);
}
return nd::eval(a_);
}
constexpr bool is_dynamic() const noexcept
{
return false;
}
private:
nd::array a_;
icm::shape shape_;
};
} // namespace nd::impl