Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r81990 - in sandbox-branches/geometry/index: boost/geometry/extensions/index test
From: adam.wulkiewicz_at_[hidden]
Date: 2012-12-15 18:21:59


Author: awulkiew
Date: 2012-12-15 18:21:59 EST (Sat, 15 Dec 2012)
New Revision: 81990
URL: http://svn.boost.org/trac/boost/changeset/81990

Log:
Added static_vector::erase() and tests.
Text files modified:
   sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp | 46 +++++++++++++++++++++++++++++++++++++
   sandbox-branches/geometry/index/test/static_vector.cpp | 49 ++++++++++++++++++++++++++++++++++++++++
   2 files changed, 95 insertions(+), 0 deletions(-)

Modified: sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp
==============================================================================
--- sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp (original)
+++ sandbox-branches/geometry/index/boost/geometry/extensions/index/static_vector.hpp 2012-12-15 18:21:59 EST (Sat, 15 Dec 2012)
@@ -159,6 +159,33 @@
         this->destroy(this->ptr(m_size));
     }
 
+ void erase(iterator position)
+ {
+ // TODO change name of this macro
+ BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type dist = std::distance(this->begin(), position));
+ BOOST_ASSERT_MSG(0 <= dist && dist < m_size, "invalid iterator");
+
+ this->move(position + 1, this->end(), position); // may throw
+ this->destroy(this->end() - 1);
+ --m_size;
+ }
+
+ void erase(iterator first, iterator last)
+ {
+ // TODO change name of this macro
+ BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type distf = std::distance(this->begin(), first));
+ BOOST_GEOMETRY_INDEX_ASSERT_UNUSED_PARAM(difference_type distl = std::distance(this->begin(), last));
+ BOOST_ASSERT_MSG(0 <= distf && distf < m_size, "invalid iterator");
+ BOOST_ASSERT_MSG(0 <= distl && distl < m_size, "invalid iterator");
+
+ difference_type n = std::distance(first, last);
+ BOOST_ASSERT_MSG(0 <= n, "invalid iterator");
+
+ this->move(last, this->end(), first); // may throw
+ this->destroy(this->end() - n, this->end());
+ m_size -= n;
+ }
+
     // basic
     template <typename Iterator>
     void assign(Iterator first, Iterator last)
@@ -358,6 +385,25 @@
         std::copy(first, last, dst); // may throw
     }
 
+ // move
+
+ void move(iterator first, iterator last, iterator dst)
+ {
+ this->move_dispatch(first, last, dst, has_trivial_assign<value_type>()); // may throw
+ }
+
+ void move_dispatch(value_type * first, value_type * last, value_type * dst,
+ boost::true_type const& /*has_trivial_assign*/)
+ {
+ ::memmove(dst, first, sizeof(value_type) * std::distance(first, last));
+ }
+
+ void move_dispatch(value_type * first, value_type * last, value_type * dst,
+ boost::false_type const& /*has_trivial_assign*/)
+ {
+ std::copy(first, last, dst); // may throw
+ }
+
     // uninitialized_copy
 
     template <typename Iterator>

Modified: sandbox-branches/geometry/index/test/static_vector.cpp
==============================================================================
--- sandbox-branches/geometry/index/test/static_vector.cpp (original)
+++ sandbox-branches/geometry/index/test/static_vector.cpp 2012-12-15 18:21:59 EST (Sat, 15 Dec 2012)
@@ -299,6 +299,50 @@
     test_compare_ranges(s.rbegin(), s.rend(), v.begin(), v.end());
 }
 
+template <typename T, size_t N>
+void test_erase_nd()
+{
+ static_vector<T, N> s;
+
+ for ( size_t i = 0 ; i < N ; ++i )
+ s.push_back(T(i));
+
+ {
+ static_vector<T, N> s1(s);
+
+ for ( size_t i = 1 ; i < N ; ++i )
+ {
+ BOOST_CHECK(s1.front() == T(i-1));
+ s1.erase(s1.begin());
+ BOOST_CHECK(s1.front() == T(i));
+ }
+ BOOST_CHECK(s1.size() == 1);
+ }
+
+ {
+ static_vector<T, N> s1(s);
+
+ for ( size_t i = N ; i > 1 ; --i )
+ {
+ BOOST_CHECK(s1.back() == T(i-1));
+ s1.erase(s1.end() - 1);
+ BOOST_CHECK(s1.back() == T(i-2));
+ }
+ BOOST_CHECK(s1.size() == 1);
+ }
+
+ {
+ static_vector<T, N> s1(s);
+
+ for ( size_t i = 1 ; i < N - 2 ; i += 3 )
+ {
+ BOOST_CHECK(s1.front() == T(i-1));
+ s1.erase(s1.begin(), s1.begin() + 3);
+ BOOST_CHECK(s1.front() == T(i+2));
+ }
+ }
+}
+
 int test_main(int, char* [])
 {
     BOOST_CHECK(counting_value::count() == 0);
@@ -348,5 +392,10 @@
     test_iterators_nd<counting_value, 10>();
     BOOST_CHECK(counting_value::count() == 0);
 
+ test_erase_nd<int, 10>();
+ test_erase_nd<value_nd, 10>();
+ test_erase_nd<counting_value, 10>();
+ BOOST_CHECK(counting_value::count() == 0);
+
     return 0;
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk