-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3.cpp
More file actions
191 lines (163 loc) · 3.76 KB
/
vector3.cpp
File metadata and controls
191 lines (163 loc) · 3.76 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
#include "vector3.h"
template<typename T, typename A>
vector_base<T, A>::vector_base(const vector_base& arg)
{
T* p = alloc.allocate(arg.sz);
for (int i = 0; i < arg.sz; ++i)
{
alloc.construct(&p[i], arg.elem[i]);
}
elem = p;
sz = arg.sz;
space = arg.space;
}
template<typename T, typename A>
vector_base<T, A>::vector_base( vector_base&& arg)
{
elem = arg.elem;
sz = arg.sz;
space = arg.space;
arg.elem = nullptr;
arg.sz = arg.space = 0;
}
template<typename T, typename A>
vector_base<T, A>::vector_base(std::initializer_list<T> lst)
:sz{ 0 },
elem{ alloc.allocate(static_cast<int>(lst.size())) },
space{ static_cast<int>(lst.size()) }
{
std::copy(lst.begin(), lst.end(), elem);
}
template<typename T, typename A>
vector3<T, A>::vector3()
: vector_base<T, A>() {}
template<typename T, typename A>
vector3<T, A>::vector3(int s)
: vector_base<T, A>(s)
{
for (int i = 0; i < s; ++i) this->alloc.construct(&this->elem[i], T());
}
template<typename T, typename A>
vector3<T, A>::vector3(std::initializer_list<T> lst)
: vector_base<T, A>(lst)
{
std::copy(lst.begin(), lst.end(), this->elem);
}
template<typename T, typename A >
vector3<T, A>::vector3(const vector3<T, A>& arg)
: vector_base<T, A>(arg) {}
template<typename T, typename A>
vector3<T, A>& vector3<T, A>::operator=(const vector3<T, A>& a)
{
if (this == &a)
{
return *this;
}
if (a.sz <= this->space)
{
for (int i = 0; i < a.sz; ++i)
{
this->elem[i] = a.elem[i];
}
this->sz = a.sz;
return *this;
}
T* p = this->alloc.allocate(this->sz);
for (int i = 0; i < a.sz; ++i)
{
p[i] = a.elem[i];
}
this->alloc.destroy(this->elem);
this->space = this->sz = a.sz;
this->elem = p;
return *this;
}
template<typename T, typename A>
vector3<T, A>::vector3(vector3<T, A>&& a)
{
this->sz = a.sz;
this->elem = a.elem;
this->space = this->sz;
a.sz = 0;
a.elem = nullptr;
a.space = a.sz;
}
template<typename T, typename A>
vector3<T, A>& vector3<T, A>::operator=(vector3<T, A>&& a)
{
this->alloc.destroy(this->elem);
this->elem = a.elem;
this->sz = a.sz;
a.elem = nullptr;
a.sz = 0;
return *this;
}
template<typename T, typename A>
T& vector3<T, A>::at(int n)
{
if (n <= 0 || this->sz > n) return this->elem[n];
throw Range_error(n);
}
template<typename T, typename A>
const T& vector3<T, A>::at(int n) const
{
if (n <= 0 || this->sz > n) return this->elem[n];
throw Range_error(n);
}
template<typename T, typename A>
void vector3<T, A>::resize(int newsize, T val)
{
reserve(newsize);
for (int i = this->sz; i < newsize; i++)
{
this->alloc.construct(&this->elem[i], val);
}
for (int i = 0; i < newsize; i++)
{
this->alloc.destroy(&this->elem[i]);
}
this->sz = newsize;
}
template<typename T, typename A>
void vector3<T, A>::push_back(const T& val)
{
if (!this->space)
{
reserve(8);
}
else
{
if (this->sz == this->space)
{
reserve(2 * this->space);
}
}
this->alloc.construct(&this->elem[this->sz], val);
++this->sz;
}
template<typename T, typename A>
void vector3<T, A>::reserve(int newalloc)
{
if (newalloc <= this->space)
{
return;
}
vector_base<T, A> b(this->alloc, newalloc);
std::uninitialized_copy(this->elem,this->elem+this->sz, b.elem);
b.sz = this->sz;
for (int i = 0; i < this->sz; ++i)
{
this->alloc.destroy(&this->elem[i]);
}
this->alloc.deallocate(this->elem, this->space);
this->elem = b.elem;
this->sz = b.sz;
this->space = b.space;
b.elem = nullptr;
b.sz = b.space = 0;
//std::swap<vector_base<T, A>>(*this, b);
}
template class vector3<int>;
template class vector3<double>;
template class vector3<char>;
template class vector3<std::string>;