Dali::Vector - Insert() and Erase() methods added. 28/26028/16
authorVictor Cebollada <v.cebollada@samsung.com>
Wed, 13 Aug 2014 16:51:54 +0000 (17:51 +0100)
committerVictor Cebollada <v.cebollada@samsung.com>
Thu, 21 Aug 2014 09:56:53 +0000 (10:56 +0100)
Insert(Iterator, Element) Inserts an element into the vector.
Insert(Iterator, Iterator, Iterator) Inserts a range of elements into the vector.
Erase(Iterator, Iterator) Erases a range of elements from the vector.

Change-Id: Ia68637c025863bdb0d7c998b017b26ce19d22a42
Signed-off-by: Victor Cebollada <v.cebollada@samsung.com>
automated-tests/src/dali-unmanaged/utc-Dali-Vector.cpp
dali/public-api/common/dali-vector.cpp
dali/public-api/common/dali-vector.h

index 667a217..0e89d85 100644 (file)
@@ -24,12 +24,9 @@ using namespace Dali;
 
 namespace
 {
-
 const Dali::VectorBase::SizeType ZERO(0);
-
 }
 
-
 int UtcDaliEmptyVectorInt(void)
 {
   tet_infoline("Testing Dali::Vector<int>");
@@ -247,6 +244,25 @@ int UtcDaliVectorIntErase(void)
     tet_printf("Assertion test failed - wrong Exception\n" );
     tet_result(TET_FAIL);
   }
+
+  try
+  {
+    // illegal erase, one before the begin
+    vector.Erase( vector.Begin() - 1u );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "(iterator < End()) && (iterator >= Begin())", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+
   DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(3), vector.Count(), TEST_LOCATION );
   DALI_TEST_EQUALS( vector[ 0 ], 2, TEST_LOCATION );
   DALI_TEST_EQUALS( vector[ 1 ], 3, TEST_LOCATION );
