August 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Mastering Symbian OS Arrays

  • January 14, 2008
  • By Alex Gusev
  • Send Email »
  • More Articles »

Typical Dynamic Array Operations

Now, you get to the practical stuff. This section provides several examples of the dynamic arrays usage. The SDK contains good and extensive examples for various types of arrays, dynamic and static, that show how to exercise typical operations, like adding new elements and so forth. What the SDK samples do not illustrate so much is how you sort the arrays, insert some elements in sorted order, and similar things. So, you will focus on this instead of duplicating the documentation.

Start with CArrays. You would want to store in them the T-classes like data or descriptors. CArrayFixBase and CArrayVarBase have Sort(), InsertIsqL(), Find(), and FindIsqL() member functions that require a TKeyArrayFix object as an argument to define the location and type of key within an array element. Those keys can represent descriptor, text, or numeric objects.

class TElement2Sort
   {
public :
   TElement2Sort()
      {
      }
public :
   TBuf<16> iBuffer;
   };

_LIT(KElem1,"fiRst");
_LIT(KElem2,"second");
_LIT(KElem3,"Third");
_LIT(KFmt,"%S\n");

LOCAL_C void MainL ()
   {

   CArrayFixFlat<TElement2Sort>* arr =
      new (ELeave) CArrayFixFlat<TElement2Sort>(3);

   TElement2Sort e1;
   e1.iBuffer.Copy(KElem1);
   TElement2Sort e2;
   e2.iBuffer.Copy(KElem2);
   TElement2Sort e3;
   e3.iBuffer.Copy(KElem3);

   arr->AppendL(e3);
   arr->AppendL(e1);
   arr->AppendL(e2);

   TKeyArrayFix key(_FOFF(TElement2Sort, iBuffer), ECmpFolded);
   Tint res = arr->Sort(key);

   for (int i = 0; i < 3; i++)
      {
         TElement2Sort e = arr->At(i);
         console->Printf(KFmt,&(e.iBuffer));
      }
   delete arr;
   }

You can insert the elements to the array in required order via an InsertIsqL() method that takes the TKeyArrayXXX parameter. Be aware that the array has to be already in the same order as the key parameter defines. You can modify the above sample to use InsertIsq() as follows:

LOCAL_C void MainL ()
   {

   CArrayFixFlat<TElement2Sort>* arr =
      new (ELeave) CArrayFixFlat<TElement2Sort>(3);

   TElement2Sort e1;
   e1.iBuffer.Copy(KElem1);
   TElement2Sort e2;
   e2.iBuffer.Copy(KElem2);
   TElement2Sort e3;
   e3.iBuffer.Copy(KElem3);

   TKeyArrayFix key(_FOFF(TElement2Sort, iBuffer), ECmpFolded);

   arr->InsertIsqL(e3,key);
   arr->InsertIsqL(e1,key);
   arr->InsertIsqL(e2,key);

   for (int i = 0; i < 3; i++)
      {
         TElement2Sort e = arr->At(i);
         console->Printf(KFmt,&(e.iBuffer));
      }
   delete arr;
   }

TLinearOrder class acts in similar way for R-Arrays. Let's see how we can sort the array of CPerson objects in the following sample which uses RPointerArray:

class CPerson : public CBase
   {
public:
   static CPerson* NewLC(Tint aId,
                         const TDesC& aFirstName,
                         const TDesC& aLastName );

   static Tint CompareByLastName(
                         const CPerson& person1,
                         const CPerson& person2);
   const TDesC& LastName();

protected:
   CPerson(Tint aId,
           const TDesC& aFirstName,
           const TDesC& aLastName);
private:
   Tint iId;
   TBuf<64> iFirstName;
   TBuf<64> iLastName;
   };

Tint CPerson::CompareByLastName( const CPerson& person1,
const CPerson& person2 )
    {
   return person1.LastName().CompareC(person2.LastName());
   }

LOCAL_C void MainL ()
   {

   RPointerArray<CPerson> people;
   CleanupClosePushL(people);

   CPerson* one = CPerson::NewLC(1, _L("John"), _L("TheFirst"));
   CPerson* two = CPerson::NewLC(3, _L("Thomas"), _L("McSecond"));
   CPerson* three = CPerson::NewLC(2, _L("Sean"), _L("O'Third"));

   people.AppendL(one);
   people.AppendL(two);
   people.AppendL(three);

   TLinearOrder<CPerson>
      sortByLastName(CPerson::CompareByLastName);
   people.Sort(sortByLastName);

   _LIT(KFmt,"%S\n");
   for (int i = 0; i < 3; i++)
      {
         CPerson* p = people[i];
         console->Printf(KFmt,&(p->LastName()));
      }

   CleanupStack::Pop(4);
   people.ResetAndDestroy();
   people.Close();

   }

You may exercise the rest of array's methods on your own, consulting the SDK documentation and samples where necessary. I hope this article helped you better understand the arrays on Symbian OS.

Conclusion

In this article, you have learned a few general topics to help you get started quicklyst with Symbian OS arrays and find your way among a crowd of different sorts and types of them. You should be able to choose suitable array class for your needs now. With more practice and a 'change and test' approach, you will definitely find a lot more quite soon. Enjoy!

About the Author

Alex Gusev started to play with mainframes at the end of the 1980s, using Pascal and REXX, but soon switched to C/C++ and Java on different platforms. When mobile PDAs seriously rose their heads in the IT market, Alex did it too. After working almost a decade for an international retail software company as a team leader of the Windows Mobile R department, he has decided to dive into Symbian OS ™ Core development.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel