diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/base_tests/stl_helpers_test.cpp | 24 | ||||
-rw-r--r-- | base/mem_trie.hpp | 18 | ||||
-rw-r--r-- | base/stl_helpers.hpp | 26 |
3 files changed, 50 insertions, 18 deletions
diff --git a/base/base_tests/stl_helpers_test.cpp b/base/base_tests/stl_helpers_test.cpp index 029df6af98..71462cfbaf 100644 --- a/base/base_tests/stl_helpers_test.cpp +++ b/base/base_tests/stl_helpers_test.cpp @@ -125,4 +125,28 @@ UNIT_TEST(SortUnique_VectorTest) TestSortUnique<vector>(); TestSortUnique<deque>(); } + +UNIT_TEST(IgnoreFirstArgument) +{ + { + int s = 0; + auto f1 = [&](int a, int b) { s += a + b; }; + auto f2 = [&](int a, int b) { s -= a + b; }; + auto f3 = my::MakeIgnoreFirstArgument(f2); + + f1(2, 3); + TEST_EQUAL(s, 5, ()); + f3(1, 2, 3); + TEST_EQUAL(s, 0, ()); + } + + { + auto f1 = [](int a, int b) -> int { return a + b; }; + auto f2 = my::MakeIgnoreFirstArgument(f1); + + auto const x = f1(2, 3); + auto const y = f2("ignored", 2, 3); + TEST_EQUAL(x, y, ()); + } +} } // namespace diff --git a/base/mem_trie.hpp b/base/mem_trie.hpp index 354675663d..fdd35af365 100644 --- a/base/mem_trie.hpp +++ b/base/mem_trie.hpp @@ -59,16 +59,6 @@ public: ForEachInNode(*root, prefix, std::forward<ToDo>(toDo)); } - // Calls |toDo| for each value in the node that is reachable - // by |prefix| from the trie root. Does nothing if such node does - // not exist. - template <typename ToDo> - void ForEachValueInNode(String const & prefix, ToDo && toDo) const - { - if (auto const * root = MoveTo(prefix)) - ForEachValueInNode(*root, std::forward<ToDo>(toDo)); - } - // Calls |toDo| for each key-value pair in a subtree that is // reachable by |prefix| from the trie root. Does nothing if such // subtree does not exist. @@ -136,14 +126,6 @@ private: toDo(prefix, value); } - // Calls |toDo| for each value in |node|. - template <typename ToDo> - void ForEachValueInNode(Node const & node, ToDo && toDo) const - { - for (auto const & value : node.m_values) - toDo(value); - } - // Calls |toDo| for each key-value pair in subtree where |node| is a // root of the subtree. |prefix| is a path from the trie root to the // |node|. diff --git a/base/stl_helpers.hpp b/base/stl_helpers.hpp index 2130dd03d7..5ee22b6897 100644 --- a/base/stl_helpers.hpp +++ b/base/stl_helpers.hpp @@ -2,6 +2,7 @@ #include <algorithm> #include <functional> +#include <type_traits> #include <utility> #include <vector> @@ -137,4 +138,29 @@ typename std::underlying_type<T>::type Key(T value) { return static_cast<typename std::underlying_type<T>::type>(value); } + +// Use this if you want to make a functor whose first +// argument is ignored and the rest are forwarded to |fn|. +template <typename Fn> +class IgnoreFirstArgument +{ +public: + template <typename Gn> + IgnoreFirstArgument(Gn && gn) : m_fn(std::forward<Gn>(gn)) {} + + template <typename Arg, typename... Args> + typename std::result_of<Fn(Args &&...)>::type operator()(Arg && arg, Args &&... args) + { + return m_fn(std::forward<Args>(args)...); + } + +private: + Fn m_fn; +}; + +template <typename Fn> +IgnoreFirstArgument<Fn> MakeIgnoreFirstArgument(Fn && fn) +{ + return IgnoreFirstArgument<Fn>(std::forward<Fn>(fn)); +} } // namespace my |