-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlukalib.h
358 lines (292 loc) · 8.84 KB
/
lukalib.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#ifndef LUKALIB_H
#define LUKALIB_H
#include <functional>
#include <algorithm>
#include <iterator>
#include <typeinfo>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <utility>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <list>
#include <map>
#include <set>
/*using std::cout;
using std::endl;
using std::list;
using std::find;
using std::stack;
using std::string;
using std::ostream;
using std::ios_base;
using std::stringstream;*/
using namespace std;
/* GENERAL FUNCTIONS */
//#define full(x) x.begin(), x.end()
#define full(x) std::begin(x), std::end(x)
#define contains(x, y) (std::find(full(x), y) != x.end())
#define pointer_iterate(iter, cont) if (cont.size()) for (auto &iter : cont) if (iter)
#define pointer_iterate_blown(iter, cont) for (auto iter = cont.begin(); iter != cont.end(); ++iter) if (*iter)
#ifndef llib_ignore_byte_typedef
typedef uint8_t byte;
#endif
template <typename T>
int sgn(T val) /// SO's user79758, jer se luki neda
{
return (T(0) < val) - (val < T(0));
}
template <class C>
void container_del(C &v)
{
if (v.size()) for (auto i : v) if (i) delete i;
}
template <class C, class O>
void value_dump(C val, O& strm) { strm << val << " "; }
template <class C, class O = ostream>
void container_dump(C v,
O& strm = cout,
function<void(typename C::value_type, O&)> print_func = value_dump<typename C::value_type, O>)
{
if (v.size()) for (auto &i : v) print_func(i, strm);
}
/*template <class C>
void container_dump2d(C v, function<void(typename C::value_type::value_type)> print_func = value_dump<typename C::value_type::value_type>)
{
container_dump(v,
[print_func](typename C::value_type v2) {
container_dump(v2, print_func);
cout << endl;
}
);
}*/
template <class T2, class T1>
T2 scast(T1 val)
{ stringstream _in; _in.setf(ios_base::fixed); _in << val;
_in.setf(ios_base::fixed); T2 _out; _in >> _out; return _out;
}
/* TREE */
/*
* idejise:
* - curious recurring pattern: neka tree ima param <T>, T : tree, to sredi sve valjda
* - T : tree, node_data, 2struki inherit
* - generic container
*
*/
template <class derived_tree>
class tree
{
template <class any_derived_tree>
friend ostream& operator<<(ostream& out, tree<any_derived_tree>& what);
protected:
list<tree<derived_tree>*> children; // *** derived_tree?
void _init()
{
}
void _clear()
{
container_del(children);
children.clear();
}
void _reset()
{
_clear();
_init();
}
virtual void _copy_from(tree<derived_tree>& what);
virtual tree<derived_tree>* _deep_copy();
void _push_left(tree<derived_tree> &what)
{
children.push_front(what._deep_copy());
}
void _push_right(tree<derived_tree> &what)
{
children.push_back(what._deep_copy());
}
public:
class iterator;
tree<derived_tree>()
{
_reset();
}
tree<derived_tree>(tree<derived_tree>& cp)
{
_copy_from(cp);
}
tree<derived_tree>(tree<derived_tree>&& slow_cp)
{
_copy_from(slow_cp);
}
virtual ~tree<derived_tree>()
{
_clear();
}
// dodati move konstr.
virtual void push_left(derived_tree& what)
{
_push_left(what);
}
virtual void push_left(derived_tree slow_what)
{
_push_left(slow_what);
}
virtual void push_right(derived_tree& what)
{
_push_right(what);
}
iterator left();
iterator right();
iterator begin();
iterator end();
virtual void print_node(ostream& out)
{
out << this << endl;
}
void print_tree(ostream& out, string prefix = "")
{
out << prefix;
print_node(out);
pointer_iterate(child, children) child->print_tree(out, prefix + "\t");
}
};
template <class derived_tree>
class tree<derived_tree>::iterator
{
list<tree<derived_tree>*> skip_children_in_next_iter;
tree<derived_tree>* t;
bool _should_skip(tree<derived_tree>* which)
{
return contains(skip_children_in_next_iter, which);
}
struct iteration_tree_frame
{
typedef typename list<tree<derived_tree>*>::iterator list_itr;
tree<derived_tree>* actual_parent;
list_itr current_position;
iterator& ref_to_itr;
iteration_tree_frame(tree* parent, list_itr current, iterator& itr)
: ref_to_itr(itr), actual_parent(parent), current_position(current)
{ }
bool operator==(const iteration_tree_frame& b) const
{
return actual_parent == b.actual_parent
&& current_position == b.current_position;
}
list_itr next_child(list_itr current_position)
{
++current_position;
while (current_position != actual_parent->children.end())
if (ref_to_itr._should_skip(*current_position))
++current_position;
else
break;
return current_position;
}
bool frame_at_last()
{
return next_child(current_position) == actual_parent->children.end();
}
// skips one, and than skips while not EOT and skippable nodes
iteration_tree_frame& operator++()
{
current_position = next_child(current_position);
return *this;
}
};
stack<iteration_tree_frame> ancestry;
public:
iterator(tree<derived_tree>* from) : t(from) { }
iterator& operator++()
{
bool made_it = false;
if (!t->children.empty())
pointer_iterate_blown(child, t->children)
if (!_should_skip(*child))
{
made_it = true;
ancestry.push(iteration_tree_frame(t, child, *this));
t = *child;
break;
}
if (!made_it)
{
while (!ancestry.empty() && ancestry.top().frame_at_last())
ancestry.pop();
if (ancestry.empty())
{
t = 0; // done iterating, set iterator to .end()
ancestry = stack<iteration_tree_frame>();
skip_children_in_next_iter = list<tree<derived_tree>*>();
}
else
{ // iterate on quasi-sister node (quasi-mother's next child)
iteration_tree_frame &last = ancestry.top();
last.current_position++;
t = *last.current_position;
}
}
return *this;
}
bool operator==(const iterator& b) const
{
return (t == b.t)
&& (ancestry == b.ancestry)
&& (skip_children_in_next_iter == b.skip_children_in_next_iter);
}
bool operator!=(const iterator& b) const
{
return !(*this == b);
}
derived_tree* operator->()
{
return dynamic_cast<derived_tree*>(t);
}
derived_tree& operator*()
{
return dynamic_cast<derived_tree&>(*t);
}
void push_and_skip_left(derived_tree &child)
{
t->push_left(child);
if (!contains(skip_children_in_next_iter, t->children.front()))
skip_children_in_next_iter.push_back(t->children.front());
}
void push_and_skip_right(derived_tree &child)
{
t->push_right(child);
if (!contains(skip_children_in_next_iter, t->children.back()))
skip_children_in_next_iter.push_back(t->children.back());
}
};
template <typename derived_tree> void
tree<derived_tree>::_copy_from(tree<derived_tree>& what)
{
_reset();
if (what.children.size()) for (tree<derived_tree>* &child : what.children)
children.push_back(new derived_tree(dynamic_cast<derived_tree&>(*child)));
}
template <typename derived_tree> tree<derived_tree>*
tree<derived_tree>::_deep_copy()
{
tree<derived_tree>* n = new derived_tree;
n->_copy_from(*this);
return n;
}
template <typename derived_tree> typename tree<derived_tree>::iterator
tree<derived_tree>::left() { return children.front()->begin(); }
template <typename derived_tree> typename tree<derived_tree>::iterator
tree<derived_tree>::right() { return children.back()->begin(); }
template <typename derived_tree> typename tree<derived_tree>::iterator
tree<derived_tree>::begin() { return iterator(this); }
template <typename derived_tree> typename tree<derived_tree>::iterator
tree<derived_tree>::end() { return iterator(0); }
template<class derived_tree>
ostream& operator<<(ostream& out, tree<derived_tree>& what)
{
what.print_tree(out);
return out;
}
#endif // LUKALIB_H