-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd_vector.cpp
More file actions
176 lines (141 loc) · 3.98 KB
/
Copy pathstd_vector.cpp
File metadata and controls
176 lines (141 loc) · 3.98 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
#include<iostream>
#include<initializer_list>
#include<string>
template<typename T>
class Vector {
public:
Vector(int size) {
m_size_allocated = size;
realloc(m_size_allocated);
size = 0;
}
Vector(std::initializer_list<T> values) {
int i = 0;
realloc(values.size());
for (auto& value : values) {
m_data[i] = value;
i++;
}
m_size = values.size();
}
Vector() {
realloc(2);
}
~Vector() {
clear();
::operator delete(m_data, m_size_allocated * sizeof(T));
std::cout << "Destroyed vector" << std::endl;
}
// for debuggin purpose
const T& operator[](unsigned int index) const {
std::cout << "const version" << std::endl;
return m_data[index];
}
T& operator[](unsigned int index) {
return m_data[index];
}
void push_back(const T& value) {
if(m_size >= m_size_allocated) {
realloc(m_size_allocated + m_size_allocated/2);
}
m_data[m_size] = value;
m_size++;
}
void push_back(T&& value) {
if(m_size >= m_size_allocated) {
realloc(m_size_allocated + m_size_allocated/2);
}
// though value is a r-value but when it enters the function it becomse a l-value so we need to type cast it back into a r-value
m_data[m_size] = std::move(value);
m_size++;
}
template<typename... Args>
T& emplace_back(Args&&... args) {
if(m_size >= m_size_allocated) {
realloc(m_size_allocated + m_size_allocated/2);
}
new(&m_data[m_size]) T(std::forward<Args>(args)...);
return m_data[m_size++];
}
void pop_back() {
if(m_size > 0) {
m_data[m_size--].~T();
}
}
void clear() {
for(;m_size;) {
m_data[--m_size].~T();
}
}
int size() {
return m_size;
}
private:
unsigned int m_size = 0;
unsigned int m_size_allocated = 0;
T* m_data = nullptr;
void realloc(unsigned int new_size) {
std::cout << "Triggered vector resize" << std::endl;
T* new_data = (T*)::operator new(new_size * sizeof(T));
for (int i=0; i<m_size; i++) {
new(&new_data[i]) T(std::move(m_data[i]));
m_data[i].~T();
}
::operator delete(m_data, sizeof(m_size));
m_data = new_data;
m_size_allocated = new_size;
};
};
class Vector3 {
public:
float x = 0.0f, y = 0.0f, z = 0.0f;
int* mem_block;
Vector3() {};
Vector3(float x, float y, float z)
: x(x), y(y), z(z) {
mem_block = new int[5];
std::cout << "constructor Created" << std::endl;
}
Vector3(Vector3& other) = delete;
Vector3(Vector3&& other)
: x(other.x), y(other.y), z(other.z) {
mem_block = other.mem_block;
other.mem_block = nullptr;
std::cout << "constructor Moved" << std::endl;
}
~Vector3() {
delete[] mem_block;
std::cout << "Destroyed vector3" << std::endl;
}
Vector3& operator=(Vector3&& other) {
std::cout << "Moved " << std::endl;
delete[] mem_block;
mem_block = other.mem_block;
other.mem_block = nullptr;
x = other.x;
y = other.y;
z = other.z;
return *this;
}
Vector3& operator=(const Vector3& other) = delete;
};
int main(){
if(false) {
Vector<std::string> vec;
vec.push_back(std::string("HELLO"));
Vector<std::string> vec1 = {"Ali", "Asgar"};
}
Vector<Vector3> vec;
{
Vector3& hello = vec.emplace_back(1.0f, 1.0f, 1.0f);
vec.emplace_back(1.0f, 1.0f, 1.0f);
vec.emplace_back(1.0f, 1.0f, 1.0f);
vec.emplace_back(1.0f, 1.0f, 1.0f);
vec.emplace_back(2.0f, 1.0f, 1.0f);
for (int i=0; i<vec.size(); i++) {
std::cout << vec[i].x << std::endl;
}
std::cin.get();
}
return 0;
}