📄 citer.h
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: CIter.h,v 1.2 2002/08/06 20:09:36 dallen Exp $
____________________________________________________________________________*/
#ifndef Included_CIter_h // [
#define Included_CIter_h
#include "CList.h"
_PGP_BEGIN
// Class CIter
template <typename T> class CIter
{
public:
CIter(const CList<T> *pItems);
virtual ~CIter() { }
PGPUInt32 Count() const {return mPItems->Count();}
PGPInt32 MaxIndex() const {return static_cast<PGPInt32>(Count() - 1);}
PGPInt32 Index() const {return mIndex;}
T * Current() const {return mCurItem;}
T * Move(PGPInt32 relOffset, PGPBoolean fromStart = FALSE);
PGPInt32 Seek(const T *pSoughtItem) const;
T * Next();
T * Prev();
void Rewind();
private:
const CList<T> *mPItems;
T *mCurItem;
PGPInt32 mIndex;
T * GoToNewIndex(PGPInt32 newIndex);
};
// Class CIter template member functions
template <typename T>
CIter<T>::CIter(const CList<T> *pItems) :
mPItems(pItems), mCurItem(NULL), mIndex(-1)
{
pgpAssertAddrValid(pItems, CList<T>);
}
template <typename T>
T *
CIter<T>::Move(PGPInt32 relOffset, PGPBoolean fromStart)
{
if (fromStart)
return GoToNewIndex(relOffset);
else
return GoToNewIndex(mIndex + relOffset);
}
template <typename T>
PGPInt32
CIter<T>::Seek(const T *pSoughtItem) const
{
pgpAssertAddrValid(pSoughtItem, T);
T *pItem = mPItems->Head();
PGPInt32 result = -1;
for (PGPInt32 i = 0; i <= MaxIndex(); i++)
{
if (pItem == pSoughtItem)
{
result = i;
break;
}
pItem = mPItems->Next(pItem);
}
return result;
}
template <typename T>
T *
CIter<T>::Next()
{
return GoToNewIndex(mIndex + 1);
}
template <typename T>
T *
CIter<T>::Prev()
{
return GoToNewIndex(mIndex - 1);
}
template <typename T>
void
CIter<T>::Rewind()
{
GoToNewIndex(-1);
}
template <typename T>
T *
CIter<T>::GoToNewIndex(PGPInt32 newIndex)
{
if ((newIndex < -1) || (newIndex > MaxIndex()))
return NULL;
if (newIndex == -1)
{
mCurItem = NULL;
}
else if (newIndex == 0)
{
mCurItem = mPItems->Head();
}
else if (newIndex == MaxIndex())
{
mCurItem = mPItems->Tail();
}
else
{
PGPBoolean trueIfGoingRight = (newIndex > mIndex);
PGPUInt32 diff = abs(newIndex - mIndex);
for (PGPUInt32 i = 0; i < diff; i++)
{
if (trueIfGoingRight)
mCurItem = mPItems->Next(mCurItem);
else
mCurItem = mPItems->Prev(mCurItem);
}
}
mIndex = newIndex;
return mCurItem;
}
_PGP_END
#endif // ] Included_CIter_h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -