predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsAlgorithm2MultiSearchHelper.cpp
branchRCL_3
changeset 58 d4f567ce2e7c
parent 32 2828b4d142c0
child 64 c1e8ba0c2b16
--- a/predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsAlgorithm2MultiSearchHelper.cpp	Thu Jul 15 18:22:55 2010 +0300
+++ b/predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsAlgorithm2MultiSearchHelper.cpp	Thu Aug 19 09:41:07 2010 +0300
@@ -23,26 +23,7 @@
 #include <collate.h>
 #include <biditext.h>
 
-// Compare functions
-TBool Compare2(const HBufC& aFirst, const HBufC& aSecond)
-    {
-    TPtrC t1(aFirst);
-    TPtrC t2(aSecond);
-    return (t1.Length() > t2.Length());
-    }
 
-TBool Compare3(const TDesC& aFirst, const TDesC& aSecond)
-    {
-    return aFirst == aSecond;
-    }
-
-TBool Compare4(const CPsQuery& aFirst, const CPsQuery& aSecond)
-    {
-    CPsQuery& first = const_cast<CPsQuery&> (aFirst);
-    CPsQuery& second = const_cast<CPsQuery&> (aSecond);
-    
-    return (first.Count() > second.Count());
-    }
 // ============================== MEMBER FUNCTIONS ============================
 
 // ----------------------------------------------------------------------------
@@ -82,13 +63,14 @@
     PRINT ( _L("Enter CPcsAlgorithm2MultiSearchHelper::ConstructL") );
 
     iAlgorithm = aAlgorithm;
-    keyMap = iAlgorithm->GetKeyMap();
-
+    iKeyMap = iAlgorithm->GetKeyMap();
+    iMaxCount = 0;
+    
     PRINT ( _L("End CPcsAlgorithm2MultiSearchHelper::ConstructL") );
     }
 
 // ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::CPcsAlgorithm2MultiSearchHelper
+// CPcsAlgorithm2MultiSearchHelper::~CPcsAlgorithm2MultiSearchHelper
 // Destructor
 // ----------------------------------------------------------------------------
 CPcsAlgorithm2MultiSearchHelper::~CPcsAlgorithm2MultiSearchHelper()
