Guest User

refine-optional

a guest
Jun 23rd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. diff --git a/cpp/src/arrow/compute/kernels/vector_sort.cc b/cpp/src/arrow/compute/kernels/vector_sort.cc
  2. index 9a3aa77ee..f8ec9b52a 100644
  3. --- a/cpp/src/arrow/compute/kernels/vector_sort.cc
  4. +++ b/cpp/src/arrow/compute/kernels/vector_sort.cc
  5. @@ -130,28 +130,26 @@ class CountSorter {
  6.      // first slot reserved for prefix sum, last slot for null value
  7.      std::vector<CounterType> counts(1 + value_range + 1);
  8.  
  9. -    auto update_counts = [&](util::optional<c_type> v) {
  10. -      if (v.has_value()) {
  11. -        ++counts[*v - min_ + 1];
  12. -      } else {
  13. -        ++counts[value_range + 1];
  14. -      }
  15. +    auto count_notnull = [&](c_type v) {
  16. +      ++counts[v - min_ + 1];
  17. +    };
  18. +    auto count_null = [&]() {
  19. +      ++counts[value_range + 1];
  20.      };
  21. -    VisitArrayDataInline<ArrowType>(*values.data(), std::move(update_counts));
  22. +    VisitArrayDataInline<ArrowType>(*values.data(), std::move(count_notnull), std::move(count_null));
  23.  
  24.      for (uint32_t i = 1; i <= value_range; ++i) {
  25.        counts[i] += counts[i - 1];
  26.      }
  27.  
  28.      int64_t index = 0;
  29. -    auto write_index = [&](util::optional<c_type> v) {
  30. -      if (v.has_value()) {
  31. -        indices_begin[counts[*v - min_]++] = index++;
  32. -      } else {
  33. -        indices_begin[counts[value_range]++] = index++;
  34. -      }
  35. +    auto write_notnull = [&](c_type v) {
  36. +      indices_begin[counts[v - min_]++] = index++;
  37. +    };
  38. +    auto write_null = [&]() {
  39. +      indices_begin[counts[value_range]++] = index++;
  40.      };
  41. -    VisitArrayDataInline<ArrowType>(*values.data(), std::move(write_index));
  42. +    VisitArrayDataInline<ArrowType>(*values.data(), std::move(write_notnull), std::move(write_null));
  43.    }
  44.  };
  45.  
  46. @@ -169,13 +167,12 @@ class CountOrCompareSorter {
  47.        c_type min{std::numeric_limits<c_type>::max()};
  48.        c_type max{std::numeric_limits<c_type>::min()};
  49.  
  50. -      auto update_minmax = [&min, &max](util::optional<c_type> v) {
  51. -        if (v.has_value()) {
  52. -          min = std::min(min, *v);
  53. -          max = std::max(max, *v);
  54. -        }
  55. +      auto update_notnull = [&min, &max](c_type v) {
  56. +          min = std::min(min, v);
  57. +          max = std::max(max, v);
  58.        };
  59. -      VisitArrayDataInline<ArrowType>(*values.data(), std::move(update_minmax));
  60. +      auto update_null = []() {};
  61. +      VisitArrayDataInline<ArrowType>(*values.data(), std::move(update_notnull), std::move(update_null));
  62.        // For signed int32/64, (max - min) may overflow and trigger UBSAN.
  63.        // Cast to largest unsigned type(uint64_t) before subtraction.
  64.        if (static_cast<uint64_t>(max) - static_cast<uint64_t>(min) <=
  65. diff --git a/cpp/src/arrow/visitor_inline.h b/cpp/src/arrow/visitor_inline.h
  66. index 7a18ceb70..321b4870d 100644
  67. --- a/cpp/src/arrow/visitor_inline.h
  68. +++ b/cpp/src/arrow/visitor_inline.h
  69. @@ -208,6 +208,16 @@ struct ArrayDataInlineVisitor<T, enable_if_has_c_type<T>> {
  70.          [&](int64_t i) { func(util::optional<c_type>(data[i])); },
  71.          [&]() { func(util::optional<c_type>()); });
  72.    }
  73. +
  74. +  template <typename VisitFuncNotNull, typename VisitFuncNull>
  75. +  static void VisitVoid(const ArrayData& arr, VisitFuncNotNull&& func_notnull, VisitFuncNull&& func_null) {
  76. +    using c_type = typename T::c_type;
  77. +    const c_type* data = arr.GetValues<c_type>(1);
  78. +    detail::VisitBitBlocksVoid(
  79. +        arr.buffers[0], arr.offset, arr.length,
  80. +        [&](int64_t i) { func_notnull(data[i]); },
  81. +        [&]() { func_null(); });
  82. +  }
  83.  };
  84.  
  85.  // Boolean
  86. @@ -368,6 +378,14 @@ VisitArrayDataInline(const ArrayData& arr, VisitFunc&& func) {
  87.                                                          std::forward<VisitFunc>(func));
  88.  }
  89.  
  90. +template <typename T, typename VisitFuncNotNull, typename VisitFuncNull>
  91. +typename internal::call_traits::enable_if_return<VisitFuncNotNull, void>::type
  92. +VisitArrayDataInline(const ArrayData& arr, VisitFuncNotNull&& func_notnull, VisitFuncNull&& func_null) {
  93. +  return internal::ArrayDataInlineVisitor<T>::VisitVoid(arr,
  94. +                                                        std::forward<VisitFuncNotNull>(func_notnull),
  95. +                                                        std::forward<VisitFuncNull>(func_null));
  96. +}
  97. +
  98.  // Visit an array's data values, in order, without overhead.
  99.  //
  100.  // The Visit method's `visitor` argument should be an object with two public methods:
Advertisement
Add Comment
Please, Sign In to add comment