OSDN Git Service

2006-09-20 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / deque.tcc
index 6b87c3b..4416b71 100644 (file)
@@ -1,6 +1,7 @@
 // Deque implementation (out of line) -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
+// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
+// 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
@@ -15,7 +16,7 @@
 
 // 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 // USA.
 
 // As a special exception, you may use this file as part of a free software
 #ifndef _DEQUE_TCC
 #define _DEQUE_TCC 1
 
-namespace _GLIBCXX_STD
-{
+_GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD)
+
   template <typename _Tp, typename _Alloc>
-    deque<_Tp,_Alloc>&
-    deque<_Tp,_Alloc>::
+    deque<_Tp, _Alloc>&
+    deque<_Tp, _Alloc>::
     operator=(const deque& __x)
     {
       const size_type __len = size();
       if (&__x != this)
        {
          if (__len >= __x.size())
-           erase(std::copy(__x.begin(), __x.end(), this->_M_impl._M_start),
-                 this->_M_impl._M_finish);
+           _M_erase_at_end(std::copy(__x.begin(), __x.end(),
+                                     this->_M_impl._M_start));
          else
            {
              const_iterator __mid = __x.begin() + difference_type(__len);
@@ -85,16 +86,16 @@ namespace _GLIBCXX_STD
     }
 
   template <typename _Tp, typename _Alloc>
-    typename deque<_Tp,_Alloc>::iterator
+    typename deque<_Tp, _Alloc>::iterator
     deque<_Tp, _Alloc>::
-    insert(iterator position, const value_type& __x)
+    insert(iterator __position, const value_type& __x)
     {
-      if (position._M_cur == this->_M_impl._M_start._M_cur)
+      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)
+      else if (__position._M_cur == this->_M_impl._M_finish._M_cur)
        {
          push_back(__x);
          iterator __tmp = this->_M_impl._M_finish;
@@ -102,114 +103,74 @@ namespace _GLIBCXX_STD
          return __tmp;
        }
       else
-        return _M_insert_aux(position, __x);
+        return _M_insert_aux(__position, __x);
     }
 
   template <typename _Tp, typename _Alloc>
-    typename deque<_Tp,_Alloc>::iterator
+    typename deque<_Tp, _Alloc>::iterator
     deque<_Tp, _Alloc>::
     erase(iterator __position)
     {
       iterator __next = __position;
       ++__next;
-      const size_type __index = __position - this->_M_impl._M_start;
-      if (__index < (size() >> 1))
+      const difference_type __index = __position - begin();
+      if (static_cast<size_type>(__index) < (size() >> 1))
        {
-         std::copy_backward(this->_M_impl._M_start, __position, __next);
+         if (__position != begin())
+           std::copy_backward(begin(), __position, __next);
          pop_front();
        }
       else
        {
-         std::copy(__next, this->_M_impl._M_finish, __position);
+         if (__next != end())
+           std::copy(__next, end(), __position);
          pop_back();
        }
-      return this->_M_impl._M_start + __index;
+      return begin() + __index;
     }
 
   template <typename _Tp, typename _Alloc>
-    typename deque<_Tp,_Alloc>::iterator
+    typename deque<_Tp, _Alloc>::iterator
     deque<_Tp, _Alloc>::
     erase(iterator __first, iterator __last)
     {
-      if (__first == this->_M_impl._M_start
-         && __last == this->_M_impl._M_finish)
+      if (__first == begin() && __last == end())
        {
          clear();
-         return this->_M_impl._M_finish;
+         return end();
        }
       else
        {
          const difference_type __n = __last - __first;
-         const difference_type __elems_before = (__first
-                                                 - this->_M_impl._M_start);
+         const difference_type __elems_before = __first - begin();
          if (static_cast<size_type>(__elems_before) < (size() - __n) / 2)
            {
-             std::copy_backward(this->_M_impl._M_start, __first, __last);
-             iterator __new_start = this->_M_impl._M_start + __n;
-             std::_Destroy(this->_M_impl._M_start, __new_start,
-                           this->get_allocator());
-             _M_destroy_nodes(this->_M_impl._M_start._M_node,
-                              __new_start._M_node);
-             this->_M_impl._M_start = __new_start;
+             if (__first != begin())
+               std::copy_backward(begin(), __first, __last);
+             _M_erase_at_begin(begin() + __n);
            }
          else
            {
-             std::copy(__last, this->_M_impl._M_finish, __first);
-             iterator __new_finish = this->_M_impl._M_finish - __n;
-             std::_Destroy(__new_finish, this->_M_impl._M_finish,
-                           this->get_allocator());
-             _M_destroy_nodes(__new_finish._M_node + 1,
-                              this->_M_impl._M_finish._M_node + 1);
-             this->_M_impl._M_finish = __new_finish;
+             if (__last != end())
+               std::copy(__last, end(), __first);
+             _M_erase_at_end(end() - __n);
            }
-         return this->_M_impl._M_start + __elems_before;
+         return begin() + __elems_before;
        }
     }
 
-  template <typename _Tp, typename _Alloc>
-    void
-    deque<_Tp, _Alloc>::
-    clear()
-    {
-      for (_Map_pointer __node = this->_M_impl._M_start._M_node + 1;
-           __node < this->_M_impl._M_finish._M_node;
-           ++__node)
-       {
-         std::_Destroy(*__node, *__node + _S_buffer_size(),
-                       this->get_allocator());
-         _M_deallocate_node(*__node);
-       }
-
-      if (this->_M_impl._M_start._M_node != this->_M_impl._M_finish._M_node)
-       {
-         std::_Destroy(this->_M_impl._M_start._M_cur,
-                       this->_M_impl._M_start._M_last,
-                       this->get_allocator());
-         std::_Destroy(this->_M_impl._M_finish._M_first,
-                       this->_M_impl._M_finish._M_cur,
-                       this->get_allocator());
-         _M_deallocate_node(this->_M_impl._M_finish._M_first);
-       }
-      else
-        std::_Destroy(this->_M_impl._M_start._M_cur,
-                     this->_M_impl._M_finish._M_cur,
-                     this->get_allocator());
-
-      this->_M_impl._M_finish = this->_M_impl._M_start;
-    }
-
   template <typename _Tp, class _Alloc>
     template <typename _InputIterator>
       void
-      deque<_Tp, _Alloc>
-      ::_M_assign_aux(_InputIterator __first, _InputIterator __last,
-                     input_iterator_tag)
+      deque<_Tp, _Alloc>::
+      _M_assign_aux(_InputIterator __first, _InputIterator __last,
+                   std::input_iterator_tag)
       {
         iterator __cur = begin();
         for (; __first != __last && __cur != end(); ++__cur, ++__first)
           *__cur = *__first;
         if (__first == __last)
-          erase(__cur, end());
+          _M_erase_at_end(__cur);
         else
           insert(end(), __first, __last);
       }
@@ -225,8 +186,7 @@ namespace _GLIBCXX_STD
          try
            {
              std::__uninitialized_fill_a(__new_start, this->_M_impl._M_start,
-                                         __x,
-                                         this->get_allocator());
+                                         __x, _M_get_Tp_allocator());
              this->_M_impl._M_start = __new_start;
            }
          catch(...)
