re PR libstdc++/63775 ([C++11] Regex range with leading dash (-) not working)
[gcc.git] / libstdc++-v3 / include / bits / deque.tcc
index b5cacc1e577627073692f6170215f453b645665f..9c8dd36f1f5590a3d620777857e2e3c0e54df730 100644 (file)
@@ -1,12 +1,11 @@
 // Deque implementation (out of line) -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
-// 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
 // terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 2, or (at your option)
+// Free Software Foundation; either version 3, or (at your option)
 // any later version.
 
 // This library is distributed in the hope that it will be useful,
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 // GNU General Public License for more details.
 
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING.  If not, write to the Free
-// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-// USA.
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
 
-// As a special exception, you may use this file as part of a free software
-// library without restriction.  Specifically, if other files instantiate
-// templates or use macros or inline functions from this file, or you compile
-// this file and link it with other files to produce an executable, this
-// file does not by itself cause the resulting executable to be covered by
-// the GNU General Public License.  This exception does not however
-// invalidate any other reasons why the executable file might be covered by
-// the GNU General Public License.
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
 
 /*
  *
  * purpose.  It is provided "as is" without express or implied warranty.
  */
 
-/** @file deque.tcc
+/** @file bits/deque.tcc
  *  This is an internal header file, included by other library headers.
- *  You should not attempt to use it directly.
+ *  Do not attempt to use it directly. @headername{deque}
  */
 
 #ifndef _DEQUE_TCC
 #define _DEQUE_TCC 1
 
-_GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_CONTAINER
+
+#if __cplusplus >= 201103L
+  template <typename _Tp, typename _Alloc>
+    void
+    deque<_Tp, _Alloc>::
+    _M_default_initialize()
+    {
+      _Map_pointer __cur;
+      __try
+        {
+          for (__cur = this->_M_impl._M_start._M_node;
+              __cur < this->_M_impl._M_finish._M_node;
+              ++__cur)
+            std::__uninitialized_default_a(*__cur, *__cur + _S_buffer_size(),
+                                          _M_get_Tp_allocator());
+          std::__uninitialized_default_a(this->_M_impl._M_finish._M_first,
+                                        this->_M_impl._M_finish._M_cur,
+                                        _M_get_Tp_allocator());
+        }
+      __catch(...)
+        {
+          std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
+                       _M_get_Tp_allocator());
+          __throw_exception_again;
+        }
+    }
+#endif
 
   template <typename _Tp, typename _Alloc>
     deque<_Tp, _Alloc>&
     deque<_Tp, _Alloc>::
     operator=(const deque& __x)
     {
-      const size_type __len = size();
       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,
+                 // so deallocate everything and take copy of __x's data.
+                 _M_replace_map(__x, __x.get_allocator());
+                 std::__alloc_on_copy(_M_get_Tp_allocator(),
+                                      __x._M_get_Tp_allocator());
+                 return *this;
+               }
+             std::__alloc_on_copy(_M_get_Tp_allocator(),
+                                  __x._M_get_Tp_allocator());
+           }
+#endif
+         const size_type __len = size();
          if (__len >= __x.size())
            _M_erase_at_end(std::copy(__x.begin(), __x.end(),
                                      this->_M_impl._M_start));
@@ -85,10 +125,76 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       return *this;
     }
 
