scenario_simulator_v2 C++ API
fold.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef OPENSCENARIO_INTERPRETER__FUNCTIONAL__FOLD_HPP_
16 #define OPENSCENARIO_INTERPRETER__FUNCTIONAL__FOLD_HPP_
17 
18 #include <functional>
19 #include <utility>
20 
22 {
23 inline namespace functional
24 {
25 template <typename F, typename T>
26 constexpr decltype(auto) fold_left(F &&, T && x)
27 {
28  return std::forward<decltype(x)>(x);
29 }
30 
31 template <typename F, typename T, typename U, typename... Ts>
32 constexpr decltype(auto) fold_left(F && f, T && x, U && y, Ts &&... xs)
33 {
34  return fold_left(
35  f, f(std::forward<decltype(x)>(x), std::forward<decltype(y)>(y)),
36  std::forward<decltype(xs)>(xs)...);
37 }
38 
39 template <typename F, typename T>
40 constexpr decltype(auto) fold_right(F &&, T && x)
41 {
42  return std::forward<decltype(x)>(x);
43 }
44 
45 template <typename F, typename T, typename... Ts>
46 constexpr decltype(auto) fold_right(F && f, T && x, Ts &&... xs)
47 {
48  return f(std::forward<decltype(x)>(x), fold_right(f, std::forward<decltype(xs)>(xs)...));
49 }
50 
51 template <typename... Ts>
52 constexpr auto fold(Ts &&... xs) -> decltype(auto)
53 {
54  return fold_left(std::forward<decltype(xs)>(xs)...);
55 }
56 } // namespace functional
57 } // namespace openscenario_interpreter
58 
59 #endif // OPENSCENARIO_INTERPRETER__FUNCTIONAL__FOLD_HPP_
constexpr decltype(auto) fold_left(F &&, T &&x)
Definition: fold.hpp:26
constexpr decltype(auto) fold_right(F &&, T &&x)
Definition: fold.hpp:40
constexpr auto fold(Ts &&... xs) -> decltype(auto)
Definition: fold.hpp:52
Definition: escape_sequence.hpp:22
Definition: junit5.hpp:25