replace.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 
31 #include <atria/meta/utils.hpp>
32 #include <functional>
33 
34 namespace atria {
35 namespace xform {
36 
37 namespace detail {
38 
39 template <typename TableT>
40 struct lookup_or_key
41 {
42  TableT table;
43 
44  template <typename KeyT>
45  auto operator() (const KeyT& k) -> const KeyT&
46  {
47  auto elem = table.find(k);
48  return elem == table.end() ? k : elem->second;
49  }
50 
51  template <typename K1T, typename K2T, typename... KTs>
52  auto operator() (K1T&& k1, K2T&& k2, KTs&&... ks)
53  -> std::tuple<estd::decay_t<K1T>,
54  estd::decay_t<K2T>,
55  estd::decay_t<KTs>...>
56  {
57  auto key = std::make_tuple(std::forward<K1T>(k1),
58  std::forward<K2T>(k2),
59  std::forward<KTs>(ks)...);
60  auto elem = table.find(key);
61  return elem == table.end() ? key : elem->second;
62  }
63 };
64 
65 template <typename TableT>
66 struct lookup_default
67 {
68  TableT table;
69 
70  template <typename... KeyTs>
71  auto operator() (KeyTs&& ...ks)
73  table[tuplify(std::forward<KeyTs>(ks)...)])
74 };
75 
76 template <typename TableT>
77 struct lookup_safe
78 {
79  TableT table;
80 
81  template <typename... KeyTs>
82  auto operator() (KeyTs&& ...ks)
84  table.at(tuplify(std::forward<KeyTs>(ks)...)))
85 };
86 
87 } // namespace detail
88 
92 template <typename TableT>
93 auto replace(TableT&& table)
95  map(detail::lookup_or_key<estd::decay_t<TableT> >{ table }))
96 
97 
100 template <typename TableT>
101 auto replace_all(TableT&& table)
103  map(detail::lookup_default<estd::decay_t<TableT> >{ table }))
104 
108 template <typename TableT>
109 auto replace_all_safe(TableT&& table)
111  map(detail::lookup_safe<estd::decay_t<TableT> >{ table }))
112 
113 } // namespace xform
114 } // 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 replace_all(TableT &&table) -> decltype(map(detail::lookup_default< estd::decay_t< TableT > >
Transducer that replaces all elements by table[tuplify(inputs)]
Definition: replace.hpp:101
typename std::decay< T >::type decay_t
Similar to C++14 std::decay_t.
Definition: type_traits.hpp:54
auto replace_all_safe(TableT &&table) -> decltype(map(detail::lookup_safe< estd::decay_t< TableT > >
Transducer that replaces all elements by table.at(tuplify(inputs))
Definition: replace.hpp:109
C++ amazing templates and reusable implementations awesomeness.
Definition: _doc.hpp:35
auto map(MappingT &&mapping) -> map_t< estd::decay_t< MappingT > >
Similar to clojure.core/map$1.
Definition: map.hpp:79
auto replace(TableT &&table) -> decltype(map(detail::lookup_or_key< estd::decay_t< TableT > >
Similar to clojure.core/replace$1.
Definition: replace.hpp:93
Fork me on GitHub