home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tlx501.zip / TEMPLATE / PTRSEQ.CPP < prev    next >
C/C++ Source or Header  |  1996-01-05  |  9KB  |  273 lines

  1. /****************************************************************************
  2.     $Id: ptrseq.cpp 501.0 1995/03/07 12:26:58 RON Exp $
  3.  
  4.     Copyright (c) 1993-95 Tarma Software Research. All rights reserved.
  5.  
  6.     Project:    Tarma Library for C++ V5.0
  7.     Author:    Ron van der Wal
  8.  
  9.     Implementation of class TLPtrSeq<T>
  10.  
  11.     $Log: ptrseq.cpp $
  12.     Revision 501.0  1995/03/07 12:26:58  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.7  1995/01/31 16:31:52  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.6  1994/10/06  17:51:16  ron
  18.     Changed #defined name
  19.  
  20.     Revision 1.5  1994/10/05  18:49:34  ron
  21.     Added constructor that accepts an iterator
  22.  
  23.     Revision 1.4  1994/09/28  14:30:40  ron
  24.     Fixed spelling of mSet to mSeq in TLPtrSeq iterators
  25.  
  26.     Revision 1.3  1994/09/27  20:27:22  ron
  27.     Changed path separator from / to \
  28.  
  29.     Revision 1.2  1994/09/26  15:32:49  ron
  30.     Implemented iterators
  31.  
  32.     Revision 1.1  1994/08/16  18:15:28  ron
  33.     Initial revision
  34.  
  35. ****************************************************************************/
  36.  
  37. #ifndef _TLX_PTRSEQ_CPP
  38. #define _TLX_PTRSEQ_CPP
  39.  
  40. //----- Project headers
  41.  
  42. #ifndef _TLX_DEBUG_H
  43. #include <tlx\501\debug.h>
  44. #endif
  45. #ifndef _TLX_EXCEPT_H
  46. #include <tlx\501\except.h>
  47. #endif
  48. #ifndef _TLX_PTRARRAY_H
  49. #include <tlx\501\ptrarray.h>
  50. #endif
  51.  
  52. #ifndef _TLX_PTRITER_CPP
  53. #include <tlx\501\template\ptriter.cpp>
  54. #endif
  55.  
  56. /*-------------------------------------------------------------------------*/
  57.     template<class T> TLPtrSeqIter<T>::TLPtrSeqIter(TLPtrSeq<T> &aSeq)
  58.  
  59. /*  Constructor. Links to the given set.
  60. ---------------------------------------------------------------------------*/
  61. : mSeq(aSeq)
  62. {
  63. }
  64.  
  65. /*-------------------------------------------------------------------------*/
  66.     template<class T> size_t TLPtrSeqIter<T>::Count() const
  67.  
  68. /*  Returns the number of items in the associated collection.
  69. ---------------------------------------------------------------------------*/
  70. {
  71.     return mSeq.Count();
  72. }
  73.  
  74. /*-------------------------------------------------------------------------*/
  75.     template<class T> bool TLPtrSeqIter<T>::FirstPos()
  76.  
  77. /*  Sets the iterator to the first position (if any), returning true
  78.     on success.
  79. ---------------------------------------------------------------------------*/
  80. {
  81.     mPos = mSeq.Mini();
  82.     return mPos <= mSeq.Maxi();
  83. }
  84.  
  85. /*-------------------------------------------------------------------------*/
  86.     template<class T> bool TLPtrSeqIter<T>::NextPos()
  87.  
  88. /*  Advances the iterator to the next position (if any), returning true
  89.     on success.
  90. ---------------------------------------------------------------------------*/
  91. {
  92.     if (mPos < mSeq.Maxi()) {    // Test first to avoid integer overflow
  93.     mPos++;
  94.     return true;
  95.     } else
  96.     return false;
  97. }
  98.  
  99. /*-------------------------------------------------------------------------*/
  100.     template<class T> T *&TLPtrSeqIter<T>::Peek() const
  101.  
  102. /*  Returns the current iteration element.
  103. ---------------------------------------------------------------------------*/
  104. {
  105.     TLX_ASSERT(IsValid());
  106.     return mSeq.PeekAt(mPos);
  107. }
  108.  
  109. /*-------------------------------------------------------------------------*/
  110.     template<class T> TLPtrSeqIterConst<T>::TLPtrSeqIterConst
  111.         (const TLPtrSeq<T> &aSeq)
  112.  
  113. /*  Constructor. Links to the given set.
  114. ---------------------------------------------------------------------------*/
  115. : mSeq(aSeq)
  116. {
  117. }
  118.  
  119. /*-------------------------------------------------------------------------*/
  120.     template<class T> size_t TLPtrSeqIterConst<T>::Count() const
  121.  
  122. /*  Returns the number of items in the associated collection.
  123. ---------------------------------------------------------------------------*/
  124. {
  125.     return mSeq.Count();
  126. }
  127.  
  128. /*-------------------------------------------------------------------------*/
  129.     template<class T> bool TLPtrSeqIterConst<T>::FirstPos()
  130.  
  131. /*  Sets the iterator to the first position (if any), returning true
  132.     on success.
  133. ---------------------------------------------------------------------------*/
  134. {
  135.     mPos = mSeq.Mini();
  136.     return mPos <= mSeq.Maxi();
  137. }
  138.  
  139. /*-------------------------------------------------------------------------*/
  140.     template<class T> bool TLPtrSeqIterConst<T>::NextPos()
  141.  
  142. /*  Advances the iterator to the next position (if any), returning true
  143.     on success.
  144. ---------------------------------------------------------------------------*/
  145. {
  146.     if (mPos < mSeq.Maxi()) {    // Test first to avoid integer overflow
  147.     mPos++;
  148.     return true;
  149.     } else
  150.     return false;
  151. }
  152.  
  153. /*-------------------------------------------------------------------------*/
  154.     template<class T> T *TLPtrSeqIterConst<T>::Peek() const
  155.  
  156. /*  Returns the current iteration element.
  157. ---------------------------------------------------------------------------*/
  158. {
  159.     TLX_ASSERT(IsValid());
  160.     return mSeq.PeekAt(mPos);
  161. }
  162.  
  163. /*-------------------------------------------------------------------------*/
  164.     template<class T> TLPtrSeq<T>::TLPtrSeq(size_t aSize, size_t aDelta)
  165.  
  166. /*  Constructor to create sequence of specified size; also doubles as
  167.     default constructor.
  168. ---------------------------------------------------------------------------*/
  169. : TLVPSeq(aSize, aDelta)
  170. {
  171.     SetDelete(DeleteT);
  172. }
  173.  
  174. /*-------------------------------------------------------------------------*/
  175.     template<class T> TLPtrSeq<T>::TLPtrSeq(T *aPtr)
  176.  
  177. /*  Constructor that initializes a sequence of 1 element.
  178. ---------------------------------------------------------------------------*/
  179. : TLVPSeq(aPtr)
  180. {
  181.     SetDelete(DeleteT);
  182. }
  183.  
  184. /*-------------------------------------------------------------------------*/
  185.     template<class T> TLPtrSeq<T>::TLPtrSeq(T **aSeq, size_t aSize)
  186.  
  187. /*  Constructor that initializes a sequence from a corresponding C-style
  188.     vector.
  189. ---------------------------------------------------------------------------*/
  190. : TLVPSeq((void **)aSeq, aSize)
  191. {
  192.     SetDelete(DeleteT);
  193. }
  194.  
  195. /*-------------------------------------------------------------------------*/
  196.     template<class T> TLPtrSeq<T>::TLPtrSeq(TLPtrIter<T> &aIter)
  197.  
  198. /*  Constructor that initializes from an iterator.
  199. ---------------------------------------------------------------------------*/
  200. : TLVPSeq(aIter.Count())
  201. {
  202.     SetDelete(DeleteT);
  203.     for (aIter.Reset(); aIter.Next(); )
  204.     Append(aIter.Peek());
  205. }
  206.  
  207. /*-------------------------------------------------------------------------*/
  208.     template<class T> TLPtrSeq<T>::TLPtrSeq(const TLPtrSeq<T> &aSeq)
  209.  
  210. /*  Copy constructor.
  211. ---------------------------------------------------------------------------*/
  212. : TLVPSeq(aSeq)
  213. {
  214.     SetDelete(DeleteT);
  215. }
  216.  
  217. /*-------------------------------------------------------------------------*/
  218.     template<class T> void TLPtrSeq<T>::DeleteT(void *aPtr)
  219.  
  220. /*  Deletes pointed to object, after the appropriate typecast.
  221. ---------------------------------------------------------------------------*/
  222. {
  223.     delete (T *)aPtr;
  224. }
  225.  
  226. /*-------------------------------------------------------------------------*/
  227.     template<class T> TLPtrSeq<T> TLPtrSeq<T>::Extract
  228.     (
  229.         index_t     aIndex,
  230.     size_t     aLength
  231.     )
  232.  
  233. /*  Extracts and returns a subsequence of the current sequence.
  234. ---------------------------------------------------------------------------*/
  235. {
  236.     if (!IsValidIndex(aIndex))
  237.     THROW(TLXIndex(LOCUS, aIndex));
  238.  
  239.     // Adjust number of items to remove, if necessary
  240.     if (aLength > (Maxi() - aIndex + 1))
  241.     aLength = Maxi() - aIndex + 1;
  242.  
  243.     // Create a copy of the subsequence, then remove the elements
  244.     TLPtrSeq<T> vCopy(&PeekAt(aIndex), aLength);
  245.     RemoveAt(aIndex, aLength);
  246.  
  247.     return vCopy;
  248. }
  249.  
  250. /*-------------------------------------------------------------------------*/
  251.     template<class T> TLPtrSeq<T> TLPtrSeq<T>::ExtractFirst(size_t aLength)
  252.  
  253. /*  Extracts and returns a subsequence of the current sequence, starting at
  254.     the beginning of the current sequence.
  255. ---------------------------------------------------------------------------*/
  256. {
  257.     return Extract(Mini(), aLength);
  258. }
  259.  
  260. /*-------------------------------------------------------------------------*/
  261.     template<class T> TLPtrSeq<T> TLPtrSeq<T>::ExtractLast(size_t aLength)
  262.  
  263. /*  Extracts and returns a subsequence of the current sequence, starting
  264.     at the end of the current sequence.
  265. ---------------------------------------------------------------------------*/
  266. {
  267.     if (aLength > Count())
  268.     aLength = Count();
  269.     return Extract(Maxi() - aLength + 1, aLength);
  270. }
  271.  
  272. #endif    // _TLX_PTRSEQ_CPP
  273.