@@ -243,7 +203,7 @@ namespace _GLIBCXX_STD
            {
              std::__uninitialized_fill_a(this->_M_impl._M_finish,
                                          __new_finish, __x,
-                                         this->get_allocator());
+                                         _M_get_Tp_allocator());
              this->_M_impl._M_finish = __new_finish;
            }
          catch(...)
@@ -268,17 +228,16 @@ namespace _GLIBCXX_STD
           for (__cur = this->_M_impl._M_start._M_node;
               __cur < this->_M_impl._M_finish._M_node;
               ++__cur)
-            std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(), __value,
-                                       this->get_allocator());
+            std::__uninitialized_fill_a(*__cur, *__cur + _S_buffer_size(),
+                                       __value, _M_get_Tp_allocator());
           std::__uninitialized_fill_a(this->_M_impl._M_finish._M_first,
                                      this->_M_impl._M_finish._M_cur,
-                                     __value,
-                                     this->get_allocator());
+                                     __value, _M_get_Tp_allocator());
         }
       catch(...)
         {
           std::_Destroy(this->_M_impl._M_start, iterator(*__cur, __cur),
-                       this->get_allocator());
+                       _M_get_Tp_allocator());
           __throw_exception_again;
         }
     }
@@ -288,7 +247,7 @@ namespace _GLIBCXX_STD
       void
       deque<_Tp, _Alloc>::
       _M_range_initialize(_InputIterator __first, _InputIterator __last,
-                          input_iterator_tag)
+                          std::input_iterator_tag)
       {
         this->_M_initialize_map(0);
         try
@@ -308,7 +267,7 @@ namespace _GLIBCXX_STD
       void
       deque<_Tp, _Alloc>::
       _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
-                          forward_iterator_tag)
+                          std::forward_iterator_tag)
       {
         const size_type __n = std::distance(__first, __last);
         this->_M_initialize_map(__n);
@@ -319,22 +278,22 @@ namespace _GLIBCXX_STD
             for (__cur_node = this->_M_impl._M_start._M_node;
                  __cur_node < this->_M_impl._M_finish._M_node;
                  ++__cur_node)
-            {
-              _ForwardIterator __mid = __first;
-              std::advance(__mid, _S_buffer_size());
-              std::__uninitialized_copy_a(__first, __mid, *__cur_node,
-                                         this->get_allocator());
-              __first = __mid;
-            }
+             {
+               _ForwardIterator __mid = __first;
+               std::advance(__mid, _S_buffer_size());
+               std::__uninitialized_copy_a(__first, __mid, *__cur_node,
+                                           _M_get_Tp_allocator());
+               __first = __mid;
+             }
             std::__uninitialized_copy_a(__first, __last,
                                        this->_M_impl._M_finish._M_first,
-                                       this->get_allocator());
+                                       _M_get_Tp_allocator());
           }
         catch(...)
           {
             std::_Destroy(this->_M_impl._M_start,
                          iterator(*__cur_node, __cur_node),
-                         this->get_allocator());
+                         _M_get_Tp_allocator());
             __throw_exception_again;
           }
       }
@@ -418,7 +377,7 @@ namespace _GLIBCXX_STD
       deque<_Tp, _Alloc>::
       _M_range_insert_aux(iterator __pos,
                           _InputIterator __first, _InputIterator __last,
-                          input_iterator_tag)
+                          std::input_iterator_tag)
       { std::copy(__first, __last, std::inserter(*this, __pos)); }
 
   template <typename _Tp, typename _Alloc>
@@ -427,7 +386,7 @@ namespace _GLIBCXX_STD
       deque<_Tp, _Alloc>::
       _M_range_insert_aux(iterator __pos,
                           _ForwardIterator __first, _ForwardIterator __last,
-                          forward_iterator_tag)
+                          std::forward_iterator_tag)
       {
         const size_type __n = std::distance(__first, __last);
         if (__pos._M_cur == this->_M_impl._M_start._M_cur)
@@ -436,7 +395,7 @@ namespace _GLIBCXX_STD
            try
              {
                std::__uninitialized_copy_a(__first, __last, __new_start,
-                                           this->get_allocator());
+                                           _M_get_Tp_allocator());
                this->_M_impl._M_start = __new_start;
              }
            catch(...)
@@ -453,7 +412,7 @@ namespace _GLIBCXX_STD
              {
                std::__uninitialized_copy_a(__first, __last,
                                            this->_M_impl._M_finish,
-                                           this->get_allocator());
+                                           _M_get_Tp_allocator());
                this->_M_impl._M_finish = __new_finish;
              }
            catch(...)
