product.hpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2014, 2015, 2016 Ableton AG, Berlin. All rights reserved.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 //
22 
27 #pragma once
28 
32 #include <atria/prelude/comp.hpp>
33 
34 namespace atria {
35 namespace xform {
36 
37 namespace detail {
38 
39 template <typename ReducingFnT,
40  typename StateT,
41  typename InputRangeT,
42  typename... ValueTs>
43 auto reduce_nested_non_empty_product(ReducingFnT&& step,
44  StateT&& initial,
45  InputRangeT&& range,
46  ValueTs&& ...values)
47  -> estd::decay_t<decltype(step(initial, values..., *std::begin(range)))>
48 {
49  auto first = std::begin(range);
50  auto last = std::end(range);
51  auto state = step(std::forward<StateT>(initial), values..., *first);
52  for (++first; !state_is_reduced(state) && first != last; ++first) {
53  auto new_state = step(std::move(state), values..., *first);
54  state = std::move(new_state);
55  }
56  return state;
57 }
58 
59 struct product_rf_gen
60 {
61  template <typename ReducingFnT,
62  typename InputRangeT>
63  struct apply
64  {
65  ReducingFnT step;
66  InputRangeT range;
67 
68  template <typename StateT, typename ...InputTs>
69  auto operator() (StateT&& s, InputTs&& ...is)
71  reduce_nested_non_empty_product(
72  step,
73  std::forward<StateT>(s),
74  range,
75  std::forward<InputTs>(is)...))
76  };
77 };
78 
79 } // namespace detail
80 
81 template <typename T>
82 using product_t = transducer_impl<detail::product_rf_gen, T>;
83 
89 template <typename InputRangeT>
90 constexpr auto product(InputRangeT&& r)
92 {
94  detail::check_non_empty(std::forward<InputRangeT>(r)) };
95 }
96 
97 template <typename InputRangeT1, typename InputRangeT2, typename ...InputRangeTs>
98 constexpr auto product(InputRangeT1&& r1, InputRangeT2&& r2, InputRangeTs&& ...rs)
100  comp(product(std::forward<InputRangeT1>(r1)),
101  product(std::forward<InputRangeT2>(r2)),
102  product(std::forward<InputRangeTs>(rs))...))
103 
104 } // namespace xform
105 } // namespace atria
#define ABL_DECLTYPE_RETURN(body_expr)
Utility for defining generic functions with a deduced return type, that are composed of a single expr...
Definition: utils.hpp:109
typename std::decay< T >::type decay_t
Similar to C++14 std::decay_t.
Definition: type_traits.hpp:54
constexpr auto range(StopT &&stop) -> decltype(comp( count(), take(std::forward< StopT >(stop))))
Generator transducer version of Python range
Definition: range.hpp:42
Utility to write simple transducers easily.
auto comp(F &&f) -> F &&
Right-to left function composition.
Definition: comp.hpp:80
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
auto state_is_reduced(T &&s) -> bool
Convenience function for calling state_traits::is_reduced
constexpr auto product(InputRangeT &&r) -> product_t< estd::decay_t< InputRangeT > >
Transducer combines every element that passes by with every element in the sequence that it takes as ...
Definition: product.hpp:90
Fork me on GitHub