reduce_nested_non_empty_variadic.hpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2014, 2015 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 
31 #include <atria/estd/utility.hpp>
33 #include <atria/meta/pack.hpp>
34 #include <iterator>
35 #include <tuple>
36 
37 namespace atria {
38 namespace xform {
39 namespace detail {
40 
41 template <typename ReducingFnT,
42  typename StateT,
43  std::size_t ...Indices,
44  typename ...InputTs,
45  typename ...InputRangeTs>
46 auto reduce_nested_non_empty_variadic_impl(estd::index_sequence<Indices...>,
47  meta::pack<InputTs...>,
48  ReducingFnT&& step,
49  StateT&& initial,
50  InputRangeTs&& ...ranges)
51  -> estd::decay_t<decltype(step(initial, *std::begin(ranges)...))>
52 {
53  auto firsts = std::make_tuple(std::begin(ranges)...);
54  auto lasts = std::make_tuple(std::end(ranges)...);
55  auto state = step(std::forward<StateT>(initial),
56  std::forward<InputTs>(*std::get<Indices>(firsts))...);
57 
58  meta::noop(++std::get<Indices>(firsts)...);
59  while (!state_is_reduced(state) && detail::tuple_all_neq(firsts, lasts)) {
60  auto new_state = step(std::move(state),
61  std::forward<InputTs>(*std::get<Indices>(firsts))...);
62  state = std::move(new_state);
63  meta::noop(++std::get<Indices>(firsts)...);
64  }
65 
66  return state;
67 }
68 
69 template <typename ReducingFnT,
70  typename StateT,
71  typename ...InputRangeTs>
72 auto reduce_nested_non_empty_variadic(ReducingFnT&& step, StateT&& state, InputRangeTs&& ...ranges)
74  reduce_nested_non_empty_variadic_impl(
75  estd::make_index_sequence<sizeof...(InputRangeTs)> {},
76  meta::pack<meta::copy_decay_t<
77  InputRangeTs,
78  estd::remove_reference_t<decltype(*std::begin(ranges))> >...>{},
79  std::forward<ReducingFnT>(step),
80  std::forward<StateT>(state),
81  std::forward<InputRangeTs>(ranges)...))
82 
83 } // namespace detail
84 } // namespace xform
85 } // 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
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
Fork me on GitHub