predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsAlgorithm2FilterHelper.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
child 68 9da50d567e3c
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: Helper class to filter results
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 #include "CPcsAlgorithm2FilterHelper.h"
       
    20 #include "CPcsDebug.h"
       
    21 
       
    22 // Compare functions
       
    23 TInt ComparePsPattern(const TPsPatternDetails& aFirst,
       
    24                       const TPsPatternDetails& aSecond)
       
    25     {
       
    26     return (CPcsAlgorithm2Utils::MyCompareC(*(aFirst.matchPattern),
       
    27                                             *(aSecond.matchPattern)));
       
    28 
       
    29     }
       
    30 
       
    31 // ============================== MEMBER FUNCTIONS ============================
       
    32 
       
    33 // ----------------------------------------------------------------------------
       
    34 // CPcsAlgorithm2FilterHelper::NewL
       
    35 // Two Phase Construction
       
    36 // ----------------------------------------------------------------------------
       
    37 CPcsAlgorithm2FilterHelper* CPcsAlgorithm2FilterHelper::NewL(TSortType aSortType)
       
    38     {
       
    39     PRINT ( _L("Enter CPcsAlgorithm2FilterHelper::NewL") );
       
    40 
       
    41     CPcsAlgorithm2FilterHelper* self = new (ELeave) CPcsAlgorithm2FilterHelper();
       
    42     CleanupStack::PushL(self);
       
    43     self->ConstructL(aSortType);
       
    44     CleanupStack::Pop(self);
       
    45 
       
    46     PRINT ( _L("End CPcsAlgorithm2FilterHelper::NewL") );
       
    47 
       
    48     return self;
       
    49     }
       
    50 
       
    51 // ----------------------------------------------------------------------------
       
    52 // CPcsAlgorithm2FilterHelper::CPcsAlgorithm2FilterHelper
       
    53 // Two Phase Construction
       
    54 // ----------------------------------------------------------------------------
       
    55 CPcsAlgorithm2FilterHelper::CPcsAlgorithm2FilterHelper()
       
    56     {
       
    57     PRINT ( _L("Enter CPcsAlgorithm2FilterHelper::CPcsAlgorithm2FilterHelper") );
       
    58     PRINT ( _L("End CPcsAlgorithm2FilterHelper::CPcsAlgorithm2FilterHelper") );
       
    59     }
       
    60 
       
    61 // ----------------------------------------------------------------------------
       
    62 // CPcsAlgorithm2FilterHelper::ConstructL
       
    63 // Two Phase Construction
       
    64 // ----------------------------------------------------------------------------
       
    65 void CPcsAlgorithm2FilterHelper::ConstructL(TSortType aSortType)
       
    66     {
       
    67     PRINT ( _L("Enter CPcsAlgorithm2FilterHelper::ConstructL") );
       
    68 
       
    69     iSortType = aSortType;
       
    70     iResultCount = -1;
       
    71 
       
    72     PRINT ( _L("End CPcsAlgorithm2FilterHelper::ConstructL") );
       
    73     }
       
    74 
       
    75 // ----------------------------------------------------------------------------
       
    76 // CPcsAlgorithm2FilterHelper::~CPcsAlgorithm2FilterHelper
       
    77 // Destructor
       
    78 // ----------------------------------------------------------------------------
       
    79 CPcsAlgorithm2FilterHelper::~CPcsAlgorithm2FilterHelper()
       
    80     {
       
    81     PRINT ( _L("Enter CPcsAlgorithm2FilterHelper::~CPcsAlgorithm2FilterHelper") );
       
    82 
       
    83     // delete match pattern pools
       
    84     for (TInt i = 0; i < iMatchPatternPools.Count(); i++)
       
    85         {
       
    86         iMatchPatternPools[i]->Reset();
       
    87         delete iMatchPatternPools[i];
       
    88         iMatchPatternPools[i] = NULL;
       
    89         }
       
    90     iMatchPatternPools.Reset();
       
    91 
       
    92     // delete match pattern details
       
    93     for (TInt i = 0; i < iMatchPatternDetails.Count(); i++)
       
    94         {
       
    95         delete iMatchPatternDetails[i]->matchPattern;
       
    96         for (TInt j = 0; j < iMatchPatternDetails[i]->subPatternArray.Count(); j++)
       
    97             {
       
    98             delete iMatchPatternDetails[i]->subPatternArray[j]->matchPattern;
       
    99             iMatchPatternDetails[i]->subPatternArray[j]->matchPattern = NULL;
       
   100             }
       
   101         iMatchPatternDetails[i]->subPatternArray.ResetAndDestroy();
       
   102         }
       
   103     iMatchPatternDetails.ResetAndDestroy();
       
   104 
       
   105     PRINT ( _L("End CPcsAlgorithm2FilterHelper::~CPcsAlgorithm2FilterHelper") );
       
   106     }
       
   107 
       
   108 // ----------------------------------------------------------------------------
       
   109 // CPcsAlgorithm2FilterHelper::AddL
       
   110 // 
       
   111 // ----------------------------------------------------------------------------
       
   112 void CPcsAlgorithm2FilterHelper::AddL(CPsData* aPsData, RPointerArray<TDesC>& aPatternSequence)
       
   113     {
       
   114     if (iSortType == EAlphabetical)
       
   115         {
       
   116         // Create pool if not already present 
       
   117         if (iMatchPatternPools.Count() == 0)
       
   118             {
       
   119             RPointerArray<CPsData> *tempPsDataArray = new (ELeave) RPointerArray<CPsData> ();
       
   120             CleanupStack::PushL( tempPsDataArray );
       
   121             iMatchPatternPools.AppendL(tempPsDataArray);
       
   122             CleanupStack::Pop( tempPsDataArray );
       
   123             }
       
   124         // Add result to the result set
       
   125         iMatchPatternPools[0]->AppendL(aPsData);
       
   126 
       
   127         // Update the count
       
   128         iResultCount++;
       
   129 
       
   130         // Update sequence list
       
   131         const TInt patternSequenceCount = aPatternSequence.Count();
       
   132         for (TInt i = 0; i < patternSequenceCount; i++)
       
   133             {
       
   134             TInt index = FindSequence(*aPatternSequence[i]);
       
   135             if (index == KErrNotFound)
       
   136                 {
       
   137                 // sequence not found, add it to array
       
   138                 TPsPatternDetails* temp = new (ELeave) TPsPatternDetails;
       
   139                 CleanupStack::PushL( temp );
       
   140                 
       
   141                 temp->matchPattern = aPatternSequence[i]->AllocL();
       
   142 
       
   143                 // First occurence should be -1 for alphabetical sort
       
   144                 // and pool index will be 0 as only one pool will be created
       
   145                 temp->firstOccurence = -1;
       
   146                 temp->poolIndex = 0;
       
   147 
       
   148                 iMatchPatternDetails.AppendL(temp);
       
   149                 CleanupStack::Pop( temp );
       
   150                 }
       
   151             }
       
   152         }
       
   153     else
       
   154         {
       
   155         // PatternBased sort
       
   156         TInt index = AddToPoolL(aPsData, aPatternSequence);
       
   157 
       
   158         // Increment the total count
       
   159         iResultCount++;
       
   160         }
       
   161     }
       
   162 
       
   163 // ----------------------------------------------------------------------------
       
   164 // CPcsAlgorithm2FilterHelper::AddToPoolL
       
   165 // 
       
   166 // ----------------------------------------------------------------------------
       
   167 
       
   168 TInt CPcsAlgorithm2FilterHelper::AddToPoolL(CPsData* aPsData, RPointerArray<TDesC>& aPatternSequence)
       
   169     {
       
   170 
       
   171     // Sort the pattern sequence
       
   172     TLinearOrder<TDesC> rule(CPcsAlgorithm2Utils::MyCompareC);
       
   173     aPatternSequence.Sort(rule);
       
   174 
       
   175     // Sort rule        
       
   176     TLinearOrder<CPsData> psDataRule(CPcsAlgorithm2Utils::CompareDataBySortOrder);
       
   177 
       
   178     // Search Rule
       
   179     TIdentityRelation<CPsData> identitySearchRule(CPsData::CompareById);
       
   180 
       
   181     TInt poolItemCount = -1;
       
   182     TInt matchpatterpoolIndexToAppend = -1;
       
   183 
       
   184     const TInt patternSequenceCount = aPatternSequence.Count();
       
   185     for (TInt cnt = 0; cnt < patternSequenceCount; cnt++)
       
   186         {
       
   187 
       
   188         // Create the pattern for aPatternSequence[cnt] in  iMatchPatternDetails
       
   189         // and return the index
       
   190         TInt indexInMatchPatternDetails = CreateMatchPatternDetailsAndPoolsL(*aPatternSequence[cnt]);
       
   191 
       
   192         //Add the data to the pool the first pattern pool.
       
   193         // The data should be added only once
       
   194         if (cnt == 0)
       
   195             {
       
   196             TInt poolIndex = iMatchPatternDetails[indexInMatchPatternDetails]->poolIndex;
       
   197             matchpatterpoolIndexToAppend = indexInMatchPatternDetails;
       
   198             iMatchPatternPools[poolIndex]->InsertInOrderAllowRepeats(aPsData, psDataRule);
       
   199             TInt findposition = iMatchPatternPools[poolIndex]->Find(aPsData, identitySearchRule);
       
   200             if (findposition != KErrNotFound)
       
   201                 {
       
   202                 poolItemCount = findposition;
       
   203                 }
       
   204             else
       
   205                 {
       
   206                 poolItemCount = -1;
       
   207                 }
       
   208 
       
   209             }
       
   210         else if (cnt > 0)
       
   211             {
       
   212             // Check if aPatternSequence[cnt] is listed in subpatterns of aPatternSequence[0]
       
   213             // If not, then create a sub pattern and apped it to  iMatchPatternDetails for aPatternSequence[0]
       
   214             TInt subSeq = FindSubSequence(*aPatternSequence[cnt], matchpatterpoolIndexToAppend);
       
   215             if (subSeq == KErrNotFound)
       
   216                 {
       
   217                 //Create the subpattern and append it.
       
   218                 TPsSubPattern* tempSubPattern = new (ELeave) TPsSubPattern;
       
   219                 tempSubPattern->matchPattern = HBufC::NewL(aPatternSequence[cnt]->Length());
       
   220                 tempSubPattern->matchPattern->Des().Copy(*(aPatternSequence[cnt]));
       
   221                 tempSubPattern->firstOccurenceInThisPattern = poolItemCount;
       
   222                 iMatchPatternDetails[matchpatterpoolIndexToAppend]->subPatternArray.AppendL(tempSubPattern);
       
   223                 }
       
   224             }
       
   225         }
       
   226 
       
   227     return 0;
       
   228     }
       
   229 
       
   230 // ----------------------------------------------------------------------------
       
   231 // CPcsAlgorithm2FilterHelper::CreateMatchPatternDetailsAndPoolsL
       
   232 // Creates the Matchpattern aSeq in  iMatchPatternDetails.
       
   233 // Returns the index of the sequence in iMatchPatternDetails
       
   234 // ----------------------------------------------------------------------------
       
   235 
       
   236 TInt CPcsAlgorithm2FilterHelper::CreateMatchPatternDetailsAndPoolsL(const TDesC& aSeq)
       
   237     {
       
   238 
       
   239     TInt indexInMatchPatternDetails = FindSequence(aSeq);
       
   240     if (indexInMatchPatternDetails == KErrNotFound)
       
   241         {
       
   242         // sequence not found, add it to array
       
   243         TPsPatternDetails* tempPatternDetailsInstance = new (ELeave) TPsPatternDetails;
       
   244 
       
   245         //TInt len = aPatternSequence[cnt]->Length();
       
   246         tempPatternDetailsInstance->matchPattern = aSeq.Alloc();
       
   247         tempPatternDetailsInstance->firstOccurence = -1; //RAVIKIRAN
       
   248 
       
   249         // Pools doesn't exist for this sequence..
       
   250         RPointerArray<CPsData> *tempPsDataArray = new (ELeave) RPointerArray<CPsData> ();
       
   251 
       
   252         tempPatternDetailsInstance->poolIndex = iMatchPatternPools.Count();
       
   253         iMatchPatternDetails.Append(tempPatternDetailsInstance);
       
   254 
       
   255         iMatchPatternPools.Append(tempPsDataArray);
       
   256 
       
   257         return (iMatchPatternDetails.Count() - 1);
       
   258         }
       
   259     else
       
   260         {
       
   261         // Pool already exists for this sequence, get the correct index and add the result
       
   262         return indexInMatchPatternDetails;
       
   263         }
       
   264 
       
   265     }
       
   266 
       
   267 // ----------------------------------------------------------------------------
       
   268 // CPcsAlgorithm2FilterHelper::GetResults
       
   269 // 
       
   270 // ----------------------------------------------------------------------------
       
   271 
       
   272 void CPcsAlgorithm2FilterHelper::GetResults(RPointerArray<CPsData>& aSearchResults)
       
   273     {
       
   274     // No results, simply return
       
   275     if (iResultCount == -1)
       
   276         {
       
   277         return;
       
   278         }
       
   279 
       
   280     // Sort sequences
       
   281     TLinearOrder<TPsPatternDetails> rule(ComparePsPattern);
       
   282     iMatchPatternDetails.Sort(rule);
       
   283 
       
   284     if (iSortType == EAlphabetical)
       
   285         {
       
   286         // Return the results stored in single array
       
   287         const TInt cnt = iMatchPatternPools[0]->Count();
       
   288         for (TInt i = 0; i < cnt; i++)
       
   289             {
       
   290             aSearchResults.Append((*(iMatchPatternPools[0]))[i]);
       
   291             }
       
   292 
       
   293         // Clear the match pattern pool for the next data store
       
   294         iMatchPatternPools[0]->Reset();
       
   295         }
       
   296     else
       
   297         {
       
   298         TInt firstOccurenceTracker = 0;
       
   299         const TInt matchPatternDetailsCount = iMatchPatternDetails.Count();
       
   300         for (TInt i = 0; i < matchPatternDetailsCount; i++)
       
   301             {
       
   302             // Get the mapping pool index for this sequence
       
   303             TInt index = iMatchPatternDetails[i]->poolIndex;
       
   304             TInt j = 0;
       
   305             const TInt poolCount = iMatchPatternPools[index]->Count();
       
   306             for (; j < poolCount; j++)
       
   307                 {
       
   308                 aSearchResults.Append((*(iMatchPatternPools[index]))[j]);
       
   309                 }
       
   310 
       
   311             // Check the sub patterns
       
   312             const TInt subPatternArrayCount =  iMatchPatternDetails[i]->subPatternArray.Count();
       
   313             for (TInt k = 0; k < subPatternArrayCount; k++)
       
   314                 {
       
   315                 HBufC* temporseq = iMatchPatternDetails[i]->subPatternArray[k]->matchPattern;
       
   316                 TRAP_IGNORE( UpdateForSubSequencesL( *temporseq, 
       
   317                     firstOccurenceTracker + 
       
   318                     iMatchPatternDetails[i]->subPatternArray[k]->firstOccurenceInThisPattern ) );
       
   319                 }
       
   320 
       
   321             if (iMatchPatternDetails[i]->firstOccurence == -1)
       
   322                 {
       
   323                 iMatchPatternDetails[i]->firstOccurence = firstOccurenceTracker;
       
   324                 }
       
   325 
       
   326             firstOccurenceTracker = firstOccurenceTracker + j;
       
   327 
       
   328             }
       
   329         }
       
   330     }
       
   331 
       
   332 // ----------------------------------------------------------------------------
       
   333 // CPcsAlgorithm2FilterHelper::GetPatternsL
       
   334 // 
       
   335 // ----------------------------------------------------------------------------
       
   336 void CPcsAlgorithm2FilterHelper::GetPatternsL(
       
   337         RPointerArray<CPsPattern>& aPatternSet)
       
   338     {
       
   339     // Sort sequences
       
   340     TLinearOrder<TPsPatternDetails> rule(ComparePsPattern);
       
   341     iMatchPatternDetails.Sort(rule);
       
   342 
       
   343     const TInt matchPatternDetailsCount = iMatchPatternDetails.Count(); 
       
   344     for (TInt i = 0; i < matchPatternDetailsCount; i++)
       
   345         {
       
   346         CPsPattern *tempPattern = CPsPattern::NewL();
       
   347 
       
   348         // Copy the sequence
       
   349         tempPattern->SetPatternL(*(iMatchPatternDetails[i]->matchPattern));
       
   350         tempPattern->SetFirstIndex(iMatchPatternDetails[i]->firstOccurence);
       
   351 
       
   352         aPatternSet.Append(tempPattern);
       
   353         }
       
   354     }
       
   355 
       
   356 // ----------------------------------------------------------------------------
       
   357 // CPcsAlgorithm2FilterHelper::FindSequence
       
   358 // Searches for for aSeq in iMatchPatternDetails and returns index 
       
   359 //  where input sequence is found. Returns -1 if not found
       
   360 // ----------------------------------------------------------------------------
       
   361 TInt CPcsAlgorithm2FilterHelper::FindSequence(const TDesC& aSeq)
       
   362     {
       
   363     TInt j = 0;
       
   364     const TInt matchPatternDetailsCount =  iMatchPatternDetails.Count();
       
   365     for (; j < matchPatternDetailsCount; j++)
       
   366         {
       
   367         const TDesC& matchPattern = *(iMatchPatternDetails[j]->matchPattern);
       
   368         if ( CPcsAlgorithm2Utils::MyCompareC(aSeq, matchPattern) == 0 )
       
   369             break;
       
   370         }
       
   371 
       
   372     if (j == iMatchPatternDetails.Count())
       
   373         return KErrNotFound;
       
   374     else
       
   375         return j;
       
   376     }
       
   377 
       
   378 // ----------------------------------------------------------------------------
       
   379 // CPcsAlgorithm2FilterHelper::FindSubSequence
       
   380 // Searches for for seq in subpatterns of iMatchPatternDetails[aPatternIndex]
       
   381 // and returns index where input sequence is found. 
       
   382 // Returns -1 if not found
       
   383 // ----------------------------------------------------------------------------
       
   384 TInt CPcsAlgorithm2FilterHelper::FindSubSequence(const TDesC& aSeq, TInt aPatternIndex)
       
   385     {
       
   386     TInt j = 0;
       
   387     const TInt subPatternArraycount =  iMatchPatternDetails[aPatternIndex]->subPatternArray.Count();
       
   388     for (; j < subPatternArraycount; j++)
       
   389         {
       
   390         const TDesC& matchPattern = *(iMatchPatternDetails[aPatternIndex]->subPatternArray[j]->matchPattern);
       
   391 	    if ( CPcsAlgorithm2Utils::MyCompareC(aSeq, matchPattern) == 0 ) 
       
   392             break;
       
   393         }
       
   394 
       
   395     if (j == iMatchPatternDetails[aPatternIndex]->subPatternArray.Count())
       
   396         {
       
   397         return KErrNotFound;
       
   398         }
       
   399     else
       
   400         {
       
   401         return j;
       
   402         }
       
   403     }
       
   404 
       
   405 // ----------------------------------------------------------------------------
       
   406 // CPcsAlgorithm2FilterHelper::UpdateForSubSequencesL
       
   407 // Searches for all the subsequences of aSeq and updates the aFirstOccrVal in 
       
   408 // iMatchPatternDetails.
       
   409 //
       
   410 // For ex: if aSeq = "ABC" and aFirstOccrVal= 2, 
       
   411 // then, firstOccurence of all substring sequences ("A", "AB", "ABC") 
       
   412 //       in iMatchPatternDetails will be set to 2
       
   413 // ----------------------------------------------------------------------------
       
   414 void CPcsAlgorithm2FilterHelper::UpdateForSubSequencesL(const TDesC& aSeq, TInt aFirstOccrVal)
       
   415     {
       
   416     TPtrC tempSequence;
       
   417     for ( TInt i = 1; i <= aSeq.Length(); i++ )
       
   418         {
       
   419         // Get the next substring in tempSequence
       
   420         tempSequence.Set( aSeq.Left(i) );
       
   421 
       
   422         // Find if this sequence exist in iMatchPatternDetails
       
   423         TInt patternIndex = FindSequence(tempSequence);
       
   424 
       
   425         //If the sequence exits and firstOccurence == -1, then set it to aFirstOccrVal 
       
   426         if ((patternIndex != KErrNotFound)&& (iMatchPatternDetails[patternIndex]->firstOccurence == -1))
       
   427             {
       
   428             iMatchPatternDetails[patternIndex]->firstOccurence = aFirstOccrVal;
       
   429             }
       
   430 
       
   431         }
       
   432     }
       
   433 // END OF FILE
       
   434