home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 May / VPR9705A.ISO / VPR_DATA / PROGRAM / CBTRIAL / SETUP / DATA.Z / DEQUE.H < prev    next >
C/C++ Source or Header  |  1997-02-14  |  31KB  |  993 lines

  1. /* $Revision:   8.1  $ */
  2.  
  3. /***************************************************************************
  4.  *
  5.  * deque - Declaration and definition for the Standard Library deque class
  6.  *
  7.  * $Id: deque,v 1.43 1995/09/29 04:16:00 hart Exp $
  8.  *
  9.  ***************************************************************************
  10.  *
  11.  * Copyright (c) 1994
  12.  * Hewlett-Packard Company
  13.  *
  14.  * Permission to use, copy, modify, distribute and sell this software
  15.  * and its documentation for any purpose is hereby granted without fee,
  16.  * provided that the above copyright notice appear in all copies and
  17.  * that both that copyright notice and this permission notice appear
  18.  * in supporting documentation.  Hewlett-Packard Company makes no
  19.  * representations about the suitability of this software for any
  20.  * purpose.  It is provided "as is" without express or implied warranty.
  21.  *
  22.  *
  23.  ***************************************************************************
  24.  *
  25.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  26.  * ALL RIGHTS RESERVED
  27.  *
  28.  * The software and information contained herein are proprietary to, and
  29.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  30.  * intends to preserve as trade secrets such software and information.
  31.  * This software is furnished pursuant to a written license agreement and
  32.  * may be used, copied, transmitted, and stored only in accordance with
  33.  * the terms of such license and with the inclusion of the above copyright
  34.  * notice.  This software and information or any other copies thereof may
  35.  * not be provided or otherwise made available to any other person.
  36.  *
  37.  * Notwithstanding any other lease or license that may pertain to, or
  38.  * accompany the delivery of, this computer software and information, the
  39.  * rights of the Government regarding its use, reproduction and disclosure
  40.  * are as set forth in Section 52.227-19 of the FARS Computer
  41.  * Software-Restricted Rights clause.
  42.  *
  43.  * Use, duplication, or disclosure by the Government is subject to
  44.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  45.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  46.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  47.  * P.O. Box 2328, Corvallis, Oregon 97339.
  48.  *
  49.  * This computer software and information is distributed with "restricted
  50.  * rights."  Use, duplication or disclosure is subject to restrictions as
  51.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  52.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  53.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  54.  * then the "Alternate III" clause applies.
  55.  *
  56.  **************************************************************************/
  57.  
  58. #ifndef __STD_DEQUE__
  59. #define __STD_DEQUE__
  60.  
  61. #include <stddefs.h>
  62.  
  63. #include <stdcomp.h>
  64. #include <function>
  65. #include <algorith>
  66. #include <iterator>
  67.  
  68. #ifndef Allocator
  69. #define Allocator allocator
  70. #include <memory>
  71. #endif
  72.  
  73. #ifndef deque
  74. #define deque deque
  75. #endif
  76.  
  77. #ifndef RWSTD_NO_NAMESPACE
  78. namespace std {
  79. #endif
  80.  
  81. template <class T>
  82. class  deque
  83. {
  84.   public:
  85.  
  86.     class  iterator;
  87.     class  const_iterator;
  88.     friend class iterator;
  89.     friend class const_iterator;
  90.  
  91.   public:
  92.  
  93.     typedef T                             value_type;
  94.     typedef Allocator<T>                  data_allocator_type;
  95.     typedef Allocator<T>::pointer         pointer;
  96.     typedef Allocator<T>::reference       reference;
  97.     typedef Allocator<T>::const_reference const_reference;
  98.     typedef Allocator<T>::size_type       size_type;
  99.     typedef Allocator<T>::difference_type difference_type;
  100.     typedef Allocator<pointer>            map_allocator_type;
  101.  
  102.   protected:
  103.  
  104.     data_allocator_type data_allocator;
  105.     map_allocator_type  map_allocator;
  106.  
  107.     typedef Allocator<pointer>::pointer map_pointer;
  108.  
  109.     static size_type buffer_size ()
  110.     {
  111.         //
  112.         // Each time we allocate memory we reserve space for
  113.         // a chunk of objects of type T.  This is currently tuned to
  114.         // allocate memory in 1K chunks, except for large objects.
  115.         //
  116.         return sizeof(T) >= 1024 ? 1 : 1024 / sizeof(T);
  117.     }
  118.  
  119.   public:
  120.     //
  121.     // Definition of our iterator.
  122.     //
  123.     class iterator : public random_access_iterator<T, difference_type>
  124.     {
  125.         friend class deque<T>;
  126.         friend class const_iterator;
  127.  
  128.       protected:
  129.  
  130.         pointer     current;
  131.         pointer     first;
  132.         pointer     last;
  133.         map_pointer node;
  134.  
  135.         iterator (pointer x, map_pointer y)
  136.             : current(x), first(*y), last(*y + buffer_size()), node(y) {}
  137.  
  138.       public:
  139.  
  140.         iterator () : current(0), first(0), last(0), node(0) {}
  141.         reference operator* () const { return *current; }
  142.         difference_type operator- (const iterator& x) const
  143.         {
  144.             return node == x.node
  145.                 ? current - x.current
  146.                 : difference_type(buffer_size() * (node - x.node - 1) +
  147.                                   (current - first) + (x.last - x.current));
  148.         }
  149.         iterator& operator++ ()
  150.         {
  151.             if (++current == last)
  152.             {
  153.                 first = *(++node);
  154.                 current = first;
  155.                 last = first + buffer_size();
  156.             }
  157.             return *this;
  158.         }
  159.         iterator operator++ (int)
  160.         {
  161.             iterator tmp = *this; ++*this; return tmp;
  162.         }
  163.         iterator& operator-- ()
  164.         {
  165.             if (current == first)
  166.             {
  167.                 first = *(--node);
  168.                 last = first + buffer_size();
  169.                 current = last;
  170.             }
  171.             --current;
  172.             return *this;
  173.         }
  174.         iterator operator-- (int)
  175.         {
  176.             iterator tmp = *this; --*this; return tmp;
  177.         }
  178.         iterator& operator+= (difference_type n)
  179.         {
  180.             difference_type offset = n + (current - first);
  181.             difference_type num_node_to_jump = offset >= 0
  182.                 ? offset / buffer_size()
  183.                 : -((-offset + buffer_size() - 1) / buffer_size());
  184.             if (num_node_to_jump == 0)
  185.                 current += n;
  186.             else
  187.             {
  188.                 node = node + num_node_to_jump;
  189.                 first = *node;
  190.                 last = first + buffer_size();
  191.                 current = first + (offset - num_node_to_jump * buffer_size());
  192.             }
  193.             return *this;
  194.         }
  195.         iterator& operator-= (difference_type n) { return *this += -n; }
  196.         iterator operator+ (difference_type n) const
  197.         {
  198.             iterator tmp = *this; return tmp += n;
  199.         }
  200.         iterator operator- (difference_type n) const
  201.         {
  202.             iterator tmp = *this; return tmp -= n;
  203.         }
  204.         reference operator[] (difference_type n) { return *(*this + n); }
  205.         bool operator== (const iterator& x) const
  206.         {
  207.             return current == x.current ||
  208.                 ((current == first || x.current == x.first) &&
  209.                  *this - x == 0);
  210.         }
  211.         bool operator< (const iterator& x) const
  212.         {
  213.             return (node == x.node) ? (current < x.current) : (node < x.node);
  214.         }
  215.     };  // End of nested definiton of iterator.
  216.  
  217.     //
  218.     // Definition of our constant iterator.
  219.     //
  220.     class const_iterator : public random_access_iterator<T, difference_type>
  221.     {
  222.         friend class deque<T>;
  223.  
  224.       protected:
  225.  
  226.         pointer     current;
  227.         pointer     first;
  228.         pointer     last;
  229.         map_pointer node;
  230.  
  231.         const_iterator (pointer x, map_pointer y)
  232.             : current(x), first(*y), last(*y + buffer_size()), node(y) {}
  233.  
  234.       public:
  235.  
  236.         const_iterator () : current(0), first(0), last(0), node(0) {}
  237.         const_iterator (const iterator& x)
  238.             : current(x.current), first(x.first), last(x.last), node(x.node) {}
  239.         const_reference operator* () const { return *current; }
  240.         difference_type operator- (const const_iterator& x) const
  241.         {
  242.             return node == x.node
  243.             ? current - x.current
  244.             : difference_type(buffer_size() * (node - x.node - 1) +
  245.                   (current - first) + (x.last - x.current));
  246.         }
  247.         const_iterator& operator++ ()
  248.         {
  249.             if (++current == last)
  250.             {
  251.                 first = *(++node);
  252.                 current = first;
  253.                 last = first + buffer_size();
  254.             }
  255.         return *this;
  256.         }
  257.         const_iterator operator++ (int)
  258.         {
  259.             const_iterator tmp = *this; ++*this; return tmp;
  260.         }
  261.         const_iterator& operator-- ()
  262.         {
  263.             if (current == first)
  264.             {
  265.                 first = *(--node);
  266.                 last = first + buffer_size();
  267.                 current = last;
  268.             }
  269.             --current;
  270.             return *this;
  271.         }
  272.         const_iterator operator-- (int)
  273.         {
  274.             const_iterator tmp = *this; --*this; return tmp;
  275.         }
  276.         const_iterator& operator+= (difference_type n)
  277.         {
  278.             difference_type offset = n + (current - first);
  279.             difference_type num_node_to_jump = offset >= 0
  280.                 ? offset / buffer_size()
  281.                 : -((-offset + buffer_size() - 1) / buffer_size());
  282.             if (num_node_to_jump == 0)
  283.                 current += n;
  284.             else
  285.             {
  286.                 node = node + num_node_to_jump;
  287.                 first = *node;
  288.                 last = first + buffer_size();
  289.                 current = first + (offset - num_node_to_jump * buffer_size());
  290.             }
  291.             return *this;
  292.         }
  293.         const_iterator& operator-= (difference_type n) { return *this += -n; }
  294.         const_iterator operator+ (difference_type n) const
  295.         {
  296.             const_iterator tmp = *this; return tmp += n;
  297.         }
  298.         const_iterator operator- (difference_type n) const
  299.         {
  300.             const_iterator tmp = *this; return tmp -= n;
  301.         }
  302.         const_reference operator[] (difference_type n)
  303.         {
  304.             return *(*this + n);
  305.         }
  306.         bool operator== (const const_iterator& x) const
  307.         {
  308.             return current == x.current ||
  309.                 ((current == first || x.current == x.first) &&
  310.              *this - x == 0);
  311.         }
  312.         bool operator< (const const_iterator& x) const
  313.         {
  314.             return (node == x.node) ? (current < x.current) : (node < x.node);
  315.         }
  316.     };  // End of nested definiton of const_iterator.
  317.  
  318.     typedef reverse_iterator<const_iterator, value_type, const_reference,
  319.                              difference_type>
  320.             const_reverse_iterator;
  321.     typedef reverse_iterator<iterator, value_type, reference, difference_type>
  322.             reverse_iterator;
  323.  
  324.   protected:
  325.  
  326.     iterator    start;
  327.     iterator    finish;
  328.     size_type   length;
  329.     map_pointer map;
  330.     size_type   map_size;
  331.  
  332.     void allocate_at_begin   ();
  333.     void allocate_at_end     ();
  334.     void deallocate_at_begin ();
  335.     void deallocate_at_end   ();
  336.  
  337.   public:
  338.     //
  339.     // construct/copy/destroy
  340.     //
  341.     deque () : start(), finish(), length(0), map(0), map_size(0) { }
  342.     //
  343.     // Build a deque of size n with each element set to default for type T.
  344.     // Requires that T have a default constructor.
  345.     //
  346.     explicit deque (size_type n)
  347.         : start(), finish(), length(0), map(0), map_size(0)
  348.     {
  349.         insert(begin(), n, T());
  350.     }
  351.     //
  352.     // Build a deque of size n with each element set to copy of value.
  353.     //
  354.     explicit deque (size_type n, const T& value)
  355.         : start(), finish(), length(0), map(0), map_size(0)
  356.     {
  357.         insert(begin(), n, value);
  358.     }
  359.     deque (const deque<T>& x)
  360.         : start(), finish(), length(0), map(0), map_size(0)
  361.     {
  362.         copy(x.begin(), x.end(), back_inserter(*this));
  363.     }
  364.  
  365. #ifndef RWSTD_NO_MEMBER_TEMPLATES
  366.     template<class InputIterator>
  367.     deque (InputIterator first, InputIterator last)
  368.         : start(), finish(), length(0), map(0), map_size(0)
  369.     {
  370.         copy(first, last, back_inserter(*this));
  371.     }
  372. #else
  373.     deque (const_iterator first, const_iterator last)
  374.         : start(), finish(), length(0), map(0), map_size(0)
  375.     {
  376.         copy(first, last, back_inserter(*this));
  377.     }
  378.     deque (const T* first, const T* last)
  379.         : start(), finish(), length(0), map(0), map_size(0)
  380.     {
  381.         copy(first, last, back_inserter(*this));
  382.     }
  383. #endif
  384.  
  385.     ~deque ();
  386.     deque<T>& operator= (const deque<T>& x)
  387.     {
  388.         if (!(this == &x))
  389.         {
  390.             if (size() >= x.size())
  391.                 erase(copy(x.begin(), x.end(), begin()), end());
  392.             else
  393.                 copy(x.begin() + size(), x.end(),
  394.                      inserter(*this,copy(x.begin(),x.begin()+size(),begin())));
  395.         }
  396.         return *this;
  397.     }
  398. #ifndef RWSTD_NO_MEMBER_TEMPLATES
  399.     template<class InputIterator>
  400.     void assign (InputIterator first, InputIterator last)
  401.     {
  402.         erase(begin(), end()); insert(begin(), first, last);
  403.     }
  404.     //
  405.     // Assign n copies of default value of type T to vector.
  406.     // This requires that T have a default constructor.
  407.     //
  408.     template<class Size, class T>
  409.     void assign (Size n)
  410.     {
  411.         erase(begin(), end()); insert(begin(), n, T());
  412.     }
  413.     //
  414.     // Assign n copies of t to this vector.
  415.     //
  416.     template<class Size, class T>
  417.     void assign (Size n, const T& t)
  418.     {
  419.         erase(begin(), end()); insert(begin(), n, t);
  420.     }
  421. #else
  422.     void assign (const_iterator first, const_iterator last)
  423.     {
  424.         erase(begin(), end()); insert(begin(), first, last);
  425.     }
  426.     void assign (const T* first, const T* last)
  427.     {
  428.         erase(begin(), end()); insert(begin(), first, last);
  429.     }
  430.     //
  431.     // Assign n copies of default value of type T to vector.
  432.     // This requires that T have a default constructor.
  433.     //
  434.     void assign (size_type n)
  435.     {
  436.         erase(begin(), end()); insert(begin(), n, T());
  437.     }
  438.     //
  439.     // Assign n copies of t to this vector.
  440.     //
  441.     void assign (size_type n, const T& t)
  442.     {
  443.         erase(begin(), end()); insert(begin(), n, t);
  444.     }
  445. #endif
  446.     //
  447.     // Iterators.
  448.     //
  449.     iterator         begin  ()       { return start;  }
  450.     const_iterator   begin  () const { return start;  }
  451.     iterator         end    ()       { return finish; }
  452.     const_iterator   end    () const { return finish; }
  453.     reverse_iterator rbegin ()
  454.     {
  455.       reverse_iterator tmp(end()); return tmp;
  456.     }
  457.     const_reverse_iterator rbegin () const
  458.     {
  459.         const_reverse_iterator tmp(end()); return tmp;
  460.     }
  461.     reverse_iterator rend ()
  462.     {
  463.         reverse_iterator tmp(begin()); return tmp;
  464.     }
  465.     const_reverse_iterator rend () const
  466.     {
  467.         const_reverse_iterator tmp(begin()); return tmp;
  468.     }
  469.     //
  470.     // Capacity.
  471.     //
  472.     size_type size     () const { return length;                    }
  473.     size_type max_size () const { return data_allocator.max_size(); }
  474.     bool      empty    () const { return length == 0;               }
  475.     void      resize (size_type new_size);
  476.     void      resize (size_type new_size, T value);
  477.     //
  478.     // Element access.
  479.     //
  480.     reference       operator[] (size_type n)       { return *(begin() + n); }
  481.     const_reference operator[] (size_type n) const { return *(begin() + n); }
  482.     const_reference at (size_type n)         const { return *(begin() + n); }
  483.     reference       at (size_type n)               { return *(begin() + n); }
  484.     reference       front ()                       { return *begin();       }
  485.     const_reference front ()                 const { return *begin();       }
  486.     reference       back ()                        { return *(end() - 1);   }
  487.     const_reference back ()                  const { return *(end() - 1);   }
  488.     //
  489.     // Modifiers.
  490.     //
  491.     void push_front (const T& x)
  492.     {
  493.         if (empty() || begin().current == begin().first) allocate_at_begin();
  494.         --start.current;
  495.         construct(start.current, x);
  496.         ++length;
  497.     }
  498.     void push_back (const T& x)
  499.     {
  500.         if (empty() || end().current == end().last) allocate_at_end();
  501.         construct(finish.current, x);
  502.         ++finish.current;
  503.         ++length;
  504.     }
  505.     //
  506.     // Insert default value of type T at position.
  507.     // Requires that T have a default constructor.
  508.     //
  509.     iterator insert (iterator position);
  510.     //
  511.     // Insert x at position.
  512.     //
  513.     iterator insert (iterator position, const T& x);
  514.     void insert (iterator position, size_type n, const T& x);
  515.  
  516. #ifndef RWSTD_NO_MEMBER_TEMPLATES
  517.     template <class Iterator>
  518.     void insert(iterator position, Iterator first, Iterator last);
  519. #else
  520.     void insert (iterator position, const T* first, const T* last);
  521.     void insert (iterator position, const_iterator first, const_iterator last);
  522. #endif
  523.  
  524.     void pop_front ()
  525.     {
  526.         destroy(start.current);
  527.         ++start.current;
  528.         --length;
  529.         if (empty() || begin().current == begin().last) deallocate_at_begin();
  530.     }
  531.     void pop_back ()
  532.     {
  533.         --finish.current;
  534.         destroy(finish.current);
  535.         --length;
  536.         if (empty() || end().current == end().first) deallocate_at_end();
  537.     }
  538.     void erase (iterator position);
  539.     void erase (iterator first, iterator last);
  540.     void swap (deque<T>& x)
  541.     {
  542. #ifndef RWSTD_NO_NAMESPACE
  543.         std::swap(start,          x.start);
  544.         std::swap(finish,         x.finish);
  545.         std::swap(length,         x.length);
  546.         std::swap(map,            x.map);
  547.         std::swap(map_size,       x.map_size);
  548.         std::swap(data_allocator, x.data_allocator);
  549.         std::swap(map_allocator,  x.map_allocator);
  550. #else
  551.         ::swap(start,             x.start);
  552.         ::swap(finish,            x.finish);
  553.         ::swap(length,            x.length);
  554.         ::swap(map,               x.map);
  555.         ::swap(map_size,          x.map_size);
  556.         ::swap(data_allocator,    x.data_allocator);
  557.         ::swap(map_allocator,     x.map_allocator);
  558. #endif
  559.     }
  560. };
  561.  
  562. template <class T>
  563. deque<T>::~deque()
  564. {
  565.     while (!empty()) pop_front();
  566. }
  567.  
  568. template <class T>
  569. void deque<T>::allocate_at_begin ()
  570. {
  571.     pointer p = data_allocator.allocate(buffer_size());
  572.     if (!empty())
  573.     {
  574.         if (start.node == map)
  575.         {
  576.             difference_type i = finish.node - start.node;
  577.             map_size = (i + 1) * 2;
  578.             map_pointer tmp = map_allocator.allocate(map_size);
  579.             copy(start.node, finish.node + 1, tmp + map_size / 4 + 1);
  580.             map_allocator.deallocate(map);
  581.             map = tmp;
  582.             map[map_size / 4] = p;
  583.             start = iterator(p + buffer_size(), map + map_size / 4);
  584.             finish = iterator(finish.current, map + map_size / 4 + i + 1);
  585.         }
  586.         else
  587.         {
  588.             *--start.node = p;
  589.             start = iterator(p + buffer_size(), start.node);
  590.         }
  591.     }
  592.     else
  593.     {
  594.         map_size = buffer_size();
  595.         map = map_allocator.allocate(map_size);
  596.         map[map_size / 2] = p;
  597.         start = iterator(p + buffer_size() / 2 + 1, map + map_size / 2);
  598.         finish = start;
  599.     }
  600. }
  601.  
  602. template <class T>
  603. void deque<T>::allocate_at_end ()
  604. {
  605.     pointer p = data_allocator.allocate(buffer_size());
  606.  
  607.     if (!empty())
  608.     {
  609.         if (finish.node == map + map_size - 1)
  610.         {
  611.             difference_type i = finish.node - start.node;
  612.             map_size = (i + 1) * 2;
  613.             map_pointer tmp = map_allocator.allocate(map_size);
  614.             copy(start.node, finish.node + 1, tmp + map_size / 4);
  615.             map_allocator.deallocate(map);
  616.             map = tmp;
  617.             map[map_size / 4 + i + 1] = p;
  618.             start = iterator(start.current, map + map_size / 4);
  619.             finish = iterator(p, map + map_size / 4 + i + 1);
  620.         }
  621.         else
  622.         {
  623.             *++finish.node = p;
  624.             finish = iterator(p, finish.node);
  625.         }
  626.     }
  627.     else
  628.     {
  629.         map_size = buffer_size();
  630.         map = map_allocator.allocate(map_size);
  631.         map[map_size / 2] = p;
  632.         start = iterator(p + buffer_size() / 2, map + map_size / 2);
  633.         finish = start;
  634.     }
  635. }
  636.  
  637. template <class T>
  638. void deque<T>::deallocate_at_begin ()
  639. {
  640.     data_allocator.deallocate(*start.node++);
  641.     if (empty())
  642.     {
  643.         start = iterator();
  644.         finish = start;
  645.         map_allocator.deallocate(map);
  646.     }
  647.     else
  648.         start = iterator(*start.node, start.node);
  649. }
  650.  
  651. template <class T>
  652. void deque<T>::deallocate_at_end ()
  653. {
  654.     data_allocator.deallocate(*finish.node--);
  655.     if (empty())
  656.     {
  657.         start = iterator();
  658.         finish = start;
  659.         map_allocator.deallocate(map);
  660.     }
  661.     else
  662.         finish = iterator(*finish.node + buffer_size(), finish.node);
  663. }
  664.  
  665. template <class T>
  666. void deque<T>::resize (size_type new_size)
  667. {
  668.     T value;
  669.     if (new_size > size())
  670.         insert(end(), new_size - size(), value);
  671.     else if (new_size < size())
  672.         erase(begin() + new_size,end());
  673. }
  674.  
  675. template <class T>
  676. void deque<T>::resize (size_type new_size, T value)
  677. {
  678.     if (new_size > size())
  679.         insert(end(), new_size - size(), value);
  680.     else if (new_size < size())
  681.         erase(begin() + new_size,end());
  682. }
  683.  
  684. template <class T>
  685. deque<T>::iterator deque<T>::insert (iterator position)
  686. {
  687.     T x;
  688.  
  689.     if (position == begin())
  690.     {
  691.         push_front(x); return begin();
  692.     }
  693.     else if (position == end())
  694.     {
  695.         push_back(x); return end() - 1;
  696.     }
  697.     else
  698.     {
  699.         difference_type index = position - begin();
  700.         if (index < length)
  701.         {
  702.             push_front(*begin());
  703.             copy(begin() + 2, begin() + index + 1, begin() + 1);
  704.         }
  705.         else
  706.         {
  707.             push_back(*(end() - 1));
  708.             copy_backward(begin() + index, end() - 2, end() - 1);
  709.         }
  710.         *(begin() + index) = x;
  711.         return begin() + index;
  712.     }
  713. }
  714.  
  715. template <class T>
  716. deque<T>::iterator deque<T>::insert (iterator position, const T& x)
  717. {
  718.     if (position == begin())
  719.     {
  720.         push_front(x); return begin();
  721.     }
  722.     else if (position == end())
  723.     {
  724.         push_back(x); return end() - 1;
  725.     }
  726.     else
  727.     {
  728.         difference_type index = position - begin();
  729.         if (index < length)
  730.         {
  731.             push_front(*begin());
  732.             copy(begin() + 2, begin() + index + 1, begin() + 1);
  733.         }
  734.         else
  735.         {
  736.             push_back(*(end() - 1));
  737.             copy_backward(begin() + index, end() - 2, end() - 1);
  738.         }
  739.         *(begin() + index) = x;
  740.         return begin() + index;
  741.     }
  742. }
  743.  
  744. template <class T>
  745. void deque<T>::insert (iterator position, size_type n, const T& x)
  746. {
  747.     difference_type index     = position - begin();
  748.     difference_type remainder = length   - index;
  749.  
  750.     if (remainder > index)
  751.     {
  752.         if (n > index)
  753.         {
  754.             difference_type m = n - index;
  755.             while (m-- > 0) push_front(x);
  756.             difference_type i = index;
  757.             while (i--) push_front(*(begin() + n - 1));
  758.             fill(begin() + n, begin() + n + index, x);
  759.         }
  760.         else
  761.         {
  762.             difference_type i = n;
  763.             while (i--) push_front(*(begin() + n - 1));
  764.             copy(begin() + n + n, begin() + n + index, begin() + n);
  765.             fill(begin() + index, begin() + n + index, x);
  766.         }
  767.     }
  768.     else
  769.     {
  770.         difference_type orig_len = index + remainder;
  771.         if (n > remainder)
  772.         {
  773.             difference_type m = n - remainder;
  774.             while (m-- > 0) push_back(x);
  775.             difference_type i = 0;
  776.             while (i < remainder) push_back(*(begin() + index + i++));
  777.             fill(begin() + index, begin() + orig_len, x);
  778.         }
  779.         else
  780.         {
  781.             difference_type i = 0;
  782.             while (i < n) push_back(*(begin() + orig_len - n + i++));
  783.             copy_backward(begin() + index, begin() + orig_len - n,
  784.                           begin() + orig_len);
  785.             fill(begin() + index, begin() + index + n, x);
  786.         }
  787.     }
  788. }
  789.  
  790.  
  791. #ifndef RWSTD_NO_MEMBER_TEMPLATES
  792. template <class T>
  793. template <class Iterator>
  794. void deque<T>::insert (iterator position, Iterator first, Iterator last)
  795. {
  796.     difference_type index     = position - begin();
  797.     difference_type remainder = length   - index;
  798.     size_type n;
  799.     __initialize(n, size_type(0));
  800.     distance(first, last, n);
  801.  
  802.     if (remainder > index)
  803.     {
  804.         if (n > index)
  805.         {
  806.             Iterator m = last - index;
  807.             while (m != first) push_front(*--m);
  808.             difference_type i = index;
  809.             while (i--) push_front(*(begin() + n - 1));
  810.             copy(last - index, last, begin() + n);
  811.         }
  812.         else
  813.         {
  814.             difference_type i = n;
  815.             while (i--) push_front(*(begin() + n - 1));
  816.             copy(begin() + n + n, begin() + n + index, begin() + n);
  817.             copy(first, last, begin() + index);
  818.         }
  819.     }
  820.     else
  821.     {
  822.         difference_type orig_len = index + remainder;
  823.         if (n > remainder)
  824.         {
  825.             Iterator m = first + remainder;
  826.             while (m != last) push_back(*m++);
  827.             difference_type i = 0;
  828.             while (i < remainder) push_back(*(begin() + index + i++));
  829.             copy(first, first + remainder, begin() + index);
  830.         }
  831.         else
  832.         {
  833.             difference_type i = 0;
  834.             while (i < n) push_back(*(begin() + orig_len - n + i++));
  835.             copy_backward(begin() + index, begin() + orig_len - n,
  836.                           begin() + orig_len);
  837.             copy(first, last, begin() + index);
  838.         }
  839.     }
  840. }
  841.  
  842. #else
  843.  
  844. template<class T>
  845. void deque<T>::insert (iterator position, const_iterator first, const_iterator last)
  846. {
  847.     difference_type index     = position - begin();
  848.     difference_type remainder = length   - index;
  849.     size_type n;
  850.     __initialize(n, size_type(0));
  851.     distance(first, last, n);
  852.  
  853.     if (remainder > index)
  854.     {
  855.         if (n > index)
  856.         {
  857.             const_iterator m = last - index;
  858.             while (m != first) push_front(*--m);
  859.             difference_type i = index;
  860.             while (i--) push_front(*(begin() + n - 1));
  861.             copy(last - index, last, begin() + n);
  862.         }
  863.         else
  864.         {
  865.             difference_type i = n;
  866.             while (i--) push_front(*(begin() + n - 1));
  867.             copy(begin() + n + n, begin() + n + index, begin() + n);
  868.             copy(first, last, begin() + index);
  869.         }
  870.     }
  871.     else
  872.     {
  873.         difference_type orig_len = index + remainder;
  874.         if (n > remainder)
  875.         {
  876.             const_iterator m = first + remainder;
  877.             while (m != last) push_back(*m++);
  878.             difference_type i = 0;
  879.             while (i < remainder) push_back(*(begin() + index + i++));
  880.             copy(first, first + remainder, begin() + index);
  881.         }
  882.         else
  883.         {
  884.             difference_type i = 0;
  885.             while (i < n) push_back(*(begin() + orig_len - n + i++));
  886.             copy_backward(begin() + index, begin() + orig_len - n,
  887.                           begin() + orig_len);
  888.             copy(first, last, begin() + index);
  889.         }
  890.     }
  891. }
  892.  
  893. template<class T>
  894. void deque<T>::insert (iterator position, const T* first, const T* last)
  895. {
  896.     difference_type index     = position - begin();
  897.     difference_type remainder = length   - index;
  898.     size_type n;
  899.     __initialize(n, size_type(0));
  900.     distance(first, last, n);
  901.  
  902.     if (remainder > index)
  903.     {
  904.         if (n > index)
  905.         {
  906.             const T* m = last - index;
  907.             while (m != first) push_front(*--m);
  908.             difference_type i = index;
  909.             while (i--) push_front(*(begin() + n - 1));
  910.             copy(last - index, last, begin() + n);
  911.         }
  912.         else
  913.         {
  914.             difference_type i = n;
  915.             while (i--) push_front(*(begin() + n - 1));
  916.             copy(begin() + n + n, begin() + n + index, begin() + n);
  917.             copy(first, last, begin() + index);
  918.         }
  919.     }
  920.     else
  921.     {
  922.         difference_type orig_len = index + remainder;
  923.         if (n > remainder)
  924.         {
  925.             const T* m = first + remainder;
  926.             while (m != last) push_back(*m++);
  927.             difference_type i = 0;
  928.             while (i < remainder) push_back(*(begin() + index + i++));
  929.             copy(first, first + remainder, begin() + index);
  930.         }
  931.         else
  932.         {
  933.             difference_type i = 0;
  934.             while (i < n) push_back(*(begin() + orig_len - n + i++));
  935.             copy_backward(begin() + index, begin() + orig_len - n,
  936.                           begin() + orig_len);
  937.             copy(first, last, begin() + index);
  938.         }
  939.     }
  940. }
  941.  
  942. #endif /*RWSTD_NO_MEMBER_TEMPLATES*/
  943.  
  944. template <class T>
  945. void deque<T>::erase (iterator position)
  946. {
  947.     if (end() - position > position - begin())
  948.     {
  949.         copy_backward(begin(), position, position + 1); pop_front();
  950.     }
  951.     else
  952.     {
  953.         copy(position + 1, end(), position); pop_back();
  954.     }
  955. }
  956.  
  957. template <class T>
  958. void deque<T>::erase (iterator first, iterator last)
  959. {
  960.     difference_type n = last - first;
  961.     if (end() - last > first - begin())
  962.     {
  963.         copy_backward(begin(), first, last);
  964.         while(n-- > 0) pop_front();
  965.     }
  966.     else
  967.     {
  968.         copy(last, end(), first);
  969.         while(n-- > 0) pop_back();
  970.     }
  971. }
  972.  
  973. template <class T>
  974. inline bool operator== (const deque<T>& x, const deque<T>& y)
  975. {
  976.     return x.size() == y.size() && equal(x.begin(), x.end(), y.begin());
  977. }
  978.  
  979. template <class T>
  980. inline bool operator< (const deque<T>& x, const deque<T>& y)
  981. {
  982.     return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  983. }
  984.  
  985. #undef Allocator
  986. #undef deque
  987.  
  988. #ifndef RWSTD_NO_NAMESPACE
  989. }
  990. #endif
  991.  
  992. #endif /*__STD_DEQUE__*/
  993.