dedupe.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/xform/skip.hpp>
35 
36 namespace atria {
37 namespace xform {
38 
39 namespace detail {
40 
41 struct dedupe_rf_gen
42 {
43  template <typename ReducingFnT>
44  struct apply
45  {
46  ReducingFnT step;
47 
48  template <typename StateT, typename ...InputTs>
49  auto operator() (StateT&& s, InputTs&& ...is)
50  -> decltype(wrap_state(step(state_unwrap(s), is...), tuplify(is...)))
51  {
52  using result_t = decltype(wrap_state(step(state_unwrap(s), is...), tuplify(is...)));
53  using complete_t = decltype(state_complete(s));
54 
55  using wrapped_t = meta::copy_decay_t<StateT, estd::decay_t<result_t>>;
56  using unwrapped_t = meta::copy_decay_t<StateT, estd::decay_t<complete_t>>;
57 
58  return with_state(
59  std::forward<StateT>(s),
60  [&] (unwrapped_t&& st) {
61  auto last = tuplify(is...);
62  return wrap_state(
63  step(state_unwrap(std::forward<unwrapped_t>(st)),
64  std::forward<InputTs>(is)...),
65  std::move(last));
66  },
67  [&] (wrapped_t&& st) {
68  auto next = tuplify(is...);
69  auto dupe = next == state_wrapper_data(st);
70  return dupe
71  ? std::forward<wrapped_t>(st)
72  : wrap_state(
73  step(state_unwrap(std::forward<wrapped_t>(st)),
74  std::forward<InputTs>(is)...),
75  std::move(next));
76  });
77  }
78  };
79 };
80 
81 } // namespace detail
82 
83 using dedupe_t = transducer_impl<detail::dedupe_rf_gen>;
84 
88 constexpr dedupe_t dedupe {};
89 
90 } // namespace xform
91 } // 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 state_unwrap(T &&s) -> decltype(state_traits_t< T >::unwrap(std::forward< T >(s)))
Convenience function for calling state_traits::unwrap
auto with_state(StateT &&st, UnwrappedFn &&, WrappedFn &&fn) -> meta::lazy_enable_if_t< !std::is_same< estd::decay_t< StateT >, estd::decay_t< decltype(state_complete(st))> >::value, std::result_of< WrappedFn(StateT)> >
Given a value st that represents the state of a reduction, this function generically dispatches to th...
Definition: with_state.hpp:58
constexpr dedupe_t dedupe
Equivalent to clojure/dedupe$0.
Definition: dedupe.hpp:88
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 wrap_state(StateT &&next, DataT &&data=DataT{}) -> state_wrapper< TagT, estd::decay_t< StateT >, estd::decay_t< DataT > >
Given a tag TagT and a state next and associated data, returns a state_wrapper instance.
Fork me on GitHub