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
|
#include <iostream>
#include <chrono>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
template <typename T, class alloc_t=std::allocator<T>>
class myvec {
public:
typedef std::size_t size_type;
typedef alloc_t allocator_type;
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const pointer const_pointer;
// constructors
myvec()
: allocator_()
, start_()
, size_()
, capacity_()
{}
myvec(const myvec& vec)
: allocator_(vec.allocator())
, start_(allocator().allocate(vec.size()))
, size_(vec.size())
, capacity_(vec.size())
{
memcpy(start_, vec.start(), size());
}
myvec(myvec&& vec)
: allocator_(std::move(vec.allocator_))
, start_(std::move(vec.start_))
, size_(std::move(vec.size_))
, capacity_(std::move(vec.capacity_))
{
vec.start_ = 0;
vec.size_ = 0;
vec.capacity_ = 0;
}
~myvec() {
if(start_) {
for(int i = 0; i < size(); ++i)
start_[i].~value_type();
allocator().deallocate(start_, capacity());
}
}
// data
size_type capacity() const { return capacity_; }
size_type size() const { return size_; }
const_pointer data() const { return start_; }
allocator_type& allocator() { return allocator_; }
// modify
void push_back(const_reference value) {
if(capacity() == size()) // if the vector is full allocate new memory
{
// get new-needed space (geometric growth factor 2)
if(capacity() != 0)
capacity_ *= 2;
else
capacity_ = 1;
// allocate space
pointer temp = allocator().allocate(capacity());
// then move the elements over and create the new element
memcpy(temp, start_, size() * sizeof(value_type));
allocator().deallocate(start_, size());
start_ = temp;
}
new (start_ + size()) value_type(value);
++size_;
}
// access
reference operator[](size_type position) { return start_[position]; }
const_reference operator[](size_type position) const { return start_[position]; }
private:
allocator_type allocator_;
pointer start_;
size_type size_;
size_type capacity_;
};
// push a few elements in both vectors and compare size, capacity and values
template <class container, typename T>
void Test1(T val) {
container vec;
std::cout << "push_back: " << std::endl;
vec.push_back(val);
std::cout << "c/s: " << vec.capacity() << ' ' << vec.size() << std::endl;
std::cout << "e: ";
for(int i = 0; i < vec.size(); ++i)
std::cout << vec[i] << ' ';
std::cout << std::endl;
std::cout << "push_back: " << std::endl;
vec.push_back(val);
std::cout << "c/s: " << vec.capacity() << ' ' << vec.size() << std::endl;
std::cout << "e: ";
for(int i = 0; i < vec.size(); ++i)
std::cout << vec[i] << ' ';
std::cout << std::endl;
std::cout << "push_back: " << std::endl;
vec.push_back(val);
std::cout << "c/s: " << vec.capacity() << ' ' << vec.size() << std::endl;
std::cout << "e: ";
for(int i = 0; i < vec.size(); ++i)
std::cout << vec[i] << ' ';
std::cout << std::endl;
std::cout << "push_back: " << std::endl;
vec.push_back(val);
std::cout << "c/s: " << vec.capacity() << ' ' << vec.size() << std::endl;
std::cout << "e: ";
for(int i = 0; i < vec.size(); ++i)
std::cout << vec[i] << ' ';
std::cout << std::endl;
std::cout << "push_back: " << std::endl;
vec.push_back(val);
std::cout << "c/s: " << vec.capacity() << ' ' << vec.size() << std::endl;
std::cout << "e: ";
for(int i = 0; i < vec.size(); ++i)
std::cout << vec[i] << ' ';
std::cout << std::endl;
}
int main(void)
{
Test1<std::vector<std::string>>(std::string("Hello"));
Test1<myvec<std::string>>(std::string("Hello"));
return 0;
}
|