|
|
|
@ -235,7 +235,7 @@ class vector {
|
|
|
|
|
allocated_length = other.allocated_length;
|
|
|
|
|
current_length = other.current_length;
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
alloc.construct(raw_beg + i, other.raw_beg[i]);
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, raw_beg + i, other.raw_beg[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
vector(vector &&other) noexcept {
|
|
|
|
@ -251,7 +251,7 @@ class vector {
|
|
|
|
|
~vector() {
|
|
|
|
|
if (raw_beg != nullptr) {
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
alloc.destroy(raw_beg + i);
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_beg + i);
|
|
|
|
|
}
|
|
|
|
|
alloc.deallocate(raw_beg, allocated_length);
|
|
|
|
|
}
|
|
|
|
@ -263,7 +263,7 @@ class vector {
|
|
|
|
|
if (this == &other) return *this;
|
|
|
|
|
if (raw_beg != nullptr) {
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
alloc.destroy(raw_beg + i);
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_beg + i);
|
|
|
|
|
}
|
|
|
|
|
alloc.deallocate(raw_beg, allocated_length);
|
|
|
|
|
}
|
|
|
|
@ -272,7 +272,7 @@ class vector {
|
|
|
|
|
allocated_length = other.allocated_length;
|
|
|
|
|
current_length = other.current_length;
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
alloc.construct(raw_beg + i, other.raw_beg[i]);
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, raw_beg + i, other.raw_beg[i]);
|
|
|
|
|
}
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
@ -358,20 +358,21 @@ class vector {
|
|
|
|
|
if (current_length == allocated_length) {
|
|
|
|
|
size_t new_allocated_length = allocated_length * 2;
|
|
|
|
|
T *new_raw_beg = alloc.allocate(new_allocated_length);
|
|
|
|
|
pos.raw_pointer = new_raw_beg + (pos.raw_pointer - raw_beg);
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
alloc.construct(new_raw_beg + i, std::move(raw_beg[i]));
|
|
|
|
|
alloc.destroy(raw_beg + i);
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, new_raw_beg + i, std::move(raw_beg[i]));
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_beg + i);
|
|
|
|
|
}
|
|
|
|
|
alloc.deallocate(raw_beg, allocated_length);
|
|
|
|
|
raw_beg = new_raw_beg;
|
|
|
|
|
raw_end = raw_beg + current_length;
|
|
|
|
|
allocated_length = new_allocated_length;
|
|
|
|
|
}
|
|
|
|
|
for (T *i = raw_end; i != pos.raw_pointer; --i) {
|
|
|
|
|
alloc.construct(i, std::move(*(i - 1)));
|
|
|
|
|
alloc.destroy(i - 1);
|
|
|
|
|
for (T *i = raw_end - 1; i != pos.raw_pointer; --i) {
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, i, std::move(*(i - 1)));
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, i - 1);
|
|
|
|
|
}
|
|
|
|
|
alloc.construct(pos.raw_pointer, value);
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, pos.raw_pointer, value);
|
|
|
|
|
raw_end++;
|
|
|
|
|
current_length++;
|
|
|
|
|
return pos;
|
|
|
|
@ -397,7 +398,7 @@ class vector {
|
|
|
|
|
raw_end = raw_beg + current_length;
|
|
|
|
|
allocated_length = new_allocated_length;
|
|
|
|
|
}
|
|
|
|
|
for (T *i = raw_end; i != raw_beg + ind; --i) {
|
|
|
|
|
for (T *i = raw_end - 1; i != raw_beg + ind; --i) {
|
|
|
|
|
alloc.construct(i, std::move(*(i - 1)));
|
|
|
|
|
alloc.destroy(i - 1);
|
|
|
|
|
}
|
|
|
|
@ -414,11 +415,23 @@ class vector {
|
|
|
|
|
iterator erase(iterator pos) {
|
|
|
|
|
if (pos.raw_pointer < raw_beg || pos.raw_pointer >= raw_end) throw invalid_iterator();
|
|
|
|
|
for (T *i = pos.raw_pointer; i != raw_end - 1; ++i) {
|
|
|
|
|
alloc.construct(i, std::move(*(i + 1)));
|
|
|
|
|
alloc.destroy(i + 1);
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, i, std::move(*(i + 1)));
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, i + 1);
|
|
|
|
|
}
|
|
|
|
|
raw_end--;
|
|
|
|
|
current_length--;
|
|
|
|
|
if (current_length != 0 && current_length <= allocated_length / 4) {
|
|
|
|
|
size_t new_allocated_length = allocated_length / 2;
|
|
|
|
|
T *new_raw_beg = alloc.allocate(new_allocated_length);
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, new_raw_beg + i, std::move(raw_beg[i]));
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_beg + i);
|
|
|
|
|
}
|
|
|
|
|
alloc.deallocate(raw_beg, allocated_length);
|
|
|
|
|
raw_beg = new_raw_beg;
|
|
|
|
|
raw_end = raw_beg + current_length;
|
|
|
|
|
allocated_length = new_allocated_length;
|
|
|
|
|
}
|
|
|
|
|
return pos;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
@ -434,14 +447,8 @@ class vector {
|
|
|
|
|
}
|
|
|
|
|
raw_end--;
|
|
|
|
|
current_length--;
|
|
|
|
|
return iterator(this, raw_beg + ind);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* adds an element to the end.
|
|
|
|
|
*/
|
|
|
|
|
void push_back(const T &value) {
|
|
|
|
|
if (current_length == allocated_length) {
|
|
|
|
|
size_t new_allocated_length = allocated_length * 2;
|
|
|
|
|
if (current_length != 0 && current_length <= allocated_length / 4) {
|
|
|
|
|
size_t new_allocated_length = allocated_length / 2;
|
|
|
|
|
T *new_raw_beg = alloc.allocate(new_allocated_length);
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
alloc.construct(new_raw_beg + i, std::move(raw_beg[i]));
|
|
|
|
@ -452,7 +459,25 @@ class vector {
|
|
|
|
|
raw_end = raw_beg + current_length;
|
|
|
|
|
allocated_length = new_allocated_length;
|
|
|
|
|
}
|
|
|
|
|
alloc.construct(raw_end, value);
|
|
|
|
|
return iterator(this, raw_beg + ind);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* adds an element to the end.
|
|
|
|
|
*/
|
|
|
|
|
void push_back(const T &value) {
|
|
|
|
|
if (current_length == allocated_length) {
|
|
|
|
|
size_t new_allocated_length = allocated_length * 2;
|
|
|
|
|
T *new_raw_beg = alloc.allocate(new_allocated_length);
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, new_raw_beg + i, std::move(raw_beg[i]));
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_beg + i);
|
|
|
|
|
}
|
|
|
|
|
alloc.deallocate(raw_beg, allocated_length);
|
|
|
|
|
raw_beg = new_raw_beg;
|
|
|
|
|
raw_end = raw_beg + current_length;
|
|
|
|
|
allocated_length = new_allocated_length;
|
|
|
|
|
}
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, raw_end, value);
|
|
|
|
|
raw_end++;
|
|
|
|
|
current_length++;
|
|
|
|
|
}
|
|
|
|
@ -462,9 +487,21 @@ class vector {
|
|
|
|
|
*/
|
|
|
|
|
void pop_back() {
|
|
|
|
|
if (current_length == 0) throw container_is_empty();
|
|
|
|
|
alloc.destroy(raw_end - 1);
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_end - 1);
|
|
|
|
|
raw_end--;
|
|
|
|
|
current_length--;
|
|
|
|
|
if (current_length != 0 && current_length <= allocated_length / 4) {
|
|
|
|
|
size_t new_allocated_length = allocated_length / 2;
|
|
|
|
|
T *new_raw_beg = alloc.allocate(new_allocated_length);
|
|
|
|
|
for (size_t i = 0; i < current_length; ++i) {
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::construct(alloc, new_raw_beg + i, std::move(raw_beg[i]));
|
|
|
|
|
std::allocator_traits<decltype(alloc)>::destroy(alloc, raw_beg + i);
|
|
|
|
|
}
|
|
|
|
|
alloc.deallocate(raw_beg, allocated_length);
|
|
|
|
|
raw_beg = new_raw_beg;
|
|
|
|
|
raw_end = raw_beg + current_length;
|
|
|
|
|
allocated_length = new_allocated_length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|