-
Notifications
You must be signed in to change notification settings - Fork 1
/
ST.h
executable file
·283 lines (257 loc) · 7.96 KB
/
ST.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Copyright (C) 2019 Thanasis Vergoulis
*
* This file is part of GeSuTr.
*
* GeSuTr is a free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GeSuTr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Contact email: vergoulis@athenarc.gr
*/
#ifndef ST_H
#define ST_H
#include <iostream>
#include <string>
#include <vector>
#include "STnode.h"
using namespace std;
/**
* The class represented the (generalised) suffix tree.
*
* @var _strs A registry (vector) containing all strings to be inserted in the tree.
* @var _st_root Pointer to the root node of the tree.
*
* @author Thanasis Vergoulis
*/
class ST
{
public:
/**
* An empty class constructor. It just initialises the root to NULL.
*
* @author Thanasis Vergoulis
*/
ST();
/**
* The class constrcutor. It constracts the tree for a given set of strings.
*
* @param strs Array containing the strings to be included in the tree.
* @param strs_num The number of strings in strs.
*
* @author Thanasis Vergoulis
*/
ST(string* strs, long strs_num);
/**
* The class destructor.
*/
~ST();
/**
* Inserts a given string in the suffix tree using the naive construction method (i.e., not Ukkonen's).
*
* @param str The string to be inserted.
*
* @return It returns 0 on success.
*
* @author Thanasis Vergoulis
*/
int strInsertNaive(string str);
/**
* Inserts one-by-one the strings contained in a given array to the tree. It calls strInsertNaive() multiple times.
*
* @param strs The array of strings to be inserted.
* @param strs_num The number of strings in strs.
*
* @return It returns 0 on success.
*
* @author Thanasis Vergoulis
*/
int strInsertNaive(string* strs, long strs_num);
/**
* Inserts a suffix of the str_id-th string of the registry in the tree.
*
* @param str_id The id of the string in the registry.
* @param suf_start The start position of the suffix in the string.
* @param suf_end The end position of the suffix in the string.
*
* @return It returns 0 on success.
*/
int insertSuffix(long str_id, long suf_start, long suf_end);
/**
* Inserts a given string in the registry (vector) of strings.
*
* @param str The string to be registered.
*
* @return The index of the str in the registry (vector).
*
* @author Thanasis Vergoulis
*/
int strRegister(string str);
/**
* Batch insertion of given strings in the registry (vector) of strings.
*
* @param strs Array of strings to be registered.
* @param strs_num The number of strings to be registered.
*
* @return Zero in case of success.
*
* @author Thanasis Vergoulis
*/
int strRegister(string* strs, int strs_num);
/**
* Prints the contents of the string registry.
*
* @author Thanasis Vergoulis
*/
void printRegistry(void);
/**
* Use str to go down from a given (initial) node. After the execution, param 'node' (is accessed by
* reference) will point:
* (a) to the initial node (if no outgoing edge matching a prefix of str was found), or
* (b) to the end-node of the matching outgoing edge (the end-node is a child of the init node)
*
* @param node Pointer to the initial node (by reference).
* @param str The string used for the traversal.
*
* @return The number of matching characters between the prefix of str and the matching edge (if any).
*
* @author Thanasis Vergoulis
*/
long traverseNodeNaive( STnode*& node, string str);
/**
* Use str to go down from a given (initial) node. After the execution, param 'node' (is accessed by
* reference) will point:
* (a) to the initial node (if no outgoing edge matching a prefix of str was found), or
* (b) to the end-node of the matching outgoing edge (the end-node is a child of the init node)
* This is a variant of the basic function that also return the left sibling of the node.
*
* @param node Pointer to the initial node (by reference).
* @param str The string used for the traversal.
* @param pre_node Pointer to the left sibling of the node (by reference).
*
* @return The number of matching characters between the prefix of str and the matching edge (if any).
*
* @author Thanasis Vergoulis
*/
long traverseNodeNaive( STnode*& node, string str, STnode*& pre_node);
/**
* Finds a child's node having label of incoming edge starting with a given character.
*
* @param node Pointer to the initial node.
* @param character The given character.
*
* @return Pointer to the child node that has the desired property. NULL pointer otherwise.
*
* @author Thanasis Vergoulis
*/
STnode* findChildByStr(STnode* node, char character);
/**
* Finds a child's node having label of incoming edge starting with a given character.
* This variant also returns (based on a parameter by reference) the previous child of
* the node (i.e., the one to the left of the returned node). This needed for particular
* use in another place of the code.
*
* @param node Pointer to the initial node.
* @param character The given character.
* @param pre_node The sibling at the left of the node (call by reference).
*
* @return Pointer to the child node that has the desired property. NULL pointer otherwise.
*
* @author Thanasis Vergoulis
*/
STnode* findChildByStr(STnode* node, char character, STnode*& pre_node);
/**
* Returns a pointer to a set of positions in which a particular query string occurs (exact occurrence).
*
* @param str The query string.
* @param occs A vector containing all occurrences (call by reference).
*
* @author Thanasis Vergoulis
*/
void findStr(string str, vector<OccPos*>& occs);
/**
* Returns a particular character from the label of the incoming edge of a given node.
*
* @param node The node.
* @param char_id The id of the character (e.g., 1st, 2nd etc).
*
* @return The corresponding character.
*
* @author Thanasis Vergoulis
*/
char getLabelChar(STnode* node,long char_id);
/**
* Returns the input edge's label of a given node.
*
* @param node The node.
*
* @return The label.
*
* @author Thanasis Vergoulis
*/
string getLabel(STnode* node);
/**
* Prints the suffix tree.
*
* @author Thanasis Vergoulis
*/
void print();
/**
* Supporting function to the print() function. It prints info about a node's children.
*
* @param src_node The node for the children of which
* @param depth The depth of the node.
*
* @author Thanasis Vergoulis
*/
void printNode(STnode* src_node, long depth);
/**
* Updates the value of _occ_num for all nodes of the tree.
*
* @author Thanasis Vergoulis
*/
void updNodeCnts();
/**
* Updates the value of _occ_num for a given node.
*
* @param cur_node The node.
*
* @author Thanasis Vergoulis
*/
void updNodeCnt(STnode* cur_node);
/**
* Returns the string in the registry having as identifier str_id.
*
* @param str_id The identifier of the string in the registry.
*
* @return The string.
*
* @author Thanasis Vergoulis
*/
string getRegStr(long str_id);
/**
* Returns a substring of a string in the registry.
*
* @param str_id The identifier of the string in the registry.
* @param str_start The starting point of the substring.
* @param str_len The length of the substring.
*
* @return The substring.
*
* @author Thanasis Vergoulis
*/
string getRegSubStr(long str_id, long str_start, long str_len);
private:
//All variables are explained in the javadoc comments of the STnode class.
vector<string> _strs;
STnode* _st_root;
};
#endif /* ST_H*/