Click here to Skip to main content
Licence CPOL
First Posted 24 Sep 2008
Views 12,040
Downloads 123
Bookmarked 10 times

High Efficient Dynamic Array and a Simple Demo

By | 24 Sep 2008 | Article
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)

About the Author

flyhigh

Software Developer (Senior)

China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiondo you compare the efficiency between csarray and carray? Pinmemberszcathay5:11 17 Feb '12  
AnswerRe: do you compare the efficiency between csarray and carray? PinmemberMember 799901418:01 19 Feb '12  
QuestionCan you please use unicode? PinmemberThe Idiot2:46 29 Sep '08  
AnswerRe: Can you please use unicode? Pinmemberflyhigh15:20 29 Sep '08  
GeneralRe: Can you please use unicode? PinmemberThe Idiot16:22 29 Sep '08  
GeneralRe: Can you please use unicode? Pinmemberflyhigh20:47 29 Sep '08  
GeneralI'm getting old PinPopularmemberNGS 5496726:49 25 Sep '08  
GeneralRe: I'm getting old Pinmemberrwagnon8:34 25 Sep '08  
GeneralRe: I'm getting old Pinmemberflyhigh14:23 25 Sep '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 25 Sep 2008
Article Copyright 2008 by flyhigh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid