atomic_base.h: Avoid including <stdbool.h>.
[gcc.git] / libstdc++-v3 / include / bits / vector.tcc
index 3aaee392e55fca50d459c0a8da32e53cad03bcf1..19784c0ac2ecaeeabbe3cf6146114da036ebac6c 100644 (file)
@@ -1,7 +1,6 @@
 // Vector implementation (out of line) -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
-// 2011 Free Software Foundation, Inc.
+// Copyright (C) 2001-2014 Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
@@ -72,8 +71,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        {
          const size_type __old_size = size();
          pointer __tmp = _M_allocate_and_copy(__n,
-                _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_start),
-                _GLIBCXX_MAKE_MOVE_ITERATOR(this->_M_impl._M_finish));
+           _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_start),
+           _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(this->_M_impl._M_finish));
          std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
                        _M_get_Tp_allocator());
          _M_deallocate(this->_M_impl._M_start,
@@ -85,7 +84,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        }
     }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template<typename _Tp, typename _Alloc>
     template<typename... _Args>
       void
@@ -94,38 +93,45 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       {
        if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
          {
-           this->_M_impl.construct(this->_M_impl._M_finish,
-                                   std::forward<_Args>(__args)...);
+           _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                                    std::forward<_Args>(__args)...);
            ++this->_M_impl._M_finish;
          }
        else
-         _M_insert_aux(end(), std::forward<_Args>(__args)...);
+         _M_emplace_back_aux(std::forward<_Args>(__args)...);
       }
 #endif
 
   template<typename _Tp, typename _Alloc>
     typename vector<_Tp, _Alloc>::iterator
     vector<_Tp, _Alloc>::
+#if __cplusplus >= 201103L
+    insert(const_iterator __position, const value_type& __x)
+#else
     insert(iterator __position, const value_type& __x)
+#endif
     {
       const size_type __n = __position - begin();
       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
          && __position == end())
        {
-         this->_M_impl.construct(this->_M_impl._M_finish, __x);
+         _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, __x);
          ++this->_M_impl._M_finish;
        }
       else
        {
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
+         const auto __pos = begin() + (__position - cbegin());
          if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
            {
              _Tp __x_copy = __x;
-             _M_insert_aux(__position, std::move(__x_copy));
+             _M_insert_aux(__pos, std::move(__x_copy));
            }
          else
-#endif
+           _M_insert_aux(__pos, __x);
+#else
            _M_insert_aux(__position, __x);
+#endif
        }
       return iterator(this->_M_impl._M_start + __n);
     }
@@ -133,23 +139,26 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
   template<typename _Tp, typename _Alloc>
     typename vector<_Tp, _Alloc>::iterator
     vector<_Tp, _Alloc>::
-    erase(iterator __position)
+    _M_erase(iterator __position)
     {
       if (__position + 1 != end())
        _GLIBCXX_MOVE3(__position + 1, end(), __position);
       --this->_M_impl._M_finish;
-      this->_M_impl.destroy(this->_M_impl._M_finish);
+      _Alloc_traits::destroy(this->_M_impl, this->_M_impl._M_finish);
       return __position;
     }
 
   template<typename _Tp, typename _Alloc>
     typename vector<_Tp, _Alloc>::iterator
     vector<_Tp, _Alloc>::
-    erase(iterator __first, iterator __last)
+    _M_erase(iterator __first, iterator __last)
     {
-      if (__last != end())
-       _GLIBCXX_MOVE3(__last, end(), __first);
-      _M_erase_at_end(__first.base() + (end() - __last));
+      if (__first != __last)
+       {
+         if (__last != end())
+           _GLIBCXX_MOVE3(__last, end(), __first);
+         _M_erase_at_end(__first.base() + (end() - __last));
+       }
       return __first;
     }
 
