sparrow 0.9.0
Loading...
Searching...
No Matches
arrow_flag_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#include <algorithm>
16#include <array>
17#include <unordered_set>
18
20
21namespace sparrow
22{
24 [[nodiscard]] constexpr bool is_valid_ArrowFlag_value(int64_t value) noexcept
25 {
26 constexpr std::array<ArrowFlag, 3> valid_values = {
30 };
31 return std::ranges::any_of(
32 valid_values,
33 [value](ArrowFlag flag)
34 {
35 return static_cast<std::underlying_type_t<ArrowFlag>>(flag) == value;
36 }
37 );
38 }
39
41 inline std::unordered_set<ArrowFlag> to_set_of_ArrowFlags(int64_t flag_values)
42 {
43 constexpr size_t n_bits = sizeof(flag_values) * 8;
44 std::unordered_set<ArrowFlag> flags;
45 for (size_t i = 0; i < n_bits; ++i)
46 {
47 const int64_t flag_value = static_cast<int64_t>(1) << i;
48 if ((flag_values & flag_value) != 0)
49 {
50 if (!is_valid_ArrowFlag_value(flag_value))
51 {
52 // TODO: Replace with a more specific exception
53 throw std::runtime_error("Invalid ArrowFlag value");
54 }
55 flags.insert(static_cast<ArrowFlag>(flag_value));
56 }
57 }
58 return flags;
59 }
60
62 inline int64_t to_ArrowFlag_value(const std::unordered_set<ArrowFlag>& flags)
63 {
64 int64_t flag_values = 0;
65 for (const ArrowFlag flag : flags)
66 {
67 flag_values |= static_cast<int64_t>(flag);
68 }
69 return flag_values;
70 }
71
72}
int64_t to_ArrowFlag_value(const std::unordered_set< ArrowFlag > &flags)
Converts a vector of ArrowFlag values to a bitfield of ArrowFlag values.
constexpr bool is_valid_ArrowFlag_value(int64_t value) noexcept
std::unordered_set< ArrowFlag > to_set_of_ArrowFlags(int64_t flag_values)
Converts a bitfield of ArrowFlag values to a set of ArrowFlag values.