@@ -519,12 +478,12 @@ namespace _GLIBCXX_STD
                {
                  iterator __start_n = (this->_M_impl._M_start
                                        + difference_type(__n));
-                 std::__uninitialized_copy_a(this->_M_impl._M_start, __start_n,
-                                             __new_start,
-                                             this->get_allocator());
+                 std::__uninitialized_copy_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);
-                 fill(__pos - difference_type(__n), __pos, __x_copy);
+                 std::fill(__pos - difference_type(__n), __pos, __x_copy);
                }
              else
                {
@@ -532,7 +491,7 @@ namespace _GLIBCXX_STD
                                                 __pos, __new_start,
                                                 this->_M_impl._M_start,
                                                 __x_copy,
-                                                this->get_allocator());
+                                                _M_get_Tp_allocator());
                  this->_M_impl._M_start = __new_start;
                  std::fill(__old_start, __pos, __x_copy);
                }
@@ -557,9 +516,10 @@ namespace _GLIBCXX_STD
                {
                  iterator __finish_n = (this->_M_impl._M_finish
                                         - difference_type(__n));
-                 std::__uninitialized_copy_a(__finish_n, this->_M_impl._M_finish,
+                 std::__uninitialized_copy_a(__finish_n,
+                                             this->_M_impl._M_finish,
                                              this->_M_impl._M_finish,
-                                             this->get_allocator());
+                                             _M_get_Tp_allocator());
                  this->_M_impl._M_finish = __new_finish;
                  std::copy_backward(__pos, __finish_n, __old_finish);
                  std::fill(__pos, __pos + difference_type(__n), __x_copy);
@@ -570,7 +530,7 @@ namespace _GLIBCXX_STD
                                                 __pos + difference_type(__n),
                                                 __x_copy, __pos,
                                                 this->_M_impl._M_finish,
-                                                this->get_allocator());
+                                                _M_get_Tp_allocator());
                  this->_M_impl._M_finish = __new_finish;
                  std::fill(__pos, __old_finish, __x_copy);
                }
@@ -605,9 +565,9 @@ namespace _GLIBCXX_STD
                  {
                    iterator __start_n = (this->_M_impl._M_start
                                          + difference_type(__n));
-                   std::__uninitialized_copy_a(this->_M_impl._M_start, __start_n,
-                                               __new_start,
-                                               this->get_allocator());
+                   std::__uninitialized_copy_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);
                    std::copy(__first, __last, __pos - difference_type(__n));
@@ -619,7 +579,7 @@ namespace _GLIBCXX_STD
                    std::__uninitialized_copy_copy(this->_M_impl._M_start,
                                                   __pos, __first, __mid,
                                                   __new_start,
-                                                  this->get_allocator());
+                                                  _M_get_Tp_allocator());
                    this->_M_impl._M_start = __new_start;
                    std::copy(__mid, __last, __old_start);
                  }
@@ -647,7 +607,7 @@ namespace _GLIBCXX_STD
                  std::__uninitialized_copy_a(__finish_n,
                                              this->_M_impl._M_finish,
                                              this->_M_impl._M_finish,
-                                             this->get_allocator());
+                                             _M_get_Tp_allocator());
                  this->_M_impl._M_finish = __new_finish;
                  std::copy_backward(__pos, __finish_n, __old_finish);
                  std::copy(__first, __last, __pos);
@@ -659,7 +619,7 @@ namespace _GLIBCXX_STD
                  std::__uninitialized_copy_copy(__mid, __last, __pos,
                                                 this->_M_impl._M_finish,
                                                 this->_M_impl._M_finish,
-                                                this->get_allocator());
+                                                _M_get_Tp_allocator());
                  this->_M_impl._M_finish = __new_finish;
                  std::copy(__first, __mid, __pos);
                }
