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/**
13 * @brief Wrapper class around `std::unordered_map` for maps with tuple keys.
14 *
15 * Example usage:
16 * ```cpp
17 * BPMNOS::tuple_map< std::tuple< int, std::string >, double > my_map;
18 * my_map[std::make_tuple(1, "example")] = 42.0;
19 * my_map[std::make_tuple(2, "test")] = 84.0;
20 *
21 * for (const auto& [key, value] : my_map) {
22 * std::cout << "(" << std::get<0>(key) << ", " << std::get<1>(key) << ") -> " << value << std::endl;
23 * }
24 * ```
25 **/
26template<typename Key, typename Value>
27class tuple_map {
28private:
29 /// hashing for tuples
30 class tuple_hash {
31 template<class T>
32 struct component {
33 const T& value;
34 component(const T& value) : value(value) {}
35 uintmax_t operator,(uintmax_t n) const {
36 n ^= std::hash<T>()(value);
37 n ^= n << (sizeof(uintmax_t) * 4 - 1);
38 return n ^ std::hash<uintmax_t>()(n);
39 }
40 };
41
42 public:
43 template<class Tuple>
44 size_t operator()(const Tuple& tuple) const {
45 return std::hash<uintmax_t>()( std::apply([](const auto& ... xs) { return (component(xs), ..., 0); }, tuple) );
46 }
47 };
48
49 std::unordered_map<Key, Value, tuple_hash> map;
50public:
51 // Type aliases
52 using iterator = typename std::unordered_map<Key, Value, tuple_hash>::iterator;
53 using const_iterator = typename std::unordered_map<Key, Value, tuple_hash>::const_iterator;
54 using size_type = typename std::unordered_map<Key, Value, tuple_hash>::size_type;
55
56 // Element access
57 Value& operator[](const Key& key) {
58 return map[key];
59 }
60
61 Value& at(const Key& key) {
62 return map.at(key);
63 }
64
65 const Value& at(const Key& key) const {
66 return map.at(key);
67 }
68
69 // Iterators
70 iterator begin() noexcept {
71 return map.begin();
72 }
73
74 const_iterator begin() const noexcept {
75 return map.begin();
76 }
77
78 const_iterator cbegin() const noexcept {
79 return map.cbegin();
80 }
81
82 iterator end() noexcept {
83 return map.end();
84 }
85
86 const_iterator end() const noexcept {
87 return map.end();
88 }
89
90 const_iterator cend() const noexcept {
91 return map.cend();
92 }
93
94 // Capacity
95 bool empty() const noexcept {
96 return map.empty();
97 }
98
99 size_type size() const noexcept {
100 return map.size();
101 }
102
103 // Modifiers
104 void clear() noexcept {
105 map.clear();
106 }
107
108 std::pair<iterator, bool> insert(const std::pair<Key, Value>& value) {
109 return map.insert(value);
110 }
111
112 void erase(iterator pos) {
113 map.erase(pos);
114 }
115
116 size_type erase(const Key& key) {
117 return map.erase(key);
118 }
119
120 void swap(tuple_map& other) noexcept {
121 map.swap(other.map);
122 }
123
124 // Lookup
125 size_type count(const Key& key) const {
126 return map.count(key);
127 }
128
129 iterator find(const Key& key) {
130 return map.find(key);
131 }
132
133 const_iterator find(const Key& key) const {
134 return map.find(key);
135 }
136};
137
138
139} // namespace BPMNOS
140#endif // BPMNOS_tuple_map_H
Wrapper class around std::unordered_map for maps with tuple keys.
Definition tuple_map.h:27
size_type count(const Key &key) const
Definition tuple_map.h:125
const_iterator cbegin() const noexcept
Definition tuple_map.h:78
bool empty() const noexcept
Definition tuple_map.h:95
const_iterator cend() const noexcept
Definition tuple_map.h:90
typename std::unordered_map< Key, Value, tuple_hash >::size_type size_type
Definition tuple_map.h:54
typename std::unordered_map< Key, Value, tuple_hash >::const_iterator const_iterator
Definition tuple_map.h:53
void erase(iterator pos)
Definition tuple_map.h:112
typename std::unordered_map< Key, Value, tuple_hash >::iterator iterator
Definition tuple_map.h:52
iterator begin() noexcept
Definition tuple_map.h:70
Value & at(const Key &key)
Definition tuple_map.h:61
Value & operator[](const Key &key)
Definition tuple_map.h:57
iterator end() noexcept
Definition tuple_map.h:82
void clear() noexcept
Definition tuple_map.h:104
const Value & at(const Key &key) const
Definition tuple_map.h:65
size_type size() const noexcept
Definition tuple_map.h:99
const_iterator begin() const noexcept
Definition tuple_map.h:74
void swap(tuple_map &other) noexcept
Definition tuple_map.h:120
const_iterator end() const noexcept
Definition tuple_map.h:86
size_type erase(const Key &key)
Definition tuple_map.h:116
std::pair< iterator, bool > insert(const std::pair< Key, Value > &value)
Definition tuple_map.h:108
iterator find(const Key &key)
Definition tuple_map.h:129
const_iterator find(const Key &key) const
Definition tuple_map.h:133
std::variant< bool, int, double, std::string > Value
Definition Value.h:10