home *** CD-ROM | disk | FTP | other *** search
/ Informática Multimedia: Special Games / INFESPGAMES.mdf / os2 / ribble / support / cvector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  5.4 KB  |  226 lines

  1. #include "CVector.h"
  2.  
  3.  
  4. template <class T>
  5. inline CVector<T>::CVector(void)
  6. {
  7.   set = 0;
  8.   chunk_size = DEF_CHUNK_SIZE;
  9. }
  10.  
  11. template <class T>
  12. inline CVector<T>::~CVector()
  13. {
  14.   if (set)
  15.     delete [] data;
  16. }
  17.  
  18. template <class T>
  19. inline CVector<T>& CVector<T>::operator+=(const T from)
  20. {
  21.   operator[](upper + 1) = from;
  22.   return *this;
  23. }
  24.  
  25. template <class T>
  26. inline CVector<T> CVector<T>::operator+(const CVector<T>& from) const
  27. {
  28.   CVector<T> temp = *this;
  29.   temp += from;
  30.   return temp;
  31. }
  32.  
  33. template <class T>
  34. inline int CVector<T>::operator!=(const CVector<T>& from) const
  35. {
  36.   return !operator==(from);
  37. }
  38.  
  39. template <class T>
  40. void CVector<T>::setRange(long from, long to, T value)
  41. {
  42.   // Fill from outer edge of vector to the
  43.   // inner so that a maximum of one chunk allocation
  44.   // is performed.
  45.  
  46.   if (to >= from)
  47.     for (long ind = to; ind >= from; ind--)
  48.       operator[](ind) = value;
  49.   else
  50.     for (long ind = from; ind >= to; ind--)
  51.       operator[](ind) = value;
  52. }
  53.  
  54. template <class T>
  55. long CVector<T>::roundUp(long from) const
  56. {
  57.   return ((from / chunk_size) + 1) * chunk_size;
  58. }
  59.  
  60. template <class T>
  61. long CVector<T>::roundDown(long from) const
  62. {
  63.   return ((from / chunk_size) - 1) * chunk_size;
  64. }
  65.  
  66. template <class T>
  67. CVector<T>::CVector(const CVector<T>& from)
  68. {
  69.   chunk_size = DEF_CHUNK_SIZE;
  70.   if ((set = from.set) != 0)
  71.     {
  72.       lower = from.lower;
  73.       upper = from.upper;
  74.       clb = from.clb;
  75.       cub = from.cub;
  76.       data = new CSHuge T[(unsigned long)(cub - clb + 1)];
  77.       CAssert(data);
  78.       unsigned long len = size();
  79.       for (unsigned long ind = 0; ind < len; ind++)
  80.         data[(unsigned long)(ind + lower - clb)] = from.data[(unsigned long)(ind + lower - clb)];
  81.     }
  82. }
  83.  
  84. template <class T>
  85. CVector<T>::CVector(const T from)
  86. {
  87.   chunk_size = DEF_CHUNK_SIZE;
  88.   set = 1;
  89.   lower = upper = 0;
  90.   clb = roundDown(lower);
  91.   cub = roundUp(upper);
  92.   data = new CSHuge T[(unsigned long)(cub - clb + 1)];
  93.   CAssert(data);
  94.   data[(unsigned long)(0 + lower - clb)] = from;
  95. }
  96.  
  97. template <class T>
  98. CVector<T>::CVector(const T CSHuge* from, unsigned long frlen)
  99. {
  100.   chunk_size = DEF_CHUNK_SIZE;
  101.   set = 1;
  102.   lower = 0;
  103.   upper = frlen - 1;
  104.   clb = roundDown(lower);
  105.   cub = roundUp(upper);
  106.   data = new CSHuge T[(unsigned long)(cub - clb + 1)];
  107.   CAssert(data);
  108.   for (unsigned long ind = 0; ind < frlen; ind++)
  109.     data[(unsigned long)(ind + lower - clb)] = from[ind];
  110. }
  111.  
  112. template <class T>
  113. CVector<T>& CVector<T>::operator=(const CVector<T>& from)
  114. {
  115.   if (this != &from)
  116.     {
  117.       if (set)
  118.         delete [] data;
  119.       if ((set = from.set) != 0)
  120.         {
  121.           lower = from.lower;
  122.           upper = from.upper;
  123.           clb = from.clb;
  124.           cub = from.cub;
  125.           data = new CSHuge T[(unsigned long)(cub - clb + 1)];
  126.           CAssert(data);
  127.           unsigned long len = size();
  128.           for (unsigned long ind = 0; ind < len; ind++)
  129.             data[(unsigned long)(ind + lower - clb)] = from.data[(unsigned long)(ind + lower - clb)];
  130.         }
  131.     }
  132.   return *this;
  133. }
  134.  
  135. template <class T>
  136. CVector<T>& CVector<T>::operator=(const T from)
  137. {
  138.   if (set)
  139.     delete [] data;
  140.   else
  141.     set = 1;
  142.   lower = upper = 0;
  143.   clb = roundDown(lower);
  144.   cub = roundUp(upper);
  145.   data = new CSHuge T[(unsigned long)(cub - clb + 1)];
  146.   CAssert(data);
  147.   data[(unsigned long)(0 - clb)] = from;
  148.   return *this;
  149. }
  150.  
  151. template <class T>
  152. CVector<T>& CVector<T>::operator+=(const CVector<T>& from)
  153. {
  154.   unsigned long len = from.size();
  155.   for (unsigned long ind = 0; ind < len; ind++)
  156.     operator[](upper + 1) = from.data[(unsigned long)(ind + from.lower - from.clb)];
  157.   return *this;
  158. }
  159.  
  160. template <class T>
  161. int CVector<T>::operator==(const CVector<T>& from) const
  162. {
  163.   if (this == &from)
  164.     return 1;
  165.   
  166.   if (size() != from.size())
  167.     return 0;
  168.   unsigned long len = size();
  169.   for (unsigned long ind = 0; ind < len; ind++)
  170.     if (data[(unsigned long)(ind + lower - clb)] != from.data[(unsigned long)(ind + from.lower - from.clb)])
  171.       return 0;
  172.   return 1;
  173. }
  174.  
  175. template <class T>
  176. T& CVector<T>::operator[](long index)
  177. {
  178.   if (!set)
  179.     {
  180.       set = 1;
  181.       lower = upper = index;
  182.       clb = roundDown(lower);
  183.       cub = roundUp(upper);
  184.       data = new CSHuge T[(unsigned long)(cub - clb + 1)];
  185.       CAssert(data);
  186.     }
  187.   else if (index < clb)
  188.     {
  189.       long newclb = roundDown(index);
  190.       long newlower = index;
  191.       T CSHuge* newdata = new CSHuge T[(unsigned long)(cub - newclb + 1)];
  192.       CAssert(newdata);
  193.       for (unsigned long ind = 0; ind < upper - lower + 1; ind++)
  194.         newdata[(unsigned long)(ind + lower - newclb)] = data[(unsigned long)(ind + lower - clb)];
  195.       delete [] data;
  196.       clb = newclb;
  197.       lower = newlower;
  198.       data = newdata;
  199.     }
  200.   else if (index > cub)
  201.     {
  202.       long newcub = roundUp(index);
  203.       long newupper = index;
  204.       T CSHuge* newdata = new CSHuge T[(unsigned long)(newcub - clb + 1)];
  205.       CAssert(newdata);
  206.       for (unsigned long ind = 0; ind < upper - lower + 1; ind++) //was newupper
  207.         newdata[(unsigned long)(ind + lower - clb)] = data[(unsigned long)(ind + lower - clb)];
  208.       delete [] data;
  209.       cub = newcub;
  210.       upper = newupper;
  211.       data = newdata;
  212.     }
  213.   else if (index < lower)
  214.     {
  215.       lower = index;
  216.     }
  217.   else if (index > upper)
  218.     {
  219.       upper = index;
  220.     }
  221.   
  222.   return data[(unsigned long)(index - clb)];
  223. }
  224.  
  225.  
  226.