-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
395 lines (353 loc) · 7.94 KB
/
main.cpp
File metadata and controls
395 lines (353 loc) · 7.94 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <compare>
#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <regex>
#include <new>
#include <typeinfo>
#include "TimeGuard.hpp"
#include "show.h"
#include "point.h"
#include <thread>
#include <mutex>
#include <condition_variable>
#include <shared_mutex>
#include "Sorting.hpp"
#include "ListNode.h"
class B;
class D1;
class D2;
class D3;
//==============================================================================//
class B {
int b;
public:
B() : b(0)
{
std::cout << "Конструктор B()" << std::endl;
}
B(const B& other)
{
if (this == &other)
return;
this->b = other.b;
std::cout << "Вызывался конструктор копирования B(B &other)" << std::endl;
}
B(int b) : b(b)
{
std::cout << "Конструктор B()" << std::endl;
}
virtual ~B()
{
std::cout << "Деструктор ~B()" << std::endl;
}
int get_b() const
{
return b;
};
virtual void show()
{
std::cout << "B : " << get_b() << std::endl;
}
};
class D1 : virtual public B {
int d1;
public:
D1() : B(), d1(0)
{
std::cout << "Конструктор D1()" << std::endl;
}
D1(int b, int d) : B(b), d1(d)
{
std::cout << "Конструктор D1()" << std::endl;
}
~D1()
{
std::cout << "Декструткор ~D1()" << std::endl;
}
int get_d1() const
{
return this->d1;
};
void show() override
{
std::cout << "D : " << this->get_b() << "\t" << d1 << std::endl;
}
int mul() const
{
return d1 * get_b();
};
};
class D2 : virtual public B {
int d2;
public:
D2() : B(), d2(0)
{
std::cout << "Конструктор D2()" << std::endl;
}
D2(int b, int d) : B(b), d2(d)
{
std::cout << "Конструктор D2()" << std::endl;
}
~D2()
{
std::cout << "Декструткор ~D2()" << std::endl;
}
int get_d2() const
{
return this->d2;
};
void show() override
{
std::cout << "D : " << this->get_b() << "\t" << d2 << std::endl;
}
int mul() const
{
return d2 * get_b();
};
};
class D3 : public D1, public D2 {
int d3;
public:
D3() : d3(0)
{
std::cout << "Конструктор D3()" << std::endl;
}
D3(int b, int d) : d3(d)
{
std::cout << "Конструктор D3()" << std::endl;
}
~D3()
{
std::cout << "Декструткор ~D3()" << std::endl;
}
int get_d3() const
{
return this->d3;
};
void show() override
{
std::cout << "D3 : " << "\t" << this->get_b() << "\t" << this->get_d1() << "\t" << this->get_d2() << "\t" << d3 << std::endl;
}
};
//==============================================================================//
class Base {
int a, b;
public:
Base() : a(0), b(0)
{
std::cout << "Base()" << std::endl;
}
Base(int a) : a(a), b(0)
{
std::cout << "Base()" << std::endl;
}
virtual ~Base()
{
std::cout << "~Base()" << std::endl;
}
int getA() { return a; }
int getB() { return b; }
virtual void show() = 0;
};
class Child final : public Base
{
//Timer t{"Field in Child"};
public:
Child()
{
std::cout << "Child()" << std::endl;
}
~Child()
{
std::cout << "~Child()" << std::endl;
}
virtual void show() override
{
std::cout << getB() * getA() << std::endl;
}
};
//==============================================================================//
std::istream& manip(std::istream& out)
{
out.width(20);
out.precision(5);
out.fill('-');
return out;
}
void check_status(std::ostream& in)
{
std::iostream::iostate i = in.rdstate();
bool a = i & std::_S_goodbit;
a = i & std::iostream::badbit;
a = i & std::iostream::failbit;
a = i & std::iostream::eofbit;
if (i == std::iostream::badbit) {
std::cout << "Fatal!";
}
if (i == std::iostream::failbit) {
std::cout << "No fatal!";
}
if (i == std::_S_goodbit) {
std::cout << "Все хорошо!";
}
if (i == std::iostream::eofbit) {
std::cout << "End!";
}
}
template<typename T>
T sum(T a, T b)
{
return a + b;
}
double divide(double a, double b)
{
try
{
if (!b) throw b;
}
catch (...)
{
std::cerr << "Дление на 0" << '\n';
exit(1);
}
return a / b;
}
template<typename T>
class G {
T x;
};
//==============================================================================//
// свой new
// void* operator new(size_t count){
// std::cout << "new: allocate " << count << " bytes" << std::endl;
// if(auto ptr = std::malloc(count); ptr != nullptr)
// return ptr;
// throw std::bad_alloc();
// }
// void* operator new[](size_t count){
// std::cout << "new[] -> ";
// return ::operator new(count);
// }
// N* b = new N;
// N* barr = new N[1];
class N {
int b;
public:
N() : b(0)
{
std::cout << "N()" << std::endl;
}
~N()
{
std::cout << "~N()" << std::endl;
}
};
//==============================================================================//
// интересный случай с move семантикой
// float a = 1.1f;
// int b = 2;
// std::cout << " For rvalue : " << std::endl;
// std::cout << " float 1.1f : " << std::endl;
// test(1.1f);
// std::cout << " int 2 : " << std::endl;
// test(2);
// std::cout << std::endl << std::endl;
// std::cout << " For lvalue : " << std::endl;
// std::cout << " float 1.1f : " << std::endl;
// test(a);
// std::cout << " int 2 : " << std::endl;
// test(b);
void func(float&& a)
{
std::cout << "f&& " << a << std::endl;
}
void func(int&& a)
{
std::cout << "i&& " << a << std::endl;
}
void func(float& a)
{
std::cout << "f& " << a << std::endl;
}
void func(int& a)
{
std::cout << "i& " << a << std::endl;
}
template <typename T>
void test(T&& v)
{
std::cout << "Common -> ";
func(v);
std::cout << "std::move -> ";
func(std::move(v));
std::cout << "std::forward<T> -> ";
func(std::forward<T&&>(v));
}
//==============================================================================//
// свой std::move()
void foo(int& v) { std::cout << "int& " << v << std::endl; }
void foo(int&& v) { std::cout << "int&& " << v << std::endl; }
template<typename T>
std::remove_reference_t<T>&& custom_move(T&& value)
{
return static_cast<std::remove_reference_t<T>&&>(value);
}
template<typename T>
T&& custom_forward(std::remove_reference_t<T>& value)
{
return static_cast<T&&>(value);
}
template<typename T>
T&& custom_forward(std::remove_reference_t<T>&& value)
{
static_assert(!std::is_lvalue_reference_v<T>);
return static_cast<T&&>(value);
}
//==============================================================================//
std::mutex mt;
int counter{ 0 };
std::condition_variable cv;
void processing()
{
int c;
{
std::unique_lock<std::mutex> lk{ mt };
cv.wait(lk, [] { return counter > 0;});
c = counter;
}
std::cout << "-";
}
void prepare()
{
{
std::lock_guard<std::mutex> lk{ mt };
++counter;
cv.notify_all();
}
std::cout << "+";
}
std::vector<int> failure_func(const std::string& str)
{
std::vector<int> res(str.size());
for(size_t i(1), j(0); i < str.size(); i++) {
j = res[i - 1];
while (j > 0 and str[j] != str[i]) {
j = res[j - 1];
}
if (str[j] == str[i]) {
res[i] = j + 1;
}
}
return res;
}
//==============================================================================//
int main()
{
TimeGuard timer("Main.cpp");
std::string str = "ABABAA";
sh::ShowContainer(failure_func(str));
}