2323#include " column/latent.h"
2424#include " column/minmax.h"
2525#include " documentation.h"
26- #include " expr/fexpr_func .h"
26+ #include " expr/fexpr_reduce_unary .h"
2727#include " expr/eval_context.h"
2828#include " expr/workframe.h"
2929#include " python/xargs.h"
@@ -33,75 +33,38 @@ namespace expr {
3333
3434
3535template <bool MIN>
36- class FExpr_MinMax : public FExpr_Func {
37- private:
38- ptrExpr arg_;
39-
36+ class FExpr_MinMax : public FExpr_ReduceUnary {
4037 public:
41- FExpr_MinMax (ptrExpr &&arg)
42- : arg_(std::move(arg)) {}
43-
44- std::string repr () const override {
45- std::string out = MIN? " min" : " max" ;
46- out += ' (' ;
47- out += arg_->repr ();
48- out += ' )' ;
49- return out;
50- }
51-
38+ using FExpr_ReduceUnary::FExpr_ReduceUnary;
5239
53- Workframe evaluate_n (EvalContext &ctx) const override {
54- Workframe outputs (ctx);
55- Workframe wf = arg_->evaluate_n (ctx);
56- Groupby gby = ctx.get_groupby ();
5740
58- if (!gby) {
59- gby = Groupby::single_group (wf.nrows ());
60- }
61-
62- if (wf.nrows () == 0 ) {
63- for (size_t i = 0 ; i < wf.ncols (); ++i) {
64- Column coli = wf.retrieve_column (i);
65- coli = Column (new ConstNa_ColumnImpl (1 , coli.stype ()));
66- outputs.add_column (std::move (coli), wf.retrieve_name (i), Grouping::GtoONE);
67- }
68- } else {
69- for (size_t i = 0 ; i < wf.ncols (); ++i) {
70- bool is_grouped = ctx.has_group_column (
71- wf.get_frame_id (i),
72- wf.get_column_id (i)
73- );
74- Column coli = evaluate1 (wf.retrieve_column (i), gby, is_grouped);
75- outputs.add_column (std::move (coli), wf.retrieve_name (i), Grouping::GtoONE);
76- }
77- }
78- return outputs;
41+ std::string name () const override {
42+ return MIN? " min"
43+ : " max" ;
7944 }
8045
8146
82- Column evaluate1 (Column && col, const Groupby& gby, bool is_grouped) const {
47+ Column evaluate1 (Column&& col, const Groupby& gby, bool is_grouped) const override {
8348 SType stype = col.stype ();
8449
8550 switch (stype) {
8651 case SType::VOID:
8752 return Column (new ConstNa_ColumnImpl (gby.size (), stype));
8853 case SType::BOOL:
8954 case SType::INT8:
90- return make<int8_t >(std::move (col), SType::INT8, gby, is_grouped);
55+ return make<int8_t >(std::move (col), gby, is_grouped);
9156 case SType::INT16:
92- return make<int16_t >(std::move (col), SType::INT16, gby, is_grouped);
93- case SType::DATE32:
94- return make<int32_t >(std::move (col), SType::DATE32, gby, is_grouped);
57+ return make<int16_t >(std::move (col), gby, is_grouped);
9558 case SType::INT32:
96- return make<int32_t >(std::move (col), SType::INT32, gby, is_grouped);
97- case SType::TIME64:
98- return make<int64_t >(std::move (col), SType::TIME64, gby, is_grouped);
59+ case SType::DATE32:
60+ return make<int32_t >(std::move (col), gby, is_grouped);
9961 case SType::INT64:
100- return make<int64_t >(std::move (col), SType::INT64, gby, is_grouped);
62+ case SType::TIME64:
63+ return make<int64_t >(std::move (col), gby, is_grouped);
10164 case SType::FLOAT32:
102- return make<float >(std::move (col), SType::FLOAT32, gby, is_grouped);
65+ return make<float >(std::move (col), gby, is_grouped);
10366 case SType::FLOAT64:
104- return make<double >(std::move (col), SType::FLOAT64, gby, is_grouped);
67+ return make<double >(std::move (col), gby, is_grouped);
10568 default :
10669 throw TypeError ()
10770 << " Invalid column of type `" << stype << " ` in " << repr ();
@@ -110,17 +73,12 @@ class FExpr_MinMax : public FExpr_Func {
11073
11174
11275 template <typename T>
113- Column make (Column &&col, SType stype, const Groupby& gby, bool is_grouped) const {
114- col.cast_inplace (stype);
115- if (is_grouped) {
116- return Column (new Latent_ColumnImpl (new MinMax_ColumnImpl<T, MIN, true >(
117- std::move (col), gby
118- )));
119- } else {
120- return Column (new Latent_ColumnImpl (new MinMax_ColumnImpl<T, MIN, false >(
121- std::move (col), gby
122- )));
123- }
76+ Column make (Column&& col, const Groupby& gby, bool is_grouped) const {
77+ return is_grouped? std::move (col)
78+ : Column (new Latent_ColumnImpl (new MinMax_ColumnImpl<T, MIN>(
79+ std::move (col), gby
80+ )));
81+
12482 }
12583};
12684
0 commit comments