This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array.cpp
192 lines (169 loc) · 3.81 KB
/
Array.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
191
192
//Artem Liakhov
#ifndef MATRIX_ARRAY_CPP
#define MATRIX_ARRAY_CPP
#include "Array.h"
//Constructors and destructor
template<class T> Array<T>::Array(size_t alloc) {
data_ = new T[alloc + 10];
allocated_ = alloc + 10;
}
template<class T> Array<T>::Array(std::initializer_list<T> list) {
size_t idx = 0;
for (auto val: list){
idx++;
}
data_ = new T[idx + 10];
allocated_ = idx + 10;
size_ = idx;
idx = 0;
try {
for (auto val: list){
data_[idx] = val;
idx++;
}
}
catch (...){
delete [] data_;
throw;
}
}
template<class T>
Array<T>::Array(const Array<T> &other){
if (other.data_ == nullptr){
data_ = nullptr;
allocated_ = 0;
size_ = 0;
return;
}
data_ = new T[other.allocated_];
allocated_ = other.allocated_;
try{
for (size_t i = 0; i < other.size_; i++){
data_[i] = other.at(i);
}
}
catch(...){
delete [] data_;
throw;
}
size_ = other.size_;
}
template<class T>
Array<T>::Array(Array<T> &&other) noexcept{
delete [] data_;
data_ = other.data_;
size_ = other.size_;
allocated_ = other.allocated_;
other.data_ = nullptr;
other.size_ = 0;
other.allocated_ = 0;
}
template<class T>Array<T>::~Array(){
delete [] data_;
data_ = nullptr;
}
//Const methods
template<class T>
size_t Array<T>::capacity() const{
return allocated_;
}
template<class T> T Array<T>::at(int idx) const {
if (idx < 0 || idx >= size_){
throw std::out_of_range("Array construct error");
}
return data_[idx];
}
template<class T>
size_t Array<T>::size() const{
return size_;
}
//Not const methods
template<class T> void Array<T>::push_back(const T val){
if (size_ < allocated_){
data_[size_] = val;
size_++;
return;
}
//Case size_ == allocated_
this->resize(size_ + 1);
data_[size_] = val;
size_++;
}
template<class T>void Array<T>::resize(size_t n_sz){
if (n_sz < size_){
size_ = n_sz;
return;
}
if (allocated_ >= n_sz){
return;
}
T* new_data_ = new T[n_sz + 10];
try{
for (size_t idx = 0; idx < size_; idx++){
new_data_[idx] = data_[idx];
}
}
catch(...){
delete [] new_data_;
throw;
}
delete [] data_;
data_ = new_data_;
allocated_ = n_sz + 10;
}
//Operators overloading
template<class T>T& Array<T>::operator[](int idx) {
if (idx < 0 || idx >= size_){
throw std::out_of_range("index outside array");
}
return data_[idx];
}
template<class T> Array<T>& Array<T>::operator=(const Array<T> &other) {
if (this == &other){
return *this;
}
if (other.data_ == nullptr){
data_ = nullptr;
size_ = 0;
allocated_ = 0;
return *this;
}
T* new_data = new T[other.allocated_];
try{
for (size_t idx = 0; idx < other.size_; idx++){
new_data[idx] = other.at(idx);
}
}
catch(...){
delete [] new_data;
throw;
}
delete [] data_; //Avoid memory leaks
data_ = new_data;
size_ = other.size_;
allocated_ = other.allocated_;
return *this;
}
template<class T>
Array<T>& Array<T>::operator=(Array<T> &&other) noexcept{
if (this == &other){
return *this;
}
delete [] data_;
data_ = other.data_;
allocated_ = other.allocated_;
size_ = other.size_;
other.data_ = nullptr;
other.allocated_ = 0;
other.allocated_ = 0;
return *this;
}
//Friend operators overloading
template<class T>
std::ostream& operator<<(std::ostream& os, const Array<T>& arr){
for (size_t idx = 0; idx < arr.size_; idx++){
os << arr.data_[idx] << " ";
}
return os;
}
#endif //MATRIX_ARRAY_CPP