predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsAlgorithm2FilterHelper.cpp
changeset 0 e686773b3f54
child 58 d4f567ce2e7c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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 TBool 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* psData, 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             iMatchPatternPools.Append(tempPsDataArray);
       
   121             }
       
   122         // Add result to the result set
       
   123         iMatchPatternPools[0]->Append(psData);
       
   124 
       
   125         // Update the count
       
   126         iResultCount++;
       
   127 
       
   128         // Update sequence list
       
   129         for (TInt i = 0; i < aPatternSequence.Count(); i++)
       
   130             {
       
   131             TInt index = FindSequence(aPatternSequence[i]);
       
   132             if (index == KErrNotFound)
       
   133                 {
       
   134                 // sequence not found, add it to array
       
   135                 TPsPatternDetails* temp = new (ELeave) TPsPatternDetails;
       
   136 
       
   137                 TInt len = aPatternSequence[i]->Length();
       
   138                 temp->matchPattern = HBufC::NewL(len);
       
   139                 temp->matchPattern->Des().Copy(*(aPatternSequence[i]));
       
   140 
       
   141                 // First occurence should be -1 for alphabetical sort
       
   142                 // and pool index will be 0 as only one pool will be created
       
   143                 temp->firstOccurence = -1;
       
   144                 temp->poolIndex = 0;
       
   145 
       
   146                 iMatchPatternDetails.Append(temp);
       
   147                 }
       
   148             }
       
   149         }
       
   150     else
       
   151         {
       
   152         // PatternBased sort
       
   153         TInt index = AddToPoolL(psData, aPatternSequence);
       
   154 
       
   155         // Increment the total count
       
   156         iResultCount++;
       
   157 
       
   158         }
       
   159     }
       
   160 
       
   161 // ----------------------------------------------------------------------------
       
   162 // CPcsAlgorithm2FilterHelper::AddToPoolL
       
   163 // 
       
   164 // ----------------------------------------------------------------------------
       
   165 
       
   166 TInt CPcsAlgorithm2FilterHelper::AddToPoolL(CPsData* psData, RPointerArray<TDesC>& aPatternSequence)
       
   167     {
       
   168 
       
   169     // Sort the pattern sequence
       
   170     TLinearOrder<TDesC> rule(CPcsAlgorithm2Utils::MyCompareC);
       
   171     aPatternSequence.Sort(rule);
       
   172 
       
   173     // Sort rule        
       
   174     TLinearOrder<CPsData> psDataRule(CPcsAlgorithm2Utils::CompareDataBySortOrder);
       
   175 
       
   176     // Search Rule
       
   177     TIdentityRelation<CPsData> identitySearchRule(CPsData::CompareById);
       
   178 
       
   179     TInt poolItemCount = -1;
       
   180     TInt matchpatterpoolIndexToAppend = -1;
       
   181 
       
   182     for (TInt cnt = 0; cnt < aPatternSequence.Count(); cnt++)
       
   183         {
       
   184 
       
   185         // Create the pattern for aPatternSequence[cnt] in  iMatchPatternDetails
       
   186         // and return the index
       
   187         TInt indexInMatchPatternDetails = CreateMatchPatternDetailsAndPoolsL(aPatternSequence[cnt]);
       
   188 
       
   189         //Add the data to the pool the first pattern pool.
       
   190         // The data should be added only once
       
   191         if (cnt == 0)
       
   192             {
       
   193             TInt poolIndex = iMatchPatternDetails[indexInMatchPatternDetails]->poolIndex;
       
   194             matchpatterpoolIndexToAppend = indexInMatchPatternDetails;
       
   195             iMatchPatternPools[poolIndex]->InsertInOrderAllowRepeats(psData, psDataRule);
       
   196             TInt findposition = iMatchPatternPools[poolIndex]->Find(psData, identitySearchRule);
       
   197             if (findposition != KErrNotFound)
       
   198                 {
       
   199                 poolItemCount = findposition;
       
   200                 }
       
   201             else
       
   202                 {
       
   203                 poolItemCount = -1;
       
   204                 }
       
   205 
       
   206             }
       
   207         else if (cnt > 0)
       
   208             {
       
   209             // Check if aPatternSequence[cnt] is listed in subpatterns of aPatternSequence[0]
       
   210             // If not, then create a sub pattern and apped it to  iMatchPatternDetails for aPatternSequence[0]
       
   211             TInt subSeq = FindSubSequence(aPatternSequence[cnt], matchpatterpoolIndexToAppend);
       
   212             if (subSeq == KErrNotFound)
       
   213                 {
       
   214                 //Create the subpattern and append it.
       
   215                 TPsSubPattern* tempSubPattern = new (ELeave) TPsSubPattern;
       
   216                 tempSubPattern->matchPattern = HBufC::NewL(aPatternSequence[cnt]->Length());
       
   217                 tempSubPattern->matchPattern->Des().Copy(*(aPatternSequence[cnt]));
       
   218                 tempSubPattern->firstOccurenceInThisPattern = poolItemCount;
       
   219                 iMatchPatternDetails[matchpatterpoolIndexToAppend]->subPatternArray.AppendL(tempSubPattern);
       
   220                 }
       
   221             }
       
   222         }
       
   223 
       
   224     return 0;
       
   225     }
       
   226 
       
   227 // ----------------------------------------------------------------------------
       
   228 // CPcsAlgorithm2FilterHelper::CreateMatchPatternDetailsAndPoolsL
       
   229 // Creates the Matchpattern aSeq in  iMatchPatternDetails.
       
   230 // Returns the index of the sequence in iMatchPatternDetails
       
   231 // ----------------------------------------------------------------------------
       
   232 
       
   233 TInt CPcsAlgorithm2FilterHelper::CreateMatchPatternDetailsAndPoolsL(TDesC* aSeq)
       
   234     {
       
   235 
       
   236     TInt indexInMatchPatternDetails = FindSequence(aSeq);
       
   237     if (indexInMatchPatternDetails == KErrNotFound)
       
   238         {
       
   239         // sequence not found, add it to array
       
   240         TPsPatternDetails* tempPatternDetailsInstance = new (ELeave) TPsPatternDetails;
       
   241 
       
   242         //TInt len = aPatternSequence[cnt]->Length();
       
   243         tempPatternDetailsInstance->matchPattern = HBufC::NewL(aSeq->Length());
       
   244         tempPatternDetailsInstance->matchPattern->Des().Copy(*(aSeq));
       
   245         tempPatternDetailsInstance->firstOccurence = -1; //RAVIKIRAN
       
   246 
       
   247         // Pools doesn't exist for this sequence..
       
   248         RPointerArray<CPsData> *tempPsDataArray = new (ELeave) RPointerArray<CPsData> ();
       
   249 
       
   250         tempPatternDetailsInstance->poolIndex = iMatchPatternPools.Count();
       
   251         iMatchPatternDetails.Append(tempPatternDetailsInstance);
       
   252 
       
   253         iMatchPatternPools.Append(tempPsDataArray);
       
   254 
       
   255         return (iMatchPatternDetails.Count() - 1);
       
   256         }
       
   257     else
       
   258         {
       
   259         // Pool already exists for this sequence, get the correct index and add the result
       
   260         return indexInMatchPatternDetails;
       
   261         }
       
   262 
       
   263     }
       
   264 
       
   265 // ----------------------------------------------------------------------------
       
   266 // CPcsAlgorithm2FilterHelper::GetResults
       
   267 // 
       
   268 // ----------------------------------------------------------------------------
       
   269 
       
   270 void CPcsAlgorithm2FilterHelper::GetResults(RPointerArray<CPsData>& aSearchResults)
       
   271     {
       
   272     // No results, simply return
       
   273     if (iResultCount == -1)
       
   274         {
       
   275         return;
       
   276         }
       
   277 
       
   278     // Sort sequences
       
   279     TLinearOrder<TPsPatternDetails> rule(ComparePsPattern);
       
   280     iMatchPatternDetails.Sort(rule);
       
   281 
       
   282     if (iSortType == EAlphabetical)
       
   283         {
       
   284         // Return the results stored in single array
       
   285         for (TInt i = 0; i < iMatchPatternPools[0]->Count(); i++)
       
   286             {
       
   287             aSearchResults.Append((*(iMatchPatternPools[0]))[i]);
       
   288             }
       
   289 
       
   290         // Clear the match pattern pool for the next data store
       
   291         iMatchPatternPools[0]->Reset();
       
   292         }
       
   293     else
       
   294         {
       
   295         TInt firstOccurenceTracker = 0;
       
   296         for (TInt i = 0; i < iMatchPatternDetails.Count(); i++)
       
   297             {
       
   298             // Get the mapping pool index for this sequence
       
   299             TInt index = iMatchPatternDetails[i]->poolIndex;
       
   300             TInt j = 0;
       
   301             for (; j < iMatchPatternPools[index]->Count(); j++)
       
   302                 {
       
   303                 aSearchResults.Append((*(iMatchPatternPools[index]))[j]);
       
   304                 }
       
   305 
       
   306             // Check the sub patterns
       
   307             for (TInt k = 0; k < iMatchPatternDetails[i]->subPatternArray.Count(); k++)
       
   308                 {
       
   309                 HBufC* temporseq = iMatchPatternDetails[i]->subPatternArray[k]->matchPattern;
       
   310                 TRAP_IGNORE( UpdateForSubSequencesL( *temporseq, 
       
   311                     firstOccurenceTracker + 
       
   312                     iMatchPatternDetails[i]->subPatternArray[k]->firstOccurenceInThisPattern ) );
       
   313                 }
       
   314 
       
   315             if (iMatchPatternDetails[i]->firstOccurence == -1)
       
   316                 {
       
   317                 iMatchPatternDetails[i]->firstOccurence = firstOccurenceTracker;
       
   318                 }
       
   319 
       
   320             firstOccurenceTracker = firstOccurenceTracker + j;
       
   321 
       
   322             }
       
   323         }
       
   324     }
       
   325 
       
   326 // ----------------------------------------------------------------------------
       
   327 // CPcsAlgorithm2FilterHelper::GetPatternsL
       
   328 // 
       
   329 // ----------------------------------------------------------------------------
       
   330 void CPcsAlgorithm2FilterHelper::GetPatternsL(
       
   331         RPointerArray<CPsPattern>& aPatternSet)
       
   332     {
       
   333     // Sort sequences
       
   334     TLinearOrder<TPsPatternDetails> rule(ComparePsPattern);
       
   335     iMatchPatternDetails.Sort(rule);
       
   336 
       
   337     for (TInt i = 0; i < iMatchPatternDetails.Count(); i++)
       
   338         {
       
   339         CPsPattern *tempPattern = CPsPattern::NewL();
       
   340 
       
   341         // Copy the sequence
       
   342         tempPattern->SetPatternL(*(iMatchPatternDetails[i]->matchPattern));
       
   343         tempPattern->SetFirstIndex(iMatchPatternDetails[i]->firstOccurence);
       
   344 
       
   345         aPatternSet.Append(tempPattern);
       
   346         }
       
   347     }
       
   348 
       
   349 // ----------------------------------------------------------------------------
       
   350 // CPcsAlgorithm2FilterHelper::FindSequence
       
   351 // Searches for for aSeq in iMatchPatternDetails and returns index 
       
   352 //  where input sequence is found. Returns -1 if not found
       
   353 // ----------------------------------------------------------------------------
       
   354 TInt CPcsAlgorithm2FilterHelper::FindSequence(TDesC* aSeq)
       
   355     {
       
   356     TBuf<50> seqBuf;
       
   357     seqBuf.Append(*aSeq);
       
   358 
       
   359     TInt j = 0;
       
   360     for (; j < iMatchPatternDetails.Count(); j++)
       
   361         {
       
   362         TBuf<50> matchPatternBuf;
       
   363         matchPatternBuf.Copy((iMatchPatternDetails[j]->matchPattern->Des()));
       
   364         if (seqBuf == matchPatternBuf)
       
   365             break;
       
   366         }
       
   367 
       
   368     if (j == iMatchPatternDetails.Count())
       
   369         return KErrNotFound;
       
   370     else
       
   371         return j;
       
   372     }
       
   373 
       
   374 // ----------------------------------------------------------------------------
       
   375 // CPcsAlgorithm2FilterHelper::FindSubSequence
       
   376 // Searches for for seq in subpatterns of iMatchPatternDetails[aPatternIndex]
       
   377 // and returns index where input sequence is found. 
       
   378 // Returns -1 if not found
       
   379 // ----------------------------------------------------------------------------
       
   380 TInt CPcsAlgorithm2FilterHelper::FindSubSequence(TDesC* aSeq, TInt aPatternIndex)
       
   381     {
       
   382     TBuf<50> seqBuf;
       
   383     seqBuf.Append(*aSeq);
       
   384 
       
   385     TInt j = 0;
       
   386     for (; j < iMatchPatternDetails[aPatternIndex]->subPatternArray.Count(); j++)
       
   387         {
       
   388         TBuf<50> matchPatternBuf;
       
   389         matchPatternBuf.Copy((iMatchPatternDetails[aPatternIndex]->subPatternArray[j]->matchPattern->Des()));
       
   390         if (seqBuf == matchPatternBuf)
       
   391             break;
       
   392         }
       
   393 
       
   394     if (j == iMatchPatternDetails[aPatternIndex]->subPatternArray.Count())
       
   395         {
       
   396         return KErrNotFound;
       
   397         }
       
   398     else
       
   399         {
       
   400         return j;
       
   401         }
       
   402     }
       
   403 
       
   404 // ----------------------------------------------------------------------------
       
   405 // CPcsAlgorithm2FilterHelper::UpdateForSubSequencesL
       
   406 // Searches for all the subsequences of aSeq and updates the aFirstOccrVal in 
       
   407 // iMatchPatternDetails.
       
   408 //
       
   409 // For ex: if aSeq = "ABC" and aFirstOccrVal= 2, 
       
   410 // then, firstOccurence of all substring sequences ("A", "AB", "ABC") 
       
   411 //       in iMatchPatternDetails will be set to 2
       
   412 // ----------------------------------------------------------------------------
       
   413 void CPcsAlgorithm2FilterHelper::UpdateForSubSequencesL(TDesC& aSeq, TInt aFirstOccrVal)
       
   414     {
       
   415     HBufC* tempSequence = HBufC::NewL(aSeq.Length() + 1);
       
   416     for (TInt i = 0; i < aSeq.Length(); i++)
       
   417         {
       
   418         // Get the next substring in tempSequence
       
   419         tempSequence->Des().Append(aSeq[i]);
       
   420 
       
   421         // Find if this sequence exist in iMatchPatternDetails
       
   422         TInt patternIndex = FindSequence(tempSequence);
       
   423 
       
   424         //If the sequence exits and firstOccurence == -1, then set it to aFirstOccrVal 
       
   425         if ((patternIndex != KErrNotFound)&& (iMatchPatternDetails[patternIndex]->firstOccurence == -1))
       
   426             {
       
   427             iMatchPatternDetails[patternIndex]->firstOccurence = aFirstOccrVal;
       
   428             }
       
   429 
       
   430         }
       
   431     delete tempSequence;
       
   432     tempSequence = NULL;
       
   433 
       
   434     return;
       
   435 
       
   436     }
       
   437 // END OF FILE
       
   438