examples/Base/HashTableExample/hashtableexample.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: This example program demonstrates the use of hash table classes.  
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         // Construct hash set using default hash and identity functions for integer
00084         RHashSet<TInt> hashSetInt();
00085   
00086         // Construct hash set using default hash and identity functions for 8bit descriptor
00087         RHashSet<TDesC8> hashSetDes8();
00088   
00089         // Construct hash set using default hash and identity functions for 16bit descriptor
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         // Creates an object of custom hash function by using our created structure
00136         THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00137 
00138         // Creates an object of identity function by using our created structure
00139         TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);        
00140  
00141         // Construct hash set by providing custom hash and identity function
00142         RHashSet<TMyOwnObject> ownHashSet(ownHashFunction, ownIdentityFunction);
00143         iConsole->Printf(KConstructOwnHashSet);
00144         }
00145  
00156 void CHashTableExample::OperationsToHashSetL()  
00157         {
00158         // Declare a const integer value for the initial item to be stored
00159     const TInt startItem=1;
00160     
00161     // Declare a const integer value for the last item to be stored     
00162         const TInt endItem=100;
00163         TInt itemToBeFound=120;
00164         TInt itemToBeRemoved=200;
00165         TInt64 items=1;
00166   
00167         iConsole->Printf(KOperation);
00168         
00169         // Creates an object of hash set using the template class RHashSet 
00170         RHashSet<TInt> hashSet;
00171         
00172         // Push hash set on to the cleanup stack
00173         CleanupClosePushL(hashSet);
00174  
00175         // Insert random items to hash set
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         // Search the set for a specified item 
00185         TInt* result= hashSet.Find(itemToBeFound);
00186         
00187         // result is NULL if specified item is not found in the set
00188         if(result)
00189                 {
00190                 iConsole->Printf(KItemPresentInHashSet);
00191                 }
00192         else
00193                 {
00194                 iConsole->Printf(KItemNotPresentInHashSet);
00195                 }
00196  
00197         // Creates an object of TIter to iterate over the elements of hash set
00198         RHashSet<TInt>::TIter hashSetIter(hashSet);
00199   
00200         // Iterate over the items in the set
00201         // Creates an iterator object of type TIter
00202         for ( ; ;)
00203                 {
00204                 const TInt* res = hashSetIter.Next();
00205                 
00206                 // Next() moves the iterator to the next item and returns it
00207                 // Returns NULL if there are no more items 
00208                 if (!res)
00209                         {
00210                         break;
00211                         }
00212                 }
00213   
00214         iConsole->Printf(KIterateItemsFromHashSet);
00215 
00216         // Remove an item from hash set
00217         TInt res = hashSet.Remove(itemToBeRemoved);
00218         // Check if the item was successfully removed
00219         if(res)
00220                 {
00221                 iConsole->Printf(KItemPresentInHashSet);
00222                 }
00223         else
00224                 {
00225                 iConsole->Printf(KItemNotPresentInHashSet);
00226                 }
00227   
00228         iConsole->Printf(KRemoveItemsFromHashSet);
00229   
00230         // Close and cleanup hash set
00231         CleanupStack::PopAndDestroy(&hashSet);
00232         
00233         iConsole->Printf(KPressAKey);
00234         iConsole->Getch();
00235         }
00236   
00241 void CHashTableExample::ConstructDefaultPtrHashSet()
00242         {
00243         // Construct hash set of pointers using default hash and identity functions for integer
00244         RPtrHashSet<TInt> ptrHashSetInt();
00245   
00246         // Construct hash set of pointers using default hash and identity functions for 8bit descriptor
00247         RPtrHashSet<TDesC8> ptrHashSetDes8();
00248   
00249         // Construct hash set of pointers using default hash and identity functions for 16bit descriptor
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         // Creates an object of custom hash function by using our created structure
00264         THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00265  
00266         // Creates an object of identity relation by using our created structure
00267         TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);        
00268  
00269         // Construct hash set of pointers by providing custom hash and identity functions
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         // Converts the words if the number is less than 20
00290         if (number<20)
00291                 {
00292                 aDes.Copy(reinterpret_cast<const TUint16*> (numbers[number]));
00293                 }
00294     
00295         // Converts the words if the number between 20 and 100 
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         // Converts the words if the number is between 100 and 1000 
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         // Converts the words if the number is greater than or equal to 1000
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     // First, set up some sample data to store in the hash
00361     // We're going to use strings for the numbers 0 to 1999 
00362         
00363         // Populate a string array temporarily with the sample data
00364         // We'll use the array to populate the set, and later on use it also
00365         // to populate a map
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                 // FindNumberInWords gets a string representation of the specified integer
00374                 FindNumberInWords(i, buf);              
00375                 iPointerArray.AppendL(hbuf);
00376                 CleanupStack::Pop(hbuf);
00377                 }
00378         
00379         // Now create a set and populate it with the data from the array
00380 
00381         // Creates an object of hash set of pointers for 16 bit data using the template class RPtrHashSet
00382         RPtrHashSet<TDesC16> ptrHashSet;        
00383         // Push hash set of pointers on to the cleanup stack
00384         CleanupClosePushL(ptrHashSet);
00385         // Insert items to hash set of pointers from 16 bit descriptor array
00386         for (i=0; i<KMaxItem; ++i)
00387                 {
00388                 ptrHashSet.InsertL(iPointerArray[i]);
00389                 }  
00390         iConsole->Printf(KInsertItemsToPtrHashSet);
00391   
00392         // Search the set for a specified item 
00393         TDesC16* item1 = ptrHashSet.Find(KFindItem);
00394         // item1 is NULL if KFindItem is not found in the set
00395         if (item1)
00396                 {
00397                 iConsole->Printf(KItemPresentInPtrHashSet);
00398                 }
00399         else
00400                 {
00401                 iConsole->Printf(KItemNotPresentInPtrHashSet);
00402                 }       
00403  
00404         // Iterate over the items in the set
00405         // Creates an iterator object of type TIter
00406         RPtrHashSet<TDesC16>::TIter ptrHashSetIter(ptrHashSet);
00407         // Loop through the items 
00408         for ( ; ; )
00409                 {
00410                 const TDesC16* resNext = ptrHashSetIter.Next();
00411                 // Next() moves the iterator to the next item and returns it
00412                 // Returns NULL if there are no more items  
00413                 if (!resNext)
00414                         { 
00415                         break;
00416                         }
00417                 }
00418         iConsole->Printf(KIterateItemsFromPtrHashSet);
00419         
00420         // Remove an item from hash set
00421         TInt err = ptrHashSet.Remove(&KRemoveItem);
00422         // Check if the item was successfully removed
00423         if (err == KErrNone)
00424                 {
00425                 iConsole->Printf(KItemPresentInPtrHashSet);
00426                 }
00427         else
00428                 {
00429                 iConsole->Printf(KItemNotPresentInPtrHashSet);
00430                 }       
00431         iConsole->Printf(KRemoveItemsFromPtrHashSet);
00432   
00433         // Close and cleanup hash set of pointers
00434         CleanupStack::PopAndDestroy(&ptrHashSet);
00435  
00436         iConsole->Printf(KPressAKey);
00437         iConsole->Getch();
00438         }
00439  
00444 void CHashTableExample::ConstructDefaultHashMap()
00445         {
00446         // Construct hash map using default hash and identity functions for integer
00447         RHashMap<TInt, TInt> hashMapInt();
00448   
00449         // Construct hash map using default hash and identity functions for 8 bit descriptor
00450         RHashMap<TDesC8, TDesC8> hashMapDes8();
00451   
00452         // Construct hash map using default hash and identity functions for 16bit descriptor
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         // Creates an object of custom hash function by using our created structure
00467         THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00468  
00469         // Creates an object of identity function by using our created structure
00470         TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);
00471  
00472         // Construct hash map by providing custom hash and identity function    
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         // Creates an object of hash map using the template class RHashMap
00497         RHashMap<TInt, TInt> hashMap;
00498         
00499         // Push hash map on to the cleanup stack
00500         CleanupClosePushL(hashMap);
00501     
00502         // Insert items to hash map
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         // Search the map for a specified item
00512         TInt* result= hashMap.Find(itemToBeFound);
00513         
00514         // result is NULL if specified item is not found in the map
00515         if(result)
00516                 {
00517                 iConsole->Printf(KItemPresentInHashMap);
00518                 }
00519         else
00520                 {
00521                 iConsole->Printf(KItemNotPresentInHashMap);
00522                 }
00523   
00524         // Iterate over the items in the map
00525         // Creates an iterator object of type TIter
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         // Remove an item from hash map
00540         TInt res = hashMap.Remove(itemToBeRemoved);
00541         
00542         // Check if the item was successfully removed
00543         if(res)
00544                 {
00545                 iConsole->Printf(KItemPresentInHashMap);
00546                 }
00547         else
00548                 {
00549                 iConsole->Printf(KItemNotPresentInHashMap);
00550                 }
00551  
00552         iConsole->Printf(KRemoveItemsFromHashMap);
00553   
00554         // Close and cleanup hash map
00555         CleanupStack::PopAndDestroy(&hashMap);
00556   
00557         iConsole->Printf(KPressAKey);
00558         iConsole->Getch();
00559         }
00560 
00565 void CHashTableExample::ConstructDefaultPtrHashMap()
00566         {
00567         // Construct hash map of pointers using default hash and identity functions for integer
00568         RPtrHashMap<TInt, TInt> ptrHashMapInt();
00569   
00570         // Construct hash map of pointers using default hash and identity functions for 8bit descriptor
00571         RPtrHashMap<TDesC8, TDesC8> ptrHashMapDes8();
00572   
00573         // Construct hash map of pointers using default hash and identity functions for 16bit descriptor
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         // Creates an object of custom hash function by using our created structure
00588         THashFunction32<TMyOwnObject> ownHashFunction(MyHashFunction);
00589   
00590         // Creates an object of identity function by using our created structure
00591         TIdentityRelation<TMyOwnObject> ownIdentityFunction(MyIdentityFunction);
00592   
00593         // Construct hash map of pointers by providing custom hash and identity function
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         // Creates an object of hash map of pointers using the template class RPtrHashMap
00616         RPtrHashMap<TDesC16, TDesC16> ptrHashMap;
00617         
00618         // Push hash map of pointers on to the cleanup stack
00619         CleanupClosePushL(ptrHashMap);
00620    
00621         // Insert items to hash map of pointers 
00622         for (i=0; i<maxItem; ++i)
00623                 {
00624             ptrHashMap.InsertL(iPointerArray[i], iPointerArray[i+1]);
00625                 }
00626   
00627         iConsole->Printf(KInsertItemsToPtrHashMap);
00628   
00629         // Search the set for a specified item 
00630         TDesC16* item1= ptrHashMap.Find(KFindItem);
00631         
00632         // item1 is NULL if KFindItem is not found in the map
00633         if(item1)
00634                 {
00635                 iConsole->Printf(KItemPresentInPtrHashMap);
00636                 }
00637         else
00638                 {
00639                 iConsole->Printf(KItemNotPresentInPtrHashMap);
00640                 }
00641 
00642         // Iterate over the items in the map
00643         // Creates an iterator object of type TIter
00644         RPtrHashMap<TDesC16, TDesC16>::TIter ptrHashMapIter(ptrHashMap);
00645  
00646         for ( ; ; )
00647                 {
00648                 const TDesC16* resNext = ptrHashMapIter.NextKey();
00649                 // Next() moves the iterator to the next item and returns it
00650                 // Returns NULL if there are no more items 
00651                 if (!resNext)
00652                         { 
00653                         break;
00654                         }
00655                 }
00656         
00657         iConsole->Printf(KIterateItemsFromPtrHashMap);
00658 
00659         // Remove an item from hash map of pointers 
00660     TInt res = ptrHashMap.Remove(&KRemoveItem);
00661     
00662     // Check if the item was successfully removed
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         // Close and Cleanup hash map of pointers
00677         CleanupStack::PopAndDestroy(&ptrHashMap);
00678         }
00679  
00680 void MainL()
00681         {
00682         CHashTableExample* app= CHashTableExample::NewL();
00683         CleanupStack::PushL(app);
00684  
00685         // Hash set 
00686         app->ConstructDefaultHashSet();
00687         app->ConstructOwnHashSet();
00688         app->OperationsToHashSetL();
00689   
00690         // Hash set of pointers
00691         app->ConstructDefaultPtrHashSet();
00692         app->ConstructOwnPtrHashSet();
00693         app->OperationsToPtrHashSetL();
00694   
00695         // Hash map
00696         app->ConstructDefaultHashMap();
00697         app->ConstructOwnHashMap();
00698         app->OperationsToHashMapL();
00699  
00700         // Hash map of pointers
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  

Generated by  doxygen 1.6.2