for_each.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 for_each_fn {
  22. private:
  23. template <class Function>
  24. struct fn {
  25. Function f_;
  26. template <class Cursor>
  27. void operator()(detail::any, Cursor cursor) const {
  28. f_(*cursor);
  29. }
  30. };
  31. struct identity {
  32. template <class T>
  33. auto operator()(T&& t) const {
  34. return (T &&) t;
  35. }
  36. };
  37. struct zero {
  38. int operator()(detail::any) const noexcept {
  39. return 0;
  40. }
  41. };
  42. public:
  43. template <class ExecutionPolicy, class RandomAccessIterator, class Function>
  44. void operator()(
  45. ExecutionPolicy&& policy,
  46. RandomAccessIterator begin,
  47. RandomAccessIterator end,
  48. Function f) const {
  49. operators::just(0) |
  50. operators::bulk(
  51. fn<Function>{f}, begin, end, policy, identity{}, zero{}) |
  52. operators::blocking_submit();
  53. }
  54. } for_each{};
  55. } // namespace pushmi