cycle.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 
32 #include <atria/prelude/comp.hpp>
33 
34 namespace atria {
35 namespace xform {
36 
37 namespace detail {
38 
39 struct cycle_rf_gen
40 {
41  struct tag {};
42 
43  template <typename ReducingFnT,
44  typename InputRangeT>
45  struct apply
46  {
47  ReducingFnT step;
48  InputRangeT range;
49 
50  template <typename StateT, typename ...InputTs>
51  auto operator() (StateT&& s, InputTs&& ...is)
52  -> decltype(wrap_state<tag>(
53  step(state_unwrap(s), is..., *std::begin(range)),
54  std::make_tuple(std::begin(range), std::end(range))))
55  {
56  using std::get;
57  using std::begin;
58  using std::end;
59 
60  auto data = state_data(std::forward<StateT>(s), [&] {
61  auto first = begin(range);
62  auto last = end(range);
63  if (first == last)
64  throw detail::empty_transducer_error{};
65  return std::make_tuple(first, last);
66  });
67 
68  if (get<0>(data) == get<1>(data))
69  get<0>(data) = begin(range);
70 
71  return wrap_state<tag>(
72  step(state_unwrap(std::forward<StateT>(s)),
73  std::forward<InputTs>(is)...,
74  *get<0>(data)++),
75  std::move(data));
76  }
77  };
78 };
79 
80 } // namespace detail
81 
82 template <typename T>
83 using cycle_t = transducer_impl<detail::cycle_rf_gen, T>;
84 
89 template <typename InputRangeT>
90 constexpr auto cycle(InputRangeT&& r)
92 {
94  std::forward<InputRangeT>(r) };
95 }
96 
97 template <typename InputRangeT, typename ...InputRangeTs>
98 constexpr auto cycle(InputRangeT&& r, InputRangeTs&& ...rs)
100  comp(cycle(std::forward<InputRangeT>(r)),
101  cycle(std::forward<InputRangeTs>(rs))...))
102 
103 } // namespace xform
104 } // 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
auto state_unwrap(T &&s) -> decltype(state_traits_t< T >::unwrap(std::forward< T >(s)))
Convenience function for calling state_traits::unwrap
constexpr auto cycle(InputRangeT &&r) -> cycle_t< estd::decay_t< InputRangeT > >
Generator transducer produces the sequence passed as parameter, by cycling over it.
Definition: cycle.hpp:90
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
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
Fork me on GitHub