-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathali_vector.h
205 lines (172 loc) · 6.31 KB
/
ali_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//
// Created by Ali Moameri on 4/10/22.
//
#ifndef ALI_VECTOR_H
#define ALI_VECTOR_H
#include <cstddef>
#include <exception>
#include <algorithm>
#include <memory>
namespace ali {
template <typename T> class vector {
public:
vector()
: m_size{0}, m_capacity{0}, ptr{nullptr} {}
explicit vector(const std::size_t& _size)
: m_size{_size}, m_capacity{_size * 2} {
ptr = m_alloc.allocate(m_capacity);
}
explicit vector(const std::size_t& _size, const T& value)
: m_size{_size}, m_capacity{_size * 2} {
ptr = m_alloc.allocate(m_capacity);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.construct(ptr + i, value);
}
vector(const std::initializer_list<T>& ls)
: m_size{ls.size()}, m_capacity{ls.size() * 2} {
ptr = m_alloc.allocate(m_capacity);
for(std::size_t i{0}; i < ls.size(); ++i)
m_alloc.construct(ptr + i, *(ls.begin() + i));
}
vector(const ali::vector<T>& rhs)
:m_size{rhs.size()}, m_capacity{rhs.capacity()} {
ptr = m_alloc.allocate(m_capacity);
for(std::size_t i{0}; i < rhs.size(); ++i)
m_alloc.construct(ptr + i, rhs[i]);
}
vector(ali::vector<T>&& rhs) noexcept
:m_size{rhs.size()}, m_capacity{rhs.capacity()} {
ptr = m_alloc.allocate(m_capacity);
for(std::size_t i{0}; i < rhs.size(); ++i)
m_alloc.construct(ptr + i, rhs[i]);
rhs.clear();
m_capacity = 0;
}
~vector() { m_alloc.deallocate(ptr, m_capacity); }
[[nodiscard]] constexpr std::size_t size() const noexcept { return m_size; }
[[nodiscard]] constexpr std::size_t capacity() const noexcept { return m_capacity; }
void clear() noexcept {
delete[] ptr;
ptr = nullptr;
m_size = 0;
}
void push_back(const T& value) {
if(m_capacity == 0)
reserve(8);
else if(m_size == m_capacity)
reserve(m_size * 2);
ptr[m_size++] = value;
}
void pop_back() {
m_size = m_size > 0 ? m_size - 1 : m_size;
}
T& at(std::size_t index) {
if (index < m_size)
return ptr[index];
else
throw std::out_of_range("index out of range");
}
const T& at(std::size_t index) const {
if (index < m_size)
return ptr[index];
else
throw std::out_of_range("index out of range");
}
void shrink_to_fit() {
if (m_size < m_capacity) {
T* new_ptr{m_alloc.allocate(m_size)};
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.construct(new_ptr + i, ptr[i]);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.destroy(ptr + i);
m_alloc.deallocate(ptr, m_capacity);
ptr = new_ptr;
m_capacity = m_size;
}
}
void reserve(std::size_t space) {
if (space > m_capacity) {
T* new_ptr{m_alloc.allocate(space)};
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.construct(new_ptr + i, ptr[i]);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.destroy(ptr + i);
m_alloc.deallocate(ptr, m_capacity);
ptr = new_ptr;
m_capacity = space;
}
}
[[nodiscard]] bool empty() const { return size() == 0; }
void swap(ali::vector<T>& rhs) {
std::swap(m_size, rhs.m_size);
std::swap(m_capacity, rhs.m_capacity);
std::swap(m_alloc, rhs.m_alloc);
std::swap(ptr, rhs.ptr);
}
T& operator[](const std::size_t& index) {
if(index >= size())
throw std::out_of_range("out of range index");
else
return ptr[index];
}
const T& operator[](const std::size_t& index) const {
if(index >= size())
throw std::out_of_range("out of range index");
else
return ptr[index];
}
ali::vector<T>& operator=(const ali::vector<T>& rhs) noexcept {
if(this == &rhs)
return *this;
if(rhs.size() <= m_capacity) {
std::copy(rhs.ptr, rhs.ptr + rhs.size(), ptr);
m_size = rhs.size();
return *this;
}
T* new_ptr{m_alloc.allocate(rhs.size())};
for(std::size_t i{0}; i < rhs.size(); ++i)
m_alloc.construct(new_ptr + i, rhs[i]);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.destroy(ptr + i);
m_alloc.deallocate(ptr, m_capacity);
ptr = new_ptr;
m_capacity = m_size = rhs.size();
return *this;
}
ali::vector<T>& operator=(ali::vector<T>&& rhs) noexcept {
if(this == &rhs)
return *this;
if(rhs.size() <= m_capacity) {
std::copy(rhs.ptr, rhs.ptr + rhs.size(), ptr);
m_size = rhs.size();
} else {
T* new_ptr{m_alloc.allocate(rhs.size())};
for(std::size_t i{0}; i < rhs.size(); ++i)
m_alloc.construct(new_ptr + i, rhs[i]);
for(std::size_t i{0}; i < m_size; ++i)
m_alloc.destroy(ptr + i);
m_alloc.deallocate(ptr, m_capacity);
ptr = new_ptr;
m_capacity = m_size = rhs.size();
}
rhs.clear();
rhs.m_capacity = 0;
return *this;
}
bool operator==(const vector &rhs) const {
if (size() != rhs.size())
return false;
for (std::size_t i{0}; i < size(); ++i) {
if (ptr[i] != rhs[i])
return false;
}
return true;
}
private:
std::size_t m_size{0};
std::size_t m_capacity{0};
std::allocator<T> m_alloc;
T* ptr{nullptr};
};
}
#endif //ALI_VECTOR_H