iter.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 iter_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  return std::make_tuple(begin(range), end(range));
62  });
63 
64  return wrap_state<tag>(
65  step(state_unwrap(std::forward<StateT>(s)),
66  std::forward<InputTs>(is)...,
67  *get<0>(data)++),
68  std::move(data));
69  }
70  };
71 
72  template <typename T>
73  friend bool state_wrapper_data_is_reduced(tag, T&& iters)
74  {
75  using std::get;
76  return get<0>(iters) == get<1>(iters);
77  }
78 };
79 
80 } // namespace detail
81 
82 template <typename T>
83 using iter_t = transducer_impl<detail::iter_rf_gen, T>;
84 
89 template <typename InputRangeT>
90 constexpr auto iter(InputRangeT&& r)
92 {
94  detail::check_non_empty(std::forward<InputRangeT>(r)) };
95 }
96 
97 template <typename InputRangeT, typename ...InputRangeTs>
98 constexpr auto iter(InputRangeT&& r, InputRangeTs&& ...rs)
100  comp(iter(std::forward<InputRangeT>(r)),
101  iter(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
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 auto iter(InputRangeT &&r) -> iter_t< estd::decay_t< InputRangeT > >
Generator transducer produces the sequence passed as parameter, by iterating over it...
Definition: iter.hpp:90
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