@@ -116,90 +98,105 @@
 // ----------------------------------------------------------------------------
 void CPcsAlgorithm2MultiSearchHelper::SearchMultiL(const CPsSettings& aSettings, 
                                                    RPointerArray<CPsQuery>& aPsQuery,
-                                                   TBool isSearchInGroup, 
-                                                   RArray<TInt>& aContactsInGroup, 
-                                                   RPointerArray<CPsData>& searchResults,
-                                                   RPointerArray<CPsPattern>& searchSeqs,
-                                                   TInt keyboardMode)
+                                                   TBool aIsSearchInGroup, 
+                                                   const RArray<TInt>& aContactsInGroup, 
+                                                   RPointerArray<CPsData>& aSearchResults,
+                                                   RPointerArray<CPsPattern>& aSearchSeqs)
     {
-    __LATENCY_MARK ( _L("CPcsAlgorithm2MultiSearchHelper::SearchMultiL") );
     PRINT ( _L("Enter CPcsAlgorithm2MultiSearchHelper::SearchMultiL") );
 
+    //__LATENCY_MARK ( _L("CPcsAlgorithm2MultiSearchHelper::SearchMultiL") );
+
+    PRINTQUERYLIST ( _L("CPcsAlgorithm2MultiSearchHelper::SearchMultiL: "), aPsQuery );
+
+    iMaxCount = aSettings.MaxResults();
     // Create CPcsAlgorithm2FilterHelper object to be used for filtering the results
     TSortType sortType = aSettings.GetSortType();
     CPcsAlgorithm2FilterHelper* filterHelper = CPcsAlgorithm2FilterHelper::NewL(sortType);
+    CleanupStack::PushL( filterHelper );
     RPointerArray<CPcsPoolElement> elements;
+    CleanupClosePushL( elements );
 
     iMultiSearchResultsArr.ResetAndDestroy();
 
     // Get the data stores  
-    RPointerArray<TDesC> aDataStores;
-    aSettings.SearchUrisL(aDataStores);
+    RPointerArray<TDesC> dataStores;
+    CleanupResetAndDestroyPushL( dataStores );
+    aSettings.SearchUrisL(dataStores);
 
     // Get the required display fields from the client
     RArray<TInt> requiredDataFields;
+    CleanupClosePushL( requiredDataFields );
     aSettings.DisplayFieldsL(requiredDataFields);
 
-    // Search from cache based on first character
+    // Search from cache based on first character of 1st item in query list
     const CPsQueryItem& firstCharItem = aPsQuery[0]->GetItemAtL(0);
-    TInt numValue  = keyMap->PoolIdForCharacter( firstCharItem.Character() );
-    
+    TInt cachePoolId = iKeyMap->PoolIdForCharacter( firstCharItem.Character(), firstCharItem.Mode() );
+
     // Get the elements from all the databases
-    for (int dsIndex = 0; dsIndex < aDataStores.Count(); dsIndex++)
+    const TInt dataStoresCount = dataStores.Count(); 
+    for (TInt dsIndex = 0; dsIndex < dataStoresCount; dsIndex++)
         {
         RPointerArray<CPsData> *temp = new (ELeave) RPointerArray<CPsData> ();
         iMultiSearchResultsArr.Append(temp);
 
         // Get the contents for this data store
-        TInt arrayIndex = iAlgorithm->GetCacheIndex(*(aDataStores[dsIndex]));
+        TInt arrayIndex = iAlgorithm->GetCacheIndex(*(dataStores[dsIndex]));
         if (arrayIndex < 0)
             {
             continue;
             }
         CPcsCache* cache = iAlgorithm->GetCache(arrayIndex);
-        cache->GetContactsForKeyL(numValue, elements);
+        cache->GetContactsForKeyL(cachePoolId, elements);
 
         // Get the supported data fields for this data store
         RArray<TInt> supportedDataFields;
+        CleanupClosePushL( supportedDataFields );
         cache->GetDataFields(supportedDataFields);
 
-        // Get the filtered data fields for this data store    		
-        TUint8 filteredDataMatch = FilterDataFieldsL(requiredDataFields, supportedDataFields);
+        // Get the filtered data fields for this data store
+        TUint8 filteredDataMatch = CPcsAlgorithm2Utils::FilterDataFieldsL(
+                requiredDataFields, supportedDataFields);
 
         // Filter the results now
-        FilterResultsMultiL(filterHelper, elements, aPsQuery, filteredDataMatch, 
-                            isSearchInGroup, aContactsInGroup, keyboardMode);
+        FilterResultsMultiL(filterHelper, 
+                            elements, 
+                            aPsQuery, 
+                            filteredDataMatch, 
+                            aIsSearchInGroup, 
+                            aContactsInGroup);
 
-        // If alphabetical sorting, get the results for this datastore               
+        // If alphabetical sorting, get the results for this datastore
         if (sortType == EAlphabetical)
             {
             filterHelper->GetResults(*(iMultiSearchResultsArr[dsIndex]));
             }
 
         elements.Reset();
-        supportedDataFields.Reset();
+        CleanupStack::PopAndDestroy( &supportedDataFields ); // Close
         }
-    aDataStores.ResetAndDestroy();
-    requiredDataFields.Reset();
+    CleanupStack::PopAndDestroy( &requiredDataFields ); // Close
+    CleanupStack::PopAndDestroy( &dataStores );         // ResetAndDestroy
 
     // If alphabetical sorting, merge the result sets of all datastores
     if (sortType == EAlphabetical)
         {
         // Form the complete searchResults array
-        CPcsAlgorithm2Utils::FormCompleteSearchResultsL(iMultiSearchResultsArr, searchResults);
+        CPcsAlgorithm2Utils::FormCompleteSearchResultsL(iMultiSearchResultsArr, 
+                                                        aSearchResults);
         }
     else
         {
         // Results are already sorted patternbased
-        filterHelper->GetResults(searchResults);
+        filterHelper->GetResults(aSearchResults);
         }
 
     // Get the sorted match sequence list
-    filterHelper->GetPatternsL(searchSeqs);
+    filterHelper->GetPatternsL(aSearchSeqs);
 
-    PRINT1 ( _L("Number of search results = %d"), searchResults.Count() );
+    PRINT1 ( _L("Number of search results = %d"), aSearchResults.Count() );
 
-    // Cleanup             
+    // Cleanup
     for (TInt i = 0; i < iMultiSearchResultsArr.Count(); i++)
         {
         iMultiSearchResultsArr[i]->Reset();
@@ -208,278 +205,148 @@
         }
 
     iMultiSearchResultsArr.Reset();
-    delete filterHelper;
+    CleanupStack::PopAndDestroy( &elements ); // Close
+    CleanupStack::PopAndDestroy( filterHelper );
+
+    //__LATENCY_MARKEND ( _L("CPcsAlgorithm2MultiSearchHelper::SearchMultiL") );
 
     PRINT ( _L("End CPcsAlgorithm2MultiSearchHelper::SearchMultiL") );
-    __LATENCY_MARKEND ( _L("CPcsAlgorithm2MultiSearchHelper::SearchMultiL") );
     }
 
 // ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::SearchInputMultiL
