-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
185 lines (178 loc) · 3.84 KB
/
vector.cpp
File metadata and controls
185 lines (178 loc) · 3.84 KB
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
#pragma once
#include <stdexcept>
template <typename T>
void TVector<T>::Init0() {
Size = 0;
Capacity = 0;
Data = nullptr;
}
template <typename T>
TVector<T>::TVector() {
Init0();
}
template <typename T>
TVector<T>::TVector(unsigned long long size) {
if (size == 0) {
Init0();
return;
}
this -> Size = size;
Capacity = size;
Data = new T[Capacity];
}
template <typename T>
TVector<T>::TVector(unsigned long long size, T value) {
if (size == 0) {
Init0();
return;
}
this -> Size = size;
Capacity = size;
Data = new T[Capacity];
for (unsigned long long i = 0; i < size; i++) {
Data[i] = value;
}
}
template <typename T>
void TVector<T>::Assign(T value) {
for (unsigned long long i = 0; i < Size; ++i) {
Data[i] = value;
}
}
template <typename T>
void TVector<T>::Reallocate(unsigned long long newcapacity) {
T * newdata = new T[newcapacity];
for (unsigned long long i = 0; i < Size; ++i) {
newdata[i] = Data[i];
}
if (Data != nullptr) {
delete[] Data;
}
Data = newdata;
Capacity = newcapacity;
}
template <typename T>
void TVector<T>::PushBack(T value) {
if (Size == Capacity) {
if (Size != 0)
Reallocate(Size * 2);
else
Reallocate(1);
}
Data[Size] = value;
++Size;
}
template <typename T>
T & TVector<T>::operator[](unsigned long long index) {
if (index >= Size) {
throw std::out_of_range("Vector outside of range!");
} else {
return Data[index];
}
}
template <typename T>
void TVector<T>::PopBack() {
if (Size != 0) {
Size--;
if (Size * 4 < Capacity) {
Reallocate(Size * 2);
}
}
return;
}
template <typename T>
void TVector<T>::Swap(TVector & other) {
T * tempdata = Data;
int tempsize = Size;
int tempcap = Capacity;
Data = other.Data;
Size = other.Size;
Capacity = other.Capacity;
other.Data = tempdata;
other.Size = tempsize;
other.Capacity = tempcap;
}
template <typename T>
unsigned long long TVector<T>::Length() const {
return Size;
}
template <typename T>
void TVector<T>::Clear() {
if (Data != nullptr) {
delete[] Data;
}
Size = 0;
Capacity = 0;
Data = nullptr;
}
template <typename T>
TVector<T>::~TVector() {
Clear();
}
template <>
TVector<char>::TVector(unsigned long long size) {
if (size == 0) {
Init0();
return;
}
this -> Size = size;
Capacity = size + 1;
Data = new char[Capacity];
}
template <>
void TVector<char>::Reallocate(unsigned long long newcapacity) {
char * newdata = new char[newcapacity];
for (unsigned long long i = 0; i < Size; ++i) {
newdata[i] = Data[i];
}
if (Data != nullptr) {
delete[] Data;
}
Data = newdata;
Capacity = newcapacity;
}
template <>
void TVector<char>::PushBack(char value) {
if (Size + 1 >= Capacity) {
if (Capacity > 0)
Reallocate(Capacity * 2);
else
Reallocate(2);
}
Data[Size] = value;
Data[Size + 1] = 0;
++Size;
}
template <>
void TVector<char>::PopBack() {
if (Size != 0) {
--Size;
if (Size * 4 < Capacity) {
Reallocate(Size * 2);
}
Data[Size] = 0;
}
return;
}
template <>
char & TVector<char>::operator[](unsigned long long index) {
if (index > Capacity) {
throw std::out_of_range("String outside of allocated space!");
} else {
return Data[index];
}
}
template <>
TVector<char>::TVector(unsigned long long size, char value) {
if (size == 0) {
Init0();
return;
}
this -> Size = size;
Capacity = size + 1;
Data = new char[Capacity];
for (unsigned long long i = 0; i < size; i++) {
Data[i] = value;
}
Data[Size] = 0;
}