+#if __cplusplus >= 201103L
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      void
+      deque<_Tp, _Alloc>::
+      emplace_front(_Args&&... __args)
+      {
+       if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
+         {
+           _Alloc_traits::construct(this->_M_impl,
+                                    this->_M_impl._M_start._M_cur - 1,
+                                    std::forward<_Args>(__args)...);
+           --this->_M_impl._M_start._M_cur;
+         }
+       else
+         _M_push_front_aux(std::forward<_Args>(__args)...);
+      }
+
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      void
+      deque<_Tp, _Alloc>::
+      emplace_back(_Args&&... __args)
+      {
+       if (this->_M_impl._M_finish._M_cur
+           != this->_M_impl._M_finish._M_last - 1)
+         {
+           _Alloc_traits::construct(this->_M_impl,
+                                    this->_M_impl._M_finish._M_cur,
+                                    std::forward<_Args>(__args)...);
+           ++this->_M_impl._M_finish._M_cur;
+         }
+       else
+         _M_push_back_aux(std::forward<_Args>(__args)...);
+      }
+#endif
+
+#if __cplusplus >= 201103L
+  template<typename _Tp, typename _Alloc>
+    template<typename... _Args>
+      typename deque<_Tp, _Alloc>::iterator
+      deque<_Tp, _Alloc>::
+      emplace(const_iterator __position, _Args&&... __args)
+      {
+       if (__position._M_cur == this->_M_impl._M_start._M_cur)
+         {
+           emplace_front(std::forward<_Args>(__args)...);
+           return this->_M_impl._M_start;
+         }
+       else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
+         {
+           emplace_back(std::forward<_Args>(__args)...);
+           iterator __tmp = this->_M_impl._M_finish;
+           --__tmp;
+           return __tmp;
+         }
+       else
+         return _M_insert_aux(__position._M_const_cast(),
+                              std::forward<_Args>(__args)...);
+      }
+#endif
+
   template <typename _Tp, typename _Alloc>
     typename deque<_Tp, _Alloc>::iterator
     deque<_Tp, _Alloc>::
+#if __cplusplus >= 201103L
+    insert(const_iterator __position, const value_type& __x)
+#else
     insert(iterator __position, const value_type& __x)
+#endif
     {
       if (__position._M_cur == this->_M_impl._M_start._M_cur)
        {
@@ -103,13 +209,13 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
          return __tmp;
        }
       else
-        return _M_insert_aux(__position, __x);
-    }
+       return _M_insert_aux(__position._M_const_cast(), __x);
+   }
 
   template <typename _Tp, typename _Alloc>
     typename deque<_Tp, _Alloc>::iterator
     deque<_Tp, _Alloc>::
