sparrow 0.6.0
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
24
25namespace sparrow
26{
38 class array
39 {
40 public:
41
42 using size_type = std::size_t;
45
49 array() = default;
50
57 template <layout A>
58 requires(not std::is_lvalue_reference_v<A>)
59 explicit array(A&& a);
60
68 template <layout A>
69 explicit array(A* a);
70
78 template <layout A>
79 explicit array(std::shared_ptr<A> a);
80
90
100
110
117 array(const array& rhs) = default;
118
125 array& operator=(const array& rhs) = default;
126
132 array(array&& rhs) = default;
133
139 array& operator=(array&& rhs) = default;
140
145
149 SPARROW_API std::optional<std::string_view> name() const;
150
155 SPARROW_API void set_name(std::optional<std::string_view> name);
156
160 SPARROW_API std::optional<key_value_view> metadata() const;
161
166 template <input_metadata_container R = std::vector<metadata_pair>>
167 void set_metadata(std::optional<R> metadata);
168
172 SPARROW_API bool empty() const;
173
178
187
194
200
206
207 template <class F>
208 using visit_result_t = std::invoke_result_t<F, null_array>;
209
219 template <class F>
220 visit_result_t<F> visit(F&& func) const;
221
232
243
244 private:
245
247
248 SPARROW_API arrow_proxy& get_arrow_proxy();
249 SPARROW_API const arrow_proxy& get_arrow_proxy() const;
250
251 cloning_ptr<array_wrapper> p_array = nullptr;
252
254 };
255
265 bool operator==(const array& lhs, const array& rhs);
266
267 template <class A>
268 concept layout_or_array = layout<A> or std::same_as<A, array>;
269
278 template <layout_or_array A>
279 bool owns_arrow_array(const A& a);
280
289 template <layout_or_array A>
290 bool owns_arrow_schema(const A& a);
291
299 template <layout_or_array A>
301
309 template <layout_or_array A>
311
319 template <layout_or_array A>
320 std::pair<ArrowArray*, ArrowSchema*> get_arrow_structures(A& a);
321
333 template <layout_or_array A>
335
347 template <layout_or_array A>
349
362 template <layout_or_array A>
363 std::pair<ArrowArray, ArrowSchema> extract_arrow_structures(A&& a);
364
365 // Implementation
366
367 template <input_metadata_container R>
368 void array::set_metadata(std::optional<R> metadata)
369 {
370 get_arrow_proxy().set_metadata(metadata);
371 }
372}
Dynamically typed array encapsulating an Arrow layout.
Definition array_api.hpp:39
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 const_reference at(size_type index) const
std::invoke_result_t< F, null_array > visit_result_t
array & operator=(const array &rhs)=default
Overwrites the content of the array with a deep copy of the given array, event if it does not have th...
void set_metadata(std::optional< R > metadata)
Sets the metadata of the array to metadata.
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:42
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 const_reference front() const
Returns a constant reference to the first element in the container.
SPARROW_API size_type size() const
array()=default
Constructs an empty array.
array_traits::const_reference const_reference
Definition array_api.hpp:44
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 ownerhips is transferred to the Array.
SPARROW_API array slice(size_type start, size_type end) const
Slices the array to keep only the elements between the given start and end.
visit_result_t< F > visit(F &&func) const
Returns the result of calling the given functor func on the layout internally hold by the array.
Definition array.hpp:42
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 array(ArrowArray *array, ArrowSchema *schema)
Constructs an array from the given Arrow C structures.
array_traits::value_type value_type
Definition array_api.hpp:43
SPARROW_API array slice_view(size_type start, size_type end) const
Slices the array to keep only the elements between the given start and end.
Proxy class over ArrowArray and ArrowSchema.
Smart pointer behaving like a copiable std::unique_ptr.
Definition memory.hpp:127
#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:79
std::pair< ArrowArray, ArrowSchema > extract_arrow_structures(A &&a)
Extracts the internal ArrowArrays and ArrowSchema structures from the given array or typed layout.
Definition array.hpp:91
ArrowSchema extract_arrow_schema(A &&a)
Extracts the internal ArrowSchema structure from the given array or typed layout.
Definition array.hpp:85
ArrowSchema * get_arrow_schema(A &a)
Returns a pointer to the internal ArrowSchema of the given array or layout.
Definition array.hpp:66
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:72
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::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