dynamicarray: Using Dynamic Arrays

This example application demonstrates how to construct and use dynamic arrays.

The example includes both simple dynamic arrays and extended dynamic arrays. You are likely to use simple dynamic arrays often. Extended dynamic arrays are combinations of element properties and storage types.

The example implements the following operations for the RArray and RPointerArray classes:

  • Appends elements

  • Inserts elements

  • Accesses elements

  • Removes elements

  • Finds elements (in various ways)

  • Sorts array

  • Destroys the array

Download

Click on the following link to download the example: DynamicArrays.zip

Download some additional files required by the example: CommonFramework.zip

View the source code: browse . View the additional files:browse.

Description

This example demonstrates how to construct and use the following types of dynamic arrays:

CArrayFixFlat

This example uses the CArrayFixFlat array class to create an array of objects. These objects are of fixed length and are stored in a flat buffer.

The following operations are performed on this array:

  • Append elements: The example uses the CArrayFix::AppendL() function to append objects to the array.

  • Insert elements: The example uses the CArrayFix::InsertL() function to insert objects into the array at the specified position.

  • Remove elements: The example uses the CArrayFixBase::Delete() function to delete objects from the array.

  • Sort elements: The example defines an array key to sort the array using the TKeyArrayFix class. The CArrayFixBase::Sort() function is used to sort the array using this array key.

  • Swap two elements: The example implements the TSwap::Swap() function through the CMySwap class. The CMySwap::Swap() function is used to swap two elements of the array.

  • Find an element: The example implements the TKeyElementFix class. This class is a TKey derived class. It overrides the TKey::At() function. The TKeyElementFix::At() function gets a pointer to the key of a specified array element. This function is used by the CArrayFixFlat::Find() function to find an element in the array.

CArrayPtrFlat

This example uses the CArrayPtrFlat array class to create an array of pointers to CBase derived objects.

The following operations are performed on this array:

  • Append elements: The example uses the CArrayFix::AppendL() function to append objects to the array.

  • Insert elements: The example uses the CArrayFix::InsertL() function to insert objects into the array at the specified position.

  • Remove elements: The example uses the CArrayFixBase::Delete() function to delete objects from the array.

CArrayVarFlat

This example uses the CArrayVarFlat array class to create an array of objects. These objects are of different lengths. Pointers to these elements are maintained in a flat dynamic buffer.

The following operations are performed on this array:

  • Append elements: The example uses the CArrayVar::AppendL() function to append objects to the array.

  • Sort elements: The example defines an array key to sort the array using the TKeyArrayVar class. The CArrayVarBase::Sort() function is used to sort the array using this array key.

CArrayPakFlat

This example uses the CArrayPakFlat array class to create an array of objects. These objects are of variable lengths. These are stored in a flat buffer. Each element is preceded by its length information.

The following operations are performed on this array:

  • Append elements: The example uses the CArrayPak::AppendL() function to append objects to the array.

  • Find an element: The example defines an array key to find an element in the array using the TKeyArrayPak class. The CArrayPakBase::Find() function is used to find an element in the array.

RArray

This example constructs a simple dynamic array called RArray. It is an efficient array of fixed length objects. The elements of this array are the instances of another class, so this is specified as a template parameter T.

This example shows the following operations to this array:

  • Append elements: The example performs the following steps:

    • Appends the objects into the array using RArray::AppendL().

    • Displays the number of objects and prints each object present in the array.

  • Insert elements: The example performs the following steps:

    • Inserts the objects at a specified position using RArray::InsertL().

    • Passes the position of the object to the second parameter of RArray::InsertL().

    • Inserts objects in the beginning, middle, and at the end of the array.

    • Displays the number of objects and prints each object present in the array.

  • Access elements: The example accesses each object in the array and display into the console.

  • Remove elements: The example removes an object at a specific position from the array using RArray::Remove().

  • Find elements: The example performs the following steps:

    • Finds the first object in the array that matches the specified object using RArray::Find().

    • Finds the last object in the array that matches the specified object using RArray::FindReverse().

    • Finds the object using a binary search technique in the array, which matches the specified object using RArray::FindInSignedKeyOrder(). It assumes that the existing objects within the array are in signed key order.

    • Finds the object using a binary search technique in the array, which matches the specified object using RArray::FindInUnsignedKeyOrder(). It assumes that the existing objects within the array are in unsigned key order.

    • Finds the object using a binary search technique in the array, which matches the specified object using RArray::SpecificFindInSignedKeyOrder(). It passes the mode as the second parameter of this API to find the first, last, or any match in case of multiple matches. It assumes that the existing objects within the array are in signed key order.

    • Finds the object using a binary search technique in the array, which matches the specified object using RArray::SpecificFindInUnsignedKeyOrder(). It passes the mode as the second parameter of this API to find the first, last, or any match in case of multiple matches. It assumes that the existing objects within the array are in unsigned key order.

  • Sort elements: The example performs the following steps:

    • Constructs two arrays of 64-bit signed integer type objects.

    • Appends random objects to the first array using RArray::Append().

    • Inserts the data present in the first array to the second array.

    • Sorts the element of second array using RArray::sort().

  • Destroy the array: The example performs the following steps:

    • Destroys the array using delete operator.

    • Closes the array and removes all the memory allocated to the array using RArray::Close().

RPointerArray

This example constructs a simple dynamic array of pointers to objects called RPointerArray. It is an efficient pointer array of fixed-length objects. The elements of this array are the instances of another class so this is specified as a template parameter T.

This example shows the following operations to this array:

  • Append elements: The example performs the following steps:
    • Appends the object pointers into the array using RPointerArray::AppendL().

    • Displays the number of object pointers and prints each object pointer present in the array.

  • Insert elements: The example performs the following steps:

    • Inserts the objects pointer at a specified position using RpointerArray::InsertL().

    • Passes the position of the object pointer to the second parameter of RpointerArray::InsertL().

    • Inserts object pointer at the beginning, in the middle, and at the end of the array.

    • Displays the number of object pointers and prints each object pointer present in the array.

  • Access elements: The example accesses each object pointer in the array and displays it to the console.

  • Remove elements: The example removes an object pointer at a specific position from the array using RPointerArray::Remove().

  • Find elements: The example performs the following steps:

  • Sort elements: The example performs the following steps:

    • Constructs two arrays of TAny pointer objects.

    • Appends random objects to the first pointer array using RPointerArray::Append().

    • Inserts the data present in the first pointer array to the second pointer array.

    • Sorts the element of the second pointer array using RPointerArray::sort().

  • Destroy the array: The example performs the following steps:

Build

The Symbian build process describes how to build this example.

For the emulator, the example builds an executable file called DynamicArrays.exe in the standard location (\epoc32\release\winscw\<build_variant> for CodeWarrior) directory. After launching the executable file, depending on the emulator you are using, you can task away from the application launcher or shell screen to view the console.