-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontest2.3.cpp
218 lines (187 loc) · 5.14 KB
/
contest2.3.cpp
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
#include <vector>
#include <iostream>
#include <stack>
namespace yeet{
template<typename T>
struct stack : private std::vector<T>
{
T &top();
T const &top() const;
bool empty() const;
std::size_t size() const;
void push(T const &value);
void push(T &&value);
void pop();
template<typename... Args>
void emplace(Args &&... args)
{
this->emplace_back(static_cast<Args &&>(args)...);
}
};
template<typename T>
T &stack<T>::top(){return this->back();}
template<typename T>
const T &stack<T>::top() const{return this->back();}
template<typename T>
bool stack<T>::empty() const{return static_cast<const std::vector<T>*>(this)->empty();}
template<typename T>
std::size_t stack<T>::size() const{return static_cast<const std::vector<T>*>(this)->size();}
template<typename T>
void stack<T>::push(T const &value){this->push_back(value);}
template<typename T>
void stack<T>::push(T &&value){this->push_back(static_cast<T&&>(value));}
template<typename T>
void stack<T>::pop(){this->pop_back();}
}
struct Test
{
int number;
~Test()
{
std::cout << "Test::~Test(): " << number << std::endl;
}
Test()
{
std::cout << "Test::Test()" << std::endl;
}
Test(int const n)
: number(n)
{
std::cout << "Test::Test(int): " << number << std::endl;
}
Test(Test const &other)
: number(other.number)
{
std::cout << "Test::Test(Test const &) : " << number << std::endl;
}
Test(Test &&other)
: number(other.number)
{
other.number = 0;
std::cout << "Test::Test(Test &&) : " << number << std::endl;
}
Test &operator=(Test const &other)
{
std::cout << "Test &Test::operator=(Test const &) : " << number << " -> " << other.number << std::endl;
if(this != &other)
{
this->~Test();
new (this) Test(other);
}
return *this;
}
Test &operator=(Test &&other)
{
std::cout << "Test &Test::operator=(Test &&) : " << number << " -> " << other.number << std::endl;
if(this != &other)
{
this->~Test();
new (this) Test(static_cast<Test &&>(other));
}
return *this;
}
};
template<typename T>
struct tuple_wrap
{
T value;
};
template<typename... Ts>
struct tuple : tuple_wrap<Ts>...
{};
template<typename T, typename... Ts>
T &get(tuple<Ts...> &fuck){
return static_cast<tuple_wrap<T>&>(fuck).value;
}
template<typename T, typename... Ts>
T const &get(tuple<Ts...> const &oopsucks){
return static_cast<const tuple_wrap<T>&>(oopsucks).value;
}
int main(){
tuple<int, float, char> const tup = {1, 2.f, '3'};
std::cout << get<int >(tup) << ' ';
std::cout << get<float>(tup) << ' ';
std::cout << get<char >(tup) << std::endl; // 1 2 3
}
// int main()
// {
// yeet::stack<Test> s;
// std::cout << s.empty() << std::endl;
// std::cout << s.size() << std::endl;
// Test test(-2);
// s.push(test);
// std::cout << s.top().number << std::endl;
// for(int i = 0; i < 5; ++i)
// s.push(Test(i + 1));
// test.number = -1;
// s.push(std::move(test));
// yeet::stack<Test> copy(s);
// std::cout << copy.empty() << std::endl;
// std::cout << copy.size() << std::endl;
// yeet::stack<Test> moved(std::move(s));
// while(!moved.empty())
// {
// std::cout << moved.top().number << std::endl;
// moved.pop();
// }
// }
namespace ifuckedoop{
template<typename T>
class dynarray
{
T *ptr;
std::size_t size, capacity;
void reallocate(std::size_t n);
public:
~dynarray()
{
for(std::size_t i = 0; i < size; ++i)
(ptr + i)->~T();
std::free(ptr);
}
dynarray()
: ptr(nullptr)
, size(0)
, capacity(0)
{}
dynarray(dynarray<T> const &);
dynarray(dynarray<T> &&);
dynarray<T> &operator=(dynarray<T> const &);
dynarray<T> &operator=(dynarray<T> &&);
T *begin() {return ptr;}
T * end() {return ptr + size;}
T const *begin() const {return ptr;}
T const * end() const {return ptr + size;}
template<typename... Args>
void emplace_back(Args &&... args)
{
if(size == capacity)
reallocate(capacity == 0 ? 1 : 2 * capacity);
new (ptr + size++) T(static_cast<Args &&>(args)...);
}
};
template<typename T>
void dynarray<T>::reallocate(std::size_t n){
T *ihateoop = static_cast<T*>(std::malloc(n * sizeof(T)));
for(int i = 0; i < size; i++){
new (ihateoop + i) T(static_cast<T&&>(ptr[i]));
(ptr + i)->~T();
}
std::free(ptr);
ptr = ihateoop;
capacity = n;
}
}
// int main(){
// ifuckedoop::dynarray<Test> array;
// Test test(-2);
// array.emplace_back(test);
// test.number = -1;
// array.emplace_back(std::move(test));
// for(int i = 1; i < 5; ++i)
// array.emplace_back(i);
// array.emplace_back(Test(10));
// for(Test const &test : array)
// std::cout << test.number << ' ';
// std::cout << std::endl;
// }