blob: 37a412ed15a7ee3772fdf37297e250bc7dfc3ffa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
#pragma once
#include "search/query_params.hpp"
#include "search/token_range.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, TokenRange const & range);
inline QueryParams::Token const & Get(size_t i) const
{
ASSERT_LESS(i, Size(), ());
return m_params.GetToken(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;
private:
QueryParams const & m_params;
size_t const m_offset;
size_t const m_size;
};
class TokenSliceNoCategories
{
public:
TokenSliceNoCategories(QueryParams const & params, TokenRange const & range);
inline QueryParams::Token const & Get(size_t i) const
{
ASSERT_LESS(i, Size(), ());
return m_params.GetToken(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 { return m_params.IsPrefixToken(m_indexes[i]); }
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::String const & Get(size_t i) const override { return m_slice.Get(i).m_original; }
size_t Size() const override { return m_slice.Size(); }
private:
TokenSlice const m_slice;
};
template <typename Cont>
class QuerySliceOnRawStrings : public StringSliceBase
{
public:
QuerySliceOnRawStrings(Cont const & tokens, String const & prefix)
: m_tokens(tokens), m_prefix(prefix)
{
}
bool HasPrefixToken() const { return !m_prefix.empty(); }
// QuerySlice overrides:
QueryParams::String 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:
Cont const & m_tokens;
QueryParams::String const & m_prefix;
};
string DebugPrint(TokenSlice const & slice);
string DebugPrint(TokenSliceNoCategories const & slice);
} // namespace search
|