home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / devtools / files / borland_ccompiler55.exe / Include / vector.cc < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-27  |  14.4 KB  |  430 lines

  1. #ifndef __VECTOR_CC
  2. #define __VECTOR_CC
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. /***************************************************************************
  5.  *
  6.  * vector.cc - Non-inline definitions for the Standard Library vector class
  7.  *
  8.  ***************************************************************************
  9.  *
  10.  * Copyright (c) 1994
  11.  * Hewlett-Packard Company
  12.  *
  13.  * Permission to use, copy, modify, distribute and sell this software
  14.  * and its documentation for any purpose is hereby granted without fee,
  15.  * provided that the above copyright notice appear in all copies and
  16.  * that both that copyright notice and this permission notice appear
  17.  * in supporting documentation.  Hewlett-Packard Company makes no
  18.  * representations about the suitability of this software for any
  19.  * purpose.  It is provided "as is" without express or implied warranty.
  20.  *
  21.  *
  22.  ***************************************************************************
  23.  *
  24.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  25.  *
  26.  * This computer software is owned by Rogue Wave Software, Inc. and is
  27.  * protected by U.S. copyright laws and other laws and by international
  28.  * treaties.  This computer software is furnished by Rogue Wave Software,
  29.  * Inc. pursuant to a written license agreement and may be used, copied,
  30.  * transmitted, and stored only in accordance with the terms of such
  31.  * license and with the inclusion of the above copyright notice.  This
  32.  * computer software or any other copies thereof may not be provided or
  33.  * otherwise made available to any other person.
  34.  *
  35.  * U.S. Government Restricted Rights.  This computer software is provided
  36.  * with Restricted Rights.  Use, duplication, or disclosure by the
  37.  * Government is subject to restrictions as set forth in subparagraph (c)
  38.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  39.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  40.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  41.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  42.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  43.  *
  44.  **************************************************************************/
  45. #include <stdcomp.h>
  46.  
  47. #ifndef _RWSTD_NO_NAMESPACE
  48. namespace std {
  49. #endif
  50.  
  51. //
  52. // This requires that T have a default constructor.
  53. //
  54.  
  55.   template <class T, class Allocator>
  56.   void vector<T,Allocator>::resize (size_type new_size)
  57.   {
  58.     // T value; // RW_BUG: bts-78526
  59.     if (new_size > size())
  60.       insert(end(), new_size - size(), T() ); // RW_BUG: bts-78526
  61.     else if (new_size < size())
  62.       erase(begin() + new_size, end());
  63.   }
  64.  
  65.   template <class T, class Allocator>
  66.   void vector<T,Allocator>::resize (size_type new_size, T value)
  67.   {
  68.     if (new_size > size())
  69.       insert(end(), new_size - size(), value);
  70.     else if (new_size < size())
  71.       erase(begin() + new_size, end());
  72.   }
  73.  
  74.   template <class T, class Allocator>
  75.   vector<T,Allocator>& vector<T,Allocator>::operator= (const vector<T,Allocator>& x)
  76.   {
  77.     if (&x == this) return *this;
  78.     if (x.size() > capacity())
  79.     {
  80.       __value_alloc_type va(__end_of_storage);
  81.       iterator tmp = va.allocate(x.end() - x.begin(),0);
  82. #ifndef _RWSTD_NO_EXCEPTIONS
  83.       try {
  84.         __end_of_storage = uninitialized_copy(x.begin(), x.end(), tmp);
  85.       } catch(...) {
  86.         va.deallocate(tmp,x.end()-x.begin());
  87.         throw;
  88.       }
  89. #else
  90.       __end_of_storage = uninitialized_copy(x.begin(), x.end(), tmp);
  91. #endif // _RWSTD_NO_EXCEPTIONS
  92.       __destroy(__start, __finish);
  93.       va.deallocate(__start,__end_of_storage.data()-__start);
  94.       __start = tmp;
  95.     }
  96.     else if (size() >= x.size())
  97.     {
  98.       iterator i = copy(x.begin(), x.end(), begin());
  99.       __destroy(i, __finish);
  100.     }
  101.     else
  102.     {
  103.       copy(x.begin(), x.begin() + size(), begin());
  104.       uninitialized_copy(x.begin() + size(), x.end(), begin() + size());
  105.     }
  106.     __finish = begin() + x.size();
  107.     return *this;
  108.   }
  109.  
  110.   template <class T, class Allocator>
  111.   void vector<T,Allocator>::__insert_aux (
  112.       iterator position, const T& x)
  113.   {
  114.     if (__finish != __end_of_storage.data())
  115.     {
  116.       __value_alloc_type(__end_of_storage).construct(__finish, *(__finish - 1));
  117.       copy_backward(position, __finish - 1, __finish);
  118.       *position = x;
  119.       ++__finish;
  120.     }
  121.     else
  122.     {
  123.       //
  124.       // We always allocate enough space for a number of additional
  125.       // elements in the vector, unless the size of each element is
  126.       // very large. See definition of __rw_allocation_size in
  127.       // memory.
  128.       //
  129.       __value_alloc_type va(__end_of_storage);
  130.       size_type len = __RWSTD::__rw_allocation_size((value_type*)0,size(),__buffer_size);
  131.       iterator tmp = va.allocate(len,__start);
  132.       uninitialized_copy(begin(), position, tmp);
  133.       va.construct((tmp + (position - begin())), x);
  134. #ifndef _RWSTD_NO_EXCEPTIONS
  135.       try {
  136.         uninitialized_copy(position, end(), tmp + (position - begin()) + 1); 
  137.       } catch(...) {
  138.         va.deallocate(tmp,len);
  139.         throw;
  140.       }
  141. #else
  142.       uninitialized_copy(position, end(), tmp + (position - begin()) + 1);
  143. #endif // _RWSTD_NO_EXCEPTIONS
  144.       int tmp_size = size(); // RW_BUG: Fix for bts-78394
  145.       __destroy(begin(), end());
  146.       va.deallocate(begin(),__end_of_storage.data() - begin());
  147.       __end_of_storage = tmp + len;
  148.       __finish = tmp + tmp_size + 1; // RW_BUG: Fix for bts-78394
  149.       __start = tmp;
  150.     }
  151.   }
  152.  
  153.   template <class T, class Allocator>
  154.   void vector<T,Allocator>::__insert_aux (
  155.       iterator position, size_type n, const T& x)
  156.   {
  157.     if (n == 0) return;
  158.     if ((size_type)(__end_of_storage.data() - __finish) >= n)
  159.     {
  160.       iterator old_end = end();
  161.       if ((size_type)(end() - position) > n)
  162.       {
  163.         uninitialized_copy(old_end - n, old_end, old_end);
  164.         __finish += n;
  165.         copy_backward(position, old_end - n, old_end);
  166.         fill(position, position + n, x);
  167.       }
  168.       else
  169.       {
  170.         size_type first_part = n - (old_end - position);
  171.         uninitialized_fill_n(old_end, first_part, x);
  172.         __finish += first_part;
  173.         uninitialized_copy(position, old_end, position + n);
  174.         __finish += n - first_part;
  175.         fill(position, old_end, x);
  176.  
  177.       }
  178.  
  179.     }
  180.     else
  181.     {
  182.       __value_alloc_type va(__end_of_storage);
  183.       size_type len = size() + max(size(), n);
  184.       iterator tmp = va.allocate(len,__start);
  185. #ifndef _RWSTD_NO_EXCEPTIONS
  186.       try {
  187.         uninitialized_copy(begin(), position, tmp);
  188.         uninitialized_fill_n(tmp + (position - begin()), n, x);
  189.         uninitialized_copy(position, end(), tmp + (position - begin() + n));
  190.       } catch(...) {
  191.         va.deallocate(tmp,len);
  192.         throw;
  193.       }
  194. #else
  195.       uninitialized_copy(begin(), position, tmp);
  196.       uninitialized_fill_n(tmp + (position - begin()), n, x);
  197.       uninitialized_copy(position, end(), tmp + (position - begin() + n));
  198. #endif // _RWSTD_NO_EXCEPTIONS
  199.       int tmp_size = size(); // RW_BUG: Fix for bts-78394
  200.       __destroy(begin(), end());
  201.       va.deallocate(begin(),__end_of_storage.data() - begin());
  202.       __end_of_storage = tmp + len;
  203.       __finish = tmp + tmp_size + n; // RW_BUG: Fix for bts-78394
  204.       __start = tmp;
  205.     }
  206.   }
  207.  
  208. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  209.   template<class T, class Allocator>
  210.   template<class InputIterator>
  211.   void vector<T,Allocator>::__insert_aux2 (iterator position,
  212.                                     InputIterator first,
  213.                                     InputIterator last)
  214. #else
  215.   template<class T, class Allocator>
  216.   void vector<T,Allocator>::__insert_aux2 (iterator position,
  217.                                     const_iterator first,
  218.                                     const_iterator last)
  219. #endif
  220.   {
  221.     if (first == last) return;
  222.     size_type n;
  223.     __initialize(n, size_type(0));
  224.     distance(first, last, n);
  225.  
  226.     if ((size_type)(__end_of_storage.data() - __finish) >= n)
  227.     {
  228.       iterator old_end = end();
  229.       if ((size_type)(old_end - position) > n)
  230.       {
  231.         uninitialized_copy(old_end - n, old_end, old_end);
  232.         __finish += n;
  233.         copy_backward(position, old_end - n, old_end);
  234.         copy(first, last, position);
  235.       }
  236.       else
  237.       {
  238.         size_type first_part = (old_end - position);
  239. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  240.         InputIterator iter(first);
  241. #else
  242.         const_iterator iter(first);
  243. #endif
  244.         advance(iter, first_part);
  245.         uninitialized_copy(iter, last, old_end);
  246. //        uninitialized_copy(first + first_part, last, old_end);
  247.         __finish += n - first_part;
  248.         uninitialized_copy(position, old_end, position + n);
  249.         __finish += first_part;
  250.         copy(first, iter, position);
  251. //        copy(first, first + (old_end - position), position);
  252.       }
  253.  
  254.     }
  255.     else
  256.     {
  257.       __value_alloc_type va(__end_of_storage);
  258.       size_type len = size() + max(size(), n);
  259.       iterator tmp = va.allocate(len,__start);
  260. #ifndef _RWSTD_NO_EXCEPTIONS
  261.       try {
  262.         uninitialized_copy(begin(), position, tmp);
  263.         uninitialized_copy(first, last, tmp + (position - begin()));
  264.         uninitialized_copy(position, end(), tmp + (position - begin() + n));
  265.       } catch(...) {
  266.         va.deallocate(tmp,len);
  267.         throw;
  268.       }
  269. #else
  270.       uninitialized_copy(begin(), position, tmp);
  271.       uninitialized_copy(first, last, tmp + (position - begin()));
  272.       uninitialized_copy(position, end(), tmp + (position - begin() + n));
  273. #endif // _RWSTD_NO_EXCEPTIONS
  274.       int tmp_size = size(); // RW_BUG: Fix for bts-78394
  275.       __destroy(begin(), end());
  276.       va.deallocate(begin(),__end_of_storage.data() - begin());
  277.       __end_of_storage = tmp + len;
  278.       __finish = tmp + tmp_size + n; // RW_BUG: Fix for bts-78394
  279.       __start = tmp;
  280.     }
  281.   }
  282.  
  283. #ifndef _RWSTD_NO_MEMBER_TEMPLATES
  284. // The body of this function is duplicated in src/vecbool.cpp and
  285. // further down in this file as well.
  286. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  287.   template <class Allocator>
  288.   template<class InputIterator>
  289.   void vector<bool, Allocator >::insert 
  290. #else
  291.   template<class InputIterator>
  292.   void vector<bool, allocator<bool> >::insert 
  293. #endif // _RWSTD_NO_CLASS_PARTIAL_SPEC
  294.   (iterator position, 
  295.    InputIterator first,
  296.    InputIterator last)
  297.  
  298.   {
  299.     if (first == last) return;
  300.     size_type n;
  301.     __initialize(n, size_type(0));
  302.     distance(first, last, n);
  303.     if (capacity() - size() >= n)
  304.     {
  305.       copy_backward(position, end(), __finish + n);
  306.       copy(first, last, position);
  307.       __finish += n;
  308.     }
  309.     else
  310.     {
  311.       size_type len = size() + max(size(), n);
  312.       unsigned int* q = __bit_alloc(len);
  313.       iterator i = copy(begin(), position, iterator(q, 0));
  314.       i = copy(first, last, i);
  315.       __finish = copy(position, end(), i);
  316.       __value_alloc_type(__end_of_storage).deallocate(__start.p,__end_of_storage.data() - __start.p);
  317.       __end_of_storage = q + (len + _RWSTD_WORD_BIT - 1)/_RWSTD_WORD_BIT;
  318.       __start = iterator(q, 0);
  319.     }
  320.   }
  321. #endif // _RWSTD_NO_MEMBER_TEMPLATES
  322.  
  323. #ifndef _RWSTD_NO_CLASS_PARTIAL_SPEC
  324. // Duplicates of the followign functions exist in src/stl/vecbool.cpp.
  325. // Which set is used depends on the availability of partial specialization.
  326.  
  327.   template <class Allocator>
  328.   void vector<bool,Allocator >::flip ()
  329.   {
  330.     for (iterator i = begin(); !(i == end()); i++)
  331.       *i = !(*i);
  332.   }
  333.  
  334.   template <class Allocator>
  335.   void vector<bool,Allocator >::swap (reference x, reference y)
  336.   {
  337.     bool tmp = x; x = y; y = tmp;
  338.   }
  339.  
  340.   template <class Allocator>
  341.   void vector<bool,Allocator >::__insert_aux (iterator position, bool x)
  342.   {
  343.     if (__finish.p != __end_of_storage.data())
  344.     {
  345.       __copy_backward(position, __finish - 1, __finish);
  346.       *position = x;
  347.       ++__finish;
  348.     }
  349.     else
  350.     {
  351.       size_type len = size() ? 2 * size() : _RWSTD_WORD_BIT;
  352.       unsigned int* q = __bit_alloc(len);
  353.       iterator i = __copy(begin(), position, iterator(q, 0));
  354.       *i++ = x;
  355.       __finish = __copy(position, end(), i);
  356.       __value_alloc_type(__end_of_storage).deallocate(__start.p,__end_of_storage.data() - __start.p);
  357.       __end_of_storage = q + (len + _RWSTD_WORD_BIT - 1)/_RWSTD_WORD_BIT;
  358.       __start = iterator(q, 0);
  359.     }
  360.   }
  361.  
  362.   template <class Allocator>
  363.   void vector<bool,Allocator >::insert (iterator position, size_type n, const bool& x)
  364.   {
  365.     if (n == 0) return;
  366.     if (capacity() - size() >= n)
  367.     {
  368.       __copy_backward(position, end(), __finish + n);
  369.       __fill(position, position + n, x);
  370.       __finish += n;
  371.     }
  372.     else
  373.     {
  374.       size_type len = size() + max(size(), n);
  375.       unsigned int* q = __bit_alloc(len);
  376.       iterator i = __copy(begin(), position, iterator(q, 0));
  377.       __fill_n(i, n, x);
  378.       __finish = __copy(position, end(), i + n);
  379.       __value_alloc_type(__end_of_storage).deallocate(__start.p,__end_of_storage.data() - __start.p);
  380.       __end_of_storage = q + (len + _RWSTD_WORD_BIT - 1)/_RWSTD_WORD_BIT;
  381.       __start = iterator(q, 0);
  382.     }
  383.   }
  384. #ifdef _RWSTD_NO_MEMBER_TEMPLATES
  385.   template <class Allocator>
  386.   void vector<bool,Allocator >::insert (iterator position, const_iterator first,
  387.                                         const_iterator last)
  388.   {
  389.     if (first == last) return;
  390.     size_type n;
  391.     __initialize(n, size_type(0));
  392.     distance(first, last, n);
  393.     if (capacity() - size() >= n)
  394.     {
  395.       __copy_backward(position, end(), __finish + n);
  396.       __copy(first, last, position);
  397.       __finish += n;
  398.     }
  399.     else
  400.     {
  401.       size_type len = size() + max(size(), n);
  402.       unsigned int* q = __bit_alloc(len);
  403.       iterator i = __copy(begin(), position, iterator(q, 0));
  404.       i = __copy(first, last, i);
  405.       __finish = __copy(position, end(), i);
  406.       __value_alloc_type(__end_of_storage).deallocate(__start.p,__end_of_storage.data() - __start.p);
  407.       __end_of_storage = q + (len + _RWSTD_WORD_BIT - 1)/_RWSTD_WORD_BIT;
  408.       __start = iterator(q, 0);
  409.     }
  410.   }
  411. #endif // _RWSTD_NO_MEMBER_TEMPLATES
  412.  
  413.   template <class Allocator>
  414.   void vector<bool,Allocator >::resize (size_type new_size, bool c)
  415.   {
  416.     if (new_size > size())
  417.       insert(end(), new_size - size(), c);
  418.     else if (new_size < size())
  419.       erase(begin() + new_size, end());
  420.   }
  421.  
  422. #endif // _RWSTD_NO_CLASS_PARTIAL_SPEC
  423.  
  424. #ifndef _RWSTD_NO_NAMESPACE
  425. }
  426. #endif
  427.  
  428. #pragma option pop
  429. #endif /* __VECTOR_CC */
  430.