00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00022 #include "hashtableexample.h"
00023 #include <e32hashtab.h>
00024 #include <e32math.h>
00025
00029 CHashTableExample::CHashTableExample()
00030 {
00031 }
00032
00033 void CHashTableExample::ConstructL()
00034 {
00035 iConsole = Console::NewL(KTitle, TSize(KConsFullScreen, KConsFullScreen));
00036 iConsole->Printf(KWelcome);
00037 iConsole->Printf(KPressAKeyMsg );
00038 iConsole->Getch();
00039 }
00040
00044 CHashTableExample::~CHashTableExample()
00045 {
00046 delete iConsole;
00047 iPointerArray.ResetAndDestroy();
00048 }
00049
00055 CHashTableExample* CHashTableExample::NewL()
00056 {
00057 CHashTableExample* self=new(ELeave)CHashTableExample();
00058 CleanupStack::PushL(self);
00059 self->ConstructL();
00060 CleanupStack::Pop(self);
00061 return self;
00062 }
00063
00068 void CHashTableExample::ConstructDefaultHashSet()
00069 {
00070
00071 RHashSet<TInt> hashSetInt();
00072
00073
00074 RHashSet<TDesC8> hashSetDes8();
00075
00076
00077 RHashSet<TDesC16> hashSetDes16();
00078
00079 iConsole->Printf(KHashSet);
00080 iConsole->Printf(KConstruct);
00081 iConsole->Printf(KConstructDefaultHashSet);
00082 }
00083
00087 struct TMyOwnObject
00088 {
00089 TInt iVar1;
00090 TInt iVar2;
00091 };
00092
00099 TUint32 MyHashFunction(const TMyOwnObject& aObject)
00100 {
00101 return DefaultHash::Integer(aObject.iVar1) + DefaultHash::Integer(aObject.iVar2 );
00102 }
00103
00111 TBool MyIdentityFunction(const TMyOwnObject& aObject1, const TMyOwnObject& aObject2)
00112 {
00113 return aObject1.iVar1 == aObject2.iVar1 && aObject1.iVar2 != aObject2.iVar2;
00114 }
00115
00120 void CHashTableExample::ConstructOwnHashSet()
00121 {
00122
00123 THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00124
00125
00126 TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);
00127
00128
00129 RHashSet<TMyOwnObject> ownHashSet(ownHashFunction, ownIdentityFunction);
00130 iConsole->Printf(KConstructOwnHashSet);
00131 }
00132
00143 void CHashTableExample::OperationsToHashSetL()
00144 {
00145
00146 const TInt startItem=1;
00147
00148
00149 const TInt endItem=100;
00150 TInt itemToBeFound=120;
00151 TInt itemToBeRemoved=200;
00152 TInt64 items=1;
00153
00154 iConsole->Printf(KOperation);
00155
00156
00157 RHashSet<TInt> hashSet;
00158
00159
00160 CleanupClosePushL(hashSet);
00161
00162
00163 for (TInt i=startItem; i<=endItem; ++i)
00164 {
00165 TInt res = Math::Rand(items);
00166 hashSet.InsertL(res);
00167 }
00168
00169 iConsole->Printf(KInsertItemsToHashSet);
00170
00171
00172 TInt* result= hashSet.Find(itemToBeFound);
00173
00174
00175 if(result)
00176 {
00177 iConsole->Printf(KItemPresentInHashSet);
00178 }
00179 else
00180 {
00181 iConsole->Printf(KItemNotPresentInHashSet);
00182 }
00183
00184
00185 RHashSet<TInt>::TIter hashSetIter(hashSet);
00186
00187
00188
00189 for ( ; ;)
00190 {
00191 const TInt* res = hashSetIter.Next();
00192
00193
00194
00195 if (!res)
00196 {
00197 break;
00198 }
00199 }
00200
00201 iConsole->Printf(KIterateItemsFromHashSet);
00202
00203
00204 TInt res = hashSet.Remove(itemToBeRemoved);
00205
00206 if(res)
00207 {
00208 iConsole->Printf(KItemPresentInHashSet);
00209 }
00210 else
00211 {
00212 iConsole->Printf(KItemNotPresentInHashSet);
00213 }
00214
00215 iConsole->Printf(KRemoveItemsFromHashSet);
00216
00217
00218 CleanupStack::PopAndDestroy(&hashSet);
00219
00220 iConsole->Printf(KPressAKey);
00221 iConsole->Getch();
00222 }
00223
00228 void CHashTableExample::ConstructDefaultPtrHashSet()
00229 {
00230
00231 RPtrHashSet<TInt> ptrHashSetInt();
00232
00233
00234 RPtrHashSet<TDesC8> ptrHashSetDes8();
00235
00236
00237 RPtrHashSet<TDesC16> ptrHashSetDes16();
00238
00239 iConsole->Printf(KPtrHashSet);
00240 iConsole->Printf(KConstruct);
00241 iConsole->Printf(KConstructDefaultPtrHashSet);
00242 }
00243
00248 void CHashTableExample::ConstructOwnPtrHashSet()
00249 {
00250
00251 THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00252
00253
00254 TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);
00255
00256
00257 RPtrHashSet<TMyOwnObject> ownPtrHashSet(ownHashFunction, ownIdentityFunction);
00258 iConsole->Printf(KConstructOwnPtrHashSet);
00259 }
00260
00266 void FindNumberInWords(const TInt& aNum, TDes& aDes)
00267 {
00268 TInt number = aNum;
00269 const TInt bufferSize=256;
00270 const TText* numbers[] = {_S("zero"), _S("one"), _S("two"),_S("three"),_S("four"),_S("five"),_S("six"),_S("seven"),
00271 _S("eight"),_S("nine"),_S("ten"),_S("eleven"),_S("twelve"),_S("thirteen"),
00272 _S("fourteen"),_S("fifteen"), _S("sixteen"),_S( "seventeen"),_S( "eighteen"),
00273 _S("nineteen"),_S( "twenty"),_S( "thirty"),_S( "forty"),_S( "fifty"),_S("sixty"),
00274 _S("seventy"),_S( "eighty"), _S("ninety"), _S("hundred"), _S("thousand") };
00275
00276
00277 if (number<20)
00278 {
00279 aDes.Copy(reinterpret_cast<const TUint16*> (numbers[number]));
00280 }
00281
00282
00283 if (number<100 && number>=20)
00284 {
00285 TInt tens = number/10;
00286 TInt units = number%10;
00287 aDes.Copy(reinterpret_cast<const TUint16*> (numbers[tens-2+20]));
00288 if (units)
00289 {
00290 aDes.Append(' ');
00291 aDes.Append(TPtrC16(reinterpret_cast<const TUint16*> (numbers[units])));
00292 }
00293 }
00294
00295
00296 if (number<1000 && number>=100)
00297 {
00298 TInt hundreds = number/100;
00299 aDes.Copy(reinterpret_cast<const TUint16*> (numbers[hundreds]));
00300 aDes.Append(' ');
00301 aDes.Append(TPtrC16(reinterpret_cast<const TUint16*> (numbers[28])));
00302 number%=100;
00303 if (number)
00304 {
00305 TBuf<bufferSize> buf;
00306 TDes& des1= buf;
00307 FindNumberInWords(number, des1);
00308 aDes.Append(KAnd);
00309 aDes+=des1;
00310 }
00311 }
00312
00313
00314 if(number>=1000)
00315 {
00316 TInt hundreds = number/1000;
00317 aDes.Copy(reinterpret_cast<const TUint16*> (numbers[hundreds]));
00318 aDes.Append(' ');
00319 aDes.Append(TPtrC16(reinterpret_cast<const TUint16*> (numbers[29])));
00320 number%=1000;
00321 if (number)
00322 {
00323 TBuf<bufferSize> buf;
00324 TDes& des1= buf;
00325 FindNumberInWords(number, des1);
00326 aDes.Append(KAnd);
00327 aDes+=des1;
00328 }
00329 }
00330 }
00331
00342 void CHashTableExample::OperationsToPtrHashSetL()
00343 {
00344
00345 iConsole->Printf(KOperation);
00346
00347
00348
00349
00350
00351
00352
00353 const TInt KMaxItem=1200;
00354 const TInt KMaxBufferSize=256;
00355 TInt i=0;
00356 for (i=0; i<KMaxItem; ++i)
00357 {
00358 HBufC* hbuf = HBufC::NewLC(KMaxBufferSize);
00359 TPtr buf = hbuf->Des();
00360
00361 FindNumberInWords(i, buf);
00362 iPointerArray.AppendL(hbuf);
00363 CleanupStack::Pop(hbuf);
00364 }
00365
00366
00367
00368
00369 RPtrHashSet<TDesC16> ptrHashSet;
00370
00371 CleanupClosePushL(ptrHashSet);
00372
00373 for (i=0; i<KMaxItem; ++i)
00374 {
00375 ptrHashSet.InsertL(iPointerArray[i]);
00376 }
00377 iConsole->Printf(KInsertItemsToPtrHashSet);
00378
00379
00380 TDesC16* item1 = ptrHashSet.Find(KFindItem);
00381
00382 if (item1)
00383 {
00384 iConsole->Printf(KItemPresentInPtrHashSet);
00385 }
00386 else
00387 {
00388 iConsole->Printf(KItemNotPresentInPtrHashSet);
00389 }
00390
00391
00392
00393 RPtrHashSet<TDesC16>::TIter ptrHashSetIter(ptrHashSet);
00394
00395 for ( ; ; )
00396 {
00397 const TDesC16* resNext = ptrHashSetIter.Next();
00398
00399
00400 if (!resNext)
00401 {
00402 break;
00403 }
00404 }
00405 iConsole->Printf(KIterateItemsFromPtrHashSet);
00406
00407
00408 TInt err = ptrHashSet.Remove(&KRemoveItem);
00409
00410 if (err == KErrNone)
00411 {
00412 iConsole->Printf(KItemPresentInPtrHashSet);
00413 }
00414 else
00415 {
00416 iConsole->Printf(KItemNotPresentInPtrHashSet);
00417 }
00418 iConsole->Printf(KRemoveItemsFromPtrHashSet);
00419
00420
00421 CleanupStack::PopAndDestroy(&ptrHashSet);
00422
00423 iConsole->Printf(KPressAKey);
00424 iConsole->Getch();
00425 }
00426
00431 void CHashTableExample::ConstructDefaultHashMap()
00432 {
00433
00434 RHashMap<TInt, TInt> hashMapInt();
00435
00436
00437 RHashMap<TDesC8, TDesC8> hashMapDes8();
00438
00439
00440 RHashMap<TDesC16, TDesC16> hashMapDes16();
00441
00442 iConsole->Printf(KHashMap);
00443 iConsole->Printf(KConstruct);
00444 iConsole->Printf(KConstructDeafultHashMap);
00445 }
00446
00451 void CHashTableExample::ConstructOwnHashMap()
00452 {
00453
00454 THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00455
00456
00457 TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);
00458
00459
00460 RHashMap<TMyOwnObject, TMyOwnObject> ownHashMap(ownHashFunction, ownIdentityFunction);
00461 iConsole->Printf(KConstructOwnHashMap);
00462 }
00463
00474 void CHashTableExample::OperationsToHashMapL()
00475 {
00476 TInt maxItem=300;
00477 TInt itemToBeFound=150;
00478 TInt itemToBeRemoved=200;
00479 TInt64 items;
00480
00481 iConsole->Printf(KOperation);
00482
00483
00484 RHashMap<TInt, TInt> hashMap;
00485
00486
00487 CleanupClosePushL(hashMap);
00488
00489
00490 for (TInt i=0; i<maxItem; i++)
00491 {
00492 TInt res = Math::Rand(items);
00493 hashMap.InsertL(res*res, res);
00494 }
00495
00496 iConsole->Printf(KInsertItemsToHashMap);
00497
00498
00499 TInt* result= hashMap.Find(itemToBeFound);
00500
00501
00502 if(result)
00503 {
00504 iConsole->Printf(KItemPresentInHashMap);
00505 }
00506 else
00507 {
00508 iConsole->Printf(KItemNotPresentInHashMap);
00509 }
00510
00511
00512
00513 RHashMap<TInt, TInt>::TIter hashMapIter(hashMap);
00514
00515 for ( ; ; )
00516 {
00517 const TInt* resNext = hashMapIter.NextKey();
00518 if (!resNext)
00519 {
00520 break;
00521 }
00522 }
00523
00524 iConsole->Printf(KIterateItemsFromHashMap);
00525
00526
00527 TInt res = hashMap.Remove(itemToBeRemoved);
00528
00529
00530 if(res)
00531 {
00532 iConsole->Printf(KItemPresentInHashMap);
00533 }
00534 else
00535 {
00536 iConsole->Printf(KItemNotPresentInHashMap);
00537 }
00538
00539 iConsole->Printf(KRemoveItemsFromHashMap);
00540
00541
00542 CleanupStack::PopAndDestroy(&hashMap);
00543
00544 iConsole->Printf(KPressAKey);
00545 iConsole->Getch();
00546 }
00547
00552 void CHashTableExample::ConstructDefaultPtrHashMap()
00553 {
00554
00555 RPtrHashMap<TInt, TInt> ptrHashMapInt();
00556
00557
00558 RPtrHashMap<TDesC8, TDesC8> ptrHashMapDes8();
00559
00560
00561 RPtrHashMap<TDesC16, TDesC16> ptrHashMapDes16();
00562
00563 iConsole->Printf(KPtrHashMap);
00564 iConsole->Printf(KConstruct);
00565 iConsole->Printf(KConstructDeafultPtrHashMap);
00566 }
00567
00572 void CHashTableExample::ConstructOwnPtrHashMap()
00573 {
00574
00575 THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00576
00577
00578 TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);
00579
00580
00581 RPtrHashMap<TMyOwnObject, TMyOwnObject> ownPtrHashMap(ownHashFunction, ownIdentityFunction);
00582 iConsole->Printf(KConstructOwnPtrHashMap);
00583 }
00584
00595 void CHashTableExample::OperationsToPtrHashMapL()
00596 {
00597 TInt i;
00598 TInt maxItem=200;
00599
00600 iConsole->Printf(KOperation);
00601
00602
00603 RPtrHashMap<TDesC16, TDesC16> ptrHashMap;
00604
00605
00606 CleanupClosePushL(ptrHashMap);
00607
00608
00609 for (i=0; i<maxItem; ++i)
00610 {
00611 ptrHashMap.InsertL(iPointerArray[i], iPointerArray[i+1]);
00612 }
00613
00614 iConsole->Printf(KInsertItemsToPtrHashMap);
00615
00616
00617 TDesC16* item1= ptrHashMap.Find(KFindItem);
00618
00619
00620 if(item1)
00621 {
00622 iConsole->Printf(KItemPresentInPtrHashMap);
00623 }
00624 else
00625 {
00626 iConsole->Printf(KItemNotPresentInPtrHashMap);
00627 }
00628
00629
00630
00631 RPtrHashMap<TDesC16, TDesC16>::TIter ptrHashMapIter(ptrHashMap);
00632
00633 for ( ; ; )
00634 {
00635 const TDesC16* resNext = ptrHashMapIter.NextKey();
00636
00637
00638 if (!resNext)
00639 {
00640 break;
00641 }
00642 }
00643
00644 iConsole->Printf(KIterateItemsFromPtrHashMap);
00645
00646
00647 TInt res = ptrHashMap.Remove(&KRemoveItem);
00648
00649
00650 if(res)
00651 {
00652 iConsole->Printf(KItemPresentInPtrHashMap);
00653 }
00654 else
00655 {
00656 iConsole->Printf(KItemNotPresentInPtrHashMap);
00657 }
00658
00659 iConsole->Printf(KRemoveItemsFromPtrHashMap);
00660 iConsole->Printf(KExitMsg);
00661 iConsole->Getch();
00662
00663
00664 CleanupStack::PopAndDestroy(&ptrHashMap);
00665 }
00666
00667 void MainL()
00668 {
00669 CHashTableExample* app= CHashTableExample::NewL();
00670 CleanupStack::PushL(app);
00671
00672
00673 app->ConstructDefaultHashSet();
00674 app->ConstructOwnHashSet();
00675 app->OperationsToHashSetL();
00676
00677
00678 app->ConstructDefaultPtrHashSet();
00679 app->ConstructOwnPtrHashSet();
00680 app->OperationsToPtrHashSetL();
00681
00682
00683 app->ConstructDefaultHashMap();
00684 app->ConstructOwnHashMap();
00685 app->OperationsToHashMapL();
00686
00687
00688 app->ConstructDefaultPtrHashMap();
00689 app->ConstructOwnPtrHashMap();
00690 app->OperationsToPtrHashMapL();
00691
00692 CleanupStack::PopAndDestroy(app);
00693 }
00694
00695 GLDEF_C TInt E32Main()
00696 {
00697 __UHEAP_MARK;
00698
00699 CTrapCleanup* cleanup = CTrapCleanup::New();
00700 if(cleanup == NULL)
00701 {
00702 return KErrNoMemory;
00703 }
00704 TRAPD(err, MainL());
00705 if(err !=KErrNone)
00706 {
00707 User::Panic(KFailed, err);
00708 }
00709 delete cleanup;
00710
00711 __UHEAP_MARKEND;
00712 return KErrNone;
00713 }
00714
00715
00716
00717
00718
00719
00720