📄 coll_list.h
字号:
#ifndef __COLL_LIST__H__
#define __COLL_LIST__H__
#include <time.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <new.h>
#include <stdarg.h>
#include <stdlib.h>
#define MAX_PATH (260)
#define ASSERT(f) ((void)0)
#define VERIFY(f) ((void)(f))
#define CONST const
#define NULL 0
#define TRUE 1
#define FALSE 0
typedef unsigned char BYTE;
typedef unsigned char* LPBYTE;
typedef int INT;
typedef unsigned int UINT;
typedef unsigned long DWORD;
typedef long BOOL;
typedef long LONG;
typedef void VOID;
typedef void* LPVOID;
typedef char CHAR;
typedef const char* LPCSTR;
typedef char* LPSTR;
struct __POSITION { };
typedef __POSITION* POSITION;
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
// 内存管理和分配
// 数组处理
#ifndef ARRAYSIZE
#define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
#define SIZEOF(a) (sizeof(a))
#endif
// 内存处理
#ifndef TMALLOC
inline void* mem_init(void* pMemory,size_t cbSize)
{ if(pMemory!=NULL&&cbSize>0)
memset(pMemory,0,cbSize);
return pMemory;
}
#define TMALLOC(n,T) ((T*)mem_init(malloc(sizeof(T)*(n)),sizeof(T)*(n)))
#endif
#ifndef TFREE
#define TFREE(a) if(a!=NULL) { free(a); a=NULL; }
#endif
#ifndef TDEL
#define TDEL(a) if(a!=NULL) { delete a; a=NULL; }
#endif
#ifndef TDELARRAY
#define TDELARRAY(a) if(a!=NULL) { delete[] a; a=NULL; }
#endif
#ifndef TFCLOSE
#define TFCLOSE(a) if(a!=NULL) { fclose(a); a=NULL; }
#endif
// 字符串处理
#ifndef nsprintf
inline int __nsprintf(char* lpszString,long nSize,const char* lpszFmt,...)
{ va_list args;
va_start(args,lpszFmt);
int nRet=_vsnprintf(lpszString,nSize,lpszFmt,args);
if(nRet<0)
{ lpszString[nSize-1]=0;
nRet=nSize-1;
}
va_end(args);
return nRet;
}
#define nsprintf __nsprintf
#endif
#ifndef nvsprintf
inline int __nvsprintf(char* lpszString,long nSize,const char* lpszFmt,va_list ap)
{ int nRet=_vsnprintf(lpszString,nSize,lpszFmt,ap);
if(nRet<0)
{ lpszString[nSize-1]=0;
nRet=nSize-1;
}
return nRet;
}
#define nvsprintf __nvsprintf
#endif
#ifndef nstrcpy
inline void __nstrcpy(char* lpszString,const char* lpszSrc,long nSize)
{ strncpy(lpszString,lpszSrc,nSize);
lpszString[nSize-1]=0;
}
#define nstrcpy __nstrcpy
#endif
#ifndef nstrcat
inline void __nstrcat(char* lpszFront,const char* lpszBack,long nSize)
{ strncat(lpszFront,lpszBack,nSize);
lpszFront[nSize-1]=0;
}
#define nstrcat __nstrcat
#endif
// 字符串复制
#ifndef COPYSTRARRAY
#define COPYSTRARRAY(a,b) nstrcpy((char*)(a),(b),ARRAYSIZE(a))
#endif
#ifndef CATSTRARRAY
#define CATSTRARRAY(a,b) nstrcat((char*)(a),(b),ARRAYSIZE(a))
#endif
// 缓冲区处理
#ifndef COPYBINARRAY
#define COPYBINARRAY(a,b) memcpy((a),(b),ARRAYSIZE(a))
#endif
// 文件处理
#ifndef FFILELENGTH
#define FFILELENGTH(a) ((fileno(a)>=0)?filelength(fileno(a)):0)
#endif
// 几种特殊类型的构造和析构函数(LPVOID类型)
inline void CollConstructElements(LPVOID* pElements, int nCount)
{ memset((void*)pElements,0,nCount*sizeof(LPVOID));
for(; nCount--; pElements++)
::new((void*)pElements) LPVOID;
}
inline void CollDestructElements(LPVOID* pElements,int nCount)
{ for(; nCount--; pElements++)
pElements->~LPVOID();
}
inline void CollCopyElements(LPVOID* pDest,const LPVOID* pSrc, int nCount)
{ while(nCount--)
{ *pDest++ = *pSrc++;
}
}
inline BOOL CollCompareElements(const LPVOID* pElement1,const LPVOID* pElement2)
{ return *pElement1==*pElement2;
}
// 数据桶结构
typedef struct TBucket
{ TBucket* m_pNext;
//BYTE m_Data[maxNum*elementSize];
static TBucket* Create(TBucket*& pHead,UINT nMax,UINT cbElement);
LPVOID Data() { return this+1; }
VOID FreeDataChain();
} TBUCKET;
// 链表类(通用模板类)
class TListPtr
{
public:
TListPtr(LONG nBlockSize=10);
~TListPtr();
// 个数获取和判空
LONG GetCount() CONST;
BOOL IsEmpty() CONST;
// 头部和尾部获取
LPVOID& GetHead();
LPVOID GetHead() CONST;
LPVOID& GetTail();
LPVOID GetTail() CONST;
// 删除头部对象和尾部对象
LPVOID RemoveHead();
LPVOID RemoveTail();
// 列表项添加
POSITION AddHead(LPVOID newElement);
POSITION AddTail(LPVOID newElement);
// 列表合并
VOID AddHead(TListPtr* pNewList);
VOID AddTail(TListPtr* pNewList);
// 列表项清空
VOID RemoveAll();
// 列表项遍历
POSITION GetHeadPosition() CONST;
POSITION GetTailPosition() CONST;
LPVOID& GetNext(POSITION& rPosition);
LPVOID GetNext(POSITION& rPosition) CONST;
LPVOID& GetPrev(POSITION& rPosition);
LPVOID GetPrev(POSITION& rPosition) CONST;
// 列表项获取和设置
LPVOID& GetAt(POSITION position);
LPVOID GetAt(POSITION position) CONST;
VOID SetAt(POSITION pos,LPVOID newElement);
// 列表删除
VOID RemoveAt(POSITION position);
// 列表插入
POSITION InsertBefore(POSITION position,LPVOID newElement);
POSITION InsertAfter(POSITION position,LPVOID newElement);
// 对象查找
POSITION Find(LPVOID searchValue,POSITION startAfter=NULL) CONST;
POSITION FindIndex(LONG nIndex) CONST;
protected:
struct CNode
{ CNode* pNext;
CNode* pPrev;
LPVOID data;
};
protected:
CNode* NewNode(CNode*,CNode*);
VOID FreeNode(CNode*);
protected:
CNode* m_pNodeHead;
CNode* m_pNodeTail;
LONG m_nCount;
CNode* m_pNodeFree;
TBUCKET* m_pBlocks;
LONG m_nBlockSize;
};
//////////////////////////////////////////////////////////////////////////
// TListPtr:内联实现
//////////////////////////////////////////////////////////////////////////
inline LONG TListPtr::GetCount() CONST
{ return m_nCount; }
inline BOOL TListPtr::IsEmpty() CONST
{ return m_nCount==0; }
inline LPVOID& TListPtr::GetHead()
{ ASSERT(m_pNodeHead!=NULL);
return m_pNodeHead->data;
}
inline LPVOID TListPtr::GetHead() CONST
{ ASSERT(m_pNodeHead!=NULL);
return m_pNodeHead->data;
}
inline LPVOID& TListPtr::GetTail()
{ ASSERT(m_pNodeTail!=NULL);
return m_pNodeTail->data;
}
inline LPVOID TListPtr::GetTail() CONST
{ ASSERT(m_pNodeTail!=NULL);
return m_pNodeTail->data;
}
inline POSITION TListPtr::GetHeadPosition() CONST
{ return (POSITION) m_pNodeHead; }
inline POSITION TListPtr::GetTailPosition() CONST
{ return (POSITION) m_pNodeTail; }
inline LPVOID& TListPtr::GetNext(POSITION& rPosition)
{ CNode* pNode=(CNode*) rPosition;
ASSERT(ClibIsValidAddress(pNode,sizeof(CNode)));
rPosition=(POSITION) pNode->pNext;
return pNode->data;
}
inline LPVOID TListPtr::GetNext(POSITION& rPosition) CONST
{ CNode* pNode=(CNode*) rPosition;
ASSERT(ClibIsValidAddress(pNode,sizeof(CNode)));
rPosition=(POSITION) pNode->pNext;
return pNode->data;
}
inline LPVOID& TListPtr::GetPrev(POSITION& rPosition)
{ CNode* pNode=(CNode*) rPosition;
ASSERT(ClibIsValidAddress(pNode,sizeof(CNode)));
rPosition=(POSITION) pNode->pPrev;
return pNode->data;
}
inline LPVOID TListPtr::GetPrev(POSITION& rPosition) CONST
{ CNode* pNode=(CNode*) rPosition;
ASSERT(ClibIsValidAddress(pNode,sizeof(CNode)));
rPosition=(POSITION) pNode->pPrev;
return pNode->data;
}
inline LPVOID& TListPtr::GetAt(POSITION position)
{ CNode* pNode=(CNode*) position;
ASSERT(ClibIsValidAddress(pNode,sizeof(CNode)));
return pNode->data;
}
inline LPVOID TListPtr::GetAt(POSITION position) CONST
{ CNode* pNode=(CNode*) position;
ASSERT(ClibIsValidAddress(pNode,sizeof(CNode)));
return pNode->data;
}
inline VOID TListPtr::SetAt(POSITION pos,LPVOID newElement)
{ CNode* pNode=(CNode*) pos;
ASSERT(ClibIsValidAddress(pNode,sizeof(CNode)));
pNode->data=newElement;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -