predictivesearch/PcsAlgorithm/Algorithm1/src/CPcsAlgorithm1MultiSearchHelper.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Supports initial search feature. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "CPcsAlgorithm1MultiSearchHelper.h"
       
    21 #include "CPcsAlgorithm1Utils.h"
       
    22 #include "CPcsDefs.h"
       
    23 #include <collate.h>
       
    24 #include <biditext.h>
       
    25 
       
    26 
       
    27 #ifdef _DEBUG
       
    28 _LIT( KPanicMultiSearchHelper, "CPcsAlgorithm1MultiSearchHelper" );
       
    29 static void Panic( TInt aReason )
       
    30     {
       
    31     User::Panic( KPanicMultiSearchHelper, aReason );
       
    32     }
       
    33 #endif
       
    34 
       
    35 
       
    36 // ============================== MEMBER FUNCTIONS ============================
       
    37 
       
    38 // ----------------------------------------------------------------------------
       
    39 // CPcsAlgorithm1MultiSearchHelper::NewL
       
    40 // Two Phase Construction
       
    41 // ----------------------------------------------------------------------------
       
    42 CPcsAlgorithm1MultiSearchHelper* CPcsAlgorithm1MultiSearchHelper::NewL(CPcsAlgorithm1* aAlgorithm)
       
    43 {
       
    44     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::NewL") );
       
    45     
       
    46 	CPcsAlgorithm1MultiSearchHelper* self = new ( ELeave ) CPcsAlgorithm1MultiSearchHelper();
       
    47 	CleanupStack::PushL( self );
       
    48 	self->ConstructL(aAlgorithm);
       
    49 	CleanupStack::Pop( self );
       
    50 
       
    51     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::NewL") );
       
    52     
       
    53 	return self;
       
    54 }
       
    55 
       
    56 // ----------------------------------------------------------------------------
       
    57 // CPcsAlgorithm1MultiSearchHelper::CPcsAlgorithm1MultiSearchHelper
       
    58 // Two Phase Construction
       
    59 // ----------------------------------------------------------------------------
       
    60 CPcsAlgorithm1MultiSearchHelper::CPcsAlgorithm1MultiSearchHelper()
       
    61 {		
       
    62     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::CPcsAlgorithm1") );
       
    63     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::CPcsAlgorithm1") );
       
    64 }
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 // CPcsAlgorithm1MultiSearchHelper::ConstructL
       
    68 // Two Phase Construction
       
    69 // ----------------------------------------------------------------------------
       
    70 void CPcsAlgorithm1MultiSearchHelper::ConstructL(CPcsAlgorithm1* aAlgorithm)
       
    71 {
       
    72     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::ConstructL") );
       
    73     
       
    74     iAlgorithm = aAlgorithm;
       
    75     iKeyMap = iAlgorithm->GetKeyMap();
       
    76     
       
    77     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::ConstructL") );
       
    78 } 	
       
    79 	
       
    80 // ----------------------------------------------------------------------------
       
    81 // CPcsAlgorithm1MultiSearchHelper::CPcsAlgorithm1MultiSearchHelper
       
    82 // Destructor
       
    83 // ----------------------------------------------------------------------------
       
    84 CPcsAlgorithm1MultiSearchHelper::~CPcsAlgorithm1MultiSearchHelper()
       
    85 {
       
    86     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::~CPcsAlgorithm1MultiSearchHelper") );        
       
    87     iMultiSearchResultsArr.ResetAndDestroy();          
       
    88     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::~CPcsAlgorithm1MultiSearchHelper") );
       
    89 }
       
    90 
       
    91 // ----------------------------------------------------------------------------
       
    92 // CPcsAlgorithm1MultiSearchHelper::SearchMultiL
       
    93 // Initial search feature.
       
    94 // Flow of steps in initial search is explained below.
       
    95 // (1) Extract the pool elements corresponding to one of the queries.
       
    96 // (2) Always the first query is used here.
       
    97 // (3) Get pool elements from all caches corresponding to the data stores.
       
    98 // (4) Convert each query to mode specific form.
       
    99 // (5) Parse each data element.
       
   100 // (6) Check for every search query atleast one data element matches.
       
   101 // (7) If NOT ignore the result.
       
   102 // (8) If so perform an additional check that number of data matches is
       
   103 //     atleast equal to number of search queries. This will ensure that same
       
   104 //     data element has not matched for multiple queries.
       
   105 // (9) Now include the element in the result.
       
   106 // ----------------------------------------------------------------------------
       
   107 void  CPcsAlgorithm1MultiSearchHelper::SearchMultiL(const CPsSettings& aSettings,
       
   108 	                                     RPointerArray<CPsQuery>& aPsQuery,
       
   109 	                                     TBool aIsSearchInGroup,
       
   110 	                                     RArray<TInt>& aContactsInGroup,
       
   111 	                                     RPointerArray<CPsData>& aSearchResults,
       
   112 	                                     RPointerArray<CPsPattern>& aSearchSeqs )
       
   113 {
       
   114     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
       
   115 
       
   116     __LATENCY_MARK ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
       
   117    
       
   118     PRINTQUERYLIST ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL: "), aPsQuery );
       
   119 
       
   120     // Create CPcsAlgorithm1FilterHelper object to be used for filtering the results
       
   121 	TSortType sortType = aSettings.GetSortType();
       
   122     CPcsAlgorithm1FilterHelper* filterHelper = CPcsAlgorithm1FilterHelper::NewL(sortType);
       
   123     CleanupStack::PushL( filterHelper );
       
   124     RPointerArray<CPcsPoolElement> elements;
       
   125     CleanupClosePushL( elements );
       
   126     
       
   127     iMultiSearchResultsArr.ResetAndDestroy();
       
   128     
       
   129     // Get the data stores  
       
   130     RPointerArray<TDesC> dataStores;
       
   131     CleanupResetAndDestroyPushL( dataStores );
       
   132     aSettings.SearchUrisL(dataStores);
       
   133           
       
   134     // Get the required display fields from the client
       
   135     RArray<TInt> requiredDataFields;
       
   136     CleanupClosePushL( requiredDataFields );
       
   137     aSettings.DisplayFieldsL(requiredDataFields);
       
   138 
       
   139     // Search from cache based on first character of 1st item in query list
       
   140     const CPsQueryItem& firstCharItem = aPsQuery[0]->GetItemAtL(0);
       
   141     TInt cachePoolId = iKeyMap->PoolIdForCharacter( firstCharItem.Character(), firstCharItem.Mode() );
       
   142 
       
   143     // Get the elements from all the databases
       
   144     for ( TInt dsIndex = 0; 
       
   145 			dsIndex < dataStores.Count(); 
       
   146 			dsIndex++ )
       
   147 	{
       
   148 	    RPointerArray<CPsData> *temp = new (ELeave) RPointerArray<CPsData> ();
       
   149 	    iMultiSearchResultsArr.Append(temp);
       
   150 	    
       
   151 	    // Get the contents for this data store
       
   152 	    TInt arrayIndex = iAlgorithm->GetCacheIndex(*(dataStores[dsIndex]));
       
   153 		if ( arrayIndex < 0 ) continue;
       
   154 		CPcsCache* cache = iAlgorithm->GetCache(arrayIndex);
       
   155 	    cache->GetContactsForKeyL(cachePoolId, elements);
       
   156 		
       
   157 	    // Get the supported data fields for this data store
       
   158         RArray<TInt> supportedDataFields;
       
   159         CleanupClosePushL( supportedDataFields );
       
   160 		cache->GetDataFields(supportedDataFields);
       
   161 		
       
   162 		// Get the filtered data fields for this data store
       
   163         TUint8 filteredDataMatch = CPcsAlgorithm1Utils::FilterDataFieldsL(requiredDataFields,
       
   164                                                                           supportedDataFields);
       
   165 
       
   166 	    // Filter the results now
       
   167 	    FilterResultsMultiL(filterHelper,
       
   168 	    			   elements, 
       
   169 		               aPsQuery, 
       
   170 		               filteredDataMatch,
       
   171 		               aIsSearchInGroup,
       
   172 		               aContactsInGroup);
       
   173 	    
       
   174 		// If alphabetical sorting, get the results for this datastore               
       
   175 		if ( sortType == EAlphabetical )
       
   176 		{
       
   177 			filterHelper->GetResults(*(iMultiSearchResultsArr[dsIndex]));
       
   178 		}
       
   179 		
       
   180 	    elements.Reset();
       
   181 	    CleanupStack::PopAndDestroy( &supportedDataFields ); // Close
       
   182 	}
       
   183     CleanupStack::PopAndDestroy( &requiredDataFields ); // Close
       
   184     CleanupStack::PopAndDestroy( &dataStores );         // ResetAndDestroy
       
   185 	
       
   186 	// If alphabetical sorting, merge the result sets of all datastores
       
   187   	if ( sortType == EAlphabetical )
       
   188   	{
       
   189   		// Form the complete searchResults array
       
   190     	CPcsAlgorithm1Utils::FormCompleteSearchResultsL(iMultiSearchResultsArr,
       
   191     											   	    aSearchResults);
       
   192   	}
       
   193   	else
       
   194   	{
       
   195   		// Results are already sorted patternbased
       
   196   		filterHelper->GetResults(aSearchResults);
       
   197   	}
       
   198   
       
   199   	// Get the sorted match sequence list
       
   200 	filterHelper->GetPatternsL(aSearchSeqs);
       
   201 	
       
   202 	PRINT1 ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL: Number of search results = %d"), aSearchResults.Count() );
       
   203              
       
   204 	// Cleanup             
       
   205     for(TInt i = 0; i < iMultiSearchResultsArr.Count(); i++)
       
   206     {
       
   207     	iMultiSearchResultsArr[i]->Reset();
       
   208     	delete iMultiSearchResultsArr[i];
       
   209         iMultiSearchResultsArr[i] = NULL;
       
   210     }
       
   211     
       
   212     iMultiSearchResultsArr.Reset();
       
   213     CleanupStack::PopAndDestroy( &elements ); // Close
       
   214     CleanupStack::PopAndDestroy( filterHelper );
       
   215    
       
   216     __LATENCY_MARKEND ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
       
   217 
       
   218     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
       
   219 }
       
   220 
       
   221 // ----------------------------------------------------------------------------
       
   222 // CPcsAlgorithm1MultiSearchHelper::SearchMatchSeqMultiL
       
   223 // Function adds matches, and locations based on multi query, and data
       
   224 // Duplicate locations are allowed (as they are removed later anyway)
       
   225 // Post condition locations in index order
       
   226 // ----------------------------------------------------------------------------
       
   227 void  CPcsAlgorithm1MultiSearchHelper::SearchMatchSeqMultiL(RPointerArray<CPsQuery>& aPsQuery,
       
   228                                                             const TDesC& aData,
       
   229                                                             RPointerArray<TDesC>& aMatchSeq,
       
   230                                                             RArray<TPsMatchLocation>& aMatchLocation )
       
   231 {
       
   232     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::SearchMatchSeqMultiL") );
       
   233     RPointerArray<HBufC> descriptorsQueryList;
       
   234     CleanupResetAndDestroyPushL( descriptorsQueryList );
       
   235     ConvertQueryToListL(aPsQuery, descriptorsQueryList);
       
   236     TLex lex(aData);
       
   237     while ( !lex.Eos() ) // Search thru all words
       
   238     {
       
   239         TPtrC currentWord = lex.NextToken(); // next word
       
   240 
       
   241         TPsMatchLocation newLocation = { lex.Offset() - currentWord.Length(), //start index
       
   242                                          0, TBidiText::TextDirectionality(currentWord) };
       
   243 
       
   244         for ( TInt queryIndex = 0; queryIndex < aPsQuery.Count(); ++queryIndex )
       
   245         {
       
   246             HBufC* currentQuery = descriptorsQueryList[queryIndex];
       
   247             TBuf<KPsQueryMaxLen> convertedWord;
       
   248 
       
   249             // Convert the data to required form (mode specific)
       
   250             iKeyMap->GetMixedKeyStringForDataL(*aPsQuery[queryIndex], currentWord, convertedWord);
       
   251 
       
   252             if ( CPcsAlgorithm1Utils::MyCompareKeyAndString(convertedWord, *currentQuery, *aPsQuery[queryIndex]) )
       
   253             {
       
   254                 newLocation.length = currentQuery->Length();
       
   255                 aMatchLocation.AppendL( newLocation );
       
   256                 TPtrC matchPart = currentWord.Left(newLocation.length);
       
   257                 CPcsAlgorithm1Utils::AppendMatchToSeqL( aMatchSeq, matchPart );
       
   258             }
       
   259         }
       
   260     }
       
   261     CleanupStack::PopAndDestroy( &descriptorsQueryList ); // ResetAndDestroy
       
   262     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::SearchMatchSeqMultiL") );
       
   263 }
       
   264 
       
   265 // ----------------------------------------------------------------------------
       
   266 // CPcsAlgorithm1MultiSearchHelper::LookupMatchL
       
   267 // ----------------------------------------------------------------------------
       
   268 void CPcsAlgorithm1MultiSearchHelper::LookupMatchL( CPsQuery& aSearchQuery,
       
   269         const TDesC& aData,
       
   270         TDes& aMatchedData )
       
   271 {
       
   272     RPointerArray<CPsQuery> queryList = MultiQueryL(aSearchQuery);
       
   273     TBuf<KPsQueryMaxLen> queryAsString = aSearchQuery.QueryAsStringLC();
       
   274     TBuf<KPsQueryMaxLen> convertedQuery;
       
   275     iKeyMap->GetMixedKeyStringForDataL( aSearchQuery, queryAsString, convertedQuery );
       
   276     
       
   277     RArray<TInt> dataWordIndexes;
       
   278     RArray<TInt> dataWordLengths;
       
   279     TLex lex( aData );
       
   280     while ( !lex.Eos() )
       
   281     {
       
   282         TPtrC currentWord = lex.NextToken();
       
   283         PRINT2( _L("idx len: %d %d"), lex.Offset() - currentWord.Length(), currentWord.Length() );
       
   284         dataWordIndexes.AppendL( lex.Offset() - currentWord.Length() );
       
   285         dataWordLengths.AppendL( currentWord.Length() );
       
   286     }
       
   287 
       
   288     RArray<TInt> queryIndexes;
       
   289     RArray<TPtrC> convertedQueriesAsDes;
       
   290     lex.Assign( queryAsString );
       
   291     while ( !lex.Eos() )
       
   292     {
       
   293         TPtrC currentWord = lex.NextToken();
       
   294         convertedQueriesAsDes.AppendL( 
       
   295                 convertedQuery.Mid( lex.Offset() - currentWord.Length(), currentWord.Length()) );
       
   296         queryIndexes.AppendL( lex.Offset() - currentWord.Length() );
       
   297     }
       
   298     
       
   299     RPointerArray< RArray<TBool> > possibleMatches;
       
   300     for ( TInt i(0); i < queryList.Count(); ++i )
       
   301     {
       
   302         RArray<TBool>* matchesForCurrentQuery = new (ELeave) RArray<TBool>;
       
   303         possibleMatches.AppendL( matchesForCurrentQuery );
       
   304         CPsQuery* currentQuery = queryList[i];
       
   305         for ( TInt j(0); j < dataWordIndexes.Count(); j++ )
       
   306         {
       
   307             TPtrC currentDataWord = aData.Mid( dataWordIndexes[j], dataWordLengths[j] );
       
   308             RBuf convertedDataWord;
       
   309             convertedDataWord.CreateL( currentDataWord.Length() );
       
   310             CleanupClosePushL( convertedDataWord );
       
   311 
       
   312             iKeyMap->GetMixedKeyStringForDataL( *currentQuery, currentDataWord.Left(currentQuery->Count()), convertedDataWord );
       
   313 
       
   314             if ( CPcsAlgorithm1Utils::MyCompareKeyAndString(convertedQueriesAsDes[i], convertedDataWord, *currentQuery) )
       
   315             {
       
   316                 matchesForCurrentQuery->AppendL( ETrue );
       
   317             }
       
   318             else
       
   319             {
       
   320                 matchesForCurrentQuery->AppendL( EFalse );
       
   321             }
       
   322             PRINT3( _L("CPcsAlgorithm1MultiSearchHelper::LookupMatchL: possibleMatches[%d][%d]=%d"),i,j, (*(possibleMatches[i]))[j] )
       
   323             CleanupStack::PopAndDestroy( &convertedDataWord );
       
   324         }
       
   325     }
       
   326     
       
   327     const TInt KUnapplied(-1);
       
   328     RArray<TInt> appliedMatches;
       
   329     appliedMatches.ReserveL( queryList.Count() );
       
   330     for ( TInt i(0); i < queryList.Count(); ++i )
       
   331     {
       
   332         appliedMatches.AppendL( KUnapplied );
       
   333     }
       
   334     
       
   335     //backtrack algorithm starts here to find fully applied match
       
   336     TInt currentQueryIndex(0);
       
   337     while ( currentQueryIndex < queryList.Count() && currentQueryIndex >= 0 )
       
   338     {
       
   339         TInt currentDataIndex = appliedMatches[ currentQueryIndex ] + 1;
       
   340         appliedMatches[ currentQueryIndex ] = KUnapplied;
       
   341         RArray<TBool>& matchesForCurrentQuery = *(possibleMatches[currentQueryIndex]);
       
   342         TBool doBacktrack( ETrue );
       
   343         while ( currentDataIndex < dataWordIndexes.Count() )
       
   344         {
       
   345             PRINT2(_L("CPcsAlgorithm1MultiSearchHelper::LookupMatchL: matchesForCurrentQuery[%d] = %d"), 
       
   346                     currentDataIndex, matchesForCurrentQuery[ currentDataIndex ] );
       
   347             
       
   348             if ( matchesForCurrentQuery[ currentDataIndex ] 
       
   349                      && (appliedMatches.Find( currentDataIndex ) == KErrNotFound) )
       
   350             {
       
   351                 appliedMatches[ currentQueryIndex ] = currentDataIndex;
       
   352                 doBacktrack = EFalse;
       
   353                 break;
       
   354             }
       
   355             ++currentDataIndex;
       
   356         }
       
   357         if ( doBacktrack )
       
   358         {
       
   359             --currentQueryIndex;
       
   360         }
       
   361         else
       
   362         {
       
   363             ++currentQueryIndex;
       
   364         }
       
   365     }
       
   366     
       
   367     if ( currentQueryIndex >= 0 ) //found
       
   368     {
       
   369         aMatchedData = queryAsString;
       
   370         for ( TInt i(0); i < appliedMatches.Count(); ++i )
       
   371         {
       
   372             TInt matchedDataIndex = appliedMatches[i];
       
   373             TPtr resultFragment = aMatchedData.MidTPtr( 
       
   374                 queryIndexes[ i ],
       
   375                 convertedQueriesAsDes[i].Length() );
       
   376             resultFragment = aData.Mid(
       
   377                 dataWordIndexes[ matchedDataIndex ],
       
   378                 convertedQueriesAsDes[i].Length() );
       
   379         }
       
   380     }
       
   381     else
       
   382     {
       
   383         aMatchedData.Zero();
       
   384     }
       
   385     
       
   386     for ( TInt i(0); i < possibleMatches.Count(); ++i )
       
   387     {
       
   388         RArray<TBool>* pointerToDelete = possibleMatches[i];
       
   389         pointerToDelete->Close();
       
   390         delete pointerToDelete;
       
   391     }
       
   392     possibleMatches.Close();
       
   393     dataWordIndexes.Close();
       
   394     dataWordLengths.Close();
       
   395 
       
   396     queryIndexes.Close();
       
   397     convertedQueriesAsDes.Close();
       
   398     queryList.Close();
       
   399     appliedMatches.Close();
       
   400     
       
   401     CleanupStack::PopAndDestroy();  //result of queryAsStringLC
       
   402 }
       
   403 
       
   404 // ----------------------------------------------------------------------------
       
   405 // CPcsAlgorithm1MultiSearchHelper::ConvertQueryToList
       
   406 // Converts the multiple search queries to a list
       
   407 // ----------------------------------------------------------------------------
       
   408 void  CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL(RPointerArray<CPsQuery>& aSearchQuery,
       
   409                                                            RPointerArray<HBufC>& aQueryList)
       
   410 {
       
   411     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL") );
       
   412 
       
   413     CleanupResetAndDestroyPushL( aQueryList );
       
   414     for ( TInt queryIndex = 0; queryIndex < aSearchQuery.Count(); queryIndex++ )
       
   415     {
       
   416         TBuf<KPsQueryMaxLen> dataWithKeys;
       
   417         iKeyMap->GetMixedKeyStringForQueryL( *aSearchQuery[queryIndex], dataWithKeys );
       
   418 
       
   419         HBufC* dWKToAppend = dataWithKeys.AllocLC();
       
   420         aQueryList.AppendL( dWKToAppend );
       
   421         CleanupStack::Pop( dWKToAppend ); // ownership transfered
       
   422     }
       
   423     
       
   424     CleanupStack::Pop( &aQueryList );
       
   425     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL") );
       
   426 }
       
   427 
       
   428 // ----------------------------------------------------------------------------
       
   429 // CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL
       
   430 // Subset search function. Refer the above function for more description.
       
   431 // ----------------------------------------------------------------------------
       
   432 void  CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL(
       
   433         CPcsAlgorithm1FilterHelper* aAlgorithmFilterHelper,
       
   434         RPointerArray<CPcsPoolElement>& aSearchSet,
       
   435         RPointerArray<CPsQuery>& aSearchQuery,
       
   436         TUint8 aFilteredDataMatch,
       
   437         TBool aIsSearchInGroup,
       
   438         RArray<TInt>& aContactsInGroup)
       
   439 {
       
   440     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
       
   441 
       
   442     __LATENCY_MARK ( _L("CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
       
   443 
       
   444     // Convert the individual queries to string form
       
   445     RPointerArray<HBufC> queryList;
       
   446     CleanupResetAndDestroyPushL( queryList );
       
   447     RPointerArray<HBufC> tempqueryList;
       
   448     CleanupClosePushL( tempqueryList );
       
   449     ConvertQueryToListL(aSearchQuery, queryList);
       
   450     
       
   451     // Remember a temporary copy of query list
       
   452     // since we sort the queries
       
   453 	for ( TInt i = 0; i < queryList.Count(); i++)
       
   454 	{
       
   455 		tempqueryList.AppendL(queryList[i]);
       
   456 	}
       
   457          
       
   458 	// Sort the query items before we search them
       
   459 	TLinearOrder<HBufC> rule( CPcsAlgorithm1Utils::CompareByLength );
       
   460 	queryList.Sort(rule);
       
   461 	
       
   462     // To hold the match results
       
   463     RPointerArray<TDesC> tmpMatchSet;
       
   464     CleanupResetAndDestroyPushL( tmpMatchSet );
       
   465        
       
   466     // Parse thru each search set elements and filter the results    
       
   467 	for ( TInt index = 0; index < aSearchSet.Count(); index++ )
       
   468 	{
       
   469 	    CPcsPoolElement* poolElement = static_cast<CPcsPoolElement*>(aSearchSet[index]);
       
   470 		CPsData* psData = poolElement->GetPsData();
       
   471 		psData->ClearDataMatches();
       
   472 		
       
   473         // Skip the contact if we are doing a group search and contact doesn't belong to the group
       
   474         if ( aIsSearchInGroup && 
       
   475              aContactsInGroup.Find( psData->Id() ) == KErrNotFound )
       
   476             {
       
   477             continue;
       
   478             }
       
   479         
       
   480 		TBool isMatch = ETrue;		
       
   481 		TInt wordMatches = 0;
       
   482 		
       
   483 		// Reset iWordMatches to zero 
       
   484 		ClearWordMatches();
       
   485 		
       
   486 		// Check for each query atleast one data element matches
       
   487 		// Loop from the last query so that longest match is seen first
       
   488 		for ( TInt queryIndex = queryList.Count() - 1; queryIndex >= 0; queryIndex-- )
       
   489 		{
       
   490 		    TBool queryMatch = EFalse;
       
   491 		    HBufC* tmpQuery = queryList[queryIndex];
       
   492 		    // Get the original query mode corresponding to this query
       
   493 		    TInt modeIndex = tempqueryList.Find(tmpQuery);
       
   494 		    
       
   495 		    TInt dataCount = psData->DataElementCount();
       
   496 		    __ASSERT_DEBUG( dataCount < MAX_DATA_FIELDS, Panic(KErrOverflow) );
       
   497 		    
       
   498 		    for ( TInt dataIndex = 0; dataIndex < dataCount; dataIndex++ )
       
   499 		    {
       
   500 		        // Filter off data fields not required in search
       
   501                 TUint8 bitIndex = 1 << dataIndex;
       
   502 
       
   503                 TUint8 filter = bitIndex & aFilteredDataMatch;
       
   504 		        if ( filter == 0x0 )
       
   505 		        {
       
   506 		            // Move to next data
       
   507 		        	continue;
       
   508 		        }
       
   509 		       	
       
   510 		        TInt wordIndex = -1;
       
   511 			    
       
   512 			    TLex lex(psData->Data(dataIndex)->Des());
       
   513 			    
       
   514 			    // First word
       
   515 			    TPtrC tmpData = lex.NextToken();
       
   516 			    
       
   517 			    // Search thru multiple words
       
   518 			    while ( tmpData.Length() != 0 ) 
       
   519 			    {
       
   520 			        wordIndex++;
       
   521 			     	
       
   522 			     	TBuf<KPsQueryMaxLen> data;
       
   523 
       
   524 	                // Convert the data to required form (mode specific)
       
   525 	                iKeyMap->GetMixedKeyStringForDataL(*aSearchQuery[modeIndex], tmpData, data);
       
   526 
       
   527 				    // Compare the data against query
       
   528                     if ( CPcsAlgorithm1Utils::MyCompareKeyAndString(data, *tmpQuery, *aSearchQuery[modeIndex]) )
       
   529 				    {
       
   530 				        psData->SetDataMatch(dataIndex);
       
   531 
       
   532 				        // Perform two checks.
       
   533 				        // 1. Ensure that the word is not matched against any previous query
       
   534 				        // 2. If it is the first match to the query
       
   535 				        TBool isWordMatch = IsWordMatch(dataIndex, wordIndex);
       
   536 
       
   537 				        // Check if the current word is not matched to any query
       
   538 		                if( !isWordMatch )
       
   539 		                {
       
   540 		                	// Check if no word is matched for this query till now
       
   541 							if ( !queryMatch )
       
   542 							{
       
   543 								wordMatches++;
       
   544 	                 			queryMatch = ETrue;
       
   545 	                 			SetWordMap(dataIndex, wordIndex);
       
   546 							}
       
   547 
       
   548 					        // Extract matched character sequence and fill in temp array
       
   549 							TInt len = tmpQuery->Length();
       
   550 							TPtrC seq = tmpData.Left(len);
       
   551 							CPcsAlgorithm1Utils::AppendMatchToSeqL( tmpMatchSet, seq );
       
   552 						}
       
   553 				    }
       
   554 				     
       
   555 				    // Next word
       
   556 				    tmpData.Set(lex.NextToken());
       
   557 			    }
       
   558 		    }
       
   559 		    
       
   560 		    // No data element matches the query. Ignore this result.
       
   561 		    if ( queryMatch == EFalse )
       
   562 		    {
       
   563 		        isMatch = EFalse;
       
   564 		     	break;
       
   565 		    }
       
   566 		}
       
   567 		
       
   568 		// If match add the element to the result set
       
   569 		//  And before adding to the result set, check if there is atleast one match per query
       
   570 		if ( isMatch && wordMatches >= queryList.Count() )
       
   571 		{
       
   572 			aAlgorithmFilterHelper->AddL(psData,tmpMatchSet);
       
   573 	    } 
       
   574 	    
       
   575 	    // Cleanup the match sequence array as 
       
   576 		// they are stored in pattern details structure
       
   577 		tmpMatchSet.ResetAndDestroy();
       
   578 	}
       
   579 	
       
   580 	// Free the query list
       
   581 	CleanupStack::PopAndDestroy( &tmpMatchSet );   // ResetAndDestroy
       
   582     CleanupStack::PopAndDestroy( &tempqueryList ); // Close
       
   583 	CleanupStack::PopAndDestroy( &queryList );     // ResetAndDestroy
       
   584 
       
   585 	__LATENCY_MARKEND ( _L("CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
       
   586 
       
   587 	PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
       
   588 }
       
   589 
       
   590 // ----------------------------------------------------------------------------
       
   591 // CPcsAlgorithm1MultiSearchHelper::SetWordMap()
       
   592 // ----------------------------------------------------------------------------
       
   593 void CPcsAlgorithm1MultiSearchHelper::SetWordMap(TInt aIndex, TInt aPosition)
       
   594 {
       
   595     TUint8 val = 1 << aPosition;
       
   596     iWordMatches[aIndex] |= val;
       
   597 }
       
   598 
       
   599 // ----------------------------------------------------------------------------
       
   600 // CPcsAlgorithm1MultiSearchHelper::IsWordMatch()
       
   601 // ----------------------------------------------------------------------------
       
   602 TBool CPcsAlgorithm1MultiSearchHelper::IsWordMatch(TInt aDataIndex, TInt aWordIndex)
       
   603 {
       
   604     TUint8 val = 1 << aWordIndex;
       
   605     return (iWordMatches[aDataIndex] & val);
       
   606 }
       
   607 
       
   608 // ----------------------------------------------------------------------------
       
   609 // CPcsAlgorithm1MultiSearchHelper::ClearWordMatches
       
   610 // Function to reset the iWordMatches
       
   611 // ----------------------------------------------------------------------------
       
   612 void CPcsAlgorithm1MultiSearchHelper::ClearWordMatches()
       
   613 {
       
   614 	for( TInt i = 0; i < MAX_DATA_FIELDS; i++)
       
   615 	{
       
   616         iWordMatches[i] = 0;
       
   617 	}
       
   618 }
       
   619 
       
   620 // ----------------------------------------------------------------------------
       
   621 // CPcsAlgorithm1MultiSearchHelper::CountMultiQueryWordsL
       
   622 
       
   623 // ----------------------------------------------------------------------------
       
   624 TInt CPcsAlgorithm1MultiSearchHelper::CountMultiQueryWordsL(CPsQuery& aQuery)
       
   625 {    
       
   626     TInt wordCount = 0;
       
   627     TBool newWord = ETrue;
       
   628 
       
   629     for (TInt i = 0; i < aQuery.Count(); i++ )
       
   630     {
       
   631         if ( aQuery.GetItemAtL(i).Character().IsSpace() )
       
   632         {
       
   633             newWord = ETrue;
       
   634         }
       
   635         else
       
   636         {
       
   637             if ( newWord )
       
   638             {
       
   639                 wordCount++;
       
   640                 newWord = EFalse;
       
   641             }
       
   642         }
       
   643     }
       
   644 
       
   645     PRINT1 ( _L("CPcsAlgorithm1MultiSearchHelper::CountMultiQueryWords: Number of words in search query: %d"), wordCount );
       
   646 
       
   647     return wordCount;
       
   648 }
       
   649 
       
   650 // ----------------------------------------------------------------------------
       
   651 // CPcsAlgorithm1MultiSearchHelper::MultiQuery
       
   652 // Checks if the query object has multiple queries embedded.
       
   653 // Seperator used is a space.
       
   654 // Scans through each query character and creates a new query object on a space.
       
   655 // Consequtive spaces are skipped.
       
   656 // Returns an array of query objects.
       
   657 // ----------------------------------------------------------------------------
       
   658 RPointerArray<CPsQuery> CPcsAlgorithm1MultiSearchHelper::MultiQueryL(CPsQuery& aQuery)
       
   659 {
       
   660     RPointerArray<CPsQuery> query;
       
   661     
       
   662 	const TInt textLength = aQuery.Count();
       
   663 	
       
   664     for (TInt beg = 0; beg < textLength; beg++)
       
   665     {
       
   666         // Skip separators before next word
       
   667         if ( !aQuery.GetItemAtL(beg).Character().IsSpace() )
       
   668         {
       
   669             // Scan the end of the word
       
   670             TInt end = beg+1;
       
   671             while (end < textLength &&
       
   672                    !aQuery.GetItemAtL(end).Character().IsSpace())
       
   673             {
       
   674                 end++;
       
   675             }
       
   676             
       
   677             // Create a new query object and append
       
   678             CPsQuery* newQuery = CPsQuery::NewL();
       
   679             for (TInt i = beg; i < end; i++)
       
   680             {
       
   681                 CPsQueryItem* item = CPsQueryItem::NewL();
       
   682                 item->SetCharacter(aQuery.GetItemAtL(i).Character());
       
   683                 item->SetMode(aQuery.GetItemAtL(i).Mode());
       
   684                 newQuery->AppendL(*item);
       
   685             }
       
   686             query.Append(newQuery);
       
   687             
       
   688             // Scan for next word
       
   689             beg = end;
       
   690         }
       
   691     }
       
   692 	
       
   693 	return query;
       
   694 }
       
   695 
       
   696 // End of file