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
#pragma once
#include <cstddef>
#include <algorithm>
#include <memory>

template <typename T>
class Vector
{
private:
size_t length;
size_t capacity;
T *buffer;

struct Deleter
{
void operator()(T *buffer) const
{
operator delete(buffer);
}
};

public:
Vector(size_t capacity = 100) : capacity(capacity), length(0), buffer(static_cast<T *>(operator new(sizeof(T) * capacity))) {}

~Vector()
{
// using unique_ptr to release the buffer place
std::unique_ptr<T, Deleter> deleter(buffer, Deleter());

for (int i = 0; i < length; ++i)
buffer[i].~T();
}

// copy constructor
Vector(const Vector &copy) : capacity(copy.length), length(0), buffer(static_cast<T *>(operator new(sizeof(T) * capacity)))
{
try
{
for (int i = 0; i < copy.length; ++i)
push_back(copy.buffer[i]);
}
catch (...)
{
// destroy everything that was to created to make it exception safe
~Vector();

// make sure the execptions continue propagating after the cleanup has completed
throw;
}
}

// copy assignment : using swap to prevent the error from the exception occurrence while this process is running
Vector &operator=(const Vector &copy)
{
// Copy and Swap idiom
Vector tmp(copy);
tmp.swap(*this);
return *this;
}

// move constructor
Vector(Vector &&move) noexcept
: capacity(0), length(0), buffer(nullptr)
{
move.swap(*this);
}

// move assignment
Vector &operator=(Vector &&move) noexcept
{
move.swap(*this);
return *this;
}

void push_back(const T &value)
{
resizeIfRequire();
pushBackInternal(value);
}

void push_back(T &&value)
{
resizeIfRequire();
pushBackInternal(std::forward<T>(value));
}

void pop_back()
{
--length;
buffer[length].~T();
}

void swap(Vector &other) noexcept
{
using std::swap;
swap(capacity, other.capacity);
swap(length, other.length);
swap(buffer, other.buffer);
}

void reserve(size_t capacityUpperBound)
{
if (capacityUpperBound > capacity)
reserveCapacity(capacityUpperBound);
}

T operator[](size_t index) const
{
return at(index);
}

T at(size_t index) const
{
return buffer[index];
}

private:
void resizeIfRequire()
{
if (length == capacity)
{
size_t newCapacity = std::max(2.0, capacity * 1.5);
reserveCapacity(newCapacity);
}
}

void pushBackInternal(T const &value)
{
new (buffer + length) T(value);
++length;
}

void moveBackInternal(T &&value)
{
new (buffer + length) T(std::move(value));
++length;
}

void reserveCapacity(size_t newCapacity)
{
Vector<T> tmpBuffer(newCapacity);

// if we can guarantee that the move constructor does not throw
simpleCopy<T>(tmpBuffer);

tmpBuffer.swap(*this);
}

// SFINAE (substitution failure is not an error) method overload
template <typename X>
typename std::enable_if<std::is_nothrow_constructible<X>::value == false>::type
simpleCopy(Vector<T> &dst)
{
std::for_each(buffer, buffer + length, [&dst](T &v) {
dst.pushBackInternal(v);
});
};

template <typename X>
typename std::enable_if<std::is_nothrow_constructible<X>::value == true>::type
simpleCopy(Vector<T> &dst)
{
std::for_each(buffer, buffer + length, [&dst](T &v) {
dst.moveBackInternal(std::move(v));
});
};
};