From b95690dcd7927babf961b48ec096b0712590ba43 Mon Sep 17 00:00:00 2001 From: Maxim Pimenov Date: Wed, 8 Feb 2017 16:21:10 +0300 Subject: [indexer] Changed a map to a trie in CategoriesHolder. Also, minor style refactorings. --- base/mem_trie.hpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'base') diff --git a/base/mem_trie.hpp b/base/mem_trie.hpp index 3c8547b8ce..354675663d 100644 --- a/base/mem_trie.hpp +++ b/base/mem_trie.hpp @@ -49,7 +49,7 @@ public: ForEachInSubtree(m_root, prefix, std::forward(toDo)); } - // Calls |toDo| for each key-value pair in a node that is reachable + // Calls |toDo| for each key-value pair in the node that is reachable // by |prefix| from the trie root. Does nothing if such node does // not exist. template @@ -59,6 +59,16 @@ public: ForEachInNode(*root, prefix, std::forward(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 + void ForEachValueInNode(String const & prefix, ToDo && toDo) const + { + if (auto const * root = MoveTo(prefix)) + ForEachValueInNode(*root, std::forward(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. @@ -126,6 +136,14 @@ private: toDo(prefix, value); } + // Calls |toDo| for each value in |node|. + template + 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|. -- cgit v1.2.3 From fa0d6036f342358218ef7950d6695e078c25f552 Mon Sep 17 00:00:00 2001 From: Maxim Pimenov Date: Wed, 8 Feb 2017 17:45:39 +0300 Subject: Review fixes. --- base/base_tests/stl_helpers_test.cpp | 24 ++++++++++++++++++++++++ base/mem_trie.hpp | 18 ------------------ base/stl_helpers.hpp | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 18 deletions(-) (limited to 'base') 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(); TestSortUnique(); } + +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)); } - // 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 - void ForEachValueInNode(String const & prefix, ToDo && toDo) const - { - if (auto const * root = MoveTo(prefix)) - ForEachValueInNode(*root, std::forward(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 - 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 #include +#include #include #include @@ -137,4 +138,29 @@ typename std::underlying_type::type Key(T value) { return static_cast::type>(value); } + +// Use this if you want to make a functor whose first +// argument is ignored and the rest are forwarded to |fn|. +template +class IgnoreFirstArgument +{ +public: + template + IgnoreFirstArgument(Gn && gn) : m_fn(std::forward(gn)) {} + + template + typename std::result_of::type operator()(Arg && arg, Args &&... args) + { + return m_fn(std::forward(args)...); + } + +private: + Fn m_fn; +}; + +template +IgnoreFirstArgument MakeIgnoreFirstArgument(Fn && fn) +{ + return IgnoreFirstArgument(std::forward(fn)); +} } // namespace my -- cgit v1.2.3