From 4b36fcf96e344f6ba92d4e8648a73ab1e6ac7250 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 29 Mar 2013 17:56:27 +0000 Subject: More aggregates Make count count not null values --- project2/basics/aggregates/avg.cpp | 30 ++++++++++++++++++++++++++++++ project2/basics/aggregates/count.cpp | 6 ++++-- project2/basics/aggregates/join.cpp | 35 +++++++++++++++++++++++++++++++++++ project2/basics/aggregates/sum.cpp | 27 +++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 project2/basics/aggregates/avg.cpp create mode 100644 project2/basics/aggregates/join.cpp create mode 100644 project2/basics/aggregates/sum.cpp diff --git a/project2/basics/aggregates/avg.cpp b/project2/basics/aggregates/avg.cpp new file mode 100644 index 0000000..62239b3 --- /dev/null +++ b/project2/basics/aggregates/avg.cpp @@ -0,0 +1,30 @@ +#include +#include +#include + +class Average : public ValueAggregate { + public: + Average(ScriptNodePtr s) : + ValueAggregate(s) + { + } + void reset() const + { + vals.clear(); + } + void pushValue(const VariableType & v) const + { + if (!v.isNull()) { + vals.push_back(v); + } + } + VariableType resultValue() const + { + return std::accumulate(vals.begin(), vals.end(), 0.0) / (double)vals.size(); + } + private: + mutable std::list vals; +}; + +DECLARE_LOADER("average", Average); + diff --git a/project2/basics/aggregates/count.cpp b/project2/basics/aggregates/count.cpp index fb3b899..725be72 100644 --- a/project2/basics/aggregates/count.cpp +++ b/project2/basics/aggregates/count.cpp @@ -11,9 +11,11 @@ class Count : public ValueAggregate { { c = 0; } - void pushValue(const VariableType &) const + void pushValue(const VariableType & v) const { - c += 1; + if (!v.isNull()) { + c += 1; + } } VariableType resultValue() const { diff --git a/project2/basics/aggregates/join.cpp b/project2/basics/aggregates/join.cpp new file mode 100644 index 0000000..841eb9a --- /dev/null +++ b/project2/basics/aggregates/join.cpp @@ -0,0 +1,35 @@ +#include + +class Join : public ValueAggregate { + public: + Join(ScriptNodePtr s) : + ValueAggregate(s), + first(true), + sep(s, "separator", ",") + { + } + void reset() const + { + sum.clear(); + first = true; + } + void pushValue(const VariableType & v) const + { + if (!first) { + sum += sep().as(); + } + sum += v.as(); + first = false; + } + VariableType resultValue() const + { + return sum; + } + private: + mutable Glib::ustring sum; + mutable bool first; + Variable sep; +}; + +DECLARE_LOADER("join", Join); + diff --git a/project2/basics/aggregates/sum.cpp b/project2/basics/aggregates/sum.cpp new file mode 100644 index 0000000..9bc1120 --- /dev/null +++ b/project2/basics/aggregates/sum.cpp @@ -0,0 +1,27 @@ +#include + +class Sum : public ValueAggregate { + public: + Sum(ScriptNodePtr s) : + ValueAggregate(s), + sum(0) + { + } + void reset() const + { + sum = 0; + } + void pushValue(const VariableType & v) const + { + sum += v.as(); + } + VariableType resultValue() const + { + return sum; + } + private: + mutable double sum; +}; + +DECLARE_LOADER("sum", Sum); + -- cgit v1.2.3