libraries/ltkutils/tsrc/tstringhash.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // tstringhash.cpp
       
     2 // 
       
     3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <fshell/ioutils.h>
       
    14 #include <fshell/stringhash.h>
       
    15 
       
    16 using namespace IoUtils;
       
    17 using LtkUtils::RStringHash;
       
    18 using LtkUtils::TStringHashIter;
       
    19 
       
    20 class CCmdTstringhash : public CCommandBase
       
    21 	{
       
    22 public:
       
    23 	static CCommandBase* NewLC();
       
    24 	~CCmdTstringhash();
       
    25 private:
       
    26 	CCmdTstringhash();
       
    27 private: // From CCommandBase.
       
    28 	virtual const TDesC& Name() const;
       
    29 	virtual const TDesC& Description() const;
       
    30 	virtual void DoRunL();
       
    31 	virtual void ArgumentsL(RCommandArgumentList& aArguments);
       
    32 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    33 private:
       
    34 	};
       
    35 
       
    36 
       
    37 CCommandBase* CCmdTstringhash::NewLC()
       
    38 	{
       
    39 	CCmdTstringhash* self = new(ELeave) CCmdTstringhash();
       
    40 	CleanupStack::PushL(self);
       
    41 	self->BaseConstructL();
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 CCmdTstringhash::~CCmdTstringhash()
       
    46 	{
       
    47 	}
       
    48 
       
    49 CCmdTstringhash::CCmdTstringhash()
       
    50 	{
       
    51 	}
       
    52 
       
    53 const TDesC& CCmdTstringhash::Name() const
       
    54 	{
       
    55 	_LIT(KName, "tstringhash");	
       
    56 	return KName;
       
    57 	}
       
    58 
       
    59 const TDesC& CCmdTstringhash::Description() const
       
    60 	{
       
    61 	// ToDo: Write description text.
       
    62 	_LIT(KDescription, "To do.");
       
    63 	return KDescription;
       
    64 	}
       
    65 
       
    66 void CCmdTstringhash::ArgumentsL(RCommandArgumentList&)
       
    67 	{
       
    68 	}
       
    69 
       
    70 void CCmdTstringhash::OptionsL(RCommandOptionList&)
       
    71 	{
       
    72 	}
       
    73 
       
    74 
       
    75 EXE_BOILER_PLATE(CCmdTstringhash)
       
    76 
       
    77 void CCmdTstringhash::DoRunL()
       
    78 	{
       
    79 	RStringHash<TInt> hash;
       
    80 	CleanupClosePushL(hash);
       
    81 
       
    82 	TInt err = hash.Insert(_L("test"), 1);
       
    83 
       
    84 	LeaveIfErr(err, _L("Failed to insert item"));
       
    85 
       
    86 	TInt* res = hash.Find(_L("test"));
       
    87 
       
    88 	if (!res) LeaveIfErr(KErrGeneral, _L("Failed to find item"));
       
    89 	if (*res != 1) LeaveIfErr(KErrGeneral, _L("item returned %d instead of 1"), *res);
       
    90 
       
    91 	TInt n = 100;
       
    92 	while (n--)
       
    93 		{
       
    94 		hash.InsertL(_L("test"), n);
       
    95 		}
       
    96 
       
    97 	hash.ReserveL(100);
       
    98 
       
    99 	hash.InsertL(_L("a"), 6);
       
   100 	hash.InsertL(_L("b"), 6);
       
   101 	hash.InsertL(_L("aasdfasdf"), 66666666);
       
   102 	hash.InsertL(_L("lalala"), 66666);
       
   103 	hash.InsertL(_L("qqq"), 666);
       
   104 	hash.InsertL(_L(""), 0);
       
   105 	hash.InsertL(_L("12345"), 54321);
       
   106 
       
   107 	TStringHashIter<TInt> iter(hash);
       
   108 	while (iter.NextValue() != NULL)
       
   109 		{
       
   110 		const TDesC* k = iter.CurrentKey();
       
   111 		const TInt* v = iter.CurrentValue();
       
   112 		Printf(_L("Key %S has value %d\r\n"), k, *v);
       
   113 		}
       
   114 
       
   115 	CleanupStack::PopAndDestroy(&hash);
       
   116 	}
       
   117 
       
   118