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