predictivesearch/PcsAlgorithm/Algorithm2/src/CPcsCache.cpp
branchRCL_3
changeset 85 38bb213f60ba
parent 68 9da50d567e3c
equal deleted inserted replaced
74:6b5524b4f673 85:38bb213f60ba
    20 *
    20 *
    21 */
    21 */
    22 
    22 
    23 // INCLUDE FILES
    23 // INCLUDE FILES
    24 #include <MVPbkContactLink.h>
    24 #include <MVPbkContactLink.h>
    25 #include <vpbkeng.rsg>
       
    26 
    25 
    27 #include "FindUtilChineseECE.h"
    26 #include "FindUtilChineseECE.h"
    28 #include "CPsData.h"
    27 #include "CPsData.h"
    29 #include "CPcsCache.h"
    28 #include "CPcsCache.h"
    30 #include "CPcsDebug.h"
    29 #include "CPcsDebug.h"
    36 
    35 
    37 // ----------------------------------------------------------------------------
    36 // ----------------------------------------------------------------------------
    38 // CPcsCache::NewL
    37 // CPcsCache::NewL
    39 // Two Phase Construction
    38 // Two Phase Construction
    40 // ----------------------------------------------------------------------------
    39 // ----------------------------------------------------------------------------
    41 CPcsCache* CPcsCache::NewL(CPcsAlgorithm2* aAlgorithm, const TDesC& aURI, 
    40 CPcsCache* CPcsCache::NewL(CPcsAlgorithm2* aAlgorithm, TDesC& aURI, 
    42                            CPcsKeyMap& aKeyMap, TUint8 aUriId)
    41                            CPcsKeyMap& aKeyMap, TUint8 aUriId)
    43     {
    42     {
    44     PRINT ( _L("Enter CPcsCache::NewL") );
    43     PRINT ( _L("Enter CPcsCache::NewL") );
    45 
    44 
    46     CPcsCache* instance = new (ELeave) CPcsCache();
    45     CPcsCache* instance = new (ELeave) CPcsCache();
    68 
    67 
    69 // ----------------------------------------------------------------------------
    68 // ----------------------------------------------------------------------------
    70 // CPcsCache::ConstructL
    69 // CPcsCache::ConstructL
    71 // 2nd Phase Constructor
    70 // 2nd Phase Constructor
    72 // ----------------------------------------------------------------------------
    71 // ----------------------------------------------------------------------------
    73 void CPcsCache::ConstructL(CPcsAlgorithm2* aAlgorithm, const TDesC& aURI, 
    72 void CPcsCache::ConstructL(CPcsAlgorithm2* aAlgorithm, TDesC& aURI, 
    74                            CPcsKeyMap& aKeyMap, TUint8 aUriId)
    73                            CPcsKeyMap& aKeyMap, TUint8 aUriId)
    75     {
    74     {
    76     PRINT ( _L("Enter CPcsCache::ConstructL") );
    75     PRINT ( _L("Enter CPcsCache::ConstructL") );
    77     iAlgorithm = aAlgorithm;
    76     iAlgorithm = aAlgorithm;
    78 
    77 
    79     iURI = aURI.AllocL();
    78     iURI = aURI.AllocL();
    80     iUriId = aUriId;
    79     iUriId = aUriId;
    81     //Update the caching status for this cache
    80     //Update the caching status for this cache
    82     iCacheStatus = ECachingNotStarted;
    81     iCacheStatus = ECachingNotStarted;
    83 
    82 
    84     iKeyMap = &aKeyMap;
    83     keyMap = &aKeyMap;
    85 
    84 
    86     // Populate iKeyArr
    85     // Populate keyArr
    87     const TInt keyMapPoolcount =  aKeyMap.PoolCount();
    86     for (TInt i = 0; i < aKeyMap.PoolCount(); i++)
    88     for (TInt i = 0; i < keyMapPoolcount; i++)
    87         {
    89         {
    88         RPointerArray<CPcsPoolElement> *keyMap = new (ELeave) RPointerArray<CPcsPoolElement> (1);
    90         RPointerArray<CPcsPoolElement>* keyMap = new (ELeave) RPointerArray<CPcsPoolElement> (1);
    89         keyArr.InsertL(keyMap, i);
    91         iKeyArr.InsertL(keyMap, i);
       
    92         }
    90         }
    93 
    91 
    94     PRINT ( _L("End CPcsCache::ConstructL") );
    92     PRINT ( _L("End CPcsCache::ConstructL") );
    95     }
    93     }
    96 
    94 
   100 // ----------------------------------------------------------------------------
    98 // ----------------------------------------------------------------------------
   101 CPcsCache::~CPcsCache()
    99 CPcsCache::~CPcsCache()
   102     {
   100     {
   103     PRINT ( _L("Enter CPcsCache::~CPcsCache") );
   101     PRINT ( _L("Enter CPcsCache::~CPcsCache") );
   104 
   102 
   105     delete iURI;
   103     if (iURI)
   106 
   104         {
   107     RemoveAllFromCache(); // cleans up iMasterPool and iCacheInfo
   105         delete iURI;
   108    
   106         }
   109     iKeyArr.ResetAndDestroy();
   107 
       
   108     // Loop thru cache info and free and the data elements
       
   109     THashMapIter<TInt, TInt> iter(cacheInfo);
       
   110 
       
   111     do
       
   112         {
       
   113         TInt* id = const_cast<TInt*> (iter.NextKey());
       
   114 
       
   115         if (id == NULL)
       
   116             break;
       
   117 
       
   118         TInt* poolMap = iter.CurrentValue();
       
   119 
       
   120         if (poolMap == NULL)
       
   121             {
       
   122             continue;
       
   123             }
       
   124 
       
   125         CPsData *data = NULL;
       
   126         for (int keyIndex = 0; keyIndex <= keyArr.Count(); keyIndex++)
       
   127             {
       
   128             TBool present = GetPoolMap(*poolMap, keyIndex);
       
   129 
       
   130             if (!present)
       
   131                 {
       
   132                 continue;
       
   133                 }
       
   134 
       
   135             RPointerArray<CPcsPoolElement> tmpKeyMap = *(keyArr[keyIndex]);
       
   136             for (int arrayIndex = 0; arrayIndex < tmpKeyMap.Count(); arrayIndex++)
       
   137                 {
       
   138                 CPcsPoolElement *element = tmpKeyMap[arrayIndex];
       
   139                 TInt localId = element->GetPsData()->Id();
       
   140                 if (*id == localId)
       
   141                     {
       
   142                     data = element->GetPsData();
       
   143                     delete element;
       
   144                     keyArr[keyIndex]->Remove(arrayIndex);
       
   145                     }
       
   146                 }
       
   147             };
       
   148 
       
   149         // Remove this element from master pool
       
   150         for (int arrayIndex = 0; arrayIndex < masterPool.Count(); arrayIndex++)
       
   151             {
       
   152             CPsData *dataElement = masterPool[arrayIndex];
       
   153             TInt localId = dataElement->Id();
       
   154             if (*id == localId)
       
   155                 {
       
   156                 masterPool.Remove(arrayIndex);
       
   157                 }
       
   158             }
       
   159 
       
   160         if (data)
       
   161             {
       
   162             delete data;
       
   163             }
       
   164 
       
   165         }
       
   166     while (1);
       
   167 
       
   168     for (TInt i = 0; i < keyArr.Count(); i++)
       
   169         {
       
   170         keyArr[i]->ResetAndDestroy();
       
   171         delete keyArr[i];
       
   172         keyArr[i] = NULL;
       
   173         }
       
   174 
       
   175     masterPool.ResetAndDestroy();
       
   176 
       
   177     cacheInfo.Close();
       
   178 
       
   179     keyArr.Reset();
   110     iDataFields.Reset();
   180     iDataFields.Reset();
   111     iSortOrder.Reset();
   181     iSortOrder.Reset();
   112     iIndexOrder.Reset();
   182     iIndexOrder.Reset();
   113     iMasterPoolBackup.Close();
       
   114 
   183 
   115     PRINT ( _L("End CPcsCache::~CPcsCache") );
   184     PRINT ( _L("End CPcsCache::~CPcsCache") );
   116     }
   185     }
   117 
   186 
   118 // ----------------------------------------------------------------------------
   187 // ----------------------------------------------------------------------------
   120 // Get list of pool elements specific to a pool
   189 // Get list of pool elements specific to a pool
   121 // ----------------------------------------------------------------------------     
   190 // ----------------------------------------------------------------------------     
   122 void CPcsCache::GetContactsForKeyL(TInt aKeyId, RPointerArray<CPcsPoolElement>& aData)
   191 void CPcsCache::GetContactsForKeyL(TInt aKeyId, RPointerArray<CPcsPoolElement>& aData)
   123     {
   192     {
   124     PRINT ( _L("Enter CPcsCache::GetContactsForKeyL") );
   193     PRINT ( _L("Enter CPcsCache::GetContactsForKeyL") );
   125 
   194     CleanupClosePushL( aData );
   126     if ( aKeyId >= 0 && aKeyId < iKeyArr.Count() )
   195     RPointerArray<CPcsPoolElement> arr = *keyArr[aKeyId];
   127         {
   196     for (int i = 0; i < arr.Count(); i++)
   128         const RPointerArray<CPcsPoolElement>& arr = *iKeyArr[aKeyId];
   197         {
   129         const TInt arrCount = arr.Count();
   198         CPcsPoolElement* value = arr[i];
   130         for (TInt i = 0; i < arrCount; i++)
   199         aData.AppendL(value);
   131             {
   200         }
   132             CPcsPoolElement* value = arr[i];
   201 
   133             aData.AppendL(value);
   202     CleanupStack::Pop();
   134             }
       
   135         }
       
   136 
       
   137     PRINT ( _L("End CPcsCache::GetContactsForKeyL") );
   203     PRINT ( _L("End CPcsCache::GetContactsForKeyL") );
   138     }
   204     }
   139 
   205 
   140 // ----------------------------------------------------------------------------
   206 // ----------------------------------------------------------------------------
   141 // CPcsCache::GetAllContentsL
   207 // CPcsCache::GetAllContentsL
   142 // Get all data elements in this cache
   208 // Get all data elements in this cache
   143 // ----------------------------------------------------------------------------     
   209 // ----------------------------------------------------------------------------     
   144 void CPcsCache::GetAllContentsL(RPointerArray<CPsData>& aData)
   210 void CPcsCache::GetAllContentsL(RPointerArray<CPsData>& aData)
   145     {
   211     {
   146     PRINT ( _L("Enter CPcsCache::GetAllContentsL") );
   212     PRINT ( _L("Enter CPcsCache::GetAllContentsL") );
   147     
   213     CleanupClosePushL( aData );
   148     const TInt masterPoolCount =  iMasterPool.Count(); 
   214     for (int i = 0; i < masterPool.Count(); i++)
   149     for (TInt i = 0; i < masterPoolCount; i++)
   215         {
   150         {
   216         CPsData* value = masterPool[i];
   151         CPsData* value = iMasterPool[i];
       
   152         aData.AppendL(value);
   217         aData.AppendL(value);
   153         }
   218         }
   154 
   219     CleanupStack::Pop();
   155     PRINT ( _L("End CPcsCache::GetAllContentsL") );
   220     PRINT ( _L("End CPcsCache::GetAllContentsL") );
   156     }
   221     }
   157 
   222 
   158 // ----------------------------------------------------------------------------
   223 // ----------------------------------------------------------------------------
   159 // CPcsCache::AddToPool
   224 // CPcsCache::AddToPool
   160 // Adds a contact to cache
   225 // Adds a contact to cache
   161 // ----------------------------------------------------------------------------
   226 // ----------------------------------------------------------------------------
   162 void CPcsCache::AddToPoolL(TUint64& aPoolMap, CPsData& aData)
   227 void CPcsCache::AddToPoolL(TInt& aPoolMap, CPsData& aData)
   163     {
   228     {
   164     // Temp hash to remember the location of pool elements
   229     // Temp hash to remember the location of pool elements
   165     // First TInt  = Pool 
   230     // First TInt  = Pool 
   166     // Second TInt = Location in the pool
   231     // Second TInt = Location in the pool
   167     // Required for memory optimization so that more than one pool
   232     // Required for memory optimization so that more than one pool
   168     // element doesn't get added for the same data
   233     // element doesn't get added for the same data
   169     RHashMap<TInt, TInt> elementHash;
   234     RHashMap<TInt, TInt> elementHash;
   170     CleanupClosePushL( elementHash );
   235     TLinearOrder<CPcsPoolElement> rule(CPcsPoolElement::CompareByData);
   171     TLinearOrder<CPcsPoolElement> rule(CPcsPoolElement::CompareByDataL);
   236 
   172 
   237     // Parse thru each data element    
   173     // Parse thru each data element
   238     for (int dataIndex = 0; dataIndex < aData.DataElementCount(); dataIndex++)
   174     const TInt dataElementCount = aData.DataElementCount();
       
   175     for (TInt dataIndex = 0; dataIndex < dataElementCount; dataIndex++)
       
   176         {
   239         {
   177         // Stores first key for each word
   240         // Stores first key for each word
   178         RArray<TChar> firstCharArr;
   241         RArray<TChar> firstKey;
   179         CleanupClosePushL( firstCharArr );
       
   180 
   242 
   181         // Recover the first character
   243         // Recover the first character
   182         if ( aData.Data(dataIndex) )
   244         if (aData.Data(dataIndex) && aData.Data(dataIndex)->Length() != 0)
   183             {
   245             {
   184             GetFirstCharsForDataL( *aData.Data(dataIndex), firstCharArr );
   246             // Split the data into words	
   185             }
   247             CWords* words = CWords::NewLC(*aData.Data(dataIndex));
   186         
   248 
   187         // Get the corresponding Pool IDs
   249             // Store the first numeric key for each word
   188         RArray<TInt> poolIdArr;
   250             for (int i = 0; i < words->MdcaCount(); i++)
   189         CleanupClosePushL( poolIdArr );
   251                 {
   190         GetPoolIdsForCharsL( firstCharArr, poolIdArr );
   252                 TChar firstChar;
   191 
   253                 for (int j = 0; j < words->MdcaPoint(i).Length(); j++)
   192         const TInt poolIdArrCount = poolIdArr.Count();
   254                     {
   193         for (TInt poolArrIndex = 0; poolArrIndex < poolIdArrCount ; poolArrIndex++)
   255                     firstChar = (words->MdcaPoint(i))[j];
   194             {
   256                     TBuf<20> word;
   195             
   257                     word.Append(firstChar);
   196             TInt poolId = poolIdArr[poolArrIndex];
   258                     if (iAlgorithm->FindUtilECE()->IsChineseWord(word))
       
   259                         {
       
   260                         RPointerArray<HBufC> spellList;
       
   261                         if (iAlgorithm->FindUtilECE()->T9ChineseTranslationL(firstChar, spellList))
       
   262                             {
       
   263                             for (int j = 0; j < spellList.Count(); j++)
       
   264                                 {
       
   265                                 firstKey.Append(keyMap->KeyForCharacter(*(spellList[j]->Ptr())));
       
   266                                 }
       
   267                             }
       
   268                         else
       
   269                             {
       
   270                             firstKey.Append( keyMap->KeyForCharacter(firstChar));
       
   271                             }
       
   272                         spellList.ResetAndDestroy();
       
   273                         }
       
   274                     else
       
   275                         {
       
   276                         firstKey.Append(keyMap->KeyForCharacter(firstChar));
       
   277                         }
       
   278                     }
       
   279                 }
       
   280 
       
   281             CleanupStack::PopAndDestroy(words);
       
   282             }
       
   283 
       
   284         for (TInt wordIndex = 0; wordIndex < firstKey.Count(); wordIndex++)
       
   285             {
       
   286             TInt arrayIndex = keyMap->PoolIdForCharacter(firstKey[wordIndex]);
   197 
   287 
   198             CPcsPoolElement* element = NULL;
   288             CPcsPoolElement* element = NULL;
   199 
   289 
   200             // Check if an element already exists in the pool for this data
   290             // Check if an element already exists in the pool for this data
   201             TInt* loc = NULL;
   291             TInt* loc = NULL;
   202             loc = elementHash.Find(poolId);
   292             loc = elementHash.Find(arrayIndex);
   203             if (loc != NULL)
   293             if (loc != NULL)
   204                 {
   294                 {
   205                 // Exists. Then recover ...
   295                 // Exists. Then recover ...
   206                 RPointerArray<CPcsPoolElement> tmpKeyMap = *(iKeyArr[poolId]);
   296                 RPointerArray<CPcsPoolElement> tmpKeyMap = *(keyArr[arrayIndex]);
   207                 element = tmpKeyMap[*loc];
   297                 element = tmpKeyMap[*loc];
   208                 }
   298                 }
   209 
   299 
   210             if (element == NULL) // Pool element doesn't exist. Create new ...
   300             if (element == NULL) // Pool element doesn't exist. Create new ...
   211                 {
   301                 {
   212                 element = CPcsPoolElement::NewL(aData);
   302                 element = CPcsPoolElement::NewL(aData);
   213                 element->ClearDataMatchAttribute();
   303                 element->ClearDataMatchAttribute();
   214                 element->SetDataMatch(dataIndex);
   304                 element->SetDataMatch(dataIndex);
   215 
   305 
   216                 // Insert to pool
   306                 // Insert to pool
   217                 iKeyArr[poolId]->InsertInOrderAllowRepeatsL(element, rule);
   307                 keyArr[arrayIndex]->InsertInOrderAllowRepeatsL(element, rule);
   218                 TInt index = iKeyArr[poolId]->FindInOrderL(element, rule);
   308                 TInt index = keyArr[arrayIndex]->FindInOrderL(element, rule);
   219 
   309 
   220                 // Set the bit for this pool
   310                 // Set the bit for this pool					
   221                 SetPoolMap(aPoolMap, poolId);
   311                 SetPoolMap(aPoolMap, arrayIndex);
   222 
   312 
   223                 // Store the array index in the temp hash
   313                 // Store the array index in the temp hash
   224                 elementHash.InsertL(poolId, index);
   314                 elementHash.InsertL(arrayIndex, index);
   225                 }
   315                 }
   226             else // Pool element exists. Just alter the data match attribute
   316             else // Pool element exists. Just alter the data match attribute
   227                 {
   317                 {
   228                 element->SetDataMatch(dataIndex);
   318                 element->SetDataMatch(dataIndex);
   229 
   319 
   230                 // Set the bit for this pool
   320                 // Set the bit for this pool					
   231                 SetPoolMap(aPoolMap, poolId);
   321                 SetPoolMap(aPoolMap, arrayIndex);
   232                 }
   322                 }
   233 
   323 
   234             } // for 2 loop
   324             } // for 2 loop
   235 
   325 
   236         CleanupStack::PopAndDestroy( &poolIdArr );    // Close
   326         firstKey.Reset();
   237         CleanupStack::PopAndDestroy( &firstCharArr ); // Close
       
   238 
   327 
   239         } // for 1 loop
   328         } // for 1 loop
   240 
   329 
   241     CleanupStack::PopAndDestroy( &elementHash );  // Close
   330     elementHash.Close();
   242     }
   331     }
   243 
   332 
   244 // ---------------------------------------------------------------------
   333 // ---------------------------------------------------------------------
   245 // CPcsCache::AddToCacheL
   334 // CPcsCache::AddToCacheL
   246 // 
   335 // 
   247 // ---------------------------------------------------------------------
   336 // ---------------------------------------------------------------------
   248 void CPcsCache::AddToCacheL(CPsData& aData)
   337 void CPcsCache::AddToCacheL(CPsData& aData)
   249     {
   338     {
   250     // Protect against duplicate items getting added
   339     // Protect against duplicate items getting added
   251     if (iCacheInfo.Find(aData.Id()) != NULL)
   340     if (cacheInfo.Find(aData.Id()) != NULL)
   252         {
   341         {
   253         return;
   342         return;
   254         }
   343         }
   255 
   344 
   256     // Include this element in the pool     
   345     // Include this element in the pool     
   257     TUint64 poolMap = 0;
   346     TInt poolMap = 0;
   258     AddToPoolL(poolMap, aData);
   347     AddToPoolL(poolMap, aData);
   259     iCacheInfo.InsertL(aData.Id(), poolMap);
   348     cacheInfo.InsertL(aData.Id(), poolMap);
   260 
   349 
   261     // Include this element in master pool        
   350     // Include this element in master pool        
   262     TLinearOrder<CPsData> rule(CPcsAlgorithm2Utils::CompareDataBySortOrderL);
   351     TLinearOrder<CPsData> rule(CPcsAlgorithm2Utils::CompareDataBySortOrder);
   263     iMasterPool.InsertInOrderAllowRepeatsL(&aData, rule);
   352     masterPool.InsertInOrderAllowRepeatsL(&aData, rule);
   264     }
   353     }
   265 
   354 
   266 // ---------------------------------------------------------------------
   355 // ---------------------------------------------------------------------
   267 // CPcsCache::RemoveContactL
   356 // CPcsCache::RemoveContactL
   268 // 
   357 // 
   269 // ---------------------------------------------------------------------
   358 // ---------------------------------------------------------------------
   270 void CPcsCache::RemoveFromCacheL(TInt aItemId)
   359 void CPcsCache::RemoveFromCacheL(TInt aItemId)
   271     {
   360     {
   272     CPsData *data = NULL;
   361     CPsData *data = NULL;
   273 
   362 
   274     TUint64* poolMap = iCacheInfo.Find(aItemId);
   363     TInt* poolMap = cacheInfo.Find(aItemId);
   275 
   364 
   276     if (poolMap == NULL)
   365     if (poolMap == NULL)
   277         {
   366         {
   278         return;
   367         return;
   279         }
   368         }
   280 
   369 
   281     // Remove this element from pools
   370     // Remove this element from pools
   282     const TInt keyArrCount = iKeyArr.Count();
   371     for (int keyIndex = 0; keyIndex <= keyArr.Count(); keyIndex++)
   283     for (TInt keyIndex = 0; keyIndex < keyArrCount; keyIndex++)
       
   284         {
   372         {
   285         TBool present = GetPoolMap(*poolMap, keyIndex);
   373         TBool present = GetPoolMap(*poolMap, keyIndex);
   286 
   374 
   287         if (!present)
   375         if (!present)
   288             {
   376             {
   289             continue;
   377             continue;
   290             }
   378             }
   291 
   379 
   292         const RPointerArray<CPcsPoolElement>& tmpKeyMap = *(iKeyArr[keyIndex]);
   380         RPointerArray<CPcsPoolElement> tmpKeyMap = *(keyArr[keyIndex]);
   293         
   381         for (int arrayIndex = 0; arrayIndex < tmpKeyMap.Count(); arrayIndex++)
   294         for (TInt arrayIndex = 0; arrayIndex < tmpKeyMap.Count(); arrayIndex++)
       
   295             {
   382             {
   296             CPcsPoolElement *element = tmpKeyMap[arrayIndex];
   383             CPcsPoolElement *element = tmpKeyMap[arrayIndex];
   297             TInt id = element->GetPsData()->Id();
   384             TInt id = element->GetPsData()->Id();
   298             if (id == aItemId)
   385             if (id == aItemId)
   299                 {
   386                 {
   300                 data = element->GetPsData();
   387                 data = element->GetPsData();
   301                 delete element;
   388                 delete element;
   302                 iKeyArr[keyIndex]->Remove(arrayIndex);
   389                 keyArr[keyIndex]->Remove(arrayIndex);
   303                 }
   390                 }
   304             }
   391             }
   305         }
   392         }
   306 
   393 
   307     // Remove this element from master pool
   394     // Remove this element from master pool
   308     for (TInt arrayIndex = 0; arrayIndex < iMasterPool.Count(); arrayIndex++)
   395     for (int arrayIndex = 0; arrayIndex < masterPool.Count(); arrayIndex++)
   309         {
   396         {
   310         CPsData *dataElement = iMasterPool[arrayIndex];
   397         CPsData *dataElement = masterPool[arrayIndex];
   311         TInt id = dataElement->Id();
   398         TInt id = dataElement->Id();
   312         if (id == aItemId)
   399         if (id == aItemId)
   313             {
   400             {
   314             iMasterPool.Remove(arrayIndex);
   401             masterPool.Remove(arrayIndex);
   315             }
   402             }
   316         }
   403         }
   317 
   404 
   318     // Delete data 
   405     // Delete data 
   319     if (data)
   406     if (data)
   321         delete data;
   408         delete data;
   322         data = NULL;
   409         data = NULL;
   323         }
   410         }
   324 
   411 
   325     // Clear up cache information
   412     // Clear up cache information
   326     iCacheInfo.Remove(aItemId);
   413     cacheInfo.Remove(aItemId);
   327     }
   414     }
   328 
   415 
   329 // ---------------------------------------------------------------------
   416 // ---------------------------------------------------------------------
   330 // CPcsCache::RemoveAllFromCacheL
   417 // CPcsCache::RemoveAllFromCacheL
   331 // 
   418 // 
   332 // ---------------------------------------------------------------------
   419 // ---------------------------------------------------------------------
   333 void CPcsCache::RemoveAllFromCache()
   420 void CPcsCache::RemoveAllFromCacheL()
   334     {
   421     {
   335     PRINT ( _L("Enter CPcsCache::RemoveAllFromCache") );
   422     PRINT ( _L("Enter CPcsCache::RemoveAllFromCacheL") );
   336 
   423 
   337     const TInt keyArrCount = iKeyArr.Count();
   424     for (TInt i = 0; i < keyArr.Count(); i++)
   338     for ( TInt i = 0 ; i < keyArrCount ; i++ )
   425         {
   339         {
   426         keyArr[i]->ResetAndDestroy();
   340         iKeyArr[i]->ResetAndDestroy();
   427 
   341         }
   428         }
   342 
   429 
   343     iMasterPool.ResetAndDestroy();
   430     masterPool.ResetAndDestroy();
   344     iCacheInfo.Close();
   431     cacheInfo.Close();
   345 
   432 
   346     PRINT ( _L("End CPcsCache::RemoveAllFromCache") );
   433     PRINT ( _L("End CPcsCache::RemoveAllFromCacheL") );
   347     }
   434     }
   348 
   435 
   349 // ---------------------------------------------------------------------
   436 // ---------------------------------------------------------------------
   350 // CPcsCache::SetPoolMap
   437 // CPcsCache::SetPoolMap
   351 // 
   438 // 
   352 // ---------------------------------------------------------------------
   439 // ---------------------------------------------------------------------
   353 void CPcsCache::SetPoolMap(TUint64& aPoolMap, TInt aArrayIndex)
   440 void CPcsCache::SetPoolMap(TInt& aPoolMap, TInt arrayIndex)
   354     {
   441     {
   355     __ASSERT_DEBUG( aArrayIndex < 64, User::Panic(_L("CPcsCache"), KErrOverflow ) );
   442     TReal val;
   356 
   443     Math::Pow(val, 2, arrayIndex);
   357     /* Some platforms do not support 64 bits shift operations.
   444 
   358      * Split to two 32 bits operations.
   445     aPoolMap |= (TInt) val;
   359      */
       
   360     
       
   361     TUint32 poolMapH = I64HIGH(aPoolMap);
       
   362     TUint32 poolMapL = I64LOW(aPoolMap);
       
   363     
       
   364     TUint32 valH = 0;
       
   365     TUint32 valL = 0;
       
   366     if (aArrayIndex < 32)
       
   367         {
       
   368         valL = 1 << aArrayIndex;
       
   369         }
       
   370     else
       
   371         {
       
   372         valH = 1 << (aArrayIndex-32);
       
   373         }
       
   374 
       
   375     poolMapH |= valH;
       
   376     poolMapL |= valL;
       
   377     
       
   378     aPoolMap = MAKE_TUINT64(poolMapH, poolMapL);
       
   379     }
   446     }
   380 
   447 
   381 // ---------------------------------------------------------------------
   448 // ---------------------------------------------------------------------
   382 // CPcsCache::GetPoolMap
   449 // CPcsCache::GetPoolMap
   383 // 
   450 // 
   384 // ---------------------------------------------------------------------
   451 // ---------------------------------------------------------------------
   385 TBool CPcsCache::GetPoolMap(TUint64& aPoolMap, TInt aArrayIndex)
   452 TBool CPcsCache::GetPoolMap(TInt& aPoolMap, TInt arrayIndex)
   386     {
   453     {
   387     __ASSERT_DEBUG( aArrayIndex < 64, User::Panic(_L("CPcsCache"), KErrOverflow ) );
   454     TReal val;
   388 
   455     Math::Pow(val, 2, arrayIndex);
   389     /* Some platforms do not support 64 bits shift operations.
   456 
   390      * Split to two 32 bits operations.
   457     return (aPoolMap & (TInt) val);
   391      */
       
   392 
       
   393     TUint32 poolMapH = I64HIGH(aPoolMap);
       
   394     TUint32 poolMapL = I64LOW(aPoolMap);
       
   395     
       
   396     TUint32 valH = 0;
       
   397     TUint32 valL = 0;
       
   398     if (aArrayIndex < 32)
       
   399         {
       
   400         valL = 1 << aArrayIndex;
       
   401         }
       
   402     else
       
   403         {
       
   404         valH = 1 << (aArrayIndex-32);
       
   405         }
       
   406 
       
   407     TBool ret = (poolMapH & valH) || (poolMapL & valL);
       
   408 
       
   409     return (ret);
       
   410     }
       
   411 
       
   412 // ---------------------------------------------------------------------
       
   413 // CPcsCache::GetFirstCharsForDataL
       
   414 // 
       
   415 // ---------------------------------------------------------------------
       
   416 void CPcsCache::GetFirstCharsForDataL( const TDesC& aData, RArray<TChar>& aFirstChars ) const
       
   417     {
       
   418     // Split the data into words
       
   419     CWords* words = CWords::NewLC(aData);
       
   420 
       
   421     // Find indexing characters for each word
       
   422     for (TInt i = 0; i < words->MdcaCount(); i++)
       
   423         {
       
   424         TPtrC16 word = words->MdcaPoint(i);
       
   425         TBool lastCharIsChinese = EFalse;
       
   426         
       
   427         // If the word contains any Chinese characters, then it is
       
   428         // stored to cache according all the available spellings of
       
   429         // all the Chinese characters
       
   430         if ( iAlgorithm->FindUtilECE()->IsChineseWordIncluded(word) )
       
   431             {
       
   432             const TInt wordLength = word.Length();
       
   433             for (TInt j = 0; j < wordLength; j++)
       
   434                 {
       
   435                 TText curChar = word[j];
       
   436                 RPointerArray<HBufC> spellList;
       
   437                 CleanupResetAndDestroyPushL( spellList );
       
   438                 if ( iAlgorithm->FindUtilECE()->DoTranslationL(curChar, spellList) )
       
   439                     {
       
   440                     lastCharIsChinese = ETrue;
       
   441                     // Append first char of each spelling
       
   442                     const TInt spellListCount = spellList.Count();
       
   443                     for (TInt k = 0; k < spellListCount; k++)
       
   444                         {
       
   445                         const HBufC* spelling = spellList[k];
       
   446                         aFirstChars.AppendL( (*spelling)[0] );
       
   447                         }
       
   448                     }
       
   449                 else
       
   450                     {
       
   451                     if ( lastCharIsChinese )
       
   452                         {
       
   453                         aFirstChars.AppendL( word[j] );
       
   454                         lastCharIsChinese = EFalse;
       
   455                         }
       
   456                     }
       
   457                 CleanupStack::PopAndDestroy( &spellList ); // ResetAndDestroy
       
   458                 }
       
   459             }
       
   460         
       
   461         // If the first charcter of the word is non-Chinese, then it's stored to the
       
   462         // cache according this first character. Other characters of the word may or
       
   463         // may not be Chinese.
       
   464         if ( !iAlgorithm->FindUtilECE()->IsChineseWordIncluded( word.Left(1) ) )
       
   465             {
       
   466             aFirstChars.AppendL( word[0] );
       
   467             }
       
   468         }
       
   469     CleanupStack::PopAndDestroy(words);
       
   470     }
       
   471 
       
   472 // ---------------------------------------------------------------------
       
   473 // CPcsCache::GetPoolIdsForCharsL
       
   474 // 
       
   475 // ---------------------------------------------------------------------
       
   476 void CPcsCache::GetPoolIdsForCharsL( const RArray<TChar>& aChars, RArray<TInt>& aPoolIds ) const
       
   477     {
       
   478     // There can potentially be two pool IDs for each character. 
       
   479     // Reserve memory for this upfront to prevent unnecessary reallocations.
       
   480     aPoolIds.ReserveL( aChars.Count() * 2 );
       
   481     
       
   482     const TInt charsCount = aChars.Count() ;
       
   483     for ( TInt i = 0 ; i < charsCount ; ++i )
       
   484         {
       
   485         TChar character = aChars[i];
       
   486         TInt itutPoolId = iKeyMap->PoolIdForCharacter( character, EPredictiveItuT );
       
   487         if ( itutPoolId != KErrNotFound )
       
   488             {
       
   489             aPoolIds.AppendL( itutPoolId );
       
   490             }
       
   491         
       
   492         TInt qwertyPoolId = iKeyMap->PoolIdForCharacter( character, EPredictiveQwerty );
       
   493         if ( qwertyPoolId != KErrNotFound )
       
   494             {
       
   495             aPoolIds.AppendL( qwertyPoolId );
       
   496             }
       
   497         }
       
   498     }
   458     }
   499 
   459 
   500 // ---------------------------------------------------------------------
   460 // ---------------------------------------------------------------------
   501 // CPcsCache::GetURI
   461 // CPcsCache::GetURI
   502 // 
   462 // 
   510 // CPcsCache::SetDataFields
   470 // CPcsCache::SetDataFields
   511 // 
   471 // 
   512 // ---------------------------------------------------------------------
   472 // ---------------------------------------------------------------------
   513 void CPcsCache::SetDataFields(RArray<TInt>& aDataFields)
   473 void CPcsCache::SetDataFields(RArray<TInt>& aDataFields)
   514     {
   474     {
   515     const TInt dataFieldsCount = aDataFields.Count();
   475     for (TInt i(0); i < aDataFields.Count(); i++)
   516     for (TInt i(0); i < dataFieldsCount; i++)
       
   517         {
   476         {
   518         iDataFields.Append(aDataFields[i]);
   477         iDataFields.Append(aDataFields[i]);
   519         }
   478         }
   520     }
   479     }
   521 
   480 
   523 // CPcsCache::GetDataFields
   482 // CPcsCache::GetDataFields
   524 // 
   483 // 
   525 // ---------------------------------------------------------------------
   484 // ---------------------------------------------------------------------
   526 void CPcsCache::GetDataFields(RArray<TInt>& aDataFields)
   485 void CPcsCache::GetDataFields(RArray<TInt>& aDataFields)
   527     {
   486     {
   528     const TInt dataFieldsCount = iDataFields.Count();
   487     for (TInt i(0); i < iDataFields.Count(); i++)
   529     for (TInt i(0); i < dataFieldsCount; i++)
       
   530         {
   488         {
   531         aDataFields.Append(iDataFields[i]);
   489         aDataFields.Append(iDataFields[i]);
   532         }
   490         }
   533     }
   491     }
   534 
   492 
   575 void CPcsCache::SetSortOrder(RArray<TInt>& aSortOrder)
   533 void CPcsCache::SetSortOrder(RArray<TInt>& aSortOrder)
   576     {
   534     {
   577     PRINT ( _L("Enter CPcsCache::SetSortOrder") );
   535     PRINT ( _L("Enter CPcsCache::SetSortOrder") );
   578 
   536 
   579     iSortOrder.Reset();
   537     iSortOrder.Reset();
   580     const TInt sortOrderCount = aSortOrder.Count();
   538 
   581     for (TInt i(0); i < sortOrderCount; i++)
   539     for (TInt i(0); i < aSortOrder.Count(); i++)
   582         {
   540         {
   583         iSortOrder.Append(aSortOrder[i]);
   541         iSortOrder.Append(aSortOrder[i]);
   584         }
   542         }
   585 
   543 
   586     ComputeIndexOrder();
   544     ComputeIndexOrder();
   593 // 
   551 // 
   594 // ---------------------------------------------------------------------
   552 // ---------------------------------------------------------------------
   595 void CPcsCache::GetSortOrder(RArray<TInt>& aSortOrder)
   553 void CPcsCache::GetSortOrder(RArray<TInt>& aSortOrder)
   596     {
   554     {
   597     aSortOrder.Reset();
   555     aSortOrder.Reset();
   598     const TInt sortOrderCount =  iSortOrder.Count();
   556 
   599     for (TInt i(0); i < sortOrderCount; i++)
   557     for (TInt i(0); i < iSortOrder.Count(); i++)
   600         {
   558         {
   601         aSortOrder.Append(iSortOrder[i]);
   559         aSortOrder.Append(iSortOrder[i]);
   602         }
   560         }
   603     }
   561     }
   604 
   562 
   607 // 
   565 // 
   608 // ---------------------------------------------------------------------
   566 // ---------------------------------------------------------------------
   609 void CPcsCache::GetIndexOrder(RArray<TInt>& aIndexOrder)
   567 void CPcsCache::GetIndexOrder(RArray<TInt>& aIndexOrder)
   610     {
   568     {
   611     aIndexOrder.Reset();
   569     aIndexOrder.Reset();
   612     const TInt indexOrderCount = iIndexOrder.Count();
   570 
   613     for (TInt i(0); i < indexOrderCount; i++)
   571     for (TInt i(0); i < iIndexOrder.Count(); i++)
   614         {
   572         {
   615         aIndexOrder.Append(iIndexOrder[i]);
   573         aIndexOrder.Append(iIndexOrder[i]);
   616         }
   574         }
   617     }
   575     }
   618 
   576 
   622 // ---------------------------------------------------------------------
   580 // ---------------------------------------------------------------------
   623 void CPcsCache::ComputeIndexOrder()
   581 void CPcsCache::ComputeIndexOrder()
   624     {
   582     {
   625     iIndexOrder.Reset();
   583     iIndexOrder.Reset();
   626 
   584 
   627     const TInt sortOrderCount = iSortOrder.Count();
   585     for (int i = 0; i < iSortOrder.Count(); i++)
   628     const TInt dataFieldsCount = iDataFields.Count(); 
   586         {
   629     for (TInt i = 0; i < sortOrderCount; i++)
   587         for (int j = 0; j < iDataFields.Count(); j++)
   630         {
       
   631         for (TInt j = 0; j <dataFieldsCount; j++)
       
   632             {
   588             {
   633             if (iSortOrder[i] == iDataFields[j])
   589             if (iSortOrder[i] == iDataFields[j])
   634                 {
   590                 {
   635                 iIndexOrder.Append(j);
   591                 iIndexOrder.Append(j);
   636                 break;
   592                 break;
   643 // CPcsCache::ResortdataInPools
   599 // CPcsCache::ResortdataInPools
   644 // 
   600 // 
   645 // ---------------------------------------------------------------------
   601 // ---------------------------------------------------------------------
   646 void CPcsCache::ResortdataInPoolsL()
   602 void CPcsCache::ResortdataInPoolsL()
   647     {
   603     {
   648     // copy iMasterPool data into iMasterPoolBackup
   604     // copy masterPool data into masterPoolBackup
   649     const TInt masterPoolCount = iMasterPool.Count();
   605     for (TInt i = 0; i < masterPool.Count(); i++ )
   650     for (TInt i = 0; i < masterPoolCount; i++ )
   606         {
   651         {
   607         masterPoolBackup.Append( masterPool[i] );
   652         iMasterPoolBackup.Append( iMasterPool[i] );
       
   653         }
   608         }
   654     //Now reset the key array
   609     //Now reset the key array
   655     const TInt keyArrCount = iKeyArr.Count(); 
   610     for (TInt i = 0; i < keyArr.Count(); i++ )
   656     for (TInt i = 0; i <keyArrCount; i++ )
   611         {
   657         {
   612         keyArr[i]->ResetAndDestroy();
   658         iKeyArr[i]->ResetAndDestroy();
   613         }
   659         }
   614     masterPool.Reset();
   660     iMasterPool.Reset();
   615     cacheInfo.Close();
   661     iCacheInfo.Close();
   616     //now add data again from the masterPoolBackup
   662     //now add data again from the iMasterPoolBackup
   617     for (TInt i = 0; i < masterPoolBackup.Count(); i++ )
   663     const TInt masterPoolBackupCount = iMasterPoolBackup.Count();
   618         {
   664     for (TInt i = 0; i < masterPoolBackupCount; i++ )
   619         CPsData* temp = static_cast<CPsData*>(masterPoolBackup[i]);
   665         {
       
   666         CPsData* temp = iMasterPoolBackup[i];
       
   667         AddToCacheL( *temp );
   620         AddToCacheL( *temp );
   668         }
   621         }
   669     iMasterPoolBackup.Reset();
   622     masterPoolBackup.Reset();
   670     }
   623     } 
   671 
       
   672 // ---------------------------------------------------------------------
       
   673 // CPcsCache::GetFirstNameIndex
       
   674 // 
       
   675 // ---------------------------------------------------------------------
       
   676 TInt CPcsCache::GetFirstNameIndex() const
       
   677     {
       
   678     TInt fnIndex = KErrNotFound;
       
   679     const TInt dataFieldsCount = iDataFields.Count();
       
   680     for ( TInt i = 0 ; i < dataFieldsCount && fnIndex == KErrNotFound ; ++i )
       
   681         {
       
   682         if ( iDataFields[i] == R_VPBK_FIELD_TYPE_FIRSTNAME )
       
   683             {
       
   684             fnIndex = i;
       
   685             }
       
   686         }
       
   687     return fnIndex;
       
   688     }
       
   689 
       
   690 // ---------------------------------------------------------------------
       
   691 // CPcsCache::GetLastNameIndex
       
   692 // 
       
   693 // ---------------------------------------------------------------------
       
   694 TInt CPcsCache::GetLastNameIndex() const
       
   695     {
       
   696     TInt lnIndex = KErrNotFound;
       
   697     const TInt dataFieldsCount = iDataFields.Count();
       
   698     for ( TInt i = 0 ; i < dataFieldsCount && lnIndex == KErrNotFound ; ++i )
       
   699         {
       
   700         if ( iDataFields[i] == R_VPBK_FIELD_TYPE_LASTNAME )
       
   701             {
       
   702             lnIndex = i;
       
   703             }
       
   704         }
       
   705     return lnIndex;
       
   706     }
       
   707 
       
   708 // End of file
   624 // End of file