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