00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "CommonFramework.h"
00018 #include <e32math.h>
00019 #include <e32std.h>
00020
00021 _LIT(KMsgNewLine,"\n");
00022 _LIT(KMsgPressAnyKey," (press any key to continue)\n");
00023 _LIT(KFormat1,"X%u");
00024
00025 const TText* firstElement = _S("DynamicArray: first");
00026 const TText* secondElement = _S("Example Code: second");
00027 const TText* thirdElement = _S("ArrayKeys Ex: third");
00028
00029
00030
00031
00032
00033 class TElement
00034 {
00035 public :
00036 TElement();
00037 public :
00038 TBuf<4> iData;
00039 };
00040
00041 TElement::TElement()
00042 {
00043 _LIT(KTextBase,"BASE");
00044 iData = KTextBase;
00045 }
00046
00047 class CArrayElement
00048 {
00049 public :
00050 CArrayElement();
00051 TBool operator==(const CArrayElement& aElement) const;
00052 public :
00053 TBuf<4> iData;
00054 };
00055
00056 CArrayElement::CArrayElement()
00057 {
00058 _LIT(KTextBase,"BASE");
00059 iData = KTextBase;
00060 }
00061
00062 TBool CArrayElement::operator==(const CArrayElement& aElement) const
00063 {
00064 return (iData == aElement.iData);
00065 }
00066
00067
00068
00069
00070
00071 class CElement : public CBase
00072 {
00073 public :
00074 CElement();
00075 ~CElement();
00076 void SetTextL(const TDesC& aText);
00077 public :
00078 HBufC* iBuf;
00079 };
00080
00081 CElement::CElement()
00082 {
00083 }
00084
00085 CElement::~CElement()
00086 {
00087 delete iBuf;
00088 }
00089
00094 void CElement::SetTextL(const TDesC& aText)
00095 {
00096 if (!iBuf)
00097 {
00098
00099
00100
00101 iBuf = HBufC::NewL(aText.Length());
00102 }
00103
00104 else if(iBuf->Length() != aText.Length() )
00105 {
00106
00107 iBuf = iBuf->ReAllocL(aText.Length());
00108 }
00109
00110 *iBuf = aText;
00111 }
00112
00113 TInt Order64Bit(const TInt64& aLeft, const TInt64& aRight)
00114 {
00115 if (aLeft<aRight)
00116 return -1;
00117 if (aLeft==aRight)
00118 return 0;
00119 return 1;
00120 }
00121
00128 class TIntPtrElement
00129 {
00130 public :
00131 TIntPtrElement();
00132 void SetIntPtr(TInt* aPtr);
00133 public :
00134 TBuf<4> iData;
00139 TInt* iIntPtr;
00140 };
00141
00145 TIntPtrElement::TIntPtrElement()
00146 {
00147 _LIT(KTextBase,"BASE");
00148 iData = KTextBase;
00149 }
00150
00155 void TIntPtrElement::SetIntPtr(TInt* aPtr)
00156 {
00157 iIntPtr = aPtr;
00158 }
00166 class TKeyTIntPtr : public TKeyArrayFix
00167 {
00168 public:
00169 TKeyTIntPtr(TInt aOffset, TKeyCmpText aType);
00170 TKeyTIntPtr(TInt aOffset, TKeyCmpNumeric aType);
00171 TAny* At(TInt aIndex);
00172 };
00173
00179 TKeyTIntPtr::TKeyTIntPtr(TInt aOffset, TKeyCmpText aType):TKeyArrayFix(aOffset,aType)
00180 {
00181 }
00182
00187 TKeyTIntPtr::TKeyTIntPtr(TInt aOffset, TKeyCmpNumeric aType):TKeyArrayFix(aOffset,aType)
00188 {
00189 }
00190
00197 TAny* TKeyTIntPtr::At(TInt aIndex)
00198 {
00199 if (aIndex==KIndexPtr)
00200 {
00201 return((TUint8 *)iPtr+iKeyOffset);
00202 }
00203
00204 TIntPtrElement* ptr = (TIntPtrElement *)(iBase->Ptr(aIndex*sizeof(TIntPtrElement)).Ptr()+iKeyOffset);
00205
00206
00207 return(ptr->iIntPtr);
00208 }
00209
00214 void PrintFixIntPtrArray(CArrayFixFlat<TIntPtrElement>& aFixFlat)
00215 {
00216 _LIT(KMsgContentsIntPtr,"Contents of the array of TIntPtrElement objects.\n");
00217 console->Printf(KMsgContentsIntPtr);
00218 for (TInt forix = 0; forix < aFixFlat.Count(); forix++)
00219 {
00220 console->Printf(KMsgNewLine);
00221 _LIT(KMsgBuf,"Index: %d\n Descriptor:");
00222 console->Printf(KMsgBuf,forix);
00223 console->Printf(aFixFlat[forix].iData);
00224 _LIT(KMsgInt,"\t\tInteger Value: %d");
00225 console->Printf(KMsgInt,*(aFixFlat[forix].iIntPtr));
00226 }
00227 console->Printf(KMsgNewLine);
00228 }
00229
00234 void PrintFixArray(CArrayFixFlat<TElement>& aFixFlat)
00235 {
00236 for (TInt forix = 0; forix < aFixFlat.Count(); forix++)
00237 {
00238 console->Printf(KMsgNewLine);
00239 console->Printf(aFixFlat[forix].iData);
00240 }
00241 console->Printf(KMsgNewLine);
00242 }
00243
00248 void PrintPtrArray(CArrayPtrFlat<CElement>* aPtrFlat)
00249 {
00250 _LIT(KCommonFormat4,"\n%S");
00251 for (TInt forix = 0; forix < aPtrFlat->Count(); forix++)
00252 {
00253 console->Printf(KCommonFormat4,((*aPtrFlat)[forix])->iBuf);
00254 }
00255 }
00256
00260 class TMySwap : public TSwap
00261 {
00262 public:
00263 TMySwap(CArrayFixFlat<TElement>* aArray);
00264 void Swap(TInt aLeft,TInt aRight);
00265 private:
00266
00267 CArrayFixFlat<TElement>* iArray;
00268 };
00269
00273 TMySwap::TMySwap(CArrayFixFlat<TElement>* aArray):iArray(aArray)
00274 {
00275 }
00276
00282 void TMySwap::Swap(TInt aLeft,TInt aRight)
00283 {
00284 TBuf<4> tbuf(iArray->At(aLeft).iData);
00285 iArray->At(aLeft).iData.Copy(iArray->At(aRight).iData);
00286 iArray->At(aRight).iData.Copy(tbuf);
00287 }
00288
00294 void DemoPakArrayFindL(CArrayPak<TText>& aPakFlat)
00295 {
00296
00297
00298
00299
00300
00301 aPakFlat.AppendL(*firstElement,(User::StringLength(firstElement)+1)*sizeof(TText));
00302
00303
00304 aPakFlat.AppendL(*secondElement,(User::StringLength(secondElement)+1)*sizeof(TText));
00305
00306
00307 aPakFlat.AppendL(*thirdElement,(User::StringLength(thirdElement)+1)*sizeof(TText));
00308
00309
00310 _LIT(KCommonFormat7,"Array Contents");
00311 console->Printf(KCommonFormat7);
00312 console->Printf(KMsgPressAnyKey);
00313 console->Getch();
00314
00315 TInt length = aPakFlat.Count();
00316 for(TInt ix = 0; ix < length; ix++)
00317 {
00318 console->Printf(TPtrC(&aPakFlat[ix]));
00319 console->Printf(KMsgNewLine);
00320 }
00321
00322
00323 _LIT(KMsgFind,"\n--->Find");
00324 console->Printf(KMsgFind);
00325 _LIT(KMsgFindElement,"\nSearching the array for:");
00326 console->Printf(KMsgFindElement);
00327
00328
00329
00330 console->Printf(TPtrC(thirdElement));
00331
00332
00333
00334
00335 TKeyArrayPak keypak(0,ECmpNormal,12);
00336
00337
00338 TInt index = -1;
00339
00340
00341 if(aPakFlat.Find(*thirdElement,keypak,index) == 0)
00342 {
00343
00344
00345 _LIT(KElementFound,"\nElement found at %d\n");
00346 console->Printf(KElementFound,index);
00347 }
00348 else
00349 {
00350
00351
00352 _LIT(KElementNotFound,"\nElement not found");
00353 console->Printf(KElementNotFound);
00354 }
00355 }
00356
00362 void DemoVarArraySortL(CArrayVar<TText>& aVarFlat)
00363 {
00364
00365
00366
00367
00368
00369 aVarFlat.AppendL(*firstElement,(User::StringLength(firstElement)+1)*sizeof(TText));
00370
00371
00372 aVarFlat.AppendL(*secondElement,(User::StringLength(secondElement)+1)*sizeof(TText));
00373 aVarFlat.AppendL(*thirdElement,(User::StringLength(thirdElement)+1)*sizeof(TText));
00374
00375
00376 _LIT(KCommonFormat6,"Array Contents [Before Sort]");
00377 console->Printf(KCommonFormat6);
00378
00379 console->Printf(KMsgPressAnyKey);
00380 console->Getch();
00381
00382 TInt length = aVarFlat.Count();
00383 for(TInt ix = 0; ix < length; ix++)
00384 {
00385 console->Printf(TPtrC(&aVarFlat[ix]));
00386 console->Printf(KMsgNewLine);
00387 }
00388
00389
00390
00391
00392 TKeyArrayVar keyvar(0,ECmpFolded,12);
00393
00394
00395 aVarFlat.Sort(keyvar);
00396
00397
00398 _LIT(KMsgSort,"\n--->Sort");
00399 console->Printf(KMsgSort);
00400 console->Printf(KMsgPressAnyKey);
00401 console->Getch();
00402
00403 for(TInt ix = 0; ix < length; ix++)
00404 {
00405 console->Printf(TPtrC(&aVarFlat[ix]));
00406 console->Printf(KMsgNewLine);
00407 }
00408 }
00413 void DemoFlatArrayL(CArrayFixFlat<TElement>& aFixFlat)
00414 {
00415 TElement theElement;
00416
00417 TInt value;
00418 for (value = 0; value < 3; value++)
00419 {
00420 theElement.iData.Format(KFormat1,value);
00421 aFixFlat.AppendL(theElement);
00422 }
00423
00424
00425
00426
00427 _LIT(KCommonFormat2,"\nLength()=%d");
00428 console->Printf(KCommonFormat2,aFixFlat.Length());
00429
00430
00431 _LIT(KCommonFormat3,"\nCount()=%d");
00432 console->Printf(KCommonFormat3,aFixFlat.Count());
00433
00434
00435 PrintFixArray(aFixFlat);
00436
00437
00438
00439 _LIT(KMsgInsert,"\n\n--->InsertL");
00440 console->Printf(KMsgInsert);
00441 _LIT(KMsgPressAnyKey," (press any key to continue)\n");
00442 console->Printf(KMsgPressAnyKey);
00443 console->Getch();
00444
00445
00446
00447 _LIT(KMsgBEG,"BEG");
00448 theElement.iData = KMsgBEG;
00449 aFixFlat.InsertL(0,theElement);
00450
00451
00452 _LIT(KMsgMID,"MID");
00453 theElement.iData = KMsgMID;
00454 aFixFlat.InsertL(2,theElement);
00455
00456
00457
00458 _LIT(KMsgEND,"END");
00459 theElement.iData = KMsgEND;
00460 aFixFlat.InsertL(aFixFlat.Count(),theElement);
00461
00462
00463 _LIT(KCommonFormat5,"Count()=%d");
00464 console->Printf(KCommonFormat5,aFixFlat.Count());
00465
00466
00467 PrintFixArray(aFixFlat);
00468
00469
00470
00471 TKeyArrayFix keyfix(_FOFF(TElement,iData),ECmpFolded);
00472
00473
00474 aFixFlat.Sort(keyfix);
00475
00476
00477 _LIT(KMsgSort,"\n--->Sort");
00478 console->Printf(KMsgSort);
00479 console->Printf(KMsgPressAnyKey);
00480 console->Getch();
00481
00482 PrintFixArray(aFixFlat);
00483
00484
00485
00486
00487 _LIT(KMsgDelete,"\n\n--->Delete");
00488 console->Printf(KMsgDelete);
00489 console->Printf(KMsgPressAnyKey);
00490 console->Getch();
00491
00492
00493
00494
00495
00496
00497
00498 aFixFlat.Delete(1);
00499 aFixFlat.Delete(2,2);
00500
00501 aFixFlat.Compress();
00502
00503
00504 console->Printf(KCommonFormat5,aFixFlat.Count());
00505
00506
00507 PrintFixArray(aFixFlat);
00508
00509
00510
00511 _LIT(KMsgReset,"\n\n--->Reset");
00512 console->Printf(KMsgReset);
00513 console->Printf(KMsgPressAnyKey);
00514 console->Getch();
00515
00516
00517 aFixFlat.Reset();
00518
00519
00520 console->Printf(KCommonFormat5,aFixFlat.Count());
00521
00522
00523 PrintFixArray(aFixFlat);
00524
00525
00526
00527
00528 _LIT(KMsgExpandExtend,"\n\n--->ExpandL & ExtendL");
00529 console->Printf(KMsgExpandExtend);
00530 console->Printf(KMsgPressAnyKey);
00531 console->Getch();
00532
00533
00534 _LIT(KFormat6,"%u");
00535 for (value = 0; value < 3; value++)
00536 {
00537 theElement.iData.Format(KFormat6,value);
00538 aFixFlat.AppendL(theElement);
00539 }
00540
00541
00542
00543
00544
00545 aFixFlat.ExpandL(1);
00546 aFixFlat.ExtendL();
00547
00548
00549 console->Printf(KCommonFormat5,aFixFlat.Count());
00550
00551
00552 PrintFixArray(aFixFlat);
00553
00554
00555
00556
00557 _LIT(KMsgResize,"\n\n--->ResizeL");
00558 console->Printf(KMsgResize);
00559 console->Printf(KMsgPressAnyKey);
00560 console->Getch();
00561
00562
00563
00564 aFixFlat.ResizeL(1);
00565
00566
00567
00568
00569
00570 aFixFlat.ResizeL(3);
00571
00572
00573
00574
00575
00576 _LIT(KMsgXXX,"XXX");
00577 theElement.iData = KMsgXXX;
00578 aFixFlat.ResizeL(5,theElement);
00579
00580
00581 console->Printf(KCommonFormat5,aFixFlat.Count());
00582
00583
00584 PrintFixArray(aFixFlat);
00585
00586
00587
00588
00589 _LIT(KMsgSetReserve,"\n\n--->SetReserveL");
00590 console->Printf(KMsgSetReserve);
00591 console->Printf(KMsgPressAnyKey);
00592 console->Getch();
00593
00594
00595
00596
00597
00598
00599
00600 aFixFlat.SetReserveL(5);
00601
00602
00603
00604 _LIT(KMsgDoubleAtoE,"AABBCCDDEE");
00605 TBufC<10> source(KMsgDoubleAtoE);
00606 TInt forix;
00607 for (forix = 0; forix < source.Length(); forix+=2)
00608 {
00609 theElement.iData = source.Mid(forix,2);
00610 aFixFlat.AppendL(theElement);
00611 }
00612
00613
00614 console->Printf(KCommonFormat5,aFixFlat.Count());
00615
00616
00617 PrintFixArray(aFixFlat);
00618
00619
00620
00621 TMySwap swap(&aFixFlat);
00622
00623
00624 swap.Swap(0,(aFixFlat.Count() - 1));
00625
00626
00627 _LIT(KMsgSwap,"\n--->Swap [First and Last element]");
00628 console->Printf(KMsgSwap);
00629 console->Printf(KMsgPressAnyKey);
00630 console->Getch();
00631
00632 PrintFixArray(aFixFlat);
00633 console->Printf(KMsgPressAnyKey);
00634 console->Getch();
00635 }
00636
00641 void DemoPtrArrayL(CArrayPtrFlat<CElement>& aPtrFlat)
00642 {
00643 CElement* ptr;
00644 ptr = new (ELeave) CElement;
00645 _LIT(KMsgFirstElement,"First Element");
00646 ptr->SetTextL(KMsgFirstElement);
00647 aPtrFlat.AppendL(ptr);
00648
00649
00650 ptr = new (ELeave) CElement;
00651 _LIT(KMsgSecondElement,"Second Element");
00652 ptr->SetTextL(KMsgSecondElement);
00653 aPtrFlat.AppendL(ptr);
00654
00655
00656 ptr = new (ELeave) CElement;
00657 _LIT(KMsgThirdElement,"Third Element");
00658 ptr->SetTextL(KMsgThirdElement);
00659 aPtrFlat.AppendL(ptr);
00660
00661
00662
00663 _LIT(KCommonFormat2,"\nLength()=%d");
00664 console->Printf(KCommonFormat2,aPtrFlat.Length());
00665
00666
00667 _LIT(KCommonFormat3,"\nCount()=%d");
00668 console->Printf(KCommonFormat3,aPtrFlat.Count());
00669
00670
00671 PrintPtrArray(&aPtrFlat);
00672
00673
00674
00675 _LIT(KMsgInsert,"\n\n--->InsertL");
00676 console->Printf(KMsgInsert);
00677 console->Printf(KMsgPressAnyKey);
00678 console->Getch();
00679
00680
00681
00682
00683 ptr = new (ELeave) CElement;
00684 _LIT(KMsgInsertedBeg,"Inserted @ beginning Element");
00685 ptr->SetTextL(KMsgInsertedBeg);
00686 aPtrFlat.InsertL(0,ptr);
00687
00688
00689 _LIT(KCommonFormat5,"Count()=%d");
00690 console->Printf(KCommonFormat5,aPtrFlat.Count());
00691
00692
00693 PrintPtrArray(&aPtrFlat);
00694
00695
00696
00697
00698 _LIT(KMsgDelete,"\n\n--->Delete");
00699 console->Printf(KMsgDelete);
00700 console->Printf(KMsgPressAnyKey);
00701 console->Getch();
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719 TInt index = -1;
00720 index = aPtrFlat.Count();
00721
00722 ptr = aPtrFlat[--index];
00723 aPtrFlat.Delete(index);
00724 delete ptr;
00725
00726 ptr = aPtrFlat.At(--index);
00727 aPtrFlat.Delete(index);
00728 delete ptr;
00729
00730
00731 console->Printf(KCommonFormat5,aPtrFlat.Count());
00732
00733
00734 PrintPtrArray(&aPtrFlat);
00735
00736
00737
00738
00739 _LIT(KMsgAt,"\n\n--->At() and the operator []");
00740 console->Printf(KMsgAt);
00741 console->Printf(KMsgPressAnyKey);
00742 console->Getch();
00743
00744
00745 _LIT(KMsgNewTextFirst,"New text for the first CElement");
00746 _LIT(KMsgNewTextSecond,"New text for the second CElement");
00747 aPtrFlat[0]->SetTextL(KMsgNewTextFirst);
00748 aPtrFlat.At(1)->SetTextL(KMsgNewTextSecond);
00749
00750
00751 console->Printf(KCommonFormat5,aPtrFlat.Count());
00752
00753
00754 PrintPtrArray(&aPtrFlat);
00755
00756
00757
00758
00759 _LIT(KMsgResetDestroy,"\n\n--->ResetAndDestroy");
00760 console->Printf(KMsgResetDestroy);
00761 console->Printf(KMsgPressAnyKey);
00762 console->Getch();
00763
00764
00765
00766 aPtrFlat.ResetAndDestroy();
00767
00768
00769 console->Printf(KCommonFormat3,aPtrFlat.Count());
00770
00771
00772 PrintPtrArray(&aPtrFlat);
00773
00774 console->Printf(KMsgNewLine);
00775 }
00776
00782 void DemoTKeyDerivedL(CArrayFixFlat<TIntPtrElement>& aFixFlat)
00783 {
00784 TIntPtrElement theIntPtrElement;
00785
00786
00787 TInt* ptrInt1 = new(ELeave)TInt(10);
00788 CleanupStack::PushL(ptrInt1);
00789 TInt* ptrInt2 = new(ELeave)TInt(0);
00790 CleanupStack::PushL(ptrInt2);
00791 TInt* ptrInt3 = new(ELeave)TInt(5);
00792 CleanupStack::PushL(ptrInt3);
00793
00794
00795 TInt* ptrInt[3] = {ptrInt1,ptrInt2,ptrInt3};
00796
00797
00798 TInt value;
00799 for (value = 0; value < 3; value++)
00800 {
00801 theIntPtrElement.iData.Format(KFormat1,value);
00802 theIntPtrElement.SetIntPtr(ptrInt[value]);
00803 aFixFlat.AppendL(theIntPtrElement);
00804 }
00805
00806
00807 PrintFixIntPtrArray(aFixFlat);
00808
00809
00810
00811 TKeyTIntPtr keyIntPtr(_FOFF(TIntPtrElement,iIntPtr),ECmpTInt);
00812
00813 _LIT(KMsgFind,"\n--->Find");
00814 console->Printf(KMsgFind);
00815 console->Printf(KMsgPressAnyKey);
00816 console->Getch();
00817
00818
00819
00820 TInt index = -1;
00821
00822
00823 TInt res = aFixFlat.Find (
00824 theIntPtrElement,
00825
00826 keyIntPtr,
00827
00828 index
00829 );
00830
00831 if(res == 0)
00832 {
00833
00834 _LIT(KElementFound,"\nElement found at index: %d\n");
00835 console->Printf(KElementFound,index);
00836 }
00837
00838 else
00839 {
00840
00841 _LIT(KElementNotFound,"\nElement not found");
00842 console->Printf(KElementNotFound);
00843 }
00844
00845 CleanupStack::PopAndDestroy(3, ptrInt1);
00846 }
00847
00852 void DemoRArrayL(RArray<CArrayElement>& aArray)
00853 {
00854 CArrayElement arrayElement;
00855
00856
00857
00858
00859 _LIT(KMsgAppend,"\n\n--->AppendL");
00860 console->Printf(KMsgAppend);
00861
00862 console->Printf(KMsgPressAnyKey);
00863 console->Getch();
00864
00865 TInt value;
00866 for (value = 0; value < 3; value++)
00867 {
00868
00869 arrayElement.iData.Format(KFormat1,value);
00870 aArray.AppendL(arrayElement);
00871 }
00872
00873
00874 _LIT(KCommonFormat3,"\nCount()=%d");
00875 console->Printf(KCommonFormat3,aArray.Count());
00876
00877
00878 TInt forix;
00879 _LIT(KCommonFormat4,"\n%S");
00880 for (forix = 0; forix < aArray.Count(); forix++)
00881 {
00882 console->Printf(KCommonFormat4,&(aArray)[forix].iData);
00883 }
00884
00885
00886
00887
00888 _LIT(KMsgInsert,"\n\n--->InsertL");
00889 console->Printf(KMsgInsert);
00890 console->Printf(KMsgPressAnyKey);
00891 console->Getch();
00892
00893
00894
00895
00896
00897
00898
00899 _LIT(KMsgBEG,"BEG");
00900 arrayElement.iData = KMsgBEG;
00901 aArray.InsertL(arrayElement, 0);
00902
00903
00904 _LIT(KMsgMID,"MID");
00905 arrayElement.iData = KMsgMID;
00906 aArray.InsertL(arrayElement, 2);
00907
00908
00909
00910 _LIT(KMsgEND,"END");
00911 arrayElement.iData = KMsgEND;
00912 aArray.InsertL(arrayElement,aArray.Count());
00913
00914
00915 _LIT(KCommonFormat5,"Count()=%d");
00916 console->Printf(KCommonFormat5,aArray.Count());
00917
00918
00919
00920
00921 _LIT(KMsgAccess,"\n\n--->Access");
00922 console->Printf(KMsgAccess);
00923 console->Printf(KMsgPressAnyKey);
00924 console->Getch();
00925
00926
00927 for (forix = 0; forix < aArray.Count(); forix++)
00928 console->Printf(KCommonFormat4,&(aArray)[forix].iData);
00929
00930
00931
00932
00933 _LIT(KMsgDelete,"\n\n--->Delete");
00934 console->Printf(KMsgDelete);
00935 console->Printf(KMsgPressAnyKey);
00936 console->Getch();
00937
00938 TInt index2;
00939 index2 = aArray.Count();
00940
00941
00942
00943
00944
00945 arrayElement = (aArray)[--index2];
00946 aArray.Remove(index2);
00947
00948
00949 console->Printf(KCommonFormat5,aArray.Count());
00950
00951
00952 for (forix = 0; forix < aArray.Count(); forix++)
00953 console->Printf(KCommonFormat4,&(aArray)[forix].iData);
00954
00955
00956
00957
00958 _LIT(KMsgFind,"\n\n--->Find in various ways");
00959 console->Printf(KMsgFind);
00960 console->Printf(KMsgPressAnyKey);
00961 console->Getch();
00962
00963 TInt pos =0;
00964
00965
00966 arrayElement = (aArray)[--index2];
00967
00968 _LIT(KCommonFormat6,"\nElement %S is found at position %d");
00969
00970
00971 pos=aArray.Find(arrayElement, TIdentityRelation<CArrayElement>());
00972 console->Printf(KCommonFormat6, &(aArray)[pos].iData, pos);
00973
00974
00975 pos=aArray.FindReverse(arrayElement, TIdentityRelation<CArrayElement>());
00976 console->Printf(KCommonFormat6, &(aArray)[pos].iData, pos);
00977
00978
00979 pos=aArray.FindInSignedKeyOrder(arrayElement);
00980 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos);
00981
00982
00983 pos=aArray.FindInUnsignedKeyOrder(arrayElement);
00984 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos);
00985
00986
00987
00988 pos=aArray.SpecificFindInSignedKeyOrder(arrayElement, EArrayFindMode_Last);
00989 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos);
00990
00991
00992
00993 pos=aArray.SpecificFindInUnsignedKeyOrder(arrayElement, EArrayFindMode_Last);
00994 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos);
00995
00996
00997 aArray.Close();
00998 }
00999
01000 void DemoRArraySort()
01001 {
01002
01003
01004
01005 _LIT(KSortFind,"\n\n--->Sort");
01006 console->Printf(KSortFind);
01007 console->Printf(KMsgPressAnyKey);
01008 console->Getch();
01009
01010 TLinearOrder<TInt64> Int64Order(Order64Bit);
01011 TInt count=10;
01012
01013
01014
01015
01016
01017 RArray<TInt64> firstArray(count);
01018 RArray<TInt64> secondArray(count);
01019
01020 for (TInt j=0; j<count; j++)
01021 {
01022 TInt64 x=(TInt64)(((TUint)Math::Random() %count));
01023
01024 firstArray.Append(x);
01025 secondArray.InsertInOrderAllowRepeats(x,Int64Order);
01026 }
01027
01028
01029 _LIT(KInitialArrayElements,"\nInitial array elements are:");
01030 console->Printf(KInitialArrayElements);
01031
01032 _LIT(KCommonFormat7," %d");
01033 TInt forix;
01034 for (forix = 0; forix < count; forix++)
01035 console->Printf(KCommonFormat7,firstArray[forix]);
01036
01037
01038 secondArray.Sort(Int64Order);
01039
01040
01041 _LIT(KSortedArrayElements,"\nSorted array elements are:");
01042 console->Printf(KSortedArrayElements);
01043 for (forix = 0; forix < count; forix++)
01044 console->Printf(KCommonFormat7,secondArray[forix]);
01045
01046 console->Getch();
01047 console->Printf(KMsgNewLine);
01048
01049
01050 firstArray.Close();
01051 secondArray.Close();
01052 }
01053
01054 void DemoRPointerArrayL(RPointerArray<CElement>& aPointerArray)
01055 {
01056 CElement* elementptr;
01057
01058
01059
01060
01061 _LIT(KMsgAppend,"\n\n--->AppendL");
01062 console->Printf(KMsgAppend);
01063 console->Printf(KMsgPressAnyKey);
01064 console->Getch();
01065
01066
01067 elementptr = new (ELeave) CElement;
01068 _LIT(KMsgFirstElement,"First Element");
01069 elementptr->SetTextL(KMsgFirstElement);
01070 aPointerArray.AppendL(elementptr);
01071
01072
01073 elementptr = new (ELeave) CElement;
01074 _LIT(KMsgSecondElement,"Second Element");
01075 elementptr->SetTextL(KMsgSecondElement);
01076 aPointerArray.AppendL(elementptr);
01077
01078
01079 elementptr = new (ELeave) CElement;
01080 _LIT(KMsgThirdElement,"Third Element");
01081 elementptr->SetTextL(KMsgThirdElement);
01082 aPointerArray.AppendL(elementptr);
01083
01084
01085 _LIT(KCommonFormat3,"\nCount()=%d");
01086 console->Printf(KCommonFormat3,aPointerArray.Count());
01087
01088
01089 _LIT(KCommonFormat4,"\n%S");
01090 TInt forix;
01091 for (forix = 0; forix < aPointerArray.Count(); forix++)
01092 {
01093 console->Printf(KCommonFormat4,aPointerArray[forix]->iBuf);
01094 }
01095
01096
01097
01098
01099 _LIT(KMsgInsert,"\n\n--->InsertL");
01100 console->Printf(KMsgInsert);
01101 console->Printf(KMsgPressAnyKey);
01102 console->Getch();
01103
01104
01105
01106
01107
01108
01109 elementptr = new (ELeave) CElement;
01110 _LIT(KMsgInsertedBeg,"Inserted @ beginning Element");
01111 elementptr->SetTextL(KMsgInsertedBeg);
01112 aPointerArray.InsertL(elementptr, 0);
01113
01114
01115 _LIT(KCommonFormat5,"Count()=%d");
01116 console->Printf(KCommonFormat5,aPointerArray.Count());
01117
01118
01119
01120
01121
01122 for (forix = 0; forix < aPointerArray.Count(); forix++)
01123 {
01124 console->Printf(KCommonFormat4,aPointerArray[forix]->iBuf);
01125 }
01126
01127
01128
01129
01130 _LIT(KMsgDelete,"\n\n--->Delete");
01131 console->Printf(KMsgDelete);
01132 console->Printf(KMsgPressAnyKey);
01133 console->Getch();
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143 TInt index1;
01144 index1 = aPointerArray.Count();
01145
01146 elementptr = aPointerArray[--index1];
01147 aPointerArray.Remove(index1);
01148 delete elementptr;
01149
01150
01151 console->Printf(KCommonFormat5,aPointerArray.Count());
01152
01153
01154 for (forix = 0; forix < aPointerArray.Count(); forix++)
01155 {
01156 console->Printf(KCommonFormat4,aPointerArray[forix]->iBuf);
01157 }
01158
01159
01160
01161
01162 _LIT(KMsgFind,"\n--->Find");
01163 console->Printf(KMsgFind);
01164 console->Printf(KMsgPressAnyKey);
01165 console->Getch();
01166
01167 elementptr = aPointerArray[index1-2];
01168
01169 _LIT(KCommonFormat8,"\nElement found at position %d");
01170
01171
01172 TInt result=aPointerArray.Find(elementptr);
01173 console->Printf(KCommonFormat8, result);
01174
01175
01176 result=aPointerArray.FindReverse(elementptr);
01177 console->Printf(KCommonFormat8, result);
01178
01179
01180
01181 result=aPointerArray.FindInAddressOrder(elementptr);
01182 console->Printf(KCommonFormat8, result);
01183
01184
01185
01186
01187 _LIT(KSortFind,"\n\n--->Sort");
01188 console->Printf(KSortFind);
01189 console->Printf(KMsgPressAnyKey);
01190 console->Getch();
01191
01192
01193
01194 _LIT(KMsgResetDestroy,"\n\n--->ResetAndDestroy");
01195 console->Printf(KMsgResetDestroy);
01196 console->Printf(KMsgPressAnyKey);
01197 console->Getch();
01198
01199
01200
01201 aPointerArray.ResetAndDestroy();
01202 }
01203
01204 void DemoSortRPointerArray()
01205 {
01206
01207
01208
01209 RPointerArray<TAny> firstpointerarray;
01210 RPointerArray<TAny> secondpointerarray;
01211
01212 TInt count = 10;
01213 for (TInt i=0; i<count; i++)
01214 {
01215 TAny* x=(TAny*)(((TUint)Math::Random() %count));
01216 firstpointerarray.Append(x);
01217 secondpointerarray.InsertInAddressOrderAllowRepeats(x);
01218 }
01219
01220
01221 _LIT(KInitialArrayElements,"\nInitial array elements are:");
01222 console->Printf(KInitialArrayElements);
01223
01224 _LIT(KCommonFormat7,"Array Contents %d \n");
01225 TInt forix;
01226 for (forix = 0; forix < count; forix++)
01227 {
01228 console->Printf(KCommonFormat7,firstpointerarray[forix]);
01229 }
01230
01231
01232 secondpointerarray.SortIntoAddressOrder();
01233
01234
01235 _LIT(KSortedArrayElements,"\nSorted array elements are:");
01236 console->Printf(KSortedArrayElements);
01237 for (forix = 0; forix < count; forix++)
01238 {
01239 console->Printf(KCommonFormat7,secondpointerarray[forix]);
01240 }
01241
01242
01243 firstpointerarray.Close();
01244 secondpointerarray.Close();
01245 }
01246
01247 LOCAL_C void doExampleL()
01248 {
01249
01250
01251
01252
01253
01254 CArrayFixFlat<TElement>* fixflat;
01255
01256
01257
01258
01259 fixflat = new (ELeave) CArrayFixFlat<TElement>(3);
01260 CleanupStack::PushL(fixflat);
01261
01262
01263 DemoFlatArrayL(*fixflat);
01264
01265
01266 CleanupStack::PopAndDestroy(fixflat);
01267
01268
01269
01270
01271 _LIT(KMsgArrayOfPointers,"\nARRAYS OF POINTERS (to CBase derived objects)\n");
01272 console->Printf(KMsgArrayOfPointers);
01273
01274 CArrayPtrFlat<CElement>* ptrflat;
01275
01276
01277
01278
01279
01280 ptrflat = new (ELeave) CArrayPtrFlat<CElement>(16);
01281 CleanupStack::PushL(ptrflat);
01282
01283
01284 DemoPtrArrayL(*ptrflat);
01285
01286
01287 CleanupStack::PopAndDestroy(ptrflat);
01288
01289
01290
01291
01292
01293 _LIT(KMsgVarArray,"\n\nVAR ARRAY\n\n");
01294 console->Printf(KMsgVarArray);
01295
01296
01297 CArrayVarFlat<TText>* varflat = new (ELeave) CArrayVarFlat<TText>(3);
01298 CleanupStack::PushL(varflat);
01299
01300
01301 DemoVarArraySortL(*varflat);
01302
01303
01304 CleanupStack::PopAndDestroy(varflat);
01305
01306 _LIT(KMsgPakArray,"\n\nPAK ARRAY\n\n");
01307 console->Printf(KMsgPakArray);
01308
01309
01310 CArrayPakFlat<TText>* pakflat = new (ELeave) CArrayPakFlat<TText>(3);
01311 CleanupStack::PushL(pakflat);
01312
01313
01314 DemoPakArrayFindL(*pakflat);
01315
01316
01317 CleanupStack::PopAndDestroy(pakflat);
01318
01319
01320
01321
01322
01323 _LIT(KMsgTKeyDerived,"\nTKEY DERIVED CLASS\n");
01324 console->Printf(KMsgPressAnyKey);
01325 console->Getch();
01326
01327 console->Printf(KMsgTKeyDerived);
01328 CArrayFixFlat<TIntPtrElement>* ptrElementArray;
01329
01330
01331
01332
01333 ptrElementArray = new (ELeave) CArrayFixFlat<TIntPtrElement>(3);
01334 CleanupStack::PushL(ptrElementArray);
01335
01336
01337 DemoTKeyDerivedL(*ptrElementArray);
01338
01339 CleanupStack::PopAndDestroy(ptrElementArray);
01340
01341
01342
01343
01344
01345 _LIT(KMsgForRArray,"\n*******Arrays of fixed length objects******");
01346 console->Printf(KMsgForRArray);
01347
01348 RArray<CArrayElement>* array;
01349
01350
01351
01352
01353
01354
01355
01356
01357 array = new (ELeave) RArray<CArrayElement>(16);
01358 CleanupStack::PushL(array);
01359
01360 DemoRArrayL(*array);
01361
01362 CleanupStack::PopAndDestroy(array);
01363
01364 DemoRArraySort();
01365
01366
01367
01368
01369
01370
01371 _LIT(KMsgForRPointerArray,"\n*******Array of pointers to objects******");
01372 console->Printf(KMsgForRPointerArray);
01373
01374 RPointerArray<CElement>* ptrarray;
01375
01376
01377
01378
01379
01380
01381 ptrarray = new (ELeave) RPointerArray<CElement>(16);
01382 CleanupStack::PushL(array);
01383
01384 DemoRPointerArrayL(*ptrarray);
01385 CleanupStack::PopAndDestroy(ptrarray);
01386
01387 DemoSortRPointerArray();
01388 }
01389