-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_vector.h
197 lines (157 loc) · 3.24 KB
/
template_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
#ifndef _VECTOR
#define _VECTOR
#include<stdexcept>
template<typename T>
class Vector {
private:
T* parr;
size_t size;
size_t capacity;
void resize();
void copyArr(const Vector<T>& other);
void emptyArr();
public:
Vector();
Vector(size_t initial_size);
~Vector();
Vector(const Vector<T>& other);
Vector<T>& operator=(const Vector<T>& other);
void insert(size_t index, const T &);
void push_back(const T &);
void push_front(const T &);
void append(const Vector<T>&);
const T& operator[](size_t index) const;
T& operator[](size_t index);
void removeAt(size_t index);
size_t getCapacity() const;
size_t getSize() const;
};
template<typename T>
Vector<T>::Vector() {
parr = new T[capacity = 1];
size = 0;
}
template<typename T>
Vector<T>::Vector(size_t initial_size) {
parr = new T[capacity = initial_size];
size = 0;
}
template<typename T>
void Vector<T>::emptyArr() {
size = 0;
capacity = 1;
delete[] parr;
parr = nullptr;
}
template<typename T>
Vector<T>:: ~Vector() {
emptyArr();
}
template<typename T>
void Vector<T>::copyArr(const Vector<T>& other) {
size_t len = other.getSize();
for (size_t i = 0; i < len; ++i) {
parr[i] = other[i];
}
}
template<typename T>
Vector<T>::Vector(const Vector<T>& other) {
capacity = other.capacity;
size = other.getSize();
parr = new T[capacity];
copyArr(other);
}
template<typename T>
void Vector<T>::resize() {
size_t newCapacity = capacity << 1;
T* pnewArr = new T[newCapacity];
for (size_t i = 0; i < size; ++i) {
pnewArr[i] = parr[i];
}
delete[] parr;
parr = pnewArr;
capacity = newCapacity;
}
template<typename T>
size_t Vector<T>::getCapacity() const{
return capacity;
}
template<typename T>
size_t Vector<T>::getSize() const{
return size;
}
template<typename T>
Vector<T>& Vector<T>::operator=(const Vector<T>& other) {
if (this != &other) {
emptyArr();
capacity = other.getCapacity();
size = other.getSize();
parr = new T[capacity];
copyArr(other);
}
return *this;
}
template<typename T>
void Vector<T>::insert(size_t index, const T& item) {
if (index <= size) {
if (size >= capacity) {
resize();
}
if (size > 0) {
for (size_t i = size - 1; i > index; --i) {
parr[i + 1] = parr[i];
}
}
parr[index] = item;
++size;
}
else {
throw std::invalid_argument("Index out of range");
}
}
template<typename T>
void Vector<T>::push_back(const T & item){
insert(size, item);
}
template<typename T>
void Vector<T>::push_front(const T & item){
insert(0, item);
}
template<typename T>
void Vector<T>::append(const Vector<T>& other) {
size_t len = other.getSize();
for (size_t i = 0; i < len; ++i) {
push_back(other[i]);
}
}
template<typename T>
const T & Vector<T>::operator[](size_t index) const{
if (index < size) {
return parr[index];
}
else {
throw std::invalid_argument("Index out of range");
}
}
template<typename T>
T & Vector<T>::operator[](size_t index){
if (index < capacity) {
return parr[index];
}
else {
throw std::invalid_argument("Index out of range");
}
}
template<typename T>
void Vector<T>::removeAt(size_t index){
if (index < size) {
for (size_t i = index; i < size - 1; ++i) {
parr[i] = parr[i + 1];
}
--size;
}
else {
throw std::invalid_argument("Index out of range");
}
}
#endif //!_VECTOR