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
|
#include <iostream>
template <class T>
class DynamicArray {
T *values;
int lastUnused;
int allocated;
void free();
void copyFrom(const DynamicArray<T>& other);
void resize();
public:
DynamicArray();
~DynamicArray();
DynamicArray(const DynamicArray<T>& other);
DynamicArray<T>& operator=(const DynamicArray<T>& other);
DynamicArray(DynamicArray<T>&& other);
DynamicArray<T>& operator=(DynamicArray<T>&& other);
void Add(const T& newValue);
// Тези оператори променят аргумента си, затова е по-добре да са член-функции
DynamicArray<T>& operator+=(const DynamicArray<T>& other);
DynamicArray<T>& operator+=(const T& newValue);
// По дефиниция не може operator=, operator(), operator[] и operator-> да бъдат имплементирани
// като приятелски функции
T& operator[](int index);
// Тези оператори не променят аргументите си, затова е по-добре да са приятелски
template <class U>
friend DynamicArray<U> operator+(const DynamicArray<U>& left, const DynamicArray<U>& right);
template <class U>
friend DynamicArray<U> operator+(const DynamicArray<U>& left, const U& right);
template <class U>
friend bool operator==(const DynamicArray<U>& left, const DynamicArray<U>& right);
template <class U>
friend bool operator!=(const DynamicArray<U>& left, const DynamicArray<U>& right);
template <class U>
friend std::ostream& operator<<(std::ostream& ostr, const DynamicArray<U>& right);
template <class U>
friend std::istream& operator>>(std::istream& ostr, DynamicArray<U>& right);
};
template <class T>
void DynamicArray<T>::free() {
delete[] values;
}
template <class T>
void DynamicArray<T>::copyFrom(const DynamicArray<T>& other) {
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
this->values = new T[allocated];
for (int i = 0; i < this->lastUnused; i++) {
this->values[i] = other.values[i];
}
}
template <class T>
void DynamicArray<T>::resize() {
allocated *= 2;
T* moreValues = new T[allocated];
for (int i = 0; i < lastUnused; i++) {
moreValues[i] = values[i];
}
delete[] values;
values = moreValues;
}
template <class T>
DynamicArray<T>::DynamicArray() {
values = nullptr;
lastUnused = allocated = 0;
}
template <class T>
DynamicArray<T>::~DynamicArray() {
free();
}
template <class T>
DynamicArray<T>::DynamicArray(const DynamicArray<T>& other) {
copyFrom(other);
}
template <class T>
DynamicArray<T>& DynamicArray<T>::operator=(const DynamicArray<T>& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
template <class T>
DynamicArray<T>::DynamicArray(DynamicArray<T>&& other) {
this->values = other.values;
other.values = nullptr;
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
}
template <class T>
DynamicArray<T>& DynamicArray<T>::operator=(DynamicArray<T>&& other) {
if (this != &other) {
free();
this->values = other.values;
other.values = nullptr;
this->lastUnused = other.lastUnused;
this->allocated = other.allocated;
}
return *this;
}
template <class T>
void DynamicArray<T>::Add(const T& number) {
if (allocated == lastUnused) {
resize();
}
values[lastUnused++] = number;
}
template <class T>
DynamicArray<T>& DynamicArray<T>::operator+=(const DynamicArray<T>& other) {
for (int i = 0; i < other.lastUnused; i++) {
this->Add(other.values[i]);
}
return *this;
}
template <class T>
DynamicArray<T>& DynamicArray<T>::operator+=(const T& number) {
for (int i = 0; i < lastUnused; i++) {
values[i] += number;
}
return *this;
}
template <class T>
T& DynamicArray<T>::operator[](int index) {
if (index < 0 || lastUnused <= index) {
throw "Invalid index!";
}
return values[index];
}
template <class T>
DynamicArray<T> operator+(const DynamicArray<T>& left, const DynamicArray<T>& right) {
DynamicArray<T> output;
(output += left) += right;
return output;
}
template <class T>
DynamicArray<T> operator+(const DynamicArray<T>& left, T right) {
DynamicArray<T> output = left; // Копиращ конструктор
output += right;
return output;
}
template <class T>
bool operator==(const DynamicArray<T>& left, const DynamicArray<T>& right) {
if (left.lastUnused != right.lastUnused) {
return false;
}
for (int i = 0; i < left.lastUnused; i++) {
if (left.values[i] != right.values[i]) {
return false;
}
}
return true;
}
template <class T>
bool operator!=(const DynamicArray<T>& left, const DynamicArray<T>& right) {
return !(left == right);
}
template <class T>
std::ostream& operator<<(std::ostream& ostr, const DynamicArray<T>& right) {
for (int i = 0; i < right.lastUnused; i++) {
ostr << right.values[i] << " ";
}
return ostr;
}
template <class T>
std::istream& operator>>(std::istream& istr, DynamicArray<T>& right) {
int length;
istr >> length;
right.lastUnused = 0;
for (int i = 0; i < length; i++) {
T temp;
istr >> temp;
right.Add(temp);
}
return istr;
}
|