-// Function to search match sequences for multi query
+// CPcsAlgorithm1MultiSearchHelper::SearchMatchSeqMultiL
+// Function adds matches, and locations based on multi query, and data
+// Duplicate locations are allowed (as they are removed later anyway)
+// Post condition locations in index order
 // ----------------------------------------------------------------------------
-void CPcsAlgorithm2MultiSearchHelper::SearchMatchSeqMultiL(RPointerArray<CPsQuery>& aPsQuery, 
-                                                           TDesC& aData, 
-                                                           RPointerArray<TDesC>& aMatchSet,
-                                                           RArray<TPsMatchLocation>& aMatchLocation)
+void CPcsAlgorithm2MultiSearchHelper::SearchMatchSeqMultiL( RPointerArray<CPsQuery>& aPsQuery,
+                                                            const TDesC& aData,
+                                                            RPointerArray<TDesC>& aMatchSeq,
+                                                            RArray<TPsMatchLocation>& aMatchLocation )
     {
     PRINT ( _L("Enter CPcsAlgorithm2MultiSearchHelper::SearchMatchSeqMultiL") );
-    CleanupResetAndDestroyPushL( aMatchSet );
-    CleanupClosePushL( aMatchLocation );
-    
-    RPointerArray<HBufC> queryList;
-    ConvertQueryToListL(aPsQuery, queryList);
-
-    RPointerArray<HBufC> tempqueryList;
-    // Remember a temporary copy of query list
-    // since we sort the queries
-    for (TInt i = 0; i < queryList.Count(); i++)
-        {
-        tempqueryList.Append(queryList[i]);
-        }
-    // To hold the match results
-    RPointerArray<TDesC> tmpMatchSet;
-    TBool isMatch = ETrue;
-    TUint32 wordMatches = 0;
-
-    // Sort the query items before we search them
-    TLinearOrder<HBufC> rule(Compare2);
-    queryList.Sort(rule);
 
-    // Check for each query atleast one data element matches
-    // Loop from the last query so that longest match is seen first
-    for (TInt queryIndex = queryList.Count() - 1; queryIndex >= 0; queryIndex--)
+    TLex lex(aData);
+    while ( !lex.Eos() ) // Search thru all words
         {
-        TBool queryMatch = EFalse;
-        HBufC* tmpQuery = queryList[queryIndex];
-        // Get the original query mode corresponding to this query
-        TInt modeIndex = tempqueryList.Find(tmpQuery);
-
-        TLex lex(aData);
+        TPtrC currentWord = lex.NextToken(); // next word
 
-        // First word
-        TPtrC tmpData = lex.NextToken();
-
-        TInt beg = lex.Offset() - tmpData.Length(); // start index of match sequence
-
-        TInt wordIndex = -1;
-        // Search thru multiple words
-        while ((tmpData.Length() != 0) && (!queryMatch))
+        const TInt psQueryCount = aPsQuery.Count(); 
+        for ( TInt queryIndex = 0; queryIndex < psQueryCount; ++queryIndex )
             {
-            wordIndex++;
+            CPsQuery* currentQuery = aPsQuery[queryIndex];
 
-            TPtr ptr = tmpQuery->Des();
-
-            // Perform two checks.
-            // 1. Ensure that the word is not matched against any previous query
-            // 2. If it is the first match to the query
-            TBool isWordMatch = EFalse;
-            TReal val;
-            Math::Pow(val, 2, wordIndex);
-            isWordMatch = wordMatches & (TUint) val;
-
-            if (!isWordMatch)
+            RArray<TInt> matchPos;
+            CleanupClosePushL( matchPos );
+            RArray<TInt> matchLen;
+            CleanupClosePushL( matchLen );
+            
+            if ( iAlgorithm->FindUtilECE()->MatchRefineL( currentWord, *currentQuery, matchPos, matchLen, ETrue ) )
                 {
-                // Check if no word is matched till now for this query
-                if (!queryMatch)
+                // Some matches found. Add all of them to result array.
+                ASSERT( matchPos.Count() == matchLen.Count() );
+                
+                TInt wordStartPos = lex.Offset() - currentWord.Length();
+                const TInt matchPosCount =  matchPos.Count(); 
+                for ( TInt i = 0 ; i < matchPosCount; ++i )
                     {
-                    queryMatch = ETrue;
-                    //set the word match bit
-                    TReal val;
-                    Math::Pow(val, 2, wordIndex);
-                    wordMatches |= (TUint) val;
-                    }
+                    TPsMatchLocation newLocation = { wordStartPos + matchPos[i], matchLen[i], 
+                            TBidiText::TextDirectionality(currentWord) };
+                    aMatchLocation.AppendL( newLocation );
 
-                TPsMatchLocation tempLocation;
-                // check for directionality of the text
-                TBool found(EFalse);
-                TBidiText::TDirectionality dir = TBidiText::TextDirectionality(tmpData, &found);
-
-                tempLocation.index = beg;
-                tempLocation.length = 0;
-                tempLocation.direction = dir;
-
-                // Add the match location to the data structure array
-                aMatchLocation.Append(tempLocation);
+                    TPtrC matchPart = currentWord.Mid( matchPos[i], matchLen[i] );
+                    CPcsAlgorithm2Utils::AppendMatchToSeqL( aMatchSeq, matchPart );
+                    }
                 }
-            }
-        // Next word
-        tmpData.Set(lex.NextToken());
-        beg = lex.Offset() - tmpData.Length(); // start index of next word  
-
-
-        // No data element matches the query. Ignore this result.
-        if (queryMatch == EFalse)
-            {
-            isMatch = EFalse;
-            break;
+                
+            CleanupStack::PopAndDestroy( &matchLen );
+            CleanupStack::PopAndDestroy( &matchPos );
             }
         }
 
