re PR libstdc++/63775 ([C++11] Regex range with leading dash (-) not working)
[gcc.git] / libstdc++-v3 / include / bits / deque.tcc
index d8c2787064727eb44f304990dee845a93bfd02db..9c8dd36f1f5590a3d620777857e2e3c0e54df730 100644 (file)
@@ -1,8 +1,6 @@
 // Deque implementation (out of line) -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
-// 2009, 2010
-// 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
  * 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
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template <typename _Tp, typename _Alloc>
     void
     deque<_Tp, _Alloc>::
@@ -92,9 +92,26 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
     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));
@@ -108,7 +125,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       return *this;
     }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template<typename _Tp, typename _Alloc>
     template<typename... _Args>
       void
@@ -117,8 +134,9 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       {
        if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
          {
-           this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1,
-                                   std::forward<_Args>(__args)...);
+           _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
@@ -134,8 +152,9 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
        if (this->_M_impl._M_finish._M_cur
            != this->_M_impl._M_finish._M_last - 1)
          {
-           this->_M_impl.construct(this->_M_impl._M_finish._M_cur,
-                                   std::forward<_Args>(__args)...);
+           _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
@@ -143,55 +162,60 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       }
 #endif
 
-  template <typename _Tp, typename _Alloc>
-    typename deque<_Tp, _Alloc>::iterator
-    deque<_Tp, _Alloc>::
-    insert(iterator __position, const value_type& __x)
-    {
-      if (__position._M_cur == this->_M_impl._M_start._M_cur)
-       {
-         push_front(__x);
-         return this->_M_impl._M_start;
-       }
-      else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
-       {
-         push_back(__x);
-         iterator __tmp = this->_M_impl._M_finish;
-         --__tmp;
-         return __tmp;
-       }
-      else
-        return _M_insert_aux(__position, __x);
-    }
-
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template<typename _Tp, typename _Alloc>
     template<typename... _Args>
       typename deque<_Tp, _Alloc>::iterator
       deque<_Tp, _Alloc>::
-      emplace(iterator __position, _Args&&... __args)
+      emplace(const_iterator __position, _Args&&... __args)
       {
        if (__position._M_cur == this->_M_impl._M_start._M_cur)
          {
-           push_front(std::forward<_Args>(__args)...);
+           emplace_front(std::forward<_Args>(__args)...);
            return this->_M_impl._M_start;
          }
        else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
          {
-           push_back(std::forward<_Args>(__args)...);
+           emplace_back(std::forward<_Args>(__args)...);
            iterator __tmp = this->_M_impl._M_finish;
            --__tmp;
            return __tmp;
          }
        else
-         return _M_insert_aux(__position, std::forward<_Args>(__args)...);
+         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>::
-    erase(iterator __position)
+#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)
+       {
+         push_front(__x);
+         return this->_M_impl._M_start;
+       }
+      else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
+       {
+         push_back(__x);
+         iterator __tmp = this->_M_impl._M_finish;
+         --__tmp;
+         return __tmp;
+       }
+      else
+       return _M_insert_aux(__position._M_const_cast(), __x);
+   }
+
+  template <typename _Tp, typename _Alloc>
+    typename deque<_Tp, _Alloc>::iterator
+    deque<_Tp, _Alloc>::
+    _M_erase(iterator __position)
     {
       iterator __next = __position;
       ++__next;
@@ -214,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();
@@ -299,7 +325,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
         _M_insert_aux(__pos, __n, __x);
     }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template <typename _Tp, typename _Alloc>
     void
     deque<_Tp, _Alloc>::
@@ -323,6 +349,24 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
            }
        }
     }
+
+  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>
@@ -361,7 +405,11 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
         __try
           {
             for (; __first != __last; ++__first)
+#if __cplusplus >= 201103L
+             emplace_back(*__first);
+#else
               push_back(*__first);
+#endif
           }
         __catch(...)
           {
@@ -408,7 +456,7 @@ _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>
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
     template<typename... _Args>
       void
       deque<_Tp, _Alloc>::
@@ -423,9 +471,10 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
        *(this->_M_impl._M_finish._M_node + 1) = this->_M_allocate_node();
        __try
          {
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
-           this->_M_impl.construct(this->_M_impl._M_finish._M_cur,
-                                   std::forward<_Args>(__args)...);
+#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
@@ -442,7 +491,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
 
   // Called only if _M_impl._M_start._M_cur == _M_impl._M_start._M_first.
   template<typename _Tp, typename _Alloc>
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
     template<typename... _Args>
       void
       deque<_Tp, _Alloc>::
@@ -460,9 +509,10 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
            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;
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
-           this->_M_impl.construct(this->_M_impl._M_start._M_cur,
-                                   std::forward<_Args>(__args)...);
+#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
@@ -483,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.
@@ -495,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;
@@ -557,7 +609,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       }
 
   template<typename _Tp, typename _Alloc>
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
     template<typename... _Args>
       typename deque<_Tp, _Alloc>::iterator
       deque<_Tp, _Alloc>::
@@ -974,7 +1026,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
       return __result;
     }
 
-#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#if __cplusplus >= 201103L
   template<typename _Tp>
     _Deque_iterator<_Tp, _Tp&, _Tp*>
     move(_Deque_iterator<_Tp, const _Tp&, const _Tp*> __first,
@@ -1038,6 +1090,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
     }
 #endif
 
-_GLIBCXX_END_NESTED_NAMESPACE
+_GLIBCXX_END_NAMESPACE_CONTAINER
+} // namespace std
 
 #endif