diff options
author | Kohei Yoshida <kohei.yoshida@collabora.com> | 2013-10-19 21:07:26 -0400 |
---|---|---|
committer | Kohei Yoshida <kohei.yoshida@collabora.com> | 2013-10-20 13:44:42 -0400 |
commit | 73353186d58ae187f3f1ce4f206795424fb3336c (patch) | |
tree | 0ead8d1f7ac4ba520212496e7b619f0425bd104b | |
parent | 7b7a73d05be7e5409013dde7bd82909f2a5e3efe (diff) |
Better way to organize 2 compared cell values.
Turns out this is slightly faster too.
Change-Id: I5a8c3474ab2a342200a5cfa9a93e6d89433595c4
-rw-r--r-- | sc/inc/compare.hxx | 26 | ||||
-rw-r--r-- | sc/source/core/tool/compare.cxx | 86 | ||||
-rw-r--r-- | sc/source/core/tool/interpr1.cxx | 68 | ||||
-rw-r--r-- | sc/source/core/tool/scmatrix.cxx | 26 |
4 files changed, 115 insertions, 91 deletions
diff --git a/sc/inc/compare.hxx b/sc/inc/compare.hxx index 0bbf8dcc45fd..3189bc81d05a 100644 --- a/sc/inc/compare.hxx +++ b/sc/inc/compare.hxx @@ -30,23 +30,23 @@ namespace sc { struct Compare { - double nVal[2]; - OUString* pVal[2]; - bool bVal[2]; - bool bEmpty[2]; + struct Cell + { + double mfValue; + OUString* mpStr; + bool mbValue; + bool mbEmpty; + + Cell(); + Cell( OUString* p ); + }; + + Cell maCells[2]; ScQueryOp meOp; bool mbIgnoreCase; - Compare( OUString* p1, OUString* p2 ) : - meOp(SC_EQUAL), - mbIgnoreCase(true) - { - pVal[0] = p1; - pVal[1] = p2; - bEmpty[0] = false; - bEmpty[1] = false; - } + Compare( OUString* p1, OUString* p2 ); }; struct CompareOptions diff --git a/sc/source/core/tool/compare.cxx b/sc/source/core/tool/compare.cxx index bf50093e08a6..5782a6b965de 100644 --- a/sc/source/core/tool/compare.cxx +++ b/sc/source/core/tool/compare.cxx @@ -26,6 +26,19 @@ namespace sc { +Compare::Cell::Cell() : + mfValue(0.0), mpStr(NULL), mbValue(false), mbEmpty(false) {} + +Compare::Cell::Cell( OUString* p ) : + mfValue(0.0), mpStr(p), mbValue(false), mbEmpty(false) {} + +Compare::Compare( OUString* p1, OUString* p2 ) : + meOp(SC_EQUAL), mbIgnoreCase(true) +{ + maCells[0] = Cell(p1); + maCells[1] = Cell(p2); +} + CompareOptions::CompareOptions( ScDocument* pDoc, const ScQueryEntry& rEntry, bool bReg ) : aQueryEntry(rEntry), bRegEx(bReg), @@ -40,24 +53,27 @@ CompareOptions::CompareOptions( ScDocument* pDoc, const ScQueryEntry& rEntry, bo double CompareFunc( const Compare& rComp, CompareOptions* pOptions ) { + const Compare::Cell& rCell1 = rComp.maCells[0]; + const Compare::Cell& rCell2 = rComp.maCells[1]; + // Keep DoubleError if encountered // #i40539# if bEmpty is set, bVal/nVal are uninitialized - if ( !rComp.bEmpty[0] && rComp.bVal[0] && !::rtl::math::isFinite( rComp.nVal[0])) - return rComp.nVal[0]; - if ( !rComp.bEmpty[1] && rComp.bVal[1] && !::rtl::math::isFinite( rComp.nVal[1])) - return rComp.nVal[1]; + if (!rCell1.mbEmpty && rCell1.mbValue && !rtl::math::isFinite(rCell1.mfValue)) + return rCell1.mfValue; + if (!rCell2.mbEmpty && rCell2.mbValue && !rtl::math::isFinite(rCell2.mfValue)) + return rCell2.mfValue; size_t nStringQuery = 0; // 0:=no, 1:=0, 2:=1 double fRes = 0; - if ( rComp.bEmpty[ 0 ] ) + if (rCell1.mbEmpty) { - if ( rComp.bEmpty[ 1 ] ) + if (rCell2.mbEmpty) ; // empty cell == empty cell, fRes 0 - else if( rComp.bVal[ 1 ] ) + else if (rCell2.mbValue) { - if (rComp.nVal[1] != 0.0) + if (rCell2.mfValue != 0.0) { - if ( rComp.nVal[ 1 ] < 0.0 ) + if (rCell2.mfValue < 0.0) fRes = 1; // empty cell > -x else fRes = -1; // empty cell < x @@ -66,18 +82,18 @@ double CompareFunc( const Compare& rComp, CompareOptions* pOptions ) } else { - if ( !rComp.pVal[ 1 ]->isEmpty() ) + if (!rCell2.mpStr->isEmpty()) fRes = -1; // empty cell < "..." // else: empty cell == "" } } - else if ( rComp.bEmpty[ 1 ] ) + else if (rCell2.mbEmpty) { - if( rComp.bVal[ 0 ] ) + if (rCell1.mbValue) { - if (rComp.nVal[0] != 0.0) + if (rCell1.mfValue != 0.0) { - if ( rComp.nVal[ 0 ] < 0.0 ) + if (rCell1.mfValue < 0.0) fRes = -1; // -x < empty cell else fRes = 1; // x > empty cell @@ -86,18 +102,18 @@ double CompareFunc( const Compare& rComp, CompareOptions* pOptions ) } else { - if ( !rComp.pVal[ 0 ]->isEmpty() ) + if (!rCell1.mpStr->isEmpty()) fRes = 1; // "..." > empty cell // else: "" == empty cell } } - else if( rComp.bVal[ 0 ] ) + else if (rCell1.mbValue) { - if( rComp.bVal[ 1 ] ) + if (rCell2.mbValue) { - if ( !::rtl::math::approxEqual( rComp.nVal[ 0 ], rComp.nVal[ 1 ] ) ) + if (!rtl::math::approxEqual(rCell1.mfValue, rCell2.mfValue)) { - if( rComp.nVal[ 0 ] - rComp.nVal[ 1 ] < 0 ) + if (rCell1.mfValue - rCell2.mfValue < 0) fRes = -1; else fRes = 1; @@ -109,7 +125,7 @@ double CompareFunc( const Compare& rComp, CompareOptions* pOptions ) nStringQuery = 2; // 1+1 } } - else if( rComp.bVal[ 1 ] ) + else if (rCell2.mbValue) { fRes = 1; // string is greater than number nStringQuery = 1; // 0+1 @@ -123,15 +139,15 @@ double CompareFunc( const Compare& rComp, CompareOptions* pOptions ) // is/must be identical to *rEntry.pStr, which is essential for // regex to work through GetSearchTextPtr(). ScQueryEntry& rEntry = pOptions->aQueryEntry; - OSL_ENSURE(rEntry.GetQueryItem().maString.getString().equals(*rComp.pVal[1]), "ScInterpreter::CompareFunc: broken options"); + OSL_ENSURE(rEntry.GetQueryItem().maString.getString().equals(*rCell2.mpStr), "ScInterpreter::CompareFunc: broken options"); if (pOptions->bRegEx) { sal_Int32 nStart = 0; - sal_Int32 nStop = rComp.pVal[0]->getLength(); + sal_Int32 nStop = rCell1.mpStr->getLength(); bool bMatch = rEntry.GetSearchTextPtr( - !pOptions->bIgnoreCase)->SearchForward( *rComp.pVal[0], - &nStart, &nStop); - if (bMatch && pOptions->bMatchWholeCell && (nStart != 0 || nStop != rComp.pVal[0]->getLength())) + !pOptions->bIgnoreCase)->SearchForward( + *rCell1.mpStr, &nStart, &nStop); + if (bMatch && pOptions->bMatchWholeCell && (nStart != 0 || nStop != rCell1.mpStr->getLength())) bMatch = false; // RegEx must match entire string. fRes = (bMatch ? 0 : 1); } @@ -142,32 +158,32 @@ double CompareFunc( const Compare& rComp, CompareOptions* pOptions ) ScGlobal::GetCaseTransliteration()); bool bMatch; if (pOptions->bMatchWholeCell) - bMatch = pTransliteration->isEqual( *rComp.pVal[0], *rComp.pVal[1]); + bMatch = pTransliteration->isEqual(*rCell1.mpStr, *rCell2.mpStr); else { OUString aCell( pTransliteration->transliterate( - *rComp.pVal[0], ScGlobal::eLnge, 0, - rComp.pVal[0]->getLength(), NULL)); + *rCell1.mpStr, ScGlobal::eLnge, 0, + rCell1.mpStr->getLength(), NULL)); OUString aQuer( pTransliteration->transliterate( - *rComp.pVal[1], ScGlobal::eLnge, 0, - rComp.pVal[1]->getLength(), NULL)); + *rCell2.mpStr, ScGlobal::eLnge, 0, + rCell2.mpStr->getLength(), NULL)); bMatch = (aCell.indexOf( aQuer ) != -1); } fRes = (bMatch ? 0 : 1); } else if (pOptions->bIgnoreCase) fRes = (double) ScGlobal::GetCollator()->compareString( - *rComp.pVal[ 0 ], *rComp.pVal[ 1 ] ); + *rCell1.mpStr, *rCell2.mpStr); else fRes = (double) ScGlobal::GetCaseCollator()->compareString( - *rComp.pVal[ 0 ], *rComp.pVal[ 1 ] ); + *rCell1.mpStr, *rCell2.mpStr); } else if (rComp.mbIgnoreCase) fRes = (double) ScGlobal::GetCollator()->compareString( - *rComp.pVal[ 0 ], *rComp.pVal[ 1 ] ); + *rCell1.mpStr, *rCell2.mpStr); else fRes = (double) ScGlobal::GetCaseCollator()->compareString( - *rComp.pVal[ 0 ], *rComp.pVal[ 1 ] ); + *rCell1.mpStr, *rCell2.mpStr); } if (nStringQuery && pOptions) @@ -183,7 +199,7 @@ double CompareFunc( const Compare& rComp, CompareOptions* pOptions ) // As in ScTable::ValidQuery() match a numeric string for a // number query that originated from a string, e.g. in SUMIF // and COUNTIF. Transliteration is not needed here. - bool bEqual = (*rComp.pVal[nStringQuery-1]) == rItem.maString.getString(); + bool bEqual = (*rComp.maCells[nStringQuery-1].mpStr) == rItem.maString.getString(); // match => fRes=0, else fRes=1 fRes = (rEntry.eOp == SC_NOT_EQUAL) ? bEqual : !bEqual; } diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx index 2cea9a2623bc..0de212f23fd9 100644 --- a/sc/source/core/tool/interpr1.cxx +++ b/sc/source/core/tool/interpr1.cxx @@ -796,20 +796,22 @@ double ScInterpreter::Compare() aComp.mbIgnoreCase = pDok->GetDocOptions().IsIgnoreCase(); for( short i = 1; i >= 0; i-- ) { + sc::Compare::Cell& rCell = aComp.maCells[i]; + switch ( GetRawStackType() ) { case svEmptyCell: Pop(); - aComp.bEmpty[ i ] = true; + rCell.mbEmpty = true; break; case svMissing: case svDouble: - aComp.nVal[ i ] = GetDouble(); - aComp.bVal[ i ] = true; + rCell.mfValue = GetDouble(); + rCell.mbValue = true; break; case svString: - *aComp.pVal[ i ] = GetString().getString(); - aComp.bVal[ i ] = false; + *rCell.mpStr = GetString().getString(); + rCell.mbValue = false; break; case svDoubleRef : case svSingleRef : @@ -820,18 +822,18 @@ double ScInterpreter::Compare() ScRefCellValue aCell; aCell.assign(*pDok, aAdr); if (aCell.hasEmptyValue()) - aComp.bEmpty[i] = true; + rCell.mbEmpty = true; else if (aCell.hasString()) { svl::SharedString aStr; GetCellString(aStr, aCell); - *aComp.pVal[i] = aStr.getString(); - aComp.bVal[i] = false; + *rCell.mpStr = aStr.getString(); + rCell.mbValue = false; } else { - aComp.nVal[i] = GetCellValue(aAdr, aCell); - aComp.bVal[i] = true; + rCell.mfValue = GetCellValue(aAdr, aCell); + rCell.mbValue = true; } } break; @@ -852,16 +854,16 @@ double ScInterpreter::Compare() break; } if (pMat->IsEmpty(0, 0)) - aComp.bEmpty[i] = true; + rCell.mbEmpty = true; else if (pMat->IsString(0, 0)) { - *aComp.pVal[i] = pMat->GetString(0, 0).getString(); - aComp.bVal[i] = false; + *rCell.mpStr = pMat->GetString(0, 0).getString(); + rCell.mbValue = false; } else { - aComp.nVal[i] = pMat->GetDouble(0, 0); - aComp.bVal[i] = true; + rCell.mfValue = pMat->GetDouble(0, 0); + rCell.mbValue = true; } } break; @@ -889,20 +891,22 @@ sc::RangeMatrix ScInterpreter::CompareMat( ScQueryOp eOp, sc::CompareOptions* pO ScAddress aAdr; for( short i = 1; i >= 0; i-- ) { + sc::Compare::Cell& rCell = aComp.maCells[i]; + switch (GetRawStackType()) { case svEmptyCell: Pop(); - aComp.bEmpty[ i ] = true; + rCell.mbEmpty = true; break; case svMissing: case svDouble: - aComp.nVal[ i ] = GetDouble(); - aComp.bVal[ i ] = true; + rCell.mfValue = GetDouble(); + rCell.mbValue = true; break; case svString: - *aComp.pVal[ i ] = GetString().getString(); - aComp.bVal[ i ] = false; + *rCell.mpStr = GetString().getString(); + rCell.mbValue = false; break; case svSingleRef: { @@ -910,18 +914,18 @@ sc::RangeMatrix ScInterpreter::CompareMat( ScQueryOp eOp, sc::CompareOptions* pO ScRefCellValue aCell; aCell.assign(*pDok, aAdr); if (aCell.hasEmptyValue()) - aComp.bEmpty[i] = true; + rCell.mbEmpty = true; else if (aCell.hasString()) { svl::SharedString aStr; GetCellString(aStr, aCell); - *aComp.pVal[i] = aStr.getString(); - aComp.bVal[i] = false; + *rCell.mpStr = aStr.getString(); + rCell.mbValue = false; } else { - aComp.nVal[i] = GetCellValue(aAdr, aCell); - aComp.bVal[i] = true; + rCell.mfValue = GetCellValue(aAdr, aCell); + rCell.mbValue = true; } } break; @@ -969,17 +973,19 @@ sc::RangeMatrix ScInterpreter::CompareMat( ScQueryOp eOp, sc::CompareOptions* pO { for ( short i=1; i>=0; i-- ) { + sc::Compare::Cell& rCell = aComp.maCells[i]; + if (aMat[i].mpMat->IsString(j, k)) { - aComp.bVal[i] = false; - *aComp.pVal[i] = aMat[i].mpMat->GetString(j, k).getString(); - aComp.bEmpty[i] = aMat[i].mpMat->IsEmpty(j, k); + rCell.mbValue = false; + *rCell.mpStr = aMat[i].mpMat->GetString(j, k).getString(); + rCell.mbEmpty = aMat[i].mpMat->IsEmpty(j, k); } else { - aComp.bVal[i] = true; - aComp.nVal[i] = aMat[i].mpMat->GetDouble(j, k); - aComp.bEmpty[i] = false; + rCell.mbValue = true; + rCell.mfValue = aMat[i].mpMat->GetDouble(j, k); + rCell.mbEmpty = false; } } aRes.mpMat->PutDouble(sc::CompareFunc(aComp, pOptions), j, k); diff --git a/sc/source/core/tool/scmatrix.cxx b/sc/source/core/tool/scmatrix.cxx index 3fbdc7f71506..3f17535f7e56 100644 --- a/sc/source/core/tool/scmatrix.cxx +++ b/sc/source/core/tool/scmatrix.cxx @@ -1264,6 +1264,8 @@ public: void operator() (const MatrixImplType::element_block_node_type& node) { + sc::Compare::Cell& rCell = mrComp.maCells[mnMatPos]; + switch (node.type) { case mdds::mtm::element_numeric: @@ -1274,9 +1276,9 @@ public: block_type::const_iterator itEnd = block_type::end(*node.data); for (; it != itEnd; ++it) { - mrComp.bVal[mnMatPos] = true; - mrComp.nVal[mnMatPos] = *it; - mrComp.bEmpty[mnMatPos] = false; + rCell.mbValue = true; + rCell.mbEmpty = false; + rCell.mfValue = *it; compare(); } } @@ -1289,9 +1291,9 @@ public: block_type::const_iterator itEnd = block_type::end(*node.data); for (; it != itEnd; ++it) { - mrComp.bVal[mnMatPos] = true; - mrComp.nVal[mnMatPos] = *it; - mrComp.bEmpty[mnMatPos] = false; + rCell.mbValue = true; + rCell.mbEmpty = false; + rCell.mfValue = *it; compare(); } } @@ -1305,18 +1307,18 @@ public: for (; it != itEnd; ++it) { const svl::SharedString& rStr = *it; - mrComp.bVal[mnMatPos] = false; - *mrComp.pVal[mnMatPos] = rStr.getString(); - mrComp.bEmpty[mnMatPos] = false; + rCell.mbValue = false; + rCell.mbEmpty = false; + *rCell.mpStr = rStr.getString(); compare(); } } break; case mdds::mtm::element_empty: { - mrComp.bVal[mnMatPos] = false; - *mrComp.pVal[mnMatPos] = svl::SharedString::getEmptyString().getString(); - mrComp.bEmpty[mnMatPos] = true; + rCell.mbValue = false; + rCell.mbEmpty = true; + *rCell.mpStr = svl::SharedString::getEmptyString().getString(); for (size_t i = 0; i < node.size; ++i) compare(); } |