00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // Examples to demonstrate arrays 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 // A T type class used in the example demonstrating the: 00031 // CArrayFixFlat class 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 // A CBase derived class used in the example demonstrating the CArrayPtrFlat 00069 // array 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 // The buffer is not created. 00099 // Hence, create the heap buffer. 00100 // Set the length of CElement::iBuf to the length of aText. 00101 iBuf = HBufC::NewL(aText.Length()); 00102 } 00103 00104 else if(iBuf->Length() != aText.Length() ) 00105 { 00106 // The Buffer exists but is a different size to the input text. 00107 iBuf = iBuf->ReAllocL(aText.Length()); 00108 } 00109 // Copy the contents of aText to iBuf. 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 // Get the address of the array element. 00204 TIntPtrElement* ptr = (TIntPtrElement *)(iBase->Ptr(aIndex*sizeof(TIntPtrElement)).Ptr()+iKeyOffset); 00205 // The TIntPtrElement::iIntPtr points to the key. 00206 // Return the key. 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 // The Swap() function is defined for this array. 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 // Append TText strings to the aPakFlat array. 00297 00298 // The length of the string to be appended is increased by one 00299 // to insert the '\0' character at the end of the TText string. 00300 // This is needed as the '\0' character is absent in descriptors. 00301 aPakFlat.AppendL(*firstElement,(User::StringLength(firstElement)+1)*sizeof(TText)); 00302 00303 // Insert the second element into the array. 00304 aPakFlat.AppendL(*secondElement,(User::StringLength(secondElement)+1)*sizeof(TText)); 00305 00306 // Insert the third element into the array. 00307 aPakFlat.AppendL(*thirdElement,(User::StringLength(thirdElement)+1)*sizeof(TText)); 00308 00309 // Print the array elements. 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 // Find an element in the array. 00323 _LIT(KMsgFind,"\n--->Find"); 00324 console->Printf(KMsgFind); 00325 _LIT(KMsgFindElement,"\nSearching the array for:"); 00326 console->Printf(KMsgFindElement); 00327 00328 // The descriptor, buf contains the string "Code". 00329 // The CArrayPak::Find() function will try to find this string in the array. 00330 console->Printf(TPtrC(thirdElement)); 00331 00332 // The key to find the element. 00333 // The length of the key is set to 12. 00334 // Thus, first 12 characters of all strings are compared with the key. 00335 TKeyArrayPak keypak(0,ECmpNormal,12); 00336 00337 // Initialise the index to -1. 00338 TInt index = -1; 00339 00340 // Perform the search. 00341 if(aPakFlat.Find(*thirdElement,keypak,index) == 0) 00342 { 00343 // Successful search. 00344 // The index variable holds the index of the element found. 00345 _LIT(KElementFound,"\nElement found at %d\n"); 00346 console->Printf(KElementFound,index); 00347 } 00348 else 00349 { 00350 // Unsuccessful search. 00351 // The element is not found. 00352 _LIT(KElementNotFound,"\nElement not found"); 00353 console->Printf(KElementNotFound); 00354 } 00355 } 00356 00362 void DemoVarArraySortL(CArrayVar<TText>& aVarFlat) 00363 { 00364 // Append TText strings to the aVarFlat array. 00365 00366 // The length of the string to be appended is increased by one 00367 // to insert the '\0' character at the end of the TText string. 00368 // This is needed as the '\0' character is absent in descriptors. 00369 aVarFlat.AppendL(*firstElement,(User::StringLength(firstElement)+1)*sizeof(TText)); 00370 00371 // Insert the second and third elements into the array. 00372 aVarFlat.AppendL(*secondElement,(User::StringLength(secondElement)+1)*sizeof(TText)); 00373 aVarFlat.AppendL(*thirdElement,(User::StringLength(thirdElement)+1)*sizeof(TText)); 00374 00375 // Print the array elements. 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 // The key to find the element. 00390 // The length of the key is set to 12. 00391 // Thus, first 12 characters of the string are compared with the key. 00392 TKeyArrayVar keyvar(0,ECmpFolded,12); 00393 00394 // Sort the array using the TKeyArrayVar key. 00395 aVarFlat.Sort(keyvar); 00396 00397 // Print the sorted array elements. 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 // Show length of each element. 00425 // The length of each element of the array is the size of the 00426 // TElement class. 00427 _LIT(KCommonFormat2,"\nLength()=%d"); 00428 console->Printf(KCommonFormat2,aFixFlat.Length()); 00429 00430 // Show number of elements. 00431 _LIT(KCommonFormat3,"\nCount()=%d"); 00432 console->Printf(KCommonFormat3,aFixFlat.Count()); 00433 00434 // Show each element 00435 PrintFixArray(aFixFlat); 00436 // 00437 // InsertL * * * * * * * * * 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 // Insert elements 00446 // ... at the beginning, 00447 _LIT(KMsgBEG,"BEG"); 00448 theElement.iData = KMsgBEG; 00449 aFixFlat.InsertL(0,theElement); 00450 00451 // ... near the middle, 00452 _LIT(KMsgMID,"MID"); 00453 theElement.iData = KMsgMID; 00454 aFixFlat.InsertL(2,theElement); 00455 00456 // ... at the end. 00457 // This is the same as using the AppendL() function 00458 _LIT(KMsgEND,"END"); 00459 theElement.iData = KMsgEND; 00460 aFixFlat.InsertL(aFixFlat.Count(),theElement); 00461 00462 // Show number of elements 00463 _LIT(KCommonFormat5,"Count()=%d"); 00464 console->Printf(KCommonFormat5,aFixFlat.Count()); 00465 00466 // Show each element 00467 PrintFixArray(aFixFlat); 00468 00469 // Define the key to sort the array. 00470 // The data member of the TElement class, iData, is the key. 00471 TKeyArrayFix keyfix(_FOFF(TElement,iData),ECmpFolded); 00472 00473 // Sort the array. 00474 aFixFlat.Sort(keyfix); 00475 00476 // Print the sorted array. 00477 _LIT(KMsgSort,"\n--->Sort"); 00478 console->Printf(KMsgSort); 00479 console->Printf(KMsgPressAnyKey); 00480 console->Getch(); 00481 00482 PrintFixArray(aFixFlat); 00483 00484 // 00485 // Delete * * * * * * * * * 00486 // 00487 _LIT(KMsgDelete,"\n\n--->Delete"); 00488 console->Printf(KMsgDelete); 00489 console->Printf(KMsgPressAnyKey); 00490 console->Getch(); 00491 00492 // Delete the 2nd, 4th and 5th elements. Note: 00493 // 00494 // 1. We use position to identify the elements (i.e. offset) 00495 // 2. As elements are deleted, the position of other 00496 // elements changes. 00497 00498 aFixFlat.Delete(1); // delete the 2nd 00499 aFixFlat.Delete(2,2); // delete what are now the 3rd and 4th 00500 00501 aFixFlat.Compress(); // compress the array 00502 00503 // Show number of elements 00504 console->Printf(KCommonFormat5,aFixFlat.Count()); 00505 00506 // Show each element 00507 PrintFixArray(aFixFlat); 00508 // 00509 // Reset * * * * * * * * * 00510 // 00511 _LIT(KMsgReset,"\n\n--->Reset"); 00512 console->Printf(KMsgReset); 00513 console->Printf(KMsgPressAnyKey); 00514 console->Getch(); 00515 00516 // reset the array (i.e. empty it) 00517 aFixFlat.Reset(); 00518 00519 // Show number of elements 00520 console->Printf(KCommonFormat5,aFixFlat.Count()); 00521 00522 // Show each element 00523 PrintFixArray(aFixFlat); 00524 00525 // 00526 // ExpandL & ExtendL * * * * * * * * * 00527 // 00528 _LIT(KMsgExpandExtend,"\n\n--->ExpandL & ExtendL"); 00529 console->Printf(KMsgExpandExtend); 00530 console->Printf(KMsgPressAnyKey); 00531 console->Getch(); 00532 00533 // re-build the array 00534 _LIT(KFormat6,"%u"); 00535 for (value = 0; value < 3; value++) 00536 { 00537 theElement.iData.Format(KFormat6,value); 00538 aFixFlat.AppendL(theElement); 00539 } 00540 // Expand by constructing a new element at position 1. 00541 // Extend the array. 00542 // 00543 // Note the use of the TElement default constructor 00544 // in both cases 00545 aFixFlat.ExpandL(1); 00546 aFixFlat.ExtendL(); 00547 00548 // Show number of elements 00549 console->Printf(KCommonFormat5,aFixFlat.Count()); 00550 00551 // Show each element 00552 PrintFixArray(aFixFlat); 00553 00554 // 00555 // ResizeL * * * * * * * * * * * * 00556 // 00557 _LIT(KMsgResize,"\n\n--->ResizeL"); 00558 console->Printf(KMsgResize); 00559 console->Printf(KMsgPressAnyKey); 00560 console->Getch(); 00561 00562 // Resize the array so that it only contains 00563 // one element 00564 aFixFlat.ResizeL(1); 00565 00566 // Resize so that it contains 3 elements. 00567 // The two new elements are bit-wise copies 00568 // of a TElement object constructed using 00569 // its default constructor. 00570 aFixFlat.ResizeL(3); 00571 00572 // Resize so that it contains 5 elements. 00573 // The two new elements are bit-wise copies of 00574 // the TElement object passed through 00575 // the reference. 00576 _LIT(KMsgXXX,"XXX"); 00577 theElement.iData = KMsgXXX; 00578 aFixFlat.ResizeL(5,theElement); 00579 00580 // Show number of elements 00581 console->Printf(KCommonFormat5,aFixFlat.Count()); 00582 00583 // Show each element 00584 PrintFixArray(aFixFlat); 00585 00586 // 00587 // SetReserveL * * * * * * * * * * * * 00588 // 00589 _LIT(KMsgSetReserve,"\n\n--->SetReserveL"); 00590 console->Printf(KMsgSetReserve); 00591 console->Printf(KMsgPressAnyKey); 00592 console->Getch(); 00593 00594 // Reserve sufficient space to append another 00595 // five elements. 00596 // This function may leave if there is 00597 // insufficient memory. 00598 // NOTE: this does NOT increase the number of 00599 // elements in the array. 00600 aFixFlat.SetReserveL(5); 00601 00602 // We can now append five elements and be sure that 00603 // no leave will occur. 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 // Show number of elements 00614 console->Printf(KCommonFormat5,aFixFlat.Count()); 00615 00616 // Show each element 00617 PrintFixArray(aFixFlat); 00618 00619 // An object of the TMySwap class. 00620 // It is used to swap two elements of the array. 00621 TMySwap swap(&aFixFlat); 00622 00623 // Swap the first and the last element of the array. 00624 swap.Swap(0,(aFixFlat.Count() - 1)); 00625 00626 // Print the array. 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 // Show length of each element 00663 _LIT(KCommonFormat2,"\nLength()=%d"); 00664 console->Printf(KCommonFormat2,aPtrFlat.Length()); 00665 00666 // Show number of elements 00667 _LIT(KCommonFormat3,"\nCount()=%d"); 00668 console->Printf(KCommonFormat3,aPtrFlat.Count()); 00669 00670 // Show each element 00671 PrintPtrArray(&aPtrFlat); 00672 // 00673 // InsertL * * * * * * * * * 00674 // 00675 _LIT(KMsgInsert,"\n\n--->InsertL"); 00676 console->Printf(KMsgInsert); 00677 console->Printf(KMsgPressAnyKey); 00678 console->Getch(); 00679 00680 // Insert an element at the beginning 00681 // of the array. 00682 00683 ptr = new (ELeave) CElement; 00684 _LIT(KMsgInsertedBeg,"Inserted @ beginning Element"); 00685 ptr->SetTextL(KMsgInsertedBeg); 00686 aPtrFlat.InsertL(0,ptr); 00687 00688 // Show number of elements 00689 _LIT(KCommonFormat5,"Count()=%d"); 00690 console->Printf(KCommonFormat5,aPtrFlat.Count()); 00691 00692 // Show each element 00693 PrintPtrArray(&aPtrFlat); 00694 00695 // 00696 // Delete * * * * * * * * * 00697 // 00698 _LIT(KMsgDelete,"\n\n--->Delete"); 00699 console->Printf(KMsgDelete); 00700 console->Printf(KMsgPressAnyKey); 00701 console->Getch(); 00702 00703 // Delete the last two elements from the array BUT 00704 // first we must get a reference to those elements 00705 // (pointers to CElement objects) otherwise 00706 // the CElement objects are orphaned. 00707 // 00708 // Here, we destroy those CElement objects. 00709 // 00710 // There are two alternative ways of indexing into 00711 // the array, using either At() or the [] operator 00712 00713 // NOTE that the code below could be compressed to: 00714 // 00715 // delete (*ptrflat)[aPtrFlat.Count()-1]; 00716 // delete (*ptrflat)[aPtrFlat.Count()-2]; 00717 // aPtrFlat.Delete(aPtrFlat.Count()-2,2); 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 // Show number of elements 00731 console->Printf(KCommonFormat5,aPtrFlat.Count()); 00732 00733 // Show each element 00734 PrintPtrArray(&aPtrFlat); 00735 00736 // 00737 // At & the [] operator * * * * * * * * * * * * 00738 // 00739 _LIT(KMsgAt,"\n\n--->At() and the operator []"); 00740 console->Printf(KMsgAt); 00741 console->Printf(KMsgPressAnyKey); 00742 console->Getch(); 00743 00744 // Make a change to the object pointed to by the first element in the array 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 // Show number of elements 00751 console->Printf(KCommonFormat5,aPtrFlat.Count()); 00752 00753 // Show each element 00754 PrintPtrArray(&aPtrFlat); 00755 00756 // 00757 // ResetAndDestroy * * * * * * * * * 00758 // 00759 _LIT(KMsgResetDestroy,"\n\n--->ResetAndDestroy"); 00760 console->Printf(KMsgResetDestroy); 00761 console->Printf(KMsgPressAnyKey); 00762 console->Getch(); 00763 00764 // destroy all of the CElement objects and reset the 00765 // array. 00766 aPtrFlat.ResetAndDestroy(); 00767 00768 // Show number of elements 00769 console->Printf(KCommonFormat3,aPtrFlat.Count()); 00770 00771 // Show each element 00772 PrintPtrArray(&aPtrFlat); 00773 00774 console->Printf(KMsgNewLine); 00775 } 00776 00782 void DemoTKeyDerivedL(CArrayFixFlat<TIntPtrElement>& aFixFlat) 00783 { 00784 TIntPtrElement theIntPtrElement; 00785 00786 // Create pointers to integers. 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 // Create an array of pointers. 00795 TInt* ptrInt[3] = {ptrInt1,ptrInt2,ptrInt3}; 00796 00797 // Append elements into the ptrElementArray array. 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 // Print the contents of the array. 00807 PrintFixIntPtrArray(aFixFlat); 00808 00809 // Define the key to find an element in the array. 00810 // Use the TKey derived class, TKeyTIntPtr to do this. 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 // Initialise to -1. 00819 // Its value will be set to 0 if the Search is successful. 00820 TInt index = -1; 00821 00822 // Perform the search. 00823 TInt res = aFixFlat.Find ( 00824 theIntPtrElement, // The object whose 00825 // key is used for comparison. 00826 keyIntPtr, // The key object that 00827 // defines the property of the key. 00828 index 00829 ); 00830 00831 if(res == 0) 00832 { 00833 // The index variable holds the index of the element found. 00834 _LIT(KElementFound,"\nElement found at index: %d\n"); 00835 console->Printf(KElementFound,index); 00836 } 00837 00838 else 00839 { 00840 // The element is not found. 00841 _LIT(KElementNotFound,"\nElement not found"); 00842 console->Printf(KElementNotFound); 00843 } 00844 00845 CleanupStack::PopAndDestroy(3, ptrInt1); // ptrInt3, ptrInt2 and ptrInt1. 00846 } 00847 00852 void DemoRArrayL(RArray<CArrayElement>& aArray) 00853 { 00854 CArrayElement arrayElement; 00855 00856 // 00857 // AppendL * * * * * * * * * 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 // Formats and copies text into this descriptor, replacing any existing data. 00869 arrayElement.iData.Format(KFormat1,value); 00870 aArray.AppendL(arrayElement); // leaves with one of the system wide error codes, if the operation fails. 00871 } 00872 00873 // Show number of elements 00874 _LIT(KCommonFormat3,"\nCount()=%d"); 00875 console->Printf(KCommonFormat3,aArray.Count()); 00876 00877 // Show each element 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 // InsertL * * * * * * * * * 00887 // 00888 _LIT(KMsgInsert,"\n\n--->InsertL"); 00889 console->Printf(KMsgInsert); 00890 console->Printf(KMsgPressAnyKey); 00891 console->Getch(); 00892 00893 // Inserts objects into the array at a specified position. 00894 // It increases the size of the dynamic array. 00895 // This function leaves with one of the system error codes, if the operation fails. 00896 00897 // Insert arrayElements 00898 // ... at the beginning, 00899 _LIT(KMsgBEG,"BEG"); 00900 arrayElement.iData = KMsgBEG; 00901 aArray.InsertL(arrayElement, 0); 00902 00903 // ... near the middle, 00904 _LIT(KMsgMID,"MID"); 00905 arrayElement.iData = KMsgMID; 00906 aArray.InsertL(arrayElement, 2); 00907 00908 // ... at the end. 00909 // This is the same as using the AppendL() function 00910 _LIT(KMsgEND,"END"); 00911 arrayElement.iData = KMsgEND; 00912 aArray.InsertL(arrayElement,aArray.Count()); 00913 00914 // Show number of elements 00915 _LIT(KCommonFormat5,"Count()=%d"); 00916 console->Printf(KCommonFormat5,aArray.Count()); 00917 00918 // 00919 // Access elements * * * * * * * * * 00920 // 00921 _LIT(KMsgAccess,"\n\n--->Access"); 00922 console->Printf(KMsgAccess); 00923 console->Printf(KMsgPressAnyKey); 00924 console->Getch(); 00925 00926 // Show each element 00927 for (forix = 0; forix < aArray.Count(); forix++) 00928 console->Printf(KCommonFormat4,&(aArray)[forix].iData); 00929 00930 // 00931 // Delete * * * * * * * * * 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 // Removes the objects at a specified position from the array. 00942 // It shrinks the array. 00943 00944 // Delete the last element 00945 arrayElement = (aArray)[--index2]; 00946 aArray.Remove(index2); 00947 00948 // Show number of elements 00949 console->Printf(KCommonFormat5,aArray.Count()); 00950 00951 // Show each element 00952 for (forix = 0; forix < aArray.Count(); forix++) 00953 console->Printf(KCommonFormat4,&(aArray)[forix].iData); 00954 00955 // 00956 // Find * * * * * * * * * 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 // Find a particular element in different ways. 00966 arrayElement = (aArray)[--index2]; 00967 00968 _LIT(KCommonFormat6,"\nElement %S is found at position %d"); 00969 00970 // Finds the first object in the array which matches the specified object using a sequential search and a matching algorithm. 00971 pos=aArray.Find(arrayElement, TIdentityRelation<CArrayElement>()); 00972 console->Printf(KCommonFormat6, &(aArray)[pos].iData, pos); 00973 00974 // Finds the last object in the array which matches the specified object using a sequential search and a matching algorithm. 00975 pos=aArray.FindReverse(arrayElement, TIdentityRelation<CArrayElement>()); 00976 console->Printf(KCommonFormat6, &(aArray)[pos].iData, pos); 00977 00978 // Finds the object in the array which matches the specified object using a binary search technique.It assumes that objects are in signed key order 00979 pos=aArray.FindInSignedKeyOrder(arrayElement); 00980 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos); 00981 00982 // Finds the object in the array which matches the specified object using a binary search technique. It assumes that objects are in unsigned key order. 00983 pos=aArray.FindInUnsignedKeyOrder(arrayElement); 00984 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos); 00985 00986 // Finds the object in the array which matches the specified object using a binary search technique.In case of more than one matching element, finds last 00987 // match as specified in the second parameter.It assumes that objects are in signed key order 00988 pos=aArray.SpecificFindInSignedKeyOrder(arrayElement, EArrayFindMode_Last); 00989 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos); 00990 00991 // Finds the object in the array which matches the specified object using a binary search technique.In case of more than one matching element, finds last 00992 // match as specified in the second parameter.It assumes that objects are in unsigned key order 00993 pos=aArray.SpecificFindInUnsignedKeyOrder(arrayElement, EArrayFindMode_Last); 00994 console->Printf(KCommonFormat6,&(aArray)[pos].iData, pos); 00995 00996 // Closes the array and frees all memory allocated to the array 00997 aArray.Close(); 00998 } 00999 01000 void DemoRArraySort() 01001 { 01002 // 01003 // Sort * * * * * * * * * 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 // Construct two arrays of 64-bit signed integer type objects. 01014 // Append random objects to first array. 01015 // Sort all objects of first array in the second array 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 // Show the initial array elements of first array. 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 // Sorts the objects within the array using the specified TLinearOrder 01038 secondArray.Sort(Int64Order); 01039 01040 // Show sorted array elements of second array 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 // Closes both the arrays and frees all the memory allocated to these arrays 01050 firstArray.Close(); 01051 secondArray.Close(); 01052 } 01053 01054 void DemoRPointerArrayL(RPointerArray<CElement>& aPointerArray) 01055 { 01056 CElement* elementptr; 01057 01058 // 01059 // AppendL * * * * * * * * * 01060 // 01061 _LIT(KMsgAppend,"\n\n--->AppendL"); 01062 console->Printf(KMsgAppend); 01063 console->Printf(KMsgPressAnyKey); 01064 console->Getch(); 01065 01066 // Append the first element. 01067 elementptr = new (ELeave) CElement; 01068 _LIT(KMsgFirstElement,"First Element"); 01069 elementptr->SetTextL(KMsgFirstElement); 01070 aPointerArray.AppendL(elementptr); 01071 01072 // Append the second element. 01073 elementptr = new (ELeave) CElement; 01074 _LIT(KMsgSecondElement,"Second Element"); 01075 elementptr->SetTextL(KMsgSecondElement); 01076 aPointerArray.AppendL(elementptr); 01077 01078 // Append the third element. 01079 elementptr = new (ELeave) CElement; 01080 _LIT(KMsgThirdElement,"Third Element"); 01081 elementptr->SetTextL(KMsgThirdElement); 01082 aPointerArray.AppendL(elementptr); 01083 01084 // Show number of elements 01085 _LIT(KCommonFormat3,"\nCount()=%d"); 01086 console->Printf(KCommonFormat3,aPointerArray.Count()); 01087 01088 // Show each element 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 // InsertL * * * * * * * * * 01098 // 01099 _LIT(KMsgInsert,"\n\n--->InsertL"); 01100 console->Printf(KMsgInsert); 01101 console->Printf(KMsgPressAnyKey); 01102 console->Getch(); 01103 01104 // Inserts objects into the array at a specified position. 01105 // It increases the size of the dynamic array. 01106 01107 // Insert an element at the beginning 01108 // of the array 01109 elementptr = new (ELeave) CElement; 01110 _LIT(KMsgInsertedBeg,"Inserted @ beginning Element"); 01111 elementptr->SetTextL(KMsgInsertedBeg); 01112 aPointerArray.InsertL(elementptr, 0); 01113 01114 // Show number of elements 01115 _LIT(KCommonFormat5,"Count()=%d"); 01116 console->Printf(KCommonFormat5,aPointerArray.Count()); 01117 01118 // 01119 // Access * * * * * * * * * 01120 // 01121 // Show each element 01122 for (forix = 0; forix < aPointerArray.Count(); forix++) 01123 { 01124 console->Printf(KCommonFormat4,aPointerArray[forix]->iBuf); 01125 } 01126 01127 // 01128 // Delete * * * * * * * * * 01129 // 01130 _LIT(KMsgDelete,"\n\n--->Delete"); 01131 console->Printf(KMsgDelete); 01132 console->Printf(KMsgPressAnyKey); 01133 console->Getch(); 01134 01135 // Delete the last elements from the array BUT 01136 // first we must get a reference to the element 01137 // (pointers to CElement objects) otherwise 01138 // the CElement objects are orphaned. 01139 // 01140 // Here, we destroy the CElement object. It shrinks the array. 01141 // 01142 // Use [] operator for indexing in to the array. 01143 TInt index1; 01144 index1 = aPointerArray.Count(); 01145 01146 elementptr = aPointerArray[--index1]; 01147 aPointerArray.Remove(index1); 01148 delete elementptr; 01149 01150 // Show number of elements 01151 console->Printf(KCommonFormat5,aPointerArray.Count()); 01152 01153 // Show each element 01154 for (forix = 0; forix < aPointerArray.Count(); forix++) 01155 { 01156 console->Printf(KCommonFormat4,aPointerArray[forix]->iBuf); 01157 } 01158 01159 // 01160 // Find * * * * * * * * * 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 // Finds the first object pointer in the array which matches the specified object pointer, using a sequential search. 01172 TInt result=aPointerArray.Find(elementptr); 01173 console->Printf(KCommonFormat8, result); 01174 01175 // Finds the last object pointer in the array which matches the specified object pointer, using a sequential search. 01176 result=aPointerArray.FindReverse(elementptr); 01177 console->Printf(KCommonFormat8, result); 01178 01179 // Finds the object pointer in the array that matches the specified object pointer, using a binary search technique. 01180 // It assumes that object pointers in the array are in address order. 01181 result=aPointerArray.FindInAddressOrder(elementptr); 01182 console->Printf(KCommonFormat8, result); 01183 01184 // 01185 // Sort * * * * * * * * * 01186 // 01187 _LIT(KSortFind,"\n\n--->Sort"); 01188 console->Printf(KSortFind); 01189 console->Printf(KMsgPressAnyKey); 01190 console->Getch(); 01191 01192 // ResetAndDestroy * * * * * * * * * 01193 // 01194 _LIT(KMsgResetDestroy,"\n\n--->ResetAndDestroy"); 01195 console->Printf(KMsgResetDestroy); 01196 console->Printf(KMsgPressAnyKey); 01197 console->Getch(); 01198 01199 // Empties the array and deletes the referenced objects. 01200 // It frees all memory allocated to the array and resets the internal state so that it is ready to be reused. 01201 aPointerArray.ResetAndDestroy(); 01202 } 01203 01204 void DemoSortRPointerArray() 01205 { 01206 // Construct two arrays of TAny pointer objects. 01207 // Append random objects to first pointer array. 01208 // Sort all objects of first array in the second pointer array. 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 // Show initial array elements of first array 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 // Sorts the object pointers within the array into address order 01232 secondpointerarray.SortIntoAddressOrder(); 01233 01234 // Show sorted array elements of second array 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 // Closes the arrays and frees all memory allocated to it 01243 firstpointerarray.Close(); 01244 secondpointerarray.Close(); 01245 } 01246 // Do the example 01247 LOCAL_C void doExampleL() 01248 { 01249 //************************************************************ 01250 // Demonstrate a general fixed length array using 01251 // a flat buffer 01252 //************************************************************ 01253 01254 CArrayFixFlat<TElement>* fixflat; 01255 // Construct array of TElement objects where the iData 01256 // data member of each element contains "X0", "X1", etc 01257 // Uses the AppendL() function to add the members 01258 // and the [] operator to access elements. 01259 fixflat = new (ELeave) CArrayFixFlat<TElement>(3); 01260 CleanupStack::PushL(fixflat); 01261 01262 // Demonstrate the CArrayFixFlat array. 01263 DemoFlatArrayL(*fixflat); 01264 01265 // Delete the fixflat array. 01266 CleanupStack::PopAndDestroy(fixflat); // fixflat 01267 //************************************************************ 01268 // Demonstrate an array of pointers to CBase 01269 // derived objects. Uses the specialised array CArrayPtrFlat 01270 //************************************************************ 01271 _LIT(KMsgArrayOfPointers,"\nARRAYS OF POINTERS (to CBase derived objects)\n"); 01272 console->Printf(KMsgArrayOfPointers); 01273 01274 CArrayPtrFlat<CElement>* ptrflat; 01275 01276 // Construct an array of four CElement objects each 01277 // containing the text "First" "second" etc 01278 // Uses the AppendL() function to add the members 01279 // and the [] operator to access elements. 01280 ptrflat = new (ELeave) CArrayPtrFlat<CElement>(16); 01281 CleanupStack::PushL(ptrflat); 01282 01283 // Demonstrate the CArrayPtrFlat array. 01284 DemoPtrArrayL(*ptrflat); 01285 01286 // Delete the array. 01287 CleanupStack::PopAndDestroy(ptrflat); // ptrflat 01288 01289 //************************************************************ 01290 // Demonstrate Variable and Packed arrays 01291 // Use TKey to sort these arrays 01292 //************************************************************ 01293 _LIT(KMsgVarArray,"\n\nVAR ARRAY\n\n"); 01294 console->Printf(KMsgVarArray); 01295 01296 // Create a Var array. 01297 CArrayVarFlat<TText>* varflat = new (ELeave) CArrayVarFlat<TText>(3); 01298 CleanupStack::PushL(varflat); 01299 01300 // Demonstrate the CArrayVarFlat array. 01301 DemoVarArraySortL(*varflat); 01302 01303 // Delete the array. 01304 CleanupStack::PopAndDestroy(varflat); // varflat 01305 01306 _LIT(KMsgPakArray,"\n\nPAK ARRAY\n\n"); 01307 console->Printf(KMsgPakArray); 01308 01309 // Create a TKeyArrayPak array. 01310 CArrayPakFlat<TText>* pakflat = new (ELeave) CArrayPakFlat<TText>(3); 01311 CleanupStack::PushL(pakflat); 01312 01313 // Demonstrate the CArrayPakFlat array. 01314 DemoPakArrayFindL(*pakflat); 01315 01316 // Delete the array. 01317 CleanupStack::PopAndDestroy(pakflat); // pakflat, 01318 01319 //************************************************************ 01320 // Demonstrate the CArrayFix::Find() function using the 01321 // TKey derived class TKeyTIntPtr. 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 // Construct array of TIntPtrElement objects where the iData 01330 // data member of each element contains "X0", "X1", etc 01331 // Uses the AppendL() function to add the members 01332 // and the [] operator to access elements. 01333 ptrElementArray = new (ELeave) CArrayFixFlat<TIntPtrElement>(3); 01334 CleanupStack::PushL(ptrElementArray); 01335 01336 // Demonstrate the use of TKey derived class. 01337 DemoTKeyDerivedL(*ptrElementArray); 01338 01339 CleanupStack::PopAndDestroy(ptrElementArray); // ptrElementArray, 01340 01341 //***************************************************************** 01342 // Demonstrate an array of type RArray for elements of the same size 01343 //***************************************************************** 01344 01345 _LIT(KMsgForRArray,"\n*******Arrays of fixed length objects******"); 01346 console->Printf(KMsgForRArray); 01347 01348 RArray<CArrayElement>* array; 01349 01350 // Construct array of CArrayElement objects where the iData 01351 // data member of each element contains "X0", "X1", etc. 01352 // The elements of the array are instances of a class, 01353 // so this is a template class. Refer to the definition of 01354 // element CArrayElement above. 01355 // Uses the AppendL() function to add the members 01356 // and the [] operator to access elements. 01357 array = new (ELeave) RArray<CArrayElement>(16); // Initialise the array granularity to 16 01358 CleanupStack::PushL(array); 01359 // Demonstrate the use of RArray. 01360 DemoRArrayL(*array); 01361 01362 CleanupStack::PopAndDestroy(array); 01363 // Demonstrates sorting of the RArray. 01364 DemoRArraySort(); 01365 01366 01367 //****************************************************************************** 01368 // Demonstrate a simple and efficient array of pointers to objects RPointerArray 01369 //****************************************************************************** 01370 01371 _LIT(KMsgForRPointerArray,"\n*******Array of pointers to objects******"); 01372 console->Printf(KMsgForRPointerArray); 01373 01374 RPointerArray<CElement>* ptrarray; 01375 01376 // Construct an array of four CElement objects each 01377 // containing the text "First", "Second" etc 01378 // Uses the AppendL() function to add the members 01379 // and the [] operator to access elements. 01380 // It leaves with one of the system wide error codes, if the operation fails. 01381 ptrarray = new (ELeave) RPointerArray<CElement>(16); // Initialise the array granularity to 16 01382 CleanupStack::PushL(array); 01383 // Demonstrate the use of RPointerArray. 01384 DemoRPointerArrayL(*ptrarray); 01385 CleanupStack::PopAndDestroy(ptrarray); 01386 // Demonstrates sorting of the RPointerArray. 01387 DemoSortRPointerArray(); 01388 } 01389
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.