LCOV - code coverage report
Current view: top level - sc/source/core/data - dpcache.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 415 557 74.5 %
Date: 2014-04-11 Functions: 65 67 97.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include "dpcache.hxx"
      21             : 
      22             : #include "document.hxx"
      23             : #include "queryentry.hxx"
      24             : #include "queryparam.hxx"
      25             : #include "dpglobal.hxx"
      26             : #include "dpobject.hxx"
      27             : #include "globstr.hrc"
      28             : #include "docoptio.hxx"
      29             : #include "dpitemdata.hxx"
      30             : #include "dputil.hxx"
      31             : #include "dpnumgroupinfo.hxx"
      32             : 
      33             : #include <rtl/math.hxx>
      34             : #include <unotools/textsearch.hxx>
      35             : #include <unotools/localedatawrapper.hxx>
      36             : #include <svl/zforlist.hxx>
      37             : 
      38             : #if DEBUG_PIVOT_TABLE
      39             : #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
      40             : #endif
      41             : 
      42             : using namespace ::com::sun::star;
      43             : 
      44             : using ::com::sun::star::uno::Exception;
      45             : using ::com::sun::star::uno::Reference;
      46             : using ::com::sun::star::uno::UNO_QUERY;
      47             : using ::com::sun::star::uno::UNO_QUERY_THROW;
      48             : 
      49          12 : ScDPCache::GroupItems::GroupItems() : mnGroupType(0) {}
      50             : 
      51           6 : ScDPCache::GroupItems::GroupItems(const ScDPNumGroupInfo& rInfo, sal_Int32 nGroupType) :
      52           6 :     maInfo(rInfo), mnGroupType(nGroupType) {}
      53             : 
      54         181 : ScDPCache::Field::Field() : mnNumFormat(0) {}
      55             : 
      56          62 : ScDPCache::ScDPCache(ScDocument* pDoc) :
      57             :     mpDoc( pDoc ),
      58             :     mnColumnCount ( 0 ),
      59             :     maEmptyRows(0, MAXROW, true),
      60             :     mnDataSize(-1),
      61             :     mnRowCount(0),
      62          62 :     mbDisposing(false)
      63             : {
      64          62 : }
      65             : 
      66             : namespace {
      67             : 
      68             : struct ClearObjectSource : std::unary_function<ScDPObject*, void>
      69             : {
      70          15 :     void operator() (ScDPObject* p) const
      71             :     {
      72          15 :         p->ClearTableData();
      73          15 :     }
      74             : };
      75             : 
      76             : }
      77             : 
      78         124 : ScDPCache::~ScDPCache()
      79             : {
      80             :     // Make sure no live ScDPObject instances hold reference to this cache any
      81             :     // more.
      82          62 :     mbDisposing = true;
      83          62 :     std::for_each(maRefObjects.begin(), maRefObjects.end(), ClearObjectSource());
      84          62 : }
      85             : 
      86             : namespace {
      87             : 
      88             : /**
      89             :  * While the macro interpret level is incremented, the formula cells are
      90             :  * (semi-)guaranteed to be interpreted.
      91             :  */
      92             : class MacroInterpretIncrementer
      93             : {
      94             : public:
      95          65 :     MacroInterpretIncrementer(ScDocument* pDoc) :
      96          65 :         mpDoc(pDoc)
      97             :     {
      98          65 :         mpDoc->IncMacroInterpretLevel();
      99          65 :     }
     100          65 :     ~MacroInterpretIncrementer()
     101             :     {
     102          65 :         mpDoc->DecMacroInterpretLevel();
     103          65 :     }
     104             : private:
     105             :     ScDocument* mpDoc;
     106             : };
     107             : 
     108         181 : OUString createLabelString(ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab)
     109             : {
     110         181 :     OUString aDocStr = pDoc->GetString(nCol, nRow, nTab);
     111         181 :     if (aDocStr.isEmpty())
     112             :     {
     113             :         // Replace an empty label string with column name.
     114          14 :         OUStringBuffer aBuf;
     115          14 :         aBuf.append(ScGlobal::GetRscString(STR_COLUMN));
     116          14 :         aBuf.append(' ');
     117             : 
     118          14 :         ScAddress aColAddr(nCol, 0, 0);
     119          14 :         aBuf.append(aColAddr.Format(SCA_VALID_COL, NULL));
     120          14 :         aDocStr = aBuf.makeStringAndClear();
     121             :     }
     122         181 :     return aDocStr;
     123             : }
     124             : 
     125        2275 : void initFromCell(
     126             :     ScDPCache& rCache, ScDocument* pDoc, SCCOL nCol, SCROW nRow, SCTAB nTab,
     127             :     ScDPItemData& rData, sal_uLong& rNumFormat)
     128             : {
     129        2275 :     OUString aDocStr = pDoc->GetString(nCol, nRow, nTab);
     130        2275 :     rNumFormat = 0;
     131             : 
     132        2275 :     ScAddress aPos(nCol, nRow, nTab);
     133             : 
     134        2275 :     if (pDoc->GetErrCode(aPos))
     135             :     {
     136           0 :         rData.SetErrorString(rCache.InternString(aDocStr));
     137             :     }
     138        2275 :     else if (pDoc->HasValueData(nCol, nRow, nTab))
     139             :     {
     140        1416 :         double fVal = pDoc->GetValue(aPos);
     141        1416 :         rNumFormat = pDoc->GetNumberFormat(aPos);
     142        1416 :         rData.SetValue(fVal);
     143             :     }
     144         859 :     else if (pDoc->HasData(nCol, nRow, nTab))
     145             :     {
     146         827 :         rData.SetString(rCache.InternString(aDocStr));
     147             :     }
     148             :     else
     149          32 :         rData.SetEmpty();
     150        2275 : }
     151             : 
     152       51640 : struct Bucket
     153             : {
     154             :     ScDPItemData maValue;
     155             :     SCROW mnOrderIndex;
     156             :     SCROW mnDataIndex;
     157             :     SCROW mnValueSortIndex;
     158        2275 :     Bucket(const ScDPItemData& rValue, SCROW nOrder, SCROW nData) :
     159        2275 :         maValue(rValue), mnOrderIndex(nOrder), mnDataIndex(nData), mnValueSortIndex(0) {}
     160             : };
     161             : 
     162             : #if DEBUG_PIVOT_TABLE
     163             : #include <iostream>
     164             : using std::cout;
     165             : using std::endl;
     166             : 
     167             : struct PrintBucket : std::unary_function<Bucket, void>
     168             : {
     169             :     void operator() (const Bucket& v) const
     170             :     {
     171             :         cout << "value: " << v.maValue.GetValue() << "  order index: " << v.mnOrderIndex << "  data index: " << v.mnDataIndex << "  value sort index: " << v.mnValueSortIndex << endl;
     172             :     }
     173             : };
     174             : 
     175             : #endif
     176             : 
     177             : struct LessByValue : std::binary_function<Bucket, Bucket, bool>
     178             : {
     179        9825 :     bool operator() (const Bucket& left, const Bucket& right) const
     180             :     {
     181        9825 :         return left.maValue < right.maValue;
     182             :     }
     183             : };
     184             : 
     185             : struct LessByValueSortIndex : std::binary_function<Bucket, Bucket, bool>
     186             : {
     187       13279 :     bool operator() (const Bucket& left, const Bucket& right) const
     188             :     {
     189       13279 :         return left.mnValueSortIndex < right.mnValueSortIndex;
     190             :     }
     191             : };
     192             : 
     193             : struct LessByDataIndex : std::binary_function<Bucket, Bucket, bool>
     194             : {
     195       12898 :     bool operator() (const Bucket& left, const Bucket& right) const
     196             :     {
     197       12898 :         return left.mnDataIndex < right.mnDataIndex;
     198             :     }
     199             : };
     200             : 
     201             : struct EqualByOrderIndex : std::binary_function<Bucket, Bucket, bool>
     202             : {
     203        2094 :     bool operator() (const Bucket& left, const Bucket& right) const
     204             :     {
     205        2094 :         return left.mnOrderIndex == right.mnOrderIndex;
     206             :     }
     207             : };
     208             : 
     209             : class PushBackValue : std::unary_function<Bucket, void>
     210             : {
     211             :     ScDPCache::ItemsType& mrItems;
     212             : public:
     213         181 :     PushBackValue(ScDPCache::ItemsType& _items) : mrItems(_items) {}
     214         935 :     void operator() (const Bucket& v)
     215             :     {
     216         935 :         mrItems.push_back(v.maValue);
     217         935 :     }
     218             : };
     219             : 
     220             : class PushBackOrderIndex : std::unary_function<Bucket, void>
     221             : {
     222             :     ScDPCache::IndexArrayType& mrData;
     223             : public:
     224         181 :     PushBackOrderIndex(ScDPCache::IndexArrayType& _items) : mrData(_items) {}
     225        2275 :     void operator() (const Bucket& v)
     226             :     {
     227        2275 :         mrData.push_back(v.mnOrderIndex);
     228        2275 :     }
     229             : };
     230             : 
     231             : class TagValueSortOrder : std::unary_function<Bucket, void>
     232             : {
     233             :     SCROW mnCurIndex;
     234             : public:
     235         181 :     TagValueSortOrder() : mnCurIndex(0) {}
     236        2275 :     void operator() (Bucket& v)
     237             :     {
     238        2275 :         v.mnValueSortIndex = mnCurIndex++;
     239        2275 :     }
     240             : };
     241             : 
     242         181 : void processBuckets(std::vector<Bucket>& aBuckets, ScDPCache::Field& rField)
     243             : {
     244         181 :     if (aBuckets.empty())
     245         181 :         return;
     246             : 
     247             :     // Sort by the value.
     248         181 :     std::sort(aBuckets.begin(), aBuckets.end(), LessByValue());
     249             : 
     250             :     // Remember this sort order.
     251         181 :     std::for_each(aBuckets.begin(), aBuckets.end(), TagValueSortOrder());
     252             : 
     253             :     {
     254             :         // Set order index such that unique values have identical index value.
     255         181 :         SCROW nCurIndex = 0;
     256         181 :         std::vector<Bucket>::iterator it = aBuckets.begin(), itEnd = aBuckets.end();
     257         181 :         ScDPItemData aPrev = it->maValue;
     258         181 :         it->mnOrderIndex = nCurIndex;
     259        2275 :         for (++it; it != itEnd; ++it)
     260             :         {
     261        2094 :             if (!aPrev.IsCaseInsEqual(it->maValue))
     262         754 :                 ++nCurIndex;
     263             : 
     264        2094 :             it->mnOrderIndex = nCurIndex;
     265        2094 :             aPrev = it->maValue;
     266         181 :         }
     267             :     }
     268             : 
     269             :     // Re-sort the bucket this time by the data index.
     270         181 :     std::sort(aBuckets.begin(), aBuckets.end(), LessByDataIndex());
     271             : 
     272             :     // Copy the order index series into the field object.
     273         181 :     rField.maData.reserve(aBuckets.size());
     274         181 :     std::for_each(aBuckets.begin(), aBuckets.end(), PushBackOrderIndex(rField.maData));
     275             : 
     276             :     // Sort by the value again.
     277         181 :     std::sort(aBuckets.begin(), aBuckets.end(), LessByValueSortIndex());
     278             : 
     279             :     // Unique by value.
     280             :     std::vector<Bucket>::iterator itUniqueEnd =
     281         181 :         std::unique(aBuckets.begin(), aBuckets.end(), EqualByOrderIndex());
     282             : 
     283             :     // Copy the unique values into items.
     284         181 :     std::vector<Bucket>::iterator itBeg = aBuckets.begin();
     285         181 :     size_t nLen = distance(itBeg, itUniqueEnd);
     286         181 :     rField.maItems.reserve(nLen);
     287         181 :     std::for_each(itBeg, itUniqueEnd, PushBackValue(rField.maItems));
     288             : }
     289             : 
     290             : }
     291             : 
     292          65 : bool ScDPCache::InitFromDoc(ScDocument* pDoc, const ScRange& rRange)
     293             : {
     294          65 :     Clear();
     295             : 
     296             :     // Make sure the formula cells within the data range are interpreted
     297             :     // during this call, for this method may be called from the interpretation
     298             :     // of GETPIVOTDATA, which disables nested formula interpretation without
     299             :     // increasing the macro level.
     300          65 :     MacroInterpretIncrementer aMacroInc(pDoc);
     301             : 
     302          65 :     SCROW nStartRow = rRange.aStart.Row();  // start of data
     303          65 :     SCROW nEndRow = rRange.aEnd.Row();
     304             : 
     305             :     // Sanity check
     306          65 :     if (!ValidRow(nStartRow) || !ValidRow(nEndRow) || nEndRow <= nStartRow)
     307          17 :         return false;
     308             : 
     309          48 :     sal_uInt16 nStartCol = rRange.aStart.Col();
     310          48 :     sal_uInt16 nEndCol = rRange.aEnd.Col();
     311          48 :     sal_uInt16 nDocTab = rRange.aStart.Tab();
     312             : 
     313          48 :     mnColumnCount = nEndCol - nStartCol + 1;
     314             : 
     315             :     // this row count must include the trailing empty rows.
     316          48 :     mnRowCount = nEndRow - nStartRow; // skip the topmost label row.
     317             : 
     318             :     // Skip trailing empty rows if exists.
     319          48 :     SCCOL nCol1 = nStartCol, nCol2 = nEndCol;
     320          48 :     SCROW nRow1 = nStartRow, nRow2 = nEndRow;
     321          48 :     pDoc->ShrinkToDataArea(nDocTab, nCol1, nRow1, nCol2, nRow2);
     322          48 :     bool bTailEmptyRows = nEndRow > nRow2; // Trailing empty rows exist.
     323          48 :     nEndRow = nRow2;
     324             : 
     325          48 :     if (nEndRow <= nStartRow)
     326             :     {
     327             :         // Check this again since the end row position has changed. It's
     328             :         // possible that the new end row becomes lower than the start row
     329             :         // after the shrinkage.
     330           0 :         Clear();
     331           0 :         return false;
     332             :     }
     333             : 
     334          48 :     maFields.reserve(mnColumnCount);
     335         229 :     for (size_t i = 0; i < static_cast<size_t>(mnColumnCount); ++i)
     336         181 :         maFields.push_back(new Field);
     337             : 
     338          48 :     maLabelNames.reserve(mnColumnCount+1);
     339             : 
     340          96 :     ScDPItemData aData;
     341         229 :     for (sal_uInt16 nCol = nStartCol; nCol <= nEndCol; ++nCol)
     342             :     {
     343         181 :         AddLabel(createLabelString(pDoc, nCol, nStartRow, nDocTab));
     344         181 :         Field& rField = maFields[nCol-nStartCol];
     345         181 :         std::vector<Bucket> aBuckets;
     346         181 :         aBuckets.reserve(nEndRow-nStartRow); // skip the topmost label cell.
     347             : 
     348             :         // Push back all original values.
     349         181 :         SCROW nOffset = nStartRow + 1;
     350        2456 :         for (SCROW i = 0, n = nEndRow-nStartRow; i < n; ++i)
     351             :         {
     352        2275 :             SCROW nRow = i + nOffset;
     353        2275 :             sal_uLong nNumFormat = 0;
     354        2275 :             initFromCell(*this, pDoc, nCol, nRow, nDocTab, aData, nNumFormat);
     355        2275 :             aBuckets.push_back(Bucket(aData, 0, i));
     356             : 
     357        2275 :             if (!aData.IsEmpty())
     358             :             {
     359        2243 :                 maEmptyRows.insert_back(i, i+1, false);
     360        2243 :                 if (nNumFormat)
     361             :                     // Only take non-default number format.
     362         815 :                     rField.mnNumFormat = nNumFormat;
     363             :             }
     364             :         }
     365             : 
     366         181 :         processBuckets(aBuckets, rField);
     367             : 
     368         181 :         if (bTailEmptyRows)
     369             :         {
     370             :             // If the last item is not empty, append one. Note that the items
     371             :             // are sorted, and empty item should come last when sorted.
     372          28 :             if (rField.maItems.empty() || !rField.maItems.back().IsEmpty())
     373             :             {
     374          24 :                 aData.SetEmpty();
     375          24 :                 rField.maItems.push_back(aData);
     376             :             }
     377             :         }
     378         181 :     }
     379             : 
     380          48 :     PostInit();
     381         113 :     return true;
     382             : }
     383             : 
     384           0 : bool ScDPCache::InitFromDataBase(DBConnector& rDB)
     385             : {
     386           0 :     Clear();
     387             : 
     388             :     try
     389             :     {
     390           0 :         mnColumnCount = rDB.getColumnCount();
     391           0 :         maFields.clear();
     392           0 :         maFields.reserve(mnColumnCount);
     393           0 :         for (size_t i = 0; i < static_cast<size_t>(mnColumnCount); ++i)
     394           0 :             maFields.push_back(new Field);
     395             : 
     396             :         // Get column titles and types.
     397           0 :         maLabelNames.clear();
     398           0 :         maLabelNames.reserve(mnColumnCount+1);
     399             : 
     400           0 :         for (sal_Int32 nCol = 0; nCol < mnColumnCount; ++nCol)
     401             :         {
     402           0 :             OUString aColTitle = rDB.getColumnLabel(nCol);
     403           0 :             AddLabel(aColTitle);
     404           0 :         }
     405             : 
     406           0 :         std::vector<Bucket> aBuckets;
     407           0 :         ScDPItemData aData;
     408           0 :         for (sal_Int32 nCol = 0; nCol < mnColumnCount; ++nCol)
     409             :         {
     410           0 :             if (!rDB.first())
     411           0 :                 continue;
     412             : 
     413           0 :             aBuckets.clear();
     414           0 :             Field& rField = maFields[nCol];
     415           0 :             SCROW nRow = 0;
     416           0 :             do
     417             :             {
     418           0 :                 short nFormatType = NUMBERFORMAT_UNDEFINED;
     419           0 :                 aData.SetEmpty();
     420           0 :                 rDB.getValue(nCol, aData, nFormatType);
     421           0 :                 aBuckets.push_back(Bucket(aData, 0, nRow));
     422           0 :                 if (!aData.IsEmpty())
     423             :                 {
     424           0 :                     maEmptyRows.insert_back(nRow, nRow+1, false);
     425           0 :                     SvNumberFormatter* pFormatter = mpDoc->GetFormatTable();
     426           0 :                     rField.mnNumFormat = pFormatter ? pFormatter->GetStandardFormat(nFormatType) : 0;
     427             :                 }
     428             : 
     429           0 :                 ++nRow;
     430             :             }
     431           0 :             while (rDB.next());
     432             : 
     433           0 :             processBuckets(aBuckets, rField);
     434             :         }
     435             : 
     436           0 :         rDB.finish();
     437             : 
     438           0 :         if (!maFields.empty())
     439           0 :             mnRowCount = maFields[0].maData.size();
     440             : 
     441           0 :         PostInit();
     442           0 :         return true;
     443             :     }
     444           0 :     catch (const Exception&)
     445             :     {
     446           0 :         return false;
     447             :     }
     448             : }
     449             : 
     450        1068 : bool ScDPCache::ValidQuery( SCROW nRow, const ScQueryParam &rParam) const
     451             : {
     452        1068 :     if (!rParam.GetEntryCount())
     453           0 :         return true;
     454             : 
     455        1068 :     if (!rParam.GetEntry(0).bDoQuery)
     456         957 :         return true;
     457             : 
     458         111 :     bool bMatchWholeCell = mpDoc->GetDocOptions().IsMatchWholeCell();
     459             : 
     460         111 :     SCSIZE nEntryCount = rParam.GetEntryCount();
     461         111 :     std::vector<bool> aPassed(nEntryCount, false);
     462             : 
     463         111 :     long nPos = -1;
     464             :     CollatorWrapper* pCollator = (rParam.bCaseSens ? ScGlobal::GetCaseCollator() :
     465         111 :                                   ScGlobal::GetCollator() );
     466             :     ::utl::TransliterationWrapper* pTransliteration = (rParam.bCaseSens ?
     467         111 :                                                        ScGlobal::GetCaseTransliteration() : ScGlobal::GetpTransliteration());
     468             : 
     469         317 :     for (size_t i = 0; i < nEntryCount && rParam.GetEntry(i).bDoQuery; ++i)
     470             :     {
     471         206 :         const ScQueryEntry& rEntry = rParam.GetEntry(i);
     472         206 :         const ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
     473             :         // we can only handle one single direct query
     474             :         // #i115431# nField in QueryParam is the sheet column, not the field within the source range
     475         206 :         SCCOL nQueryCol = (SCCOL)rEntry.nField;
     476         206 :         if ( nQueryCol < rParam.nCol1 )
     477           0 :             nQueryCol = rParam.nCol1;
     478         206 :         if ( nQueryCol > rParam.nCol2 )
     479           0 :             nQueryCol = rParam.nCol2;
     480         206 :         SCCOL nSourceField = nQueryCol - rParam.nCol1;
     481         206 :         SCROW nId = GetItemDataId( nSourceField, nRow, false );
     482         206 :         const ScDPItemData* pCellData = GetItemDataById( nSourceField, nId );
     483             : 
     484         206 :         bool bOk = false;
     485             : 
     486         206 :         if (rEntry.GetQueryItem().meType == ScQueryEntry::ByEmpty)
     487             :         {
     488           0 :             if (rEntry.IsQueryByEmpty())
     489           0 :                 bOk = pCellData->IsEmpty();
     490             :             else
     491             :             {
     492             :                 OSL_ASSERT(rEntry.IsQueryByNonEmpty());
     493           0 :                 bOk = !pCellData->IsEmpty();
     494             :             }
     495             :         }
     496         206 :         else if (rEntry.GetQueryItem().meType != ScQueryEntry::ByString && pCellData->IsValue())
     497             :         {   // by Value
     498         206 :             double nCellVal = pCellData->GetValue();
     499             : 
     500         206 :             switch (rEntry.eOp)
     501             :             {
     502             :                 case SC_EQUAL :
     503          16 :                     bOk = ::rtl::math::approxEqual(nCellVal, rItem.mfVal);
     504          16 :                     break;
     505             :                 case SC_LESS :
     506           0 :                     bOk = (nCellVal < rItem.mfVal) && !::rtl::math::approxEqual(nCellVal, rItem.mfVal);
     507           0 :                     break;
     508             :                 case SC_GREATER :
     509          95 :                     bOk = (nCellVal > rItem.mfVal) && !::rtl::math::approxEqual(nCellVal, rItem.mfVal);
     510          95 :                     break;
     511             :                 case SC_LESS_EQUAL :
     512          95 :                     bOk = (nCellVal < rItem.mfVal) || ::rtl::math::approxEqual(nCellVal, rItem.mfVal);
     513          95 :                     break;
     514             :                 case SC_GREATER_EQUAL :
     515           0 :                     bOk = (nCellVal > rItem.mfVal) || ::rtl::math::approxEqual(nCellVal, rItem.mfVal);
     516           0 :                     break;
     517             :                 case SC_NOT_EQUAL :
     518           0 :                     bOk = !::rtl::math::approxEqual(nCellVal, rItem.mfVal);
     519           0 :                     break;
     520             :                 default:
     521           0 :                     bOk= false;
     522           0 :                     break;
     523             :             }
     524             :         }
     525           0 :         else if ((rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL)
     526           0 :                  || (rEntry.GetQueryItem().meType == ScQueryEntry::ByString
     527           0 :                      && pCellData->HasStringData() )
     528             :                 )
     529             :         {   // by String
     530           0 :             OUString  aCellStr = pCellData->GetString();
     531             : 
     532           0 :             bool bRealRegExp = (rParam.bRegExp && ((rEntry.eOp == SC_EQUAL)
     533           0 :                                                    || (rEntry.eOp == SC_NOT_EQUAL)));
     534           0 :             bool bTestRegExp = false;
     535           0 :             if (bRealRegExp || bTestRegExp)
     536             :             {
     537           0 :                 sal_Int32 nStart = 0;
     538           0 :                 sal_Int32 nEnd   = aCellStr.getLength();
     539             : 
     540             :                 bool bMatch = (bool) rEntry.GetSearchTextPtr( rParam.bCaseSens )
     541           0 :                               ->SearchForward( aCellStr, &nStart, &nEnd );
     542             :                 // from 614 on, nEnd is behind the found text
     543           0 :                 if (bMatch && bMatchWholeCell
     544           0 :                     && (nStart != 0 || nEnd != aCellStr.getLength()))
     545           0 :                     bMatch = false;    // RegExp must match entire cell string
     546           0 :                 if (bRealRegExp)
     547           0 :                     bOk = ((rEntry.eOp == SC_NOT_EQUAL) ? !bMatch : bMatch);
     548             :             }
     549           0 :             if (!bRealRegExp)
     550             :             {
     551           0 :                 if (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL)
     552             :                 {
     553           0 :                     if (bMatchWholeCell)
     554             :                     {
     555             :                         // TODO: Use shared string for fast equality check.
     556           0 :                         OUString aStr = rEntry.GetQueryItem().maString.getString();
     557           0 :                         bOk = pTransliteration->isEqual(aCellStr, aStr);
     558           0 :                         bool bHasStar = false;
     559             :                         sal_Int32 nIndex;
     560           0 :                         if (( nIndex = aStr.indexOf('*') ) != -1)
     561           0 :                             bHasStar = true;
     562           0 :                         if (bHasStar && (nIndex>0))
     563             :                         {
     564           0 :                             for (sal_Int32 j=0;(j<nIndex) && (j< aCellStr.getLength()) ; j++)
     565             :                             {
     566           0 :                                 if (aCellStr[j] == aStr[j])
     567             :                                 {
     568           0 :                                     bOk=true;
     569             :                                 }
     570             :                                 else
     571             :                                 {
     572           0 :                                     bOk=false;
     573           0 :                                     break;
     574             :                                 }
     575             :                             }
     576           0 :                         }
     577             :                     }
     578             :                     else
     579             :                     {
     580           0 :                         OUString aQueryStr = rEntry.GetQueryItem().maString.getString();
     581           0 :                         ::com::sun::star::uno::Sequence< sal_Int32 > xOff;
     582             :                         OUString aCell = pTransliteration->transliterate(
     583           0 :                             aCellStr, ScGlobal::eLnge, 0, aCellStr.getLength(), &xOff);
     584             :                         OUString aQuer = pTransliteration->transliterate(
     585           0 :                             aQueryStr, ScGlobal::eLnge, 0, aQueryStr.getLength(), &xOff);
     586           0 :                         bOk = (aCell.indexOf( aQuer ) != -1);
     587             :                     }
     588           0 :                     if (rEntry.eOp == SC_NOT_EQUAL)
     589           0 :                         bOk = !bOk;
     590             :                 }
     591             :                 else
     592             :                 {   // use collator here because data was probably sorted
     593             :                     sal_Int32 nCompare = pCollator->compareString(
     594           0 :                         aCellStr, rEntry.GetQueryItem().maString.getString());
     595           0 :                     switch (rEntry.eOp)
     596             :                     {
     597             :                         case SC_LESS :
     598           0 :                             bOk = (nCompare < 0);
     599           0 :                             break;
     600             :                         case SC_GREATER :
     601           0 :                             bOk = (nCompare > 0);
     602           0 :                             break;
     603             :                         case SC_LESS_EQUAL :
     604           0 :                             bOk = (nCompare <= 0);
     605           0 :                             break;
     606             :                         case SC_GREATER_EQUAL :
     607           0 :                             bOk = (nCompare >= 0);
     608           0 :                             break;
     609             :                         case SC_NOT_EQUAL:
     610             :                             OSL_FAIL("SC_NOT_EQUAL");
     611           0 :                             break;
     612             :                         case SC_TOPVAL:
     613             :                         case SC_BOTVAL:
     614             :                         case SC_TOPPERC:
     615             :                         case SC_BOTPERC:
     616             :                         default:
     617           0 :                             break;
     618             :                     }
     619             :                 }
     620           0 :             }
     621             :         }
     622             : 
     623         206 :         if (nPos == -1)
     624             :         {
     625         111 :             nPos++;
     626         111 :             aPassed[nPos] = bOk;
     627             :         }
     628             :         else
     629             :         {
     630          95 :             if (rEntry.eConnect == SC_AND)
     631             :             {
     632          95 :                 aPassed[nPos] = aPassed[nPos] && bOk;
     633             :             }
     634             :             else
     635             :             {
     636           0 :                 nPos++;
     637           0 :                 aPassed[nPos] = bOk;
     638             :             }
     639             :         }
     640             :     }
     641             : 
     642         111 :     for (long j=1; j <= nPos; j++)
     643           0 :         aPassed[0] = aPassed[0] || aPassed[j];
     644             : 
     645         111 :     bool bRet = aPassed[0];
     646         111 :     return bRet;
     647             : }
     648             : 
     649          12 : bool ScDPCache::IsRowEmpty(SCROW nRow) const
     650             : {
     651          12 :     bool bEmpty = true;
     652          12 :     maEmptyRows.search_tree(nRow, bEmpty);
     653          12 :     return bEmpty;
     654             : }
     655             : 
     656         806 : const ScDPCache::GroupItems* ScDPCache::GetGroupItems(long nDim) const
     657             : {
     658         806 :     if (nDim < 0)
     659           0 :         return NULL;
     660             : 
     661         806 :     long nSourceCount = static_cast<long>(maFields.size());
     662         806 :     if (nDim < nSourceCount)
     663         766 :         return maFields[nDim].mpGroup.get();
     664             : 
     665          40 :     nDim -= nSourceCount;
     666          40 :     if (nDim < static_cast<long>(maGroupFields.size()))
     667          40 :         return &maGroupFields[nDim];
     668             : 
     669           0 :     return NULL;
     670             : }
     671             : 
     672       26313 : OUString ScDPCache::GetDimensionName(LabelsType::size_type nDim) const
     673             : {
     674             :     OSL_ENSURE(nDim < maLabelNames.size()-1 , "ScDPTableDataCache::GetDimensionName");
     675             :     OSL_ENSURE(maLabelNames.size() == static_cast <sal_uInt16> (mnColumnCount+1), "ScDPTableDataCache::GetDimensionName");
     676             : 
     677       26313 :     if ( nDim+1 < maLabelNames.size() )
     678             :     {
     679       26313 :         return maLabelNames[nDim+1];
     680             :     }
     681             :     else
     682           0 :         return OUString();
     683             : }
     684             : 
     685             : namespace {
     686             : 
     687             : typedef boost::unordered_set<OUString, OUStringHash> LabelSet;
     688             : 
     689             : class InsertLabel : public std::unary_function<OUString, void>
     690             : {
     691             :     LabelSet& mrNames;
     692             : public:
     693         181 :     InsertLabel(LabelSet& rNames) : mrNames(rNames) {}
     694         490 :     void operator() (const OUString& r)
     695             :     {
     696         490 :         mrNames.insert(r);
     697         490 :     }
     698             : };
     699             : 
     700             : }
     701             : 
     702          48 : void ScDPCache::PostInit()
     703             : {
     704             :     OSL_ENSURE(!maFields.empty(), "Cache not initialized!");
     705             : 
     706          48 :     maEmptyRows.build_tree();
     707             :     typedef mdds::flat_segment_tree<SCROW, bool>::const_reverse_iterator itr_type;
     708          48 :     itr_type it = maEmptyRows.rbegin();
     709             :     OSL_ENSURE(it != maEmptyRows.rend(), "corrupt flat_segment_tree instance!");
     710          48 :     mnDataSize = maFields[0].maData.size();
     711          48 :     ++it; // Skip the first position.
     712             :     OSL_ENSURE(it != maEmptyRows.rend(), "buggy version of flat_segment_tree is used.");
     713          48 :     if (it->second)
     714             :     {
     715          48 :         SCROW nLastNonEmpty = it->first - 1;
     716          48 :         if (nLastNonEmpty+1 < mnDataSize)
     717           1 :             mnDataSize = nLastNonEmpty+1;
     718             :     }
     719          48 : }
     720             : 
     721          65 : void ScDPCache::Clear()
     722             : {
     723          65 :     mnColumnCount = 0;
     724          65 :     mnRowCount = 0;
     725          65 :     maFields.clear();
     726          65 :     maLabelNames.clear();
     727          65 :     maGroupFields.clear();
     728          65 :     maEmptyRows.clear();
     729          65 :     maStringPool.clear();
     730          65 : }
     731             : 
     732         181 : void ScDPCache::AddLabel(const OUString& rLabel)
     733             : {
     734             : 
     735         181 :     if ( maLabelNames.empty() )
     736          48 :         maLabelNames.push_back(ScGlobal::GetRscString(STR_PIVOT_DATA));
     737             : 
     738             :     //reset name if needed
     739         181 :     LabelSet aExistingNames;
     740         181 :     std::for_each(maLabelNames.begin(), maLabelNames.end(), InsertLabel(aExistingNames));
     741         181 :     sal_Int32 nSuffix = 1;
     742         362 :     OUString aNewName = rLabel;
     743             :     while (true)
     744             :     {
     745         181 :         if (!aExistingNames.count(aNewName))
     746             :         {
     747             :             // unique name found!
     748         181 :             maLabelNames.push_back(aNewName);
     749         362 :             return;
     750             :         }
     751             : 
     752             :         // Name already exists.
     753           0 :         OUStringBuffer aBuf(rLabel);
     754           0 :         aBuf.append(++nSuffix);
     755           0 :         aNewName = aBuf.makeStringAndClear();
     756         181 :     }
     757             : }
     758             : 
     759        8540 : SCROW ScDPCache::GetItemDataId(sal_uInt16 nDim, SCROW nRow, bool bRepeatIfEmpty) const
     760             : {
     761             :     OSL_ENSURE(nDim < mnColumnCount, "ScDPTableDataCache::GetItemDataId ");
     762             : 
     763        8540 :     const Field& rField = maFields[nDim];
     764        8540 :     if (static_cast<size_t>(nRow) >= rField.maData.size())
     765             :     {
     766             :         // nRow is in the trailing empty rows area.
     767          58 :         if (bRepeatIfEmpty)
     768           0 :             nRow = rField.maData.size()-1; // Move to the last non-empty row.
     769             :         else
     770             :             // Return the last item, which should always be empty if the
     771             :             // initialization has skipped trailing empty rows.
     772          58 :             return rField.maItems.size()-1;
     773             : 
     774             :     }
     775        8482 :     else if (bRepeatIfEmpty)
     776             :     {
     777          24 :         while (nRow > 0 && rField.maItems[rField.maData[nRow]].IsEmpty())
     778           0 :             --nRow;
     779             :     }
     780             : 
     781        8482 :     return rField.maData[nRow];
     782             : }
     783             : 
     784       12877 : const ScDPItemData* ScDPCache::GetItemDataById(long nDim, SCROW nId) const
     785             : {
     786       12877 :     if (nDim < 0 || nId < 0)
     787           0 :         return NULL;
     788             : 
     789       12877 :     size_t nSourceCount = maFields.size();
     790       12877 :     size_t nDimPos = static_cast<size_t>(nDim);
     791       12877 :     size_t nItemId = static_cast<size_t>(nId);
     792       12877 :     if (nDimPos < nSourceCount)
     793             :     {
     794             :         // source field.
     795       12545 :         const Field& rField = maFields[nDimPos];
     796       12545 :         if (nItemId < rField.maItems.size())
     797       11648 :             return &rField.maItems[nItemId];
     798             : 
     799         897 :         if (!rField.mpGroup)
     800           3 :             return NULL;
     801             : 
     802         894 :         nItemId -= rField.maItems.size();
     803         894 :         const ItemsType& rGI = rField.mpGroup->maItems;
     804         894 :         if (nItemId >= rGI.size())
     805           0 :             return NULL;
     806             : 
     807         894 :         return &rGI[nItemId];
     808             :     }
     809             : 
     810             :     // Try group fields.
     811         332 :     nDimPos -= nSourceCount;
     812         332 :     if (nDimPos >= maGroupFields.size())
     813          22 :         return NULL;
     814             : 
     815         310 :     const ItemsType& rGI = maGroupFields[nDimPos].maItems;
     816         310 :     if (nItemId >= rGI.size())
     817           0 :         return NULL;
     818             : 
     819         310 :     return &rGI[nItemId];
     820             : }
     821             : 
     822           1 : size_t ScDPCache::GetFieldCount() const
     823             : {
     824           1 :     return maFields.size();
     825             : }
     826             : 
     827           1 : size_t ScDPCache::GetGroupFieldCount() const
     828             : {
     829           1 :     return maGroupFields.size();
     830             : }
     831             : 
     832         663 : SCROW ScDPCache::GetRowCount() const
     833             : {
     834         663 :     return mnRowCount;
     835             : }
     836             : 
     837         306 : SCROW ScDPCache::GetDataSize() const
     838             : {
     839             :     OSL_ENSURE(mnDataSize <= GetRowCount(), "Data size should never be larger than the row count.");
     840         306 :     return mnDataSize >= 0 ? mnDataSize : 0;
     841             : }
     842             : 
     843          18 : const ScDPCache::ItemsType& ScDPCache::GetDimMemberValues(SCCOL nDim) const
     844             : {
     845             :     OSL_ENSURE( nDim>=0 && nDim < mnColumnCount ," nDim < mnColumnCount ");
     846          18 :     return maFields.at(nDim).maItems;
     847             : }
     848             : 
     849        3373 : sal_uLong ScDPCache::GetNumberFormat( long nDim ) const
     850             : {
     851        3373 :     if ( nDim >= mnColumnCount )
     852           0 :         return 0;
     853             : 
     854             :     // TODO: Find a way to determine the dominant number format in presence of
     855             :     // multiple number formats in the same field.
     856        3373 :     return maFields[nDim].mnNumFormat;
     857             : }
     858             : 
     859        1621 : bool ScDPCache::IsDateDimension( long nDim ) const
     860             : {
     861        1621 :     if (nDim >= mnColumnCount)
     862           0 :         return false;
     863             : 
     864        1621 :     SvNumberFormatter* pFormatter = mpDoc->GetFormatTable();
     865        1621 :     if (!pFormatter)
     866           0 :         return false;
     867             : 
     868        1621 :     short eType = pFormatter->GetType(maFields[nDim].mnNumFormat);
     869        1621 :     return (eType == NUMBERFORMAT_DATE) || (eType == NUMBERFORMAT_DATETIME);
     870             : }
     871             : 
     872         655 : long ScDPCache::GetDimMemberCount(long nDim) const
     873             : {
     874             :     OSL_ENSURE( nDim>=0 && nDim < mnColumnCount ," ScDPTableDataCache::GetDimMemberCount : out of bound ");
     875         655 :     return maFields[nDim].maItems.size();
     876             : }
     877             : 
     878          19 : SCCOL ScDPCache::GetDimensionIndex(const OUString& sName) const
     879             : {
     880          39 :     for (size_t i = 1; i < maLabelNames.size(); ++i)
     881             :     {
     882          39 :         if (maLabelNames[i].equals(sName))
     883          19 :             return static_cast<SCCOL>(i-1);
     884             :     }
     885           0 :     return -1;
     886             : }
     887             : 
     888         827 : const OUString* ScDPCache::InternString(const OUString& rStr) const
     889             : {
     890         827 :     StringSetType::iterator it = maStringPool.find(rStr);
     891         827 :     if (it != maStringPool.end())
     892             :         // In the pool.
     893         631 :         return &(*it);
     894             : 
     895         196 :     std::pair<StringSetType::iterator, bool> r = maStringPool.insert(rStr);
     896         196 :     return r.second ? &(*r.first) : NULL;
     897             : }
     898             : 
     899          81 : void ScDPCache::AddReference(ScDPObject* pObj) const
     900             : {
     901          81 :     maRefObjects.insert(pObj);
     902          81 : }
     903             : 
     904          81 : void ScDPCache::RemoveReference(ScDPObject* pObj) const
     905             : {
     906          81 :     if (mbDisposing)
     907             :         // Object being deleted.
     908          96 :         return;
     909             : 
     910          66 :     maRefObjects.erase(pObj);
     911          66 :     if (maRefObjects.empty())
     912          47 :         mpDoc->GetDPCollection()->RemoveCache(this);
     913             : }
     914             : 
     915           3 : const ScDPCache::ObjectSetType& ScDPCache::GetAllReferences() const
     916             : {
     917           3 :     return maRefObjects;
     918             : }
     919             : 
     920         100 : SCROW ScDPCache::GetIdByItemData(long nDim, const ScDPItemData& rItem) const
     921             : {
     922         100 :     if (nDim < 0)
     923           0 :         return -1;
     924             : 
     925         100 :     if (nDim < mnColumnCount)
     926             :     {
     927             :         // source field.
     928          34 :         const ItemsType& rItems = maFields[nDim].maItems;
     929         450 :         for (size_t i = 0, n = rItems.size(); i < n; ++i)
     930             :         {
     931         416 :             if (rItems[i] == rItem)
     932           0 :                 return i;
     933             :         }
     934             : 
     935          34 :         if (!maFields[nDim].mpGroup)
     936           0 :             return -1;
     937             : 
     938             :         // grouped source field.
     939          34 :         const ItemsType& rGI = maFields[nDim].mpGroup->maItems;
     940         130 :         for (size_t i = 0, n = rGI.size(); i < n; ++i)
     941             :         {
     942         130 :             if (rGI[i] == rItem)
     943          34 :                 return rItems.size() + i;
     944             :         }
     945           0 :         return -1;
     946             :     }
     947             : 
     948             :     // group field.
     949          66 :     nDim -= mnColumnCount;
     950          66 :     if (static_cast<size_t>(nDim) < maGroupFields.size())
     951             :     {
     952          66 :         const ItemsType& rGI = maGroupFields[nDim].maItems;
     953         110 :         for (size_t i = 0, n = rGI.size(); i < n; ++i)
     954             :         {
     955         110 :             if (rGI[i] == rItem)
     956          66 :                 return i;
     957             :         }
     958             :     }
     959             : 
     960           0 :     return -1;
     961             : }
     962             : 
     963        4530 : OUString ScDPCache::GetFormattedString(long nDim, const ScDPItemData& rItem) const
     964             : {
     965        4530 :     if (nDim < 0)
     966         110 :         return rItem.GetString();
     967             : 
     968        4420 :     ScDPItemData::Type eType = rItem.GetType();
     969        4420 :     if (eType == ScDPItemData::Value)
     970             :     {
     971             :         // Format value using the stored number format.
     972        2886 :         sal_uLong nNumFormat = GetNumberFormat(nDim);
     973        2886 :         SvNumberFormatter* pFormatter = mpDoc->GetFormatTable();
     974        2886 :         if (pFormatter)
     975             :         {
     976        2886 :             Color* pColor = NULL;
     977        2886 :             OUString aStr;
     978        2886 :             pFormatter->GetOutputString(rItem.GetValue(), nNumFormat, aStr, &pColor);
     979        2886 :             return aStr;
     980             :         }
     981             :     }
     982             : 
     983        1534 :     if (eType == ScDPItemData::GroupValue)
     984             :     {
     985         796 :         ScDPItemData::GroupValueAttr aAttr = rItem.GetGroupValue();
     986         796 :         double fStart = 0.0, fEnd = 0.0;
     987         796 :         const GroupItems* p = GetGroupItems(nDim);
     988         796 :         if (p)
     989             :         {
     990         796 :             fStart = p->maInfo.mfStart;
     991         796 :             fEnd = p->maInfo.mfEnd;
     992             :         }
     993             :         return ScDPUtil::getDateGroupName(
     994         796 :             aAttr.mnGroupType, aAttr.mnValue, mpDoc->GetFormatTable(), fStart, fEnd);
     995             :     }
     996             : 
     997         738 :     if (eType == ScDPItemData::RangeStart)
     998             :     {
     999          10 :         double fVal = rItem.GetValue();
    1000          10 :         const GroupItems* p = GetGroupItems(nDim);
    1001          10 :         if (!p)
    1002           0 :             return rItem.GetString();
    1003             : 
    1004          10 :         sal_Unicode cDecSep = ScGlobal::pLocaleData->getNumDecimalSep()[0];
    1005          10 :         return ScDPUtil::getNumGroupName(fVal, p->maInfo, cDecSep, mpDoc->GetFormatTable());
    1006             :     }
    1007             : 
    1008         728 :     return rItem.GetString();
    1009             : }
    1010             : 
    1011          12 : long ScDPCache::AppendGroupField()
    1012             : {
    1013          12 :     maGroupFields.push_back(new GroupItems);
    1014          12 :     return static_cast<long>(maFields.size() + maGroupFields.size() - 1);
    1015             : }
    1016             : 
    1017          18 : void ScDPCache::ResetGroupItems(long nDim, const ScDPNumGroupInfo& rNumInfo, sal_Int32 nGroupType)
    1018             : {
    1019          18 :     if (nDim < 0)
    1020           0 :         return;
    1021             : 
    1022          18 :     long nSourceCount = static_cast<long>(maFields.size());
    1023          18 :     if (nDim < nSourceCount)
    1024             :     {
    1025           6 :         maFields.at(nDim).mpGroup.reset(new GroupItems(rNumInfo, nGroupType));
    1026           6 :         return;
    1027             :     }
    1028             : 
    1029          12 :     nDim -= nSourceCount;
    1030          12 :     if (nDim < static_cast<long>(maGroupFields.size()))
    1031             :     {
    1032          12 :         GroupItems& rGI = maGroupFields[nDim];
    1033          12 :         rGI.maItems.clear();
    1034          12 :         rGI.maInfo = rNumInfo;
    1035          12 :         rGI.mnGroupType = nGroupType;
    1036             :     }
    1037             : }
    1038             : 
    1039         830 : SCROW ScDPCache::SetGroupItem(long nDim, const ScDPItemData& rData)
    1040             : {
    1041         830 :     if (nDim < 0)
    1042           0 :         return -1;
    1043             : 
    1044         830 :     long nSourceCount = static_cast<long>(maFields.size());
    1045         830 :     if (nDim < nSourceCount)
    1046             :     {
    1047         774 :         GroupItems& rGI = *maFields.at(nDim).mpGroup;
    1048         774 :         rGI.maItems.push_back(rData);
    1049         774 :         SCROW nId = maFields[nDim].maItems.size() + rGI.maItems.size() - 1;
    1050         774 :         return nId;
    1051             :     }
    1052             : 
    1053          56 :     nDim -= nSourceCount;
    1054          56 :     if (nDim < static_cast<long>(maGroupFields.size()))
    1055             :     {
    1056          56 :         ItemsType& rItems = maGroupFields.at(nDim).maItems;
    1057          56 :         rItems.push_back(rData);
    1058          56 :         return rItems.size()-1;
    1059             :     }
    1060             : 
    1061           0 :     return -1;
    1062             : }
    1063             : 
    1064          19 : void ScDPCache::GetGroupDimMemberIds(long nDim, std::vector<SCROW>& rIds) const
    1065             : {
    1066          19 :     if (nDim < 0)
    1067           0 :         return;
    1068             : 
    1069          19 :     long nSourceCount = static_cast<long>(maFields.size());
    1070          19 :     if (nDim < nSourceCount)
    1071             :     {
    1072           5 :         if (!maFields.at(nDim).mpGroup)
    1073           0 :             return;
    1074             : 
    1075           5 :         size_t nOffset = maFields[nDim].maItems.size();
    1076           5 :         const ItemsType& rGI = maFields[nDim].mpGroup->maItems;
    1077         774 :         for (size_t i = 0, n = rGI.size(); i < n; ++i)
    1078         769 :             rIds.push_back(static_cast<SCROW>(i + nOffset));
    1079             : 
    1080           5 :         return;
    1081             :     }
    1082             : 
    1083          14 :     nDim -= nSourceCount;
    1084          14 :     if (nDim < static_cast<long>(maGroupFields.size()))
    1085             :     {
    1086          14 :         const ItemsType& rGI = maGroupFields.at(nDim).maItems;
    1087          88 :         for (size_t i = 0, n = rGI.size(); i < n; ++i)
    1088          74 :             rIds.push_back(static_cast<SCROW>(i));
    1089             :     }
    1090             : }
    1091             : 
    1092             : namespace {
    1093             : 
    1094             : struct ClearGroupItems : std::unary_function<ScDPCache::Field, void>
    1095             : {
    1096          28 :     void operator() (ScDPCache::Field& r) const
    1097             :     {
    1098          28 :         r.mpGroup.reset();
    1099          28 :     }
    1100             : };
    1101             : 
    1102             : }
    1103             : 
    1104           8 : void ScDPCache::ClearGroupFields()
    1105             : {
    1106           8 :     maGroupFields.clear();
    1107           8 :     std::for_each(maFields.begin(), maFields.end(), ClearGroupItems());
    1108           8 : }
    1109             : 
    1110         160 : const ScDPNumGroupInfo* ScDPCache::GetNumGroupInfo(long nDim) const
    1111             : {
    1112         160 :     if (nDim < 0)
    1113           0 :         return NULL;
    1114             : 
    1115         160 :     long nSourceCount = static_cast<long>(maFields.size());
    1116         160 :     if (nDim < nSourceCount)
    1117             :     {
    1118          94 :         if (!maFields.at(nDim).mpGroup)
    1119          59 :             return NULL;
    1120             : 
    1121          35 :         return &maFields[nDim].mpGroup->maInfo;
    1122             :     }
    1123             : 
    1124          66 :     nDim -= nSourceCount;
    1125          66 :     if (nDim < static_cast<long>(maGroupFields.size()))
    1126          66 :         return &maGroupFields.at(nDim).maInfo;
    1127             : 
    1128           0 :     return NULL;
    1129             : }
    1130             : 
    1131          51 : sal_Int32 ScDPCache::GetGroupType(long nDim) const
    1132             : {
    1133          51 :     if (nDim < 0)
    1134           0 :         return 0;
    1135             : 
    1136          51 :     long nSourceCount = static_cast<long>(maFields.size());
    1137          51 :     if (nDim < nSourceCount)
    1138             :     {
    1139          17 :         if (!maFields.at(nDim).mpGroup)
    1140           0 :             return 0;
    1141             : 
    1142          17 :         return maFields[nDim].mpGroup->mnGroupType;
    1143             :     }
    1144             : 
    1145          34 :     nDim -= nSourceCount;
    1146          34 :     if (nDim < static_cast<long>(maGroupFields.size()))
    1147          34 :         return maGroupFields.at(nDim).mnGroupType;
    1148             : 
    1149           0 :     return 0;
    1150             : }
    1151             : 
    1152        8379 : SCROW ScDPCache::GetOrder(long /*nDim*/, SCROW nIndex) const
    1153             : {
    1154        8379 :     return nIndex;
    1155             : }
    1156             : 
    1157          16 : ScDocument* ScDPCache::GetDoc() const
    1158             : {
    1159          16 :     return mpDoc;
    1160             : };
    1161             : 
    1162       87847 : long ScDPCache::GetColumnCount() const
    1163             : {
    1164       87847 :     return mnColumnCount;
    1165         102 : }
    1166             : 
    1167             : #if DEBUG_PIVOT_TABLE
    1168             : 
    1169             : namespace {
    1170             : 
    1171             : std::ostream& operator<< (::std::ostream& os, const OUString& str)
    1172             : {
    1173             :     return os << OUStringToOString(str, RTL_TEXTENCODING_UTF8).getStr();
    1174             : }
    1175             : 
    1176             : void dumpItems(const ScDPCache& rCache, long nDim, const ScDPCache::ItemsType& rItems, size_t nOffset)
    1177             : {
    1178             :     for (size_t i = 0; i < rItems.size(); ++i)
    1179             :         cout << "      " << (i+nOffset) << ": " << rCache.GetFormattedString(nDim, rItems[i]) << endl;
    1180             : }
    1181             : 
    1182             : void dumpSourceData(const ScDPCache& rCache, long nDim, const ScDPCache::ItemsType& rItems, const ScDPCache::IndexArrayType& rArray)
    1183             : {
    1184             :     ScDPCache::IndexArrayType::const_iterator it = rArray.begin(), itEnd = rArray.end();
    1185             :     for (; it != itEnd; ++it)
    1186             :         cout << "      '" << rCache.GetFormattedString(nDim, rItems[*it]) << "'" << endl;
    1187             : }
    1188             : 
    1189             : const char* getGroupTypeName(sal_Int32 nType)
    1190             : {
    1191             :     static const char* pNames[] = {
    1192             :         "", "years", "quarters", "months", "days", "hours", "minutes", "seconds"
    1193             :     };
    1194             : 
    1195             :     switch (nType)
    1196             :     {
    1197             :         case sheet::DataPilotFieldGroupBy::YEARS:    return pNames[1];
    1198             :         case sheet::DataPilotFieldGroupBy::QUARTERS: return pNames[2];
    1199             :         case sheet::DataPilotFieldGroupBy::MONTHS:   return pNames[3];
    1200             :         case sheet::DataPilotFieldGroupBy::DAYS:     return pNames[4];
    1201             :         case sheet::DataPilotFieldGroupBy::HOURS:    return pNames[5];
    1202             :         case sheet::DataPilotFieldGroupBy::MINUTES:  return pNames[6];
    1203             :         case sheet::DataPilotFieldGroupBy::SECONDS:  return pNames[7];
    1204             :         default:
    1205             :             ;
    1206             :     }
    1207             : 
    1208             :     return pNames[0];
    1209             : }
    1210             : 
    1211             : }
    1212             : 
    1213             : void ScDPCache::Dump() const
    1214             : {
    1215             :     // Change these flags to fit your debugging needs.
    1216             :     bool bDumpItems = false;
    1217             :     bool bDumpSourceData = false;
    1218             : 
    1219             :     cout << "--- pivot cache dump" << endl;
    1220             :     {
    1221             :         FieldsType::const_iterator it = maFields.begin(), itEnd = maFields.end();
    1222             :         for (size_t i = 0; it != itEnd; ++it, ++i)
    1223             :         {
    1224             :             const Field& fld = *it;
    1225             :             cout << "* source dimension: " << GetDimensionName(i) << " (ID = " << i << ")" << endl;
    1226             :             cout << "    item count: " << fld.maItems.size() << endl;
    1227             :             if (bDumpItems)
    1228             :                 dumpItems(*this, i, fld.maItems, 0);
    1229             :             if (fld.mpGroup)
    1230             :             {
    1231             :                 cout << "    group item count: " << fld.mpGroup->maItems.size() << endl;
    1232             :                 cout << "    group type: " << getGroupTypeName(fld.mpGroup->mnGroupType) << endl;
    1233             :                 if (bDumpItems)
    1234             :                     dumpItems(*this, i, fld.mpGroup->maItems, fld.maItems.size());
    1235             :             }
    1236             : 
    1237             :             if (bDumpSourceData)
    1238             :             {
    1239             :                 cout << "    source data (re-constructed):" << endl;
    1240             :                 dumpSourceData(*this, i, fld.maItems, fld.maData);
    1241             :             }
    1242             :         }
    1243             :     }
    1244             : 
    1245             :     {
    1246             :         GroupFieldsType::const_iterator it = maGroupFields.begin(), itEnd = maGroupFields.end();
    1247             :         for (size_t i = maFields.size(); it != itEnd; ++it, ++i)
    1248             :         {
    1249             :             const GroupItems& gi = *it;
    1250             :             cout << "* group dimension: (unnamed) (ID = " << i << ")" << endl;
    1251             :             cout << "    item count: " << gi.maItems.size() << endl;
    1252             :             cout << "    group type: " << getGroupTypeName(gi.mnGroupType) << endl;
    1253             :             if (bDumpItems)
    1254             :                 dumpItems(*this, i, gi.maItems, 0);
    1255             :         }
    1256             :     }
    1257             : 
    1258             :     {
    1259             :         struct { SCROW start; SCROW end; bool empty; } aRange;
    1260             :         cout << "* empty rows: " << endl;
    1261             :         mdds::flat_segment_tree<SCROW, bool>::const_iterator it = maEmptyRows.begin(), itEnd = maEmptyRows.end();
    1262             :         if (it != itEnd)
    1263             :         {
    1264             :             aRange.start = it->first;
    1265             :             aRange.empty = it->second;
    1266             :             ++it;
    1267             :         }
    1268             : 
    1269             :         for (; it != itEnd; ++it)
    1270             :         {
    1271             :             aRange.end = it->first-1;
    1272             :             cout << "    rows " << aRange.start << "-" << aRange.end << ": " << (aRange.empty ? "empty" : "not-empty") << endl;
    1273             :             aRange.start = it->first;
    1274             :             aRange.empty = it->second;
    1275             :         }
    1276             :     }
    1277             : 
    1278             :     cout << "---" << endl;
    1279             : }
    1280             : 
    1281             : #endif
    1282             : 
    1283             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10