-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleVector.cpp
More file actions
222 lines (183 loc) · 6.65 KB
/
SimpleVector.cpp
File metadata and controls
222 lines (183 loc) · 6.65 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//
// Created by darion.yaphet on 2025/5/25.
//
#include <iostream>
#include <memory> // allocator, uninitialized_copy, move
#include <algorithm> // copy, move
#include <vector>
#include <memory_resource> // for unique_ptr test
template<typename T>
class SimpleVector {
private:
T *elements = nullptr;
size_t the_size = 0;
size_t the_capacity = 0;
std::allocator<T> alloc;
// 工具方法:分配并构造新内存
void alloc_n_copy(const T *src, size_t new_cap) {
T *new_block = alloc.allocate(new_cap);
try {
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::construct(alloc, new_block + i, src[i]);
} catch (...) {
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::destroy(alloc, new_block + i);
alloc.deallocate(new_block, new_cap);
throw;
}
elements = new_block;
the_capacity = new_cap;
}
public:
// 默认构造函数
SimpleVector() = default;
// 构造函数:指定大小
explicit SimpleVector(size_t n) : the_size(n), the_capacity(n) {
elements = alloc.allocate(n);
for (size_t i = 0; i < n; ++i)
std::allocator_traits<std::allocator<T> >::construct(alloc, elements + i);
}
// 拷贝构造函数
SimpleVector(const SimpleVector &other) : the_size(other.the_size), the_capacity(other.the_capacity) {
alloc_n_copy(other.elements, other.the_capacity);
}
// 移动构造函数
SimpleVector(SimpleVector &&other) noexcept
: elements(other.elements), the_size(other.the_size), the_capacity(other.the_capacity) {
other.elements = nullptr;
other.the_size = 0;
other.the_capacity = 0;
}
// 拷贝赋值运算符
SimpleVector &operator=(const SimpleVector &other) {
if (this != &other) {
SimpleVector tmp(other); // 复制构造
swap(tmp); // 交换
}
return *this;
}
// 移动赋值运算符
SimpleVector &operator=(SimpleVector &&other) noexcept {
if (this != &other) {
clear();
alloc.deallocate(elements, the_capacity);
elements = other.elements;
the_size = other.the_size;
the_capacity = other.the_capacity;
other.elements = nullptr;
other.the_size = 0;
other.the_capacity = 0;
}
return *this;
}
~SimpleVector() {
clear();
alloc.deallocate(elements, the_capacity);
}
// 获取大小
size_t size() const { return the_size; }
size_t capacity() const { return the_capacity; }
bool empty() const { return the_size == 0; }
// 下标访问
T &operator[](size_t index) {
return elements[index];
}
const T &operator[](size_t index) const {
return elements[index];
}
// push_back(左值)
void push_back(const T &value) {
check_and_allocate();
std::allocator_traits<std::allocator<T> >::construct(alloc, elements + the_size++, value);
}
// push_back(右值,支持移动)
void push_back(T &&value) {
check_and_allocate();
std::allocator_traits<std::allocator<T> >::construct(alloc, elements + the_size++, std::move(value));
}
// 清空数据(但不释放内存)
void clear() {
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::destroy(alloc, elements + i);
the_size = 0;
}
// 改变大小
void resize(size_t new_size) {
if (new_size > the_capacity)
reserve(std::max(new_size, the_capacity * 2));
for (size_t i = the_size; i < new_size; ++i)
std::allocator_traits<std::allocator<T> >::construct(alloc, elements + i); // 默认构造
for (size_t i = new_size; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::destroy(alloc, elements + i); // 析构多余部分
the_size = new_size;
}
// 预留空间
void reserve(size_t new_cap) {
if (new_cap <= the_capacity)
return;
T *new_block = alloc.allocate(new_cap);
try {
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::construct(alloc, new_block + i, std::move(elements[i]));
} catch (...) {
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::destroy(alloc, new_block + i);
alloc.deallocate(new_block, new_cap);
throw;
}
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::destroy(alloc, elements + i);
alloc.deallocate(elements, the_capacity);
elements = new_block;
the_capacity = new_cap;
}
// 缩小容量到 size()
void shrink_to_fit() {
if (the_capacity == the_size)
return;
T *new_block = alloc.allocate(the_size);
try {
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::construct(alloc, new_block + i, std::move(elements[i]));
} catch (...) {
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::destroy(alloc, new_block + i);
alloc.deallocate(new_block, the_size);
throw;
}
for (size_t i = 0; i < the_size; ++i)
std::allocator_traits<std::allocator<T> >::destroy(alloc, elements + i);
alloc.deallocate(elements, the_capacity);
elements = new_block;
the_capacity = the_size;
}
private:
void check_and_allocate() {
if (the_size == the_capacity)
reserve(the_capacity ? the_capacity * 2 : 1);
}
void swap(SimpleVector &other) noexcept {
std::swap(elements, other.elements);
std::swap(the_size, other.the_size);
std::swap(the_capacity, other.the_capacity);
std::swap(alloc, other.alloc);
}
};
int main() {
SimpleVector<std::unique_ptr<int> > vec;
vec.push_back(std::make_unique<int>(10));
vec.push_back(std::make_unique<int>(20));
vec.push_back(std::make_unique<int>(30));
for (size_t i = 0; i < vec.size(); ++i) {
std::cout << "vec[" << i << "] = " << *vec[i] << std::endl;
}
vec.resize(5);
for (size_t i = 0; i < vec.size(); ++i) {
if (vec[i])
std::cout << "vec[" << i << "] = " << *vec[i] << std::endl;
else
std::cout << "vec[" << i << "] is null" << std::endl;
}
vec.shrink_to_fit();
return 0;
}