Skip to content

Commit 12d58ac

Browse files
committed
Added push_back for moveable values.
1 parent 9bb5713 commit 12d58ac

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

src/vector.tcc

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ public:
8484
*/
8585
void push_back(T const&);
8686

87+
/*!
88+
* Add an element to the back.
89+
*
90+
* \param el Element.
91+
*/
92+
void push_back(T const&&);
93+
8794
/*!
8895
* Remove an element from the back.
8996
*
@@ -95,7 +102,7 @@ public:
95102
friend void swap(Vector<U>&, Vector<U>&) noexcept;
96103

97104
private:
98-
void copy_(T const* const);
105+
void copy_(T const* const, size_t const size);
99106

100107
size_t size_ {0};
101108
T* data_ {nullptr};
@@ -105,7 +112,7 @@ private:
105112
template <class T>
106113
Vector<T>::Vector(Vector const& other)
107114
: size_ {other.size_}, data_ {new T[other.size_]} {
108-
copy_(other.data_);
115+
copy_(other.data_, other.size_);
109116
}
110117

111118
template <class T>
@@ -120,7 +127,7 @@ template <class T>
120127
template <size_t n>
121128
Vector<T>::Vector(T const (&arr)[n])
122129
: size_ {n}, data_ {new T[n]} {
123-
copy_(arr);
130+
copy_(arr, n);
124131
}
125132

126133
template <class T>
@@ -179,11 +186,9 @@ size_t Vector<T>::size() const {
179186

180187
template <class T>
181188
void Vector<T>::resize(size_t const size) {
182-
size_ = min(size_, size);
183-
184189
T* data {new T[size]};
185190
swap_(data_, data);
186-
copy_(data);
191+
copy_(data, min(size_, size));
187192
delete[] data;
188193

189194
size_ = size;
@@ -198,21 +203,27 @@ void Vector<T>::clear() {
198203

199204
template <class T>
200205
void Vector<T>::push_back(T const& el) {
201-
resize(++size_);
206+
resize(size_ + 1);
207+
data_[size_ - 1] = el;
208+
}
209+
210+
template <class T>
211+
void Vector<T>::push_back(T const&& el) {
212+
resize(size_ + 1);
202213
data_[size_ - 1] = el;
203214
}
204215

205216
template <class T>
206217
T Vector<T>::pop_back() {
207218
T el {data_[size_ - 1]};
208-
resize(--size_);
219+
resize(size_ - 1);
209220
return el;
210221
}
211222

212223

213224
template <class T>
214-
void Vector<T>::copy_(T const* const data) {
215-
for (size_t i {0}; i < size_; ++i) {
225+
void Vector<T>::copy_(T const* const data, size_t const size) {
226+
for (size_t i {0}; i < size; ++i) {
216227
data_[i] = data[i];
217228
}
218229
}

tests/test_vector.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,32 @@ TEST_CASE("Vector push element.", "[vector]") {
150150
REQUIRE(v.size() == 0);
151151
REQUIRE(not v.data());
152152

153+
int el {1};
154+
v.push_back(el);
155+
REQUIRE(v.size() == 1);
156+
REQUIRE(v.data());
157+
REQUIRE(v[0] == 1);
158+
159+
el = 2;
160+
v.push_back(el);
161+
REQUIRE(v.size() == 2);
162+
REQUIRE(v[1] == 2);
163+
}
164+
165+
TEST_CASE("Vector push moveable element.", "[vector]") {
166+
Vector<int> v {};
167+
168+
REQUIRE(v.size() == 0);
169+
REQUIRE(not v.data());
170+
153171
v.push_back(1);
154172
REQUIRE(v.size() == 1);
155173
REQUIRE(v.data());
156174
REQUIRE(v[0] == 1);
175+
176+
v.push_back(2);
177+
REQUIRE(v.size() == 2);
178+
REQUIRE(v[1] == 2);
157179
}
158180

159181
TEST_CASE("Vector pop element.", "[vector]") {
@@ -164,6 +186,10 @@ TEST_CASE("Vector pop element.", "[vector]") {
164186
int el {v.pop_back()};
165187
REQUIRE(v.size() == 1);
166188
REQUIRE(el == 2);
189+
190+
el = v.pop_back();
191+
REQUIRE(v.size() == 0);
192+
REQUIRE(el == 1);
167193
}
168194

169195

0 commit comments

Comments
 (0)