forked from averywang21/CS106L-201920
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myvector.h
210 lines (173 loc) · 5.21 KB
/
myvector.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
#ifndef MYVECTOR_H
#define MYVECTOR_H
#include <string>
#include <algorithm>
template <class ValueType>
class MyVector {
public:
using iterator = ValueType*;
using const_iterator = const ValueType*;
using size_type = size_t;
// Default constructor
MyVector();
// Fill constructor, second argument optional
explicit MyVector(size_type n, const ValueType& val = ValueType());
// Rule of three:
// copy constructor, copy assignment, and destructor
MyVector(const MyVector& rhs);
MyVector& operator=(const MyVector& rhs);
MyVector(const MyVector&& rhs);
MyVector& operator=(const MyVector&& rhs);
~MyVector();
size_type size() const;
bool empty() const;
ValueType& operator[](size_type indx);
const ValueType& operator[](size_type indx) const ;
ValueType& at(size_type indx);
const ValueType& at(size_type indx) const;
iterator insert(iterator pos, const ValueType& elem);
void push_back(const ValueType& elem);
iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;
private:
void grow();
ValueType* elems;
size_type logicalSize;
size_type allocatedSize;
const size_type kInitialSize = 10;
};
template <class ValueType>
MyVector<ValueType>::MyVector() :
logicalSize(0), allocatedSize(kInitialSize)
{
elems = new ValueType[allocatedSize];
}
template <class ValueType>
MyVector<ValueType>::MyVector(size_type n, const ValueType &val) :
logicalSize(n), allocatedSize(2*n)
{
elems = new ValueType[allocatedSize];
std::fill(begin(), end(), val);
}
template <class ValueType>
MyVector<ValueType>::MyVector(const MyVector& rhs) :
logicalSize(rhs.logicalSize), allocatedSize(rhs.allocatedSize)
{
elems = new ValueType[allocatedSize];
std::copy(rhs.begin(), rhs.end(), begin());
}
template <class ValueType>
MyVector<ValueType>::MyVector(const MyVector&& rhs) :
logicalSize(rhs.logicalSize), allocatedSize(rhs.allocatedSize)
{
elems = rhs.elems;
rhs.elems = nullptr;
}
/*
* Can't use initialiser list here because this is not
* a constructor. It also wouldn't make sense since
* the members in this object should already be initialised
* if the copy assignment operator is being used.
*/
template <class ValueType>
MyVector<ValueType>& MyVector<ValueType>::operator =(const MyVector& rhs) {
// Make sure we don't self assign
if(this != &rhs) {
delete[] elems;
logicalSize = rhs.logicalSize;
allocatedSize = rhs.allocatedSize;
elems = new ValueType[allocatedSize];
std::copy(rhs.begin(), rhs.end(), begin());
}
return *this;
}
template <class ValueType>
MyVector<ValueType>& MyVector<ValueType>::operator =(const MyVector&& rhs) {
if (this != &rhs) {
logicalSize = rhs.logicalSize;
allocatedSize = rhs.allocatedSize;
elems = rhs.elems;
}
return *this;
}
template <class ValueType>
MyVector<ValueType>::~MyVector() {
delete[] elems;
}
template <class ValueType>
typename MyVector<ValueType>::iterator MyVector<ValueType>::begin() {
return elems;
}
template <class ValueType>
typename MyVector<ValueType>::const_iterator MyVector<ValueType>::begin() const {
return elems;
}
template <class ValueType>
typename MyVector<ValueType>::iterator MyVector<ValueType>::end() {
return elems + size();
}
template <class ValueType>
typename MyVector<ValueType>::const_iterator MyVector<ValueType>::end() const {
return elems + size();
}
template <class ValueType>
typename MyVector<ValueType>::size_type MyVector<ValueType>::size() const {
return logicalSize;
}
template <class ValueType>
bool MyVector<ValueType>::empty() const {
return size() == 0;
}
template <class ValueType>
ValueType& MyVector<ValueType>::at(size_type index) {
if(index >= size()) {
throw std::out_of_range("Out of bounds access of vector");
}
return *(begin() + index);
}
template <class ValueType>
const ValueType& MyVector<ValueType>::at(size_type index) const{
if(index >= size()) {
throw std::out_of_range("Out of bounds access of vector");
}
return *(begin() + index);
}
template <class ValueType>
ValueType& MyVector<ValueType>::operator[](size_type index) {
return *(begin() + index);
}
template <class ValueType>
const ValueType& MyVector<ValueType>::operator[](size_type index) const {
return *(begin() + index);
}
template <class ValueType>
void MyVector<ValueType>::grow() {
iterator new_elems = new ValueType[2*allocatedSize];
std::copy(begin(), end(), new_elems);
delete[] elems;
allocatedSize *= 2;
elems = new_elems;
}
template <class ValueType>
typename MyVector<ValueType>::iterator MyVector<ValueType>::insert(iterator pos, const ValueType &elem) {
size_type indx = pos - begin();
if(size() == allocatedSize) {
grow();
}
/*
* If we grew, we need pos to point to the new array
* since the previous address of pos would be invalid
*/
pos = begin() + indx;
std::copy_backward(pos, end(), end() + 1);
*pos = elem;
++logicalSize;
return pos;
}
template <class ValueType>
void MyVector<ValueType>::push_back(const ValueType &elem) {
insert(end(), elem);
}
#endif // MYVECTOR_H