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

  1. /****************************************************************************
  2.     $Id: stack.cpp 501.0 1995/03/07 12:27:02 RON Exp $
  3.  
  4.     Copyright (c) 1991-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 TLStack<T>.
  10.  
  11.     $Log: stack.cpp $
  12.     Revision 501.0  1995/03/07 12:27:02  RON
  13.     Updated for TLX 5.01
  14.     Revision 1.5  1995/01/31 16:30:52  RON
  15.     Update for release 012
  16.     Added partial support for SunPro C++ compiler
  17.     Revision 1.4  1994/09/28  14:42:57  ron
  18.     Removed Macintosh-style #include references
  19.  
  20.     Revision 1.3  1994/09/27  20:27:43  ron
  21.     Changed path separator from / to \
  22.  
  23.     Revision 1.2  1994/09/26  15:36:21  ron
  24.     hanged include file references
  25.  
  26.     Revision 1.1  1994/08/16  18:15:35  ron
  27.     Initial revision
  28.  
  29. ****************************************************************************/
  30.  
  31. #ifndef _TLX_STACK_CPP
  32. #define _TLX_STACK_CPP
  33.  
  34. //----- Project headers
  35.  
  36. #ifndef _TLX_ARRAYS_H
  37. #include <tlx\501\arrays.h>
  38. #endif
  39. #ifndef _TLX_DEBUG_H
  40. #include <tlx\501\debug.h>
  41. #endif
  42. #ifndef _TLX_EXCEPT_H
  43. #include <tlx\501\except.h>
  44. #endif
  45.  
  46. #ifndef _TLX_SEQBASE_CPP
  47. #include <tlx\501\template\seqbase.cpp>
  48. #endif
  49.  
  50. /*-------------------------------------------------------------------------*/
  51.     template<class T> TLStack<T>::TLStack(size_t size, size_t delta)
  52.  
  53. /*  Constructor creating a stack with the given initial capacity and
  54.     expansion factor.
  55. ---------------------------------------------------------------------------*/
  56. : TLSeqBase<T>(size, delta)
  57. {
  58. }
  59.  
  60. /*-------------------------------------------------------------------------*/
  61.     template<class T> TLStack<T>::TLStack(const T &t)
  62.  
  63. /*  Constructor creating a stack which holds a single element. The stack
  64.     is not expandable initially.
  65. ---------------------------------------------------------------------------*/
  66. : TLSeqBase<T>(t)
  67. {
  68. }
  69.  
  70. /*-------------------------------------------------------------------------*/
  71.     template<class T> bool TLStack<T>::IterFirst(iter_t &i) const
  72.  
  73. /*  Initializes the iterator to address the first (topmost) element of the
  74.     stack. Returns nonzero if the iterator is valid after the operation,
  75.     else zero.
  76. ---------------------------------------------------------------------------*/
  77. {
  78.     return (i.ix = mCount - 1) >= 0;
  79. }
  80.  
  81. /*-------------------------------------------------------------------------*/
  82.     template<class T> bool TLStack<T>::IterLast(iter_t &i) const
  83.  
  84. /*  Initializes the iterator to address the last (bottommost) element of the
  85.     stack. Returns nonzero if the iterator is valid after the operation,
  86.     else zero.
  87. ---------------------------------------------------------------------------*/
  88. {
  89.     return (i.ix = 0) < mCount;
  90. }
  91.  
  92. /*-------------------------------------------------------------------------*/
  93.     template<class T> bool TLStack<T>::IterNext(iter_t &i) const
  94.  
  95. /*  Advances the iterator to address the next (bottomward) element of the
  96.     stack. Returns nonzero if the iterator is valid after the operation,
  97.     else zero.
  98. ---------------------------------------------------------------------------*/
  99. {
  100.     return --i.ix >= 0;
  101. }
  102.  
  103. /*-------------------------------------------------------------------------*/
  104.     template<class T> bool TLStack<T>::IterPrev(iter_t &i) const
  105.  
  106. /*  Advances the iterator to address the previous (topward) element of the
  107.     stack. Returns nonzero if the iterator is valid after the operation,
  108.     else zero.
  109. ---------------------------------------------------------------------------*/
  110. {
  111.     return ++i.ix < mCount;
  112. }
  113.  
  114.  
  115. /*-------------------------------------------------------------------------*/
  116.     template<class T> TLStack<T> &TLStack<T>::operator =(const T &t)
  117.  
  118. /*  Overloading of the assignment operator that allows assignment of a
  119.     single element to the stack. The previous stack contents are lost.
  120. ---------------------------------------------------------------------------*/
  121. {
  122.     RemoveAll();
  123.     Push(t);
  124.     return *this;
  125. }
  126.  
  127. /*-------------------------------------------------------------------------*/
  128.     template<class T> TLStack<T> &TLStack<T>::operator =(const TLStack<T> &s)
  129.  
  130. /*  Overloading of the assignment operator.
  131. ---------------------------------------------------------------------------*/
  132. {
  133.     if (this != &s)
  134.     TLSeqBase<T>::operator =(s);
  135.     return *this;
  136. }
  137.  
  138. /*-------------------------------------------------------------------------*/
  139.     template<class T> T TLStack<T>::Pop()
  140.  
  141. /*  Removes and returns the topmost element of the stack. If the stack is
  142.     empty, a TLXEmpty exception is thrown.
  143. ---------------------------------------------------------------------------*/
  144. {
  145.     if (IsEmpty())
  146.     THROW(TLXEmpty(LOCUS));
  147.  
  148.     return mVect[--mCount];
  149. }
  150.  
  151. /*-------------------------------------------------------------------------*/
  152.     template<class T> void TLStack<T>::Push(const T &t)
  153.  
  154. /*  Adds a new element to the top of the stack.
  155. ---------------------------------------------------------------------------*/
  156. {
  157.     if (IsFull())
  158.         Expand();
  159.  
  160.     TLX_ASSERT(Available() >= 1);
  161.  
  162.     mVect[mCount++] = t;
  163. }
  164.  
  165. /*-------------------------------------------------------------------------*/
  166.     template<class T> T &TLStack<T>::PeekTop()
  167.  
  168. /*  Returns a reference to the topmost element of the stack. If the stack
  169.     is empty, a TLXEmpty exception is thrown.
  170. ---------------------------------------------------------------------------*/
  171. {
  172.     if (IsEmpty())
  173.     THROW(TLXEmpty(LOCUS));
  174.  
  175.     return mVect[mCount - 1];
  176. }
  177.  
  178. /*-------------------------------------------------------------------------*/
  179.     template<class T> const T &TLStack<T>::PeekTop() const
  180.  
  181. /*  Returns a reference to the topmost element of the stack. If the stack
  182.     is empty, a TLXEmpty exception is thrown.
  183. ---------------------------------------------------------------------------*/
  184. {
  185.     if (IsEmpty())
  186.     THROW(TLXEmpty(LOCUS));
  187.  
  188.     return mVect[mCount - 1];
  189. }
  190.  
  191. /*-------------------------------------------------------------------------*/
  192.     template<class T> void TLStack<T>::RemoveTop()
  193.  
  194. /*  Removes the topmost element of the stack. If the stack is empty, a
  195.     TLXEmpty exception is thrown.
  196. ---------------------------------------------------------------------------*/
  197. {
  198.     if (IsEmpty())
  199.     THROW(TLXEmpty(LOCUS));
  200.  
  201.     TLX_ASSERT(mCount > 0);
  202.     --mCount;
  203. }
  204.  
  205. #endif    // _TLX_STACK_CPP
  206.