/
binary_sa_search.h
68 lines (68 loc) · 1.47 KB
/
binary_sa_search.h
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
#ifndef BINARY_SA_SEARCH_H_
#define BINARY_SA_SEARCH_H_
#include <stdint.h>
#include <iostream>
#include <limits>
#include "alphabet.h"
#include "assert_helpers.h"
#include "ds.h"
#include "btypes.h"
template <typename TStr, typename TSufElt>
inline TIndexOffU binarySASearch(
const TStr &host,
TIndexOffU qry,
const EList<TSufElt> &sa)
{
TIndexOffU lLcp = 0, rLcp = 0;
TIndexOffU l = 0, r = (TIndexOffU)sa.size() + 1;
TIndexOffU hostLen = (TIndexOffU)host.length();
while (true)
{
assert_gt(r, l);
TIndexOffU m = (l + r) >> 1;
if (m == l)
{
if (m > 0 && sa[m - 1] == qry)
{
return std::numeric_limits<TIndexOffU>::max();
}
assert_leq(m, sa.size());
return m;
}
assert_gt(m, 0);
TIndexOffU suf = sa[m - 1];
if (suf == qry)
{
return std::numeric_limits<TIndexOffU>::max();
}
TIndexOffU lcp = min(lLcp, rLcp);
#ifndef NDEBUG
if (sstr_suf_upto_neq(host, qry, host, suf, lcp))
{
assert(0);
}
#endif
while (suf + lcp < hostLen && qry + lcp < hostLen && host[suf + lcp] == host[qry + lcp])
{
lcp++;
}
bool fell = (suf + lcp == hostLen || qry + lcp == hostLen);
if ((fell && qry + lcp == hostLen) || (!fell && host[suf + lcp] < host[qry + lcp]))
{
l = m;
lLcp = max(lLcp, lcp);
}
else if ((fell && suf + lcp == hostLen) || (!fell && host[suf + lcp] > host[qry + lcp]))
{
r = m;
rLcp = max(rLcp, lcp);
}
else
{
assert(false);
}
}
assert(false);
return std::numeric_limits<TIndexOffU>::max();
}
#endif