Introduction
If you are looking for a high efficient dynamic array, I'm sure this code can be useful. I call it as CSArray. It is a tamplate class of C++. It wraps array operators includeing almost all of interfaces of CArray of MFC and some new features. Besides, it also provides a good sort capability. If you need data keep in sorted, you need not to write sort function any longer but offer a simple data compare callback function. Last but not least, you can tell CSArray how to make use of memory by specify a growup size property.
You can call Add, RemoveAt, etc. While you enjoy these goods, you need not to worry about it may wast memory, because CSArray can make data array neat automaticlly.
The follow image show how I manager data in CSArray. At fist look, someone may doubts it is a normal list. In fact, it combines list and array together, therefore, it has high efficiency like array and high flxibility like list. If we set growup to 1, it is a pure list, and if we set growup large enough, it is a pure array.
Using the code
I'm an Chinese. In demo, I use one of my Chinese dictionary as source data because I have no time to prepare so much test data for it. Its filename is userdef.txt. One can use those functional buttons to understand how to operate CSArray. I use a virtual listctrl to display data.
CSArray supports almost all interfaces of CArray of MFC. If you use CArray in your code, you will find it is vary easy for you to convert CArray to CSArray except for CSArray accept only one template parameter.
Next, I will introduce some new functions.
void SetGrowBy(WORD nGrowBy)
int SortFind(T *pData,FUNCMP funCmp=NULL,LPARAM lParam=0,int nBegin=0,UINT sfType=SFT_ANY)
int SortInsert(T newElement,BOOL bRepeat=TRUE,FUNCMP funCmp=NULL,LPARAM lParam=0,UINT sfType=SFT_ANY)
BOOL Attach(T *pData,int nLen)
T *Detach(int *pnLen)
Warning!!!
CSArray should used for struct data but not class data, because I use memcpy, memmove, etc. to make data neat.
History
2008-9-25:submit