-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlib.h
261 lines (219 loc) · 5.21 KB
/
mlib.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
#pragma once
#include <memory>
#include <iostream>
namespace cc
{
template <typename K, typename T>
class RB_tree
{
private:
enum
{
RED = 0,
BLACK = 1
};
enum
{
LEFT = 0,
RIGHT = 1
};
class rb_node
{
private:
K key;
T value;
bool color;
rb_node *p_left_child;
rb_node *p_right_child;
public:
rb_node() = delete;
rb_node(const rb_node &node) = delete;
rb_node(K&& _key, T&& _value, bool _color) : key(_key), value(_value), color(_color), p_left_child(nullptr), p_right_child(nullptr) { std::cout << "param\n"; };
rb_node(rb_node &&rv_node);
void insert(rb_node&& node, bool side) { (side == LEFT ? p_left_child = new rb_node(node) : p_right_child = new rb_node(node)); };
T& get_value() { return value; };
~rb_node();
};
rb_node* root;
int count;
public:
RB_tree() : root(nullptr), count(0) {};
void push(K&& _key, T&& _value);
T& get_value(const K& key);
~RB_tree();
};
template <typename K, typename T>
RB_tree<K,T>::rb_node::rb_node(rb_node &&rv_node) {
key = std::move(rv_node.key);
value = std::move(rv_node.value);
color = rv_node.color;
p_left_child = rv_node.p_left_child;
p_right_child = rv_node.p_right_child;
rv_node.p_left_child = nullptr;
rv_node.p_right_child = nullptr;
std::cout << "move-const\n";
}
template <typename K, typename T>
RB_tree<K,T>::rb_node::~rb_node(){
if(p_left_child != nullptr)
delete p_left_child;
if(p_right_child != nullptr)
delete p_right_child;
}
template <typename K, typename T>
void RB_tree<K,T>::push(K&& _key, T&& _value){
if(count == 0){
root = new rb_node(std::forward<K>(_key), std::forward<T>(_value), RED);
++count;
}
}
template <typename K, typename T>
RB_tree<K,T>::~RB_tree(){
if(root != nullptr)
delete root;
}
template <typename K, typename T>
T& RB_tree<K,T>::get_value(const K& key){
return root->get_value();
}
// vector
template <class T>
class vector
{
private:
T *_data;
size_t _count;
size_t _capacity;
struct iterator
{
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T *;
using reference = T &;
iterator(pointer ptr) : _ptr(ptr) {}
reference operator*() const { return *_ptr; }
pointer operator->() { return _ptr; }
iterator &operator++()
{
++_ptr;
return *this;
}
iterator operator++(int)
{
auto res = *this;
++(*this);
return res;
}
friend bool operator==(const iterator &a, const iterator &b) { return a._ptr == b._ptr; };
friend bool operator!=(const iterator &a, const iterator &b) { return a._ptr != b._ptr; };
private:
pointer _ptr;
};
public:
vector();
vector(size_t size);
T &operator[](size_t index);
size_t size();
size_t capacity();
void push_back(const T &item);
iterator begin();
iterator end();
~vector();
};
template <class T>
vector<T>::vector() : _data(nullptr), _count(0), _capacity(0) {}
template <class T>
vector<T>::vector(size_t size) : _data(static_cast<T *>(malloc(size * sizeof(T)))), _count(size), _capacity(size)
{
for (size_t i = 0; i < size; ++i)
new (_data + i) T();
}
template <class T>
T &vector<T>::operator[](size_t index)
{
if (index < 0 || index >= _count)
throw 1;
return _data[index];
}
template <class T>
size_t vector<T>::size()
{
return _count;
}
template <class T>
size_t vector<T>::capacity()
{
return _capacity;
}
template <class T>
void vector<T>::push_back(const T &item)
{
if (_count >= _capacity)
{
++_capacity *= 2;
T *tmp_p = _data;
_data = static_cast<T *>(malloc(_capacity * sizeof(T)));
for (size_t i = 0; i < _count; ++i)
new (_data + i) T(std::move(tmp_p[i]));
for (int i = 0; i < _count; ++i)
(tmp_p + i)->~T();
if (tmp_p != nullptr)
free(tmp_p);
}
new (_data + _count) T(item);
++_count;
}
template <class T>
vector<T>::~vector()
{
for (int i = 0; i < _count; ++i)
(_data + i)->~T();
if (_data != nullptr)
free(_data);
}
template <class T>
typename vector<T>::iterator vector<T>::begin()
{
return iterator(&(_data[0]));
}
template <class T>
typename vector<T>::iterator vector<T>::end()
{
return iterator(&(_data[_count]));
}
};
class tp{
private:
int* p;
int* meta;
public:
tp(){
std::cout << "def constructor\n";
p = new int[10];
meta = new int[5];
};
tp(const tp& item){
std::cout << "copy constructor\n";
p = new int[10];
meta = new int[5];
for(int i =0; i < 10; ++i)
p[i] = item.p[i];
}
tp(tp&& item){
std::cout << "move constructor\n";
p = item.p;
item.p = nullptr;
}
~tp(){
std::cout << "destructor\n";
if(p != nullptr){
std::cout << "data deleted\n";
delete[] p;
}
if(meta != nullptr){
std::cout << "meta deleted\n";
delete[] meta;
}
};
};