forked from CUST-ACM/CUSTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
125 lines (108 loc) · 3.41 KB
/
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
/* file: vector.h
* date: 2018/04/28
* author: axp (Xiao Peng)
*/
#ifndef _CUSTL_VECTOR_H
#define _CUSTL_VECTOR_H
#include "allocator.h"
namespace custl {
template<typename T, typename Alloc = allocator<T> >
class vector {
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
protected:
typedef Alloc data_allocator;
iterator start;
iterator finish;
iterator end_of_storage;
void insert_aux(iterator position, const T& x);
void deallocate() {
if (start)
data_allocator::deallocate(start, end_of_storage - start);
}
void fill_initialize(size_type n, const T& x) {
start = allocate_and_fill(n, x);
finish = start + n;
end_of_storage = finish;
}
iterator allocate_and_fill(size_type n, const T& x) {
iterator result = data_allocator::allocate(n);
uninitialized_fill_n(result, n, x);
return result;
}
public:
vector() : start(0), finish(0), end_of_storage(0) {}
~vector(){
destory(start, finish);
deallocate();
}
size_type size() const { return size_type(finish - start); }
size_type capacity() const { return size_type(end_of_storage - start); }
bool empty() const { return start == finish; }
iterator begin() { return start; }
iterator end() { return finish; }
reference front() { return *start; }
reference back() { return *(finish - 1); }
reference operator[](size_type n) { return *(start + n); }
void push_back(const value_type& x) {
if (finish != end_of_storage) {
construct(finish, x);
++finish;
} else {
insert_aux(finish, x);
}
}
void pop_back() {
--finish;
destory(finish);
}
iterator insert(const_iterator position, const value_type& x) {
int n = position - begin();
insert_aux(position, x);
return begin() + n;
}
};
//TODO(axp)
//insert和insert_aux先随便写个了,之后再更
template<typename T, typename Alloc>
void vector<T, Alloc>::insert_aux(iterator position, const T& x) {
if (finish != end_of_storage) {
construct(finish, *(finish - 1));
++finish;
for (iterator it = position + 1; it < finish; ++it)
*it = *(it - 1);
*position = x;
} else {
const size_type old_size = size();
const size_type new_size = old_size == 0 ? 1 : 2 * old_size;
iterator new_start = data_allocator::allocate(new_size);
iterator new_finish = new_start;
for (iterator it = begin(); it != end(); ++it)
{
if (it == position) {
*new_finish = x;
++new_finish;
}
*new_finish = *it;
++new_finish;
}
if (position == end()) {
*new_finish = x;
++new_finish;
}
destory(start, finish);
deallocate();
start = new_start;
finish = new_finish;
end_of_storage = new_start + new_size;
}
}
}; // namespace custl
#endif // _CUSTL_VECTOR_H