@@ -673,13 +633,38 @@ namespace _GLIBCXX_STD
         }
       }
 
+   template<typename _Tp, typename _Alloc>
+     void
+     deque<_Tp, _Alloc>::
+     _M_destroy_data_aux(iterator __first, iterator __last)
+     {
+       for (_Map_pointer __node = __first._M_node + 1;
+           __node < __last._M_node; ++__node)
+        std::_Destroy(*__node, *__node + _S_buffer_size(),
+                      _M_get_Tp_allocator());
+
+       if (__first._M_node != __last._M_node)
+        {
+          std::_Destroy(__first._M_cur, __first._M_last,
+                        _M_get_Tp_allocator());
+          std::_Destroy(__last._M_first, __last._M_cur,
+                        _M_get_Tp_allocator());
+        }
+       else
+        std::_Destroy(__first._M_cur, __last._M_cur,
+                      _M_get_Tp_allocator());
+     }
+
   template <typename _Tp, typename _Alloc>
     void
     deque<_Tp, _Alloc>::
     _M_new_elements_at_front(size_type __new_elems)
     {
-      const size_type __new_nodes
-       = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
+      if (this->max_size() - this->size() < __new_elems)
+       __throw_length_error(__N("deque::_M_new_elements_at_front"));
+
+      const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
+                                    / _S_buffer_size());
       _M_reserve_map_at_front(__new_nodes);
       size_type __i;
       try
@@ -700,8 +685,11 @@ namespace _GLIBCXX_STD
     deque<_Tp, _Alloc>::
     _M_new_elements_at_back(size_type __new_elems)
     {
-      const size_type __new_nodes
-       = (__new_elems + _S_buffer_size() - 1) / _S_buffer_size();
+      if (this->max_size() - this->size() < __new_elems)
+       __throw_length_error(__N("deque::_M_new_elements_at_back"));
+
+      const size_type __new_nodes = ((__new_elems + _S_buffer_size() - 1)
+                                    / _S_buffer_size());
       _M_reserve_map_at_back(__new_nodes);
       size_type __i;
       try
@@ -734,8 +722,8 @@ namespace _GLIBCXX_STD
                         + (__add_at_front ? __nodes_to_add : 0);
          if (__new_nstart < this->_M_impl._M_start._M_node)
            std::copy(this->_M_impl._M_start._M_node,
-                   this->_M_impl._M_finish._M_node + 1,
-                   __new_nstart);
+                     this->_M_impl._M_finish._M_node + 1,
+                     __new_nstart);
          else
            std::copy_backward(this->_M_impl._M_start._M_node,
                               this->_M_impl._M_finish._M_node + 1,
@@ -762,6 +750,29 @@ namespace _GLIBCXX_STD
       this->_M_impl._M_start._M_set_node(__new_nstart);
       this->_M_impl._M_finish._M_set_node(__new_nstart + __old_num_nodes - 1);
     }
-} // namespace std
+
+  // Overload for deque::iterators, exploiting the "segmented-iterator
+  // optimization".  NB: leave const_iterators alone!
+  template<typename _Tp>
+    void
+    fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>& __first,
+        const _Deque_iterator<_Tp, _Tp&, _Tp*>& __last, const _Tp& __value)
+    {
+      typedef typename _Deque_iterator<_Tp, _Tp&, _Tp*>::_Self _Self;
+
+      for (typename _Self::_Map_pointer __node = __first._M_node + 1;
+           __node < __last._M_node; ++__node)
+       std::fill(*__node, *__node + _Self::_S_buffer_size(), __value);
+
+      if (__first._M_node != __last._M_node)
+       {
+         std::fill(__first._M_cur, __first._M_last, __value);
+         std::fill(__last._M_first, __last._M_cur, __value);
+       }
+      else
+       std::fill(__first._M_cur, __last._M_cur, __value);
+    }
+
+_GLIBCXX_END_NESTED_NAMESPACE
 
 #endif