summaryrefslogtreecommitdiff
path: root/sc
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2012-07-17 11:54:35 -0400
committerKohei Yoshida <kohei.yoshida@gmail.com>2012-07-19 14:05:09 -0400
commit0ddcb7a07a42294200b86dac7e67a4c82e03fcf4 (patch)
treecf68a3543f755cbc641798709614969f9d33000e /sc
parentb4c774a51fab40569abf6298789f6dedfbebdd12 (diff)
Implement Sum(), SumSquare(), etc...
The unit test still segfaults. Change-Id: Ib1fc78f94776e04ba8cb6ec8a24162b308cc569f
Diffstat (limited to 'sc')
-rw-r--r--sc/source/core/tool/scmatrix.cxx134
1 files changed, 130 insertions, 4 deletions
diff --git a/sc/source/core/tool/scmatrix.cxx b/sc/source/core/tool/scmatrix.cxx
index ee6e658577a7..4146d26f6d2e 100644
--- a/sc/source/core/tool/scmatrix.cxx
+++ b/sc/source/core/tool/scmatrix.cxx
@@ -975,24 +975,150 @@ double ScMatrixImpl::Or() const
return EvalMatrix<OrEvaluator>(maMat);
}
+namespace {
+
+struct SumOp
+{
+ void operator() (double& rAccum, double fVal)
+ {
+ rAccum += fVal;
+ }
+};
+
+struct SumSquareOp
+{
+ void operator() (double& rAccum, double fVal)
+ {
+ rAccum += fVal*fVal;
+ }
+};
+
+struct ProductOp
+{
+ void operator() (double& rAccum, double fVal)
+ {
+ rAccum *= fVal;
+ }
+};
+
+template<typename _Op>
+class WalkElementBlocks : std::unary_function<MatrixImplType::element_block_node_type, void>
+{
+ _Op maOp;
+
+ ScMatrix::IterateResult maRes;
+ bool mbFirst:1;
+ bool mbTextAsZero:1;
+public:
+ WalkElementBlocks(bool bTextAsZero) : maRes(0.0, 0.0, 0), mbFirst(true), mbTextAsZero(bTextAsZero) {}
+
+ const ScMatrix::IterateResult& getResult() const { return maRes; }
+
+ void operator() (const MatrixImplType::element_block_node_type& node)
+ {
+ switch (node.type)
+ {
+ case mdds::mtm::element_numeric:
+ {
+ mdds::mtv::numeric_element_block::const_iterator it = mdds::mtv::numeric_element_block::begin(*node.data);
+ mdds::mtv::numeric_element_block::const_iterator itEnd = mdds::mtv::numeric_element_block::end(*node.data);
+ for (; it != itEnd; ++it)
+ {
+ if (mbFirst)
+ {
+ maOp(maRes.mfFirst, *it);
+ mbFirst = false;
+ }
+ else
+ maOp(maRes.mfRest, *it);
+ }
+ maRes.mnCount += node.size;
+ }
+ break;
+ case mdds::mtm::element_boolean:
+ {
+ mdds::mtv::boolean_element_block::const_iterator it = mdds::mtv::boolean_element_block::begin(*node.data);
+ mdds::mtv::boolean_element_block::const_iterator itEnd = mdds::mtv::boolean_element_block::end(*node.data);
+ for (; it != itEnd; ++it)
+ {
+ if (mbFirst)
+ {
+ maOp(maRes.mfFirst, *it);
+ mbFirst = false;
+ }
+ else
+ maOp(maRes.mfRest, *it);
+ }
+ maRes.mnCount += node.size;
+ }
+ break;
+ case mdds::mtm::element_string:
+ if (mbTextAsZero)
+ maRes.mnCount += node.size;
+ break;
+ case mdds::mtm::element_empty:
+ default:
+ ;
+ }
+ }
+};
+
+class CountElements : std::unary_function<MatrixImplType::element_block_node_type, void>
+{
+ size_t mnCount;
+ bool mbCountString;
+public:
+ CountElements(bool bCountString) : mnCount(0), mbCountString(bCountString) {}
+
+ size_t getCount() const { return mnCount; }
+
+ void operator() (const MatrixImplType::element_block_node_type& node)
+ {
+ switch (node.type)
+ {
+ case mdds::mtm::element_numeric:
+ case mdds::mtm::element_boolean:
+ mnCount += node.size;
+ break;
+ case mdds::mtm::element_string:
+ if (mbCountString)
+ mnCount += node.size;
+ break;
+ case mdds::mtm::element_empty:
+ default:
+ ;
+ }
+ }
+};
+
+}
+
ScMatrix::IterateResult ScMatrixImpl::Sum(bool bTextAsZero) const
{
- return ScMatrix::IterateResult(0, 0, 0);
+ WalkElementBlocks<SumOp> aFunc(bTextAsZero);
+ maMat.walk(aFunc);
+ return aFunc.getResult();
}
ScMatrix::IterateResult ScMatrixImpl::SumSquare(bool bTextAsZero) const
{
- return ScMatrix::IterateResult(0, 0, 0);
+ WalkElementBlocks<SumSquareOp> aFunc(bTextAsZero);
+ maMat.walk(aFunc);
+ return aFunc.getResult();
}
ScMatrix::IterateResult ScMatrixImpl::Product(bool bTextAsZero) const
{
- return ScMatrix::IterateResult(0, 0, 0);
+ WalkElementBlocks<ProductOp> aFunc(bTextAsZero);
+ maMat.walk(aFunc);
+ return aFunc.getResult();
}
size_t ScMatrixImpl::Count(bool bCountStrings) const
{
- return 0;
+ CountElements aFunc(bCountStrings);
+ maMat.walk(aFunc);
+ return aFunc.getCount();
}
void ScMatrixImpl::CalcPosition(SCSIZE nIndex, SCSIZE& rC, SCSIZE& rR) const