@@ -160,6 +169,25 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     {
       if (&__x != this)
        {
+#if __cplusplus >= 201103L
+         if (_Alloc_traits::_S_propagate_on_copy_assign())
+           {
+             if (!_Alloc_traits::_S_always_equal()
+                 && _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
+               {
+                 // replacement allocator cannot free existing storage
+                 this->clear();
+                 _M_deallocate(this->_M_impl._M_start,
+                               this->_M_impl._M_end_of_storage
+                               - this->_M_impl._M_start);
+                 this->_M_impl._M_start = nullptr;
+                 this->_M_impl._M_finish = nullptr;
+                 this->_M_impl._M_end_of_storage = nullptr;
+               }
+             std::__alloc_on_copy(_M_get_Tp_allocator(),
+                                  __x._M_get_Tp_allocator());
+           }
+#endif
          const size_type __xlen = __x.size();
          if (__xlen > capacity())
            {
@@ -200,15 +228,15 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       if (__n > capacity())
        {
          vector __tmp(__n, __val, _M_get_Tp_allocator());
-         __tmp.swap(*this);
+         __tmp._M_impl._M_swap_data(this->_M_impl);
        }
       else if (__n > size())
        {
          std::fill(begin(), end(), __val);
-         std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
-                                       __n - size(), __val,
-                                       _M_get_Tp_allocator());
-         this->_M_impl._M_finish += __n - size();
+         this->_M_impl._M_finish =
+           std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
+                                         __n - size(), __val,
+                                         _M_get_Tp_allocator());
        }
       else
         _M_erase_at_end(std::fill_n(this->_M_impl._M_start, __n, __val));
@@ -266,23 +294,24 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
          }
       }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template<typename _Tp, typename _Alloc>
     template<typename... _Args>
       typename vector<_Tp, _Alloc>::iterator
       vector<_Tp, _Alloc>::
-      emplace(iterator __position, _Args&&... __args)
+      emplace(const_iterator __position, _Args&&... __args)
       {
        const size_type __n = __position - begin();
        if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage
            && __position == end())
          {
-           this->_M_impl.construct(this->_M_impl._M_finish,
-                                   std::forward<_Args>(__args)...);
+           _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                                    std::forward<_Args>(__args)...);
            ++this->_M_impl._M_finish;
          }
        else
-         _M_insert_aux(__position, std::forward<_Args>(__args)...);
+         _M_insert_aux(begin() + (__position - cbegin()),
+                       std::forward<_Args>(__args)...);
        return iterator(this->_M_impl._M_start + __n);
       }
 
@@ -300,17 +329,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
     {
       if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage)
        {
-         this->_M_impl.construct(this->_M_impl._M_finish,
-                                 _GLIBCXX_MOVE(*(this->_M_impl._M_finish
-                                                 - 1)));
+         _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
+                                  _GLIBCXX_MOVE(*(this->_M_impl._M_finish
+                                                  - 1)));
          ++this->_M_impl._M_finish;
-#ifndef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus < 201103L
          _Tp __x_copy = __x;
 #endif
          _GLIBCXX_MOVE_BACKWARD3(__position.base(),
                                  this->_M_impl._M_finish - 2,
                                  this->_M_impl._M_finish - 1);
-#ifndef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus < 201103L
          *__position = __x_copy;
 #else
          *__position = _Tp(std::forward<_Args>(__args)...);
@@ -329,30 +358,32 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
              // case, where the moves could alter a new element belonging
              // to the existing vector.  This is an issue only for callers
              // taking the element by const lvalue ref (see 23.1/13).
-             this->_M_impl.construct(__new_start + __elems_before,
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
-                                     std::forward<_Args>(__args)...);
+             _Alloc_traits::construct(this->_M_impl,
+                                      __new_start + __elems_before,
+#if __cplusplus >= 201103L
+                                      std::forward<_Args>(__args)...);
 #else
-                                     __x);
+                                      __x);
 #endif
-             __new_finish = 0;
+             __new_finish = pointer();
+
+             __new_finish
+               = std::__uninitialized_move_if_noexcept_a
+               (this->_M_impl._M_start, __position.base(),
+                __new_start, _M_get_Tp_allocator());
 
