added wxVector::resize()

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@57248 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-12-11 13:45:04 +00:00
parent de150f6af9
commit e068310a5a
3 changed files with 73 additions and 3 deletions

View File

@@ -170,7 +170,7 @@ public:
wxVector() : m_size(0), m_capacity(0), m_values(NULL) {} wxVector() : m_size(0), m_capacity(0), m_values(NULL) {}
wxVector(size_type size) wxVector(size_type size)
: m_size(0), m_capacity(0), m_values(NULL) : m_size(0), m_capacity(0), m_values(NULL)
{ {
reserve(size); reserve(size);
for ( size_t n = 0; n < size; n++ ) for ( size_t n = 0; n < size; n++ )
@@ -178,7 +178,7 @@ public:
} }
wxVector(size_type size, const value_type& v) wxVector(size_type size, const value_type& v)
: m_size(0), m_capacity(0), m_values(NULL) : m_size(0), m_capacity(0), m_values(NULL)
{ {
reserve(size); reserve(size);
for ( size_t n = 0; n < size; n++ ) for ( size_t n = 0; n < size; n++ )
@@ -229,6 +229,22 @@ public:
m_capacity = n; m_capacity = n;
} }
void resize(size_type n)
{
if ( n < m_size )
Shrink(n);
else if ( n > m_size )
Extend(n, value_type());
}
void resize(size_type n, const value_type& v)
{
if ( n < m_size )
Shrink(n);
else if ( n > m_size )
Extend(n, v);
}
size_type size() const size_type size() const
{ {
return m_size; return m_size;
@@ -386,6 +402,20 @@ private:
} }
private: private:
void Shrink(size_type n)
{
for ( size_type i = n; i < m_size; i++ )
m_values[i].~T();
m_size = n;
}
void Extend(size_type n, const value_type& v)
{
reserve(n);
for ( size_type i = m_size; i < n; i++ )
push_back(v);
}
size_type m_size, size_type m_size,
m_capacity; m_capacity;
value_type *m_values; value_type *m_values;

View File

@@ -120,7 +120,7 @@ public:
Return reverse iterator to end of the vector. Return reverse iterator to end of the vector.
*/ */
reverse_iterator rbegin(); reverse_iterator rbegin();
/** /**
Return reverse iterator to beginning of the vector. Return reverse iterator to beginning of the vector.
*/ */
@@ -218,6 +218,19 @@ public:
*/ */
void reserve(size_type n); void reserve(size_type n);
/**
Makes the vector of size @a n.
If @a n is less than the current size(), the elements at the end of the
vector are erased. If it is greater, then the vector is completed with
either the copies of the given object @a v or @c value_type() objects
until it becomes of size @a n.
*/
//@{
void resize(size_type n);
void resize(size_type n, const value_type& v);
//@}
/** /**
Returns the size of the vector. Returns the size of the vector.
*/ */

View File

@@ -82,6 +82,7 @@ private:
CPPUNIT_TEST( Iterators ); CPPUNIT_TEST( Iterators );
CPPUNIT_TEST( Objects ); CPPUNIT_TEST( Objects );
CPPUNIT_TEST( NonPODs ); CPPUNIT_TEST( NonPODs );
CPPUNIT_TEST( Resize );
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
void PushPopTest(); void PushPopTest();
@@ -90,6 +91,7 @@ private:
void Iterators(); void Iterators();
void Objects(); void Objects();
void NonPODs(); void NonPODs();
void Resize();
DECLARE_NO_COPY_CLASS(VectorsTestCase) DECLARE_NO_COPY_CLASS(VectorsTestCase)
}; };
@@ -239,3 +241,28 @@ void VectorsTestCase::NonPODs()
vs.erase(vs.begin()); vs.erase(vs.begin());
vs.clear(); vs.clear();
} }
void VectorsTestCase::Resize()
{
wxVector<CountedObject> v;
v.resize(3);
CPPUNIT_ASSERT_EQUAL( 3, v.size() );
CPPUNIT_ASSERT_EQUAL( 3, CountedObject::GetCount() );
CPPUNIT_ASSERT_EQUAL( 0, v[0].GetValue() );
CPPUNIT_ASSERT_EQUAL( 0, v[1].GetValue() );
CPPUNIT_ASSERT_EQUAL( 0, v[2].GetValue() );
v.resize(1);
CPPUNIT_ASSERT_EQUAL( 1, v.size() );
CPPUNIT_ASSERT_EQUAL( 1, CountedObject::GetCount() );
v.resize(4, CountedObject(17));
CPPUNIT_ASSERT_EQUAL( 4, v.size() );
CPPUNIT_ASSERT_EQUAL( 4, CountedObject::GetCount() );
CPPUNIT_ASSERT_EQUAL( 0, v[0].GetValue() );
CPPUNIT_ASSERT_EQUAL( 17, v[1].GetValue() );
CPPUNIT_ASSERT_EQUAL( 17, v[2].GetValue() );
CPPUNIT_ASSERT_EQUAL( 17, v[3].GetValue() );
}