@@ -385,7 +401,7 @@ int UtcDaliVectorDoubleRemove(void)
   catch( Dali::DaliException& e )
   {
     tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
-    DALI_TEST_ASSERT( e, "(iterator < end)", TEST_LOCATION );
+    DALI_TEST_ASSERT( e, "(iterator < End()) && (iterator >= Begin())", TEST_LOCATION );
   }
   catch( ... )
   {
@@ -407,7 +423,7 @@ int UtcDaliVectorDoubleRemove(void)
   catch( Dali::DaliException& e )
   {
     tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
-    DALI_TEST_ASSERT( e, "(iterator < end) && (iterator >= Begin()", TEST_LOCATION );
+    DALI_TEST_ASSERT( e, "(iterator < End()) && (iterator >= Begin())", TEST_LOCATION );
   }
   catch( ... )
   {
@@ -578,7 +594,7 @@ int UtcDaliVectorAsserts(void)
   catch(Dali::DaliException& e)
   {
     tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
-    DALI_TEST_ASSERT( e, "VectorBase::mData", TEST_LOCATION );
+    DALI_TEST_ASSERT( e, "(iterator < End()) && (iterator >= Begin())", TEST_LOCATION );
   }
   catch(...)
   {
@@ -601,7 +617,7 @@ int UtcDaliVectorAsserts(void)
   catch(Dali::DaliException& e)
   {
     tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
-    DALI_TEST_ASSERT( e, "VectorBase::mData", TEST_LOCATION );
+    DALI_TEST_ASSERT( e, "(iterator < End()) && (iterator >= Begin())", TEST_LOCATION );
   }
   catch(...)
   {
@@ -753,6 +769,490 @@ int UtcDaliVectorAcidTest(void)
   END_TEST;
 }
 
+int UtcDaliVectorPushBack(void)
+{
+  tet_infoline( "Testing Dali::Vector< int* >PushBack(Element)" );
+
+  Vector<unsigned int> vector;
+  DALI_TEST_EQUALS( ZERO, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, vector.Capacity(), TEST_LOCATION );
+
+  vector.Reserve( 2u );
+  DALI_TEST_EQUALS( ZERO, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( 2u, vector.Capacity(), TEST_LOCATION );
+
+  vector.PushBack( 0u );
+  vector.PushBack( 1u );
+  vector.PushBack( 2u );
+
+  DALI_TEST_EQUALS( 3u, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( 6u, vector.Capacity(), TEST_LOCATION );
+
+  vector.PushBack( 3u );
+
+  DALI_TEST_EQUALS( 4u, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( 6u, vector.Capacity(), TEST_LOCATION );
+
+  DALI_TEST_EQUALS( 0u, vector[0u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 1u, vector[1u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 2u, vector[2u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 3u, vector[3u], TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliVectorInsert01(void)
+{
+  tet_infoline( "Testing Dali::Vector< int* >Insert(Iterator, Element)" );
+
+  // Test order of array inserted-into:
+  Vector< unsigned int > orderedVector;
+  orderedVector.PushBack( 9u );
+  for( unsigned int i = 8u; i <= 8u; --i )
+  {
+    orderedVector.Insert( orderedVector.Begin(), i );
+    DALI_TEST_EQUALS( 10u - i, orderedVector.Count(), TEST_LOCATION );
+    DALI_TEST_EQUALS( i, orderedVector[0u], TEST_LOCATION );
+  }
+
+  for( unsigned int i = 0u; i < 10u; ++i )
+  {
+    DALI_TEST_EQUALS( i, orderedVector[i], TEST_LOCATION );
+  }
+
+  // Test insertion out of range in non-empty array throws:
+  try
+  {
+    orderedVector.Insert( orderedVector.Begin() + 99u, 99u );
+    tet_printf( "Assertion expected, but not occurred at %s\n", TEST_LOCATION );
+    tet_result( TET_FAIL );
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf( "Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
+    DALI_TEST_ASSERT( e, "( at <= End() ) && ( at >= Begin() )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf( "Assertion test failed - wrong Exception\n" );
+    tet_result( TET_FAIL );
+  }
+
+  try
+  {
+    orderedVector.Insert( orderedVector.Begin() - 1u, 99u );
+    tet_printf( "Assertion expected, but not occurred at %s\n", TEST_LOCATION );
+    tet_result( TET_FAIL );
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf( "Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
+    DALI_TEST_ASSERT( e, "( at <= End() ) && ( at >= Begin() )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf( "Assertion test failed - wrong Exception\n" );
+    tet_result( TET_FAIL );
+  }
+
+  // Test insertion part-way through a largish array retains ordering:
+
+  // Build vector with hole in sequence:
+  Vector< unsigned int > longerVector;
+  const unsigned int insertionPoint = 131571u;
+  const unsigned int finalLength = 262143u;
+  for( unsigned int i = 0u; i < insertionPoint; ++i )
+  {
+    longerVector.PushBack( i );
+  }
+  for( unsigned int i = insertionPoint; i < finalLength; ++i )
+  {
+    longerVector.PushBack( i + 1 );
+  }
+
+  // Fill the hole in the sequence:
+  longerVector.Insert( longerVector.Begin() + insertionPoint, insertionPoint );
+
+  // Check the sequence is monotonically increasing by one every time:
+  for( unsigned int i = 0u; i <= finalLength; ++i )
+  {
+    DALI_TEST_EQUALS( i, longerVector[i], TEST_LOCATION );
+  }
+
+  // Insert into an empty vector
+  Vector< unsigned int > vector;
+
+  vector.Insert( vector.End(), orderedVector.Begin(), orderedVector.End() );
+  for( unsigned int i = 0u; i < 10u; ++i )
+  {
+    DALI_TEST_EQUALS( i, vector[i], TEST_LOCATION );
+  }
+
+  vector.Clear();
+  vector.Insert( vector.Begin(), orderedVector.Begin(), orderedVector.End() );
+  for( unsigned int i = 0u; i < 10u; ++i )
+  {
+    DALI_TEST_EQUALS( i, vector[i], TEST_LOCATION );
+  }
+
+  // Insert nothing.
+  vector.Insert( vector.Begin(), orderedVector.Begin(), orderedVector.Begin() );
+  for( unsigned int i = 0u; i < 10u; ++i )
+  {
+    DALI_TEST_EQUALS( i, vector[i], TEST_LOCATION );
+  }
+
+  vector.Insert( vector.Begin() + 5, vector.Begin() + 5, vector.Begin() + 5 );
+  for( unsigned int i = 0u; i < 10u; ++i )
+  {
+    DALI_TEST_EQUALS( i, vector[i], TEST_LOCATION );
+  }
+
+  // AutoInsert
+  vector.Clear();
+  vector.PushBack( 0u );
+  vector.PushBack( 1u );
+  vector.PushBack( 2u );
+  vector.PushBack( 3u );
+
+  vector.Insert( vector.Begin() + 2, vector.Begin(), vector.End() );
+  DALI_TEST_EQUALS( 8u, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( 0u, vector[0u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 1u, vector[1u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 0u, vector[2u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 1u, vector[3u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 2u, vector[4u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 3u, vector[5u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 2u, vector[6u], TEST_LOCATION );
+  DALI_TEST_EQUALS( 3u, vector[7u], TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliVectorInsert02(void)
+{
+  tet_infoline("Testing Dali::Vector<char>::Insert(Iterator,Iterator,Iterator)");
+
+  Vector< char > vector;
+  DALI_TEST_EQUALS( ZERO, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, vector.Capacity(), TEST_LOCATION );
+  vector.PushBack( 1 );
+  vector.PushBack( 2 );
+  vector.PushBack( 3 );
+  vector.PushBack( 4 );
+  vector.PushBack( 5 );
+
+  Vector< char > vector2;
+  DALI_TEST_EQUALS( ZERO, vector2.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, vector2.Capacity(), TEST_LOCATION );
+  vector2.PushBack( 6 );
+  vector2.PushBack( 7 );
+  vector2.PushBack( 8 );
+  vector2.PushBack( 9 );
+  vector2.PushBack( 10 );
+
+  // Test insert at end
+  vector.Insert( vector.End(), vector2.Begin(), vector2.Begin() + 1u );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(6), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 1, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 2, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 3, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 3 ], 4, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 4 ], 5, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 5 ], 6, TEST_LOCATION );
+
+  // Test insert at begin
+  vector.Insert( vector.Begin(), vector2.Begin()+1, vector2.Begin() + 2u );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(7), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 7, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 1, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 2, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 3 ], 3, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 4 ], 4, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 5 ], 5, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 6 ], 6, TEST_LOCATION );
+
+  // Test insert in the middle
+  vector.Insert( vector.Begin() + 3, vector2.Begin()+3, vector2.End() );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(9), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 7, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 1, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 2, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 3 ], 9, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 4 ], 10, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 5 ], 3, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 6 ], 4, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 7 ], 5, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 8 ], 6, TEST_LOCATION );
+  END_TEST;
+}
+
+int UtcDaliVectorIntInsertAssert(void)
+{
+  tet_infoline("Testing Dali::Vector<char>::Insert(Iterator,Iterator,Iterator) asserts");
+
+  Vector< char > vector;
+  DALI_TEST_EQUALS( ZERO, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, vector.Capacity(), TEST_LOCATION );
+  vector.PushBack( 1 );
+  vector.PushBack( 2 );
+
+  Vector< char > vector2;
+  DALI_TEST_EQUALS( ZERO, vector2.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, vector2.Capacity(), TEST_LOCATION );
+  vector2.PushBack( 6 );
+  vector2.PushBack( 7 );
+  vector2.PushBack( 8 );
+  vector2.PushBack( 9 );
+  vector2.PushBack( 10 );
+
+  try
+  {
+    vector.Insert( vector.Begin() +  3u, vector2.Begin(), vector2.End() );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "( at <= End() ) && ( at >= Begin() )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+  try
+  {
+    vector.Insert( vector.Begin() -  1u, vector2.Begin(), vector2.End() );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "( at <= End() ) && ( at >= Begin() )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+  try
+  {
+    vector.Insert( vector.End(), vector2.End(), vector2.Begin() );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "( from <= to )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+  END_TEST;
+ }
+
+
+int UtcDaliVectorIntEraseRange(void)
+{
+  tet_infoline("Testing Dali::Vector<char>::Erase(Iterator,Iterator)");
+
+  Vector< char > vector;
+  DALI_TEST_EQUALS( ZERO, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, vector.Capacity(), TEST_LOCATION );
+
+  // Try to delete from empty vector.
+
+  vector.Erase( vector.Begin(), vector.End() );
+  DALI_TEST_EQUALS( ZERO, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, vector.Capacity(), TEST_LOCATION );
+
+  vector.PushBack( 1 );
+  vector.PushBack( 2 );
+  vector.PushBack( 3 );
+  vector.PushBack( 4 );
+  vector.PushBack( 5 );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(5), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 1, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 2, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 3, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 3 ], 4, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 4 ], 5, TEST_LOCATION );
+
+  Vector< char >::Iterator ret;
+
+  ret = vector.Erase( vector.Begin() + 1u, vector.Begin() + 2u );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(4), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 1, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 3, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 4, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 3 ], 5, TEST_LOCATION );
+  DALI_TEST_EQUALS( *ret, 3, TEST_LOCATION );
+
+  ret = vector.Erase( vector.Begin(), vector.Begin() + 2 );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(2), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 4, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 5, TEST_LOCATION );
+  DALI_TEST_EQUALS( *ret, 4, TEST_LOCATION );
+
+  // try erasing last
+  vector.PushBack( 99 );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(3), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 99, TEST_LOCATION );
+  ret = vector.Erase( vector.Begin() + 1u, vector.End() );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(1), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 4, TEST_LOCATION );
+  DALI_TEST_EQUALS( ret, vector.End(), TEST_LOCATION );
+
+  // try erasing all
+  vector.PushBack( 100 );
+  vector.PushBack( 101 );
+  vector.PushBack( 102 );
+
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(4), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 4, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 100, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 101, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 3 ], 102, TEST_LOCATION );
+
+  ret = vector.Erase( vector.Begin(), vector.End() );
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(0), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ret, vector.End(), TEST_LOCATION );
+
+  // try erase from Iterator to the same Iterator.
+  vector.PushBack( 100 );
+  vector.PushBack( 101 );
+  vector.PushBack( 102 );
+
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(3), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 100, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 101, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 102, TEST_LOCATION );
+
+  ret = vector.Erase( vector.Begin() + 1, vector.Begin() + 1 );
+
+  DALI_TEST_EQUALS( static_cast<Dali::VectorBase::SizeType>(3), vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 0 ], 100, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 1 ], 101, TEST_LOCATION );
+  DALI_TEST_EQUALS( vector[ 2 ], 102, TEST_LOCATION );
+
+  DALI_TEST_EQUALS( *ret, 101, TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliVectorIntEraseRangeAssert(void)
+{
+  tet_infoline("Testing Dali::Vector<char>::Erase(Iterator,Iterator) asserts");
+
+  Vector< char > vector;
+  DALI_TEST_EQUALS( ZERO, vector.Count(), TEST_LOCATION );
+  DALI_TEST_EQUALS( ZERO, vector.Capacity(), TEST_LOCATION );
+
+
+  // Add some elements.
+  vector.PushBack( 1 );
+  vector.PushBack( 2 );
+
+  // first out of bounds
+  try
+  {
+    vector.Erase( vector.Begin() + 3u, vector.Begin() + 4u );
+    tet_printf( "Assertion expected, but not occurred at %s\n", TEST_LOCATION );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "( first <= End() ) && ( first >= Begin() )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+  try
+  {
+    vector.Erase( vector.Begin() - 1u, vector.End() );
+    tet_printf( "Assertion expected, but not occurred at %s\n", TEST_LOCATION );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "( first <= End() ) && ( first >= Begin() )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+  // last out of bounds
+
+  try
+  {
+    vector.Erase( vector.Begin(), vector.Begin() + 3u );
+    tet_printf( "Assertion expected, but not occurred at %s\n", TEST_LOCATION );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "( last <= End() ) && ( last >= Begin() )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+  try
+  {
+    vector.Erase( vector.Begin(), vector.Begin() - 1u );
+    tet_printf( "Assertion expected, but not occurred at %s\n", TEST_LOCATION );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "( last <= End() ) && ( last >= Begin() )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+  vector.PushBack( 3 );
+
+  // first > last
+  try
+  {
+    vector.Erase( vector.Begin() + 2u, vector.Begin() + 1u );
+    tet_printf( "Assertion expected, but not occurred at %s\n", TEST_LOCATION );
+    tet_result(TET_FAIL);
+  }
+  catch( Dali::DaliException& e )
+  {
+    tet_printf("Assertion %s test at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
+    DALI_TEST_ASSERT( e, "( first <= last )", TEST_LOCATION );
+  }
+  catch( ... )
+  {
+    tet_printf("Assertion test failed - wrong Exception\n" );
+    tet_result(TET_FAIL);
+  }
+
+  END_TEST;
+}
+
 namespace
 {
 
index e3f037f..643c89f 100644 (file)
@@ -36,11 +36,11 @@ VectorBase::~VectorBase()
 
 VectorBase::SizeType VectorBase::Capacity() const
 {
-  SizeType capacity = 0;
+  SizeType capacity = 0u;
   if( mData )
   {
     SizeType* metadata = reinterpret_cast< SizeType* >( mData );
-    capacity = *(metadata - 2);
+    capacity = *(metadata - 2u);
   }
   return capacity;
 }
@@ -54,18 +54,18 @@ void VectorBase::Release()
     SizeType* metadata = reinterpret_cast< SizeType* >( mData );
     // TODO would be nice to memset to a bitpattern to catch illegal use of container after release
     // but that would require knowledge of the itemsize
-    free( metadata - 2 );
-    mData = 0;
+    free( metadata - 2u );
+    mData = 0u;
   }
 }
 
 void VectorBase::SetCount( SizeType count )
 {
-  // someone can call Resize( 0 ) before ever populating the vector
+  // someone can call Resize( 0u ) before ever populating the vector
   if( mData )
   {
     SizeType* metadata = reinterpret_cast< SizeType* >( mData );
-    *(metadata - 1) = count;
+    *(metadata - 1u) = count;
   }
 }
 
@@ -75,7 +75,7 @@ void VectorBase::Reserve( SizeType capacity, SizeType elementSize )
   SizeType oldCount = Count();
   if( capacity > oldCapacity )
   {
-    const SizeType wholeAllocation = sizeof(SizeType) * 2 + capacity * elementSize;
+    const SizeType wholeAllocation = sizeof(SizeType) * 2u + capacity * elementSize;
     void* wholeData = (void*)malloc( wholeAllocation );
 #if defined( DEBUG_ENABLED )
     // in debug build this will help identify a vector of uninitialized data
@@ -97,19 +97,16 @@ void VectorBase::Reserve( SizeType capacity, SizeType elementSize )
 
 void VectorBase::Copy( const VectorBase& vector, SizeType elementSize )
 {
-  if( this != &vector )
-  {
-    // release old data
-    Release();
-    // reserve space based on source capacity
-    const SizeType capacity = vector.Capacity();
-    Reserve( capacity, elementSize );
-    // copy over whole data
-    const SizeType wholeAllocation = sizeof(SizeType) * 2 + capacity * elementSize;
-    SizeType* srcData = reinterpret_cast< SizeType* >( vector.mData );
-    SizeType* dstData = reinterpret_cast< SizeType* >( mData );
-    memcpy( dstData - 2, srcData - 2, wholeAllocation );
-  }
+  // release old data
+  Release();
+  // reserve space based on source capacity
+  const SizeType capacity = vector.Capacity();
+  Reserve( capacity, elementSize );
+  // copy over whole data
+  const SizeType wholeAllocation = sizeof(SizeType) * 2u + capacity * elementSize;
+  SizeType* srcData = reinterpret_cast< SizeType* >( vector.mData );
+  SizeType* dstData = reinterpret_cast< SizeType* >( mData );
+  memcpy( dstData - 2u, srcData - 2u, wholeAllocation );
 }
 
 void VectorBase::Swap( VectorBase& vector )
@@ -128,7 +125,41 @@ void VectorBase::Erase( char* address, SizeType elementSize )
     SizeType numberOfBytes = endAddress - startAddress;
     // addresses overlap so use memmove
     memmove( address, startAddress, numberOfBytes );
-    SetCount( Count() - 1 );
+    SetCount( Count() - 1u );
+  }
+}
+
+char* VectorBase::Erase( char* first, char* last, SizeType elementSize )
+{
+  char* next = NULL;
+
+  if( mData )
+  {
+    char* startAddress = last;
+    const char* endAddress = reinterpret_cast< char* >( mData ) + Count() * elementSize;
+    SizeType numberOfBytes = endAddress - startAddress;
+    // addresses overlap so use memmove
+    memmove( first, startAddress, numberOfBytes );
+    SetCount( Count() - ( last - first ) / elementSize );
+
+    next = first;
+  }
+
+  return next;
+}
+
+void VectorBase::CopyMemory( char* destination, const char* source, size_t numberOfBytes )
+{
+  if( ( ( source < destination ) && ( source + numberOfBytes > destination ) ) ||
+      ( ( destination < source ) && ( destination + numberOfBytes > source ) ) )
+  {
+    // If there is overlap, use memmove.
+    memmove( destination, source, numberOfBytes );
+  }
+  else
+  {
+    // It's safe to use memcpy if there isn't overlap.
+    memcpy( destination, source, numberOfBytes );
   }
 }
 
index 595dc5e..6b5dc8f 100644 (file)
  *
  */
 
-
 // EXTERNAL INCLUDES
 #include <cstddef>
 
 // INTERNAL INCLUDES
 #include <dali/public-api/common/dali-common.h>
+#include <dali/public-api/math/math-utils.h>
 
 /**
  * @brief For DALi internal use asserts are enabled in debug builds.
@@ -81,16 +81,15 @@ public: // API
    */
   SizeType Count() const
   {
-    SizeType items = 0;
+    SizeType items = 0u;
     if( mData )
     {
       SizeType* metadata = reinterpret_cast< SizeType* >( mData );
-      items = *(metadata - 1);
+      items = *(metadata - 1u);
     }
     return items;
   }
 
-
   /**
    * @return The count of elements in this vector.
    */
@@ -153,6 +152,28 @@ protected: // for Derived classes
    */
   void Erase( char* address, SizeType elementSize );
 
+  /**
+   * @brief Erase a range of elements.
+   *
+   * Does not change capacity.
+   * @param[in] first Address to the first element to be erased.
+   * @param[in] last Address to the last element to be erased.
+   * @param[in] elementSize Size of one of the elements to be erased.
+   * @return address pointing to the next element of the last one.
+   */
+  char* Erase( char* first, char* last, SizeType elementSize );
+
+  /**
+   * Copies a number of bytes from \e source to \e destination.
+   *
+   * \e source and \e destination must not overlap.
+   *
+   * @param[in] destination Pointer to the destination address.
+   * @param[in] source Pointer to the source address.
+   * @param[in] numberOfBytes The number of bytes to be copied.
+   */
+  void CopyMemory( char* destination, const char* source, size_t numberOfBytes );
+
 private:
 
   // not copiable as it does not know the size of elements
@@ -262,6 +283,55 @@ protected: // API for deriving classes
     VectorBase::Erase( address, elementSize );
   }
 
+  /**
+   * @brief Erase a range of elements. Does not change capacity.
+   *
+   * @param[in] first Address to the first element to be erased.
+   * @param[in] last Address to the last element to be erased.
+   * @param[in] elementSize Size of one of the elements to be erased.
+   * @return address pointing to the next element of the last one.
+   */
+  char* Erase( char* first, char* last, SizeType elementSize )
+  {
+    return VectorBase::Erase( first, last, elementSize );
+  }
+
+  /**
+   * @brief Inserts the given elements into the vector.
+   *
+   * @param[in] at Address where to insert the elements into the vector.
+   * @param[in] from Address to the first element to be inserted.
+   * @param[in] to Address to the last element to be inserted.
+   * @param[in] elementSize Size of one of the elements to be inserted.
+   */
+  void Insert( char* at, char* from, char* to, SizeType elementSize )
+  {
+    const SizeType size = to - from;
+    const SizeType count = Count();
+    const SizeType newCount = count + size / elementSize;
+
+    if( newCount > Capacity() )
+    {
+      // Calculate the at offset as the pointer is invalid after the Reserve() call.
+      std::size_t offset = at - reinterpret_cast<char*>( mData );
+
+      // need more space
+      Reserve( NextPowerOfTwo( newCount ), elementSize ); // reserve enough space to store at least the next power of two elements of the new required size.
+
+      // Set the new at pointer.
+      at = reinterpret_cast<char*>( mData ) + offset;
+    }
+    // set new count first as otherwise the debug assert will hit us
+    SetCount( newCount );
+
+    // Move current items to a new position inside the vector.
+    CopyMemory( at + size,
+                at,
+                ( reinterpret_cast<char*>( mData ) + count * elementSize ) - at );
+
+    // Copy the given items.
+    CopyMemory( at, from, size );
+  }
 };
 
 /**
@@ -364,6 +434,7 @@ public: // API
   }
 
   /**
+   * @pre index must be in the vector's range.
    * @param  index of the element.
    * @return reference to the element for given index.
    */
@@ -374,6 +445,7 @@ public: // API
   }
 
   /**
+   * @pre index must be in the vector's range.
    * @param  index of the element.
    * @return reference to the element for given index.
    */
@@ -389,7 +461,11 @@ public: // API
   /**
    * @brief Push back an element to the vector.
    *
-   * @param element to be added.
+   * The underlying storage may be reallocated to provide space.
+   * If this occurs, all pre-existing pointers into the vector will
+   * become invalid.
+   *
+   * @param[in] element to be added.
    */
   void PushBack( const ItemType& element )
   {
@@ -399,7 +475,7 @@ public: // API
     if( newCount > capacity )
     {
       // need more space
-      Reserve( newCount << 1 ); // reserve double the current count
+      Reserve( newCount << 1u ); // reserve double the current count
     }
     // set new count first as otherwise the debug assert will hit us
     VectorBase::SetCount( newCount );
@@ -407,6 +483,65 @@ public: // API
   }
 
   /**
+   *@brief Insert an element to the vector.
+   *
+   * Elements after \e at are moved one position to the right.
+   *
+   * The underlying storage may be reallocated to provide space.
+   * If this occurs, all pre-existing pointers into the vector will
+   * become invalid.
+   *
+   * @pre Iterator at must be in the vector's range ( Vector::Begin(), Vector::End() ).
+   *
+   * @param[in] at Iterator where to insert the elements into the vector.
+   * @param[in] element to be added.
+   */
+  void Insert( Iterator at, const ItemType& element )
+  {
+    DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
+    const SizeType size = sizeof( ItemType );
+    char* address = const_cast<char*>( reinterpret_cast<const char*>( &element ) );
+    VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
+                                        address,
+                                        address + size,
+                                        size );
+  }
+
+  /**
+   * @brief Inserts the given elements into the vector.
+   *
+   * Elements after \e at are moved the number of given elements positions to the right.
+   *
+   * The underlying storage may be reallocated to provide space.
+   * If this occurs, all pre-existing pointers into the vector will
+   * become invalid.
+   *
+   * @pre Iterator \e at must be in the vector's range ( Vector::Begin(), Vector::End() ).
+   * @pre Iterators \e from and \e to must be valid iterators.
+   * @pre Iterator \e from must not be grater than Iterator \e to.
+   *
+   * @param[in] at Iterator where to insert the elements into the vector.
+   * @param[in] from Iterator to the first element to be inserted.
+   * @param[in] to Iterator to the last element to be inserted.
+   */
+  void Insert( Iterator at, Iterator from, Iterator to )
+  {
+    DALI_ASSERT_VECTOR( ( at <= End() ) && ( at >= Begin() ) && "Iterator not inside vector" );
+    DALI_ASSERT_VECTOR( ( from <= to ) && "from address can't be greater than to" );
+
+    if( from == to )
+    {
+      // nothing to copy.
+      return;
+    }
+
+    VectorAlgorithms<BaseType>::Insert( reinterpret_cast< char* >( at ),
+                                        reinterpret_cast< char* >( from ),
+                                        reinterpret_cast< char* >( to ),
+                                        sizeof( ItemType ) );
+  }
+
+  /**
    * @brief Reserve space in the vector.
    *
    * Reserving less than current Capacity is a no-op.
@@ -421,7 +556,7 @@ public: // API
    * @brief Resize the vector. Does not change capacity.
    *
    * @param count to resize to.
-   * @param item to insert to the new indeces.
+   * @param item to insert to the new indices.
    */
   void Resize( SizeType count, ItemType item = ItemType() )
   {
@@ -436,7 +571,7 @@ public: // API
       // remember how many new items get added
       SizeType newItems = count - oldCount;
       Reserve( count );
-      for( ; newItems > 0; --newItems )
+      for( ; newItems > 0u; --newItems )
       {
         PushBack( item );
       }
@@ -447,14 +582,16 @@ public: // API
    * @brief Erase an element.
    *
    * Does not change capacity. Other elements get moved.
+   *
+   * @pre Iterator \e iterator must be within the vector's range ( Vector::Begin(), Vector::End() - 1 ).
+   *
    * @param iterator Iterator pointing to item to remove.
    * @return Iterator pointing to next element.
    */
   Iterator Erase( Iterator iterator )
   {
-    DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
-    DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin() ) && "Iterator not inside vector" );
-    if( iterator < ( End() - 1 ) )
+    DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
+    if( iterator < ( End() - 1u ) )
     {
       VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( iterator ), sizeof( ItemType ) );
     }
