|
| 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 | + |
| 25 | +namespace dt { |
| 26 | +namespace expr { |
| 27 | + |
| 28 | +template <typename T> |
| 29 | +inline static T op_lshift(T x, int32_t y) { |
| 30 | + return (y >= 0)? static_cast<T>(x << y) : static_cast<T>(x >> -y); |
| 31 | +} |
| 32 | + |
| 33 | +template <typename T> |
| 34 | +inline static T op_rshift(T x, int32_t y) { |
| 35 | + return (y >= 0)? static_cast<T>(x >> y) : static_cast<T>(x << -y); |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +template<bool LSHIFT> |
| 40 | +class FExprBitShift : public FExpr_BinaryOp { |
| 41 | + public: |
| 42 | + using FExpr_BinaryOp::FExpr_BinaryOp; |
| 43 | + using FExpr_BinaryOp::lhs_; |
| 44 | + using FExpr_BinaryOp::rhs_; |
| 45 | + |
| 46 | + |
| 47 | + std::string name() const override { return LSHIFT?"<<":">>"; } |
| 48 | + int precedence() const noexcept override { return 10; } |
| 49 | + |
| 50 | + |
| 51 | + Column evaluate1(Column&& lcol, Column&& rcol) const override { |
| 52 | + xassert(lcol.nrows() == rcol.nrows()); |
| 53 | + auto stype1 = lcol.stype(); |
| 54 | + auto stype2 = rcol.stype(); |
| 55 | + xassert(Type::from_stype(stype1).is_integer()); |
| 56 | + xassert(Type::from_stype(stype1).is_integer() || Type::from_stype(stype1).is_boolean()); |
| 57 | + |
| 58 | + auto stype0 = (stype2 == SType::INT32)? SType::AUTO : SType::INT32; |
| 59 | + |
| 60 | + switch (stype1) { |
| 61 | + case SType::INT8: return make<int8_t, LSHIFT>(std::move(lcol), std::move(rcol), stype0); |
| 62 | + case SType::INT16: return make<int16_t, LSHIFT>(std::move(lcol), std::move(rcol), stype0); |
| 63 | + case SType::INT32: return make<int32_t, LSHIFT>(std::move(lcol), std::move(rcol), stype0); |
| 64 | + case SType::INT64: return make<int64_t, LSHIFT>(std::move(lcol), std::move(rcol), stype0); |
| 65 | + default: |
| 66 | + std::string op = LSHIFT?"<<":">>"; |
| 67 | + throw TypeError() << "Operator " << op << " cannot be applied to columns of " |
| 68 | + "types `" << stype1 << "` and `" << stype2 << "`"; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private: |
| 73 | + template <typename T, bool L_SHIFT> |
| 74 | + static Column make(Column&& a, Column&& b, SType stype) { |
| 75 | + xassert(compatible_type<T>(stype)); |
| 76 | + size_t nrows = a.nrows(); |
| 77 | + b.cast_inplace(stype); |
| 78 | + if (L_SHIFT) { |
| 79 | + return Column(new FuncBinary1_ColumnImpl<T, int32_t, T>( |
| 80 | + std::move(a), std::move(b), |
| 81 | + op_lshift<T>, |
| 82 | + nrows, a.stype() |
| 83 | + )); |
| 84 | + } |
| 85 | + return Column(new FuncBinary1_ColumnImpl<T, int32_t, T>( |
| 86 | + std::move(a), std::move(b), |
| 87 | + op_rshift<T>, |
| 88 | + nrows, a.stype() |
| 89 | + )); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | + |
| 94 | +py::oobj PyFExpr::nb__lshift__(py::robj lhs, py::robj rhs) { |
| 95 | + return PyFExpr::make( |
| 96 | + new FExprBitShift<true>(as_fexpr(lhs), as_fexpr(rhs))); |
| 97 | +} |
| 98 | + |
| 99 | +py::oobj PyFExpr::nb__rshift__(py::robj lhs, py::robj rhs) { |
| 100 | + return PyFExpr::make( |
| 101 | + new FExprBitShift<false>(as_fexpr(lhs), as_fexpr(rhs))); |
| 102 | +} |
| 103 | + |
| 104 | +}} // namespace dt::expr |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
0 commit comments