1
1
#ifndef LIBRAPID_ARRAY_FROM_DATA_HPP
2
2
#define LIBRAPID_ARRAY_FROM_DATA_HPP
3
3
4
- namespace librapid {
4
+ namespace librapid ::array {
5
5
template <typename ShapeType, typename StorageType>
6
- LIBRAPID_ALWAYS_INLINE auto array::ArrayContainer<ShapeType, StorageType>::fromData(
7
- const std::initializer_list<Scalar> &data) -> ArrayContainer {
8
- static_assert (!std::is_same_v<ShapeType, MatrixShape>,
9
- " Cannot create a matrix from a 1D array" );
6
+ LIBRAPID_ALWAYS_INLINE
7
+ ArrayContainer<ShapeType, StorageType>::ArrayContainer(
8
+ const std::initializer_list<Scalar> &data) :
9
+ m_shape ({data.size ()}),
10
+ m_size(data.size()), m_storage(StorageType::fromData(data)) {
10
11
LIBRAPID_ASSERT (data.size () > 0 , " Array must have at least one element" );
11
- return ArrayContainer (data);
12
12
}
13
13
14
14
template <typename ShapeType, typename StorageType>
15
- LIBRAPID_ALWAYS_INLINE auto
16
- array::ArrayContainer<ShapeType, StorageType>::fromData(const std::vector<Scalar> &data)
17
- -> ArrayContainer {
18
- static_assert (!std::is_same_v<ShapeType, MatrixShape>,
19
- " Cannot create a matrix from a 1D array" );
15
+ LIBRAPID_ALWAYS_INLINE ArrayContainer<ShapeType, StorageType>::ArrayContainer::ArrayContainer(
16
+ const std::vector<Scalar> &data) :
17
+ m_shape ({data.size ()}),
18
+ m_size(data.size()), m_storage(StorageType::fromData(data)) {
20
19
LIBRAPID_ASSERT (data.size () > 0 , " Array must have at least one element" );
21
- return ArrayContainer (data);
22
20
}
23
21
24
22
template <typename ShapeType, typename StorageType>
25
- LIBRAPID_ALWAYS_INLINE auto array:: ArrayContainer<ShapeType, StorageType>::fromData (
26
- const std::initializer_list<std::initializer_list<Scalar>> &data) -> ArrayContainer {
23
+ LIBRAPID_ALWAYS_INLINE ArrayContainer<ShapeType, StorageType>::ArrayContainer (
24
+ const std::initializer_list<std::initializer_list<Scalar>> &data) {
27
25
LIBRAPID_ASSERT (data.size () > 0 , " Cannot create a zero-sized array" );
28
26
29
27
if constexpr (std::is_same_v<ShapeType, MatrixShape>) {
@@ -36,7 +34,9 @@ namespace librapid {
36
34
res (i, j) = data.begin ()[i].begin ()[j];
37
35
}
38
36
}
39
- return res;
37
+
38
+ // return res;
39
+ *this = res;
40
40
} else {
41
41
auto newShape = ShapeType ({data.size (), data.begin ()->size ()});
42
42
#if defined(LIBRAPID_ENABLE_ASSERT)
@@ -47,14 +47,16 @@ namespace librapid {
47
47
#endif
48
48
auto res = ArrayContainer (newShape);
49
49
int64_t index = 0 ;
50
- for (const auto &item : data) res[index ++] = fromData (item);
51
- return res;
50
+ for (const auto &item : data) res[index ++] = ArrayContainer (item);
51
+
52
+ // return res;
53
+ *this = res;
52
54
}
53
55
}
54
56
55
57
template <typename ShapeType, typename StorageType>
56
- LIBRAPID_ALWAYS_INLINE auto array:: ArrayContainer<ShapeType, StorageType>::fromData (
57
- const std::vector<std::vector<Scalar>> &data) -> ArrayContainer {
58
+ LIBRAPID_ALWAYS_INLINE ArrayContainer<ShapeType, StorageType>::ArrayContainer (
59
+ const std::vector<std::vector<Scalar>> &data) {
58
60
LIBRAPID_ASSERT (data.size () > 0 , " Cannot create a zero-sized array" );
59
61
60
62
if constexpr (std::is_same_v<ShapeType, MatrixShape>) {
@@ -65,7 +67,9 @@ namespace librapid {
65
67
" Arrays must have consistent shapes" );
66
68
for (size_t j = 0 ; j < data[i].size (); ++j) { res (i, j) = data[i][j]; }
67
69
}
68
- return res;
70
+
71
+ // return res;
72
+ *this = res;
69
73
} else {
70
74
auto newShape = ShapeType ({data.size (), data.begin ()->size ()});
71
75
#if defined(LIBRAPID_ENABLE_ASSERT)
@@ -76,19 +80,21 @@ namespace librapid {
76
80
#endif
77
81
auto res = ArrayContainer (newShape);
78
82
int64_t index = 0 ;
79
- for (const auto &item : data) res[index ++] = fromData (item);
80
- return res;
83
+ for (const auto &item : data) res[index ++] = ArrayContainer (item);
84
+
85
+ // return res;
86
+ *this = res;
81
87
}
82
88
}
83
89
84
90
#define HIGHER_DIMENSIONAL_FROM_DATA (TYPE ) \
85
91
template <typename ShapeType, typename StorageType> \
86
- LIBRAPID_ALWAYS_INLINE auto array:: ArrayContainer<ShapeType, StorageType>::fromData( \
87
- const TYPE &data) -> ArrayContainer { \
92
+ LIBRAPID_ALWAYS_INLINE ArrayContainer<ShapeType, StorageType>::ArrayContainer( \
93
+ const TYPE &data) { \
88
94
LIBRAPID_ASSERT (data.size () > 0 , " Cannot create a zero-sized array" ); \
89
95
std::vector<ArrayContainer> tmp (data.size ()); \
90
96
int64_t index = 0 ; \
91
- for (const auto &item : data) tmp[index ++] = std::move (fromData (item)); \
97
+ for (const auto &item : data) tmp[index ++] = std::move (ArrayContainer (item)); \
92
98
auto zeroShape = tmp[0 ].shape (); \
93
99
for (int64_t i = 0 ; i < data.size (); ++i) \
94
100
LIBRAPID_ASSERT (tmp[i].shape ().operator ==(zeroShape), \
@@ -98,7 +104,7 @@ namespace librapid {
98
104
for (size_t i = 0 ; i < zeroShape.ndim (); ++i) { newShape[i + 1 ] = zeroShape[i]; } \
99
105
auto res = Array<Scalar, Backend>(newShape); \
100
106
for (int64_t i = 0 ; i < data.size (); ++i) res[i] = tmp[i]; \
101
- return res; \
107
+ * this = res; \
102
108
}
103
109
104
110
#define SINIT (SUB_TYPE ) std::initializer_list<SUB_TYPE>
@@ -120,6 +126,6 @@ namespace librapid {
120
126
121
127
#undef SINIT
122
128
#undef HIGHER_DIMENSIONAL_FROM_DATA
123
- } // namespace librapid
129
+ } // namespace librapid::array
124
130
125
131
#endif // LIBRAPID_ARRAY_FROM_DATA_HPP
0 commit comments