Skip to content

Commit 26a07c0

Browse files
committed
FExpr for or
1 parent 2d7f24b commit 26a07c0

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

src/core/expr/fbinary/bimaker.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static std::unordered_map<size_t, bimaker_ptr> bimakers_library;
4646
bimaker_ptr resolve_op(Op opcode, SType stype1, SType stype2) {
4747
switch (opcode) {
4848
//case Op::AND: return resolve_op_and(stype1, stype2);
49-
case Op::OR: return resolve_op_or(stype1, stype2);
49+
//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);
5252
case Op::RSHIFT: return resolve_op_rshift(stype1, stype2);

src/core/expr/fbinary/bimaker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ using bimaker_ptr = std::unique_ptr<bimaker>;
7878
bimaker_ptr resolve_op(Op, SType, SType);
7979

8080
//bimaker_ptr resolve_op_and(SType, SType);
81-
bimaker_ptr resolve_op_or(SType, SType);
81+
//bimaker_ptr resolve_op_or(SType, SType);
8282
bimaker_ptr resolve_op_xor(SType, SType);
8383
bimaker_ptr resolve_op_lshift(SType, SType);
8484
bimaker_ptr resolve_op_rshift(SType, SType);

src/core/expr/fbinary/bitwise_and.cc

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,44 @@ static bool op_and_bool(ref_t<T> x, bool xvalid, ref_t<T> y, bool yvalid, int8_t
4040
return xvalid;
4141
}
4242

43+
44+
template <typename T>
45+
static bool op_or_bool(ref_t<T> x, bool xvalid, ref_t<T> y, bool yvalid, int8_t* out)
46+
{
47+
if (x == 1 && xvalid) { // short-circuit
48+
*out = 1;
49+
return true;
50+
}
51+
if (!yvalid) return false;
52+
if (y == 1) {
53+
*out = 1;
54+
return true;
55+
}
56+
*out = 0;
57+
return xvalid;
58+
}
59+
60+
4361
template <typename T>
4462
inline static T op_and(T x, T y) {
4563
return (x & y);
4664
}
4765

48-
class FExpr__and__ : public FExpr_BinaryOp {
66+
template <typename T>
67+
inline static T op_or(T x, T y) {
68+
return (x | y);
69+
}
70+
71+
template<bool AND>
72+
class FExpr__andor__ : public FExpr_BinaryOp {
4973
public:
5074
using FExpr_BinaryOp::FExpr_BinaryOp;
5175
using FExpr_BinaryOp::lhs_;
5276
using FExpr_BinaryOp::rhs_;
5377

5478

55-
std::string name() const override { return "&"; }
56-
int precedence() const noexcept override { return 4; }
79+
std::string name() const override { return AND?"&":"|"; }
80+
int precedence() const noexcept override { return AND?4:3; }
5781

5882

5983
Column evaluate1(Column&& lcol, Column&& rcol) const override {
@@ -67,33 +91,47 @@ class FExpr__and__ : public FExpr_BinaryOp {
6791
return Column::new_na_column(lcol.nrows(), SType::VOID);
6892
}
6993
if (stype1 == SType::BOOL && stype2 == SType::BOOL) {
94+
if (AND) {
95+
return Column(new FuncBinary2_ColumnImpl<int8_t, int8_t, int8_t>(
96+
std::move(lcol), std::move(rcol),
97+
op_and_bool<int8_t>,
98+
nrows, SType::BOOL
99+
));
100+
}
70101
return Column(new FuncBinary2_ColumnImpl<int8_t, int8_t, int8_t>(
71102
std::move(lcol), std::move(rcol),
72-
op_and_bool<int8_t>,
103+
op_or_bool<int8_t>,
73104
nrows, SType::BOOL
74105
));
75106
}
76107
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);
108+
case SType::INT8: return make<int8_t, AND>(std::move(lcol), std::move(rcol), stype0);
109+
case SType::INT16: return make<int16_t, AND>(std::move(lcol), std::move(rcol), stype0);
110+
case SType::INT32: return make<int32_t, AND>(std::move(lcol), std::move(rcol), stype0);
111+
case SType::INT64: return make<int64_t, AND>(std::move(lcol), std::move(rcol), stype0);
81112
default:
82113
throw TypeError() << "Operator `&` cannot be applied to columns of "
83114
"types `" << stype1 << "` and `" << stype2 << "`";
84115
}
85116
}
86117

87118
private:
88-
template <typename T>
119+
template <typename T, bool ANDD>
89120
static Column make(Column&& a, Column&& b, SType stype) {
90121
xassert(compatible_type<T>(stype));
91122
size_t nrows = a.nrows();
92123
a.cast_inplace(stype);
93124
b.cast_inplace(stype);
125+
if (AND) {
126+
return Column(new FuncBinary1_ColumnImpl<T, T, T>(
127+
std::move(a), std::move(b),
128+
op_and<T>,
129+
nrows, stype
130+
));
131+
}
94132
return Column(new FuncBinary1_ColumnImpl<T, T, T>(
95133
std::move(a), std::move(b),
96-
op_and<T>,
134+
op_or<T>,
97135
nrows, stype
98136
));
99137
}
@@ -103,7 +141,12 @@ class FExpr__and__ : public FExpr_BinaryOp {
103141

104142
py::oobj PyFExpr::nb__and__(py::robj lhs, py::robj rhs) {
105143
return PyFExpr::make(
106-
new FExpr__and__(as_fexpr(lhs), as_fexpr(rhs)));
144+
new FExpr__andor__<true>(as_fexpr(lhs), as_fexpr(rhs)));
145+
}
146+
147+
py::oobj PyFExpr::nb__or__(py::robj lhs, py::robj rhs) {
148+
return PyFExpr::make(
149+
new FExpr__andor__<false>(as_fexpr(lhs), as_fexpr(rhs)));
107150
}
108151

109152

src/core/expr/fexpr.cc

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

188188
//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); }
190-
oobj PyFExpr::nb__or__(robj lhs, robj rhs) { return make_binexpr(dt::expr::Op::OR, lhs, rhs); }
190+
//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); }
192192
oobj PyFExpr::nb__rshift__(robj lhs, robj rhs) { return make_binexpr(dt::expr::Op::RSHIFT, lhs, rhs); }
193193

0 commit comments

Comments
 (0)