Add as_array

This commit is contained in:
karl 2021-03-15 14:12:22 +01:00
parent 2b62e37ccd
commit 560abcb201
2 changed files with 20 additions and 7 deletions

View File

@ -105,6 +105,10 @@ class Vector {
}
}
T *as_array() {
return data;
}
// Returns the number of elements in the vector, regardless of the actually
// reserved memory.
unsigned int size() const {
@ -132,7 +136,8 @@ class Vector {
// Resize the size of the vector to the given new_size
// If this decreases the size, some elements are deleted
// If this increases the size, some default-constructed elements are added
// If this increases the size, some default-constructed elements are added.
// If no default constructor is available, a padder must be passed.
void resize(unsigned int new_size, const T &padder = T()) {
int difference = new_size - size();
@ -141,7 +146,7 @@ class Vector {
reallocate(new_size);
}
// Add additional default-constructed items
// Add additional items
for (int i = 0; i < difference; i++) {
new (data + element_count + i) T(padder);
}

View File

@ -14,12 +14,20 @@ SCENARIO("Vector size and length are correct", "[vector]") {
}
SCENARIO("The bracket operator returns the correct element", "[vector]") {
GIVEN("A vector with some elements") {
Vector<int> v(20);
v.push_back(1);
v.push_back(2);
v.push_back(3);
THEN("The element in the middle must have the expected value") {
REQUIRE(v[1] == 2);
}
THEN("The same value must be accessible using as_array") {
REQUIRE(v.as_array()[1] == 2);
}
}
}
SCENARIO("Equality is returned correctly", "[vector]") {