-
Notifications
You must be signed in to change notification settings - Fork 0
/
uy_vector.h
291 lines (241 loc) · 7.51 KB
/
uy_vector.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
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
#include <iostream>
#include <new>
#include <string.h>
#include <algorithm>
#include "uy_allocator_2.h"
#include "simple_alloc.h"
using namespace std;
template<class T> fill(T* pos,size_t n,const T& target)
{
for(size_t i=0;i<n;++i)
{
*pos = target;
++pos;
}
}
template <class T1,class T2>
inline void construct(T1* p,const T2& target)
{
//placement new , 在已分配内存上构造对象
new (p) T1 (target);
}
template <class T1,class T2>
inline T2 uninitialized_copy(T1 first,T1 last,T2 result_first)
{
return copy(first,last,result_first);
}
//适配非基础数据类型进行析构
template <class T>
inline void destroy(T* p)
{
p->~T();
}
template <class iterator>
inline void destroy(iterator start,iterator finish)
{
for(;start < finish;++start)
{
destroy(start);
}
}
// template <class iterator,class Size,class T>
// inline iterator uninitialized_fill_n(iterator start,iterator finish,const T& target)
// {
// for(;start < finish;++ start)
// {
// construct(start,target);
// }
// return start;
// }
template <class iterator,class Size,class T>
inline iterator uninitialized_fill_n(iterator start,Size n,const T& target)
{
iterator finish = start + n;
for(;start < finish;++ start)
{
construct(start,target);
}
return start;
}
template <class value_type,class Alloc = uy_allocator_2<0> >
class uy_vector
{
public:
//迭代器直接使用指针
typedef value_type* iterator;
typedef value_type& reference;
typedef ptrdiff_t difference_type;
uy_vector():start(0),finish(0),end_of_storage(0){}
uy_vector(size_t n,const value_type& target){ fill_initialized(n,target);}
uy_vector(int n,const value_type& target){ fill_initialized(n,target);}
uy_vector(long n,const value_type& target){ fill_initialized(n,target);}
explicit uy_vector(size_t n){ fill_initialized(n,value_type());}
~uy_vector()
{
destroy(start,finish);
deallocate();
}
iterator begin() {return start;}
iterator end() {return finish;}
size_t size() const {return finish - start;}
size_t capacity() const {return end_of_storage - start;}
bool empty() const {return finish == start;}
reference operator [] (size_t n) {return *(start + n);}
reference front() {return *begin();};
reference back() {return *(end()-1);}
void push_back(const value_type& target)
{
if(end() != end_of_storage)
{
*end() = target;
++ finish;
}else{
insert_aux(end(),target);
}
}
void pop_back()
{
--finish;
destroy(finish);
}
iterator erase(iterator pos)
{
destroy(pos);
if(pos != end() -1)copy(pos + 1,end() ,pos);
--finish;
destroy(finish);
return pos;
}
iterator erase(iterator first,iterator last)
{
destroy(first,last);
iterator i = copy(last,end(),first);
destroy(i,end());
finish = finish - (last - first);
return first;
}
void clear() { erase(begin(),end());}
void insert_aux(iterator pos,const value_type& target);
void insert(iterator pos,size_t n,const value_type& target);
void resize(size_t new_size,const value_type& target)
{
if(new_size < size())
{
erase(begin() + new_size,end());
}else{
insert(end(),new_size - size(),target);
}
}
void resize(size_t new_size)
{
resize(new_size,value_type());
}
protected:
//使用两级空间配置器 uy_allocator_2 封装的 simple_alloc
typedef simple_alloc <value_type,Alloc> data_allocator;
iterator start;
iterator finish;
iterator end_of_storage;
void deallocate()
{
if(start)data_allocator::deallocate(start,end_of_storage-start);
}
void fill_initialized(size_t n,const value_type& target)
{
start = data_allocator::allocate(n);
uninitialized_fill_n(start,n,target);
finish = start + n;
end_of_storage = finish;
}
};
template <class value_type,class Alloc>
void uy_vector<value_type,Alloc> :: insert_aux(iterator pos,const value_type& target)
{
if(finish != end_of_storage)
{
//有可用空间,直接插入
construct(finish,*(finish - 1));
++finish;
copy_backward(pos,finish - 2,finish - 1);
*pos = target;
}else{
//无可用空间时,申请新空间->复制原有对象->插入新对象->析构旧空间上的对象->释放旧的内存空间
const size_t old_size = size();
//扩容策略采用增大为原来的两倍
const size_t len = old_size != 0 ? (2 * old_size) : 1;
//iterator new_start = (iterator)malloc(sizeof(value_type)*len);
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
//cout<<"**"<<*new_start<<"**"<<endl;
try
{
new_finish = uninitialized_copy(start,pos,new_start);
// cout<<"*"<<new_finish<<endl;
//iterator old_finish_pos = new_finish;
construct(new_finish,target);
//*old_finish_pos = target;
//cout<<"*"<<*(new_finish-1)<<" "<<"target = "<<target<<"*"<<endl;
++ new_finish;
new_finish = uninitialized_copy(pos,finish,new_finish);
}catch (...){
destroy(new_start,new_finish);
data_allocator::deallocate(new_start,len);
cout<<"err out of memory"<<endl;
throw;
}
destroy(start,finish);
deallocate();
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}
}
//need test
template <class value_type,class Alloc>
void uy_vector<value_type,Alloc>::insert(iterator pos,size_t n,const value_type& target)
{
if(n != 0)
{
if(end_of_storage - finish >= n)
{
const size_t elems_after = finish - pos;
const iterator old_finish = finish;
if(elems_after > n)
{
uninitialized_copy(finish - n,finish,finish);
finish += n;
copy_backward(pos,old_finish - n,old_finish);
fill(pos,pos+n,target);
}else{
uninitialized_fill_n(finish,n - elems_after,target);
finish += n-elems_after;
uninitialized_copy(pos,old_finish,finish);
finish += elems_after;
fill(pos,old_finish,target);
}
// copy_backward(pos,finish,pos+n);
// fill(pos,n,target);
// finish = finish + n;
}else{
const size_t old_size = size();
const size_t len = capacity() + max(capacity(),n);
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
try
{
new_finish = uninitialized_copy(start,pos,new_start);
new_finish = uninitialized_fill_n(new_finish,n,target);
new_finish = uninitialized_copy(pos,finish,new_finish);
}catch (...){
destroy(new_start,new_finish);
data_allocator::deallocate(new_start,len);
throw;
}
destroy(start,finish);
deallocate();
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}
}
}