-    erase(iterator __position)
+    _M_erase(iterator __position)
     {
       iterator __next = __position;
       ++__next;
@@ -117,13 +223,13 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       if (static_cast<size_type>(__index) < (size() >> 1))
        {
          if (__position != begin())
-           std::copy_backward(begin(), __position, __next);
+           _GLIBCXX_MOVE_BACKWARD3(begin(), __position, __next);
          pop_front();
        }
       else
        {
          if (__next != end())
-           std::copy(__next, end(), __position);
+           _GLIBCXX_MOVE3(__next, end(), __position);
          pop_back();
        }
       return begin() + __index;
@@ -132,9 +238,11 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
   template <typename _Tp, typename _Alloc>
     typename deque<_Tp, _Alloc>::iterator
     deque<_Tp, _Alloc>::
-    erase(iterator __first, iterator __last)
+    _M_erase(iterator __first, iterator __last)
     {
-      if (__first == begin() && __last == end())
+      if (__first == __last)
+       return __first;
+      else if (__first == begin() && __last == end())
        {
          clear();
          return end();
@@ -146,13 +254,13 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
          if (static_cast<size_type>(__elems_before) <= (size() - __n) / 2)
            {
              if (__first != begin())
-               std::copy_backward(begin(), __first, __last);
+               _GLIBCXX_MOVE_BACKWARD3(begin(), __first, __last);
              _M_erase_at_begin(begin() + __n);
            }
          else
            {
              if (__last != end())
-               std::copy(__last, end(), __first);
+               _GLIBCXX_MOVE3(__last, end(), __first);
              _M_erase_at_end(end() - __n);
            }
          return begin() + __elems_before;
@@ -183,13 +291,13 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       if (__pos._M_cur == this->_M_impl._M_start._M_cur)
        {
          iterator __new_start = _M_reserve_elements_at_front(__n);
-         try
+         __try
            {
              std::__uninitialized_fill_a(__new_start, this->_M_impl._M_start,
                                          __x, _M_get_Tp_allocator());
              this->_M_impl._M_start = __new_start;
            }
-         catch(...)
+         __catch(...)
            {
              _M_destroy_nodes(__new_start._M_node,
                               this->_M_impl._M_start._M_node);
@@ -199,14 +307,14 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
        {
          iterator __new_finish = _M_reserve_elements_at_back(__n);
-         try
+         __try
            {
              std::__uninitialized_fill_a(this->_M_impl._M_finish,
                                          __new_finish, __x,
                                          _M_get_Tp_allocator());
              this->_M_impl._M_finish = __new_finish;
            }
-         catch(...)
+         __catch(...)
            {
              _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
                               __new_finish._M_node + 1);
@@ -217,13 +325,57 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
         _M_insert_aux(__pos, __n, __x);
     }
 
+#if __cplusplus >= 201103L
+  template <typename _Tp, typename _Alloc>
+    void
+    deque<_Tp, _Alloc>::
+    _M_default_append(size_type __n)
+    {
+      if (__n)
+       {
+         iterator __new_finish = _M_reserve_elements_at_back(__n);
+         __try
+           {
+             std::__uninitialized_default_a(this->_M_impl._M_finish,
+                                            __new_finish,
+                                            _M_get_Tp_allocator());
+             this->_M_impl._M_finish = __new_finish;
+           }
+         __catch(...)
+           {
+             _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
+                              __new_finish._M_node + 1);
+             __throw_exception_again;
+           }
+       }
+    }
+
+  template <typename _Tp, typename _Alloc>
+    bool
+    deque<_Tp, _Alloc>::
+    _M_shrink_to_fit()
+    {
+      const difference_type __front_capacity
+       = (this->_M_impl._M_start._M_cur - this->_M_impl._M_start._M_first);
+      if (__front_capacity == 0)
+       return false;
+
+      const difference_type __back_capacity
+       = (this->_M_impl._M_finish._M_last - this->_M_impl._M_finish._M_cur);
+      if (__front_capacity + __back_capacity < _S_buffer_size())
+       return false;
+
+      return std::__shrink_to_fit_aux<deque>::_S_do_it(*this);
+    }
+#endif
+
   template <typename _Tp, typename _Alloc>
     void
     deque<_Tp, _Alloc>::
     _M_fill_initialize(const value_type& __value)
     {
       _Map_pointer __cur;
-      try
+      __try
         {
           for (__cur = this->_M_impl._M_start._M_node;
               __cur < this->_M_impl._M_finish._M_node;
@@ -234,7 +386,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                                      this->_M_impl._M_finish._M_cur,
                                      __value, _M_get_Tp_allocator());
         }
-      catch(...)
+      __catch(...)
         {
           std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
                        _M_get_Tp_allocator());
@@ -250,12 +402,16 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                           std::input_iterator_tag)
       {
         this->_M_initialize_map(0);
-        try
+        __try
           {
             for (; __first != __last; ++__first)
+#if __cplusplus >= 201103L
+             emplace_back(*__first);
+#else
               push_back(*__first);
+#endif
           }
-        catch(...)
+        __catch(...)
           {
             clear();
             __throw_exception_again;
@@ -273,7 +429,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
         this->_M_initialize_map(__n);
 
         _Map_pointer __cur_node;
-        try
+        __try
           {
             for (__cur_node = this->_M_impl._M_start._M_node;
                  __cur_node < this->_M_impl._M_finish._M_node;
@@ -289,7 +445,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                                        this->_M_impl._M_finish._M_first,
                                        _M_get_Tp_allocator());
           }
-        catch(...)
+        __catch(...)
           {
             std::_Destroy(this->_M_impl._M_start,
                          iterator(*__cur_node, __cur_node),
@@ -299,51 +455,75 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       }
 
   // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_last - 1.
-  template <typename _Tp, typename _Alloc>
-    void
-    deque<_Tp, _Alloc>::
-    _M_push_back_aux(const value_type& __t)
-    {
-      value_type __t_copy = __t;
-      _M_reserve_map_at_back();
-      *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
-      try
-        {
-          this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t_copy);
-          this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
-                                             + 1);
-          this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
-        }
-      catch(...)
-        {
-          _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
-          __throw_exception_again;
-        }
-    }
+  template<typename _Tp, typename _Alloc>
+#if __cplusplus >= 201103L
+    template<typename... _Args>
+      void
+      deque<_Tp, _Alloc>::
+      _M_push_back_aux(_Args&&... __args)
+#else
+      void
+      deque<_Tp, _Alloc>::
+      _M_push_back_aux(const value_type& __t)
+#endif
+      {
+       _M_reserve_map_at_back();
+       *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
+       __try
+         {
+#if __cplusplus >= 201103L
+           _Alloc_traits::construct(this->_M_impl,
+                                    this->_M_impl._M_finish._M_cur,
+                                    std::forward<_Args>(__args)...);
+#else
+           this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __t);
+#endif
+           this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node
+                                               + 1);
+           this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_first;
+         }
+       __catch(...)
+         {
+           _M_deallocate_node(*(this->_M_impl._M_finish._M_node + 1));
+           __throw_exception_again;
+         }
+      }
 
   // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
-  template <typename _Tp, typename _Alloc>
-    void
-    deque<_Tp, _Alloc>::
-    _M_push_front_aux(const value_type& __t)
-    {
-      value_type __t_copy = __t;
-      _M_reserve_map_at_front();
-      *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
-      try
-        {
-          this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
-                                            - 1);
-          this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
-          this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t_copy);
-        }
-      catch(...)
-        {
-          ++this->_M_impl._M_start;
-          _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
-          __throw_exception_again;
-        }
-    }
+  template<typename _Tp, typename _Alloc>
+#if __cplusplus >= 201103L
+    template<typename... _Args>
+      void
+      deque<_Tp, _Alloc>::
+      _M_push_front_aux(_Args&&... __args)
+#else
+      void
+      deque<_Tp, _Alloc>::
+      _M_push_front_aux(const value_type& __t)
+#endif
+      {
+       _M_reserve_map_at_front();
+       *(this->_M_impl._M_start._M_node - 1) = this->_M_allocate_node();
+       __try
+         {
+           this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node
+                                              - 1);
+           this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_last - 1;
+#if __cplusplus >= 201103L
+           _Alloc_traits::construct(this->_M_impl,
+                                    this->_M_impl._M_start._M_cur,
+                                    std::forward<_Args>(__args)...);
+#else
+           this->_M_impl.construct(this->_M_impl._M_start._M_cur, __t);
+#endif
+         }
+       __catch(...)
+         {
+           ++this->_M_impl._M_start;
+           _M_deallocate_node(*(this->_M_impl._M_start._M_node - 1));
+           __throw_exception_again;
+         }
+      }
 
   // Called only if _M_impl._M_finish._M_cur == _M_impl._M_finish._M_first.
   template <typename _Tp, typename _Alloc>
