#include<iostream>
#include<memory>
#include<cstddef>
#include<utility>
template<typename T,class A_ = std::allocator<T>>
class Vec {
public:
Vec() :elements(nullptr), first_free(nullptr), cap(nullptr){}
Vec(const Vec&);
Vec &operator=(const Vec&);
~Vec();
void push_back(const T&);
std::size_t size() const {
return first_free - elements;
}
std::size_t capacity()const {
return cap - elements;
}
typename T* begin() const {
return elements;
}
typename T* end() const {
return first_free;
}
private:
A_ alloc;
void check_alloc(){
if (size() == capacity())
reallocate();
}
void reallocate();
void free();
std::pair<T*, T*> alloc_copy(const T*, const T*);
typename T* elements;
typename T* first_free;
typename T* cap;
};
template<typename T, class A_>
void Vec<T,A_>::push_back(const T& t){
check_alloc();
alloc.construct(first_free++, t);
}
template<typename T, class A_>
std::pair<T*, T*> Vec<T, A_>::alloc_copy(const T* b, const T* e){
auto data = alloc.allocate(e - b);
return { data, std::uninitialized_copy(b, e, data) };
}
template<typename T, class A_>
void Vec<T, A_>::free(){
if (elements){
for (auto p = first_free; p != elements;)
alloc.destroy(--p);
alloc.deallocate(elements, cap - elements);
}
}
template<typename T, class A_>
Vec<T, A_>::Vec(const Vec<T, A_> &v){
auto newdata = alloc_copy(v.begin(), v.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
template<typename T, class A_>
Vec<T, A_> &Vec<T, A_>::operator=(const Vec<T, A_> &v){
auto newdata = alloc_copy(v.begin(), v.end());
free();
elements = newdata.first;
first_free = cap = newdata.second;
return *this;
}
template<typename T, class A_>
Vec<T, A_>::~Vec(){
free();
}
template<typename T, class A_>
void Vec<T, A_>::reallocate(){
auto newcapacity = size() != 0 ? 2 * size() : 1;
auto newdata = alloc.allocate(newcapacity);
auto dest = newdata;
auto elem = elements;
for (std::size_t i = 0; i != size(); ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
first_free = dest;
cap = elements + newcapacity;
}
#include<memory>
#include<cstddef>
#include<utility>
template<typename T,class A_ = std::allocator<T>>
class Vec {
public:
Vec() :elements(nullptr), first_free(nullptr), cap(nullptr){}
Vec(const Vec&);
Vec &operator=(const Vec&);
~Vec();
void push_back(const T&);
std::size_t size() const {
return first_free - elements;
}
std::size_t capacity()const {
return cap - elements;
}
typename T* begin() const {
return elements;
}
typename T* end() const {
return first_free;
}
private:
A_ alloc;
void check_alloc(){
if (size() == capacity())
reallocate();
}
void reallocate();
void free();
std::pair<T*, T*> alloc_copy(const T*, const T*);
typename T* elements;
typename T* first_free;
typename T* cap;
};
template<typename T, class A_>
void Vec<T,A_>::push_back(const T& t){
check_alloc();
alloc.construct(first_free++, t);
}
template<typename T, class A_>
std::pair<T*, T*> Vec<T, A_>::alloc_copy(const T* b, const T* e){
auto data = alloc.allocate(e - b);
return { data, std::uninitialized_copy(b, e, data) };
}
template<typename T, class A_>
void Vec<T, A_>::free(){
if (elements){
for (auto p = first_free; p != elements;)
alloc.destroy(--p);
alloc.deallocate(elements, cap - elements);
}
}
template<typename T, class A_>
Vec<T, A_>::Vec(const Vec<T, A_> &v){
auto newdata = alloc_copy(v.begin(), v.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
template<typename T, class A_>
Vec<T, A_> &Vec<T, A_>::operator=(const Vec<T, A_> &v){
auto newdata = alloc_copy(v.begin(), v.end());
free();
elements = newdata.first;
first_free = cap = newdata.second;
return *this;
}
template<typename T, class A_>
Vec<T, A_>::~Vec(){
free();
}
template<typename T, class A_>
void Vec<T, A_>::reallocate(){
auto newcapacity = size() != 0 ? 2 * size() : 1;
auto newdata = alloc.allocate(newcapacity);
auto dest = newdata;
auto elem = elements;
for (std::size_t i = 0; i != size(); ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
first_free = dest;
cap = elements + newcapacity;
}