137 lines
2.1 KiB
C++
137 lines
2.1 KiB
C++
//-------------------------------------------------------------------------------------------------------+
|
|
// Copyright (C), 1998-2007, SH Software Co. Ltd.
|
|
// = FileName : SqStack 模板类
|
|
// = Version : ver2.0
|
|
// = Author : zjq
|
|
// = CreateDate : 2002-09-09
|
|
// = Description: SqStack 声明 基于动态数组的堆栈
|
|
// = Maintainers:
|
|
//
|
|
//-------------------------------------------------------------------------------------------------------+
|
|
#ifndef _SQSTACK_H_
|
|
#define _SQSTACK_H_
|
|
|
|
#ifndef _STACK_H_
|
|
#include "Stack.h"
|
|
#endif
|
|
|
|
#ifndef _SQTABLE_H_
|
|
#include "SqTable.h"
|
|
#endif
|
|
|
|
template<class T> class SqStack: public Stack<T>
|
|
{
|
|
public:
|
|
|
|
SqStack(long autoAllocUnit = 100,long depth = -1);
|
|
|
|
virtual ~SqStack();
|
|
|
|
virtual void Clear();
|
|
|
|
virtual void GoBack();
|
|
|
|
virtual void GoAhead();
|
|
|
|
virtual bool Pop(T&t);
|
|
|
|
virtual bool Push(T const&t);
|
|
|
|
virtual bool GetTopData(T&t);
|
|
|
|
virtual long GetDepth();
|
|
|
|
virtual T& operator[](long i);
|
|
|
|
protected:
|
|
|
|
SqTable<T> table;
|
|
};
|
|
|
|
template<class T>
|
|
SqStack<T>::~SqStack()
|
|
{
|
|
|
|
}
|
|
|
|
template<class T>
|
|
virtual T& SqStack<T>::operator[](long i)
|
|
{
|
|
return table[i];
|
|
}
|
|
|
|
template<class T>
|
|
SqStack<T>::SqStack(long autoAllocUnit,long depth)
|
|
:Stack<T>(depth),table(autoAllocUnit,autoAllocUnit)
|
|
{
|
|
}
|
|
|
|
template<class T>
|
|
bool SqStack<T>::Push(T const&t)
|
|
{
|
|
if(this->IsDepthLimited() && GetDepth() >= this->maxDepth)
|
|
{
|
|
return false;
|
|
}
|
|
table[table.GetCur()+1] = t;
|
|
this->oldDepth = GetDepth();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
bool SqStack<T>::Pop(T&t)
|
|
{
|
|
if(this->IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
t = table.GetCurData();
|
|
table.MoveToPre();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
bool SqStack<T>::GetTopData(T&t)
|
|
{
|
|
if(this->IsEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
t = table.GetCurData();
|
|
return true;
|
|
}
|
|
|
|
template<class T>
|
|
void SqStack<T>::Clear()
|
|
{
|
|
table.Clear();
|
|
this->oldDepth = 0;
|
|
}
|
|
|
|
template<class T>
|
|
long SqStack<T>::GetDepth()
|
|
{
|
|
return table.GetCur()+1;
|
|
}
|
|
|
|
template<class T>
|
|
void SqStack<T>::GoBack()
|
|
{
|
|
if(this->CanGoBack())
|
|
{
|
|
table.MoveToPre();
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
void SqStack<T>::GoAhead()
|
|
{
|
|
if(this->CanGoAhead())
|
|
{
|
|
table.MoveToNext();
|
|
}
|
|
}
|
|
|
|
#endif
|