sparrow 0.6.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 <unordered_set>
16
18
19namespace sparrow
20{
22 constexpr bool is_valid_ArrowFlag_value(int64_t value) noexcept
23 {
24 constexpr std::array<ArrowFlag, 3> valid_values = {
28 };
29 return std::ranges::any_of(
30 valid_values,
31 [value](ArrowFlag flag)
32 {
33 return static_cast<std::underlying_type_t<ArrowFlag>>(flag) == value;
34 }
35 );
36 }
37
39 inline std::unordered_set<ArrowFlag> to_set_of_ArrowFlags(int64_t flag_values)
40 {
41 constexpr size_t n_bits = sizeof(flag_values) * 8;
42 std::unordered_set<ArrowFlag> flags;
43 for (size_t i = 0; i < n_bits; ++i)
44 {
45 const int64_t flag_value = static_cast<int64_t>(1) << i;
46 if ((flag_values & flag_value) != 0)
47 {
48 if (!is_valid_ArrowFlag_value(flag_value))
49 {
50 // TODO: Replace with a more specific exception
51 throw std::runtime_error("Invalid ArrowFlag value");
52 }
53 flags.insert(static_cast<ArrowFlag>(flag_value));
54 }
55 }
56 return flags;
57 }
58
60 inline int64_t to_ArrowFlag_value(const std::unordered_set<ArrowFlag>& flags)
61 {
62 int64_t flag_values = 0;
63 for (const ArrowFlag flag : flags)
64 {
65 flag_values |= static_cast<int64_t>(flag);
66 }
67 return flag_values;
68 }
69
70}
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.