-
Notifications
You must be signed in to change notification settings - Fork 77
/
vector.cpp
190 lines (153 loc) · 3.63 KB
/
vector.cpp
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
template <class T>
class Vector {
public:
typedef T* iterator;
Vector();
Vector(size_t size);
Vector(size_t size, const T & initial);
Vector(const Vector<T>& v);
~Vector();
Vector<T> & operator = (const Vector<T> &);
size_t capacity() const;
size_t size() const;
bool empty() const;
const iterator begin() const;
iterator begin();
const iterator end() const;
iterator end();
// both form should be implemented otherwise some STL functions won't work
const T& front() const;
T& front();
const T& back() const;
T& back();
void push_back(const T& value);
void pop_back();
void reserve(size_t capacity);
void resize(size_t size);
T & operator[](size_t index) const;
void clear();
private:
size_t _size;
size_t _capacity;
T* buffer;
};
template<class T>
Vector<T>::Vector() {
_capacity = 0;
_size = 0;
buffer = 0;
}
template<class T>
Vector<T>::Vector(const Vector<T> & v) {
_size = v._size;
_capacity = v._capacity;
buffer = new T[_size];
copy(v.buffer, v.buffer + _size, buffer);
}
template<class T>
Vector<T>::Vector(size_t size) {
_size = size;
_capacity = size;
buffer = new T[_capacity];
}
template <class T>
bool Vector<T>:: empty() const {
return _size == 0;
}
template<class T>
Vector<T>::Vector(size_t size, const T& initial) {
_size = size;
_capacity = size;
buffer = new T [_capacity];
fill(buffer, buffer + _size, initial);
}
template<class T>
Vector<T>& Vector<T>::operator = (const Vector<T> & v) {
free(buffer);
_size = v._size;
_capacity = v._capacity;
buffer = new T [_capacity];
copy(v.buffer, v.buffer + _size, buffer);
return *this;
}
template<class T>
const typename Vector<T>::iterator Vector<T>::begin() const {
return buffer;
}
template<class T>
typename Vector<T>::iterator Vector<T>::begin() {
return buffer;
}
template<class T>
const typename Vector<T>::iterator Vector<T>::end() const {
return buffer + _size;
}
template<class T>
typename Vector<T>::iterator Vector<T>::end() {
return buffer + _size;
}
template<class T>
const T& Vector<T>::front() const {
return buffer[0];
}
template<class T>
T& Vector<T>::front() {
return buffer[0];
}
template<class T>
const T& Vector<T>::back() const {
return buffer[_size - 1];
}
template<class T>
T& Vector<T>::back() {
return buffer[_size - 1];
}
template<class T>
void Vector<T>::push_back(const T & v) {
/*
Incidentally, one common way of regrowing an array is to double the size as needed.
This is so that if you are inserting n items at most only O(log n) regrowths are performed
and at most O(n) space is wasted.
*/
if (_size >= _capacity) {
if(_capacity == 0)
reserve(_capacity + 1);
else
reserve(_capacity * 2);
}
buffer [_size++] = v;
}
template<class T>
void Vector<T>::pop_back() {
(reinterpret_cast<T*>(buffer)[_size - 1]).~T();
_size--;
}
template<class T>
void Vector<T>::reserve(size_t capacity) {
T* newBuffer = new T[capacity];
copy(buffer, buffer + _size, newBuffer);
_capacity = capacity;
free(buffer);
buffer = newBuffer;
}
template<class T>
size_t Vector<T>::size() const {
return _size;
}
template<class T>
void Vector<T>::resize(size_t size) {
reserve(size);
_size = size;
}
template<class T>
T& Vector<T>::operator[](size_t index) const {
return buffer[index];
}
template<class T>
size_t Vector<T>::capacity() const {
return _capacity;
}
template<class T>
Vector<T>::~Vector() {
free(buffer);
}