Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

High Efficient Dynamic Array and a Simple Demo

2.33/5 (8 votes)
24 Sep 2008CPOL2 min read 1   348  
Help you manager dynamic array
sarrayDemo

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.
node structure

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)
//function: tell how much rooms should be allocated when there is no empty room for new element.
//remark: the function should be call at the begin of use CSArray or after you have called RemoveAll. 
//In one word, before call it, make sure that CSArray is empty.

 int SortFind(T *pData,FUNCMP funCmp=NULL,LPARAM lParam=0,int nBegin=0,UINT sfType=SFT_ANY)
//function: find element in a sorted array.
//param
//T *pData[in]:specify what element you try to find.
//FUNCMP funCmp[in]:callback data compare function. If it is NULL, CSArray use memcmp to replace.
//LPARFAM lParam[in]:a user defined 32bits data, used in funCmp.
//int nBegin[in]:tell CSArray to start search from here.
//UINT sfType[in]:tell CSArray how to do while there are several elements which satisfy the conditions you offered.
//SFT_ANY will return any equal element, SFT_HEAR will return the first matched element 
//and SFT_TAIL will return the last matched element.
//return: a found element index. -1 is no element found.

int SortInsert(T newElement,BOOL bRepeat=TRUE,FUNCMP funCmp=NULL,LPARAM lParam=0,UINT sfType=SFT_ANY)
//function:insert an element to array and keep array sorted.
//T newElement[in]: target element to be inserted.
//BOOL bRepeat[in]: indicate whether compared equal elment should be inserted. 
//If false and if find a equal element, function return -1.
//FUNCMP funCmp,LPARAM lParam,UINT sfType[in]:same as SortFind.
//return: new inserted element index. -1 is not inserted.
//remark:sfType tell CSArray how to insert a compared equal element.
  
BOOL Attach(T *pData,int nLen)
//function: attach an external data block to CSArray.
 T *Detach(int *pnLen)
//function: remove external data from CSArray.
//remark: sometime we have a sorted data, and we need to index an element quickly, 
//we can simply hand data to CSArray. If doing so, for most case, 
//try not to call interfaces such as "add","removeat",etc.

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)