-             __new_finish =
-               std::__uninitialized_move_a(this->_M_impl._M_start,
-                                           __position.base(), __new_start,
-                                           _M_get_Tp_allocator());
              ++__new_finish;
 
-             __new_finish =
-               std::__uninitialized_move_a(__position.base(),
-                                           this->_M_impl._M_finish,
-                                           __new_finish,
-                                           _M_get_Tp_allocator());
+             __new_finish
+               = std::__uninitialized_move_if_noexcept_a
+               (__position.base(), this->_M_impl._M_finish,
+                __new_finish, _M_get_Tp_allocator());
            }
           __catch(...)
            {
              if (!__new_finish)
-               this->_M_impl.destroy(__new_start + __elems_before);
+               _Alloc_traits::destroy(this->_M_impl,
+                                      __new_start + __elems_before);
              else
                std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
              _M_deallocate(__new_start, __len);
@@ -369,6 +400,50 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        }
     }
 
+#if __cplusplus >= 201103L
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      void
+      vector<_Tp, _Alloc>::
+      _M_emplace_back_aux(_Args&&... __args)
+      {
+       const size_type __len =
+         _M_check_len(size_type(1), "vector::_M_emplace_back_aux");
+       pointer __new_start(this->_M_allocate(__len));
+       pointer __new_finish(__new_start);
+       __try
+         {
+           _Alloc_traits::construct(this->_M_impl, __new_start + size(),
+                                    std::forward<_Args>(__args)...);
+           __new_finish = pointer();
+
+           __new_finish
+             = std::__uninitialized_move_if_noexcept_a
+             (this->_M_impl._M_start, this->_M_impl._M_finish,
+              __new_start, _M_get_Tp_allocator());
+
+           ++__new_finish;
+         }
+       __catch(...)
+         {
+           if (!__new_finish)
+             _Alloc_traits::destroy(this->_M_impl, __new_start + size());
+           else
+             std::_Destroy(__new_start, __new_finish, _M_get_Tp_allocator());
+           _M_deallocate(__new_start, __len);
+           __throw_exception_again;
+         }
+       std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
+                     _M_get_Tp_allocator());
+       _M_deallocate(this->_M_impl._M_start,
+                     this->_M_impl._M_end_of_storage
+                     - this->_M_impl._M_start);
+       this->_M_impl._M_start = __new_start;
+       this->_M_impl._M_finish = __new_finish;
+       this->_M_impl._M_end_of_storage = __new_start + __len;
+      }
+#endif
+
   template<typename _Tp, typename _Alloc>
     void
     vector<_Tp, _Alloc>::
@@ -396,11 +471,11 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                }
              else
                {
-                 std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
-                                               __n - __elems_after,
-                                               __x_copy,
-                                               _M_get_Tp_allocator());
-                 this->_M_impl._M_finish += __n - __elems_after;
+                 this->_M_impl._M_finish =
+                   std::__uninitialized_fill_n_a(this->_M_impl._M_finish,
+                                                 __n - __elems_after,
+                                                 __x_copy,
+                                                 _M_get_Tp_allocator());
                  std::__uninitialized_move_a(__position.base(), __old_finish,
                                              this->_M_impl._M_finish,
                                              _M_get_Tp_allocator());
@@ -421,20 +496,19 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                  std::__uninitialized_fill_n_a(__new_start + __elems_before,
                                                __n, __x,
                                                _M_get_Tp_allocator());
-                 __new_finish = 0;
+                 __new_finish = pointer();
+
+                 __new_finish
+                   = std::__uninitialized_move_if_noexcept_a
+                   (this->_M_impl._M_start, __position.base(),
+                    __new_start, _M_get_Tp_allocator());
 
-                 __new_finish =
-                   std::__uninitialized_move_a(this->_M_impl._M_start,
-                                               __position.base(),
-                                               __new_start,
-                                               _M_get_Tp_allocator());
                  __new_finish += __n;
 
-                 __new_finish =
-                   std::__uninitialized_move_a(__position.base(),
-                                               this->_M_impl._M_finish,
-                                               __new_finish,
-                                               _M_get_Tp_allocator());
+                 __new_finish
+                   = std::__uninitialized_move_if_noexcept_a
+                   (__position.base(), this->_M_impl._M_finish,
+                    __new_finish, _M_get_Tp_allocator());
                }
              __catch(...)
                {
@@ -460,7 +534,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        }
     }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template<typename _Tp, typename _Alloc>
     void
     vector<_Tp, _Alloc>::
@@ -471,9 +545,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
          if (size_type(this->_M_impl._M_end_of_storage
                        - this->_M_impl._M_finish) >= __n)
            {
-             std::__uninitialized_default_n_a(this->_M_impl._M_finish,
-                                              __n, _M_get_Tp_allocator());
-             this->_M_impl._M_finish += __n;
+             this->_M_impl._M_finish =
+               std::__uninitialized_default_n_a(this->_M_impl._M_finish,
+                                                __n, _M_get_Tp_allocator());
            }
          else
            {
@@ -484,14 +558,13 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
              pointer __new_finish(__new_start);
              __try
                {
+                 __new_finish
+                   = std::__uninitialized_move_if_noexcept_a
+                   (this->_M_impl._M_start, this->_M_impl._M_finish,
+                    __new_start, _M_get_Tp_allocator());
                  __new_finish =
-                   std::__uninitialized_move_a(this->_M_impl._M_start,
-                                               this->_M_impl._M_finish,
-                                               __new_start,
-                                               _M_get_Tp_allocator());
-                 std::__uninitialized_default_n_a(__new_finish, __n,
-                                                  _M_get_Tp_allocator());
-                 __new_finish += __n;
+                   std::__uninitialized_default_n_a(__new_finish, __n,
+                                                    _M_get_Tp_allocator());
                }
              __catch(...)
                {
@@ -511,6 +584,16 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
            }
        }
     }
+
+  template<typename _Tp, typename _Alloc>
+    bool
+    vector<_Tp, _Alloc>::
+    _M_shrink_to_fit()
+    {
+      if (capacity() == size())
+       return false;
+      return std::__shrink_to_fit_aux<vector>::_S_do_it(*this);
+    }
 #endif
 
   template<typename _Tp, typename _Alloc>
@@ -577,20 +660,18 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                pointer __new_finish(__new_start);
                __try
                  {
-                   __new_finish =
-                     std::__uninitialized_move_a(this->_M_impl._M_start,
-                                                 __position.base(),
-                                                 __new_start,
-                                                 _M_get_Tp_allocator());
-                   __new_finish =
-                     std::__uninitialized_copy_a(__first, __last,
-                                                 __new_finish,
-                                                 _M_get_Tp_allocator());
-                   __new_finish =
-                     std::__uninitialized_move_a(__position.base(),
-                                                 this->_M_impl._M_finish,
-                                                 __new_finish,
-                                                 _M_get_Tp_allocator());
+                   __new_finish
+                     = std::__uninitialized_move_if_noexcept_a
+                     (this->_M_impl._M_start, __position.base(),
+                      __new_start, _M_get_Tp_allocator());
+                   __new_finish
+                     = std::__uninitialized_copy_a(__first, __last,
+                                                   __new_finish,
+                                                   _M_get_Tp_allocator());
+                   __new_finish
+                     = std::__uninitialized_move_if_noexcept_a
+                     (__position.base(), this->_M_impl._M_finish,
+                      __new_finish, _M_get_Tp_allocator());
                  }
                __catch(...)
                  {
@@ -613,24 +694,17 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 
 
   // vector<bool>
-
   template<typename _Alloc>
     void
     vector<bool, _Alloc>::
-    reserve(size_type __n)
+    _M_reallocate(size_type __n)
     {
-      if (__n > this->max_size())
-       __throw_length_error(__N("vector::reserve"));
-      if (this->capacity() < __n)
-       {
-         _Bit_type* __q = this->_M_allocate(__n);
-         this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
-                                                   iterator(__q, 0));
-         this->_M_deallocate();
-         this->_M_impl._M_start = iterator(__q, 0);
-         this->_M_impl._M_end_of_storage = (__q + (__n + int(_S_word_bit) - 1)
-                                            / int(_S_word_bit));
-       }
+      _Bit_type* __q = this->_M_allocate(__n);
+      this->_M_impl._M_finish = _M_copy_aligned(begin(), end(),
+                                               iterator(__q, 0));
+      this->_M_deallocate();
+      this->_M_impl._M_start = iterator(__q, 0);
+      this->_M_impl._M_end_of_storage = __q + _S_nword(__n);
     }
 
   template<typename _Alloc>
