examples/Base/HashTableExample/hashtableexample.cpp

Go to the documentation of this file.
00001 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // License: "Symbian Foundation License v1.0" to Symbian Foundation
00004 // members and "Symbian Foundation End User License Agreement v1.0"
00005 // to non-members at the URL
00006 // "http://www.symbianfoundation.org/legal/licencesv10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // This example program demonstrates the use of hash table classes.
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         // Construct hash set using default hash and identity functions for integer
00071         RHashSet<TInt> hashSetInt();
00072   
00073         // Construct hash set using default hash and identity functions for 8bit descriptor
00074         RHashSet<TDesC8> hashSetDes8();
00075   
00076         // Construct hash set using default hash and identity functions for 16bit descriptor
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         // Creates an object of custom hash function by using our created structure
00123         THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00124 
00125         // Creates an object of identity function by using our created structure
00126         TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);        
00127  
00128         // Construct hash set by providing custom hash and identity function
00129         RHashSet<TMyOwnObject> ownHashSet(ownHashFunction, ownIdentityFunction);
00130         iConsole->Printf(KConstructOwnHashSet);
00131         }
00132  
00143 void CHashTableExample::OperationsToHashSetL()  
00144         {
00145         // Declare a const integer value for the initial item to be stored
00146     const TInt startItem=1;
00147     
00148     // Declare a const integer value for the last item to be stored     
00149         const TInt endItem=100;
00150         TInt itemToBeFound=120;
00151         TInt itemToBeRemoved=200;
00152         TInt64 items=1;
00153   
00154         iConsole->Printf(KOperation);
00155         
00156         // Creates an object of hash set using the template class RHashSet 
00157         RHashSet<TInt> hashSet;
00158         
00159         // Push hash set on to the cleanup stack
00160         CleanupClosePushL(hashSet);
00161  
00162         // Insert random items to hash set
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         // Search the set for a specified item 
00172         TInt* result= hashSet.Find(itemToBeFound);
00173         
00174         // result is NULL if specified item is not found in the set
00175         if(result)
00176                 {
00177                 iConsole->Printf(KItemPresentInHashSet);
00178                 }
00179         else
00180                 {
00181                 iConsole->Printf(KItemNotPresentInHashSet);
00182                 }
00183  
00184         // Creates an object of TIter to iterate over the elements of hash set
00185         RHashSet<TInt>::TIter hashSetIter(hashSet);
00186   
00187         // Iterate over the items in the set
00188         // Creates an iterator object of type TIter
00189         for ( ; ;)
00190                 {
00191                 const TInt* res = hashSetIter.Next();
00192                 
00193                 // Next() moves the iterator to the next item and returns it
00194                 // Returns NULL if there are no more items 
00195                 if (!res)
00196                         {
00197                         break;
00198                         }
00199                 }
00200   
00201         iConsole->Printf(KIterateItemsFromHashSet);
00202 
00203         // Remove an item from hash set
00204         TInt res = hashSet.Remove(itemToBeRemoved);
00205         // Check if the item was successfully removed
00206         if(res)
00207                 {
00208                 iConsole->Printf(KItemPresentInHashSet);
00209                 }
00210         else
00211                 {
00212                 iConsole->Printf(KItemNotPresentInHashSet);
00213                 }
00214   
00215         iConsole->Printf(KRemoveItemsFromHashSet);
00216   
00217         // Close and cleanup hash set
00218         CleanupStack::PopAndDestroy(&hashSet);
00219         
00220         iConsole->Printf(KPressAKey);
00221         iConsole->Getch();
00222         }
00223   
00228 void CHashTableExample::ConstructDefaultPtrHashSet()
00229         {
00230         // Construct hash set of pointers using default hash and identity functions for integer
00231         RPtrHashSet<TInt> ptrHashSetInt();
00232   
00233         // Construct hash set of pointers using default hash and identity functions for 8bit descriptor
00234         RPtrHashSet<TDesC8> ptrHashSetDes8();
00235   
00236         // Construct hash set of pointers using default hash and identity functions for 16bit descriptor
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         // Creates an object of custom hash function by using our created structure
00251         THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00252  
00253         // Creates an object of identity relation by using our created structure
00254         TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);        
00255  
00256         // Construct hash set of pointers by providing custom hash and identity functions
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         // Converts the words if the number is less than 20
00277         if (number<20)
00278                 {
00279                 aDes.Copy(reinterpret_cast<const TUint16*> (numbers[number]));
00280                 }
00281     
00282         // Converts the words if the number between 20 and 100 
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         // Converts the words if the number is between 100 and 1000 
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         // Converts the words if the number is greater than or equal to 1000
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     // First, set up some sample data to store in the hash
00348     // We're going to use strings for the numbers 0 to 1999 
00349         
00350         // Populate a string array temporarily with the sample data
00351         // We'll use the array to populate the set, and later on use it also
00352         // to populate a map
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                 // FindNumberInWords gets a string representation of the specified integer
00361                 FindNumberInWords(i, buf);              
00362                 iPointerArray.AppendL(hbuf);
00363                 CleanupStack::Pop(hbuf);
00364                 }
00365         
00366         // Now create a set and populate it with the data from the array
00367 
00368         // Creates an object of hash set of pointers for 16 bit data using the template class RPtrHashSet
00369         RPtrHashSet<TDesC16> ptrHashSet;        
00370         // Push hash set of pointers on to the cleanup stack
00371         CleanupClosePushL(ptrHashSet);
00372         // Insert items to hash set of pointers from 16 bit descriptor array
00373         for (i=0; i<KMaxItem; ++i)
00374                 {
00375                 ptrHashSet.InsertL(iPointerArray[i]);
00376                 }  
00377         iConsole->Printf(KInsertItemsToPtrHashSet);
00378   
00379         // Search the set for a specified item 
00380         TDesC16* item1 = ptrHashSet.Find(KFindItem);
00381         // item1 is NULL if KFindItem is not found in the set
00382         if (item1)
00383                 {
00384                 iConsole->Printf(KItemPresentInPtrHashSet);
00385                 }
00386         else
00387                 {
00388                 iConsole->Printf(KItemNotPresentInPtrHashSet);
00389                 }       
00390  
00391         // Iterate over the items in the set
00392         // Creates an iterator object of type TIter
00393         RPtrHashSet<TDesC16>::TIter ptrHashSetIter(ptrHashSet);
00394         // Loop through the items 
00395         for ( ; ; )
00396                 {
00397                 const TDesC16* resNext = ptrHashSetIter.Next();
00398                 // Next() moves the iterator to the next item and returns it
00399                 // Returns NULL if there are no more items  
00400                 if (!resNext)
00401                         { 
00402                         break;
00403                         }
00404                 }
00405         iConsole->Printf(KIterateItemsFromPtrHashSet);
00406         
00407         // Remove an item from hash set
00408         TInt err = ptrHashSet.Remove(&KRemoveItem);
00409         // Check if the item was successfully removed
00410         if (err == KErrNone)
00411                 {
00412                 iConsole->Printf(KItemPresentInPtrHashSet);
00413                 }
00414         else
00415                 {
00416                 iConsole->Printf(KItemNotPresentInPtrHashSet);
00417                 }       
00418         iConsole->Printf(KRemoveItemsFromPtrHashSet);
00419   
00420         // Close and cleanup hash set of pointers
00421         CleanupStack::PopAndDestroy(&ptrHashSet);
00422  
00423         iConsole->Printf(KPressAKey);
00424         iConsole->Getch();
00425         }
00426  
00431 void CHashTableExample::ConstructDefaultHashMap()
00432         {
00433         // Construct hash map using default hash and identity functions for integer
00434         RHashMap<TInt, TInt> hashMapInt();
00435   
00436         // Construct hash map using default hash and identity functions for 8 bit descriptor
00437         RHashMap<TDesC8, TDesC8> hashMapDes8();
00438   
00439         // Construct hash map using default hash and identity functions for 16bit descriptor
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         // Creates an object of custom hash function by using our created structure
00454         THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00455  
00456         // Creates an object of identity function by using our created structure
00457         TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);
00458  
00459         // Construct hash map by providing custom hash and identity function    
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         // Creates an object of hash map using the template class RHashMap
00484         RHashMap<TInt, TInt> hashMap;
00485         
00486         // Push hash map on to the cleanup stack
00487         CleanupClosePushL(hashMap);
00488     
00489         // Insert items to hash map
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         // Search the map for a specified item
00499         TInt* result= hashMap.Find(itemToBeFound);
00500         
00501         // result is NULL if specified item is not found in the map
00502         if(result)
00503                 {
00504                 iConsole->Printf(KItemPresentInHashMap);
00505                 }
00506         else
00507                 {
00508                 iConsole->Printf(KItemNotPresentInHashMap);
00509                 }
00510   
00511         // Iterate over the items in the map
00512         // Creates an iterator object of type TIter
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         // Remove an item from hash map
00527         TInt res = hashMap.Remove(itemToBeRemoved);
00528         
00529         // Check if the item was successfully removed
00530         if(res)
00531                 {
00532                 iConsole->Printf(KItemPresentInHashMap);
00533                 }
00534         else
00535                 {
00536                 iConsole->Printf(KItemNotPresentInHashMap);
00537                 }
00538  
00539         iConsole->Printf(KRemoveItemsFromHashMap);
00540   
00541         // Close and cleanup hash map
00542         CleanupStack::PopAndDestroy(&hashMap);
00543   
00544         iConsole->Printf(KPressAKey);
00545         iConsole->Getch();
00546         }
00547 
00552 void CHashTableExample::ConstructDefaultPtrHashMap()
00553         {
00554         // Construct hash map of pointers using default hash and identity functions for integer
00555         RPtrHashMap<TInt, TInt> ptrHashMapInt();
00556   
00557         // Construct hash map of pointers using default hash and identity functions for 8bit descriptor
00558         RPtrHashMap<TDesC8, TDesC8> ptrHashMapDes8();
00559   
00560         // Construct hash map of pointers using default hash and identity functions for 16bit descriptor
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         // Creates an object of custom hash function by using our created structure
00575         THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00576   
00577         // Creates an object of identity function by using our created structure
00578         TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);
00579   
00580         // Construct hash map of pointers by providing custom hash and identity function
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         // Creates an object of hash map of pointers using the template class RPtrHashMap
00603         RPtrHashMap<TDesC16, TDesC16> ptrHashMap;
00604         
00605         // Push hash map of pointers on to the cleanup stack
00606         CleanupClosePushL(ptrHashMap);
00607    
00608         // Insert items to hash map of pointers 
00609         for (i=0; i<maxItem; ++i)
00610                 {
00611             ptrHashMap.InsertL(iPointerArray[i], iPointerArray[i+1]);
00612                 }
00613   
00614         iConsole->Printf(KInsertItemsToPtrHashMap);
00615   
00616         // Search the set for a specified item 
00617         TDesC16* item1= ptrHashMap.Find(KFindItem);
00618         
00619         // item1 is NULL if KFindItem is not found in the map
00620         if(item1)
00621                 {
00622                 iConsole->Printf(KItemPresentInPtrHashMap);
00623                 }
00624         else
00625                 {
00626                 iConsole->Printf(KItemNotPresentInPtrHashMap);
00627                 }
00628 
00629         // Iterate over the items in the map
00630         // Creates an iterator object of type TIter
00631         RPtrHashMap<TDesC16, TDesC16>::TIter ptrHashMapIter(ptrHashMap);
00632  
00633         for ( ; ; )
00634                 {
00635                 const TDesC16* resNext = ptrHashMapIter.NextKey();
00636                 // Next() moves the iterator to the next item and returns it
00637                 // Returns NULL if there are no more items 
00638                 if (!resNext)
00639                         { 
00640                         break;
00641                         }
00642                 }
00643         
00644         iConsole->Printf(KIterateItemsFromPtrHashMap);
00645 
00646         // Remove an item from hash map of pointers 
00647     TInt res = ptrHashMap.Remove(&KRemoveItem);
00648     
00649     // Check if the item was successfully removed
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         // Close and Cleanup hash map of pointers
00664         CleanupStack::PopAndDestroy(&ptrHashMap);
00665         }
00666  
00667 void MainL()
00668         {
00669         CHashTableExample* app= CHashTableExample::NewL();
00670         CleanupStack::PushL(app);
00671  
00672         // Hash set 
00673         app->ConstructDefaultHashSet();
00674         app->ConstructOwnHashSet();
00675         app->OperationsToHashSetL();
00676   
00677         // Hash set of pointers
00678         app->ConstructDefaultPtrHashSet();
00679         app->ConstructOwnPtrHashSet();
00680         app->OperationsToPtrHashSetL();
00681   
00682         // Hash map
00683         app->ConstructDefaultHashMap();
00684         app->ConstructOwnHashMap();
00685         app->OperationsToHashMapL();
00686  
00687         // Hash map of pointers
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  

Generated by  doxygen 1.6.2