Array
Auxiliary Structure and Function
Data structure and function in this section will be used in our example code. Make sure you have included <Origin.h> and <Array.h> before running these examples.
#include <Origin.h>
#include <Array.h>
- Structure used to hold data in our example code.
class POINTEX
{
public:
POINTEX(int n)
{
m_n = n;
printf("Constructor\n");
}
~POINTEX()
{
printf("Destructor, n = %d\n", m_n);
}
int m_n;
};
- This function simply set the size of the array as 3 with values {1,2,3}
static void _perpare_array(Array<POINTEX&> &myArray)
{
// Add elements to the array.
int ii;
int nSize = 3;
for(ii = 0; ii < nSize; ii++)
{
POINTEX *pp = new POINTEX(ii+1);
myArray.Add(*pp);
}
}
Access data objects
Append or set a object
This example show how to add objects to the array
//Method 1:
// add at the end of the array
void Array_add_item_at_end()
{
Array<POINTEX&> myArray;
myArray.SetAsOwner(true);
_perpare_array(myArray);
POINTEX *pObj = new POINTEX(211);
myArray.Add(*pObj);
}
//Method 2:
// add at any place, might replace other object if the place is occupied already
void Array_set_or_add_one_item( int nToSet = 0 )
{
Array<POINTEX&> myArray;
myArray.SetAsOwner(true);
_perpare_array(myArray);
POINTEX *pObj = new POINTEX( 200 );
myArray.SetAtGrow( nToSet, *pObj );//if nToSet is bigger than 2, Array will resize itself as to hold newly added object
}
Get and set an object
This example show how to get an object and modify its value
//Method 1:
// get object reference by Array::GetAt(int nIndex) and then modify this reference
void Array_modify_one_item_1(int nToModify = 0)
{
Array<POINTEX&> myArray;
myArray.SetAsOwner(true);
_perpare_array(myArray);
POINTEX& obj = myArray.GetAt(nToModify);
if( obj )
obj.m_n = 100;
}
//Method 2:
// create a new object and set it to Array to replace old item, Array::SetAt will destruct old object firstly whatever Set Owner TRUE or FALSE
void Array_modify_one_item_2(int nToModify = 0)
{
Array<POINTEX&> myArray;
myArray.SetAsOwner(true);
_perpare_array(myArray);
POINTEX *pNew = new POINTEX(100);
myArray.SetAt(nToModify, *pNew);
}
Get and set size
This example show how to get and set the size of the array
//////////////// Get the size of this list ////////////////
void Array_get_size_of_the_array()
{
Array<POINTEX&> myArray;
myArray.SetAsOwner(true);
_perpare_array(myArray);
int nSize = myArray.GetSize();
ASSERT( 3 == nSize );
POINTEX *pObj = new POINTEX(20);
myArray.Add( *pObj );
nSize = myArray.GetSize();//nSize should be 4
ASSERT( 4 == nSize );
POINTEX *pObjTwo = new POINTEX(22);
myArray.SetAtGrow( 8, *pObjTwo );
nSize = myArray.GetSize();//nSize should be 9 since we added an object to the place indexed as 8 and array resize itself as 9
ASSERT( 9 == nSize );
}
//////////////// Set the size of this list ////////////////
void Array_set_size_of_the_array()
{
Array<POINTEX&> myArray;
myArray.SetAsOwner(true);
_perpare_array(myArray);
myArray.SetSize(8);// following code myArray.GetSize() should return 8 after set size here
ASSERT( 8 == myArray.GetSize() );
myArray.SetSize(2);// myArray.GetSize() should return 2 following this line
ASSERT( 2 == myArray.GetSize() );
}
Storage relative
If set array as the owner, objects in it will be deleted when destructor of Array is called or on reducing the size of the array
//////////////// Set the list as the owner of objects' memory ////////////////
void Array_set_as_owner()
{
Array<POINTEX&> myArray;
myArray.SetAsOwner(true);
_perpare_array(myArray);
BOOL bOwner = TRUE;
myArray.SetAsOwner( bOwner );//following myArray.IsOwner() should return true if this function is called successfully
ASSERT( myArray.IsOwner() );
bOwner = FALSE;
myArray.SetAsOwner( bOwner );//following myArray.IsOwner() should return false;
ASSERT( !myArray.IsOwner() );
}
|