partition_by.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 
29 #include <atria/xform/reduce.hpp>
32 #include <atria/xform/skip.hpp>
34 #include <vector>
35 
36 namespace atria {
37 namespace xform {
38 
39 namespace detail {
40 
41 struct partition_by_rf_gen
42 {
43  struct tag {};
44 
45  template <typename ReducingFnT,
46  typename MappingT>
47  struct apply
48  {
49  ReducingFnT step;
50  MappingT mapping;
51 
52  template <typename ...InputTs>
53  using container_t = std::vector<
55  decltype(tuplify(std::declval<InputTs>()...))> >;
56 
57  template <typename StateT, typename ...InputTs>
58  auto operator() (StateT&& s, InputTs&& ...is)
59  -> decltype(
60  wrap_state<tag>(
61  skip(step, state_unwrap(s), container_t<InputTs...>{}),
62  make_tuple(mapping(is...), container_t<InputTs...>{}, step)))
63  {
64  auto mapped = estd::invoke(mapping, std::forward<InputTs>(is)...);
65 
66  auto data = state_data(std::forward<StateT>(s), [&] {
67  auto v = container_t<InputTs...>{};
68  return make_tuple(mapped, std::move(v), step);
69  });
70 
71  auto& last_mapped = std::get<0>(data);
72  auto& next_vector = std::get<1>(data);
73  auto& next_step = std::get<2>(data);
74 
75  const auto complete_group = mapped != last_mapped;
76 
77  auto next_state = complete_group
78  ? call(step, state_unwrap(std::forward<StateT>(s)), next_vector)
79  : skip(step, state_unwrap(std::forward<StateT>(s)), next_vector);
80 
81  if (complete_group)
82  next_vector.clear();
83 
84  next_vector.push_back(tuplify(std::forward<InputTs>(is)...));
85 
86  return wrap_state<tag> (
87  std::move(next_state),
88  make_tuple(std::move(mapped),
89  std::move(next_vector),
90  std::move(next_step)));
91  }
92  };
93 
94  template <typename T>
95  friend auto state_wrapper_complete(tag, T&& wrapper)
96  -> estd::decay_t<decltype(
97  state_complete(state_unwrap(std::forward<T>(wrapper))))>
98  {
99  auto next = std::get<1>(state_wrapper_data(std::forward<T>(wrapper)));
100  auto step = std::get<2>(state_wrapper_data(std::forward<T>(wrapper)));
101  return state_complete(
102  next.empty()
103  ? state_unwrap(std::forward<T>(wrapper))
104  : step(state_unwrap(std::forward<T>(wrapper)), std::move(next)));
105  }
106 };
107 
108 } // namespace detail
109 
110 template <typename T>
111 using partition_by_t = transducer_impl<detail::partition_by_rf_gen, T>;
112 
116 template <typename MappingT>
117 auto partition_by(MappingT&& mapping)
119 {
120  return partition_by_t<estd::decay_t<MappingT> > { mapping };
121 }
122 
123 } // namespace xform
124 } // namespace atria
auto state_wrapper_data(TagT tag, T &&s, D &&) -> decltype(state_wrapper_data(tag, std::forward< T >(s)))
Utility function for easy overloading of state_traits::data for state wrappers with a specific tag...
auto call(ReducingFnT &&step, StateT &&state, InputTs &&...ins) -> estd::enable_if_t< is_skip_state< estd::decay_t< StateT > >
Call the next reducing function in a transducer that could otherwise skip calling the next reducing f...
Definition: skip.hpp:276
auto state_unwrap(T &&s) -> decltype(state_traits_t< T >::unwrap(std::forward< T >(s)))
Convenience function for calling state_traits::unwrap
STL namespace.
typename std::decay< T >::type decay_t
Similar to C++14 std::decay_t.
Definition: type_traits.hpp:54
auto state_data(T &&s, D &&d) -> decltype(state_traits_t< T >::data(std::forward< T >(s), std::forward< D >(d)))
Convenience function for calling state_traits::data
Utility to write simple transducers easily.
auto state_complete(T &&s) -> decltype(state_traits_t< T >::complete(std::forward< T >(s)))
Convenience function for calling state_traits::complete
auto partition_by(MappingT &&mapping) -> partition_by_t< estd::decay_t< MappingT > >
Similar to clojure.core/partition-by$1.
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
auto skip(ReducingFnT &&, StateT &&state, InputTs &&...) -> skip_result_t< ReducingFnT, StateT, InputTs... >
Skip calling the next reducing function in a transducer.
Definition: skip.hpp:228
auto state_wrapper_complete(TagT, T &&s) -> decltype(state_complete(state_unwrap(std::forward< T >(s))))
Utility function for easy overloading of state_traits::complete for state wrappers with a specific ta...
Fork me on GitHub