reduce.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. /*
  3. * Copyright 2018-present Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <folly/experimental/pushmi/examples/bulk.h>
  18. #include <folly/experimental/pushmi/o/just.h>
  19. #include <folly/experimental/pushmi/o/submit.h>
  20. namespace pushmi {
  21. PUSHMI_INLINE_VAR constexpr struct reduce_fn {
  22. private:
  23. template <class BinaryOp>
  24. struct fn {
  25. BinaryOp binary_op_;
  26. template <class Acc, class Cursor>
  27. void operator()(Acc& acc, Cursor cursor) const {
  28. acc = binary_op_(acc, *cursor);
  29. }
  30. };
  31. struct identity {
  32. template <class T>
  33. auto operator()(T&& t) const {
  34. return (T &&) t;
  35. }
  36. };
  37. public:
  38. template <class ExecutionPolicy, class ForwardIt, class T, class BinaryOp>
  39. T operator()(
  40. ExecutionPolicy&& policy,
  41. ForwardIt begin,
  42. ForwardIt end,
  43. T init,
  44. BinaryOp binary_op) const {
  45. return operators::just(std::move(init)) |
  46. operators::bulk(
  47. fn<BinaryOp>{binary_op},
  48. begin,
  49. end,
  50. policy,
  51. identity{},
  52. identity{}) |
  53. operators::get<T>;
  54. }
  55. } reduce{};
  56. } // namespace pushmi