Skip to content

Commit 6662dd1

Browse files
committed
resize and reserve
1 parent 3fa125e commit 6662dd1

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

vector/include/vector.hpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,69 @@ class vector {
1919
delete[] m_data; // free memory in destructor
2020
}
2121

22+
/**
23+
* @brief Set the capacity of the vector.
24+
* @param new_capacity The new capacity for the vector.
25+
* @return void.
26+
*/
27+
void reserve(const std::size_t new_capacity) {
28+
// reserve only affects capacity not size
29+
// therefore a reserving an amount smaller than size will be ignored
30+
31+
if (new_capacity > m_capacity) {
32+
reallocate(new_capacity);
33+
}
34+
}
35+
36+
/**
37+
* @brief Set the size of the vector.
38+
* @param new_size The new size for the vector.
39+
* @return void.
40+
*/
41+
void resize(const std::size_t new_size) {
42+
// resize affects size and capacity
43+
// therefore a resizing an amount smaller than size will shrink the vector
44+
45+
// reserve more space if needed
46+
if (new_size > m_capacity) reallocate(new_size);
47+
48+
// shrink if needed
49+
for (std::size_t i = new_size; i < m_size; ++i) {
50+
m_data[i].~T(); // call destructor explicitly
51+
}
52+
53+
// if growing, initialize new elements with default constructor
54+
for (std::size_t i = m_size; i < new_size; ++i) {
55+
new (&m_data[i]) T();
56+
}
57+
58+
m_size = new_size;
59+
}
60+
61+
/**
62+
* @brief Set the size of the vector.
63+
* @param new_size The new size for the vector.
64+
* @param val The value to initialize new elements with.
65+
* @return void.
66+
*/
67+
void resize(const std::size_t new_size, const T& val) {
68+
69+
// reserve more space if needed
70+
if (new_size > m_capacity) reallocate(new_size);
71+
72+
// shrink if needed
73+
for (std::size_t i = new_size; i < m_size; ++i) {
74+
m_data[i].~T(); // call destructor explicitly
75+
}
76+
77+
// if growing, initialize new elements with val
78+
for (std::size_t i = m_size; i < new_size; ++i) {
79+
new (&m_data[i]) T(val);
80+
}
81+
82+
m_size = new_size;
83+
}
84+
2285
/**
2386
* @brief Add an element to the end of the vector.
2487
* @param value The value to add to the end of the vector.
@@ -121,6 +184,8 @@ class vector {
121184
*/
122185
void reallocate(std::size_t new_capacity) {
123186

187+
// reserc
188+
124189
// TODO: handle reallocation for shrinking
125190
if (new_capacity <= m_size) {
126191
m_size = new_capacity;

vector/tests/vector_test.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,59 @@ TEST_F(VectorTest, PopBack) {
210210

211211
EXPECT_EQ(vals.size(), 1);
212212
}
213+
214+
TEST_F(VectorTest, ReserveTest) {
215+
std2::vector<Point3D> points;
216+
217+
// Add 100 points
218+
for(int i = 0; i < 100; i++) {
219+
points.push_back(Point3D(i, i, i));
220+
}
221+
222+
std::cout << "Before reserve: size=" << points.size() << std::endl;
223+
224+
points.reserve(50);
225+
226+
std::cout << "After reserve(50): size=" << points.size() << std::endl;
227+
228+
EXPECT_EQ(points.size(), 100);
229+
}
230+
231+
TEST_F(VectorTest, ResizeTest) {
232+
std2::vector<Point3D> points;
233+
234+
// First reserve space for 200
235+
points.reserve(200);
236+
237+
// Add 100 points
238+
for(int i = 0; i < 100; i++) {
239+
points.push_back(Point3D(i, i, i));
240+
}
241+
242+
std::cout << "Initial state: size=" << points.size() << std::endl;
243+
244+
// Now resize to 50
245+
points.resize(50);
246+
247+
std::cout << "After resize(50): size=" << points.size() << std::endl;
248+
249+
// Now resize to 100
250+
points.resize(100, Point3D(1.0f, 1.0f, 1.0f));
251+
252+
std::cout << "After resize(100): size=" << points.size() << std::endl;
253+
254+
}
255+
256+
TEST_F(VectorTest, ResizeWithValue) {
257+
std2::vector<Point3D> points;
258+
259+
// Resize to 3 elements, all with value (1,1,1)
260+
points.resize(3, Point3D(1.0f));
261+
262+
EXPECT_EQ(points.size(), 3);
263+
for(size_t i = 0; i < points.size(); ++i) {
264+
EXPECT_EQ(points[i].x, 1.0f);
265+
EXPECT_EQ(points[i].y, 1.0f);
266+
EXPECT_EQ(points[i].z, 1.0f);
267+
}
268+
}

0 commit comments

Comments
 (0)