-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
182 lines (152 loc) · 4.01 KB
/
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
#pragma once
#include <vector> // size_t
#include <cstdint> // int8_t
#include <stdexcept> // std::out_of_range
template<typename T>
class Vector {
size_t m_size = 0;
size_t m_capacity = 0;
T* m_arr = nullptr;
public:
~Vector();
size_t size() const noexcept;
size_t capacity() const noexcept;
void clear() noexcept;
bool empty() const noexcept;
void reserve(size_t new_capacity);
void resize(size_t new_size, const T& value = T());
void push_back(const T& value);
void push_back(T&& value);
void pop_back();
void insert(size_t index, const T& value);
void erase(size_t index);
void swap(Vector<T>& other);
T& operator[](size_t index);
T& at(size_t index);
T front();
T back();
};
template<typename T>
Vector<T>::~Vector() {
clear();
m_capacity = 0;
}
template<typename T>
size_t Vector<T>::size() const noexcept { return m_size; }
template<typename T>
size_t Vector<T>::capacity() const noexcept { return m_capacity; }
template<typename T>
void Vector<T>::clear() noexcept {
if (m_size == 0) {
return;
}
for (size_t i = 0; i < m_size; ++i) {
T& item = m_arr[i];
item.~T();
}
delete[] reinterpret_cast<int8_t*>(m_arr);
m_size = 0;
}
template<typename T>
bool Vector<T>::empty() const noexcept { return m_size == 0; }
template<typename T>
void Vector<T>::reserve(size_t new_capacity) {
if (new_capacity <= m_capacity) {return;}
auto new_arr = reinterpret_cast<T*>(new int8_t[new_capacity * sizeof(T)]);
size_t i = 0;
try {
for (; i < m_size; ++i) {
new(new_arr + i) T(m_arr[i]); // placement new
// copy constructor exception safety
}
}
catch (...) {
for (auto j = i; j < i; ++j) {
(new_arr + j)->~T();
}
delete[] reinterpret_cast<int8_t*>(new_arr);
throw;
}
for (size_t i = 0; i < m_size; ++i) {
T& item = m_arr[i];
item.~T();
}
delete[] reinterpret_cast<int8_t*>(m_arr);
m_arr = new_arr;
m_capacity = new_capacity;
}
template<typename T>
void Vector<T>::resize(size_t new_size, const T& value) {
if (new_size > m_capacity) {
reserve(new_size);
}
for (auto i = m_size; i < new_size; ++i) {
m_arr[i] = value;
}
m_size = new_size;
}
template<typename T>
void Vector<T>::push_back(const T& value) {
if (m_size == m_capacity) {
reserve(m_capacity == 0 ? 1 : m_capacity * 2);
}
new (m_arr + m_size) T(value);
++m_size;
}
template<typename T>
void Vector<T>::push_back(T&& value) {
if (m_size == m_capacity) {
reserve(m_capacity == 0 ? 1 : m_capacity * 2);
}
new (m_arr + m_size) T(std::move(value));
++m_size;
}
template<typename T>
void Vector<T>::pop_back() {
if (empty()) {return;} // std::vector UB
(m_arr + m_size - 1)->~T();
--m_size;
}
template<typename T>
void Vector<T>::insert(size_t index, const T& value) {
if (index >= m_capacity) {
reserve(m_capacity == 0 ? 1 : m_capacity * 2);
}
if (index < m_size) {
for (size_t i = m_size; i > index; --i) {
m_arr[i] = m_arr[i - 1];
}
}
m_arr[index] = value;
++m_size;
}
template<typename T>
void Vector<T>::erase(size_t index) {
if (index >= m_size) {return;} // std::vector : UB
for(int i = index + 1; i < m_size; ++i) {
m_arr[i - 1] = m_arr[i];
}
m_arr[m_size - 1].~T();
--m_size;
}
template<typename T>
void Vector<T>::swap(Vector<T>& other) {
std::swap(m_arr, other.m_arr);
std::swap(m_size, other.m_size);
std::swap(m_capacity, other.m_capacity);
}
template<typename T>
T& Vector<T>::operator[](size_t index) {
return m_arr[index];
}
template<typename T>
T& Vector<T>::at(size_t index) {
if (index >= m_size) {
throw std::out_of_range("out_of_range");
}
return operator[](index);
}
template<typename T>
T Vector<T>::front() { return at(0); }
template<typename T>
T Vector<T>::back() { return at(m_size - 1); }