]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/boost/container/detail/copy_move_algo.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / container / detail / copy_move_algo.hpp
index b71560d9e540eba5edfc4db80826a28a5605324c..8325cc65ed5c28ef22d462eba13c3efbea3f2683 100644 (file)
@@ -26,6 +26,7 @@
 #include <boost/container/detail/mpl.hpp>
 #include <boost/container/detail/type_traits.hpp>
 #include <boost/container/detail/construct_in_place.hpp>
+#include <boost/container/detail/destroyers.hpp>
 
 // move
 #include <boost/move/adl_move_swap.hpp>
@@ -171,7 +172,7 @@ struct disable_if_memtransfer_copy_assignable
 template
    <typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline F memmove(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
+BOOST_CONTAINER_FORCEINLINE F memmove(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    typedef typename boost::container::iterator_traits<I>::value_type value_type;
    value_type *const dest_raw = boost::movelib::iterator_to_raw_pointer(r);
@@ -180,7 +181,7 @@ inline F memmove(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
    if(BOOST_LIKELY(beg_raw != end_raw && dest_raw && beg_raw)){
       const typename boost::container::iterator_traits<I>::difference_type n = end_raw - beg_raw;
       std::memmove(dest_raw, beg_raw, sizeof(value_type)*n);
-      boost::container::iterator_advance(r, n);
+      r += n;
    }
    return r;
 }
@@ -189,12 +190,12 @@ template
    <typename I, // I models InputIterator
     typename U, // U models unsigned integral constant
     typename F> // F models ForwardIterator
-F memmove_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
+BOOST_CONTAINER_FORCEINLINE F memmove_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    typedef typename boost::container::iterator_traits<I>::value_type value_type;
    if(BOOST_LIKELY(n)){
       std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
-      boost::container::iterator_advance(r, n);
+      r += n;
    }
 
    return r;
@@ -204,12 +205,12 @@ template
    <typename I, // I models InputIterator
     typename U, // U models unsigned integral constant
     typename F> // F models ForwardIterator
-I memmove_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
+BOOST_CONTAINER_FORCEINLINE I memmove_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    if(BOOST_LIKELY(n)){
       typedef typename boost::container::iterator_traits<I>::value_type value_type;
       std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
-      boost::container::iterator_advance(f, n);
+      f += n;
    }
    return f;
 }
@@ -218,13 +219,13 @@ template
    <typename I, // I models InputIterator
     typename U, // U models unsigned integral constant
     typename F> // F models ForwardIterator
-I memmove_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
+BOOST_CONTAINER_FORCEINLINE I memmove_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    typedef typename boost::container::iterator_traits<I>::value_type value_type;
    if(BOOST_LIKELY(n)){
       std::memmove(boost::movelib::iterator_to_raw_pointer(r), boost::movelib::iterator_to_raw_pointer(f), sizeof(value_type)*n);
-      boost::container::iterator_advance(f, n);
-      boost::container::iterator_advance(r, n);
+      f += n;
+      r += n;
    }
    return f;
 }
@@ -315,7 +316,7 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_move_alloc(Allocator &, I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove(f, l, r); }
 
@@ -341,7 +342,8 @@ inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
          ++f; ++r;
       }
@@ -360,7 +362,7 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_move_alloc_n(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n(f, n, r); }
 
@@ -386,7 +388,8 @@ inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, I>::type
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), boost::move(*f));
          ++f; ++r;
       }
@@ -405,7 +408,7 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
    uninitialized_move_alloc_n_source(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n_source(f, n, r); }
 
@@ -450,7 +453,7 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_copy_alloc(Allocator &, I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove(f, l, r); }
 
@@ -476,7 +479,8 @@ inline typename dtl::disable_if_memtransfer_copy_constructible<I, F, F>::type
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), *f);
          ++f; ++r;
       }
@@ -495,7 +499,7 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, F>::type
    uninitialized_copy_alloc_n(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n(f, n, r); }
 
@@ -540,7 +544,7 @@ template
    <typename Allocator,
     typename I, // I models InputIterator
     typename F> // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_constructible<I, F, I>::type
    uninitialized_copy_alloc_n_source(Allocator &, I f, typename boost::container::allocator_traits<Allocator>::size_type n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n_source(f, n, r); }
 
