You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/11/30 12:22:11 UTC

[GitHub] [arrow] milesgranger commented on a diff in pull request #14696: ARROW-18282: [C++][Python] Support step >= 1 in list_slice kernel

milesgranger commented on code in PR #14696:
URL: https://github.com/apache/arrow/pull/14696#discussion_r1035902151


##########
cpp/src/arrow/compute/kernels/scalar_nested.cc:
##########
@@ -96,6 +97,22 @@ std::string ToString(const std::optional<T>& o) {
   return o.has_value() ? ToChars(*o) : "(nullopt)";
 }
 
+int64_t MaxSliceLength(const int64_t start, const int64_t stop, const int64_t step) {
+  const auto startf = static_cast<float>(start);
+  const auto stopf = static_cast<float>(stop);
+  const auto stepf = static_cast<float>(step);
+
+  if (stopf - startf <= stepf) {
+    return 1;
+  }
+
+  auto length = static_cast<int64_t>(std::floor((stopf - startf) / stepf));
+  if (fmod(static_cast<float>(length), stepf) > 0.0) {
+    ++length;
+  }
+  return length;

Review Comment:
   More tests added, let me know if you see any specific combinations you'd like to see.
   
   > (also, shouldn't it be fmod(stop-start, step) instead?)
   
   It needs to be `length`, which if there is no step, then `stop-start` is okay, but that divided by step gives the actual length if step != 1. Then (at least in python) if there is any remainder one more is added to the possible length.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org