@@ -658,9 +732,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
          this->_M_impl._M_finish = std::copy(__position, end(),
                                              __i + difference_type(__n));
          this->_M_deallocate();
-         this->_M_impl._M_end_of_storage = (__q + ((__len
-                                                    + int(_S_word_bit) - 1)
-                                                   / int(_S_word_bit)));
+         this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
          this->_M_impl._M_start = iterator(__q, 0);
        }
     }
@@ -693,10 +765,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
                __i = std::copy(__first, __last, __i);
                this->_M_impl._M_finish = std::copy(__position, end(), __i);
                this->_M_deallocate();
-               this->_M_impl._M_end_of_storage = (__q
-                                                  + ((__len
-                                                      + int(_S_word_bit) - 1)
-                                                     / int(_S_word_bit)));
+               this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
                this->_M_impl._M_start = iterator(__q, 0);
              }
          }
@@ -724,17 +793,54 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
          *__i++ = __x;
          this->_M_impl._M_finish = std::copy(__position, end(), __i);
          this->_M_deallocate();
-         this->_M_impl._M_end_of_storage = (__q + ((__len
-                                                    + int(_S_word_bit) - 1)
-                                                   / int(_S_word_bit)));
+         this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
          this->_M_impl._M_start = iterator(__q, 0);
        }
     }
 
+  template<typename _Alloc>
+    typename vector<bool, _Alloc>::iterator
+    vector<bool, _Alloc>::
+    _M_erase(iterator __position)
+    {
+      if (__position + 1 != end())
+        std::copy(__position + 1, end(), __position);
+      --this->_M_impl._M_finish;
+      return __position;
+    }
+
+  template<typename _Alloc>
+    typename vector<bool, _Alloc>::iterator
+    vector<bool, _Alloc>::
+    _M_erase(iterator __first, iterator __last)
+    {
+      if (__first != __last)
+       _M_erase_at_end(std::copy(__last, end(), __first));
+      return __first;
+    }
+
+#if __cplusplus >= 201103L
+  template<typename _Alloc>
+    bool
+    vector<bool, _Alloc>::
+    _M_shrink_to_fit()
+    {
+      if (capacity() - size() < int(_S_word_bit))
+       return false;
+      __try
+       {
+         _M_reallocate(size());
+         return true;
+       }
+      __catch(...)
+       { return false; }
+    }
+#endif
+
 _GLIBCXX_END_NAMESPACE_CONTAINER
 } // namespace std
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -743,7 +849,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Alloc>
     size_t
     hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>::
-    operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const
+    operator()(const _GLIBCXX_STD_C::vector<bool, _Alloc>& __b) const noexcept
     {
       size_t __hash = 0;
       using _GLIBCXX_STD_C::_S_word_bit;
@@ -776,6 +882,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 _GLIBCXX_END_NAMESPACE_VERSION
 } // namespace std
 
-#endif // __GXX_EXPERIMENTAL_CXX0X__
+#endif // C++11
 
 #endif /* _VECTOR_TCC */