/
ordered.hpp
217 lines (182 loc) · 6.52 KB
/
ordered.hpp
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
#pragma once
#include <functional>
#include <vector>
#include <set>
#include <unordered_set>
namespace detail {
template<typename T, template<typename...> class Templ>
struct is_specialization_of : std::false_type { };
template<typename... T, template<typename...> class Templ>
struct is_specialization_of<Templ<T...>, Templ> : std::true_type { };
template <typename T, typename, bool = detail::is_specialization_of<T, std::map>::value || detail::is_specialization_of<T, std::set>::value>
struct key_compare {
typedef typename T::key_compare type;
};
template <typename T, typename Comp>
struct key_compare<T, Comp, false> {
typedef Comp type;
};
template <typename T, typename Comp>
using key_compare_t = typename key_compare<T, Comp>::type;
template <typename C>
auto adl_begin(C&& c) {
using std::begin;
return begin(c);
}
template <typename C>
auto adl_end(C&& c) {
using std::end;
return end(c);
}
template <typename C>
auto adl_cbegin(C&& c) {
using std::cbegin;
return cbegin(c);
}
template <typename C>
auto adl_cend(C&& c) {
using std::cend;
return cend(c);
}
template <typename It, typename Diff>
auto incr_tagged(std::forward_iterator_tag, It it, Diff n) {
if (n < 0) throw std::range_error("negative increment to forward iterator");
for (;n--;) ++it;
return it;
}
template <typename It, typename Diff>
auto incr_tagged(std::bidirectional_iterator_tag, It it, Diff n) {
if (n < 0) for (;n++;) --it;
else for (;n--;) ++it;
return it;
}
template <typename It, typename Diff>
auto incr_tagged(std::random_access_iterator_tag, It it, Diff n) {
it += n;
return it;
}
template <typename It, typename Diff>
auto incr(It it, Diff n) {
return incr_tagged(std::iterator_traits<It>::iterator_category(), it, n);
}
}
template <typename Val, typename Predicate = std::less<>, typename Cont = std::vector<Val>>
struct ordered;
template <typename Cont>
using ordered_container = ordered<typename Cont::value_type, detail::key_compare_t<Cont, std::less<>>, Cont>;
template <typename Val, typename Predicate, typename Cont>
struct ordered {
private:
Cont container;
Predicate predicate; // TODO: EBCO this member
public:
typedef typename Cont::value_type key_type;
typedef typename Cont::value_type value_type;
typedef typename Cont::reference reference;
typedef typename Cont::const_reference const_reference;
typedef typename Cont::pointer pointer;
typedef typename Cont::const_pointer const_pointer;
typedef Predicate key_compare;
typedef Predicate value_compare;
typedef typename Cont::allocator_type allocator_type;
typedef typename Cont::size_type size_type;
typedef typename Cont::difference_type difference_type;
typedef size_type index_type;
typedef typename Cont::iterator begin_iterator;
typedef typename Cont::iterator end_iterator;
typedef typename Cont::iterator iterator;
typedef typename Cont::const_iterator const_iterator;
iterator insert(const value_type& val) {
if (container.empty()) {
return container.insert(detail::adl_cend(container), val);
}
auto elementgreaterthanorequalto = std::lower_bound(detail::adl_cbegin(container), detail::adl_cend(container), val, std::ref(predicate));
return container.insert(elementgreaterthanorequalto, val);
}
iterator insert(value_type&& val) {
if (container.empty()) {
return container.insert(detail::adl_cend(container), val);
}
auto elementgreaterthanorequalto = std::lower_bound(detail::adl_cbegin(container), detail::adl_cend(container), val, std::ref(predicate));
return container.insert(elementgreaterthanorequalto, std::move(val));
}
template <typename... Tn>
iterator emplace(Tn&&... argn) {
if (container.empty()) {
return container.insert(detail::adl_cend(container), std::forward<Tn>(argn)...);
}
auto elementgreaterthanorequalto = std::lower_bound(detail::adl_cbegin(container), detail::adl_cend(container), val, std::ref(predicate));
return container.emplace(elementgreaterthanorequalto, std::forward<Tn>(argn)...);
}
iterator erase(index_type i) {
auto it = detail::incr(detail::adl_begin(container), i);
return erase(it);
}
iterator erase(const_iterator i) {
return container.erase(i);
}
void clear() {
container.clear();
}
size_type size() const { return container.size(); }
auto begin() { return detail::adl_begin(container); }
auto begin() const { return detail::adl_begin(container); }
auto cbegin() const { return detail::adl_cbegin(container); }
auto end() { return detail::adl_end(container); }
auto end() const { return detail::adl_end(container); }
auto cend() const { return detail::adl_cend(container); }
};
template <typename Val, typename Predicate, typename Allocator>
struct ordered<Val, Predicate, std::set<Val, Predicate, Allocator>> {
private:
typedef std::set<Val, Predicate, Allocator> Cont;
Cont container;
public:
typedef typename Cont::value_type key_type;
typedef typename Cont::value_type value_type;
typedef typename Cont::reference reference;
typedef typename Cont::const_reference const_reference;
typedef typename Cont::pointer pointer;
typedef typename Cont::const_pointer const_pointer;
typedef Predicate key_compare;
typedef Predicate value_compare;
typedef typename Cont::allocator_type allocator_type;
typedef typename Cont::size_type size_type;
typedef typename Cont::difference_type difference_type;
typedef size_type index_type;
typedef typename Cont::iterator begin_iterator;
typedef typename Cont::iterator end_iterator;
typedef typename Cont::iterator iterator;
typedef typename Cont::const_iterator const_iterator;
typedef typename Cont::reverse_iterator reverse_iterator;
typedef typename Cont::const_reverse_iterator const_reverse_iterator;
iterator insert(const value_type& val) {
auto x = container.insert(val);
return x.first;
}
iterator insert(value_type&& val) {
auto x = container.insert(std::move(val));
return x.first;
}
template <typename... Tn>
iterator emplace(Tn&&... argn) {
auto x = container.emplace(std::forward<Tn>(argn)...);
return x.first;
}
iterator erase(index_type i) {
auto it = detail::incr(detail::adl_begin(container), i);
return erase(it);
}
iterator erase(const_iterator i) {
return container.erase(i);
}
void clear() {
container.clear();
}
auto begin() { return detail::adl_begin(container); }
auto begin() const { return detail::adl_begin(container); }
auto cbegin() const { return detail::adl_cbegin(container); }
auto end() { return detail::adl_end(container); }
auto end() const { return detail::adl_end(container); }
auto cend() const { return detail::adl_cend(container); }
};