eager.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 
34 #include <vector>
35 
36 #include <ableton/build_system/Warnings.hpp>
37 ABL_DISABLE_WARNINGS
38 #include <boost/range/iterator_range.hpp>
39 ABL_RESTORE_WARNINGS
40 #include <functional>
41 
42 namespace atria {
43 namespace xform {
44 
45 namespace detail {
46 
47 struct eager_rf_gen
48 {
49  struct tag {};
50 
51  template <typename ReducingFnT,
52  typename MappingT>
53  struct apply
54  {
55  ReducingFnT step;
56  MappingT mapping;
57 
58  template <typename ...InputTs>
59  using container_t = std::vector<
61  decltype(tuplify(std::declval<InputTs>()...))> >;
62 
63  template <typename ...InputTs>
64  using data_t = std::tuple<
65  container_t<InputTs...>,
66  std::reference_wrapper<apply> >;
67 
68  template <typename StateT, typename ...InputTs>
69  auto operator() (StateT&& s, InputTs&& ...is)
70  -> decltype(
71  wrap_state<tag>(
72  state_unwrap(std::forward<StateT>(s)),
73  std::declval<data_t<InputTs...>>()))
74  {
75  auto data = state_data(std::forward<StateT>(s), [&] {
76  return data_t<InputTs...>(container_t<InputTs...>{}, *this);
77  });
78 
79  std::get<0>(data).push_back(tuplify(std::forward<InputTs>(is)...));
80 
81  return wrap_state<tag> (
82  state_unwrap(std::forward<StateT>(s)),
83  std::move(data));
84  }
85  };
86 
87  template <typename T>
88  friend auto state_wrapper_complete(tag, T&& wrapper)
90  identity_(
93  std::get<1>(state_wrapper_data(std::forward<T>(wrapper))).get().step,
94  state_unwrap(std::forward<T>(wrapper)),
95  std::get<1>(state_wrapper_data(std::forward<T>(wrapper))).get().mapping(
96  std::get<0>(state_wrapper_data(std::forward<T>(wrapper))))))))
97 };
98 
99 } // namespace detail
100 
101 template <typename T>
102 using eager_t = transducer_impl<detail::eager_rf_gen, T>;
103 
107 template <typename MappingT>
108 auto eager(MappingT&& mapping)
110 {
111  return eager_t<estd::decay_t<MappingT> > { mapping };
112 }
113 
114 namespace detail {
115 
116 struct sorted_range_t
117 {
118  template <typename RangeT>
119  auto operator() (RangeT&& range) -> RangeT&&
120  {
121  std::sort(range.begin(), range.end());
122  return std::forward<RangeT>(range);
123  }
124 };
125 
126 } // namespace
127 
128 using sorted_t = eager_t<detail::sorted_range_t>;
129 
134 constexpr sorted_t sorted {};
135 
136 namespace detail {
137 
138 struct reversed_range_t
139 {
140  template <typename RangeT>
141  auto operator() (RangeT&& range)
143  boost::make_iterator_range(
144  range.rbegin(),
145  range.rend()))
146 };
147 
148 } // namespace
149 
150 using reversed_t = eager_t<detail::reversed_range_t>;
151 
156 constexpr reversed_t reversed {};
157 
158 } // namespace xform
159 } // 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...
#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
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
constexpr auto range(StopT &&stop) -> decltype(comp( count(), take(std::forward< StopT >(stop))))
Generator transducer version of Python range
Definition: range.hpp:42
constexpr reversed_t reversed
Eager transducer that reverses the input sequence.
Definition: eager.hpp:156
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 reduce_nested(ReducingFnT &&step, StateT &&state, InputRangeTs &&...ranges) -> decltype(detail::is_non_empty(ranges...) ?call(detail::reduce_nested_non_empty_flipped, std::forward< StateT >(state), std::forward< ReducingFnT >(step), std::forward< InputRangeTs >(ranges)...) :skip(std::forward< StateT >(state)))
Similar to reduce, but does not unwrap reduced values.
auto eager(MappingT &&mapping) -> eager_t< estd::decay_t< MappingT > >
Similar to clojure.core/eager-all$1.
Definition: eager.hpp:108
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
constexpr sorted_t sorted
Eager transducer that sorts the input sequence.
Definition: eager.hpp:134
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