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
diff options
context:
space:
mode:
Diffstat (limited to 'search/token_slice.hpp')
-rw-r--r--search/token_slice.hpp110
1 files changed, 110 insertions, 0 deletions
diff --git a/search/token_slice.hpp b/search/token_slice.hpp
new file mode 100644
index 0000000000..87da032ce9
--- /dev/null
+++ b/search/token_slice.hpp
@@ -0,0 +1,110 @@
+#pragma once
+
+#include "search/query_params.hpp"
+
+#include "indexer/string_slice.hpp"
+
+#include "base/assert.hpp"
+
+#include "std/cstdint.hpp"
+#include "std/string.hpp"
+#include "std/vector.hpp"
+
+namespace search
+{
+class TokenSlice
+{
+public:
+ TokenSlice(QueryParams const & params, size_t startToken, size_t endToken);
+
+ inline QueryParams::TSynonymsVector const & Get(size_t i) const
+ {
+ ASSERT_LESS(i, Size(), ());
+ return m_params.GetTokens(m_offset + i);
+ }
+
+ inline size_t Size() const { return m_size; }
+
+ inline bool Empty() const { return Size() == 0; }
+
+ // Returns true if the |i|-th token in the slice is the incomplete
+ // (prefix) token.
+ bool IsPrefix(size_t i) const;
+
+ // Returns true if the |i|-th token in the slice is the last
+ // (regardless - full or not) token in the query.
+ bool IsLast(size_t i) const;
+
+private:
+ QueryParams const & m_params;
+ size_t const m_offset;
+ size_t const m_size;
+};
+
+class TokenSliceNoCategories
+{
+public:
+ TokenSliceNoCategories(QueryParams const & params, size_t startToken, size_t endToken);
+
+ inline QueryParams::TSynonymsVector const & Get(size_t i) const
+ {
+ ASSERT_LESS(i, Size(), ());
+ return m_params.GetTokens(m_indexes[i]);
+ }
+
+ inline size_t Size() const { return m_indexes.size(); }
+
+ inline bool Empty() const { return Size() == 0; }
+
+ inline bool IsPrefix(size_t i) const
+ {
+ ASSERT_LESS(i, Size(), ());
+ return m_indexes[i] == m_params.m_tokens.size();
+ }
+
+private:
+ QueryParams const & m_params;
+ vector<size_t> m_indexes;
+};
+
+class QuerySlice : public StringSliceBase
+{
+public:
+ QuerySlice(TokenSlice const & slice) : m_slice(slice) {}
+
+ // QuerySlice overrides:
+ QueryParams::TString const & Get(size_t i) const override { return m_slice.Get(i).front(); }
+ size_t Size() const override { return m_slice.Size(); }
+
+private:
+ TokenSlice const m_slice;
+};
+
+template <typename TCont>
+class QuerySliceOnRawStrings : public StringSliceBase
+{
+public:
+ QuerySliceOnRawStrings(TCont const & tokens, TString const & prefix)
+ : m_tokens(tokens), m_prefix(prefix)
+ {
+ }
+
+ // QuerySlice overrides:
+ QueryParams::TString const & Get(size_t i) const override
+ {
+ ASSERT_LESS(i, Size(), ());
+ return i == m_tokens.size() ? m_prefix : m_tokens[i];
+ }
+
+ size_t Size() const override { return m_tokens.size() + (m_prefix.empty() ? 0 : 1); }
+
+private:
+ TCont const & m_tokens;
+ TString const & m_prefix;
+};
+
+string DebugPrint(TokenSlice const & slice);
+
+string DebugPrint(TokenSliceNoCategories const & slice);
+
+} // namespace search