Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82045 - in sandbox/static_vector: boost/container test
From: adam.wulkiewicz_at_[hidden]
Date: 2012-12-16 21:00:41


Author: awulkiew
Date: 2012-12-16 21:00:40 EST (Sun, 16 Dec 2012)
New Revision: 82045
URL: http://svn.boost.org/trac/boost/changeset/82045

Log:
added static_vector::swap() for a container with different capacity
Text files modified:
   sandbox/static_vector/boost/container/static_vector.hpp | 66 ++++++++++++++++++++++++++++++++-------
   sandbox/static_vector/test/static_vector.cpp | 55 +++++++++++++++++++++++++++-----
   2 files changed, 100 insertions(+), 21 deletions(-)

Modified: sandbox/static_vector/boost/container/static_vector.hpp
==============================================================================
--- sandbox/static_vector/boost/container/static_vector.hpp (original)
+++ sandbox/static_vector/boost/container/static_vector.hpp 2012-12-16 21:00:40 EST (Sun, 16 Dec 2012)
@@ -110,8 +110,14 @@
 
 }} // namespace detail::static_vector
 
-// Eventually size_type_t = typename boost::uint_value_t<Capacity>::least
-// however it will probably be slower
+// TODO
+
+// StoredSizeType needed?
+
+// Should StoredSizeType be typename boost::uint_value_t<Capacity>::least by default?
+// It would probably be slower
+
+// Pass error_handling as a template parameter and implement various ones?
 
 template <typename Value, std::size_t Capacity, typename StoredSizeType = std::size_t>
 class static_vector
@@ -140,14 +146,14 @@
     explicit static_vector(size_type count)
         : m_size(0)
     {
- resize(count); // may throw
+ this->resize(count); // may throw
     }
 
     // strong
     static_vector(size_type count, value_type const& value)
         : m_size(0)
     {
- resize(count, value); // may throw
+ this->resize(count, value); // may throw
     }
 
     // strong
@@ -158,8 +164,8 @@
     }
 
     // strong
- template <size_t C>
- static_vector(static_vector<value_type, C> const& other)
+ template <std::size_t C, typename S>
+ static_vector(static_vector<value_type, C, S> const& other)
         : m_size(other.size())
     {
         errh::check_capacity(other.size()); // may throw
@@ -173,22 +179,22 @@
         : m_size(0)
     {
         // TODO - add MPL_ASSERT, check if Iterator is really an iterator
- assign(first, last); // may throw
+ this->assign(first, last); // may throw
     }
 
     // basic
     static_vector & operator=(static_vector const& other)
     {
- assign(other.begin(), other.end()); // may throw
+ this->assign(other.begin(), other.end()); // may throw
 
         return *this;
     }
 
     // basic
- template <size_t C>
- static_vector & operator=(static_vector<value_type, C> const& other)
+ template <std::size_t C, typename S>
+ static_vector & operator=(static_vector<value_type, C, S> const& other)
     {
- assign(other.begin(), other.end()); // may throw
+ this->assign(other.begin(), other.end()); // may throw
 
         return *this;
     }
@@ -218,8 +224,38 @@
             for (; other_it != other.end() ; ++it, ++other_it)
                 boost::swap(*it, *other_it); // may throw
             this->uninitialized_copy(it, this->end(), other_it); // may throw
- this->destroy(it, it->end());
+ this->destroy(it, this->end());
             boost::swap(m_size, other.m_size);
+ };
+ }
+
+ // basic
+ template <std::size_t C, typename S>
+ void swap(static_vector<value_type, C, S> & other)
+ {
+ errh::check_capacity(*this, other.size());
+ errh::check_capacity(other, this->size());
+
+ iterator it = this->begin();
+ iterator other_it = other.begin();
+
+ if ( this->size() < other.size() )
+ {
+ for (; it != this->end() ; ++it, ++other_it)
+ boost::swap(*it, *other_it); // may throw
+ iterator other_it_backup = other_it;
+ for (; other_it != other.end() ; ++other_it)
+ this->push_back(*other_it); // may throw
+ other.erase(other_it_backup, other.end());
+ }
+ else
+ {
+ for (; other_it != other.end() ; ++it, ++other_it)
+ boost::swap(*it, *other_it); // may throw
+ iterator it_backup = it;
+ for (; it != this->end() ; ++it)
+ other.push_back(*it); // may throw
+ this->erase(it_backup, this->end());
         }
     }
 
@@ -1163,6 +1199,12 @@
     x.swap(y);
 }
 
+template<typename V, std::size_t C1, typename S1, std::size_t C2, typename S2>
+inline void swap(static_vector<V, C1, S1> & x, static_vector<V, C2, S2> & y)
+{
+ x.swap(y);
+}
+
 }} // namespace boost::container
 
 #endif // BOOST_CONTAINER_STATIC_VECTOR_HPP

Modified: sandbox/static_vector/test/static_vector.cpp
==============================================================================
--- sandbox/static_vector/test/static_vector.cpp (original)
+++ sandbox/static_vector/test/static_vector.cpp 2012-12-16 21:00:40 EST (Sun, 16 Dec 2012)
@@ -490,15 +490,47 @@
 template <typename T, size_t N>
 void test_swap_nd()
 {
- static_vector<T, N> v;
- static_vector<T, N/2> s;
-
- for (size_t i = 0 ; i < N ; ++i )
- v.push_back(T(i));
- for (size_t i = 0 ; i < N/2 ; ++i )
- s.push_back(T(i));
-
- //TODO
+ {
+ static_vector<T, N> v;
+ static_vector<T, N> s;
+
+ for (size_t i = 0 ; i < N ; ++i )
+ v.push_back(T(i));
+ for (size_t i = 0 ; i < N/2 ; ++i )
+ s.push_back(T(i));
+
+ s.swap(v);
+
+ BOOST_CHECK(v.size() == N/2);
+ BOOST_CHECK(s.size() == N);
+ for (size_t i = 0 ; i < N/2 ; ++i )
+ BOOST_CHECK(v[i] == T(i));
+ for (size_t i = 0 ; i < N ; ++i )
+ BOOST_CHECK(s[i] == T(i));
+ }
+ {
+ static_vector<T, N> v;
+ static_vector<T, N/2> s;
+
+ for (size_t i = 0 ; i < N/2 ; ++i )
+ v.push_back(T(i));
+ for (size_t i = 0 ; i < N/3 ; ++i )
+ s.push_back(T(i));
+
+ s.swap(v);
+
+ BOOST_CHECK(v.size() == N/3);
+ BOOST_CHECK(s.size() == N/2);
+ for (size_t i = 0 ; i < N/3 ; ++i )
+ BOOST_CHECK(v[i] == T(i));
+ for (size_t i = 0 ; i < N/2 ; ++i )
+ BOOST_CHECK(s[i] == T(i));
+ }
+ {
+ static_vector<T, N> v(N, T(0));
+ static_vector<T, N/2> s(N/2, T(1));
+ BOOST_CHECK_THROW(s.swap(v), std::bad_alloc);
+ }
 }
 
 int test_main(int, char* [])
@@ -570,5 +602,10 @@
     test_exceptions_nd<counting_value, 10>();
     BOOST_CHECK(counting_value::count() == 0);
 
+ test_swap_nd<int, 10>();
+ test_swap_nd<value_nd, 10>();
+ test_swap_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