Add Vector::resize()

Bug: 8384764
Change-Id: Icee83d389f3e555eba7d419b64c8d52a9aa21b8b
This commit is contained in:
Jesse Hall 2013-03-11 10:16:48 -07:00 committed by Alex Ray
parent a729ab1e3b
commit b73559d86c
3 changed files with 18 additions and 1 deletions

View file

@ -80,7 +80,13 @@ public:
//! sets the capacity. capacity can never be reduced less than size()
inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); }
/*!
/*!
* set the size of the vector. items are appended with the default
* constructor, or removed from the end as needed.
*/
inline ssize_t resize(size_t size) { return VectorImpl::resize(size); }
/*!
* C-style array access
*/

View file

@ -64,6 +64,7 @@ public:
inline bool isEmpty() const { return mCount == 0; }
size_t capacity() const;
ssize_t setCapacity(size_t size);
ssize_t resize(size_t size);
/*! append/insert another vector or array */
ssize_t insertVectorAt(const VectorImpl& vector, size_t index);

View file

@ -343,6 +343,16 @@ ssize_t VectorImpl::setCapacity(size_t new_capacity)
return new_capacity;
}
ssize_t VectorImpl::resize(size_t size) {
ssize_t result = NO_ERROR;
if (size > mCount) {
result = insertAt(mCount, size - mCount);
} else if (size < mCount) {
result = removeItemsAt(size, mCount - size);
}
return result < 0 ? result : size;
}
void VectorImpl::release_storage()
{
if (mStorage) {