@@ -353,7 +533,8 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       _M_deallocate_node(this->_M_impl._M_finish._M_first);
       this->_M_impl._M_finish._M_set_node(this->_M_impl._M_finish._M_node - 1);
       this->_M_impl._M_finish._M_cur = this->_M_impl._M_finish._M_last - 1;
-      this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
+      _Alloc_traits::destroy(_M_get_Tp_allocator(),
+                            this->_M_impl._M_finish._M_cur);
     }
 
   // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_last - 1.
@@ -365,7 +546,8 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
     void deque<_Tp, _Alloc>::
     _M_pop_front_aux()
     {
-      this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
+      _Alloc_traits::destroy(_M_get_Tp_allocator(),
+                            this->_M_impl._M_start._M_cur);
       _M_deallocate_node(this->_M_impl._M_start._M_first);
       this->_M_impl._M_start._M_set_node(this->_M_impl._M_start._M_node + 1);
       this->_M_impl._M_start._M_cur = this->_M_impl._M_start._M_first;
@@ -392,13 +574,13 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
         if (__pos._M_cur == this->_M_impl._M_start._M_cur)
          {
            iterator __new_start = _M_reserve_elements_at_front(__n);
-           try
+           __try
              {
                std::__uninitialized_copy_a(__first, __last, __new_start,
                                            _M_get_Tp_allocator());
                this->_M_impl._M_start = __new_start;
              }
-           catch(...)
+           __catch(...)
              {
                _M_destroy_nodes(__new_start._M_node,
                                 this->_M_impl._M_start._M_node);
@@ -408,14 +590,14 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
         else if (__pos._M_cur == this->_M_impl._M_finish._M_cur)
          {
            iterator __new_finish = _M_reserve_elements_at_back(__n);
-           try
+           __try
              {
                std::__uninitialized_copy_a(__first, __last,
                                            this->_M_impl._M_finish,
                                            _M_get_Tp_allocator());
                this->_M_impl._M_finish = __new_finish;
              }
-           catch(...)
+           __catch(...)
              {
                _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
                                 __new_finish._M_node + 1);
@@ -426,38 +608,47 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
           _M_insert_aux(__pos, __first, __last, __n);
       }
 
-  template <typename _Tp, typename _Alloc>
+  template<typename _Tp, typename _Alloc>
+#if __cplusplus >= 201103L
+    template<typename... _Args>
+      typename deque<_Tp, _Alloc>::iterator
+      deque<_Tp, _Alloc>::
+      _M_insert_aux(iterator __pos, _Args&&... __args)
+      {
+       value_type __x_copy(std::forward<_Args>(__args)...); // XXX copy
+#else
     typename deque<_Tp, _Alloc>::iterator
-    deque<_Tp, _Alloc>::
-    _M_insert_aux(iterator __pos, const value_type& __x)
-    {
-      difference_type __index = __pos - this->_M_impl._M_start;
-      value_type __x_copy = __x; // XXX copy
-      if (static_cast<size_type>(__index) < size() / 2)
-       {
-         push_front(front());
-         iterator __front1 = this->_M_impl._M_start;
-         ++__front1;
-         iterator __front2 = __front1;
-         ++__front2;
-         __pos = this->_M_impl._M_start + __index;
-         iterator __pos1 = __pos;
-         ++__pos1;
-         std::copy(__front2, __pos1, __front1);
-       }
-      else
-       {
-         push_back(back());
-         iterator __back1 = this->_M_impl._M_finish;
-         --__back1;
-         iterator __back2 = __back1;
-         --__back2;
-         __pos = this->_M_impl._M_start + __index;
-         std::copy_backward(__pos, __back2, __back1);
-       }
-      *__pos = __x_copy;
-      return __pos;
-    }
+      deque<_Tp, _Alloc>::
+      _M_insert_aux(iterator __pos, const value_type& __x)
+      {
+       value_type __x_copy = __x; // XXX copy
+#endif
+       difference_type __index = __pos - this->_M_impl._M_start;
+       if (static_cast<size_type>(__index) < size() / 2)
+         {
+           push_front(_GLIBCXX_MOVE(front()));
+           iterator __front1 = this->_M_impl._M_start;
+           ++__front1;
+           iterator __front2 = __front1;
+           ++__front2;
+           __pos = this->_M_impl._M_start + __index;
+           iterator __pos1 = __pos;
+           ++__pos1;
+           _GLIBCXX_MOVE3(__front2, __pos1, __front1);
+         }
+       else
+         {
+           push_back(_GLIBCXX_MOVE(back()));
+           iterator __back1 = this->_M_impl._M_finish;
+           --__back1;
+           iterator __back2 = __back1;
+           --__back2;
+           __pos = this->_M_impl._M_start + __index;
+           _GLIBCXX_MOVE_BACKWARD3(__pos, __back2, __back1);
+         }
+       *__pos = _GLIBCXX_MOVE(__x_copy);
+       return __pos;
+      }
 
   template <typename _Tp, typename _Alloc>
     void
@@ -472,22 +663,22 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
          iterator __new_start = _M_reserve_elements_at_front(__n);
          iterator __old_start = this->_M_impl._M_start;
          __pos = this->_M_impl._M_start + __elems_before;
-         try
+         __try
            {
              if (__elems_before >= difference_type(__n))
                {
                  iterator __start_n = (this->_M_impl._M_start
                                        + difference_type(__n));
-                 std::__uninitialized_copy_a(this->_M_impl._M_start,
+                 std::__uninitialized_move_a(this->_M_impl._M_start,
                                              __start_n, __new_start,
                                              _M_get_Tp_allocator());
                  this->_M_impl._M_start = __new_start;
-                 std::copy(__start_n, __pos, __old_start);
+                 _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
                  std::fill(__pos - difference_type(__n), __pos, __x_copy);
                }
              else
                {
-                 std::__uninitialized_copy_fill(this->_M_impl._M_start,
+                 std::__uninitialized_move_fill(this->_M_impl._M_start,
                                                 __pos, __new_start,
                                                 this->_M_impl._M_start,
                                                 __x_copy,
@@ -496,7 +687,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                  std::fill(__old_start, __pos, __x_copy);
                }
            }
-         catch(...)
+         __catch(...)
            {
              _M_destroy_nodes(__new_start._M_node,
                               this->_M_impl._M_start._M_node);
@@ -510,23 +701,23 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
          const difference_type __elems_after =
            difference_type(__length) - __elems_before;
          __pos = this->_M_impl._M_finish - __elems_after;
-         try
+         __try
            {
              if (__elems_after > difference_type(__n))
                {
                  iterator __finish_n = (this->_M_impl._M_finish
                                         - difference_type(__n));
-                 std::__uninitialized_copy_a(__finish_n,
+                 std::__uninitialized_move_a(__finish_n,
                                              this->_M_impl._M_finish,
                                              this->_M_impl._M_finish,
                                              _M_get_Tp_allocator());
                  this->_M_impl._M_finish = __new_finish;
-                 std::copy_backward(__pos, __finish_n, __old_finish);
+                 _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
                  std::fill(__pos, __pos + difference_type(__n), __x_copy);
                }
              else
                {
-                 std::__uninitialized_fill_copy(this->_M_impl._M_finish,
+                 std::__uninitialized_fill_move(this->_M_impl._M_finish,
                                                 __pos + difference_type(__n),
                                                 __x_copy, __pos,
                                                 this->_M_impl._M_finish,
@@ -535,7 +726,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                  std::fill(__pos, __old_finish, __x_copy);
                }
            }
-         catch(...)
+         __catch(...)
            {
              _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
                               __new_finish._M_node + 1);
@@ -559,24 +750,24 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
            iterator __new_start = _M_reserve_elements_at_front(__n);
            iterator __old_start = this->_M_impl._M_start;
            __pos = this->_M_impl._M_start + __elemsbefore;
-           try
+           __try
              {
                if (__elemsbefore >= difference_type(__n))
                  {
                    iterator __start_n = (this->_M_impl._M_start
                                          + difference_type(__n));
-                   std::__uninitialized_copy_a(this->_M_impl._M_start,
+                   std::__uninitialized_move_a(this->_M_impl._M_start,
                                                __start_n, __new_start,
                                                _M_get_Tp_allocator());
                    this->_M_impl._M_start = __new_start;
-                   std::copy(__start_n, __pos, __old_start);
+                   _GLIBCXX_MOVE3(__start_n, __pos, __old_start);
                    std::copy(__first, __last, __pos - difference_type(__n));
                  }
                else
                  {
                    _ForwardIterator __mid = __first;
                    std::advance(__mid, difference_type(__n) - __elemsbefore);
-                   std::__uninitialized_copy_copy(this->_M_impl._M_start,
+                   std::__uninitialized_move_copy(this->_M_impl._M_start,
                                                   __pos, __first, __mid,
                                                   __new_start,
                                                   _M_get_Tp_allocator());
@@ -584,7 +775,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                    std::copy(__mid, __last, __old_start);
                  }
              }
-           catch(...)
+           __catch(...)
              {
                _M_destroy_nodes(__new_start._M_node,
                                 this->_M_impl._M_start._M_node);
@@ -598,25 +789,25 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
           const difference_type __elemsafter =
             difference_type(__length) - __elemsbefore;
           __pos = this->_M_impl._M_finish - __elemsafter;
-          try
+          __try
             {
               if (__elemsafter > difference_type(__n))
                {
                  iterator __finish_n = (this->_M_impl._M_finish
                                         - difference_type(__n));
-                 std::__uninitialized_copy_a(__finish_n,
+                 std::__uninitialized_move_a(__finish_n,
                                              this->_M_impl._M_finish,
                                              this->_M_impl._M_finish,
                                              _M_get_Tp_allocator());
                  this->_M_impl._M_finish = __new_finish;
-                 std::copy_backward(__pos, __finish_n, __old_finish);
+                 _GLIBCXX_MOVE_BACKWARD3(__pos, __finish_n, __old_finish);
                  std::copy(__first, __last, __pos);
                }
               else
                {
                  _ForwardIterator __mid = __first;
                  std::advance(__mid, __elemsafter);
-                 std::__uninitialized_copy_copy(__mid, __last, __pos,
+                 std::__uninitialized_copy_move(__mid, __last, __pos,
                                                 this->_M_impl._M_finish,
                                                 this->_M_impl._M_finish,
                                                 _M_get_Tp_allocator());
@@ -624,7 +815,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                  std::copy(__first, __mid, __pos);
                }
             }
-          catch(...)
+          __catch(...)
             {
               _M_destroy_nodes(this->_M_impl._M_finish._M_node + 1,
                               __new_finish._M_node + 1);
@@ -667,12 +858,12 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                                     / _S_buffer_size());
       _M_reserve_map_at_front(__new_nodes);
       size_type __i;
-      try
+      __try
         {
           for (__i = 1; __i <= __new_nodes; ++__i)
             *(this->_M_impl._M_start._M_node - __i) = this->_M_allocate_node();
         }
-      catch(...)
+      __catch(...)
         {
           for (size_type __j = 1; __j < __i; ++__j)
             _M_deallocate_node(*(this->_M_impl._M_start._M_node - __j));
@@ -692,12 +883,12 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
                                     / _S_buffer_size());
       _M_reserve_map_at_back(__new_nodes);
       size_type __i;
-      try
+      __try
         {
           for (__i = 1; __i <= __new_nodes; ++__i)
             *(this->_M_impl._M_finish._M_node + __i) = this->_M_allocate_node();
         }
-      catch(...)
+      __catch(...)
         {
           for (size_type __j = 1; __j < __i; ++__j)
             _M_deallocate_node(*(this->_M_impl._M_finish._M_node + __j));
@@ -752,7 +943,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
     }
 
   // Overload for deque::iterators, exploiting the "segmented-iterator
-  // optimization".  NB: leave const_iterators alone!
+  // optimization".
   template<typename _Tp>
     void
     fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
@@ -773,6 +964,133 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
        std::fill(__first._M_cur, __last._M_cur, __value);
     }
 
-_GLIBCXX_END_NESTED_NAMESPACE
+  template<typename _Tp>
+    _Deque_iterator<_Tp, _Tp&, _Tp*>
+    copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
+        _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
+        _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
+    {
+      typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
+      typedef typename _Self::difference_type difference_type;
+
+      difference_type __len = __last - __first;
+      while (__len > 0)
+       {
+         const difference_type __clen
+           = std::min(__len, std::min(__first._M_last - __first._M_cur,
+                                      __result._M_last - __result._M_cur));
+         std::copy(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
+         __first += __clen;
+         __result += __clen;
+         __len -= __clen;
+       }
+      return __result;
+    }
+
+  template<typename _Tp>
+    _Deque_iterator<_Tp, _Tp&, _Tp*>
+    copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
+                 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
+                 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
+    {
+      typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
+      typedef typename _Self::difference_type difference_type;
+
+      difference_type __len = __last - __first;
+      while (__len > 0)
+       {
+         difference_type __llen = __last._M_cur - __last._M_first;
+         _Tp* __lend = __last._M_cur;
+
+         difference_type __rlen = __result._M_cur - __result._M_first;
+         _Tp* __rend = __result._M_cur;
+
+         if (!__llen)
+           {
+             __llen = _Self::_S_buffer_size();
+             __lend = *(__last._M_node - 1) + __llen;
+           }
+         if (!__rlen)
+           {
+             __rlen = _Self::_S_buffer_size();
+             __rend = *(__result._M_node - 1) + __rlen;
+           }
+
+         const difference_type __clen = std::min(__len,
+                                                 std::min(__llen, __rlen));
+         std::copy_backward(__lend - __clen, __lend, __rend);
+         __last -= __clen;
+         __result -= __clen;
+         __len -= __clen;
+       }
+      return __result;
+    }
+
+#if __cplusplus >= 201103L
+  template<typename _Tp>
+    _Deque_iterator<_Tp, _Tp&, _Tp*>
+    move(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
+        _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
+        _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
+    {
+      typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
+      typedef typename _Self::difference_type difference_type;
+
+      difference_type __len = __last - __first;
+      while (__len > 0)
+       {
+         const difference_type __clen
+           = std::min(__len, std::min(__first._M_last - __first._M_cur,
+                                      __result._M_last - __result._M_cur));
+         std::move(__first._M_cur, __first._M_cur + __clen, __result._M_cur);
+         __first += __clen;
+         __result += __clen;
+         __len -= __clen;
+       }
+      return __result;
+    }
+
+  template<typename _Tp>
+    _Deque_iterator<_Tp, _Tp&, _Tp*>
+    move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
+                 _Deque_iterator<_Tp, const _Tp&, const _Tp*> __last,
+                 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
+    {
+      typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
+      typedef typename _Self::difference_type difference_type;
+
+      difference_type __len = __last - __first;
+      while (__len > 0)
+       {
+         difference_type __llen = __last._M_cur - __last._M_first;
+         _Tp* __lend = __last._M_cur;
+
+         difference_type __rlen = __result._M_cur - __result._M_first;
+         _Tp* __rend = __result._M_cur;
+
+         if (!__llen)
+           {
+             __llen = _Self::_S_buffer_size();
+             __lend = *(__last._M_node - 1) + __llen;
+           }
+         if (!__rlen)
+           {
+             __rlen = _Self::_S_buffer_size();
+             __rend = *(__result._M_node - 1) + __rlen;
+           }
+
+         const difference_type __clen = std::min(__len,
+                                                 std::min(__llen, __rlen));
+         std::move_backward(__lend - __clen, __lend, __rend);
+         __last -= __clen;
+         __result -= __clen;
+         __len -= __clen;
+       }
+      return __result;
+    }
+#endif
+
+_GLIBCXX_END_NAMESPACE_CONTAINER
+} // namespace std
 
 #endif