-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSM_utils.hpp
More file actions
311 lines (284 loc) · 10.6 KB
/
SM_utils.hpp
File metadata and controls
311 lines (284 loc) · 10.6 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#pragma once
#include <random>
#include <algorithm>
#include <utility>
#include <set>
#include <vector>
#include <queue>
#include <iostream>
#include <cassert>
//#include <ranges>
//random utilities,
namespace SM_utils{
template<typename T>
using numeric=T;
//concept numeric=std::integral<T> || std::floating_point<T>;
template<typename T, typename Compare=std::less<T>>
class flat_set {
private:
std::vector<T> vect;
const Compare compare={};
public:
using iterator=typename std::vector<T>::iterator;
using const_iterator=typename std::vector<T>::const_iterator;
[[nodiscard]] auto begin(){
return vect.begin();
}
[[nodiscard]] auto begin() const {
return vect.begin();
}
[[nodiscard]] auto cbegin() const {
return vect.cbegin();
}
[[nodiscard]] auto end(){
return vect.end();
}
[[nodiscard]] auto end() const {
return vect.end();
}
[[nodiscard]] auto cend() const {
return vect.cend();
}
[[nodiscard]] auto data(){
return vect.data();
}
[[nodiscard]] auto data() const {
return vect.data();
}
[[nodiscard]] auto& operator[](std::size_t i) {
return vect[i];
}
[[nodiscard]] const auto& operator[](std::size_t i) const {
return vect[i];
}
[[nodiscard]] auto size() const {
return vect.size();
}
void reserve(std::size_t s){
vect.reserve(s);
}
template<typename... Args>
void emplace(Args&&... args){
T t(std::forward<Args>(args)...);
vect.insert(std::lower_bound(vect.begin(), vect.end(), t, compare), std::move(t));
}
void insert(T&& t) {
vect.insert(std::lower_bound(vect.begin(), vect.end(), t, compare), std::move(t));
}
void insert(const T& t) {
vect.insert(std::lower_bound(vect.begin(), vect.end(), t, compare), t);
}
void erase(const T& t) {
vect.erase(std::lower_bound(vect.begin(), vect.end(), t, compare));
}
void erase(iterator t) {
vect.erase(t);
}
void erase(const_iterator t) {
vect.erase(t);
}
template<typename U>
[[nodiscard]] auto find(const U& t) {
const auto ret = std::lower_bound(vect.begin(), vect.end(), t, compare);
if(ret!=vect.end() && !compare(*ret, t) && !compare(t, *ret)){
return ret;
}
return vect.end();
}
template<typename U>
[[nodiscard]] auto find(const U& t) const {
const auto ret = std::lower_bound(vect.begin(), vect.end(), t, compare);
if(ret!=vect.end() && !compare(*ret, t) && !compare(t, *ret)){
return ret;
}
return vect.end();
}
[[nodiscard]] auto contains(const T& t) const{
return std::binary_search(vect.begin(), vect.end(), t, compare);
}
flat_set()=default;
flat_set(auto begin, auto end): vect(begin, end) {}
};
template<typename T, typename Enable = std::void_t<>>
struct is_pointer_fancy_impl : std::false_type {};
template<typename T>
struct is_pointer_fancy_impl<T, std::void_t<typename T::element_type>> : std::true_type {};
template<typename T>
struct is_pointer_fancy_impl<T*> : std::true_type {};
template<typename T>
struct is_pointer_fancy : is_pointer_fancy_impl<std::decay_t<T>> {};
template<typename T>
constexpr inline bool is_pointer_fancy_v=is_pointer_fancy<T>::value;
template<typename OriginalIterator>
class UnowningIterator {
private:
using pointer = typename OriginalIterator::value_type::element_type**;
using value_type = typename OriginalIterator::value_type::element_type*;
using reference = typename OriginalIterator::value_type::element_type*;
using difference_type = typename OriginalIterator::difference_type;
using iterator_category = typename OriginalIterator::iterator_category;
OriginalIterator inner_it;
public:
[[nodiscard]] value_type operator*() const {
return inner_it->get();
}
auto operator++() {
return UnowningIterator{++inner_it};
}
auto operator++(int) & {
return UnowningIterator{inner_it++};
}
auto operator+(std::size_t rhs) {
return UnowningIterator{inner_it+rhs};
}
auto operator-(std::size_t rhs) {
return UnowningIterator{inner_it-rhs};
}
[[nodiscard]] difference_type operator-(const UnowningIterator& rhs) const {
return inner_it-rhs.inner_it;
}
[[nodiscard]] bool operator!=(const UnowningIterator& rhs) const {
return inner_it!=rhs.inner_it;
}
explicit UnowningIterator(OriginalIterator inner_it_): inner_it{inner_it_} {}
};
template</*std::ranges::random_access_range*/typename ContainerType, /*std::forward_iterator*/typename iterator>
class NestingIterator: public iterator{
public:
using pointer = typename ContainerType::value_type*;
using value_type = typename ContainerType::value_type;
using reference = typename ContainerType::value_type&;
using difference_type = typename iterator::difference_type;
using iterator_category = typename iterator::iterator_category;
private:
ContainerType& outerArray;
public:
[[nodiscard]] auto& operator*() {
return outerArray[iterator::operator*()];
}
[[nodiscard]] const auto& operator*() const {
return outerArray[iterator::operator*()];
}
NestingIterator(ContainerType& outerArray_, iterator currentLocation) :
iterator(currentLocation),
outerArray{outerArray_}
{}
};
//template<typename T>
//concept priority_queue=requires(T c) { c.top(); c.pop(); c.size();};
template</*priority_queue*/ typename ContainerType>
class ConsumingIterator{
private:
ContainerType& container;
public:
explicit ConsumingIterator(ContainerType& container_) : container{container_} {}
[[nodiscard]] auto operator*() const {
return container.top();
}
void operator++(){
return container.pop();
}
[[nodiscard]] bool operator!=([[maybe_unused]] ConsumingIterator<ContainerType>& end) const {
return container.size();
}
};
template</*priority_queue*/ typename ContainerType>
class ConsumingRange{
private:
ContainerType& container;
public:
explicit ConsumingRange(ContainerType& container_) : container{container_} { }
[[nodiscard]] auto begin() const {
return ConsumingIterator<ContainerType>{container};
}
[[nodiscard]] auto end() const {
return ConsumingIterator<ContainerType>{container};
}
};
//intended usage is to top and pop and then occassionally reinsert numbers that have been already popped (so all numbers inserted are less than max)
template</*std::integral*/typename T>
class IncreasingPQ{
private:
T max;
std::priority_queue<T, std::vector<T>, std::greater<T>> reinserted;
public:
[[nodiscard]] const auto& top() const {
if(reinserted.empty()){
return max;
}
return reinserted.top();
}
void pop(){
if(reinserted.empty()){
++max;
} else {
reinserted.pop();
}
}
void push(const T& value){
assert(value<max);
reinserted.push(value);
}
explicit IncreasingPQ(T starting) : max{starting} {}
};
class CountingIterator{
private:
std::size_t value;
public:
using difference_type = std::ptrdiff_t;
using value_type = std::size_t;
using pointer = std::size_t*;
using reference = std::size_t&;
using iterator_category = std::random_access_iterator_tag;
explicit CountingIterator(std::size_t initialValue) : value(initialValue){}
void operator++(){
++value;
};
void operator--(){
--value;
};
[[nodiscard]] auto operator+(const std::size_t val) const {
return CountingIterator{value+val};
};
void operator+=(const std::size_t val) {
value+=val;
};
[[nodiscard]] difference_type operator-(const CountingIterator& rhs) const{
return value-rhs.value;
}
[[nodiscard]] bool operator==(CountingIterator const& it) const {
return value==it.value;
}
[[nodiscard]] bool operator!=(CountingIterator const& it) const {
//std::cout<<value<<" "<<it.value<<std::endl;
return value!=it.value;
}
[[nodiscard]] value_type operator*() const {
return value;
}
};
template</*std::ranges::random_access_range*/typename ContainerType, typename Compare=std::less<>, /*std::integral*/typename IndexType=std::size_t>
class IndexCompare{
private:
const ContainerType& container;
const Compare compare={};
public:
explicit IndexCompare(const ContainerType& container_) : container{container_} { }
[[nodiscard]] auto operator()(IndexType a, IndexType b) const{
return compare(container[a], container[b]);
}
};
//template magic? template magic. Of course it's stack overflow. NOTE THAT IT's REVERSED
//It goes is_base_template<ExpectedDerived, ExpectedBase>
template <template <typename...> class C, typename...Ts>
std::true_type is_base_of_template_impl(const C<Ts...>*);
template <template <typename...> class C>
std::false_type is_base_of_template_impl(...);
template <typename T, template <typename...> class C>
using is_base_of_template = decltype(is_base_of_template_impl<C>(std::declval<T*>()));
#ifdef NDEBUG
inline constexpr bool debug=false;
#else
inline constexpr bool debug=true;
#endif
} // namespace SM_utils