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>
66 requires(std::convertible_to<std::ranges::range_value_t<NR>, std::string> and std::same_as<std::ranges::range_value_t<CR>,
array>)
76 template <std::ranges::input_range CR>
77 requires std::same_as<std::ranges::range_value_t<CR>,
array>
178 template <
class U,
class R>
179 [[nodiscard]] std::vector<U> to_vector(R&& range)
const;
183 [[nodiscard]]
SPARROW_API bool check_consistency()
const;
185 std::vector<name_type> m_name_list;
186 std::vector<array> m_array_list;
187 mutable std::unordered_map<name_type, const array*> m_array_map;
188 mutable bool m_dirty_map =
true;
206 template <std::ranges::input_range NR, std::ranges::input_range CR>
207 requires(std::convertible_to<std::ranges::range_value_t<NR>, std::string>
208 and std::same_as<std::ranges::range_value_t<CR>,
array>)
210 : m_name_list(to_vector<name_type>(std::move(
names)))
211 , m_array_list(to_vector<array>(std::move(
columns)))
213 update_array_map_cache();
218 inline std::vector<record_batch::name_type>
get_names(
const std::vector<array>& array_list)
220 const auto names = array_list
221 | std::views::transform(
224 return ar.
name().value();
227 return {names.begin(), names.end()};
231 template <std::ranges::input_range CR>
232 requires std::same_as<std::ranges::range_value_t<CR>, array>
237 update_array_map_cache();
240 template <
class U,
class R>
241 std::vector<U> record_batch::to_vector(R&& range)
const
244 if constexpr (std::ranges::sized_range<
decltype(range)>)
246 v.reserve(std::ranges::size(range));
248 std::ranges::move(range, std::back_inserter(v));
253#if defined(__cpp_lib_format)
255struct std::formatter<
sparrow::record_batch>
257 constexpr auto parse(std::format_parse_context& ctx)
262 auto format(
const sparrow::record_batch& rb, std::format_context& ctx)
const
264 const auto values_by_columns = rb.
columns()
265 | std::views::transform(
266 [&rb](
const auto& ar)
268 return std::views::iota(0u, rb.
nb_rows())
269 | std::views::transform(
285 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.
record_batch(NR &&names, CR &&columns)
Constructs a record_batch from a range of names and a range of arrays.
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
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 &stream, T n)