BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
tuple_map.h
Go to the documentation of this file.
1#ifndef BPMNOS_tuple_map_H
2#define BPMNOS_tuple_map_H
3
4#include <cinttypes>
5#include <cstddef>
6#include <functional>
7#include <unordered_map>
8#include <tuple>
9
10namespace BPMNOS {
11
12// General check for vector types
13template<typename T>
14struct is_vector : std::false_type {};
15
16template<typename T, typename Allocator>
17struct is_vector<std::vector<T, Allocator>> : std::true_type {};
18
19
20/**
21 * @brief Wrapper class around `std::unordered_map` for maps with tuple keys.
22 *
23 * Example usage:
24 * ```cpp
25 * BPMNOS::tuple_map< std::tuple< int, std::string >, double > my_map;
26 * my_map[std::make_tuple(1, "example")] = 42.0;
27 * my_map[std::make_tuple(2, "test")] = 84.0;
28 *
29 * for (const auto& [key, value] : my_map) {
30 * std::cout << "(" << std::get<0>(key) << ", " << std::get<1>(key) << ") -> " << value << std::endl;
31 * }
32 * ```
33 **/
34template<typename Key, typename Value>
35class tuple_map {
36private:
37 /// hashing for vectors
38 template<typename T>
39 class vector_hash {
40 public:
41 size_t operator()(const std::vector<T>& vec) const {
42 size_t hash = 0;
43 for (const auto& elem : vec) {
44 hash ^= std::hash<T>()(elem) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
45 }
46 return hash;
47 }
48 };
49
50 /// hashing for tuples
51 class tuple_hash {
52 template<class T>
53 struct component {
54 const T& value;
55 component(const T& value) : value(value) {}
56 uintmax_t operator,(uintmax_t n) const {
57 if constexpr (is_vector<T>::value) {
58 // Use vector_hash for any std::vector<T>
59 n ^= vector_hash<typename T::value_type>{}(value);
60 }
61 else {
62 // Use std::hash for other types
63 n ^= std::hash<T>()(value);
64 }
65 n ^= n << (sizeof(uintmax_t) * 4 - 1);
66 return n ^ std::hash<uintmax_t>()(n);
67 }
68 };
69
70 public:
71 template<class Tuple>
72 size_t operator()(const Tuple& tuple) const {
73 return std::hash<uintmax_t>()( std::apply([](const auto& ... xs) { return (component(xs), ..., 0); }, tuple) );
74 }
75 };
76
77 std::unordered_map<Key, Value, tuple_hash> map;
78public:
79 // Type aliases
80 using iterator = typename std::unordered_map<Key, Value, tuple_hash>::iterator;
81 using const_iterator = typename std::unordered_map<Key, Value, tuple_hash>::const_iterator;
82 using size_type = typename std::unordered_map<Key, Value, tuple_hash>::size_type;
83
84 // Element access
85 Value& operator[](const Key& key) {
86 return map[key];
87 }
88
89 bool contains(const Key& key) const {
90 return map.contains(key);
91 }
92
93 Value& at(const Key& key) {
94 return map.at(key);
95 }
96
97 const Value& at(const Key& key) const {
98 return map.at(key);
99 }
100
101 // Iterators
102 iterator begin() noexcept {
103 return map.begin();
104 }
105
106 const_iterator begin() const noexcept {
107 return map.begin();
108 }
109
110 const_iterator cbegin() const noexcept {
111 return map.cbegin();
112 }
113
114 iterator end() noexcept {
115 return map.end();
116 }
117
118 const_iterator end() const noexcept {
119 return map.end();
120 }
121
122 const_iterator cend() const noexcept {
123 return map.cend();
124 }
125
126 // Capacity
127 bool empty() const noexcept {
128 return map.empty();
129 }
130
131 size_type size() const noexcept {
132 return map.size();
133 }
134
135 // Modifiers
136 void clear() noexcept {
137 map.clear();
138 }
139
140 std::pair<iterator, bool> insert(const std::pair<Key, Value>& value) {
141 return map.insert(value);
142 }
143
144 // Emplace function
145 std::pair<iterator, bool> emplace(const Key& key, const Value& value) {
146 return map.emplace(key,value);
147 }
148
149 std::pair<iterator, bool> try_emplace(const Key& key, const Value& value) {
150 return map.try_emplace(key,value);
151 }
152
153 void erase(iterator pos) {
154 map.erase(pos);
155 }
156
157 size_type erase(const Key& key) {
158 return map.erase(key);
159 }
160
161 void swap(tuple_map& other) noexcept {
162 map.swap(other.map);
163 }
164
165 // Lookup
166 size_type count(const Key& key) const {
167 return map.count(key);
168 }
169
170 iterator find(const Key& key) {
171 return map.find(key);
172 }
173
174 const_iterator find(const Key& key) const {
175 return map.find(key);
176 }
177};
178
179
180} // namespace BPMNOS
181#endif // BPMNOS_tuple_map_H
Wrapper class around std::unordered_map for maps with tuple keys.
Definition tuple_map.h:35
size_type count(const Key &key) const
Definition tuple_map.h:166
const_iterator cbegin() const noexcept
Definition tuple_map.h:110
bool empty() const noexcept
Definition tuple_map.h:127
const_iterator cend() const noexcept
Definition tuple_map.h:122
typename std::unordered_map< Key, Value, tuple_hash >::size_type size_type
Definition tuple_map.h:82
typename std::unordered_map< Key, Value, tuple_hash >::const_iterator const_iterator
Definition tuple_map.h:81
void erase(iterator pos)
Definition tuple_map.h:153
typename std::unordered_map< Key, Value, tuple_hash >::iterator iterator
Definition tuple_map.h:80
iterator begin() noexcept
Definition tuple_map.h:102
Value & at(const Key &key)
Definition tuple_map.h:93
Value & operator[](const Key &key)
Definition tuple_map.h:85
iterator end() noexcept
Definition tuple_map.h:114
std::pair< iterator, bool > try_emplace(const Key &key, const Value &value)
Definition tuple_map.h:149
void clear() noexcept
Definition tuple_map.h:136
const Value & at(const Key &key) const
Definition tuple_map.h:97
size_type size() const noexcept
Definition tuple_map.h:131
const_iterator begin() const noexcept
Definition tuple_map.h:106
bool contains(const Key &key) const
Definition tuple_map.h:89
void swap(tuple_map &other) noexcept
Definition tuple_map.h:161
const_iterator end() const noexcept
Definition tuple_map.h:118
size_type erase(const Key &key)
Definition tuple_map.h:157
std::pair< iterator, bool > insert(const std::pair< Key, Value > &value)
Definition tuple_map.h:140
iterator find(const Key &key)
Definition tuple_map.h:170
const_iterator find(const Key &key) const
Definition tuple_map.h:174
std::pair< iterator, bool > emplace(const Key &key, const Value &value)
Definition tuple_map.h:145
std::variant< bool, int, double, std::string > Value
Definition Value.h:10
STL namespace.