sparrow 0.3.0
Loading...
Searching...
No Matches
layout_utils.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::detail
22{
23 // Functor to get the value of the layout at index i.
24 //
25 // This is usefull to create a iterator over the values of a layout.
26 // This functor will be passed to the functor_index_iterator.
27 template <class LAYOUT_TYPE, class VALUE_TYPE>
29 {
30 public:
31
32 using value_type = VALUE_TYPE;
33 using layout_type = LAYOUT_TYPE;
34
35 constexpr explicit layout_value_functor(layout_type* layout_ = nullptr)
36 : p_layout(layout_)
37 {
38 }
39
40 [[nodiscard]] value_type operator()(std::size_t i) const
41 {
42 return this->p_layout->value(i);
43 }
44
45 private:
46
47 layout_type* p_layout;
48 };
49
50 // Functor to get the optional-value of the layout at index i.
51 //
52 // This is usefull to create a iterator over the nullable-values of a layout.
53 // This functor will be passed to the functor_index_iterator.
54 template <class LAYOUT_TYPE, class VALUE_TYPE>
56 {
57 public:
58
59 using value_type = VALUE_TYPE;
60 using layout_type = LAYOUT_TYPE;
61
62 constexpr explicit layout_bracket_functor(layout_type* layout_ = nullptr)
63 : p_layout(layout_)
64 {
65 }
66
67 value_type operator()(std::size_t i) const
68 {
69 return this->p_layout->operator[](i);
70 }
71
72 private:
73
74 layout_type* p_layout;
75 };
76
77 template <layout_offset OFFSET_TYPE, std::ranges::range SIZES_RANGE>
78 requires(std::unsigned_integral<std::ranges::range_value_t<SIZES_RANGE>>)
80 {
82
83 OFFSET_TYPE offset = 0;
84 auto it = buffer.begin();
85 for (auto size : sizes)
86 {
87 *it = offset;
88 offset += static_cast<OFFSET_TYPE>(size);
89 ++it;
90 }
91 *it = offset;
92 return buffer;
93 }
94
95} // namespace sparrow
Object that owns a piece of contiguous memory.
Definition buffer.hpp:109
constexpr iterator begin() noexcept
Definition buffer.hpp:621
constexpr layout_bracket_functor(layout_type *layout_=nullptr)
value_type operator()(std::size_t i) const
value_type operator()(std::size_t i) const
constexpr layout_value_functor(layout_type *layout_=nullptr)
sparrow::u8_buffer< OFFSET_TYPE > offset_buffer_from_sizes(SIZES_RANGE &&sizes)
std::size_t range_size(R &&r)
Definition ranges.hpp:31