Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / MFC
Article

High Efficient Dynamic Array and a Simple Demo

Rate me:
Please Sign up or sign in to vote.
2.33/5 (8 votes)
24 Sep 2008CPOL2 min read 32.8K   349   13   9
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)


Written By
Software Developer (Junior)
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiondo you compare the efficiency between csarray and carray? Pin
szcathay17-Feb-12 5:11
szcathay17-Feb-12 5:11 
AnswerRe: do you compare the efficiency between csarray and carray? Pin
Member 799901419-Feb-12 18:01
Member 799901419-Feb-12 18:01 
QuestionCan you please use unicode? Pin
Shao Voon Wong29-Sep-08 2:46
mvaShao Voon Wong29-Sep-08 2:46 
AnswerRe: Can you please use unicode? Pin
flyhigh29-Sep-08 15:20
professionalflyhigh29-Sep-08 15:20 
GeneralRe: Can you please use unicode? Pin
Shao Voon Wong29-Sep-08 16:22
mvaShao Voon Wong29-Sep-08 16:22 
GeneralRe: Can you please use unicode? Pin
flyhigh29-Sep-08 20:47
professionalflyhigh29-Sep-08 20:47 
GeneralI'm getting old PinPopular
NGS 54967225-Sep-08 6:49
NGS 54967225-Sep-08 6:49 
GeneralRe: I'm getting old Pin
rwagnon25-Sep-08 8:34
rwagnon25-Sep-08 8:34 
ya this is just a linked list? Am I missing something?
GeneralRe: I'm getting old Pin
flyhigh25-Sep-08 14:23
professionalflyhigh25-Sep-08 14:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.