Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorygorshenin <mipt.vi002@gmail.com>2017-02-10 17:53:09 +0300
committerGitHub <noreply@github.com>2017-02-10 17:53:09 +0300
commit8a57be557d739a6bb4bf68608e7186368a9e7c69 (patch)
tree90a869b5d25b6bd7c784c8f094b5a3e1f0899da1 /base
parent5adc66214dfbfba1d328bc651e5e94075c9b8c0a (diff)
parentfa0d6036f342358218ef7950d6695e078c25f552 (diff)
Merge pull request #5380 from mpimenov/categories-trie
[indexer] Changed a map to a trie in CategoriesHolder.
Diffstat (limited to 'base')
-rw-r--r--base/base_tests/stl_helpers_test.cpp24
-rw-r--r--base/mem_trie.hpp2
-rw-r--r--base/stl_helpers.hpp26
3 files changed, 51 insertions, 1 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 3c8547b8ce..fdd35af365 100644
--- a/base/mem_trie.hpp
+++ b/base/mem_trie.hpp
@@ -49,7 +49,7 @@ public:
ForEachInSubtree(m_root, prefix, std::forward<ToDo>(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 <typename ToDo>
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