diff options
-rw-r--r-- | project2/basics/aggregates/avg.cpp | 30 | ||||
-rw-r--r-- | project2/basics/aggregates/count.cpp | 6 | ||||
-rw-r--r-- | project2/basics/aggregates/join.cpp | 35 | ||||
-rw-r--r-- | project2/basics/aggregates/sum.cpp | 27 |
4 files changed, 96 insertions, 2 deletions
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 <aggregate.h> +#include <list> +#include <numeric> + +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<double> 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 <aggregate.h> + +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<Glib::ustring>(); + } + sum += v.as<Glib::ustring>(); + 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 <aggregate.h> + +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<double>(); + } + VariableType resultValue() const + { + return sum; + } + private: + mutable double sum; +}; + +DECLARE_LOADER("sum", Sum); + |