-    // Count the number of bits set
-    TInt matchCount = 0;
-    matchCount = BitsSet32(wordMatches);
-
-    // If match add the element to the result set
-    // Before adding to the result set, check if there is atleast one match per query
-    // Number of bits set in word matches is atleast equal to total number of queries.
-    if ((isMatch) && (matchCount >= queryList.Count()))
-        {
-
-        // Include the match sequences in the final results
-        for (int i = 0; i < tmpMatchSet.Count(); i++)
-            {
-            TIdentityRelation<TDesC> rule(Compare3);
-            CleanupStack::PushL(tmpMatchSet[i]);
-            if (aMatchSet.Find(tmpMatchSet[i], rule) == KErrNotFound)
-                {
-                aMatchSet.Append(tmpMatchSet[i]);
-                CleanupStack::Pop();
-                }
-            else
-                {
-                CleanupStack::PopAndDestroy();
-                }
-            }
-
-        // Reset tmp match set
-        tmpMatchSet.Reset();
-        }
-    else
-        {
-        tmpMatchSet.ResetAndDestroy();
-        }
-
-    // Free the query list
-    queryList.ResetAndDestroy();
-    tempqueryList.Reset();
-    
-    CleanupStack::Pop();
-    CleanupStack::Pop( &aMatchSet );
-
     PRINT ( _L("End CPcsAlgorithm2MultiSearchHelper::SearchMatchSeqMultiL") );
     }
 
 // ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::ConvertQueryToList
