-
Notifications
You must be signed in to change notification settings - Fork 154
/
fexpr_cumminmax.cc
127 lines (104 loc) · 3.97 KB
/
fexpr_cumminmax.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//------------------------------------------------------------------------------
// Copyright 2022 H2O.ai
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//------------------------------------------------------------------------------
#include "column/const.h"
#include "column/cumminmax.h"
#include "column/latent.h"
#include "documentation.h"
#include "expr/fexpr_func.h"
#include "expr/eval_context.h"
#include "expr/workframe.h"
#include "python/xargs.h"
#include "stype.h"
namespace dt {
namespace expr {
template <bool MIN>
class FExpr_CumMinMax : public FExpr_Func {
private:
ptrExpr arg_;
public:
FExpr_CumMinMax(ptrExpr&& arg)
: arg_(std::move(arg)) {}
std::string repr() const override {
std::string out = MIN? "cummin" : "cummax";
out += '(';
out += arg_->repr();
out += ')';
return out;
}
Workframe evaluate_n(EvalContext& ctx) const override {
Workframe wf = arg_->evaluate_n(ctx);
Groupby gby = Groupby::single_group(wf.nrows());
if (ctx.has_groupby()) {
wf.increase_grouping_mode(Grouping::GtoALL);
gby = ctx.get_groupby();
}
for (size_t i = 0; i < wf.ncols(); ++i) {
Column coli = evaluate1(wf.retrieve_column(i), gby);
wf.replace_column(i, std::move(coli));
}
return wf;
}
Column evaluate1(Column&& col, const Groupby& gby) const {
SType stype = col.stype();
switch (stype) {
case SType::VOID: return Column(new ConstNa_ColumnImpl(col.nrows()));
case SType::BOOL:
case SType::INT8: return make<int8_t>(std::move(col), gby);
case SType::INT16: return make<int16_t>(std::move(col), gby);
case SType::DATE32:
case SType::INT32: return make<int32_t>(std::move(col), gby);
case SType::TIME64:
case SType::INT64: return make<int64_t>(std::move(col), gby);
case SType::FLOAT32: return make<float>(std::move(col), gby);
case SType::FLOAT64: return make<double>(std::move(col), gby);
default: throw TypeError()
<< "Invalid column of type `" << stype << "` in " << repr();
}
}
template <typename T>
Column make(Column&& col, const Groupby& gby) const {
return Column(new Latent_ColumnImpl(
new CumMinMax_ColumnImpl<T, MIN>(std::move(col), gby)
));
}
};
static py::oobj pyfn_cummax(const py::XArgs& args) {
auto cummax = args[0].to_oobj();
return PyFExpr::make(new FExpr_CumMinMax<false>(as_fexpr(cummax)));
}
static py::oobj pyfn_cummin(const py::XArgs& args) {
auto cummin = args[0].to_oobj();
return PyFExpr::make(new FExpr_CumMinMax<true>(as_fexpr(cummin)));
}
DECLARE_PYFN(&pyfn_cummax)
->name("cummax")
->docs(doc_dt_cummax)
->arg_names({"cummax"})
->n_positional_args(1)
->n_required_args(1);
DECLARE_PYFN(&pyfn_cummin)
->name("cummin")
->docs(doc_dt_cummin)
->arg_names({"cummin"})
->n_positional_args(1)
->n_required_args(1);
}} // dt::expr