|
22 | 22 | #include <vector> |
23 | 23 |
|
24 | 24 | #include "sparrow/array.hpp" |
| 25 | +#include "sparrow/layout/struct_layout/struct_array.hpp" |
25 | 26 | #include "sparrow/utils/contracts.hpp" |
26 | 27 |
|
27 | 28 | #if defined(__cpp_lib_format) |
@@ -65,13 +66,32 @@ namespace sparrow |
65 | 66 | requires(std::convertible_to<std::ranges::range_value_t<NR>, std::string> and std::same_as<std::ranges::range_value_t<CR>, array>) |
66 | 67 | record_batch(NR&& names, CR&& columns); |
67 | 68 |
|
| 69 | + /* |
| 70 | + * Constructs a @ref record_batch from a range of arrays. Each array |
| 71 | + * must have a name: if \c arr is an array, then \c arr.name(), must |
| 72 | + * not return an empty string. |
| 73 | + * |
| 74 | + * @param comumns An input range of arrays |
| 75 | + */ |
| 76 | + template <std::ranges::input_range CR> |
| 77 | + requires std::same_as<std::ranges::range_value_t<CR>, array> |
| 78 | + record_batch(CR&& columns); |
| 79 | + |
68 | 80 | /** |
69 | 81 | * Constructs a record_batch from a list of \c std::pair<name_type, array>. |
70 | 82 | * |
71 | 83 | * @param init a list of pair "name - array". |
72 | 84 | */ |
73 | 85 | SPARROW_API record_batch(initializer_type init); |
74 | 86 |
|
| 87 | + /** |
| 88 | + * Construct a record batch from the given struct array. |
| 89 | + * The array must owns its internal arrow structures. |
| 90 | + * |
| 91 | + * @param ar An input struct array |
| 92 | + */ |
| 93 | + SPARROW_API record_batch(struct_array&& ar); |
| 94 | + |
75 | 95 | SPARROW_API record_batch(const record_batch&); |
76 | 96 | SPARROW_API record_batch& operator=(const record_batch&); |
77 | 97 |
|
@@ -129,6 +149,13 @@ namespace sparrow |
129 | 149 | */ |
130 | 150 | SPARROW_API column_range columns() const; |
131 | 151 |
|
| 152 | + /** |
| 153 | + * Moves the internal columns of the record batch into a struct_array |
| 154 | + * object. The record batch is empty anymore after calling this |
| 155 | + * method. |
| 156 | + */ |
| 157 | + SPARROW_API struct_array extract_struct_array(); |
| 158 | + |
132 | 159 | private: |
133 | 160 |
|
134 | 161 | template <class U, class R> |
@@ -169,6 +196,31 @@ namespace sparrow |
169 | 196 | SPARROW_ASSERT_TRUE(check_consistency()); |
170 | 197 | } |
171 | 198 |
|
| 199 | + namespace detail |
| 200 | + { |
| 201 | + std::vector<record_batch::name_type> get_names(const std::vector<array>& array_list) |
| 202 | + { |
| 203 | + const auto names = array_list |
| 204 | + | std::views::transform( |
| 205 | + [](const array& ar) |
| 206 | + { |
| 207 | + return ar.name().value(); |
| 208 | + } |
| 209 | + ); |
| 210 | + return {names.begin(), names.end()}; |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | + template <std::ranges::input_range CR> |
| 215 | + requires std::same_as<std::ranges::range_value_t<CR>, array> |
| 216 | + record_batch::record_batch(CR&& columns) |
| 217 | + : m_name_list(detail::get_names(columns)) |
| 218 | + , m_array_list(to_vector<array>(std::move(columns))) |
| 219 | + { |
| 220 | + init_array_map(); |
| 221 | + SPARROW_ASSERT_TRUE(check_consistency()); |
| 222 | + } |
| 223 | + |
172 | 224 | template <class U, class R> |
173 | 225 | std::vector<U> record_batch::to_vector(R&& range) const |
174 | 226 | { |
|
0 commit comments