Click here to Skip to main content
13,590,511 members
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 24 Sep 2008
Licenced CPOL

High Efficient Dynamic Array and a Simple Demo

, 24 Sep 2008
Rate this:
Please Sign up or sign in to vote.
Help you manager dynamic array


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.
//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.


CSArray should used for struct data but not class data, because I use memcpy, memmove, etc. to make data neat.




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


About the Author

Software Developer (Junior)
China China
No Biography provided

You may also be interested in...


Comments and Discussions

Questiondo you compare the efficiency between csarray and carray? Pin
szcathay17-Feb-12 5:11
memberszcathay17-Feb-12 5:11 
AnswerRe: do you compare the efficiency between csarray and carray? Pin
Member 799901419-Feb-12 18:01
memberMember 799901419-Feb-12 18:01 
QuestionCan you please use unicode? Pin
The Idiot29-Sep-08 2:46
memberThe Idiot29-Sep-08 2:46 
AnswerRe: Can you please use unicode? Pin
flyhigh29-Sep-08 15:20
memberflyhigh29-Sep-08 15:20 
GeneralRe: Can you please use unicode? Pin
The Idiot29-Sep-08 16:22
memberThe Idiot29-Sep-08 16:22 
GeneralRe: Can you please use unicode? Pin
flyhigh29-Sep-08 20:47
memberflyhigh29-Sep-08 20:47 
GeneralI'm getting old Pin
NGS 54967225-Sep-08 6:49
memberNGS 54967225-Sep-08 6:49 
GeneralRe: I'm getting old Pin
rwagnon25-Sep-08 8:34
memberrwagnon25-Sep-08 8:34 
GeneralRe: I'm getting old Pin
flyhigh25-Sep-08 14:23
memberflyhigh25-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180618.1 | Last Updated 25 Sep 2008
Article Copyright 2008 by flyhigh
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid