Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r82097 - in sandbox/static_vector/boost/container: . detail
From: adam.wulkiewicz_at_[hidden]
Date: 2012-12-19 10:11:21


Author: awulkiew
Date: 2012-12-19 10:11:20 EST (Wed, 19 Dec 2012)
New Revision: 82097
URL: http://svn.boost.org/trac/boost/changeset/82097

Log:
destroy(), construct(), uninitialized_copy_s() moved to detail::static_vector namespace.
Text files modified:
   sandbox/static_vector/boost/container/detail/static_vector_util.hpp | 128 ++++++++++++++++-
   sandbox/static_vector/boost/container/static_vector.hpp | 279 ++++++++++++---------------------------
   2 files changed, 202 insertions(+), 205 deletions(-)

Modified: sandbox/static_vector/boost/container/detail/static_vector_util.hpp
==============================================================================
--- sandbox/static_vector/boost/container/detail/static_vector_util.hpp (original)
+++ sandbox/static_vector/boost/container/detail/static_vector_util.hpp 2012-12-19 10:11:20 EST (Wed, 19 Dec 2012)
@@ -12,8 +12,10 @@
 #ifndef BOOST_CONTAINER_DETAIL_STATIC_VECTOR_UTIL_HPP
 #define BOOST_CONTAINER_DETAIL_STATIC_VECTOR_UTIL_HPP
 
+#include <cstddef>
 #include <cstring>
 #include <memory>
+#include <limits>
 
 #include <boost/mpl/if.hpp>
 #include <boost/mpl/and.hpp>
@@ -63,7 +65,7 @@
>
 {};
 
-// copy
+// copy(I, I, O)
 
 template <typename V>
 inline V * copy_dispatch(const V * first, const V * last, V * dst,
@@ -95,7 +97,7 @@
     return copy_dispatch(first, last, dst, use_memcpy()); // may throw
 }
 
-// uninitialized_copy
+// uninitialized_copy(I, I, O)
 
 template <typename V>
 V * uninitialized_copy_dispatch(const V * first, const V * last, V * dst,
@@ -127,7 +129,7 @@
     return uninitialized_copy_dispatch(first, last, dst, use_memcpy()); // may throw
 }
 
-// uninitialized_fill
+// uninitialized_fill(I, V)
 
 template <typename V>
 void uninitialized_fill_dispatch(V * ptr, V const& v,
@@ -140,7 +142,7 @@
 void uninitialized_fill_dispatch(I pos, V const& v,
                                  boost::mpl::bool_<false> const& /*use_memcpy*/)
 {
- new (static_cast<void*>(&*pos)) V(v); // may throw
+ new (static_cast<void*>(boost::addressof(*pos))) V(v); // may throw
 }
 
 template <typename I, typename V>
@@ -156,7 +158,7 @@
     uninitialized_fill_dispatch(dst, v, use_memcpy()); // may throw
 }
 
-// move
+// move(I, I, O)
 
 // TODO use boost::move(I, I, O) instead of copy
 
@@ -191,7 +193,7 @@
     return move_dispatch(first, last, dst, use_memmove()); // may throw
 }
 
-// move_backward
+// move_backward(BDI, BDI, BDO)
 
 // TODO use boost::move_backward(I, I, O) instead of copy_backward
 
@@ -226,7 +228,7 @@
     return move_backward_dispatch(first, last, dst, use_memmove()); // may throw
 }
 
-// fill
+// fill(I, V)
 
 template <typename V>
 void fill_dispatch(V * ptr, V const& v,
@@ -255,6 +257,83 @@
     fill_dispatch(pos, v, use_memcpy()); // may throw
 }
 
