-
-
Notifications
You must be signed in to change notification settings - Fork 902
Expand file tree
/
Copy pathmap_variant.h
More file actions
174 lines (149 loc) · 6.45 KB
/
map_variant.h
File metadata and controls
174 lines (149 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/********************************************************************************
* *
* This file is part of IfcOpenShell. *
* *
* IfcOpenShell is free software: you can redistribute it and/or modify *
* it under the terms of the Lesser GNU General Public License as published by *
* the Free Software Foundation, either version 3.0 of the License, or *
* (at your option) any later version. *
* *
* IfcOpenShell is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Lesser GNU General Public License for more details. *
* *
* You should have received a copy of the Lesser GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
********************************************************************************/
#include <map>
#include <variant>
#include <tuple>
#include <utility>
#include <cstddef>
#include <string>
#include <iostream>
// VariantMap: A map interface that delegates to one of several map types.
// The underlying maps are referenced by pointers (not moved into the variant).
// All map types must share the same key_type, mapped_type, and value_type.
template <typename... Maps>
class VariantMap {
public:
// The variant holds a pointer to the map
using variant_type = std::variant<std::monostate, Maps*...>;
variant_type map_;
// Deduce common types from the first map type.
// @todo these are not common types, but just the 1st
using key_type = typename std::tuple_element<0, std::tuple<Maps...>>::type::key_type;
using mapped_type = typename std::tuple_element<0, std::tuple<Maps...>>::type::mapped_type;
using value_type = typename std::tuple_element<0, std::tuple<Maps...>>::type::value_type;
using underlying_iterator_variant = std::variant<typename Maps::iterator...>;
class iterator {
public:
using value_type = VariantMap::value_type;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type;
using iterator_category = std::forward_iterator_tag;
underlying_iterator_variant it_var;
// mutable cache to support operator-> (so that it->second works)
mutable std::unique_ptr<value_type> cached_value_ptr_;
iterator() = default;
explicit iterator(underlying_iterator_variant v)
: it_var(std::move(v)) {}
iterator(const iterator& other)
: it_var(other.it_var), cached_value_ptr_(nullptr) {}
iterator& operator=(const iterator& other) {
if (this != &other) {
it_var = other.it_var;
cached_value_ptr_.reset(); // clear the cache
}
return *this;
}
value_type operator*() const {
return std::visit([](auto& it) -> value_type { return *it; }, it_var);
}
value_type* operator->() const {
// @todo we need to make a copy here (stored in unique_ptr) because the
// value_type appears to be pair<const K, V> instead of <K, V> or something
// related...
cached_value_ptr_ = std::make_unique<value_type>(**this);
return cached_value_ptr_.get();
}
iterator& operator++() {
std::visit([](auto& it) { ++it; }, it_var);
return *this;
}
iterator operator++(int) {
iterator tmp(*this);
++(*this);
return tmp;
}
bool operator==(const iterator& other) const {
return it_var == other.it_var;
}
bool operator!=(const iterator& other) const {
return !(*this == other);
}
};
VariantMap() {}
template <typename MapT>
VariantMap(MapT* m) : map_(m) {}
iterator begin() const{
return std::visit([](auto m) -> iterator {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, std::monostate>) {
return iterator{};
} else {
return iterator(m->begin());
}
}, map_);
}
iterator end() const {
return std::visit([](auto m) -> iterator {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, std::monostate>) {
return iterator{};
} else {
return iterator(m->end());
}
}, map_);
}
iterator find(const key_type& key) const {
return std::visit([&key](auto m) -> iterator {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, std::monostate>) {
return iterator{};
} else {
return iterator(m->find(key));
}
}, map_);
}
size_t erase(const key_type& key) {
return std::visit([&key](auto m) -> size_t {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, std::monostate>) {
return size_t(0);
} else {
return m->erase(key);
}
}, map_);
}
size_t erase(const iterator& it) {
return std::visit([&it](auto m) -> size_t {
if constexpr (std::is_same_v<std::decay_t<decltype(m)>, std::monostate>) {
return size_t(0);
} else {
// @todo erasing by iterator would be more efficient
return m->erase(it->first);
}
}, map_);
}
std::pair<iterator, bool> insert(const value_type& val) {
return std::visit([this, &val](auto m) -> std::pair<iterator, bool> {
// @todo is monostate still necessary here?
if constexpr (!std::is_same_v<std::decay_t<decltype(m)>, std::monostate>) {
auto result = m->insert(val);
return { iterator(result.first), result.second };
} else {
return { end(), false };
}
}, map_);
}
};