|
| 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 |
0 commit comments