+// destroy(I, I)
+
+template <typename I>
+void destroy_dispatch(I /*first*/, I /*last*/,
+ boost::true_type const& /*has_trivial_destructor*/)
+{}
+
+template <typename I>
+void destroy_dispatch(I first, I last,
+ boost::false_type const& /*has_trivial_destructor*/)
+{
+ typedef typename boost::iterator_value<I>::type value_type;
+ for ( ; first != last ; ++first )
+ first->~value_type();
+}
+
+template <typename I>
+void destroy(I first, I last)
+{
+ typedef typename boost::iterator_value<I>::type value_type;
+ destroy_dispatch(first, last, has_trivial_destructor<value_type>());
+}
+
+// destroy(I)
+
+template <typename I>
+void destroy_dispatch(I /*pos*/,
+ boost::true_type const& /*has_trivial_destructor*/)
+{}
+
+template <typename I>
+void destroy_dispatch(I pos,
+ boost::false_type const& /*has_trivial_destructor*/)
+{
+ typedef typename boost::iterator_value<I>::type value_type;
+ pos->~value_type();
+}
+
+template <typename I>
+void destroy(I pos)
+{
+ typedef typename boost::iterator_value<I>::type value_type;
+ destroy_dispatch(pos, has_trivial_destructor<value_type>());
+}
+
+// construct
+
+template <typename I>
+void construct_dispatch(I /*first*/, I /*last*/,
+ boost::true_type const& /*has_trivial_constructor*/)
+{}
+
+template <typename I>
+void construct_dispatch(I first, I last,
+ boost::false_type const& /*has_trivial_constructor*/)
+{
+ typedef typename boost::iterator_value<I>::type value_type;
+ I it = first;
+ try
+ {
+ for ( ; it != last ; ++it )
+ new (it) value_type(); // may throw
+ }
+ catch(...)
+ {
+ destroy(first, it);
+ throw;
+ }
+}
+
+template <typename I>
+void construct(I first, I last)
+{
+ typedef typename boost::iterator_value<I>::type value_type;
+ construct_dispatch(first, last, has_trivial_constructor<value_type>()); // may throw
+}
+
 #else // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
 
 template <typename I, typename O>
@@ -264,37 +343,62 @@
 }
 
 template <typename I, typename F>
-F uninitialized_copy(I first, I last, F dst)
+inline F uninitialized_copy(I first, I last, F dst)
 {
     return std::uninitialized_copy(first, last, dst); // may throw
 }
 
 template <typename I, typename V>
-void uninitialized_fill(I pos, V const& v)
+inline void uninitialized_fill(I pos, V const& v)
 {
     new (static_cast<void*>(&*pos)) V(v); // may throw
 }
 
 template <typename I, typename O>
-O move(I first, I last, O dst)
+inline O move(I first, I last, O dst)
 {
     return std::copy(first, last, dst); // may throw
 }
 
 template <typename BDI, typename BDO>
-BDO move_backward(BDI first, BDI last, BDO dst)
+inline BDO move_backward(BDI first, BDI last, BDO dst)
 {
     return std::copy_backward(first, last, dst); // may throw
 }
 
 template <typename I, typename V>
-void fill(I pos, V const& v)
+inline void fill(I pos, V const& v)
 {
     *pos = v; // may throw
 }
 
 #endif // BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
 
+// uninitialized_copy_checked
+
+template <typename I, typename F>
+inline std::size_t uninitialized_copy_s(I first, I last, F dest, std::size_t max_count)
+{
+ std::size_t count = 0;
+ F it = dest;
+ try
+ {
+ for ( ; first != last ; ++it, ++first, ++count )
+ {
+ if ( max_count <= count )
+ return (std::numeric_limits<std::size_t>::max)();
+
+ uninitialized_fill(it, *first); // may throw
+ }
+ }
+ catch(...)
+ {
+ destroy(dest, it);
+ throw;
+ }
+ return count;
+}
+
 }}}} // namespace boost::container::detail::static_vector
 
 #endif // BOOST_CONTAINER_DETAIL_STATIC_VECTOR_UTIL_HPP

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-19 10:11:20 EST (Wed, 19 Dec 2012)
@@ -12,7 +12,6 @@
 
 #include <boost/container/detail/static_vector_util.hpp>
 
-#include <cstddef>
 #include <stdexcept>
 
 #include <boost/assert.hpp>