@@ -467,25 +604,64 @@ public: // API
   }
 
   /**
+   * @brief Erase a range of elements.
+   *
+   * Does not change capacity. Other elements get moved.
+   *
+   * @pre Iterator \e first must be in the vector's range ( Vector::Begin(), Vector::End() ).
+   * @pre Iterator \e last must be in the vector's range ( Vector::Begin(), Vector::End() ).
+   * @pre Iterator \e first must not be grater than Iterator \e last.
+   *
+   * @param[in] first Iterator to the first element to be erased.
+   * @param[in] last Iterator to the last element to be erased.
+   *
+   * @return Iterator pointing to the next element of the last one.
+   */
+  Iterator Erase( Iterator first, Iterator last )
+  {
+    DALI_ASSERT_VECTOR( ( first <= End() ) && ( first >= Begin() ) && "Iterator not inside vector" );
+    DALI_ASSERT_VECTOR( ( last <= End() ) && ( last >= Begin() ) && "Iterator not inside vector" );
+    DALI_ASSERT_VECTOR( ( first <= last ) && "first iterator greater than last" );
+
+    Iterator nextElement;
+
+    if( last == End() )
+    {
+      // Erase up to the end.
+      VectorBase::SetCount( VectorBase::Count() - ( last - first ) );
+      nextElement = End();
+    }
+    else
+    {
+      nextElement = reinterpret_cast<Iterator>( VectorAlgorithms<BaseType>::Erase( reinterpret_cast< char* >( first ),
+                                                                                   reinterpret_cast< char* >( last ),
+                                                                                   sizeof( ItemType ) ) );
+    }
+
+    return nextElement;
+  }
+
+  /**
    * @brief Removes an element.
    *
    * Does not maintain order.  Swaps the element with end and
    * decreases size by one.  This is much faster than Erase so use
    * this in case order does not matter. Does not change capacity.
    *
+   * @pre Iterator \e iterator must be in the vector's range ( Vector::Begin(), Vector::End() - 1 ).
+   *
    * @param iterator Iterator pointing to item to remove.
    */
   void Remove( Iterator iterator )
   {
-    DALI_ASSERT_VECTOR( VectorBase::mData && "Vector is empty" );
-    Iterator end = End();
-    DALI_ASSERT_VECTOR( (iterator < end) && (iterator >= Begin() ) && "Iterator not inside vector" );
-    Iterator last = end - 1;
-    if( last != iterator )
+    DALI_ASSERT_VECTOR( (iterator < End()) && (iterator >= Begin()) && "Iterator not inside vector" );
+
+    Iterator last = End() - 1u;
+    if( last > iterator )
     {
       std::swap( *iterator, *last );
     }
-    VectorBase::SetCount( VectorBase::Count() - 1 );
+    VectorBase::SetCount( VectorBase::Count() - 1u );
   }
 
   /**
@@ -513,7 +689,6 @@ public: // API
   {
     VectorAlgorithms<BaseType>::Release();
   }
-
 };
 
 } // namespace Dali