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