-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.h
More file actions
171 lines (147 loc) · 3.82 KB
/
Vector.h
File metadata and controls
171 lines (147 loc) · 3.82 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
#include <stdexcept>
template <typename T> class Vector {
public:
Vector() : data_{nullptr}, capacity_{0}, size_{0} {}
Vector(std::initializer_list<T> init)
: size_{init.size()}, capacity_{init.size()} {
data_ = new T[capacity_];
size_t cnt = 0;
for (auto &val : init) {
data_[cnt] = val;
++cnt;
}
}
Vector(size_t size) : size_{size}, capacity_{size} {
data_ = new T[capacity_];
}
Vector(size_t size, const T &value) : size_{size}, capacity_{size} {
data_ = new T[capacity_];
for (size_t i = 0; i < size_; ++i) {
data_[i] = value;
}
}
Vector(const Vector &other) : size_{other.size_}, capacity_{other.capacity_} {
data_ = new T[capacity_];
for (size_t i = 0; i < size_; ++i) {
data_[i] = other[i];
}
}
Vector(Vector &&other) noexcept
: data_{other.data_}, capacity_{other.capacity_}, size_{other.size_} {
other.data_ = nullptr;
other.size_ = 0;
other.capacity_ = 0;
}
Vector &operator=(const Vector &other) {
if (this != &other) {
delete[] data_;
size_ = other.size_;
capacity_ = other.capacity_;
data_ = new T[capacity_];
for (size_t i = 0; i < size_; ++i) {
data_[i] = other[i];
}
}
return *this;
}
Vector &operator=(Vector &&other) noexcept {
if (this != &other) {
delete[] data_;
data_ = other.data_;
capacity_ = other.capacity_;
size_ = other.size_;
other.data_ = nullptr;
other.size_ = 0;
other.capacity_ = 0;
}
return *this;
}
bool operator==(const Vector &other) const {
if (size_ != other.size_)
return false;
for (size_t i = 0; i < size_; ++i) {
if (data_[i] != other.data_[i])
return false;
}
return true;
}
bool operator!=(const Vector &other) const { return !(*this == other); }
~Vector() { delete[] data_; }
void Resize(size_t size) {
T *new_data = new T[size];
for (size_t i = 0; i < std::min(size_, size); ++i) {
new_data[i] = data_[i];
}
delete[] data_;
data_ = new_data;
size_ = size;
capacity_ = size;
}
void Swap(Vector &other) {
std::swap(data_, other.data_);
std::swap(capacity_, other.capacity_);
std::swap(size_, other.size_);
}
void PushBack(const T &value) {
if (size_ == capacity_) {
ResizePrivate(capacity_ == 0 ? 1 : capacity_ * 2);
}
data_[size_] = value;
++size_;
}
void PopBack() {
if (size_ == 0) {
throw std::out_of_range(
"What are you doing?! Vector is empty!!!!!!!!!!!!!!");
}
--size_;
}
void Clear() { size_ = 0; }
T &operator[](size_t index) {
if (index >= size_) {
throw std::out_of_range("What are you doing?! Index out of range!!!");
}
return data_[index];
}
const T &operator[](size_t index) const {
if (index >= size_) {
throw std::out_of_range("What are you doing?! Index out of range!!!!");
}
return data_[index];
}
T &Front() {
if (size_ == 0) {
throw std::out_of_range(
"What are you doing?! I think that vector is empty)");
}
return data_[0];
}
T &Back() {
if (size_ == 0) {
throw std::out_of_range(
"What are you doing?! I think that vector is empty)");
}
return data_[size_ - 1];
}
void Reverse() {
for (size_t i = 0; i < size_ / 2; ++i) {
std::swap(data_[i], data_[size_ - 1 - i]);
}
}
size_t GetSize() const { return size_; }
size_t GetCapacity() const { return capacity_; }
bool Empty() const { return size_ == 0; }
private:
T *data_;
size_t capacity_;
size_t size_;
void ResizePrivate(size_t new_capacity) {
T *new_data = new T[new_capacity];
for (size_t i = 0; i < size_; ++i) {
new_data[i] = data_[i];
}
delete[] data_;
data_ = new_data;
capacity_ = new_capacity;
}
};