@@ -565,7 +569,8 @@ inline typename dtl::disable_if_memzero_initializable<F, F>::type
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r));
          ++r;
       }
@@ -583,12 +588,14 @@ inline typename dtl::disable_if_memzero_initializable<F, F>::type
 template
    <typename Allocator,
     typename F> // F models ForwardIterator
-inline typename dtl::enable_if_memzero_initializable<F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memzero_initializable<F, F>::type
    uninitialized_value_init_alloc_n(Allocator &, typename boost::container::allocator_traits<Allocator>::size_type n, F r)
 {
    typedef typename boost::container::iterator_traits<F>::value_type value_type;
-   std::memset((void*)boost::movelib::iterator_to_raw_pointer(r), 0, sizeof(value_type)*n);
-   boost::container::iterator_advance(r, n);
+   if (BOOST_LIKELY(n)){
+      std::memset((void*)boost::movelib::iterator_to_raw_pointer(r), 0, sizeof(value_type)*n);
+      r += n;
+   }
    return r;
 }
 
@@ -612,7 +619,8 @@ inline F uninitialized_default_init_alloc_n(Allocator &a, typename boost::contai
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), default_init);
          ++r;
       }
@@ -684,7 +692,8 @@ inline F uninitialized_fill_alloc_n(Allocator &a, const T &v, typename boost::co
 {
    F back = r;
    BOOST_TRY{
-      while (n--) {
+      while (n) {
+         --n;
          allocator_traits<Allocator>::construct(a, boost::movelib::iterator_to_raw_pointer(r), v);
          ++r;
       }
@@ -750,7 +759,7 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
    copy_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n(f, n, r); }
 
@@ -767,7 +776,8 @@ typename F>   // F models ForwardIterator
 inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
    copy_n_source(I f, U n, F r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       boost::container::assign_in_place(r, f);
       ++f; ++r;
    }
@@ -778,7 +788,7 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
    copy_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n_source(f, n, r); }
 
@@ -795,7 +805,8 @@ typename F>   // F models ForwardIterator
 inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
    copy_n_source_dest(I f, U n, F &r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = *f;
       ++f; ++r;
    }
@@ -806,7 +817,7 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
    copy_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n_source_dest(f, n, r);  }
 
@@ -849,7 +860,8 @@ typename F>   // F models ForwardIterator
 inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
    move_n(I f, U n, F r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = ::boost::move(*f);
       ++f; ++r;
    }
@@ -860,7 +872,7 @@ template
 <typename I,   // I models InputIterator
 typename U,   // U models unsigned integral constant
 typename F>   // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
    move_n(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n(f, n, r); }
 
@@ -887,13 +899,15 @@ inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, F>::type
 template
 <typename I,   // I models InputIterator
 typename F>   // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, F>::type
    move_backward(I f, I l, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {
    typedef typename boost::container::iterator_traits<I>::value_type value_type;
    const typename boost::container::iterator_traits<I>::difference_type n = boost::container::iterator_distance(f, l);
-   r -= n;
-   std::memmove((boost::movelib::iterator_to_raw_pointer)(r), (boost::movelib::iterator_to_raw_pointer)(f), sizeof(value_type)*n);
+   if (BOOST_LIKELY(n)){
+      r -= n;
+      std::memmove((boost::movelib::iterator_to_raw_pointer)(r), (boost::movelib::iterator_to_raw_pointer)(f), sizeof(value_type)*n);
+   }
    return r;
 }
 
@@ -910,7 +924,8 @@ template
 inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
    move_n_source_dest(I f, U n, F &r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = ::boost::move(*f);
       ++f; ++r;
    }
@@ -921,7 +936,7 @@ template
 <typename I    // I models InputIterator
 ,typename U    // U models unsigned integral constant
 ,typename F>   // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
    move_n_source_dest(I f, U n, F &r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n_source_dest(f, n, r); }
 
@@ -938,7 +953,8 @@ template
 inline typename dtl::disable_if_memtransfer_copy_assignable<I, F, I>::type
    move_n_source(I f, U n, F r)
 {
-   while (n--) {
+   while (n) {
+      --n;
       *r = ::boost::move(*f);
       ++f; ++r;
    }
@@ -949,7 +965,7 @@ template
 <typename I    // I models InputIterator
 ,typename U    // U models unsigned integral constant
 ,typename F>   // F models ForwardIterator
-inline typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_memtransfer_copy_assignable<I, F, I>::type
    move_n_source(I f, U n, F r) BOOST_NOEXCEPT_OR_NOTHROW
 {  return dtl::memmove_n_source(f, n, r); }
 
@@ -977,7 +993,7 @@ template
    <typename Allocator
    ,typename I   // I models InputIterator
    ,typename U>  // U models unsigned integral constant
-inline typename dtl::enable_if_trivially_destructible<I, void>::type
+BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_trivially_destructible<I, void>::type
    destroy_alloc_n(Allocator &, I, U)
 {}
 
@@ -1157,6 +1173,100 @@ void move_assign_range_alloc_n( Allocator &a, I inp_start, typename allocator_tr
    }
 }
 
+template<class Allocator, class Iterator>
+struct array_destructor
+{
+   typedef typename ::boost::container::iterator_traits<Iterator>::value_type value_type;
+   typedef typename dtl::if_c
+      <dtl::is_trivially_destructible<value_type>::value
+      ,dtl::null_scoped_destructor_range<Allocator>
+      ,dtl::scoped_destructor_range<Allocator>
+      >::type type;
+};
+
+template
+   <typename Allocator
+   ,typename F // F models ForwardIterator
+   ,typename O // G models OutputIterator
+   ,typename InsertionProxy
+   >
+void uninitialized_move_and_insert_alloc
+   ( Allocator &a
+   , F first
+   , F pos
+   , F last
+   , O d_first
+   , typename allocator_traits<Allocator>::size_type n
+   , InsertionProxy insert_range_proxy)
+{
+   typedef typename array_destructor<Allocator, F>::type array_destructor_t;
+
+   //Anti-exception rollbacks
+   array_destructor_t new_values_destroyer(d_first, d_first, a);
+
+   //Initialize with [begin(), pos) old buffer
+   //the start of the new buffer
+   O d_last = ::boost::container::uninitialized_move_alloc(a, first, pos, d_first);
+   new_values_destroyer.set_end(d_last);
+   //Initialize new objects, starting from previous point
+   insert_range_proxy.uninitialized_copy_n_and_update(a, d_last, n);
+   d_last += n;
+   new_values_destroyer.set_end(d_last);
+   //Initialize from the rest of the old buffer,
+   //starting from previous point
+   (void) ::boost::container::uninitialized_move_alloc(a, pos, last, d_last);
+   //All construction successful, disable rollbacks
+   new_values_destroyer.release();
+}
+
+template
+   <typename Allocator
+   ,typename F // F models ForwardIterator
+   ,typename InsertionProxy
+   >
+void expand_forward_and_insert_alloc
+   ( Allocator &a
+   , F pos
+   , F last
+   , typename allocator_traits<Allocator>::size_type n
+   , InsertionProxy insert_range_proxy)
+{
+   typedef typename array_destructor<Allocator, F>::type array_destructor_t;
+
+   if (BOOST_UNLIKELY(!n)){
+      return;
+   }
+   else if (last == pos){
+      insert_range_proxy.uninitialized_copy_n_and_update(a, last, n);
+   }
+   else{
+      typedef typename allocator_traits<Allocator>::size_type alloc_size_type;
+      const alloc_size_type elems_after = static_cast<alloc_size_type>(last - pos);
+      if(elems_after >= n){
+         //New elements can be just copied.
+         //Move to uninitialized memory last objects
+         ::boost::container::uninitialized_move_alloc_n(a, last - n, n, last);
+         array_destructor_t on_exception(last, last, a);
+         //Copy previous to last objects to the initialized end
+         boost::container::move_backward(pos, last - n, last);
+         //Insert new objects in the pos
+         insert_range_proxy.copy_n_and_update(a, pos, n);
+         on_exception.release();
+      }
+      else {
+         //The new elements don't fit in the [pos, end()) range.
+         //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
+         F new_last = ::boost::container::uninitialized_move_alloc(a, pos, last, pos + n);
+         array_destructor_t on_exception(pos + n, new_last, a);
+         //Copy first new elements in pos (gap is still there)
+         insert_range_proxy.copy_n_and_update(a, pos, elems_after);
+         //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
+         insert_range_proxy.uninitialized_copy_n_and_update(a, last, n - elems_after);
+         on_exception.release();
+      }
+   }
+}
+
 }  //namespace container {
 }  //namespace boost {