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