-// Converts the multiple search queries to a list
-// ----------------------------------------------------------------------------
-void CPcsAlgorithm2MultiSearchHelper::ConvertQueryToListL(RPointerArray<CPsQuery>& aSearchQuery, 
-                                                          RPointerArray<HBufC>& aQueryList)
-    {
-    for (int queryIndex = 0; queryIndex < aSearchQuery.Count(); queryIndex++)
-        {
-        CPsQuery* query = aSearchQuery[queryIndex];
-
-        HBufC* tmpSearchQuery = HBufC::NewL(KPsQueryMaxLen);
-        TPtr ptr = tmpSearchQuery->Des();
-
-        if (query->KeyboardModeL() == EItut) // ITU
-            {
-            keyMap->GetNumericKeyString(query->QueryAsStringLC(), ptr);
-            CleanupStack::PopAndDestroy();
-            }
-        else if (query->KeyboardModeL() == EQwerty) // QWERTY
-            {
-            ptr = query->QueryAsStringLC();
-            ptr.LowerCase();
-            CleanupStack::PopAndDestroy();
-            }
-        else // UNDEFINED
-            {
-            ExtractQueryL(*query, ptr);
-            ptr.LowerCase();
-            }
-        aQueryList.Append(tmpSearchQuery);
-        }
-    }
-
-// ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::ConvertdDataToKeyBoardModeL
-// Converts the input data to the key board mode specified by the query
-// ----------------------------------------------------------------------------
-void CPcsAlgorithm2MultiSearchHelper::ConvertdDataToKeyBoardModeL(CPsQuery* aQuery, 
-                                                                  TPtrC aInputData, 
-                                                                  TBuf<KPsQueryMaxLen>& aOutputData)
-    {
-    if (aQuery->KeyboardModeL() == EItut)
-        {
-        keyMap->GetNumericKeyString(aInputData, aOutputData);
-        }
-    else if (aQuery->KeyboardModeL() == EQwerty)
-        {
-        aOutputData = aInputData;
-        aOutputData.LowerCase();
-        }
-    else
-        {
-        ExtractQueryL(aInputData, *aQuery, aOutputData);
-        aOutputData.LowerCase();
-        }
-    }
-
-// ----------------------------------------------------------------------------
 // CPcsAlgorithm2MultiSearchHelper::FilterResultsMultiL
 // Subset search function. Refer the above function for more description.
 // ----------------------------------------------------------------------------
 void CPcsAlgorithm2MultiSearchHelper::FilterResultsMultiL(CPcsAlgorithm2FilterHelper* aAlgorithmFilterHelper, 
-                                                          RPointerArray<CPcsPoolElement>& searchSet,
-                                                          RPointerArray<CPsQuery>& searchQuery, 
+                                                          RPointerArray<CPcsPoolElement>& aSearchSet,
+                                                          RPointerArray<CPsQuery>& aSearchQuery, 
                                                           TUint8 aFilteredDataMatch,
-                                                          TBool isSearchInGroup, 
-                                                          RArray<TInt>& aContactsInGroup,
-                                                          TInt keyboardMode)
+                                                          TBool aIsSearchInGroup, 
+                                                          const RArray<TInt>& aContactsInGroup)
     {
     PRINT ( _L("Enter CPcsAlgorithm2MultiSearchHelper::FilterResultsMultiL") );
 
+    //__LATENCY_MARK ( _L("CPcsAlgorithm2MultiSearchHelper::FilterResultsMultiL") );
+
+    TInt maxcount = 0;
+    
     // Convert the individual queries to string form
     RPointerArray<CPsQuery> mySearchQuery;
+    CleanupResetAndDestroyPushL( mySearchQuery );
 
     // Remember a temporary copy of query list
     // Copy the content of searchQuery
-    for (TInt i=0; i<searchQuery.Count(); i++)
+    const TInt searchQueryCount = aSearchQuery.Count(); 
+    for (TInt i=0; i < searchQueryCount; i++)
         {
         CPsQuery* tempQuery = CPsQuery::NewL();
-        iAlgorithm->FindUtilECE()->GetPartOfQueryL(*(searchQuery[i]), 0, 
-                                                   searchQuery[i]->Count()-1, *tempQuery);
-        mySearchQuery.Append(tempQuery);
+        CleanupStack::PushL( tempQuery );
+        iAlgorithm->FindUtilECE()->GetPartOfQueryL(
+                *(aSearchQuery[i]), 0, aSearchQuery[i]->Count()-1, *tempQuery );
+        mySearchQuery.AppendL(tempQuery);
+        CleanupStack::Pop(tempQuery); // ownership transferred
         }
 
     // Sort the query items according to the length of each query 
-    TLinearOrder<CPsQuery> rule(Compare4);
+    TLinearOrder<CPsQuery> rule(CPcsAlgorithm2Utils::CompareLength);
     mySearchQuery.Sort(rule);
 
     // To hold the match results
     RPointerArray<TDesC> tmpMatchSet;
+    CleanupResetAndDestroyPushL( tmpMatchSet );
 
-    // Parse thru each search set elements and filter the results    
-    for (int index = 0; index < searchSet.Count(); index++)
+    // Parse thru each search set elements and filter the results
+    const TInt searchSetCount = aSearchSet.Count(); 
+    for (TInt index = 0; index < searchSetCount; index++)
         {
-        CPcsPoolElement* poolElement = static_cast<CPcsPoolElement*> (searchSet[index]);
+        CPcsPoolElement* poolElement = static_cast<CPcsPoolElement*> (aSearchSet[index]);
         CPsData* psData = poolElement->GetPsData();
         psData->ClearDataMatches();
 
+        // Skip the contact if we are doing a group search and contact doesn't belong to the group
+        if ( aIsSearchInGroup && 
+             aContactsInGroup.Find( psData->Id() ) == KErrNotFound )
+            {
+            continue;
+            }
+        
         TBool isMatch = ETrue;
-        TUint8 wordMatches = 0;
+        TInt wordMatches = 0;
 
         // Reset iWordMatches to zero 
         ClearWordMatches();
 
         // Check for each query atleast one data element matches
         // Loop from the last query so that longest match is seen first
-        for (TInt queryIndex = mySearchQuery.Count() - 1; queryIndex >= 0; queryIndex--)            
+        for (TInt queryIndex = mySearchQuery.Count() - 1; queryIndex >= 0; queryIndex--)
             {
             TBool queryMatch = EFalse;
             CPsQuery* tmpPsQuery = mySearchQuery[queryIndex];
 
-            for (TInt dataIndex = 0; dataIndex < psData->DataElementCount(); dataIndex++)
+            const TInt dataElementCount = psData->DataElementCount();
+            for (TInt dataIndex = 0; dataIndex < dataElementCount; dataIndex++)
                 {
                 // Filter off data fields not required in search
-                TReal bitIndex;
-                Math::Pow(bitIndex, 2, dataIndex);
-
-                TUint8 filter = (TUint8) bitIndex & aFilteredDataMatch;
-                if (filter == 0x0)
+                TUint8 bitIndex = 1 << dataIndex;
+                TUint8 filter = bitIndex & aFilteredDataMatch;
+                
+                // Omit the data fields which is not required in search
+                // or not matched with the pool element
+                if ( filter == 0x0 ) 
                     {
                     // Move to next data
                     continue;
@@ -497,27 +364,9 @@
                     {
                     wordIndex++;
                     
-                    TPtrC queryPtr = tmpPsQuery->QueryAsStringLC();
-                    TBool matched = EFalse;
+                    // Compare the data against query
+                    TBool matched = iAlgorithm->FindUtilECE()->MatchRefineL(tmpData, *tmpPsQuery);
                     
-                    if (keyboardMode == EModeUndefined)
-                        {
-                        matched = iAlgorithm->FindUtilECE()->MatchRefineL(tmpData, *tmpPsQuery);
-                        }
-                    else if (keyboardMode == EItut)
-                        {
-                        matched = iAlgorithm->FindUtil()->Interface()->
-                                  MatchRefineL(tmpData, queryPtr, ECustomConverter, iAlgorithm);
-                        }
-                    else // Qwerty
-                        {
-                        matched = iAlgorithm->FindUtil()->Interface()->MatchRefineL(tmpData, queryPtr);
-                        
-                        }
-                    
-                    CleanupStack::PopAndDestroy(); // queryPtr
-                    
-                    // Compare the data against query
                     if (matched)
                         {
                         psData->SetDataMatch(dataIndex);
@@ -540,25 +389,18 @@
 
                             // Extract matched character sequence and fill in temp array
                             TInt len = tmpPsQuery->Count();
-                            if (iAlgorithm->FindUtilECE()->IsChineseWord(tmpData))
+                            if (iAlgorithm->FindUtilECE()->IsChineseWordIncluded(tmpData))
                                 {
                                 len = 1;
                                 }
 
-                            HBufC* seq = HBufC::NewL(len);
-                            *seq = tmpData.Mid(0, len);
-
-                            seq->Des().UpperCase();
-                            TIdentityRelation<TDesC> searchRule(Compare3);
-                            if (tmpMatchSet.Find(seq, searchRule) == KErrNotFound)
-                                {
-                                tmpMatchSet.Append(seq);
-                                }
-                            else
-                                {
-                                delete seq;
-                                seq = NULL;
-                                }
+                            TPtrC seq = tmpData.Left(len);
+                            CPcsAlgorithm2Utils::AppendMatchToSeqL( tmpMatchSet, seq );
+                            
+                            // TODO: Match seqs could be extracted from actual
+                            //       match locations by using the other overload of
+                            //       CFindUtilChineseECE::MatchRefineL().
+                            //       Currently, match seq data is not used by clients.
                             }
                         }
 
@@ -574,112 +416,50 @@
                 break;
                 }
             }
+        
 
         // If match add the element to the result set
         //  And before adding to the result set, check if there is atleast one match per query
-        if ((isMatch) && (wordMatches >= mySearchQuery.Count()))
+        if ( isMatch && wordMatches >= mySearchQuery.Count() )
             {
-            if (isSearchInGroup)
-                {
-                if (aContactsInGroup.Find(psData->Id()) != KErrNotFound)
-                    {
-                    aAlgorithmFilterHelper->AddL(psData, tmpMatchSet);
-                    }
-                }
-            else
-                {
-                aAlgorithmFilterHelper->AddL(psData, tmpMatchSet);
-                }
+            aAlgorithmFilterHelper->AddL(psData, tmpMatchSet);
+            maxcount++;
             }
-
+        
+        if ( iMaxCount != -1 && maxcount > iMaxCount )
+            {
+            break;
+            }
+        
         // Cleanup the match sequence array as 
         // they are stored in pattern details structure
         tmpMatchSet.ResetAndDestroy();
         }
 
-    mySearchQuery.Reset();
+    CleanupStack::PopAndDestroy( &tmpMatchSet );   // ResetAndDestroy
+    CleanupStack::PopAndDestroy( &mySearchQuery ); // ResetAndDestroy
+
+    //__LATENCY_MARKEND ( _L("CPcsAlgorithm2MultiSearchHelper::FilterResultsMultiL") );
 
     PRINT ( _L("End CPcsAlgorithm2MultiSearchHelper::FilterResultsMultiL") );
     }
 
 // ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::SetWordMap()
+// CPcsAlgorithm2MultiSearchHelper::SetWordMap
 // ----------------------------------------------------------------------------
 void CPcsAlgorithm2MultiSearchHelper::SetWordMap(TInt aIndex, TInt aPosition)
     {
-    TReal val;
-    Math::Pow(val, 2, aPosition);
-
-    iWordMatches[aIndex] |= (TUint8) val;
-    }
-
-// ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::IsWordMatch()
-// ----------------------------------------------------------------------------
-TBool CPcsAlgorithm2MultiSearchHelper::IsWordMatch(TInt aDataIndex, TInt aWordIndex)
-    {
-    TReal val;
-    Math::Pow(val, 2, aWordIndex);
-
-    return (iWordMatches[aDataIndex] & (TUint8) val);
+    TUint8 val = 1 << aPosition;
+    iWordMatches[aIndex] |= val;
     }
 
 // ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::ExtractQueryL()
-// Extracts the query as a string. If the mode of query item is ITU numeric
-// character is used. Else the character is used.
-// ----------------------------------------------------------------------------
-void CPcsAlgorithm2MultiSearchHelper::ExtractQueryL(CPsQuery& aQuery, TDes& aOutput)
-    {
-    for (int i = 0; i < aQuery.Count(); i++)
-        {
-        if (aQuery.GetItemAtL(i).Mode() == EItut)
-            {
-            TBuf<KPsQueryMaxLen> outBuf;
-            keyMap->GetNumericKeyString(aQuery.QueryAsStringLC(), outBuf);
-            aOutput.Append(outBuf[i]);
-            CleanupStack::PopAndDestroy();
-            }
-        else
-            {
-            aOutput.Append(aQuery.GetItemAtL(i).Character());
-            }
-        }
-    }
-
-// ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::ExtractQueryL()
-// Converts the input data refering the modes in the query.
-// If the mode of query item is ITU numeric character is used. Else the character
-// is used.
+// CPcsAlgorithm2MultiSearchHelper::IsWordMatch
 // ----------------------------------------------------------------------------
-void CPcsAlgorithm2MultiSearchHelper::ExtractQueryL(TDesC& aInput, CPsQuery& aQuery, TDes& aOutput)
+TBool CPcsAlgorithm2MultiSearchHelper::IsWordMatch(TInt aDataIndex, TInt aWordIndex)
     {
-    TInt len = -1;
-
-    // Always loop thru the lowest length
-    if (aInput.Length() > aQuery.Count())
-        {
-        len = aQuery.Count();
-        }
-    else
-        {
-        len = aInput.Length();
-        }
-
-    for (int i = 0; i < len; i++)
-        {
-        if (aQuery.GetItemAtL(i).Mode() == EItut)
-            {
-            TBuf<KPsQueryMaxLen> outBuf;
-            keyMap->GetNumericKeyString(aInput, outBuf);
-            aOutput.Append(outBuf[i]);
-            }
-        else
-            {
-            aOutput.Append(aInput[i]);
-            }
-        }
+    TUint8 val = 1 << aWordIndex;
+    return (iWordMatches[aDataIndex] & val);
     }
 
 // ----------------------------------------------------------------------------
@@ -699,7 +479,7 @@
     }
 
 // ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::BitsSet32
