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 Value& at(const Key& key) {
59 return map.at(key);
60 }
61
62 const Value& at(const Key& key) const {
63 return map.at(key);
64 }
65
66 // Iterators
67 iterator begin() noexcept {
68 return map.begin();
69 }
70
71 const_iterator begin() const noexcept {
72 return map.begin();
73 }
74
75 const_iterator cbegin() const noexcept {
76 return map.cbegin();
77 }
78
79 iterator end() noexcept {
80 return map.end();
81 }
82
83 const_iterator end() const noexcept {
84 return map.end();
85 }
86
87 const_iterator cend() const noexcept {
88 return map.cend();
89 }
90
91 // Capacity
92 bool empty() const noexcept {
93 return map.empty();
94 }
95
96 size_type size() const noexcept {
97 return map.size();
98 }
99
100 // Modifiers
101 void clear() noexcept {
102 map.clear();
103 }
104
105 std::pair<iterator, bool> insert(const std::pair<Key, Value>& value) {
106 return map.insert(value);
107 }
108
109 std::pair<iterator, bool> emplace(const Key& key, const Value& value) {
110 return map.emplace(key,value);
111 }
112
113 void erase(iterator pos) {
114 map.erase(pos);
115 }
116
117 size_type erase(const Key& key) {
118 return map.erase(key);
119 }
120
121 void swap(vector_map& other) noexcept {
122 map.swap(other.map);
123 }
124
125 // Lookup
126 size_type count(const Key& key) const {
127 return map.count(key);
128 }
129
130 iterator find(const Key& key) {
131 return map.find(key);
132 }
133
134 const_iterator find(const Key& key) const {
135 return map.find(key);
136 }
137};
138
139} // namespace BPMNOS
140
141#endif // BPMNOS_vector_map_H
142
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:130
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:62
bool empty() const noexcept
Definition vector_map.h:92
iterator begin() noexcept
Definition vector_map.h:67
const_iterator begin() const noexcept
Definition vector_map.h:71
iterator end() noexcept
Definition vector_map.h:79
const_iterator end() const noexcept
Definition vector_map.h:83
Value & at(const Key &key)
Definition vector_map.h:58
std::pair< iterator, bool > emplace(const Key &key, const Value &value)
Definition vector_map.h:109
void clear() noexcept
Definition vector_map.h:101
size_type size() const noexcept
Definition vector_map.h:96
const_iterator cend() const noexcept
Definition vector_map.h:87
void swap(vector_map &other) noexcept
Definition vector_map.h:121
size_type erase(const Key &key)
Definition vector_map.h:117
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:134
size_type count(const Key &key) const
Definition vector_map.h:126
const_iterator cbegin() const noexcept
Definition vector_map.h:75
void erase(iterator pos)
Definition vector_map.h:113
std::pair< iterator, bool > insert(const std::pair< Key, Value > &value)
Definition vector_map.h:105
Value & operator[](const Key &key)
Definition vector_map.h:54
std::variant< bool, int, double, std::string > Value
Definition Value.h:10