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:
authorMaxim Pimenov <m@maps.me>2017-02-13 17:40:58 +0300
committerMaxim Pimenov <m@maps.me>2017-02-13 19:10:37 +0300
commit5ddc8db2baa8f158f5ed84c09af550f52e50a1ce (patch)
tree8b9dc1649433f36571b622124af8812b70b221e0 /base
parent8808823db18c6dcd4842681f0b5765361e337a93 (diff)
Review fixes.
Diffstat (limited to 'base')
-rw-r--r--base/mem_trie.hpp48
-rw-r--r--base/string_utils.cpp11
-rw-r--r--base/string_utils.hpp11
3 files changed, 40 insertions, 30 deletions
diff --git a/base/mem_trie.hpp b/base/mem_trie.hpp
index d5394f5bf3..6bd161eed9 100644
--- a/base/mem_trie.hpp
+++ b/base/mem_trie.hpp
@@ -3,6 +3,7 @@
#include "base/macros.hpp"
#include "base/stl_add.hpp"
+#include <algorithm>
#include <cstdint>
#include <map>
#include <memory>
@@ -32,25 +33,28 @@ public:
return *this;
}
- // A read-only iterator. Any modification to the
+ // A read-only iterator wrapping a Node. Any modification to the
// underlying trie is assumed to invalidate the iterator.
class Iterator
{
public:
Iterator(MemTrie::Node const & node) : m_node(node) {}
+ // Iterates over all possible moves from this Iterator's node
+ // and calls |toDo| with two arguments:
+ // (Char of the move, Iterator wrapping the node of the move).
template <typename ToDo>
- void ForEachMove(ToDo && todo) const
+ void ForEachMove(ToDo && toDo) const
{
for (auto const & move : m_node.m_moves)
- todo(move.first, Iterator(*move.second));
+ toDo(move.first, Iterator(*move.second));
}
+ // Calls |toDo| for every value in this Iterator's node.
template <typename ToDo>
- void ForEachInNode(ToDo && todo) const
+ void ForEachInNode(ToDo && toDo) const
{
- for (auto const & value : m_node.m_values)
- todo(value);
+ std::for_each(m_node.m_values.begin(), m_node.m_values.end(), std::forward<ToDo>(toDo));
}
private:
@@ -71,32 +75,32 @@ public:
cur->AddValue(value);
}
- // Traverses all key-value pairs in the trie and calls |todo| on each of them.
+ // Traverses all key-value pairs in the trie and calls |toDo| on each of them.
template <typename ToDo>
- void ForEachInTrie(ToDo && todo) const
+ void ForEachInTrie(ToDo && toDo) const
{
String prefix;
- ForEachInSubtree(m_root, prefix, std::forward<ToDo>(todo));
+ ForEachInSubtree(m_root, prefix, std::forward<ToDo>(toDo));
}
- // Calls |todo| for each key-value pair in the 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>
- void ForEachInNode(String const & prefix, ToDo && todo) const
+ void ForEachInNode(String const & prefix, ToDo && toDo) const
{
if (auto const * root = MoveTo(prefix))
- ForEachInNode(*root, prefix, std::forward<ToDo>(todo));
+ ForEachInNode(*root, prefix, std::forward<ToDo>(toDo));
}
- // Calls |todo| for each key-value pair in a subtree that is
+ // 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.
template <typename ToDo>
- void ForEachInSubtree(String prefix, ToDo && todo) const
+ void ForEachInSubtree(String prefix, ToDo && toDo) const
{
if (auto const * root = MoveTo(prefix))
- ForEachInSubtree(*root, prefix, std::forward<ToDo>(todo));
+ ForEachInSubtree(*root, prefix, std::forward<ToDo>(toDo));
}
size_t GetNumNodes() const { return m_numNodes; }
@@ -149,27 +153,27 @@ private:
return cur;
}
- // Calls |todo| for each key-value pair in a |node| that is
+ // Calls |toDo| for each key-value pair in a |node| that is
// reachable by |prefix| from the trie root.
template <typename ToDo>
- void ForEachInNode(Node const & node, String const & prefix, ToDo && todo) const
+ void ForEachInNode(Node const & node, String const & prefix, ToDo && toDo) const
{
for (auto const & value : node.m_values)
- todo(prefix, value);
+ toDo(prefix, value);
}
- // Calls |todo| for each key-value pair in subtree where |node| is a
+ // 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|.
template <typename ToDo>
- void ForEachInSubtree(Node const & node, String & prefix, ToDo && todo) const
+ void ForEachInSubtree(Node const & node, String & prefix, ToDo && toDo) const
{
- ForEachInNode(node, prefix, todo);
+ ForEachInNode(node, prefix, toDo);
for (auto const & move : node.m_moves)
{
prefix.push_back(move.first);
- ForEachInSubtree(*move.second, prefix, todo);
+ ForEachInSubtree(*move.second, prefix, toDo);
prefix.pop_back();
}
}
diff --git a/base/string_utils.cpp b/base/string_utils.cpp
index d1ac84b24a..f2546161fb 100644
--- a/base/string_utils.cpp
+++ b/base/string_utils.cpp
@@ -237,19 +237,14 @@ bool IsASCIIString(std::string const & str)
bool IsASCIIDigit(UniChar c) { return c >= '0' && c <= '9'; }
bool IsASCIILatin(UniChar c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); }
+
bool StartsWith(UniString const & s, UniString const & p)
{
- if (p.size() > s.size())
- return false;
- for (size_t i = 0; i < p.size(); ++i)
- {
- if (s[i] != p[i])
- return false;
- }
- return true;
+ return StartsWith(s.begin(), s.end(), p.begin(), p.end());
}
bool StartsWith(std::string const & s1, char const * s2) { return (s1.compare(0, strlen(s2), s2) == 0); }
+
bool EndsWith(std::string const & s1, char const * s2)
{
size_t const n = s1.size();
diff --git a/base/string_utils.hpp b/base/string_utils.hpp
index af8670c5a4..8fc2228b40 100644
--- a/base/string_utils.hpp
+++ b/base/string_utils.hpp
@@ -439,6 +439,17 @@ std::string to_string_dac(double d, int dac);
inline std::string to_string_with_digits_after_comma(double d, int dac) { return to_string_dac(d, dac); }
//@}
+template <typename IterT1, typename IterT2>
+bool StartsWith(IterT1 beg, IterT1 end, IterT2 begPrefix, IterT2 endPrefix)
+{
+ while (beg != end && begPrefix != endPrefix && *beg == *begPrefix)
+ {
+ ++beg;
+ ++begPrefix;
+ }
+ return begPrefix == endPrefix;
+}
+
bool StartsWith(UniString const & s, UniString const & p);
bool StartsWith(std::string const & s1, char const * s2);