Skip to content

Commit 2d7f24b

Browse files
committed
and FExpr
1 parent 1911f0a commit 2d7f24b

File tree

4 files changed

+114
-3
lines changed

4 files changed

+114
-3
lines changed

src/core/expr/fbinary/bimaker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static std::unordered_map<size_t, bimaker_ptr> bimakers_library;
4545

4646
bimaker_ptr resolve_op(Op opcode, SType stype1, SType stype2) {
4747
switch (opcode) {
48-
case Op::AND: return resolve_op_and(stype1, stype2);
48+
//case Op::AND: return resolve_op_and(stype1, stype2);
4949
case Op::OR: return resolve_op_or(stype1, stype2);
5050
case Op::XOR: return resolve_op_xor(stype1, stype2);
5151
case Op::LSHIFT: return resolve_op_lshift(stype1, stype2);

src/core/expr/fbinary/bimaker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ using bimaker_ptr = std::unique_ptr<bimaker>;
7777
// Main resolver, calls individual-op resolvers below
7878
bimaker_ptr resolve_op(Op, SType, SType);
7979

80-
bimaker_ptr resolve_op_and(SType, SType);
80+
//bimaker_ptr resolve_op_and(SType, SType);
8181
bimaker_ptr resolve_op_or(SType, SType);
8282
bimaker_ptr resolve_op_xor(SType, SType);
8383
bimaker_ptr resolve_op_lshift(SType, SType);

src/core/expr/fbinary/bitwise_and.cc

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//------------------------------------------------------------------------------
2+
// Copyright 2020 H2O.ai
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a
5+
// copy of this software and associated documentation files (the "Software"),
6+
// to deal in the Software without restriction, including without limitation
7+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
// and/or sell copies of the Software, and to permit persons to whom the
9+
// Software is furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
// IN THE SOFTWARE.
21+
//------------------------------------------------------------------------------
22+
#include "column/func_binary.h"
23+
#include "expr/fbinary/fexpr_binaryop.h"
24+
namespace dt {
25+
namespace expr {
26+
27+
template <typename T>
28+
static bool op_and_bool(ref_t<T> x, bool xvalid, ref_t<T> y, bool yvalid, int8_t* out)
29+
{
30+
if (x == 0 && xvalid) { // short-circuit
31+
*out = 0;
32+
return true;
33+
}
34+
if (!yvalid) return false;
35+
if (y == 0) {
36+
*out = 0;
37+
return true;
38+
}
39+
*out = 1;
40+
return xvalid;
41+
}
42+
43+
template <typename T>
44+
inline static T op_and(T x, T y) {
45+
return (x & y);
46+
}
47+
48+
class FExpr__and__ : public FExpr_BinaryOp {
49+
public:
50+
using FExpr_BinaryOp::FExpr_BinaryOp;
51+
using FExpr_BinaryOp::lhs_;
52+
using FExpr_BinaryOp::rhs_;
53+
54+
55+
std::string name() const override { return "&"; }
56+
int precedence() const noexcept override { return 4; }
57+
58+
59+
Column evaluate1(Column&& lcol, Column&& rcol) const override {
60+
xassert(lcol.nrows() == rcol.nrows());
61+
size_t nrows = lcol.nrows();
62+
auto stype1 = lcol.stype();
63+
auto stype2 = rcol.stype();
64+
auto stype0 = common_stype(stype1, stype2);
65+
66+
if (stype1 == SType::VOID || stype2 == SType::VOID) {
67+
return Column::new_na_column(lcol.nrows(), SType::VOID);
68+
}
69+
if (stype1 == SType::BOOL && stype2 == SType::BOOL) {
70+
return Column(new FuncBinary2_ColumnImpl<int8_t, int8_t, int8_t>(
71+
std::move(lcol), std::move(rcol),
72+
op_and_bool<int8_t>,
73+
nrows, SType::BOOL
74+
));
75+
}
76+
switch (stype0) {
77+
case SType::INT8: return make<int8_t>(std::move(lcol), std::move(rcol), stype0);
78+
case SType::INT16: return make<int16_t>(std::move(lcol), std::move(rcol), stype0);
79+
case SType::INT32: return make<int32_t>(std::move(lcol), std::move(rcol), stype0);
80+
case SType::INT64: return make<int64_t>(std::move(lcol), std::move(rcol), stype0);
81+
default:
82+
throw TypeError() << "Operator `&` cannot be applied to columns of "
83+
"types `" << stype1 << "` and `" << stype2 << "`";
84+
}
85+
}
86+
87+
private:
88+
template <typename T>
89+
static Column make(Column&& a, Column&& b, SType stype) {
90+
xassert(compatible_type<T>(stype));
91+
size_t nrows = a.nrows();
92+
a.cast_inplace(stype);
93+
b.cast_inplace(stype);
94+
return Column(new FuncBinary1_ColumnImpl<T, T, T>(
95+
std::move(a), std::move(b),
96+
op_and<T>,
97+
nrows, stype
98+
));
99+
}
100+
};
101+
102+
103+
104+
py::oobj PyFExpr::nb__and__(py::robj lhs, py::robj rhs) {
105+
return PyFExpr::make(
106+
new FExpr__and__(as_fexpr(lhs), as_fexpr(rhs)));
107+
}
108+
109+
110+
111+
}} // namespace dt::expr

src/core/expr/fexpr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static oobj make_binexpr(dt::expr::Op op, robj lhs, robj rhs) {
185185
}
186186

187187

188-
oobj PyFExpr::nb__and__(robj lhs, robj rhs) { return make_binexpr(dt::expr::Op::AND, lhs, rhs); }
188+
//oobj PyFExpr::nb__and__(robj lhs, robj rhs) { return make_binexpr(dt::expr::Op::AND, lhs, rhs); }
189189
oobj PyFExpr::nb__xor__(robj lhs, robj rhs) { return make_binexpr(dt::expr::Op::XOR, lhs, rhs); }
190190
oobj PyFExpr::nb__or__(robj lhs, robj rhs) { return make_binexpr(dt::expr::Op::OR, lhs, rhs); }
191191
oobj PyFExpr::nb__lshift__(robj lhs, robj rhs) { return make_binexpr(dt::expr::Op::LSHIFT, lhs, rhs); }

0 commit comments

Comments
 (0)