sparrow 0.3.0
Loading...
Searching...
No Matches
trivial_copyable_data_access.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 implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
20
21namespace sparrow
22{
23 template <typename T>
24 concept trivial_copyable_type = std::is_trivially_copyable_v<T> && std::is_standard_layout_v<T>;
25
26 namespace details
27 {
34 template <trivial_copyable_type T, typename L>
36 {
37 public:
38
40 using inner_reference = T&;
41 using inner_const_reference = const T&;
44
47
53 trivial_copyable_data_access(L* layout, size_t data_buffer_index)
54 : p_layout(layout)
55 , m_data_buffer_index(data_buffer_index)
56 {
57 }
58
59 [[nodiscard]] constexpr T* data()
60 {
61 return get_proxy().buffers()[m_data_buffer_index].template data<T>()
62 + static_cast<size_t>(get_proxy().offset());
63 }
64
65 [[nodiscard]] constexpr const T* data() const
66 {
67 return get_proxy().buffers()[m_data_buffer_index].template data<T>()
68 + static_cast<size_t>(get_proxy().offset());
69 }
70
71 [[nodiscard]] constexpr T& value(size_t i)
72 {
73 SPARROW_ASSERT_TRUE(i < get_proxy().length());
74 return data()[i];
75 }
76
77 [[nodiscard]] constexpr const T& value(size_t i) const
78 {
79 SPARROW_ASSERT_TRUE(i < get_proxy().length());
80 return data()[i];
81 }
82
84 {
85 auto& buffers = get_proxy().get_array_private_data()->buffers();
86 return make_buffer_adaptor<T>(buffers[m_data_buffer_index]);
87 }
88
89 constexpr void resize_values(size_t new_length, const T& value)
90 {
91 const size_t new_size = new_length + static_cast<size_t>(get_proxy().offset());
92 get_data_buffer().resize(new_size, value);
93 }
94
95 constexpr value_iterator insert_value(const_value_iterator pos, T value, size_t count)
96 {
97 const const_value_iterator value_cbegin{data()};
98 const const_value_iterator value_cend{sparrow::next(value_cbegin, get_proxy().length())};
99 SPARROW_ASSERT_TRUE(value_cbegin <= pos);
100 SPARROW_ASSERT_TRUE(pos <= value_cend);
101 const auto distance = std::distance(value_cbegin, sparrow::next(pos, get_proxy().offset()));
102 get_data_buffer().insert(pos, count, value);
103 const value_iterator value_begin{data()};
104 return sparrow::next(value_begin, distance);
105 }
106
107 constexpr value_iterator insert_value(size_t idx, T value, size_t count)
108 {
109 SPARROW_ASSERT_TRUE(idx <= get_proxy().length());
110 const const_value_iterator begin{data()};
111 const const_value_iterator it = sparrow::next(begin, idx);
112 return insert_value(it, value, count);
113 }
114
115 // Template parameter InputIt must be an iterator type that iterates over elements of type T
116 template <mpl::iterator_of_type<T> InputIt>
117 constexpr value_iterator insert_values(const_value_iterator pos, InputIt first, InputIt last)
118 {
119 const const_value_iterator value_cbegin{data()};
120 const const_value_iterator value_cend{sparrow::next(value_cbegin, get_proxy().length())};
121 SPARROW_ASSERT_TRUE(value_cbegin <= pos);
122 SPARROW_ASSERT_TRUE(pos <= value_cend);
123 const auto distance = std::distance(value_cbegin, sparrow::next(pos, get_proxy().offset()));
124 get_data_buffer().insert(pos, first, last);
125 const value_iterator value_begin{data()};
126 return sparrow::next(value_begin, distance);
127 }
128
129 template <mpl::iterator_of_type<T> InputIt>
130 constexpr value_iterator insert_values(size_t idx, InputIt first, InputIt last)
131 {
132 SPARROW_ASSERT_TRUE(idx <= get_proxy().length());
133 const const_value_iterator begin{data()};
134 const const_value_iterator it = sparrow::next(begin, idx);
135 return insert_values(it, first, last);
136 }
137
139 {
140 const const_value_iterator value_cbegin{data()};
141 const const_value_iterator value_cend{sparrow::next(value_cbegin, get_proxy().length())};
142 SPARROW_ASSERT_TRUE(value_cbegin <= pos);
143 SPARROW_ASSERT_TRUE(pos < value_cend);
144 const auto distance = static_cast<size_t>(
145 std::distance(value_cbegin, sparrow::next(pos, get_proxy().offset()))
146 );
147 auto data_buffer = get_data_buffer();
148 const auto first = sparrow::next(data_buffer.cbegin(), distance);
149 const auto last = sparrow::next(first, count);
150 data_buffer.erase(first, last);
151 const value_iterator value_begin{data()};
152 return sparrow::next(value_begin, distance);
153 }
154
155 constexpr value_iterator erase_values(size_t idx, size_t count)
156 {
157 SPARROW_ASSERT_TRUE(idx <= get_proxy().length());
158 const const_value_iterator cbegin{data()};
159 const const_value_iterator it = sparrow::next(cbegin, idx);
160 erase_values(it, count);
161 return sparrow::next(value_iterator{data()}, idx);
162 }
163
164 private:
165
166 [[nodiscard]] arrow_proxy& get_proxy()
167 {
169 }
170
171 [[nodiscard]] const arrow_proxy& get_proxy() const
172 {
174 }
175
176 L* p_layout;
177 size_t m_data_buffer_index;
178 };
179 }
180}
Proxy class over ArrowArray and ArrowSchema.
Class which have internally a reference to a contiguous container of a certain type and provides an A...
static const sparrow::arrow_proxy & get_arrow_proxy(const ARRAY &array)
constexpr value_iterator insert_values(const_value_iterator pos, InputIt first, InputIt last)
trivial_copyable_data_access(L *layout, size_t data_buffer_index)
Constructor for trivial_copyable_data_access.
constexpr value_iterator erase_values(const_value_iterator pos, size_t count)
constexpr value_iterator insert_values(size_t idx, InputIt first, InputIt last)
constexpr buffer_adaptor< T, buffer< uint8_t > & > get_data_buffer()
constexpr value_iterator erase_values(size_t idx, size_t count)
constexpr void resize_values(size_t new_length, const T &value)
constexpr value_iterator insert_value(const_value_iterator pos, T value, size_t count)
constexpr value_iterator insert_value(size_t idx, T value, size_t count)
#define SPARROW_ASSERT_TRUE(expr__)
constexpr InputIt next(InputIt it, Distance n)
Definition iterator.hpp:503
auto make_buffer_adaptor(FromBufferRef &buf)