COMMON: Add a version of Common::Array::resize with a fill value
This commit is contained in:
parent
930028522e
commit
4002c7bdde
3 changed files with 29 additions and 16 deletions
|
@ -381,6 +381,21 @@ public:
|
|||
_size = newSize;
|
||||
}
|
||||
|
||||
/** Change the size of the array and initialize new elements that exceed the
|
||||
* current array's size with copies of value. */
|
||||
void resize(size_type newSize, const T value) {
|
||||
reserve(newSize);
|
||||
|
||||
T *storage = _storage;
|
||||
|
||||
for (size_type i = newSize; i < _size; ++i)
|
||||
storage[i].~T();
|
||||
if (newSize > _size)
|
||||
uninitialized_fill_n(storage + _size, newSize - _size, value);
|
||||
|
||||
_size = newSize;
|
||||
}
|
||||
|
||||
/** Assign to this array the elements between the given iterators from another array,
|
||||
* from @p first included to @p last excluded.
|
||||
*/
|
||||
|
|
|
@ -91,12 +91,8 @@ public:
|
|||
typedef const T const_reference;
|
||||
|
||||
vector() : Common::Array<T>() {}
|
||||
vector(size_t newSize) : Common::Array<T>() {
|
||||
Common::Array<T>::resize(newSize);
|
||||
}
|
||||
vector(size_t newSize, const T elem) {
|
||||
resize(newSize, elem);
|
||||
}
|
||||
vector(size_t newSize) : Common::Array<T>(newSize) {}
|
||||
vector(size_t newSize, const T elem) : Common::Array<T>(newSize, elem) {}
|
||||
|
||||
reverse_iterator rbegin() {
|
||||
return reverse_iterator(this, (int)Common::Array<T>::size() - 1);
|
||||
|
@ -115,16 +111,6 @@ public:
|
|||
Common::Array<T>::remove_at(0);
|
||||
}
|
||||
|
||||
void resize(size_t newSize) {
|
||||
Common::Array<T>::resize(newSize);
|
||||
}
|
||||
void resize(size_t newSize, const T elem) {
|
||||
size_t oldSize = Common::Array<T>::size();
|
||||
resize(newSize);
|
||||
for (size_t idx = oldSize; idx < newSize; ++idx)
|
||||
this->operator[](idx) = elem;
|
||||
}
|
||||
|
||||
T at(size_t index) const {
|
||||
return (*this)[index];
|
||||
}
|
||||
|
|
|
@ -416,6 +416,18 @@ class ArrayTestSuite : public CxxTest::TestSuite
|
|||
TS_ASSERT_EQUALS(array.size(), (unsigned int)2);
|
||||
TS_ASSERT_EQUALS(array[0], -3);
|
||||
TS_ASSERT_EQUALS(array[1], 163);
|
||||
|
||||
array.resize(4, 42);
|
||||
TS_ASSERT_EQUALS(array.size(), (unsigned int)4);
|
||||
TS_ASSERT_EQUALS(array[0], -3);
|
||||
TS_ASSERT_EQUALS(array[1], 163);
|
||||
TS_ASSERT_EQUALS(array[2], 42);
|
||||
TS_ASSERT_EQUALS(array[3], 42);
|
||||
|
||||
array.resize(2, 42);
|
||||
TS_ASSERT_EQUALS(array.size(), (unsigned int)2);
|
||||
TS_ASSERT_EQUALS(array[0], -3);
|
||||
TS_ASSERT_EQUALS(array[1], 163);
|
||||
}
|
||||
|
||||
void test_swap() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue