clean code

This commit is contained in:
zhanglistar 2024-10-15 12:28:01 +08:00
parent 0410f38dec
commit 5b841d994e

View File

@ -607,32 +607,61 @@ public:
} }
else if constexpr (is_division && is_decimal_b) else if constexpr (is_division && is_decimal_b)
{ {
processWithRightNullmapImpl<op_case>(a, b, c, size, right_nullmap, res_nullmap, [&scale_a](const auto & left, const auto & right) if (res_nullmap)
{ {
return applyScaledDiv<is_decimal_a>( if (right_nullmap)
static_cast<NativeResultType>(left), right, scale_a); res_nullmap->assign(*right_nullmap);
});
processWithRightNullmapImpl<true, op_case>(a, b, c, size, res_nullmap, [&scale_a](const auto & left, const auto & right)
{
return applyScaledDiv<is_decimal_a>(
static_cast<NativeResultType>(left), right, scale_a);
});
}
else
processWithRightNullmapImpl<false, op_case>(a, b, c, size, right_nullmap, [&scale_a](const auto & left, const auto & right)
{
return applyScaledDiv<is_decimal_a>(
static_cast<NativeResultType>(left), right, scale_a);
});
return; return;
} }
processWithRightNullmapImpl<op_case>( if (res_nullmap)
a, b, c, size, right_nullmap, res_nullmap, {
[](const auto & left, const auto & right) if (right_nullmap)
{ res_nullmap->assign(*right_nullmap);
return apply(
static_cast<NativeResultType>(left), processWithRightNullmapImpl<true, op_case>(
static_cast<NativeResultType>(right)); a, b, c, size, res_nullmap,
}); [](const auto & left, const auto & right)
{
return apply(
static_cast<NativeResultType>(left),
static_cast<NativeResultType>(right));
});
}
else
processWithRightNullmapImpl<false, op_case>(
a, b, c, size, right_nullmap,
[](const auto & left, const auto & right)
{
return apply(
static_cast<NativeResultType>(left),
static_cast<NativeResultType>(right));
});
} }
template <bool is_decimal_a, bool is_decimal_b, class A, class B> template <bool is_decimal_a, bool is_decimal_b, class A, class B>
static ResultType process(A a, B b, NativeResultType scale_a, NativeResultType scale_b, NullMap * res_nullmap) static ResultType process(A a, B b, NativeResultType scale_a, NativeResultType scale_b, NullMap * m)
requires(!is_decimal<A> && !is_decimal<B>) requires(!is_decimal<A> && !is_decimal<B>)
{ {
try try
{ {
ResultType res{};
if constexpr (is_division && is_decimal_b) if constexpr (is_division && is_decimal_b)
return applyScaledDiv<is_decimal_a>(a, b, scale_a); res = applyScaledDiv<is_decimal_a>(a, b, scale_a);
else if constexpr (is_plus_minus_compare) else if constexpr (is_plus_minus_compare)
{ {
if (scale_a != 1) if (scale_a != 1)
@ -640,19 +669,17 @@ public:
if (scale_b != 1) if (scale_b != 1)
return applyScaled<false>(a, b, scale_b); return applyScaled<false>(a, b, scale_b);
} }
else
ResultType res = apply(a, b); res = apply(a, b);
if constexpr (std::is_floating_point_v<ResultType>) if constexpr (std::is_floating_point_v<ResultType>)
{ if (unlikely(!std::isfinite(res)) && m)
if (unlikely(!std::isfinite(res)) && res_nullmap) (*m)[0] = 1;
(*res_nullmap)[0] = 1;
}
return res; return res;
} }
catch (const std::exception&) catch (const std::exception&)
{ {
if (res_nullmap) if (m)
(*res_nullmap)[0] = 1; (*m)[0] = 1;
else else
throw; throw;
return ResultType(); /// Unreachable to disable compiler error. return ResultType(); /// Unreachable to disable compiler error.
@ -660,14 +687,14 @@ public:
} }
private: private:
template <OpCase op_case, typename ApplyFunc> template <bool may_gen_null, OpCase op_case, typename ApplyFunc>
static void processWithRightNullmapImpl(const auto & a, const auto & b, ResultContainerType & c, size_t size, const NullMap * right_nullmap, NullMap * res_nullmap, ApplyFunc apply_func) static void processWithRightNullmapImpl(const auto & a, const auto & b, ResultContainerType & c, size_t size, std::conditional_t<may_gen_null, NullMap *, const NullMap *> res_nullmap, ApplyFunc apply_func)
{ {
if (right_nullmap) if (res_nullmap)
{ {
if constexpr (op_case == OpCase::RightConstant) if constexpr (op_case == OpCase::RightConstant)
{ {
if ((*right_nullmap)[0]) if ((*res_nullmap)[0])
{ {
for (size_t i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
c[i] = ResultType(); c[i] = ResultType();
@ -676,73 +703,71 @@ private:
for (size_t i = 0; i < size; ++i) for (size_t i = 0; i < size; ++i)
{ {
try if constexpr (may_gen_null)
{
c[i] = apply_func(undec(a[i]), undec(b));
if constexpr (std::is_floating_point_v<ResultContainerType>)
{
if (unlikely(!std::isfinite(c[i])) && res_nullmap)
(*res_nullmap)[i] = 1;
}
}
catch (const std::exception&)
{
if (res_nullmap)
(*res_nullmap)[i] = 1;
else
throw;
}
}
}
else
{
for (size_t i = 0; i < size; ++i)
{
if ((*right_nullmap)[i])
c[i] = ResultType();
else
{ {
try try
{ {
c[i] = apply_func(unwrap<op_case, OpCase::LeftConstant>(a, i), undec(b[i])); c[i] = apply_func(undec(a[i]), undec(b));
if constexpr (std::is_floating_point_v<ResultContainerType>) if constexpr (std::is_floating_point_v<ResultContainerType>)
{ if (unlikely(!std::isfinite(c[i])))
if (unlikely(!std::isfinite(c[i])) && res_nullmap)
(*res_nullmap)[i] = 1; (*res_nullmap)[i] = 1;
}
} }
catch (const std::exception&) catch (const std::exception&)
{ {
if (res_nullmap) (*res_nullmap)[i] = 1;
(*res_nullmap)[i] = 1;
else
throw;
} }
} }
else
c[i] = apply_func(undec(a[i]), undec(b));
} }
} }
else
for (size_t i = 0; i < size; ++i)
{
if ((*res_nullmap)[i])
c[i] = ResultType();
else
{
if constexpr (may_gen_null)
{
try
{
c[i] = apply_func(unwrap<op_case, OpCase::LeftConstant>(a, i), undec(b[i]));
if constexpr (std::is_floating_point_v<ResultContainerType>)
if (unlikely(!std::isfinite(c[i])))
(*res_nullmap)[i] = 1;
}
catch (const std::exception&)
{
(*res_nullmap)[i] = 1;
}
}
else
c[i] = apply_func(unwrap<op_case, OpCase::LeftConstant>(a, i), undec(b[i]));
}
}
return;
} }
else /// res_nullmap is nullptr
for (size_t i = 0; i < size; ++i)
{ {
for (size_t i = 0; i < size; ++i) if constexpr (may_gen_null)
{ {
try try
{ {
c[i] = apply_func(unwrap<op_case, OpCase::LeftConstant>(a, i), unwrap<op_case, OpCase::RightConstant>(b, i)); c[i] = apply_func(unwrap<op_case, OpCase::LeftConstant>(a, i), unwrap<op_case, OpCase::RightConstant>(b, i));
if constexpr (std::is_floating_point_v<ResultContainerType>) if constexpr (std::is_floating_point_v<ResultContainerType>)
{ if (unlikely(!std::isfinite(c[i])))
if (unlikely(!std::isfinite(c[i])) && res_nullmap)
(*res_nullmap)[i] = 1; (*res_nullmap)[i] = 1;
}
} }
catch (const std::exception&) catch (const std::exception&)
{ {
if (res_nullmap) (*res_nullmap)[i] = 1;
(*res_nullmap)[i] = 1;
else
throw;
} }
} }
else
c[i] = apply_func(unwrap<op_case, OpCase::LeftConstant>(a, i), unwrap<op_case, OpCase::RightConstant>(b, i));
} }
} }