sparrow 0.3.0
Loading...
Searching...
No Matches
offsets.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#include <numeric>
16#include <ranges>
17
20
21#pragma once
22
23namespace sparrow
24{
25 template <layout_offset OT, std::ranges::sized_range R>
26 requires std::ranges::sized_range<std::ranges::range_value_t<R>>
27 [[nodiscard]] buffer<OT> make_offset_buffer(const R& range)
28 {
29 const size_t range_size = std::ranges::size(range);
30 buffer<OT> offsets(range_size + 1, 0);
31 std::transform(
32 range.cbegin(),
33 range.cend(),
34 offsets.begin() + 1,
35 [](const auto& elem)
36 {
37 return static_cast<OT>(elem.size());
38 }
39 );
40 std::partial_sum(offsets.begin(), offsets.end(), offsets.begin());
41 return offsets;
42 }
43}
Object that owns a piece of contiguous memory.
Definition buffer.hpp:109
constexpr iterator begin() noexcept
Definition buffer.hpp:621
constexpr iterator end() noexcept
Definition buffer.hpp:627
buffer< OT > make_offset_buffer(const R &range)
Definition offsets.hpp:27
std::size_t range_size(R &&r)
Definition ranges.hpp:31