distinct.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 
36 #include <unordered_set>
37 
38 namespace atria {
39 namespace xform {
40 
41 namespace detail {
42 
43 struct distinct_rf_gen
44 {
45  template <typename ReducingFnT>
46  struct apply
47  {
48  ReducingFnT step;
49 
50  template <typename ...InputTs>
51  using cache_t = std::unordered_set<
52  estd::decay_t<decltype(tuplify(std::declval<InputTs>()...))> >;
53 
54  template <typename StateT, typename ...InputTs>
55  auto operator() (StateT&& s, InputTs&& ...is)
56  -> decltype(wrap_state(step(state_unwrap(s), is...), cache_t<InputTs...>{}))
57  {
58  using result_t = decltype(wrap_state(step(state_unwrap(s), is...), cache_t<InputTs...>{}));
59  using complete_t = decltype(state_complete(s));
60 
61  using wrapped_t = meta::copy_decay_t<StateT, estd::decay_t<result_t>>;
62  using unwrapped_t = meta::copy_decay_t<StateT, estd::decay_t<complete_t>>;
63 
64  return with_state(
65  std::forward<StateT>(s),
66  [&] (unwrapped_t&& st) {
67  return wrap_state(
68  step(state_unwrap(std::forward<unwrapped_t>(st)),
69  std::forward<InputTs>(is)...),
70  cache_t<InputTs...>{ tuplify(is...) });
71  },
72  [&] (wrapped_t st) {
73  auto& cache = state_wrapper_data(st);
74  auto unique = cache.insert(tuplify(is...)).second;
75  return !unique
76  ? st
77  : wrap_state(
78  step(std::move(state_unwrap(st)),
79  std::forward<InputTs>(is)...),
80  std::move(cache));
81  });
82  }
83  };
84 };
85 
86 } // namespace detail
87 
88 using distinct_t = transducer_impl<detail::distinct_rf_gen>;
89 
93 constexpr distinct_t distinct {};
94 
95 } // namespace xform
96 } // 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
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.
constexpr distinct_t distinct
Equivalent to clojure/distinct$0.
Definition: distinct.hpp:93
Fork me on GitHub