Commit 54ee5225 authored by Alex Yarmula's avatar Alex Yarmula Committed by Facebook Github Bot

add a repeat-n-times overload to folly::window

Summary:
Add the ability to use folly::window without providing a vector ahead of time.
This allows window to scale to a large number of iterations.

Reviewed By: yfeldblum

Differential Revision: D7863481

fbshipit-source-id: 86c6b7926e9c3b7510233fac20a50c1fb38f5b57
parent 8b829f52
...@@ -547,6 +547,25 @@ class DeferredExecutor final : public Executor { ...@@ -547,6 +547,25 @@ class DeferredExecutor final : public Executor {
Executor* executor_; Executor* executor_;
folly::Synchronized<std::shared_ptr<FutureBatonType>> baton_; folly::Synchronized<std::shared_ptr<FutureBatonType>> baton_;
}; };
// Vector-like structure to play with window,
// which otherwise expects a vector of size `times`,
// which would be expensive with large `times` sizes.
struct WindowFakeVector {
using iterator = std::vector<size_t>::iterator;
WindowFakeVector(int size) : size_(size) {}
size_t operator[](const size_t index) const {
return index;
}
size_t size() const {
return size_;
}
private:
size_t size_;
};
} // namespace detail } // namespace detail
} // namespace futures } // namespace futures
...@@ -1451,6 +1470,12 @@ window(Collection input, F func, size_t n) { ...@@ -1451,6 +1470,12 @@ window(Collection input, F func, size_t n) {
return window(executor, std::move(input), std::move(func), n); return window(executor, std::move(input), std::move(func), n);
} }
template <class F>
auto window(size_t times, F func, size_t n)
-> std::vector<invoke_result_t<F, size_t>> {
return window(futures::detail::WindowFakeVector(times), std::move(func), n);
}
template <class Collection, class F, class ItT, class Result> template <class Collection, class F, class ItT, class Result>
std::vector<Future<Result>> std::vector<Future<Result>>
window(Executor* executor, Collection input, F func, size_t n) { window(Executor* executor, Collection input, F func, size_t n) {
......
...@@ -82,6 +82,20 @@ TEST(Window, basic) { ...@@ -82,6 +82,20 @@ TEST(Window, basic) {
}).get(); }).get();
EXPECT_EQ(6, res); EXPECT_EQ(6, res);
} }
{
SCOPED_TRACE("repeat same fn");
auto res = reduce(
window(
5UL,
[](size_t iteration) {
return folly::makeFuture(iteration); },
2),
0UL,
[](size_t sum, const Try<size_t>& b) {
return sum + b.value();
}).get();
EXPECT_EQ(0 + 1 + 2 + 3 + 4, res);
}
} }
TEST(Window, exception) { TEST(Window, exception) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment