LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
set.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_SET_H
24#define FROZEN_SET_H
25
28#include "frozen/bits/version.h"
29#include "frozen/bits/defines.h"
30
31#include <iterator>
32#include <utility>
33
34namespace frozen {
35
36template <class Key, std::size_t N, class Compare = std::less<Key>> class set : private Compare {
37 using container_type = bits::carray<Key, N>;
38 container_type keys_;
39
40public:
41 /* container typedefs*/
42 using key_type = Key;
43 using value_type = Key;
46 using key_compare = Compare;
47 using value_compare = Compare;
53 using reverse_iterator = std::reverse_iterator<iterator>;
55 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
56
57public:
58 /* constructors */
59 constexpr set(const set &other) = default;
60
61 constexpr set(container_type keys, Compare const & comp)
62 : Compare{comp}
63 , keys_(bits::quicksort(keys, value_comp())) {
64 }
65
66 explicit constexpr set(container_type keys)
67 : set{keys, Compare{}} {}
68
69 constexpr set(std::initializer_list<Key> keys, Compare const & comp)
70 : set{container_type{keys}, comp} {
71 }
72
73 constexpr set(std::initializer_list<Key> keys)
74 : set{keys, Compare{}} {}
75
76 constexpr set& operator=(const set &other) = default;
77
78 /* capacity */
79 constexpr bool empty() const { return !N; }
80 constexpr size_type size() const { return N; }
81 constexpr size_type max_size() const { return N; }
82
83 /* lookup */
84 template <class KeyType>
85 constexpr std::size_t count(KeyType const &key) const {
86 return bits::binary_search<N>(keys_.begin(), key, value_comp());
87 }
88
89 template <class KeyType>
90 constexpr const_iterator find(KeyType const &key) const {
91 const_iterator where = lower_bound(key);
92 if ((where != end()) && !value_comp()(key, *where))
93 return where;
94 else
95 return end();
96 }
97
98 template <class KeyType>
99 constexpr bool contains(KeyType const &key) const {
100 return this->find(key) != keys_.end();
101 }
102
103 template <class KeyType>
104 constexpr std::pair<const_iterator, const_iterator> equal_range(KeyType const &key) const {
105 auto const lower = lower_bound(key);
106 if (lower == end())
107 return {lower, lower};
108 else
109 return {lower, lower + 1};
110 }
111
112 template <class KeyType>
113 constexpr const_iterator lower_bound(KeyType const &key) const {
114 auto const where = bits::lower_bound<N>(keys_.begin(), key, value_comp());
115 if ((where != end()) && !value_comp()(key, *where))
116 return where;
117 else
118 return end();
119 }
120
121 template <class KeyType>
122 constexpr const_iterator upper_bound(KeyType const &key) const {
123 auto const where = bits::lower_bound<N>(keys_.begin(), key, value_comp());
124 if ((where != end()) && !value_comp()(key, *where))
125 return where + 1;
126 else
127 return end();
128 }
129
130 /* observers */
131 constexpr const key_compare& key_comp() const { return value_comp(); }
132 constexpr const key_compare& value_comp() const { return static_cast<const Compare&>(*this); }
133
134 /* iterators */
135 constexpr const_iterator begin() const { return keys_.begin(); }
136 constexpr const_iterator cbegin() const { return keys_.begin(); }
137 constexpr const_iterator end() const { return keys_.end(); }
138 constexpr const_iterator cend() const { return keys_.end(); }
139
140 constexpr const_reverse_iterator rbegin() const { return const_reverse_iterator{keys_.end()}; }
141 constexpr const_reverse_iterator crbegin() const { return const_reverse_iterator{keys_.end()}; }
142 constexpr const_reverse_iterator rend() const { return const_reverse_iterator{keys_.begin()}; }
143 constexpr const_reverse_iterator crend() const { return const_reverse_iterator{keys_.begin()}; }
144
145 /* comparison */
146 constexpr bool operator==(set const& rhs) const { return bits::equal(begin(), end(), rhs.begin()); }
147 constexpr bool operator!=(set const& rhs) const { return !(*this == rhs); }
148 constexpr bool operator<(set const& rhs) const { return bits::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end()); }
149 constexpr bool operator<=(set const& rhs) const { return (*this < rhs) || (*this == rhs); }
150 constexpr bool operator>(set const& rhs) const { return bits::lexicographical_compare(rhs.begin(), rhs.end(), begin(), end()); }
151 constexpr bool operator>=(set const& rhs) const { return (*this > rhs) || (*this == rhs); }
152};
153
154template <class Key, class Compare> class set<Key, 0, Compare> : private Compare {
155 using container_type = bits::carray<Key, 0>; // just for the type definitions
156
157public:
158 /* container typedefs*/
159 using key_type = Key;
160 using value_type = Key;
163 using key_compare = Compare;
164 using value_compare = Compare;
173
174public:
175 /* constructors */
176 constexpr set(const set &other) = default;
177 constexpr set(bits::carray<Key, 0>, Compare const &) {}
178 explicit constexpr set(bits::carray<Key, 0>) {}
179
180 constexpr set(std::initializer_list<Key>, Compare const &comp)
181 : Compare{comp} {}
182 constexpr set(std::initializer_list<Key> keys) : set{keys, Compare{}} {}
183
184 constexpr set& operator=(const set &other) = default;
185
186 /* capacity */
187 constexpr bool empty() const { return true; }
188 constexpr size_type size() const { return 0; }
189 constexpr size_type max_size() const { return 0; }
190
191 /* lookup */
192 template <class KeyType>
193 constexpr std::size_t count(KeyType const &) const { return 0; }
194
195 template <class KeyType>
196 constexpr const_iterator find(KeyType const &) const { return end(); }
197
198 template <class KeyType>
199 constexpr std::pair<const_iterator, const_iterator>
200 equal_range(KeyType const &) const { return {end(), end()}; }
201
202 template <class KeyType>
203 constexpr const_iterator lower_bound(KeyType const &) const { return end(); }
204
205 template <class KeyType>
206 constexpr const_iterator upper_bound(KeyType const &) const { return end(); }
207
208 /* observers */
209 constexpr const key_compare& key_comp() const { return value_comp(); }
210 constexpr const key_compare& value_comp() const { return static_cast<Compare const&>(*this); }
211
212 /* iterators */
213 constexpr const_iterator begin() const { return nullptr; }
214 constexpr const_iterator cbegin() const { return nullptr; }
215 constexpr const_iterator end() const { return nullptr; }
216 constexpr const_iterator cend() const { return nullptr; }
217
218 constexpr const_reverse_iterator rbegin() const { return nullptr; }
219 constexpr const_reverse_iterator crbegin() const { return nullptr; }
220 constexpr const_reverse_iterator rend() const { return nullptr; }
221 constexpr const_reverse_iterator crend() const { return nullptr; }
222};
223
224template <typename T>
225constexpr auto make_set(bits::ignored_arg = {}/* for consistency with the initializer below for N = 0*/) {
226 return set<T, 0>{};
227}
228
229template <typename T, std::size_t N>
230constexpr auto make_set(const T (&args)[N]) {
231 return set<T, N>(args);
232}
233
234template <typename T, std::size_t N>
235constexpr auto make_set(std::array<T, N> const &args) {
236 return set<T, N>(args);
237}
238
239template <typename T, typename Compare, std::size_t N>
240constexpr auto make_set(const T (&args)[N], Compare const& compare = Compare{}) {
241 return set<T, N, Compare>(args, compare);
242}
243
244template <typename T, typename Compare, std::size_t N>
245constexpr auto make_set(std::array<T, N> const &args, Compare const& compare = Compare{}) {
246 return set<T, N, Compare>(args, compare);
247}
248
249#ifdef FROZEN_LETITGO_HAS_DEDUCTION_GUIDES
250
251template<class T, class... Args>
252set(T, Args...) -> set<T, sizeof...(Args) + 1>;
253
254#endif // FROZEN_LETITGO_HAS_DEDUCTION_GUIDES
255
256} // namespace frozen
257
258#endif
Definition basic_types.h:90
const value_type & const_reference
Definition basic_types.h:109
const_pointer const_iterator
Definition basic_types.h:113
const value_type * const_pointer
Definition basic_types.h:111
std::size_t size_type
Definition basic_types.h:114
typename container_type::const_pointer pointer
Definition set.h:167
constexpr const_iterator end() const
Definition set.h:215
constexpr std::pair< const_iterator, const_iterator > equal_range(KeyType const &) const
Definition set.h:200
constexpr set(bits::carray< Key, 0 >)
Definition set.h:178
Key key_type
Definition set.h:159
constexpr set(std::initializer_list< Key >, Compare const &comp)
Definition set.h:180
constexpr set(const set &other)=default
constexpr const_reverse_iterator crbegin() const
Definition set.h:219
constexpr const_reverse_iterator crend() const
Definition set.h:221
pointer const_pointer
Definition set.h:168
constexpr const_iterator cend() const
Definition set.h:216
constexpr const_iterator find(KeyType const &) const
Definition set.h:196
typename container_type::size_type difference_type
Definition set.h:162
constexpr const_iterator upper_bound(KeyType const &) const
Definition set.h:206
constexpr const key_compare & value_comp() const
Definition set.h:210
constexpr const_iterator cbegin() const
Definition set.h:214
constexpr set & operator=(const set &other)=default
constexpr const_reverse_iterator rend() const
Definition set.h:220
typename container_type::const_reference reference
Definition set.h:165
constexpr set(std::initializer_list< Key > keys)
Definition set.h:182
constexpr const key_compare & key_comp() const
Definition set.h:209
const_pointer const_iterator
Definition set.h:171
Key value_type
Definition set.h:160
typename container_type::size_type size_type
Definition set.h:161
constexpr const_reverse_iterator rbegin() const
Definition set.h:218
reference const_reference
Definition set.h:166
constexpr size_type max_size() const
Definition set.h:189
constexpr std::size_t count(KeyType const &) const
Definition set.h:193
Compare key_compare
Definition set.h:163
pointer reverse_iterator
Definition set.h:170
pointer iterator
Definition set.h:169
constexpr size_type size() const
Definition set.h:188
Compare value_compare
Definition set.h:164
constexpr bool empty() const
Definition set.h:187
const_pointer const_reverse_iterator
Definition set.h:172
constexpr const_iterator begin() const
Definition set.h:213
constexpr set(bits::carray< Key, 0 >, Compare const &)
Definition set.h:177
constexpr const_iterator lower_bound(KeyType const &) const
Definition set.h:203
Definition set.h:36
constexpr set(std::initializer_list< Key > keys, Compare const &comp)
Definition set.h:69
constexpr size_type max_size() const
Definition set.h:81
typename container_type::const_pointer pointer
Definition set.h:50
constexpr set(std::initializer_list< Key > keys)
Definition set.h:73
constexpr set & operator=(const set &other)=default
constexpr size_type size() const
Definition set.h:80
Compare value_compare
Definition set.h:47
constexpr bool contains(KeyType const &key) const
Definition set.h:99
Compare key_compare
Definition set.h:46
constexpr std::pair< const_iterator, const_iterator > equal_range(KeyType const &key) const
Definition set.h:104
constexpr std::size_t count(KeyType const &key) const
Definition set.h:85
reference const_reference
Definition set.h:49
typename container_type::const_reference reference
Definition set.h:48
constexpr set(container_type keys)
Definition set.h:66
Key key_type
Definition set.h:42
Key value_type
Definition set.h:43
typename container_type::const_iterator iterator
Definition set.h:52
constexpr bool operator<(set const &rhs) const
Definition set.h:148
constexpr const_iterator begin() const
Definition set.h:135
constexpr const_reverse_iterator crbegin() const
Definition set.h:141
constexpr const_iterator lower_bound(KeyType const &key) const
Definition set.h:113
constexpr bool operator!=(set const &rhs) const
Definition set.h:147
std::reverse_iterator< iterator > reverse_iterator
Definition set.h:53
constexpr set(container_type keys, Compare const &comp)
Definition set.h:61
constexpr const_reverse_iterator rbegin() const
Definition set.h:140
pointer const_pointer
Definition set.h:51
constexpr bool operator==(set const &rhs) const
Definition set.h:146
constexpr bool operator>(set const &rhs) const
Definition set.h:150
constexpr bool operator>=(set const &rhs) const
Definition set.h:151
iterator const_iterator
Definition set.h:54
constexpr const_iterator end() const
Definition set.h:137
constexpr const_iterator cbegin() const
Definition set.h:136
constexpr bool empty() const
Definition set.h:79
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition set.h:55
constexpr set(const set &other)=default
constexpr const key_compare & value_comp() const
Definition set.h:132
typename container_type::size_type size_type
Definition set.h:44
typename container_type::size_type difference_type
Definition set.h:45
constexpr const_iterator find(KeyType const &key) const
Definition set.h:90
constexpr bool operator<=(set const &rhs) const
Definition set.h:149
constexpr const_iterator cend() const
Definition set.h:138
constexpr const_reverse_iterator rend() const
Definition set.h:142
constexpr const_reverse_iterator crend() const
Definition set.h:143
constexpr const_iterator upper_bound(KeyType const &key) const
Definition set.h:122
constexpr const key_compare & key_comp() const
Definition set.h:131
@ T
Definition AcceleratorCodes.hpp:97
Definition algorithms.h:33
constexpr bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2)
Definition algorithms.h:210
constexpr ForwardIt lower_bound(ForwardIt first, const T &value, Compare const &compare)
Definition algorithms.h:197
constexpr bool lexicographical_compare(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
Definition algorithms.h:221
constexpr bool binary_search(ForwardIt first, const T &value, Compare const &compare)
Definition algorithms.h:202
Definition algorithm.h:30
constexpr auto make_set(bits::ignored_arg={})
Definition set.h:225
Definition basic_types.h:39