sparrow 2.4.0
C++20 idiomatic APIs for the Apache Arrow Columnar Format
Loading...
Searching...
No Matches
array_api.hpp
Go to the documentation of this file.
1// Copyright 2024 Man Group Operations Limited
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or mplied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <iterator>
18#include <ranges>
19#include <stdexcept>
20
35
36namespace sparrow
37{
49 class array
50 {
51 public:
52
53 using size_type = std::size_t;
56
59
63 array() = default;
64
72 template <layout A>
73 requires(not std::is_lvalue_reference_v<A>)
74 constexpr explicit array(A&& a);
75
84 template <layout A>
85 constexpr explicit array(A* a);
86
95 template <layout A>
96 constexpr explicit array(std::shared_ptr<A> a);
97
107
117
127
134 constexpr array(const array& rhs) = default;
135
143 array& operator=(const array& rhs) = default;
144
150 array(array&& rhs) = default;
151
158 array& operator=(array&& rhs) = default;
159
163 [[nodiscard]] SPARROW_API enum data_type data_type() const;
164
174 [[nodiscard]] SPARROW_API std::optional<array> dictionary() const;
175
180 [[nodiscard]] auto children() const
181 {
182 return get_arrow_proxy().children()
183 | std::views::transform(
184 [](const arrow_proxy& child)
185 {
186 return array{child.view()};
187 }
188 );
189 }
190
194 [[nodiscard]] SPARROW_API std::optional<std::string_view> name() const;
195
201 SPARROW_API void set_name(std::optional<std::string_view> name);
202
206 [[nodiscard]] SPARROW_API std::optional<key_value_view> metadata() const;
207
214 template <input_metadata_container R = std::vector<metadata_pair>>
215 constexpr void set_metadata(std::optional<R> metadata);
216
222 [[nodiscard]] SPARROW_API bool empty() const;
223
227 [[nodiscard]] SPARROW_API size_type size() const;
228
232 [[nodiscard]] SPARROW_API size_type offset() const;
233
237 [[nodiscard]] SPARROW_API std::int64_t null_count() const;
238
245 [[nodiscard]] SPARROW_API const_reference at(size_type index) const;
246
253
260 [[nodiscard]] SPARROW_API const_reference front() const;
261
268 [[nodiscard]] SPARROW_API const_reference back() const;
269
275 [[nodiscard]] SPARROW_API iterator begin() const;
276
282 [[nodiscard]] SPARROW_API iterator end() const;
283
289 [[nodiscard]] SPARROW_API const_iterator cbegin() const;
290
296 [[nodiscard]] SPARROW_API const_iterator cend() const;
297
298 template <class F>
299 using visit_result_t = std::invoke_result_t<F, null_array>;
300
311 template <class F>
312 constexpr visit_result_t<F> visit(F&& func) const;
313
319 [[nodiscard]] SPARROW_API array view() const;
320
326 [[nodiscard]] SPARROW_API bool is_view() const;
327
331 [[nodiscard]] SPARROW_API bool is_dictionary() const;
332
349 [[nodiscard]] SPARROW_API array slice(size_type start, size_type end) const;
350
370 [[nodiscard]] SPARROW_API array slice_view(size_type start, size_type end) const;
371
386
396
409
420
429
430
431 private:
432
439
445 [[nodiscard]] SPARROW_API arrow_proxy& get_arrow_proxy();
446
452 [[nodiscard]] SPARROW_API const arrow_proxy& get_arrow_proxy() const;
453
454 cloning_ptr<array_wrapper> p_array = nullptr;
455
457 };
458
468 bool operator==(const array& lhs, const array& rhs);
469
470 template <class A>
471 concept layout_or_array = layout<A> or std::same_as<A, array>;
472
481 template <layout_or_array A>
482 bool owns_arrow_array(const A& a);
483
493 template <layout_or_array A>
494 bool owns_arrow_schema(const A& a);
495
504 template <layout_or_array A>
506
515 template <layout_or_array A>
516 const ArrowArray* get_arrow_array(const A& a);
517
526 template <layout_or_array A>
528
537 template <layout_or_array A>
538 const ArrowSchema* get_arrow_schema(const A& a);
539
548 template <layout_or_array A>
549 std::pair<ArrowArray*, ArrowSchema*> get_arrow_structures(A& a);
550
559 template <layout_or_array A>
560 std::pair<const ArrowArray*, const ArrowSchema*> get_arrow_structures(const A& a);
561
573 template <layout_or_array A>
575
587 template <layout_or_array A>
589
602 template <layout_or_array A>
603 std::pair<ArrowArray, ArrowSchema> extract_arrow_structures(A&& a);
604
605 // Implementation
606
607 template <input_metadata_container R>
608 constexpr void array::set_metadata(std::optional<R> metadata)
609 {
610 get_arrow_proxy().set_metadata(metadata);
611 }
612}
Dynamically typed array encapsulating an Arrow layout.
Definition array_api.hpp:50
SPARROW_API iterator begin() const
Returns an iterator to the beginning of the array.
SPARROW_API const_reference at(size_type index) const
std::invoke_result_t< F, null_array > visit_result_t
constexpr visit_result_t< F > visit(F &&func) const
Returns the result of calling the given functor func on the layout internally held by the array.
Definition array.hpp:42
array & operator=(const array &rhs)=default
Overwrites the content of the array with a deep copy of the given array, even if it does not have the...
SPARROW_API iterator erase(const_iterator pos)
Inserts a copy of value before pos in the array, repeating the insertion count times.
array & operator=(array &&rhs)=default
The move assignment operator.
SPARROW_API array(ArrowArray &&array, ArrowSchema *schema)
Constructs an Array from the given Arrow C structures.
array(array &&rhs)=default
The move constructor.
SPARROW_API bool empty() const
Checks if the array has no element, i.e.
std::size_t size_type
Definition array_api.hpp:53
const_iterator iterator
Definition array_api.hpp:58
SPARROW_API std::optional< key_value_view > metadata() const
SPARROW_API std::optional< std::string_view > name() const
SPARROW_API const_reference back() const
Returns a constant reference to the last element in the container.
SPARROW_API bool is_dictionary() const
auto children() const
SPARROW_API const_reference front() const
Returns a constant reference to the first element in the container.
SPARROW_API void slice_inplace(size_type start, size_type end)
Restricts *this to the elements in the half-open range [start, end) in place.
array_const_iterator const_iterator
Definition array_api.hpp:57
SPARROW_API size_type size() const
array()=default
Constructs an empty array.
constexpr void set_metadata(std::optional< R > metadata)
Sets the metadata of the array to metadata.
array_traits::const_reference const_reference
Definition array_api.hpp:55
SPARROW_API std::int64_t null_count() const
SPARROW_API const_reference operator[](size_type index) const
SPARROW_API array(ArrowArray &&array, ArrowSchema &&schema)
Constructs an Array from the given Arrow C structures, whose ownership is transferred to the Array.
SPARROW_API iterator insert(const_iterator pos, const_iterator first, const_iterator last, size_type count)
Inserts elements from the range [first, last) before the element at the specified position,...
SPARROW_API iterator erase(const_iterator first, const_iterator last)
Removes the elements in the range [ first , last ) from the array.
SPARROW_API array slice(size_type start, size_type end) const
Returns a deep-copy of the array restricted to the elements in the half-open range [start,...
SPARROW_API size_type offset() const
SPARROW_API const_iterator cend() const
Returns a constant iterator to the end of the array.
SPARROW_API enum data_type data_type() const
SPARROW_API void set_name(std::optional< std::string_view > name)
Sets the name of the array to name.
SPARROW_API bool is_view() const
Checks if the array is a view.
SPARROW_API iterator insert(const_iterator pos, const_iterator first, const_iterator last)
Inserts elements from the range [first, last) before the element at the specified position.
SPARROW_API array view() const
Returns a view of the array.
SPARROW_API const_iterator cbegin() const
Returns a constant iterator to the beginning of the array.
SPARROW_API array(ArrowArray *array, ArrowSchema *schema)
Constructs an array from the given Arrow C structures.
array_traits::value_type value_type
Definition array_api.hpp:54
constexpr array(const array &rhs)=default
Performs a deep copy of the given array, even if it does not have the ownership of its internal data.
SPARROW_API array slice_view(size_type start, size_type end) const
Returns a zero-copy view of the array restricted to the elements in the half-open range [start,...
SPARROW_API std::optional< array > dictionary() const
Retrieves the dictionary array associated with this array, if it exists.
SPARROW_API iterator end() const
Returns an iterator to the end of the array.
SPARROW_API arrow_proxy view() const
Get a non-owning view of the arrow_proxy.
Smart pointer behaving like a copiable std::unique_ptr.
Definition memory.hpp:126
Concept for layouts.
#define SPARROW_API
Definition config.hpp:38
SPARROW_API bool operator==(const array &lhs, const array &rhs)
Compares the content of two arrays.
ArrowArray extract_arrow_array(A &&a)
Extracts the internal ArrowArray structure from the given Array or typed layout.
Definition array.hpp:98
std::pair< ArrowArray, ArrowSchema > extract_arrow_structures(A &&a)
Extracts the internal ArrowArray and ArrowSchema structures from the given array or typed layout.
Definition array.hpp:110
ArrowSchema extract_arrow_schema(A &&a)
Extracts the internal ArrowSchema structure from the given array or typed layout.
Definition array.hpp:104
ArrowSchema * get_arrow_schema(A &a)
Returns a pointer to the internal ArrowSchema of the given array or layout.
Definition array.hpp:72
bool owns_arrow_schema(const A &a)
Returns true if the given layout or array has ownership of its internal ArrowSchema.
Definition array.hpp:54
bool owns_arrow_array(const A &a)
Returns true if the given layout or array has ownership of its internal ArrowArray.
Definition array.hpp:48
std::pair< ArrowArray *, ArrowSchema * > get_arrow_structures(A &a)
Returns pointers to the internal ArrowArray and ArrowSchema of the given Array or layout.
Definition array.hpp:84
ArrowArray * get_arrow_array(A &a)
Returns a pointer to the internal ArrowArray of the given array or layout.
Definition array.hpp:60
mpl::rename< mpl::unique< mpl::transform< detail::array_const_reference_t, all_base_types_t > >, nullable_variant > const_reference
mpl::rename< mpl::transform< detail::array_value_type_t, all_base_types_t >, nullable_variant > value_type