partition.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_rf_gen
42 {
43  struct tag {};
44 
45  template <typename ReducingFnT,
46  typename IntegralT>
47  struct apply
48  {
49  ReducingFnT step;
50  IntegralT size;
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(container_t<InputTs...>{}, step)))
63  {
64  auto data = state_data(std::forward<StateT>(s), [&] {
65  auto v = container_t<InputTs...>{};
66  v.reserve(size);
67  return make_tuple(std::move(v), step);
68  });
69 
70  auto& next_vector = std::get<0>(data);
71  auto& next_step = std::get<1>(data);
72 
73  next_vector.push_back(tuplify(std::forward<InputTs>(is)...));
74  const auto complete_group = next_vector.size() == size;
75 
76  auto next_state = complete_group
77  ? call(step, state_unwrap(std::forward<StateT>(s)), next_vector)
78  : skip(step, state_unwrap(std::forward<StateT>(s)), next_vector);
79 
80  if (complete_group)
81  next_vector.clear();
82 
83  return wrap_state<tag> (
84  std::move(next_state),
85  make_tuple(std::move(next_vector),
86  std::move(next_step)));
87  }
88  };
89 
90  template <typename T>
91  friend auto state_wrapper_complete(tag, T&& wrapper)
92  -> estd::decay_t<decltype(
93  state_complete(state_unwrap(std::forward<T>(wrapper))))>
94  {
95  auto next = std::get<0>(state_wrapper_data(std::forward<T>(wrapper)));
96  auto step = std::get<1>(state_wrapper_data(std::forward<T>(wrapper)));
97  return state_complete(
98  next.empty()
99  ? state_unwrap(std::forward<T>(wrapper))
100  : step(state_unwrap(std::forward<T>(wrapper)),
101  std::move(next)));
102  }
103 };
104 
105 } // namespace detail
106 
107 template <typename T>
108 using partition_t = transducer_impl<detail::partition_rf_gen, T>;
109 
113 template <typename IntegralT>
114 auto partition(IntegralT&& n)
116 {
118 }
119 
120 } // namespace xform
121 } // 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
auto partition(IntegralT &&n) -> partition_t< estd::decay_t< IntegralT > >
Similar to clojure.core/partition-all$1.
Definition: partition.hpp:114
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
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