LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/*
2 * Frozen
3 * Copyright 2016 QuarksLab
4 *
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22
23#ifndef FROZEN_LETITGO_MAP_H
24#define FROZEN_LETITGO_MAP_H
25
29#include "frozen/bits/mpl.h"
30#include "frozen/bits/version.h"
31
32#include <iterator>
33#include <utility>
34
35namespace frozen {
36
37namespace impl {
38
39template <class Comparator> class CompareKey : private Comparator {
40public:
41 constexpr Comparator const& key_comp() const noexcept {
42 return static_cast<Comparator const&>(*this);
43 }
44
45 constexpr CompareKey(Comparator const &comparator)
46 : Comparator(comparator) {}
47
48 template <class Key1, class Key2, class Value>
49 constexpr auto operator()(std::pair<Key1, Value> const &self,
50 std::pair<Key2, Value> const &other) const -> decltype(key_comp()(std::get<0>(self), std::get<0>(other))) {
51 return key_comp()(std::get<0>(self), std::get<0>(other));
52 }
53
54 template <class Key1, class Key2, class Value>
55 constexpr auto operator()(Key1 const &self_key,
56 std::pair<Key2, Value> const &other) const -> decltype(key_comp()(self_key, std::get<0>(other))) {
57 return key_comp()(self_key, std::get<0>(other));
58 }
59
60 template <class Key1, class Key2, class Value>
61 constexpr auto operator()(std::pair<Key1, Value> const &self,
62 Key2 const &other_key) const -> decltype(key_comp()(std::get<0>(self), other_key)) {
63 return key_comp()(std::get<0>(self), other_key);
64 }
65
66 template <class Key1, class Key2>
67 constexpr auto operator()(Key1 const &self_key, Key2 const &other_key) const -> decltype(key_comp()(self_key, other_key)) {
68 return key_comp()(self_key, other_key);
69 }
70};
71
72} // namespace impl
73
74template <class Key, class Value, std::size_t N, class Compare = std::less<Key>>
75class map : private impl::CompareKey<Compare> {
76 using container_type = bits::carray<std::pair<const Key, Value>, N>;
77 container_type items_;
78
79public:
80 using key_type = Key;
81 using mapped_type = Value;
85 using key_compare = Compare;
93 using reverse_iterator = std::reverse_iterator<iterator>;
94 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
95
96public:
97 /* constructors */
98 constexpr map(container_type items, Compare const &compare)
99 : impl::CompareKey<Compare>{compare}
101
102 explicit constexpr map(container_type items)
103 : map{items, Compare{}} {}
104
105 constexpr map(std::initializer_list<value_type> items, Compare const &compare)
106 : map{container_type {items}, compare} {
107 }
108
109 constexpr map(std::initializer_list<value_type> items)
110 : map{items, Compare{}} {}
111
112 /* element access */
113 constexpr Value const& at(Key const &key) const {
114 return at_impl(*this, key);
115 }
116 constexpr Value& at(Key const &key) {
117 return at_impl(*this, key);
118 }
119
120 /* iterators */
121 constexpr iterator begin() { return items_.begin(); }
122 constexpr const_iterator begin() const { return items_.begin(); }
123 constexpr const_iterator cbegin() const { return items_.begin(); }
124 constexpr iterator end() { return items_.end(); }
125 constexpr const_iterator end() const { return items_.end(); }
126 constexpr const_iterator cend() const { return items_.end(); }
127
128 constexpr reverse_iterator rbegin() { return reverse_iterator{items_.end()}; }
129 constexpr const_reverse_iterator rbegin() const { return const_reverse_iterator{items_.end()}; }
130 constexpr const_reverse_iterator crbegin() const { return const_reverse_iterator{items_.end()}; }
131 constexpr reverse_iterator rend() { return reverse_iterator{items_.begin()}; }
132 constexpr const_reverse_iterator rend() const { return const_reverse_iterator{items_.begin()}; }
133 constexpr const_reverse_iterator crend() const { return const_reverse_iterator{items_.begin()}; }
134
135 /* capacity */
136 constexpr bool empty() const { return !N; }
137 constexpr size_type size() const { return N; }
138 constexpr size_type max_size() const { return N; }
139
140 /* lookup */
141
142 template <class KeyType>
143 constexpr std::size_t count(KeyType const &key) const {
144 return bits::binary_search<N>(items_.begin(), key, value_comp());
145 }
146
147 template <class KeyType>
148 constexpr const_iterator find(KeyType const &key) const {
149 return map::find_impl(*this, key);
150 }
151 template <class KeyType>
152 constexpr iterator find(KeyType const &key) {
153 return map::find_impl(*this, key);
154 }
155
156 template <class KeyType>
157 constexpr bool contains(KeyType const &key) const {
158 return this->find(key) != this->end();
159 }
160
161 template <class KeyType>
162 constexpr std::pair<const_iterator, const_iterator>
163 equal_range(KeyType const &key) const {
164 return equal_range_impl(*this, key);
165 }
166 template <class KeyType>
167 constexpr std::pair<iterator, iterator> equal_range(KeyType const &key) {
168 return equal_range_impl(*this, key);
169 }
170
171 template <class KeyType>
172 constexpr const_iterator lower_bound(KeyType const &key) const {
173 return lower_bound_impl(*this, key);
174 }
175 template <class KeyType>
176 constexpr iterator lower_bound(KeyType const &key) {
177 return lower_bound_impl(*this, key);
178 }
179
180 template <class KeyType>
181 constexpr const_iterator upper_bound(KeyType const &key) const {
182 return upper_bound_impl(*this, key);
183 }
184 template <class KeyType>
185 constexpr iterator upper_bound(KeyType const &key) {
186 return upper_bound_impl(*this, key);
187 }
188
189 /* observers */
190 constexpr const key_compare& key_comp() const { return value_comp().key_comp(); }
191 constexpr const value_compare& value_comp() const { return static_cast<impl::CompareKey<Compare> const&>(*this); }
192
193 private:
194 template <class This, class KeyType>
195 static inline constexpr auto& at_impl(This&& self, KeyType const &key) {
196 auto where = self.find(key);
197 if (where != self.end())
198 return where->second;
199 else
200 FROZEN_THROW_OR_ABORT(std::out_of_range("unknown key"));
201 }
202
203 template <class This, class KeyType>
204 static inline constexpr auto find_impl(This&& self, KeyType const &key) {
205 auto where = self.lower_bound(key);
206 if (where != self.end() && !self.value_comp()(key, *where))
207 return where;
208 else
209 return self.end();
210 }
211
212 template <class This, class KeyType>
213 static inline constexpr auto equal_range_impl(This&& self, KeyType const &key) {
214 auto lower = self.lower_bound(key);
215 using lower_t = decltype(lower);
216 if (lower != self.end() && !self.value_comp()(key, *lower))
217 return std::pair<lower_t, lower_t>{lower, lower + 1};
218 else
219 return std::pair<lower_t, lower_t>{lower, lower};
220 }
221
222 template <class This, class KeyType>
223 static inline constexpr auto lower_bound_impl(This&& self, KeyType const &key) -> decltype(self.end()) {
224 return bits::lower_bound<N>(self.items_.begin(), key, self.value_comp());
225 }
226
227 template <class This, class KeyType>
228 static inline constexpr auto upper_bound_impl(This&& self, KeyType const &key) {
229 auto lower = self.lower_bound(key);
230 if (lower != self.end() && !self.value_comp()(key, *lower))
231 return lower + 1;
232 else
233 return lower;
234 }
235};
236
237template <class Key, class Value, class Compare>
238class map<Key, Value, 0, Compare> : private impl::CompareKey<Compare> {
239 using container_type = bits::carray<std::pair<Key, Value>, 0>;
240
241public:
242 using key_type = Key;
243 using mapped_type = Value;
247 using key_compare = Compare;
257
258public:
259 /* constructors */
260 constexpr map(const map &other) = default;
261 constexpr map(std::initializer_list<value_type>, Compare const &compare)
262 : impl::CompareKey<Compare>{compare} {}
263 constexpr map(std::initializer_list<value_type> items)
264 : map{items, Compare{}} {}
265
266 /* element access */
267 template <class KeyType>
268 constexpr mapped_type at(KeyType const &) const {
269 FROZEN_THROW_OR_ABORT(std::out_of_range("invalid key"));
270 }
271 template <class KeyType>
272 constexpr mapped_type at(KeyType const &) {
273 FROZEN_THROW_OR_ABORT(std::out_of_range("invalid key"));
274 }
275
276 /* iterators */
277 constexpr iterator begin() { return nullptr; }
278 constexpr const_iterator begin() const { return nullptr; }
279 constexpr const_iterator cbegin() const { return nullptr; }
280 constexpr iterator end() { return nullptr; }
281 constexpr const_iterator end() const { return nullptr; }
282 constexpr const_iterator cend() const { return nullptr; }
283
284 constexpr reverse_iterator rbegin() { return nullptr; }
285 constexpr const_reverse_iterator rbegin() const { return nullptr; }
286 constexpr const_reverse_iterator crbegin() const { return nullptr; }
287 constexpr reverse_iterator rend() { return nullptr; }
288 constexpr const_reverse_iterator rend() const { return nullptr; }
289 constexpr const_reverse_iterator crend() const { return nullptr; }
290
291 /* capacity */
292 constexpr bool empty() const { return true; }
293 constexpr size_type size() const { return 0; }
294 constexpr size_type max_size() const { return 0; }
295
296 /* lookup */
297
298 template <class KeyType>
299 constexpr std::size_t count(KeyType const &) const { return 0; }
300
301 template <class KeyType>
302 constexpr const_iterator find(KeyType const &) const { return end(); }
303 template <class KeyType>
304 constexpr iterator find(KeyType const &) { return end(); }
305
306 template <class KeyType>
307 constexpr std::pair<const_iterator, const_iterator>
308 equal_range(KeyType const &) const { return {end(), end()}; }
309 template <class KeyType>
310 constexpr std::pair<iterator, iterator>
311 equal_range(KeyType const &) { return {end(), end()}; }
312
313 template <class KeyType>
314 constexpr const_iterator lower_bound(KeyType const &) const { return end(); }
315 template <class KeyType>
316 constexpr iterator lower_bound(KeyType const &) { return end(); }
317
318 template <class KeyType>
319 constexpr const_iterator upper_bound(KeyType const &) const { return end(); }
320 template <class KeyType>
321 constexpr iterator upper_bound(KeyType const &) { return end(); }
322
323/* observers */
324 constexpr key_compare const& key_comp() const { return value_comp().key_comp(); }
325 constexpr value_compare const& value_comp() const { return static_cast<impl::CompareKey<Compare> const&>(*this); }
326};
327
328template <typename T, typename U, typename Compare = std::less<T>>
329constexpr auto make_map(bits::ignored_arg = {}/* for consistency with the initializer below for N = 0*/) {
330 return map<T, U, 0, Compare>{};
331}
332
333template <typename T, typename U, std::size_t N>
334constexpr auto make_map(std::pair<T, U> const (&items)[N]) {
335 return map<T, U, N>{items};
336}
337
338template <typename T, typename U, std::size_t N>
339constexpr auto make_map(std::array<std::pair<T, U>, N> const &items) {
340 return map<T, U, N>{items};
341}
342
343template <typename T, typename U, typename Compare, std::size_t N>
344constexpr auto make_map(std::pair<T, U> const (&items)[N], Compare const& compare = Compare{}) {
345 return map<T, U, N, Compare>{items, compare};
346}
347
348template <typename T, typename U, typename Compare, std::size_t N>
349constexpr auto make_map(std::array<std::pair<T, U>, N> const &items, Compare const& compare = Compare{}) {
350 return map<T, U, N, Compare>{items, compare};
351}
352
353} // namespace frozen
354
355#endif
Definition basic_types.h:90
const value_type & const_reference
Definition basic_types.h:109
value_type * pointer
Definition basic_types.h:110
std::ptrdiff_t difference_type
Definition basic_types.h:115
const_pointer const_iterator
Definition basic_types.h:113
pointer iterator
Definition basic_types.h:112
const value_type * const_pointer
Definition basic_types.h:111
std::size_t size_type
Definition basic_types.h:114
std::pair< const Key, Value > value_type
Definition basic_types.h:107
value_type & reference
Definition basic_types.h:108
Definition map.h:39
constexpr CompareKey(Comparator const &comparator)
Definition map.h:45
constexpr auto operator()(std::pair< Key1, Value > const &self, Key2 const &other_key) const -> decltype(key_comp()(std::get< 0 >(self), other_key))
Definition map.h:61
constexpr auto operator()(std::pair< Key1, Value > const &self, std::pair< Key2, Value > const &other) const -> decltype(key_comp()(std::get< 0 >(self), std::get< 0 >(other)))
Definition map.h:49
constexpr Comparator const & key_comp() const noexcept
Definition map.h:41
constexpr auto operator()(Key1 const &self_key, std::pair< Key2, Value > const &other) const -> decltype(key_comp()(self_key, std::get< 0 >(other)))
Definition map.h:55
constexpr auto operator()(Key1 const &self_key, Key2 const &other_key) const -> decltype(key_comp()(self_key, other_key))
Definition map.h:67
constexpr map(std::initializer_list< value_type >, Compare const &compare)
Definition map.h:261
constexpr const_iterator cend() const
Definition map.h:282
constexpr value_compare const & value_comp() const
Definition map.h:325
constexpr mapped_type at(KeyType const &)
Definition map.h:272
constexpr map(std::initializer_list< value_type > items)
Definition map.h:263
constexpr std::size_t count(KeyType const &) const
Definition map.h:299
Value mapped_type
Definition map.h:243
constexpr const_reverse_iterator crend() const
Definition map.h:289
constexpr reverse_iterator rbegin()
Definition map.h:284
constexpr const_reverse_iterator crbegin() const
Definition map.h:286
constexpr iterator find(KeyType const &)
Definition map.h:304
constexpr std::pair< iterator, iterator > equal_range(KeyType const &)
Definition map.h:311
constexpr map(const map &other)=default
constexpr const_iterator begin() const
Definition map.h:278
constexpr iterator end()
Definition map.h:280
constexpr iterator lower_bound(KeyType const &)
Definition map.h:316
constexpr const_iterator find(KeyType const &) const
Definition map.h:302
typename container_type::size_type size_type
Definition map.h:245
pointer iterator
Definition map.h:253
typename container_type::const_pointer const_pointer
Definition map.h:252
constexpr bool empty() const
Definition map.h:292
const_pointer const_iterator
Definition map.h:254
Key key_type
Definition map.h:242
constexpr iterator upper_bound(KeyType const &)
Definition map.h:321
constexpr reverse_iterator rend()
Definition map.h:287
constexpr std::pair< const_iterator, const_iterator > equal_range(KeyType const &) const
Definition map.h:308
typename container_type::difference_type difference_type
Definition map.h:246
constexpr const_reverse_iterator rbegin() const
Definition map.h:285
constexpr key_compare const & key_comp() const
Definition map.h:324
constexpr const_iterator end() const
Definition map.h:281
const_pointer const_reverse_iterator
Definition map.h:256
constexpr mapped_type at(KeyType const &) const
Definition map.h:268
typename container_type::value_type value_type
Definition map.h:244
constexpr size_type size() const
Definition map.h:293
typename container_type::reference reference
Definition map.h:249
constexpr const_iterator cbegin() const
Definition map.h:279
Compare key_compare
Definition map.h:247
constexpr const_iterator upper_bound(KeyType const &) const
Definition map.h:319
constexpr const_reverse_iterator rend() const
Definition map.h:288
impl::CompareKey< Compare > value_compare
Definition map.h:248
constexpr iterator begin()
Definition map.h:277
typename container_type::pointer pointer
Definition map.h:251
constexpr size_type max_size() const
Definition map.h:294
constexpr const_iterator lower_bound(KeyType const &) const
Definition map.h:314
pointer reverse_iterator
Definition map.h:255
typename container_type::const_reference const_reference
Definition map.h:250
Definition map.h:75
constexpr iterator end()
Definition map.h:124
constexpr size_type max_size() const
Definition map.h:138
constexpr const_iterator find(KeyType const &key) const
Definition map.h:148
constexpr reverse_iterator rend()
Definition map.h:131
constexpr const_reverse_iterator crbegin() const
Definition map.h:130
constexpr iterator find(KeyType const &key)
Definition map.h:152
constexpr const_iterator lower_bound(KeyType const &key) const
Definition map.h:172
constexpr const_iterator begin() const
Definition map.h:122
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition map.h:94
constexpr map(container_type items)
Definition map.h:102
constexpr const_iterator upper_bound(KeyType const &key) const
Definition map.h:181
typename container_type::const_pointer const_pointer
Definition map.h:90
typename container_type::size_type size_type
Definition map.h:83
constexpr const key_compare & key_comp() const
Definition map.h:190
constexpr const_iterator cbegin() const
Definition map.h:123
constexpr const_reverse_iterator rbegin() const
Definition map.h:129
constexpr map(std::initializer_list< value_type > items)
Definition map.h:109
constexpr iterator upper_bound(KeyType const &key)
Definition map.h:185
constexpr const_iterator end() const
Definition map.h:125
constexpr const_iterator cend() const
Definition map.h:126
constexpr const_reverse_iterator crend() const
Definition map.h:133
constexpr bool empty() const
Definition map.h:136
Key key_type
Definition map.h:80
typename container_type::const_reference const_reference
Definition map.h:88
constexpr Value & at(Key const &key)
Definition map.h:116
constexpr map(container_type items, Compare const &compare)
Definition map.h:98
typename container_type::const_iterator const_iterator
Definition map.h:92
Compare key_compare
Definition map.h:85
constexpr map(std::initializer_list< value_type > items, Compare const &compare)
Definition map.h:105
constexpr size_type size() const
Definition map.h:137
typename container_type::difference_type difference_type
Definition map.h:84
constexpr iterator begin()
Definition map.h:121
constexpr iterator lower_bound(KeyType const &key)
Definition map.h:176
typename container_type::pointer pointer
Definition map.h:89
Value mapped_type
Definition map.h:81
typename container_type::reference reference
Definition map.h:87
constexpr reverse_iterator rbegin()
Definition map.h:128
constexpr std::pair< const_iterator, const_iterator > equal_range(KeyType const &key) const
Definition map.h:163
constexpr bool contains(KeyType const &key) const
Definition map.h:157
constexpr std::size_t count(KeyType const &key) const
Definition map.h:143
typename container_type::iterator iterator
Definition map.h:91
typename container_type::value_type value_type
Definition map.h:82
std::reverse_iterator< iterator > reverse_iterator
Definition map.h:93
constexpr std::pair< iterator, iterator > equal_range(KeyType const &key)
Definition map.h:167
constexpr const value_compare & value_comp() const
Definition map.h:191
impl::CompareKey< Compare > value_compare
Definition map.h:86
constexpr const_reverse_iterator rend() const
Definition map.h:132
constexpr Value const & at(Key const &key) const
Definition map.h:113
#define FROZEN_THROW_OR_ABORT(_)
Definition exceptions.h:29
constexpr ForwardIt lower_bound(ForwardIt first, const T &value, Compare const &compare)
Definition algorithms.h:197
typename remove_cv< T >::type remove_cv_t
Definition mpl.h:50
constexpr void quicksort(Iterator left, Iterator right, Compare const &compare)
Definition algorithms.h:134
constexpr bool binary_search(ForwardIt first, const T &value, Compare const &compare)
Definition algorithms.h:202
Definition map.h:37
Definition algorithm.h:30
constexpr auto make_map(bits::ignored_arg={})
Definition map.h:329
Definition basic_types.h:39