-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTBohr.cpp
118 lines (93 loc) · 2.59 KB
/
TBohr.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
#include "TBohr.h"
#include <queue>
TBohr::TBohr() :
patternSize(0), pieces(0) {
root = new TBohrItem;
root->sufflink = root;
}
TBohrItem* TBohr::Move(TBohrItem* item, TLetter letter) {
try {
item = item->path.at(letter);
} catch (std::out_of_range&) {
if (item == root)
item = root;
else
item = Move(item->sufflink, letter);
}
return item;
}
TBohrItem* TBohr::Next(TBohrItem* item, TLetter letter) {
if (!item)
return nullptr;
try {
item = item->path.at(letter);
} catch (std::out_of_range&) {
item = nullptr;
}
return item;
}
void TBohr::Linkate() {
TBohrItem *node, *child;
std::queue<TBohrItem *> queue;
queue.push(root);
while (!queue.empty()) {
node = queue.front();
queue.pop();
std::map<TLetter, TBohrItem *>::iterator childPairIt;
for (childPairIt = node->path.begin(); childPairIt != node->path.end();
++childPairIt) {
child = childPairIt->second;
queue.push(child);
child->sufflink = FindSufflink(child, node, childPairIt->first);
child->success.insert(child->success.end(),
child->sufflink->success.begin(),
child->sufflink->success.end());
child->success.shrink_to_fit();
}
}
}
TBohrItem* TBohr::FindSufflink(TBohrItem* child, TBohrItem* parent,
const TLetter letter) {
TBohrItem *linkup = parent->sufflink, *check;
while (true) {
check = Next(linkup, letter);
if (check)
return (check != child) ? check : root;
if (linkup == root)
return root;
linkup = linkup->sufflink;
}
}
void TBohr::Push(const std::vector<TLetter>& str) {
TBohrItem *bohr = root, *next;
for (size_t i = 0; i < str.size(); ++i) {
next = Next(bohr, str[i]);
if (!next) {
next = new TBohrItem;
next->sufflink = root;
bohr->path.insert(std::pair<TLetter, TBohrItem*>(str[i], next));
}
bohr = next;
}
bohr->success.push_back(pieces);
pieces++;
}
void TBohr::Search(std::vector<TLetter>& text,
std::vector<std::pair<int, int> >& grid) {
Linkate();
int m = text.size();
TBohrItem* node = root;
int occurrence;
int c;
for (c = 0; c < m; ++c) {
for (size_t i = 0; i < node->success.size(); ++i) {
occurrence = c - pieceIndex[node->success[i]] ;
std::cout << grid[occurrence].first << ", " << grid[occurrence].second << ", " << node->success[i] + 1 << std::endl;
}
node = Move(node, text[c]);
}
for (size_t i = 0; i < node->success.size(); ++i) {
occurrence = c - pieceIndex[node->success[i]] ;
std::cout << grid[occurrence].first << ", " << grid[occurrence].second << ", " << node->success[i] + 1 << std::endl;
}
}