18#include <initializer_list>
21#include <unordered_map>
28#if defined(__cpp_lib_format)
54 using name_range = std::ranges::ref_view<const std::vector<name_type>>;
55 using column_range = std::ranges::ref_view<const std::vector<array>>;
65 template <std::ranges::input_range NR, std::ranges::input_range CR>
67 std::convertible_to<std::ranges::range_value_t<NR>, std::string>
68 and std::same_as<std::ranges::range_value_t<CR>,
array>
79 template <std::ranges::input_range CR>
80 requires std::same_as<std::ranges::range_value_t<CR>,
array>
181 template <
class U,
class R>
182 [[nodiscard]] std::vector<U> to_vector(R&& range)
const;
186 [[nodiscard]]
SPARROW_API bool check_consistency()
const;
188 std::vector<name_type> m_name_list;
189 std::vector<array> m_array_list;
190 mutable std::unordered_map<name_type, const array*> m_array_map;
191 mutable bool m_dirty_map =
true;
209 template <std::ranges::input_range NR, std::ranges::input_range CR>
210 requires(std::convertible_to<std::ranges::range_value_t<NR>, std::string>
211 and std::same_as<std::ranges::range_value_t<CR>,
array>)
213 : m_name_list(to_vector<name_type>(std::move(
names)))
214 , m_array_list(to_vector<array>(std::move(
columns)))
216 update_array_map_cache();
221 inline std::vector<record_batch::name_type>
get_names(
const std::vector<array>& array_list)
223 const auto names = array_list
224 | std::views::transform(
227 return ar.
name().value();
230 return {names.begin(), names.end()};
234 template <std::ranges::input_range CR>
235 requires std::same_as<std::ranges::range_value_t<CR>, array>
240 update_array_map_cache();
243 template <
class U,
class R>
244 std::vector<U> record_batch::to_vector(R&& range)
const
247 if constexpr (std::ranges::sized_range<
decltype(range)>)
249 v.reserve(std::ranges::size(range));
251 std::ranges::move(range, std::back_inserter(v));
256#if defined(__cpp_lib_format)
258struct std::formatter<
sparrow::record_batch>
260 constexpr auto parse(std::format_parse_context& ctx)
265 auto format(
const sparrow::record_batch& rb, std::format_context& ctx)
const
267 const auto values_by_columns = rb.
columns()
268 | std::views::transform(
269 [&rb](
const auto& ar)
271 return std::views::iota(0u, rb.
nb_rows())
272 | std::views::transform(
288 os << std::format(
"{}", value);
Dynamically typed array encapsulating an Arrow layout.
SPARROW_API std::optional< std::string_view > name() const
Table-like data structure.
std::initializer_list< std::pair< name_type, array > > initializer_type
SPARROW_API void add_column(array column)
Appends the array column to the record batch, and maps it to its internal name.
SPARROW_API record_batch & operator=(const record_batch &)
SPARROW_API const array & get_column(const name_type &key) const
SPARROW_API record_batch(struct_array &&ar)
Construct a record batch from the given struct array.
SPARROW_API name_range names() const
SPARROW_API record_batch(const record_batch &)
record_batch & operator=(record_batch &&)=default
SPARROW_API bool contains_column(const name_type &key) const
Checks if the record_batch constains a column mapped to the specified name.
SPARROW_API struct_array extract_struct_array()
Moves the internal columns of the record batch into a struct_array object.
SPARROW_API column_range columns() const
SPARROW_API const name_type & get_column_name(size_type index) const
SPARROW_API void add_column(name_type name, array column)
Appends the array column to the record batch, and maps it with name.
SPARROW_API size_type nb_rows() const
SPARROW_API const array & get_column(size_type index) const
SPARROW_API size_type nb_columns() const
record_batch(record_batch &&)=default
record_batch(NR &&names, CR &&columns)
Constructs a record_batch from a range of names and a range of arrays.
SPARROW_API record_batch(initializer_type init)
Constructs a record_batch from a list of std::pair<name_type, array>.
std::ranges::ref_view< const std::vector< array > > column_range
std::ranges::ref_view< const std::vector< name_type > > name_range
std::vector< record_batch::name_type > get_names(const std::vector< array > &array_list)
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
constexpr void to_table_with_columns(OutputIt out, const Headers &headers, const Columns &columns)
std::ostream & operator<<(std::ostream &os, const sparrow::nullval_t &)