CbmRoot
Loading...
Searching...
No Matches
CbmEnumKeySet.h
Go to the documentation of this file.
1/* Copyright (C) 2025 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#pragma once
11
12#include "CbmTypeTraits.h"
13
14#include <array>
15#include <cstddef>
16#include <limits>
17#include <type_traits>
18#include <utility>
19
20namespace cbm::util
21{
25 template<auto... KeyList>
26 class EnumKeySet {
27 public:
28 static_assert(have_same_type_v<KeyList...>, "EnumKeySet: the non-type values Keys must have the same type");
29
30 using Key_t = std::common_type_t<decltype(KeyList)...>;
31 static_assert(std::is_enum_v<Key_t>, "EnumKeySet: the parameter pack Keys must have an enumerator type");
32
33 using Underlying_t = std::underlying_type_t<Key_t>;
34
39 static constexpr bool CheckKey(Key_t key) { return ((key == KeyList) || ...); }
40
43 template<Key_t Key>
44 static constexpr size_t Index()
45 {
46 static_assert(is_in_sequence_v<Key, KeyList...>,
47 "EnumKeySet::Index<Key>(): passing the Key, which is not defined in the mapping sequence");
48 return Index(Key);
49 }
50
53 static constexpr size_t Index(Key_t key)
54 {
55 return static_cast<size_t>(kMapper[static_cast<Underlying_t>(key) - MinKey()]);
56 }
57
59 static constexpr Underlying_t MaxKey()
60 {
61 Underlying_t res{std::numeric_limits<Underlying_t>::min()};
62 return ((res = std::max(static_cast<Underlying_t>(KeyList), res)), ...);
63 }
64
66 static constexpr Underlying_t MinKey()
67 {
68 Underlying_t res{std::numeric_limits<Underlying_t>::max()};
69 return ((res = std::min(static_cast<Underlying_t>(KeyList), res)), ...);
70 }
71
73 static constexpr size_t NofKeys() { return sizeof...(KeyList); }
74
76 static constexpr size_t MapperSize() { return static_cast<size_t>(MaxKey() - MinKey() + 1); }
77
78 using KeyContainer_t = std::array<Key_t, NofKeys()>;
79
80 private:
81 using Container_t = std::array<size_t, MapperSize()>;
82
84 static constexpr auto InitializedKeysArray()
85 {
86 KeyContainer_t res{};
87 size_t idx{0};
88 ((res[idx++] = KeyList), ...);
89 return res;
90 }
91
93 static constexpr auto InitializedMapper()
94 {
95 Container_t res{};
96 // Initialize the array with -1
97 for (auto& value : res) {
98 value = static_cast<Underlying_t>(-1);
99 }
100 // Apply the map from Keys
101 size_t idx{0};
102 ((res[static_cast<Underlying_t>(KeyList) - MinKey()] = idx++), ...);
103 return res;
104 }
105
106 static constexpr Container_t kMapper = InitializedMapper();
107
108 public:
111 };
112} // namespace cbm::util
Different metaprogramming utilities (type traits) for CBM experiment.
A subset of some enumeration entries and rule for mapping them to a contiguous integral index.
static constexpr size_t Index()
Returns an index of the enumeration key, which is passed as a template parameter.
static constexpr size_t Index(Key_t key)
Returns an index of the enumeration key, which is passed as a parameter.
static constexpr auto InitializedKeysArray()
Returns initialized keys.
std::underlying_type_t< Key_t > Underlying_t
Underlying integral type of the enumerator.
static constexpr bool CheckKey(Key_t key)
Checks, if a key is provided within the Keys pack.
std::array< Key_t, NofKeys()> KeyContainer_t
static constexpr Underlying_t MinKey()
Returns minimal value of the keys.
static constexpr Underlying_t MaxKey()
Returns maximal value of the keys.
static constexpr size_t NofKeys()
Returns number of keys.
std::array< size_t, MapperSize()> Container_t
static constexpr size_t MapperSize()
Returns size of the mapper.
static constexpr auto InitializedMapper()
Returns the initialized mapper.
std::common_type_t< decltype(KeyList)... > Key_t
Type of the keys.
constexpr bool have_same_type_v
Checks, if non-type parameter pack has the same type.
constexpr bool is_in_sequence_v