predictivesearch/PcsAlgorithm/Algorithm1/src/CPcsAlgorithm1MultiSearchHelper.cpp
branchRCL_3
changeset 6 e8e3147d53eb
parent 3 04ab22b956c2
child 7 b3431bff8c19
equal deleted inserted replaced
5:81f8547efd4f 6:e8e3147d53eb
    21 #include "CPcsAlgorithm1Utils.h"
    21 #include "CPcsAlgorithm1Utils.h"
    22 #include "CPcsDefs.h"
    22 #include "CPcsDefs.h"
    23 #include <collate.h>
    23 #include <collate.h>
    24 #include <biditext.h>
    24 #include <biditext.h>
    25 
    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 
    26 // ============================== MEMBER FUNCTIONS ============================
    36 // ============================== MEMBER FUNCTIONS ============================
    27 
    37 
    28 // ----------------------------------------------------------------------------
    38 // ----------------------------------------------------------------------------
    29 // CPcsAlgorithm1MultiSearchHelper::NewL
    39 // CPcsAlgorithm1MultiSearchHelper::NewL
    30 // Two Phase Construction
    40 // Two Phase Construction
    94 //     data element has not matched for multiple queries.
   104 //     data element has not matched for multiple queries.
    95 // (9) Now include the element in the result.
   105 // (9) Now include the element in the result.
    96 // ----------------------------------------------------------------------------
   106 // ----------------------------------------------------------------------------
    97 void  CPcsAlgorithm1MultiSearchHelper::SearchMultiL(const CPsSettings& aSettings,
   107 void  CPcsAlgorithm1MultiSearchHelper::SearchMultiL(const CPsSettings& aSettings,
    98 	                                     RPointerArray<CPsQuery>& aPsQuery,
   108 	                                     RPointerArray<CPsQuery>& aPsQuery,
    99 	                                     TBool isSearchInGroup,
   109 	                                     TBool aIsSearchInGroup,
   100 	                                     RArray<TInt>& aContactsInGroup,
   110 	                                     RArray<TInt>& aContactsInGroup,
   101 	                                     RPointerArray<CPsData>& searchResults,
   111 	                                     RPointerArray<CPsData>& aSearchResults,
   102 	                                     RPointerArray<CPsPattern>& searchSeqs )
   112 	                                     RPointerArray<CPsPattern>& aSearchSeqs )
   103 {
   113 {
   104     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
   114     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
   105 
   115 
   106     __LATENCY_MARK ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
   116     __LATENCY_MARK ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
   107    
   117    
   108     PRINTQUERYLIST ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL: "), aPsQuery );
   118     PRINTQUERYLIST ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL: "), aPsQuery );
   109 
   119 
   110     // Create CPcsAlgorithm1FilterHelper object to be used for filtering the results
   120     // Create CPcsAlgorithm1FilterHelper object to be used for filtering the results
   111 	TSortType sortType = aSettings.GetSortType();
   121 	TSortType sortType = aSettings.GetSortType();
   112     CPcsAlgorithm1FilterHelper* filterHelper = CPcsAlgorithm1FilterHelper::NewL(sortType);
   122     CPcsAlgorithm1FilterHelper* filterHelper = CPcsAlgorithm1FilterHelper::NewL(sortType);
       
   123     CleanupStack::PushL( filterHelper );
   113     RPointerArray<CPcsPoolElement> elements;
   124     RPointerArray<CPcsPoolElement> elements;
   114     
   125     CleanupClosePushL( elements );
   115     iMultiSearchResultsArr.ResetAndDestroy();  
   126     
       
   127     iMultiSearchResultsArr.ResetAndDestroy();
   116     
   128     
   117     // Get the data stores  
   129     // Get the data stores  
   118     RPointerArray<TDesC> aDataStores;
   130     RPointerArray<TDesC> dataStores;
   119     aSettings.SearchUrisL(aDataStores);
   131     CleanupResetAndDestroyPushL( dataStores );
       
   132     aSettings.SearchUrisL(dataStores);
   120           
   133           
   121     // Get the required display fields from the client
   134     // Get the required display fields from the client
   122     RArray<TInt> requiredDataFields;
   135     RArray<TInt> requiredDataFields;
       
   136     CleanupClosePushL( requiredDataFields );
   123     aSettings.DisplayFieldsL(requiredDataFields);
   137     aSettings.DisplayFieldsL(requiredDataFields);
   124 
   138 
   125     // Search based on first character of 1st item in query list
   139     // Search from cache based on first character of 1st item in query list
   126     TInt numValue = iKeyMap->PoolIdForCharacter(aPsQuery[0]->GetItemAtL(0).Character());
   140     const CPsQueryItem& firstCharItem = aPsQuery[0]->GetItemAtL(0);
       
   141     TInt cachePoolId = iKeyMap->PoolIdForCharacter( firstCharItem.Character(), firstCharItem.Mode() );
   127 
   142 
   128     // Get the elements from all the databases
   143     // Get the elements from all the databases
   129     for ( int dsIndex = 0; 
   144     for ( TInt dsIndex = 0; 
   130 			dsIndex < aDataStores.Count(); 
   145 			dsIndex < dataStores.Count(); 
   131 			dsIndex++ )
   146 			dsIndex++ )
   132 	{
   147 	{
   133 	    RPointerArray<CPsData> *temp = new (ELeave) RPointerArray<CPsData> ();
   148 	    RPointerArray<CPsData> *temp = new (ELeave) RPointerArray<CPsData> ();
   134 	    iMultiSearchResultsArr.Append(temp);
   149 	    iMultiSearchResultsArr.Append(temp);
   135 	    
   150 	    
   136 	    // Get the contents for this data store
   151 	    // Get the contents for this data store
   137 	    TInt arrayIndex = iAlgorithm->GetCacheIndex(*(aDataStores[dsIndex]));					    
   152 	    TInt arrayIndex = iAlgorithm->GetCacheIndex(*(dataStores[dsIndex]));
   138 		if ( arrayIndex < 0 ) continue;		
   153 		if ( arrayIndex < 0 ) continue;
   139 		CPcsCache* cache = iAlgorithm->GetCache(arrayIndex);	    		   
   154 		CPcsCache* cache = iAlgorithm->GetCache(arrayIndex);
   140 	    cache->GetContactsForKeyL(numValue,elements);
   155 	    cache->GetContactsForKeyL(cachePoolId, elements);
   141 		
   156 		
   142 	    // Get the supported data fields for this data store
   157 	    // Get the supported data fields for this data store
   143         RArray<TInt> supportedDataFields;
   158         RArray<TInt> supportedDataFields;
       
   159         CleanupClosePushL( supportedDataFields );
   144 		cache->GetDataFields(supportedDataFields);
   160 		cache->GetDataFields(supportedDataFields);
   145 		
   161 		
   146 		// Get the filtered data fields for this data store    		
   162 		// Get the filtered data fields for this data store
   147 		TUint8 filteredDataMatch = FilterDataFieldsL(requiredDataFields, 
   163 		TUint8 filteredDataMatch = FilterDataFieldsL(requiredDataFields, 
   148 		                                             supportedDataFields);
   164 		                                             supportedDataFields);
   149 
   165 
   150 	    // Filter the results now
   166 	    // Filter the results now
   151 	    FilterResultsMultiL(filterHelper,
   167 	    FilterResultsMultiL(filterHelper,
   152                             elements,
   168 	    			   elements, 
   153                             aPsQuery,
   169 		               aPsQuery, 
   154                             filteredDataMatch,
   170 		               filteredDataMatch,
   155                             isSearchInGroup,
   171 		               aIsSearchInGroup,
   156                             aContactsInGroup);	
   172 		               aContactsInGroup);
   157 	    
   173 	    
   158 		// If alphabetical sorting, get the results for this datastore               
   174 		// If alphabetical sorting, get the results for this datastore               
   159 		if ( sortType == EAlphabetical )
   175 		if ( sortType == EAlphabetical )
   160 		{
   176 		{
   161 			filterHelper->GetResults(*(iMultiSearchResultsArr[dsIndex]));
   177 			filterHelper->GetResults(*(iMultiSearchResultsArr[dsIndex]));
   162 		}
   178 		}
   163 		
   179 		
   164 	    elements.Reset();
   180 	    elements.Reset();
   165 	    supportedDataFields.Reset();
   181 	    CleanupStack::PopAndDestroy( &supportedDataFields ); // Close
   166 	}
   182 	}
   167 	aDataStores.ResetAndDestroy();
   183     CleanupStack::PopAndDestroy( &requiredDataFields ); // Close
   168     requiredDataFields.Reset(); 
   184     CleanupStack::PopAndDestroy( &dataStores );         // ResetAndDestroy
   169 	
   185 	
   170 	// If alphabetical sorting, merge the result sets of all datastores
   186 	// If alphabetical sorting, merge the result sets of all datastores
   171   	if ( sortType == EAlphabetical )
   187   	if ( sortType == EAlphabetical )
   172   	{
   188   	{
   173   		// Form the complete searchResults array
   189   		// Form the complete searchResults array
   174     	CPcsAlgorithm1Utils::FormCompleteSearchResultsL(iMultiSearchResultsArr,
   190     	CPcsAlgorithm1Utils::FormCompleteSearchResultsL(iMultiSearchResultsArr,
   175     											   	    searchResults);
   191     											   	    aSearchResults);
   176   	}
   192   	}
   177   	else
   193   	else
   178   	{
   194   	{
   179   		// Results are already sorted patternbased
   195   		// Results are already sorted patternbased
   180   		filterHelper->GetResults(searchResults);
   196   		filterHelper->GetResults(aSearchResults);
   181   	}
   197   	}
   182   
   198   
   183   	// Get the sorted match sequence list
   199   	// Get the sorted match sequence list
   184 	filterHelper->GetPatternsL(searchSeqs);
   200 	filterHelper->GetPatternsL(aSearchSeqs);
   185 	
   201 	
   186 	PRINT1 ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL: Number of search results = %d"), searchResults.Count() ); 
   202 	PRINT1 ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL: Number of search results = %d"), aSearchResults.Count() );
   187              
   203              
   188 	// Cleanup             
   204 	// Cleanup             
   189     for(TInt i = 0; i < iMultiSearchResultsArr.Count(); i++)
   205     for(TInt i = 0; i < iMultiSearchResultsArr.Count(); i++)
   190     {
   206     {
   191     	iMultiSearchResultsArr[i]->Reset();
   207     	iMultiSearchResultsArr[i]->Reset();
   192     	delete iMultiSearchResultsArr[i];
   208     	delete iMultiSearchResultsArr[i];
   193         iMultiSearchResultsArr[i] = NULL;
   209         iMultiSearchResultsArr[i] = NULL;
   194     }
   210     }
   195     
   211     
   196     iMultiSearchResultsArr.Reset();
   212     iMultiSearchResultsArr.Reset();
   197     delete filterHelper;
   213     CleanupStack::PopAndDestroy( &elements ); // Close
       
   214     CleanupStack::PopAndDestroy( filterHelper );
   198    
   215    
   199     __LATENCY_MARKEND ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
   216     __LATENCY_MARKEND ( _L("CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
   200 
   217 
   201     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
   218     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::SearchMultiL") );
   202 }
   219 }
   409 void  CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL(RPointerArray<CPsQuery>& aSearchQuery,
   426 void  CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL(RPointerArray<CPsQuery>& aSearchQuery,
   410                                                            RPointerArray<HBufC>& aQueryList)
   427                                                            RPointerArray<HBufC>& aQueryList)
   411 {
   428 {
   412     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL") );
   429     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL") );
   413 
   430 
   414     for ( int queryIndex = 0; queryIndex < aSearchQuery.Count(); queryIndex++ )
   431     for ( TInt queryIndex = 0; queryIndex < aSearchQuery.Count(); queryIndex++ )
   415     {
   432     {
   416         TBuf<KPsQueryMaxLen> dataWithKeys;
   433         TBuf<KPsQueryMaxLen> dataWithKeys;
   417         iKeyMap->GetMixedKeyStringForQueryL( *aSearchQuery[queryIndex], dataWithKeys );
   434         iKeyMap->GetMixedKeyStringForQueryL( *aSearchQuery[queryIndex], dataWithKeys );
   418 
   435 
   419         HBufC* dWKToAppend = HBufC::NewL(KPsQueryMaxLen);
   436         HBufC* dWKToAppend = dataWithKeys.AllocLC();
   420         TPtr tPtr(dWKToAppend->Des());
   437         aQueryList.AppendL( dWKToAppend );
   421         tPtr.Copy(dataWithKeys);
   438         CleanupStack::Pop( dWKToAppend ); // ownership transfered
   422 
       
   423         aQueryList.Append( dWKToAppend );
       
   424     }
   439     }
   425 
   440 
   426     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL") );
   441     PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::ConvertQueryToListL") );
   427 }
   442 }
   428 
   443 
   430 // CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL
   445 // CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL
   431 // Subset search function. Refer the above function for more description.
   446 // Subset search function. Refer the above function for more description.
   432 // ----------------------------------------------------------------------------
   447 // ----------------------------------------------------------------------------
   433 void  CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL(
   448 void  CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL(
   434         CPcsAlgorithm1FilterHelper* aAlgorithmFilterHelper,
   449         CPcsAlgorithm1FilterHelper* aAlgorithmFilterHelper,
   435         RPointerArray<CPcsPoolElement>& searchSet,
   450         RPointerArray<CPcsPoolElement>& aSearchSet,
   436         RPointerArray<CPsQuery>& searchQuery,
   451         RPointerArray<CPsQuery>& aSearchQuery,
   437         TUint8 aFilteredDataMatch,
   452         TUint8 aFilteredDataMatch,
   438         TBool isSearchInGroup,
   453         TBool aIsSearchInGroup,
   439         RArray<TInt>& aContactsInGroup)
   454         RArray<TInt>& aContactsInGroup)
   440 {
   455 {
   441     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
   456     PRINT ( _L("Enter CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
   442 
   457 
   443     __LATENCY_MARK ( _L("CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
   458     __LATENCY_MARK ( _L("CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
   444 
   459 
   445     // Convert the individual queries to string form
   460     // Convert the individual queries to string form
   446     RPointerArray<HBufC> queryList;    
   461     RPointerArray<HBufC> queryList;
   447     RPointerArray<HBufC> tempqueryList;   
   462     CleanupResetAndDestroyPushL( queryList );
   448     ConvertQueryToListL(searchQuery, queryList);
   463     RPointerArray<HBufC> tempqueryList;
       
   464     CleanupClosePushL( tempqueryList );
       
   465     ConvertQueryToListL(aSearchQuery, queryList);
   449     
   466     
   450     // Remember a temporary copy of query list
   467     // Remember a temporary copy of query list
   451     // since we sort the queries
   468     // since we sort the queries
   452 	for( TInt i = 0; i < queryList.Count(); i++)
   469 	for( TInt i = 0; i < queryList.Count(); i++)
   453 	{
   470 	{
   457 	// Sort the query items before we search them
   474 	// Sort the query items before we search them
   458 	TLinearOrder<HBufC> rule( CPcsAlgorithm1Utils::CompareByLength );
   475 	TLinearOrder<HBufC> rule( CPcsAlgorithm1Utils::CompareByLength );
   459 	queryList.Sort(rule);
   476 	queryList.Sort(rule);
   460 	
   477 	
   461     // To hold the match results
   478     // To hold the match results
   462     RPointerArray<TDesC> tmpMatchSet;  
   479     RPointerArray<TDesC> tmpMatchSet;
       
   480     CleanupResetAndDestroyPushL( tmpMatchSet );
   463        
   481        
   464     // Parse thru each search set elements and filter the results    
   482     // Parse thru each search set elements and filter the results    
   465 	for ( int index = 0; index < searchSet.Count(); index++ )
   483 	for ( TInt index = 0; index < aSearchSet.Count(); index++ )
   466 	{
   484 	{
   467 	    CPcsPoolElement* poolElement = static_cast<CPcsPoolElement*>(searchSet[index]);
   485 	    CPcsPoolElement* poolElement = static_cast<CPcsPoolElement*>(aSearchSet[index]);
   468 		CPsData* psData = poolElement->GetPsData();
   486 		CPsData* psData = poolElement->GetPsData();
   469 		psData->ClearDataMatches();
   487 		psData->ClearDataMatches();
   470 		
   488 		
   471 		TBool isMatch = ETrue;		
   489 		TBool isMatch = ETrue;		
   472 		TUint8 wordMatches = 0;
   490 		TInt wordMatches = 0;
   473 		
   491 		
   474 		// Reset iWordMatches to zero 
   492 		// Reset iWordMatches to zero 
   475 		ClearWordMatches();
   493 		ClearWordMatches();
   476 		
   494 		
   477 		// Check for each query atleast one data element matches
   495 		// Check for each query atleast one data element matches
   481 		    TBool queryMatch = EFalse;
   499 		    TBool queryMatch = EFalse;
   482 		    HBufC* tmpQuery = queryList[queryIndex];
   500 		    HBufC* tmpQuery = queryList[queryIndex];
   483 		    // Get the original query mode corresponding to this query
   501 		    // Get the original query mode corresponding to this query
   484 		    TInt modeIndex = tempqueryList.Find(tmpQuery);
   502 		    TInt modeIndex = tempqueryList.Find(tmpQuery);
   485 		    
   503 		    
   486 		    for ( TInt dataIndex = 0; dataIndex < psData->DataElementCount(); dataIndex++ )
   504 		    TInt dataCount = psData->DataElementCount();
       
   505 		    __ASSERT_DEBUG( dataCount < MAX_DATA_FIELDS, Panic(KErrOverflow) );
       
   506 		    
       
   507 		    for ( TInt dataIndex = 0; dataIndex < dataCount; dataIndex++ )
   487 		    {
   508 		    {
   488 		        // Filter off data fields not required in search
   509 		        // Filter off data fields not required in search
   489 		        TReal bitIndex;
   510                 TUint8 bitIndex = 1 << dataIndex;
   490 		        Math::Pow(bitIndex, 2, dataIndex);
   511 
   491 
   512                 TUint8 filter = bitIndex & aFilteredDataMatch;
   492                 TUint8 filter = (TUint8)bitIndex & aFilteredDataMatch;     		         
       
   493 		        if ( filter == 0x0 )
   513 		        if ( filter == 0x0 )
   494 		        {
   514 		        {
   495 		            // Move to next data
   515 		            // Move to next data
   496 		        	continue;
   516 		        	continue;
   497 		        }
   517 		        }
   498 		       	
   518 		       	
   499 		        TInt wordIndex = -1;			     
   519 		        TInt wordIndex = -1;
   500 			    
   520 			    
   501 			    TLex lex(psData->Data(dataIndex)->Des());
   521 			    TLex lex(psData->Data(dataIndex)->Des());
   502 			    
   522 			    
   503 			    // First word
   523 			    // First word
   504 			    TPtrC tmpData = lex.NextToken();
   524 			    TPtrC tmpData = lex.NextToken();
   505 			    
   525 			    
   506 			    // Search thru multiple words
   526 			    // Search thru multiple words
   507 			    while ( tmpData.Length() != 0 ) 
   527 			    while ( tmpData.Length() != 0 ) 
   508 			    {			      
   528 			    {
   509 			        wordIndex++;			         				  
   529 			        wordIndex++;
   510 			     	
   530 			     	
   511 			     	TBuf<KPsQueryMaxLen> data; 
   531 			     	TBuf<KPsQueryMaxLen> data;
   512 
   532 
   513 	                // Convert the data to required form (mode specific)
   533 	                // Convert the data to required form (mode specific)
   514 	                iKeyMap->GetMixedKeyStringForDataL(*searchQuery[modeIndex], tmpData, data);
   534 	                iKeyMap->GetMixedKeyStringForDataL(*aSearchQuery[modeIndex], tmpData, data);
   515 
   535 
   516 				    // Compare the data against query
   536 				    // Compare the data against query
   517                     if ( CPcsAlgorithm1Utils::MyCompareKeyAndString(data, *tmpQuery, *searchQuery[modeIndex]) )
   537                     if ( CPcsAlgorithm1Utils::MyCompareKeyAndString(data, *tmpQuery, *aSearchQuery[modeIndex]) )
   518 				    {
   538 				    {
   519 				        psData->SetDataMatch(dataIndex);				        			        
   539 				        psData->SetDataMatch(dataIndex);
   520 
   540 
   521 				        // Perform two checks.
   541 				        // Perform two checks.
   522 				        // 1. Ensure that the word is not matched against any previous query
   542 				        // 1. Ensure that the word is not matched against any previous query
   523 				        // 2. If it is the first match to the query
   543 				        // 2. If it is the first match to the query
   524 				        TBool isWordMatch = IsWordMatch(dataIndex, wordIndex);				        
   544 				        TBool isWordMatch = IsWordMatch(dataIndex, wordIndex);
   525 
   545 
   526 				        // Check if the current word is not matched to any query
   546 				        // Check if the current word is not matched to any query
   527 		                if( !isWordMatch )
   547 		                if( !isWordMatch )
   528 		                {
   548 		                {
   529 		                	// Check if no word is matched for this query till now
   549 		                	// Check if no word is matched for this query till now
   534 	                 			SetWordMap(dataIndex, wordIndex);
   554 	                 			SetWordMap(dataIndex, wordIndex);
   535 							}
   555 							}
   536 
   556 
   537 					        // Extract matched character sequence and fill in temp array
   557 					        // Extract matched character sequence and fill in temp array
   538 							TInt len = tmpQuery->Length();
   558 							TInt len = tmpQuery->Length();
   539 							HBufC* seq = HBufC::NewL(len);
   559 							HBufC* seq = tmpData.Left(len).AllocLC();
   540 							*seq = tmpData.Mid(0, len);
       
   541 							
   560 							
   542 							seq->Des().UpperCase();
   561 							seq->Des().UpperCase();
   543 							TIdentityRelation<TDesC> searchRule(CPcsAlgorithm1Utils::CompareExact);
   562 							TIdentityRelation<TDesC> searchRule(CPcsAlgorithm1Utils::CompareExact);
   544 							if ( tmpMatchSet.Find(seq, searchRule) == KErrNotFound )
   563 							if ( tmpMatchSet.Find(seq, searchRule) == KErrNotFound )
   545 							{
   564 							{
   546                                 tmpMatchSet.Append(seq);
   565                                 tmpMatchSet.AppendL(seq);
       
   566                                 CleanupStack::Pop(seq);
   547 							}
   567 							}
   548 							else
   568 							else
   549 							{ 
   569 							{ 
   550                                 delete seq;
   570                                 CleanupStack::PopAndDestroy(seq);
   551 								seq = NULL;
   571 								seq = NULL;
   552 							}
   572 							}
   553 						}
   573 						}
   554 				    }
   574 				    }
   555 				     
   575 				     
   556 				    // Next word
   576 				    // Next word
   557 				    tmpData.Set(lex.NextToken());
   577 				    tmpData.Set(lex.NextToken());
   558 			    }			     
   578 			    }
   559 		    }
   579 		    }
   560 		    
   580 		    
   561 		    // No data element matches the query. Ignore this result.
   581 		    // No data element matches the query. Ignore this result.
   562 		    if ( queryMatch == EFalse )
   582 		    if ( queryMatch == EFalse )
   563 		    {
   583 		    {
   566 		    }
   586 		    }
   567 		}
   587 		}
   568 		
   588 		
   569 		// If match add the element to the result set
   589 		// If match add the element to the result set
   570 		//  And before adding to the result set, check if there is atleast one match per query
   590 		//  And before adding to the result set, check if there is atleast one match per query
   571 		if (( isMatch ) && ( wordMatches >= queryList.Count()))
   591 		if ( isMatch && wordMatches >= queryList.Count() )
   572 		{
   592 		{
   573 			if ( isSearchInGroup )
   593 			if ( aIsSearchInGroup )
   574 			{
   594 			{
   575 				if ( aContactsInGroup.Find(psData->Id()) != KErrNotFound )
   595 				if ( aContactsInGroup.Find(psData->Id()) != KErrNotFound )
   576 				{
   596 				{
   577 					aAlgorithmFilterHelper->AddL(psData,tmpMatchSet);
   597 					aAlgorithmFilterHelper->AddL(psData,tmpMatchSet);
   578 				}
   598 				}
   583 			}
   603 			}
   584 	    } 
   604 	    } 
   585 	    
   605 	    
   586 	    // Cleanup the match sequence array as 
   606 	    // Cleanup the match sequence array as 
   587 		// they are stored in pattern details structure
   607 		// they are stored in pattern details structure
   588 		tmpMatchSet.ResetAndDestroy();       
   608 		tmpMatchSet.ResetAndDestroy();
   589 	}
   609 	}
   590 	
   610 	
   591 	// Free the query list
   611 	// Free the query list
   592 	queryList.ResetAndDestroy();
   612 	CleanupStack::PopAndDestroy( &tmpMatchSet );   // ResetAndDestroy
   593 	tempqueryList.Reset();
   613     CleanupStack::PopAndDestroy( &tempqueryList ); // Close
       
   614 	CleanupStack::PopAndDestroy( &queryList );     // ResetAndDestroy
   594 
   615 
   595 	__LATENCY_MARKEND ( _L("CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
   616 	__LATENCY_MARKEND ( _L("CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
   596 
   617 
   597 	PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
   618 	PRINT ( _L("End CPcsAlgorithm1MultiSearchHelper::FilterResultsMultiL") );
   598 }
   619 }
   599 
   620 
   600 // ----------------------------------------------------------------------------
   621 // ----------------------------------------------------------------------------
   601 // CPcsAlgorithm1MultiSearchHelper::SetWordMap()
   622 // CPcsAlgorithm1MultiSearchHelper::SetWordMap()
   602 // ----------------------------------------------------------------------------
   623 // ----------------------------------------------------------------------------
   603 void CPcsAlgorithm1MultiSearchHelper::SetWordMap( TInt aIndex, TInt aPosition)
   624 void CPcsAlgorithm1MultiSearchHelper::SetWordMap(TInt aIndex, TInt aPosition)
   604 {
   625 {
   605     TReal val;
   626     TUint8 val = 1 << aPosition;
   606 	Math::Pow(val, 2, aPosition);
   627     iWordMatches[aIndex] |= val;
   607 
       
   608 	iWordMatches[aIndex] |= (TUint8)val;
       
   609 }
   628 }
   610 
   629 
   611 // ----------------------------------------------------------------------------
   630 // ----------------------------------------------------------------------------
   612 // CPcsAlgorithm1MultiSearchHelper::IsWordMatch()
   631 // CPcsAlgorithm1MultiSearchHelper::IsWordMatch()
   613 // ----------------------------------------------------------------------------
   632 // ----------------------------------------------------------------------------
   614 TBool CPcsAlgorithm1MultiSearchHelper::IsWordMatch(TInt aDataIndex, TInt aWordIndex)
   633 TBool CPcsAlgorithm1MultiSearchHelper::IsWordMatch(TInt aDataIndex, TInt aWordIndex)
   615 {
   634 {
   616     TReal val;
   635     TUint8 val = 1 << aWordIndex;
   617 	Math::Pow(val, 2, aWordIndex);
   636     return (iWordMatches[aDataIndex] & val);
   618 
       
   619 	return(iWordMatches[aDataIndex] & (TUint8)val);
       
   620 }
   637 }
   621 
   638 
   622 // ----------------------------------------------------------------------------
   639 // ----------------------------------------------------------------------------
   623 // CPcsAlgorithm1MultiSearchHelper::ClearWordMatches
   640 // CPcsAlgorithm1MultiSearchHelper::ClearWordMatches
   624 // Function to reset the iWordMatches
   641 // Function to reset the iWordMatches
   636 
   653 
   637 // ----------------------------------------------------------------------------
   654 // ----------------------------------------------------------------------------
   638 TInt CPcsAlgorithm1MultiSearchHelper::CountMultiQueryWordsL(CPsQuery& aQuery)
   655 TInt CPcsAlgorithm1MultiSearchHelper::CountMultiQueryWordsL(CPsQuery& aQuery)
   639 {    
   656 {    
   640     TInt wordCount = 0;
   657     TInt wordCount = 0;
   641     TBool newWord = true;
   658     TBool newWord = ETrue;
   642 
   659 
   643     for (TInt i = 0; i < aQuery.Count(); i++ )
   660     for (TInt i = 0; i < aQuery.Count(); i++ )
   644     {
   661     {
   645         if ( aQuery.GetItemAtL(i).Character().IsSpace() )
   662         if ( aQuery.GetItemAtL(i).Character().IsSpace() )
   646         {
   663         {
   647             newWord = true;
   664             newWord = ETrue;
   648         }
   665         }
   649         else
   666         else
   650         {
   667         {
   651             if ( newWord )
   668             if ( newWord )
   652             {
   669             {
   653                 wordCount++;
   670                 wordCount++;
   654                 newWord = false;
   671                 newWord = EFalse;
   655             }
   672             }
   656         }
   673         }
   657     }
   674     }
   658 
   675 
   659     PRINT1 ( _L("CPcsAlgorithm1MultiSearchHelper::CountMultiQueryWords: Number of words in search query: %d"), wordCount );
   676     PRINT1 ( _L("CPcsAlgorithm1MultiSearchHelper::CountMultiQueryWords: Number of words in search query: %d"), wordCount );
   675     
   692     
   676 	const TInt textLength = aQuery.Count();
   693 	const TInt textLength = aQuery.Count();
   677 	
   694 	
   678     for (TInt beg = 0; beg < textLength; beg++)
   695     for (TInt beg = 0; beg < textLength; beg++)
   679     {
   696     {
   680         // Skip separators before next word	                
   697         // Skip separators before next word
   681         if ( !aQuery.GetItemAtL(beg).Character().IsSpace() )
   698         if ( !aQuery.GetItemAtL(beg).Character().IsSpace() )
   682         {
   699         {
   683             // Scan the end of the word
   700             // Scan the end of the word
   684             TInt end = beg+1;
   701             TInt end = beg+1;
   685             while (end < textLength &&                  
   702             while (end < textLength &&
   686                    !aQuery.GetItemAtL(end).Character().IsSpace())
   703                    !aQuery.GetItemAtL(end).Character().IsSpace())
   687             {
   704             {
   688                 end++;
   705                 end++;
   689             }
   706             }
   690             
   707             
   693             for (TInt i = beg; i < end; i++)
   710             for (TInt i = beg; i < end; i++)
   694             {
   711             {
   695                 CPsQueryItem* item = CPsQueryItem::NewL();
   712                 CPsQueryItem* item = CPsQueryItem::NewL();
   696                 item->SetCharacter(aQuery.GetItemAtL(i).Character());
   713                 item->SetCharacter(aQuery.GetItemAtL(i).Character());
   697                 item->SetMode(aQuery.GetItemAtL(i).Mode());
   714                 item->SetMode(aQuery.GetItemAtL(i).Mode());
   698             	newQuery->AppendL(*item);
   715                 newQuery->AppendL(*item);
   699             }
   716             }
   700             query.Append(newQuery);
   717             query.Append(newQuery);
   701             
   718             
   702             // Scan for next word
   719             // Scan for next word
   703             beg = end;
   720             beg = end;
   717 TUint8 CPcsAlgorithm1MultiSearchHelper::FilterDataFieldsL(RArray<TInt>& aRequiredDataFields,
   734 TUint8 CPcsAlgorithm1MultiSearchHelper::FilterDataFieldsL(RArray<TInt>& aRequiredDataFields,
   718                                                           RArray<TInt>& aSupportedDataFields)
   735                                                           RArray<TInt>& aSupportedDataFields)
   719 {
   736 {
   720     TUint8 filteredMatch = 0x0;
   737     TUint8 filteredMatch = 0x0;
   721     
   738     
   722 	for ( int i = 0; i < aSupportedDataFields.Count(); i++ )
   739 	for ( TInt i = 0; i < aSupportedDataFields.Count(); i++ )
   723 	{
   740 	{
   724 		for ( int j = 0; j < aRequiredDataFields.Count(); j++ )
   741 		for ( TInt j = 0; j < aRequiredDataFields.Count(); j++ )
   725 		{
   742 		{
   726 			if ( aSupportedDataFields[i] == aRequiredDataFields[j] )
   743 			if ( aSupportedDataFields[i] == aRequiredDataFields[j] )
   727 			{
   744 			{
   728 				TReal val;
   745                 TUint8 val = 1 << i;
   729 			    Math::Pow(val, 2, i);
   746 			    filteredMatch |= val;
   730 			    
       
   731 			    filteredMatch |= (TUint8)val;	
       
   732 			}
   747 			}
   733 		}
   748 		}
   734 	}
   749 	}
   735 	
   750 	
   736 	return filteredMatch;
   751 	return filteredMatch;