📄 ptrlist.h
字号:
//---------------------------------------------------------------------------
// 名称:PtrList.h
// 功能:CPtrList类的头文件
// 版本:1.0
// 工具: Visual C++ 6.0
// 引入:无
// 引出:无
// 作者:刘健颖
// 时间:2004-05-26
//---------------------------------------------------------------------------
#pragma once
struct __POSITION { };
typedef __POSITION * POSITION;
struct CPlex // warning variable length structure
{
CPlex* pNext;
#ifndef _WIN64
#if (_AFX_PACKING >= 8)
DWORD dwReserved[1]; // align on 8 byte boundary
#endif
#endif
// BYTE data[maxNum*elementSize];
void* data() { return this+1; }
static CPlex* PASCAL Create(CPlex*& head, UINT_PTR nMax, UINT_PTR cbElement);
// like 'calloc' but no zero fill
// may throw memory exceptions
void FreeDataChain(); // free this one and links
};
//---------------------------------------------------------------------------
// CPtrList类的相关信息
//---------------------------------------------------------------------------
class CPtrList
{
// DECLARE_DYNAMIC( CPtrList );
protected:
struct CNode
{
CNode* pNext;
CNode* pPrev;
void* data;
};
public:
// Construction
CPtrList(int nBlockSize = 10);
// Attributes (head and tail)
// count of elements
int GetCount() const
{
return m_nCount;
}
BOOL IsEmpty() const
{
return m_nCount == 0;
}
// peek at head or tail
void*& GetHead();
void* GetHead() const;
void*& GetTail();
void* GetTail() const;
// Operations
// get head or tail (and remove it) - don't call on empty list!
void* RemoveHead();
void* RemoveTail();
// add before head or after tail
POSITION AddHead(void* newElement);
POSITION AddTail(void* newElement);
// add another list of elements before head or after tail
void AddHead(CPtrList* pNewList);
void AddTail(CPtrList* pNewList);
// remove all elements
void RemoveAll();
// iteration
POSITION GetHeadPosition() const;
POSITION GetTailPosition() const;
void* GetNext(POSITION& rPosition); // return *Position++
void* GetNext(POSITION& rPosition) const; // return *Position++
void*& GetPrev(POSITION& rPosition); // return *Position--
void* GetPrev(POSITION& rPosition) const; // return *Position--
// getting/modifying an element at a given position
void*& GetAt(POSITION position);
void* GetAt(POSITION position) const;
void SetAt(POSITION pos, void* newElement);
void RemoveAt(POSITION position);
// inserting before or after a given position
POSITION InsertBefore(POSITION position, void* newElement);
POSITION InsertAfter(POSITION position, void* newElement);
// helper functions (note: O(n) speed)
POSITION Find(void* searchValue, POSITION startAfter = NULL) const;
// defaults to starting at the HEAD
// return NULL if not found
POSITION FindIndex(int nIndex) const;
// get the 'nIndex'th element (may return NULL)
// Implementation
protected:
DWORD m_dwCObjectInstead;
CNode* m_pNodeHead;
CNode* m_pNodeTail;
int m_nCount;
CNode* m_pNodeFree;
struct CPlex* m_pBlocks;
int m_nBlockSize;
CNode* NewNode(CNode*, CNode*);
void FreeNode(CNode*);
public:
~CPtrList();
// local typedefs for class templates
typedef void* BASE_TYPE;
typedef void* BASE_ARG_TYPE;
};
/////////////////////////////////////////////////////////////////////////////
// CTypedPtrList<BASE_CLASS, TYPE>
template<class BASE_CLASS, class TYPE>
class _CTypedPtrList : public BASE_CLASS
{
public:
// Construction
_CTypedPtrList(INT_PTR nBlockSize = 10)
: BASE_CLASS(nBlockSize) { }
// peek at head or tail
TYPE& GetHead()
{ return (TYPE&)BASE_CLASS::GetHead(); }
TYPE GetHead() const
{ return (TYPE)BASE_CLASS::GetHead(); }
TYPE& GetTail()
{ return (TYPE&)BASE_CLASS::GetTail(); }
TYPE GetTail() const
{ return (TYPE)BASE_CLASS::GetTail(); }
// get head or tail (and remove it) - don't call on empty list!
TYPE RemoveHead()
{ return (TYPE)BASE_CLASS::RemoveHead(); }
TYPE RemoveTail()
{ return (TYPE)BASE_CLASS::RemoveTail(); }
// iteration
TYPE& GetNext(POSITION& rPosition)
{ return (TYPE&)BASE_CLASS::GetNext(rPosition); }
TYPE GetNext(POSITION& rPosition) const
{ return (TYPE)BASE_CLASS::GetNext(rPosition); }
TYPE& GetPrev(POSITION& rPosition)
{ return (TYPE&)BASE_CLASS::GetPrev(rPosition); }
TYPE GetPrev(POSITION& rPosition) const
{ return (TYPE)BASE_CLASS::GetPrev(rPosition); }
// getting/modifying an element at a given position
TYPE& GetAt(POSITION position)
{ return (TYPE&)BASE_CLASS::GetAt(position); }
TYPE GetAt(POSITION position) const
{ return (TYPE)BASE_CLASS::GetAt(position); }
void SetAt(POSITION pos, TYPE newElement)
{ BASE_CLASS::SetAt(pos, newElement); }
// inserting before or after a given position
POSITION InsertBefore(POSITION position, TYPE newElement)
{ return BASE_CLASS::InsertBefore(position, newElement); }
POSITION InsertAfter(POSITION position, TYPE newElement)
{ return BASE_CLASS::InsertAfter(position, newElement); }
// transfer before or after a given position
// Transfer semantics ensure no leakage by deleting the element in the case of an exception
POSITION TransferInsertBefore(POSITION position, TYPE newElement)
{
try
{
return BASE_CLASS::InsertBefore(position, newElement);
}
catch(...)
{
delete newElement;
throw;
}
}
POSITION TransferInsertAfter(POSITION position, TYPE newElement)
{
try
{
return BASE_CLASS::InsertAfter(position, newElement);
}
catch(...)
{
delete newElement;
throw;
}
}
};
template<class BASE_CLASS, class TYPE>
class CTypedPtrList : public _CTypedPtrList<BASE_CLASS, TYPE>
{
public:
// Construction
CTypedPtrList(INT_PTR nBlockSize = 10)
: _CTypedPtrList<BASE_CLASS, TYPE>(nBlockSize) { }
// add before head or after tail
POSITION AddHead(TYPE newElement)
{ return BASE_CLASS::AddHead(newElement); }
POSITION AddTail(TYPE newElement)
{ return BASE_CLASS::AddTail(newElement); }
// transfer add before head or tail
POSITION TransferAddHead(TYPE newElement)
{
try
{
return BASE_CLASS::AddHead(newElement);
}
catch(...)
{
delete newElement;
throw;
}
}
POSITION TransferAddTail(TYPE newElement)
{
try
{
return BASE_CLASS::AddTail(newElement);
}
catch(...)
{
delete newElement;
throw;
}
}
// add another list of elements before head or after tail
void AddHead(CTypedPtrList<BASE_CLASS, TYPE>* pNewList)
{ BASE_CLASS::AddHead(pNewList); }
void AddTail(CTypedPtrList<BASE_CLASS, TYPE>* pNewList)
{ BASE_CLASS::AddTail(pNewList); }
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -