sparrow ..
Loading...
Searching...
No Matches
array_wrapper.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 <memory>
18#include <variant>
19
24
25namespace sparrow
26{
27 namespace detail
28 {
29 // Helper struct to allow overloading on the type of ARRAY
30 // to get the data_type for an array. This is needed since
31 // some arrays (for instance run_length_encoded_array)
32 // do not have a inner_value_type, therefore we specialize
33 // this in their respecitve headers.
34 template <class ARRAY>
36
37 template <class ARRAY>
39 {
40 [[nodiscard]] static constexpr bool get() noexcept
41 {
42 return false;
43 }
44 };
45 }
46
51 {
52 public:
53
54 using wrapper_ptr = std::unique_ptr<array_wrapper>;
55
56 virtual ~array_wrapper() = default;
57
61
62 [[nodiscard]] wrapper_ptr clone() const;
63
64 [[nodiscard]] constexpr enum data_type data_type() const noexcept;
65 [[nodiscard]] constexpr bool is_dictionary() const;
66
67 [[nodiscard]] constexpr arrow_proxy& get_arrow_proxy();
68 [[nodiscard]] constexpr const arrow_proxy& get_arrow_proxy() const;
69
70 protected:
71
72 constexpr array_wrapper(enum data_type dt);
73 constexpr array_wrapper(const array_wrapper&) = default;
74
75 private:
76
77 enum data_type m_data_type;
78 [[nodiscard]] virtual bool is_dictionary_impl() const = 0;
79 [[nodiscard]] virtual arrow_proxy& get_arrow_proxy_impl() = 0;
80 [[nodiscard]] virtual const arrow_proxy& get_arrow_proxy_impl() const = 0;
81 [[nodiscard]] virtual wrapper_ptr clone_impl() const = 0;
82 };
83
84 template <class T>
86 {
87 public:
88
89 array_wrapper_impl(T&& ar);
90 array_wrapper_impl(T* ar);
91 array_wrapper_impl(std::shared_ptr<T> ar);
92
93 ~array_wrapper_impl() override = default;
94
95 T& get_wrapped();
96 const T& get_wrapped() const;
97
98 private:
99
100 using wrapper_ptr = array_wrapper::wrapper_ptr;
101
102 [[nodiscard]] constexpr enum data_type get_data_type() const noexcept;
103
104 constexpr array_wrapper_impl(const array_wrapper_impl&);
105 [[nodiscard]] constexpr bool is_dictionary_impl() const noexcept override;
106 [[nodiscard]] constexpr arrow_proxy& get_arrow_proxy_impl() override;
107 [[nodiscard]] constexpr const arrow_proxy& get_arrow_proxy_impl() const override;
108 [[nodiscard]] wrapper_ptr clone_impl() const override;
109
110 using storage_type = std::variant<value_ptr<T>, std::shared_ptr<T>, T*>;
111 storage_type m_storage;
112 T* p_array;
113 };
114
115 template <class T>
117
118 template <class T>
119 const T& unwrap_array(const array_wrapper&);
120
121 /********************************
122 * array_wrapper implementation *
123 ********************************/
124
125 inline auto array_wrapper::clone() const -> wrapper_ptr
126 {
127 return clone_impl();
128 }
129
130 constexpr enum data_type array_wrapper::data_type() const noexcept
131 {
132 return m_data_type;
133 }
134
135 constexpr bool array_wrapper::is_dictionary() const
136 {
137 return is_dictionary_impl();
138 }
139
144
146 {
147 return get_arrow_proxy_impl();
148 }
149
151 : m_data_type(dt)
152 {
153 }
154
155 /*************************************
156 * array_wrapper_impl implementation *
157 *************************************/
158
159 template <class T>
161 : array_wrapper(get_data_type())
162 , m_storage(value_ptr<T>(std::move(ar)))
163 , p_array(std::get<value_ptr<T>>(m_storage).get())
164 {
165 }
166
167 template <class T>
169 : array_wrapper(get_data_type())
170 , m_storage(ar)
171 , p_array(ar)
172 {
173 }
174
175 template <class T>
177 : array_wrapper(get_data_type())
178 , m_storage(std::move(ar))
179 , p_array(std::get<std::shared_ptr<T>>(m_storage).get())
180 {
181 }
182
183 template <class T>
185 {
186 return *p_array;
187 }
188
189 template <class T>
191 {
192 return *p_array;
193 }
194
195 template <class T>
196 constexpr enum data_type array_wrapper_impl<T>::get_data_type() const noexcept
197 {
199 }
200
201 template <class T>
202 constexpr array_wrapper_impl<T>::array_wrapper_impl(const array_wrapper_impl& rhs)
203 : array_wrapper(rhs)
204 , m_storage(value_ptr<T>(T(rhs.get_wrapped())))
205 , p_array(
206 std::visit(
207 [](auto&& arg)
208 {
209 using U = std::decay_t<decltype(arg)>;
210 if constexpr (std::is_same_v<U, T*>)
211 {
212 return arg;
213 }
214 else
215 {
216 return arg.get();
217 }
218 },
219 m_storage
220 )
221 ) // Always deep copy
222 {
223 }
224
225 template <class T>
226 constexpr bool array_wrapper_impl<T>::is_dictionary_impl() const noexcept
227 {
229 }
230
231 template <class T>
236
237 template <class T>
242
243 template <class T>
244 auto array_wrapper_impl<T>::clone_impl() const -> wrapper_ptr
245 {
246 return wrapper_ptr{new array_wrapper_impl<T>(*this)};
247 }
248
249 template <class T>
251 {
252 return static_cast<array_wrapper_impl<T>&>(ar).get_wrapped();
253 }
254
255 template <class T>
256 const T& unwrap_array(const array_wrapper& ar)
257 {
258 return static_cast<const array_wrapper_impl<T>&>(ar).get_wrapped();
259 }
260}
wrapper_ptr clone_impl() const override
~array_wrapper_impl() override=default
constexpr arrow_proxy & get_arrow_proxy_impl() override
constexpr bool is_dictionary_impl() const noexcept override
Base class for array type erasure.
wrapper_ptr clone() const
std::unique_ptr< array_wrapper > wrapper_ptr
array_wrapper & operator=(array_wrapper &&)=delete
array_wrapper & operator=(const array_wrapper &)=delete
virtual ~array_wrapper()=default
virtual arrow_proxy & get_arrow_proxy_impl()=0
virtual bool is_dictionary_impl() const =0
constexpr bool is_dictionary() const
constexpr arrow_proxy & get_arrow_proxy()
array_wrapper(array_wrapper &&)=delete
constexpr enum data_type data_type() const noexcept
virtual wrapper_ptr clone_impl() const =0
static const sparrow::arrow_proxy & get_arrow_proxy(const ARRAY &array)
A value_ptr is a smart pointer that behaves like a value.
Definition memory.hpp:36
T & unwrap_array(array_wrapper &)
visit_result_t< F > visit(F &&func, const array_wrapper &ar)
Definition dispatch.hpp:49
data_type
Runtime identifier of arrow data types, usually associated with raw bytes with the associated value.
Metafunction for retrieving the data_type of a typed array.
static constexpr bool get() noexcept