+// CPcsAlgorithm2MultiSearchHelper::ClearWordMatches
 // Function to reset the iWordMatches
 // ----------------------------------------------------------------------------
 void CPcsAlgorithm2MultiSearchHelper::ClearWordMatches()
@@ -729,13 +509,14 @@
             {
             // Scan the end of the word
             TInt end = beg;
-            for (; end < textLength && !aQuery.GetItemAtL(end).Character().IsSpace(); ++end)
+            while ( end < textLength && !aQuery.GetItemAtL(end).Character().IsSpace() )
                 {
+                end++;
                 }
 
             // Create a new query object and append
             CPsQuery* newQuery = CPsQuery::NewL();
-            for (int i = beg; i < end; i++)
+            for (TInt i = beg; i < end; i++)
                 {
                 CPsQueryItem* item = CPsQueryItem::NewL();
                 item->SetCharacter(aQuery.GetItemAtL(i).Character());
@@ -752,35 +533,6 @@
     return query;
     }
 
-// ----------------------------------------------------------------------------
-// CPcsAlgorithm2MultiSearchHelper::FilterDataFieldsL()
-// Constructs a bit pattern using the required/supported data fields
-// For example, 6, 4 and 27 are supported fields <-- 00000111
-//              6 and 4 are required fields      <-- 00000011
-// Bit pattern returned is 00000011.
-// ----------------------------------------------------------------------------
-TUint8 CPcsAlgorithm2MultiSearchHelper::FilterDataFieldsL(RArray<TInt>& aRequiredDataFields, 
-                                                          RArray<TInt>& aSupportedDataFields)
-    {
-    TUint8 filteredMatch = 0x0;
-
-    for (int i = 0; i < aSupportedDataFields.Count(); i++)
-        {
-        for (int j = 0; j < aRequiredDataFields.Count(); j++)
-            {
-            if (aSupportedDataFields[i] == aRequiredDataFields[j])
-                {
-                TReal val;
-                Math::Pow(val, 2, i);
-
-                filteredMatch |= (TUint8) val;
-                }
-            }
-        }
-
-    return filteredMatch;
-    }
-
 // End of file