@@ -46,8 +45,7 @@
 struct error_handling
 {
     template <typename V, std::size_t Capacity, typename S>
- static void check_capacity(container::static_vector<V, Capacity, S> const&,
- typename container::static_vector<V, Capacity, S>::size_type s)
+ static void check_capacity(container::static_vector<V, Capacity, S> const&, std::size_t s)
     {
         //BOOST_ASSERT_MSG(s <= Capacity, "size can't exceed the capacity");
         if ( Capacity < s )
@@ -167,7 +165,8 @@
     static_vector(static_vector const& other)
         : m_size(other.size())
     {
- this->uninitialized_copy(other.begin(), other.end(), this->begin()); // may throw
+ namespace sv = detail::static_vector;
+ sv::uninitialized_copy(other.begin(), other.end(), this->begin()); // may throw
     }
 
     // strong
@@ -177,7 +176,8 @@
     {
         errh::check_capacity(other.size()); // may throw
         
- this->uninitialized_copy(other.begin(), other.end(), this->begin()); // may throw
+ namespace sv = detail::static_vector;
+ sv::uninitialized_copy(other.begin(), other.end(), this->begin()); // may throw
     }
 
     // strong
@@ -209,13 +209,15 @@
     // nothrow
     ~static_vector()
     {
- this->destroy(this->begin(), this->end());
+ namespace sv = detail::static_vector;
+ sv::destroy(this->begin(), this->end());
     }
 
     // nothrow
     // swap (note: linear complexity)
     void swap(static_vector & other)
     {
+// namespace sv = detail::static_vector;
 // iterator it = this->begin();
 // iterator other_it = other.begin();
 
@@ -223,16 +225,16 @@
 // {
 // for (; it != this->end() ; ++it, ++other_it)
 // boost::swap(*it, *other_it); // may throw
-// this->uninitialized_copy(other_it, other.end(), it); // may throw
-// this->destroy(other_it, other.end());
+// sv::uninitialized_copy(other_it, other.end(), it); // may throw
+// sv::destroy(other_it, other.end());
 // boost::swap(m_size, other.m_size);
 // }
 // else
 // {
 // 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, this->end());
+// sv::uninitialized_copy(it, this->end(), other_it); // may throw
+// sv::destroy(it, this->end());
 // boost::swap(m_size, other.m_size);
 // };
 
@@ -275,15 +277,17 @@
     // strong
     void resize(size_type count)
     {
+ namespace sv = detail::static_vector;
+
         if ( count < m_size )
         {
- this->destroy(this->begin() + count, this->end());
+ sv::destroy(this->begin() + count, this->end());
         }
         else
         {
             errh::check_capacity(*this, count); // may throw
 
- this->construct(this->end(), this->begin() + count); // may throw
+ sv::construct(this->end(), this->begin() + count); // may throw
         }
         m_size = count; // update end
     }
@@ -293,7 +297,8 @@
     {
         if ( count < m_size )
         {
- this->destroy(this->begin() + count, this->end());
+ namespace sv = detail::static_vector;
+ sv::destroy(this->begin() + count, this->end());
         }
         else
         {
@@ -315,7 +320,8 @@
     {
         errh::check_capacity(*this, m_size + 1); // may throw
         
- this->uninitialized_fill(this->end(), value); // may throw
+ namespace sv = detail::static_vector;
+ sv::uninitialized_fill(this->end(), value); // may throw
         ++m_size; // update end
     }
 
@@ -325,32 +331,36 @@
         errh::check_empty(*this);
 
         //--m_size; // update end
- //this->destroy(this->end());
+ //namespace sv = detail::static_vector;
+ //sv::destroy(this->end());
 
         // safer and more intuitive version
- this->destroy(this->end() - 1);
+ namespace sv = detail::static_vector;
+ sv::destroy(this->end() - 1);
         --m_size; // update end
     }
 
     // basic
     iterator insert(iterator position, value_type const& value)
     {
+ namespace sv = detail::static_vector;
+
         errh::check_iterator_end_eq(*this, position);
         errh::check_capacity(*this, m_size + 1); // may throw
 
         if ( position == this->end() )
         {
- this->uninitialized_fill(position, value); // may throw
+ sv::uninitialized_fill(position, value); // may throw
             ++m_size; // update end
         }
         else
         {
             // TODO - should following lines check for exception and revert to the old size?
 
- this->uninitialized_fill(this->end(), *(this->end() - 1)); // may throw
+ sv::uninitialized_fill(this->end(), *(this->end() - 1)); // may throw
             ++m_size; // update end
- this->move_backward(position, this->end() - 2, this->end() - 1); // may throw
- this->fill(position, value); // may throw
+ sv::move_backward(position, this->end() - 2, this->end() - 1); // may throw
+ sv::fill(position, value); // may throw
         }
 
         return position;
@@ -369,22 +379,24 @@
         }
         else
         {
+ namespace sv = detail::static_vector;
+
             difference_type to_move = std::distance(position, this->end());
             
             // TODO - should following lines check for exception and revert to the old size?
 
             if ( count < static_cast<size_type>(to_move) )
             {
- this->uninitialized_copy(this->end() - count, this->end(), this->end()); // may throw
+ sv::uninitialized_copy(this->end() - count, this->end(), this->end()); // may throw
                 m_size += count; // update end
- this->move_backward(position, position + to_move - count, this->end() - count); // may throw
+ sv::move_backward(position, position + to_move - count, this->end() - count); // may throw
                 std::fill_n(position, count, value); // may throw
             }
             else
             {
                 std::uninitialized_fill(this->end(), position + count, value); // may throw
                 m_size += count - to_move; // update end
- this->uninitialized_copy(position, position + to_move, position + count); // may throw
+ sv::uninitialized_copy(position, position + to_move, position + count); // may throw
                 m_size += to_move; // update end
                 std::fill_n(position, to_move, value); // may throw
             }
@@ -408,10 +420,12 @@
     // basic
     iterator erase(iterator position)
     {
+ namespace sv = detail::static_vector;
+
         errh::check_iterator_end_neq(*this, position);
 
- this->move(position + 1, this->end(), position); // may throw
- this->destroy(this->end() - 1);
+ sv::move(position + 1, this->end(), position); // may throw
+ sv::destroy(this->end() - 1);
         --m_size;
 
         return position;
@@ -420,14 +434,16 @@
     // basic
     iterator erase(iterator first, iterator last)
     {
+ namespace sv = detail::static_vector;
+
         errh::check_iterator_end_eq(*this, first);
         errh::check_iterator_end_eq(*this, last);
         
         difference_type n = std::distance(first, last);
         //BOOST_ASSERT_MSG(0 <= n, "invalid range");
 
- this->move(last, this->end(), first); // may throw
- this->destroy(this->end() - n, this->end());
+ sv::move(last, this->end(), first); // may throw
+ sv::destroy(this->end() - n, this->end());
         m_size -= n;
 
         return first;
@@ -448,8 +464,10 @@
     {
         if ( count < m_size )
         {
+ namespace sv = detail::static_vector;
+
             std::fill_n(this->begin(), count, value);
- this->destroy(this->begin() + count, this->end());
+ sv::destroy(this->begin() + count, this->end());
         }
         else
         {
@@ -464,7 +482,8 @@
     // nothrow
     void clear()
     {
- this->destroy(this->begin(), this->end());
+ namespace sv = detail::static_vector;
+ sv::destroy(this->begin(), this->end());
         m_size = 0; // update end
     }
 
@@ -559,13 +578,16 @@
     {
         errh::check_iterator_end_eq(*this, position);
         
- difference_type count = std::distance(first, last);
+ typename boost::iterator_difference<Iterator>::type
+ count = std::distance(first, last);
 
         errh::check_capacity(*this, m_size + count); // may throw
 
         if ( position == this->end() )
         {
- this->uninitialized_copy(first, last, position); // may throw
+ namespace sv = detail::static_vector;
+
+ sv::uninitialized_copy(first, last, position); // may throw
             m_size += count; // update end
         }
         else
@@ -581,16 +603,19 @@
 
         if ( position == this->end() )
         {
- std::pair<bool, size_type> copy_data =
- this->uninitialized_copy_checked(first, last, position, std::distance(position, this->begin() + Capacity)); // may throw
+ namespace sv = detail::static_vector;
+
+ std::ptrdiff_t d = std::distance(position, this->begin() + Capacity);
+ std::size_t count = sv::uninitialized_copy_s(first, last, position, d); // may throw
             
- errh::check_capacity(*this, copy_data.first ? m_size + copy_data.second : Capacity + 1); // may throw
+ errh::check_capacity(*this, count <= static_cast<std::size_t>(d) ? m_size + count : Capacity + 1); // may throw
 
- m_size += copy_data.second;
+ m_size += count;
         }
         else
         {
- difference_type count = std::distance(first, last);
+ typename boost::iterator_difference<Iterator>::type
+ count = std::distance(first, last);
             
             errh::check_capacity(*this, m_size + count); // may throw
 
@@ -601,27 +626,29 @@
     template <typename Iterator>
     void insert_in_the_middle(iterator position, Iterator first, Iterator last, difference_type count)
     {
+ namespace sv = detail::static_vector;
+
         difference_type to_move = std::distance(position, this->end());
 
         // TODO - should following lines check for exception and revert to the old size?
 
         if ( count < to_move )
         {
- this->uninitialized_copy(this->end() - count, this->end(), this->end()); // may throw
+ sv::uninitialized_copy(this->end() - count, this->end(), this->end()); // may throw
             m_size += count; // update end
- this->move_backward(position, position + to_move - count, this->end() - count); // may throw
- this->copy(first, last, position); // may throw
+ sv::move_backward(position, position + to_move - count, this->end() - count); // may throw
+ sv::copy(first, last, position); // may throw
         }
         else
         {
             Iterator middle_iter = first;
             std::advance(middle_iter, to_move);
 
- this->uninitialized_copy(middle_iter, last, this->end()); // may throw
+ sv::uninitialized_copy(middle_iter, last, this->end()); // may throw
             m_size += count - to_move; // update end
- this->uninitialized_copy(position, position + to_move, position + count); // may throw
+ sv::uninitialized_copy(position, position + to_move, position + count); // may throw
             m_size += to_move; // update end
- this->copy(first, middle_iter, position) ; // may throw
+ sv::copy(first, middle_iter, position) ; // may throw
         }
     }
 
@@ -630,20 +657,23 @@
     template <typename Iterator>
     void assign_dispatch(Iterator first, Iterator last, boost::random_access_traversal_tag const& /*not_random_access*/)
     {
- size_type s = std::distance(first, last);
+ namespace sv = detail::static_vector;
+
+ typename boost::iterator_difference<Iterator>::type
+ s = std::distance(first, last);
 
- errh::check_capacity(*this, s); // may throw
+ errh::check_capacity(*this, s); // may throw
 
- if ( m_size <= s )
+ if ( m_size <= static_cast<size_type>(s) )
         {
- this->copy(first, first + m_size, this->begin()); // may throw
+ sv::copy(first, first + m_size, this->begin()); // may throw
             // TODO - perform uninitialized_copy first?
- this->uninitialized_copy(first + m_size, last, this->end()); // may throw
+ sv::uninitialized_copy(first + m_size, last, this->end()); // may throw
         }
         else
         {
- this->copy(first, last, this->begin()); // may throw
- this->destroy(this->begin() + s, this->end());
+ sv::copy(first, last, this->begin()); // may throw
+ sv::destroy(this->begin() + s, this->end());
         }
         m_size = s; // update end
     }
@@ -651,162 +681,25 @@
     template <typename Iterator, typename Traversal>
     void assign_dispatch(Iterator first, Iterator last, Traversal const& /*not_random_access*/)
     {
+ namespace sv = detail::static_vector;
+
         size_type s = 0;
         iterator it = this->begin();
 
         for ( ; it != this->end() && first != last ; ++it, ++first, ++s )
             *it = *first; // may throw
 
- this->destroy(it, this->end());
+ sv::destroy(it, this->end());
 
- std::pair<bool, size_type> copy_data =
- this->uninitialized_copy_checked(first, last, it, std::distance(it, this->begin() + Capacity)); // may throw
- s += copy_data.second;
+ std::ptrdiff_t d = std::distance(it, this->begin() + Capacity);
+ std::size_t count = sv::uninitialized_copy_s(first, last, it, d); // may throw
+ s += count;
 
- errh::check_capacity(*this, copy_data.first ? s : Capacity + 1); // may throw
+ errh::check_capacity(*this, count <= static_cast<std::size_t>(d) ? s : Capacity + 1); // may throw
 
         m_size = s; // update end
     }
 
- // uninitialized_copy_checked
-
- template <typename Iterator>
- std::pair<bool, size_type> uninitialized_copy_checked(Iterator first, Iterator last, iterator dest, size_type max_count)
- {
- size_type count = 0;
- iterator it = dest;
- try
- {
- for ( ; first != last ; ++it, ++first, ++count )
- {
- if ( max_count <= count )
- return std::make_pair(false, count);
-
- this->uninitialized_fill(it, *first); // may throw
- }
- }
- catch(...)
- {
- this->destroy(dest, it);
- throw;
- }
- return std::make_pair(true, count);
- }
-
- // copy
-
- template <typename Iterator>
- inline static void copy(Iterator first, Iterator last, iterator dst)
- {
- namespace sv = detail::static_vector;
- sv::copy(first, last, dst); // may throw
- }
-
- // uninitialized_copy
-
- template <typename Iterator>
- void uninitialized_copy(Iterator first, Iterator last, iterator dst)
- {
- namespace sv = detail::static_vector;
- sv::uninitialized_copy(first, last, dst); // may throw
- }
-
- // uninitialized_fill
-
- template <typename V>
- void uninitialized_fill(iterator dst, V const& v)
- {
- namespace sv = detail::static_vector;
- sv::uninitialized_fill(dst, v); // may throw
- }
-
- // move
-
- void move(iterator first, iterator last, iterator dst)
- {
- namespace sv = detail::static_vector;
- sv::move(first, last, dst); // may throw
- }
-
- // move_backward
-
- void move_backward(iterator first, iterator last, iterator dst)
- {
- namespace sv = detail::static_vector;
- sv::move_backward(first, last, dst); // may throw
- }
-
- // fill
-
- template <typename V>
- void fill(iterator dst, V const& v)
- {
- namespace sv = detail::static_vector;
- sv::fill(dst, v); // may throw
- }
-
- // destroy
-
- void destroy(iterator first, iterator last)
- {
- this->destroy_dispatch(first, last, has_trivial_destructor<value_type>());
- }
-
- void destroy_dispatch(value_type * /*first*/, value_type * /*last*/,
- boost::true_type const& /*has_trivial_destructor*/)
- {}
-
- void destroy_dispatch(value_type * first, value_type * last,
- boost::false_type const& /*has_trivial_destructor*/)
- {
- for ( ; first != last ; ++first )
- first->~value_type();
- }
-
- // destroy
-
- void destroy(iterator it)
- {
- this->destroy_dispatch(it, has_trivial_destructor<value_type>());
- }
-
- void destroy_dispatch(value_type * /*ptr*/,
- boost::true_type const& /*has_trivial_destructor*/)
- {}
-
- void destroy_dispatch(value_type * ptr,
- boost::false_type const& /*has_trivial_destructor*/)
- {
- ptr->~value_type();
- }
-
- // construct
-
- void construct(iterator first, iterator last)
- {
- this->construct_dispatch(first, last, has_trivial_constructor<value_type>()); // may throw
- }
-
- void construct_dispatch(value_type * /*first*/, value_type * /*last*/,
- boost::true_type const& /*has_trivial_constructor*/)
- {}
-
- void construct_dispatch(value_type * first, value_type * last,
- boost::false_type const& /*has_trivial_constructor*/)
- {
- value_type * it = first;
- try
- {
- for ( ; it != last ; ++it )
- new (it) value_type(); // may throw
- }
- catch(...)
- {
- this->destroy(first, it);
- throw;
- }
- }
-
     Value * ptr()
     {
         return (reinterpret_cast<Value*>(m_storage.address()));


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