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