Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef StackHPP
00033 #define StackHPP
00034
00035 #ifdef _MSC_VER
00036 #pragma warning(push, 1)
00037 #endif
00038
00039 #include <stddef.h>
00040
00041 #ifdef _MSC_VER
00042 #pragma warning(pop)
00043 #endif
00044
00045
00046 #include <string.h>
00047 #include <zeusbase/System/Interfaces/IStack.hpp>
00048 #include <zeusbase/System/Iterators.hpp>
00049
00050 BEGIN_NAMESPACE_Zeus
00051
00052
00055
00056 template <class T> class TStack : public IStack<T>
00057 {
00058 public:
00059 TStack();
00060 TStack(const T& rEmptyObject);
00061 virtual ~TStack();
00062
00063
00064 virtual void MQUALIFIER copyToStack(IStack<T>& rStack) const;
00065 virtual void MQUALIFIER copyToList(IList<T>& rList) const;
00066 virtual void MQUALIFIER flush();
00067 virtual Int MQUALIFIER getCount() const;
00068 virtual bool MQUALIFIER hasItem(const T& rItem) const;
00069 virtual bool MQUALIFIER hasAllItems(const IList<T>& rlstItems) const;
00070 virtual bool MQUALIFIER isEmpty() const;
00071 virtual void MQUALIFIER push(const T& rData);
00072 virtual void MQUALIFIER pushAll(const IList<T>& rlstItems);
00073 virtual T MQUALIFIER pop();
00074 virtual T& MQUALIFIER peek();
00075
00076
00077 TStack<T>& operator=(const TStack<T>& rStack);
00078 TStack<T>& operator=(const IStack<T>& rStack);
00079
00080 protected:
00081 T getItem(Int iIndex) const;
00082
00083 private:
00084
00087
00088 struct TBaseElement
00089 {
00090 inline TBaseElement(T data, TBaseElement* next)
00091 {
00092 Data = data;
00093 Next = next;
00094 }
00095
00096 T Data;
00097 TBaseElement* Next;
00098 };
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00115 TBaseElement* m_pElHead;
00117 Int m_iSize;
00119 T m_EmptyObject;
00120 };
00121
00122
00123
00126
00127 template <class T> inline TStack<T>::TStack()
00128
00129
00130
00131
00132
00133
00134
00135 : m_EmptyObject(T())
00136 {
00137 m_pElHead = NULL;
00138 m_iSize = 0;
00139
00140
00141
00142 }
00143
00144
00149
00150 template <class T> inline TStack<T>::TStack(const T& rEmptyItem)
00151 : m_EmptyObject(rEmptyItem)
00152 {
00153 m_pElHead = NULL;
00154 m_iSize = 0;
00155 }
00156
00157
00160
00161 template <class T> inline TStack<T>::~TStack()
00162 {
00163 flush();
00164 m_pElHead = NULL;
00165 }
00166
00167
00168
00173
00174 template <class T> T TStack<T>::getItem(Int iIndex) const
00175 {
00176 T tRetval = m_EmptyObject;
00177 if (m_pElHead != NULL && iIndex >= 0 && iIndex < m_iSize)
00178 {
00179 TBaseElement* pActElement = m_pElHead;
00180 Int iCount = 0;
00181 while(iCount <= iIndex && pActElement != NULL)
00182 {
00183 pActElement = pActElement->Next;
00184 ++iCount;
00185 }
00186
00187 if (pActElement != NULL)
00188 {
00189 tRetval = pActElement->Data;
00190 }
00191 }
00192 return tRetval;
00193 }
00194
00195
00198
00199 template <class T> void MQUALIFIER TStack<T>::flush()
00200 {
00201 while(m_pElHead != NULL)
00202 {
00203 TBaseElement* next = m_pElHead->Next;
00204 delete m_pElHead;
00205 m_pElHead = next;
00206 }
00207 m_iSize = 0;
00208 }
00209
00210
00213
00214 template <class T> bool MQUALIFIER TStack<T>::hasItem(const T& rItem) const
00215 {
00216 bool bRetval = false;
00217 TBaseElement* pActElement = m_pElHead;
00218 while(pActElement != NULL && !bRetval)
00219 {
00220 bRetval = (pActElement->Data == rItem);
00221 pActElement = pActElement->Next;
00222 }
00223 return bRetval;
00224 }
00225
00226
00229
00230 template <class T> inline bool MQUALIFIER TStack<T>::hasAllItems(const IList<T>& rlstItems) const
00231 {
00232 bool bRetval = true;
00233 TConstIterator<T> It = rlstItems.getConstIterator();
00234 while(bRetval && It.hasNextItem())
00235 {
00236 bRetval &= this->hasItem(It.getNextItemConst());
00237 }
00238 return bRetval;
00239 }
00240
00241
00244
00245 template <class T> inline void MQUALIFIER TStack<T>::push(const T& data)
00246 {
00247 m_pElHead = new TBaseElement(data, m_pElHead);
00248 m_iSize++;
00249 }
00250
00251
00254
00255 template <class T> inline void MQUALIFIER TStack<T>::pushAll(const IList<T>& rlstItems)
00256 {
00257 TConstIterator<T> It = rlstItems.getConstIterator();
00258 while(It.hasNextItem())
00259 {
00260 this->push(It.getNextItemConst());
00261 }
00262 }
00263
00264
00265
00268
00269 template <class T> T MQUALIFIER TStack<T>::pop()
00270 {
00271 if (m_pElHead != NULL)
00272 {
00273 T retval = m_pElHead->Data;
00274 TBaseElement* del = m_pElHead;
00275 m_pElHead = m_pElHead->Next;
00276 delete del;
00277 m_iSize--;
00278 return retval;
00279 }
00280 else
00281 {
00282 return m_EmptyObject;
00283 }
00284 }
00285
00286
00287
00290
00291 template <class T> inline T& MQUALIFIER TStack<T>::peek()
00292 {
00293 if (m_pElHead != NULL)
00294 {
00295 return m_pElHead->Data;
00296 }
00297 else
00298 {
00299 return m_EmptyObject;
00300 }
00301 }
00302
00303
00306
00307 template <class T> inline Int MQUALIFIER TStack<T>::getCount() const
00308 {
00309 return m_iSize;
00310 }
00311
00312
00313
00316
00317 template <class T> inline bool MQUALIFIER TStack<T>::isEmpty() const
00318 {
00319 return (m_iSize == 0);
00320 }
00321
00322
00323
00326
00327 template <class T> void MQUALIFIER TStack<T>::copyToStack(IStack<T>& rStack) const
00328 {
00329 TStack<T> stackTemp;
00330
00331 TBaseElement* pActElement = m_pElHead;
00332 while(pActElement != NULL)
00333 {
00334 stackTemp.push(pActElement->Data);
00335 pActElement = pActElement->Next;
00336 }
00337
00338 while(!stackTemp.isEmpty())
00339 {
00340 rStack.push(stackTemp.pop());
00341 }
00342 }
00343
00344
00347
00348 template <class T> void MQUALIFIER TStack<T>::copyToList(IList<T>& rList) const
00349 {
00350 TBaseElement* pActElement = m_pElHead;
00351 while(pActElement != NULL)
00352 {
00353 rList.add(pActElement->Data);
00354 pActElement = pActElement->Next;
00355 }
00356 }
00357
00358
00362
00363 template <class T> inline TStack<T>& TStack<T>::operator=(const TStack<T>& rStack)
00364 {
00365 rStack.copyToStack(*this);
00366 return *this;
00367 }
00368
00369
00373
00374 template <class T> inline TStack<T>& TStack<T>::operator=(const IStack<T>& rStack)
00375 {
00376 rStack.copyToStack(*this);
00377 return *this;
00378 }
00379
00380 END_NAMESPACE_Zeus
00381
00382 #endif