-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathflat_enum_map.h
222 lines (182 loc) · 7.94 KB
/
flat_enum_map.h
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Copyright © 2001-2014, Hove and/or its affiliates. All rights reserved.
This file is part of Navitia,
the software to build cool stuff with public transport.
Hope you'll enjoy and contribute to this project,
powered by Hove (www.hove.com).
Help us simplify mobility and open public transport:
a non ending quest to the responsive locomotion way of traveling!
LICENCE: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Stay tuned using
twitter @navitia
IRC #navitia on freenode
https://groups.google.com/d/forum/navitia
www.navitia.io
*/
#pragma once
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/array.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <array>
#include <type_traits>
namespace navitia {
// template <typename EnumKey> struct enum_size_trait {};
// for the moment the compiler version does not support std::underlying type
#ifdef GCC_NOT_TOO_OLD
template <typename Enum>
struct get_enum_type {
typedef std::underlying_type<Enum>::type type;
};
#else
template <typename Enum>
struct get_enum_type {
using type = int;
};
#endif
/**
* Default specialization, use 'size' field that has to be the last
* Specialize it for the enum that does not have a 'size' element at the end
*/
template <typename Enum>
struct enum_size_trait {
static constexpr typename get_enum_type<Enum>::type size() {
return static_cast<typename get_enum_type<Enum>::type>(Enum::size);
}
};
/**
* Helper, the first elt of the enum should always be 0
*/
template <typename Enum>
Enum get_first_elt() {
return static_cast<Enum>(0);
}
template <typename EnumKey, typename Value>
class flat_enum_map_iterator;
/**
* Simple container associating an enum value to a value.
*
* The mapped enum MUST have its first value initialized to 0
*
* to get the number of elements in the enum we can either define a 'size' last element
* or specialize the enum_size_trait for the enum
*
* the underlying type is a std::array for performance concern
*
* Note:
* Due to a gcc bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57086
* the flat_enum_container has to be an aggregate (else it won't work nor compile if stored in vector)
* so no constructor can be defined thus the array cannot be default initialized
*/
template <typename EnumKey, typename Value>
struct flat_enum_map {
using underlying_container = std::array<Value, enum_size_trait<EnumKey>::size()>;
using iterator = flat_enum_map_iterator<EnumKey, Value>;
using const_iterator = flat_enum_map_iterator<EnumKey, const Value>;
underlying_container array;
// flat_enum_map() = default;
// flat_enum_map(flat_enum_map& v) : array(v.array){}
// flat_enum_map(const flat_enum_map& v) = default;
// flat_enum_map(flat_enum_map&&) = default;
// flat_enum_map& operator=(const flat_enum_map&) = default;
// template<typename ...Val>
// flat_enum_map(Val&& ...v) : array{{std::forward<Val>(v)...}} {}
Value& operator[](EnumKey key) { return array[size_t(key)]; }
constexpr const Value& operator[](EnumKey key) const { return array[size_t(key)]; }
const Value& at(EnumKey key) const {
auto idx = size_t(key);
if (idx >= array.size()) {
throw std::out_of_range("enum not in range");
}
return array[idx];
}
constexpr const Value& operator[](size_t idx) const { return array[idx]; }
template <class Archive>
void serialize(Archive& ar, const unsigned int) {
ar& boost::serialization::make_array(array.data(),
array.size()); // default serialization not available in boost 1.48
}
const_iterator begin() const { return const_iterator(array.begin(), get_first_elt<EnumKey>()); }
const_iterator end() const { return const_iterator(array.end()); }
iterator begin() { return iterator(array.begin(), get_first_elt<EnumKey>()); }
iterator end() { return iterator(array.end()); }
};
template <typename Enum>
class enum_iterator
: public boost::iterator_facade<enum_iterator<Enum>, Enum, boost::random_access_traversal_tag, Enum> {
using underlying_type = typename get_enum_type<Enum>::type;
underlying_type _it;
public:
using difference_type = std::make_signed<typename get_enum_type<Enum>::type>;
enum_iterator() : _it(enum_size_trait<Enum>::size()) {}
enum_iterator(const Enum& e) : _it(static_cast<underlying_type>(e)) {}
enum_iterator(const underlying_type& i) : _it(i) {}
void increment() { ++_it; }
void decrement() { --_it; }
void advance(difference_type n) { _it += n; }
difference_type distance_to(const enum_iterator& other) { return _it - other._it; }
bool equal(const enum_iterator& other) const { return _it == other._it; }
Enum dereference() const { return static_cast<Enum>(_it); }
};
template <typename Enum>
boost::iterator_range<enum_iterator<Enum>> enum_range() {
using it = enum_iterator<Enum>;
return boost::make_iterator_range(it(get_first_elt<Enum>()), it());
}
template <typename Enum>
boost::iterator_range<boost::reverse_iterator<enum_iterator<Enum>>> reverse_enum_range() {
using it = enum_iterator<Enum>;
using rit = boost::reverse_iterator<it>;
return boost::make_iterator_range(rit(it(enum_size_trait<Enum>::size())), rit(it(0)));
}
template <typename Enum>
boost::iterator_range<enum_iterator<Enum>> enum_range_from(Enum e) {
using it = enum_iterator<Enum>;
return boost::make_iterator_range(it(e), it());
}
template <typename Enum>
boost::iterator_range<boost::reverse_iterator<enum_iterator<Enum>>> reverse_enum_range_from(Enum e) {
using it = enum_iterator<Enum>;
using rit = boost::reverse_iterator<it>;
using e_type = typename get_enum_type<Enum>::type;
return boost::make_iterator_range(rit(it(static_cast<Enum>(static_cast<e_type>(e) + 1))), rit(it(0)));
}
template <typename EnumKey, typename Value>
class flat_enum_map_iterator : public boost::iterator_facade<flat_enum_map_iterator<EnumKey, Value>,
std::pair<EnumKey, Value&>,
boost::random_access_traversal_tag,
std::pair<EnumKey, Value&>> {
using enum_map = flat_enum_map<EnumKey, Value>;
using difference_type = typename flat_enum_map_iterator<EnumKey, Value>::difference_type;
typename enum_map::underlying_container::iterator _iterator;
enum_iterator<EnumKey> _enum_iterator;
public:
flat_enum_map_iterator() = default;
flat_enum_map_iterator(typename enum_map::underlying_container::iterator it) : _iterator(it) {}
flat_enum_map_iterator(typename enum_map::underlying_container::iterator it, EnumKey e)
: _iterator(it), _enum_iterator(e) {}
void increment() {
++_iterator;
++_enum_iterator;
}
void decrement() {
--_iterator;
--_enum_iterator;
}
void advance(difference_type n) {
_iterator += n;
_enum_iterator += n;
}
difference_type distance_to(const flat_enum_map_iterator& other) { return this->_iterator - other._iterator; }
bool equal(const flat_enum_map_iterator& other) const { return this->_iterator == other._iterator; }
std::pair<EnumKey, Value&> dereference() const { return {*_enum_iterator, *_iterator}; }
};
} // namespace navitia