-
Notifications
You must be signed in to change notification settings - Fork 1
/
STnode.cpp
executable file
·250 lines (212 loc) · 4.88 KB
/
STnode.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
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
/* 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
*/
#include "STnode.h"
OccPos::OccPos(long str_id, long occ_pos, OccPos* next_pos)
{
this->_str_id = str_id;
this->_occ_pos = occ_pos;
this->_next_pos = next_pos;
}
STnode::STnode(STnode* parent, long str_id, long start, long end)
{
this->_str_id = str_id;
this->_children = NULL; //no children
this->_right_sibling = NULL; //no right sibling
this->_parent = parent; //get the node's parent in the tree
this->_in_label_start = start; //get the start of the incoming edge's label (based on str_id string)
this->_in_label_end = end; //get the end of the incoming edge's label (based on str_id string)
this->_occs_num = 0;
this->_occ_positions = NULL;
}
STnode::~STnode(void)
{
}
void STnode::addChild(STnode* ch)
{
if( this->_children == NULL) //this will be the first child
{
this->_children = ch;
}
else
{
STnode* cur_child = this->_children;
while( cur_child->_right_sibling != NULL ) //browse until you find one child with no right siblings (i.e., the last one)
{
cur_child = cur_child->_right_sibling;
}
cur_child->_right_sibling = ch; //add the child, as the rightmost
}
}
bool STnode::isLeaf()
{
if( this->_occ_positions==NULL )
return false;
else
return true;
}
STnode* STnode::getParent(void)
{
return this->_parent;
}
void STnode::setParent(STnode* parent)
{
this->_parent = parent;
}
STnode* STnode::getChildren()
{
return this->_children;
}
void STnode::setChildren(STnode* ch)
{
_children = ch;
}
STnode* STnode::getRightSibling(void)
{
return _right_sibling;
}
void STnode::setRightSibling(STnode* right_sib)
{
_right_sibling = right_sib;
}
long STnode::getInLabelStart()
{
return this->_in_label_start;
}
void STnode::setInLabelStart(long start)
{
this->_in_label_start = start;
}
long STnode::getInLabelEnd()
{
return this->_in_label_end;
}
void STnode::setInLabelEnd(long end)
{
this->_in_label_end = end;
}
long STnode::getRefStrId()
{
return this->_str_id;
}
long STnode::getInLabelLength()
{
return (this->_in_label_end - this->_in_label_start + 1);
}
void STnode::addOccPos(long str_id, long pos)
{
OccPos* p = new OccPos(str_id, pos, NULL);
OccPos* cur_p = this->_occ_positions;
if( cur_p == NULL )
{
this->_occ_positions = p;
}
else
{
while( cur_p->_next_pos!=NULL )
{
cur_p = cur_p->_next_pos;
}
cur_p->_next_pos = p;
}
}
void STnode::setOccPos(OccPos* p)
{
this->_occ_positions = p;
}
OccPos* STnode::getOccPos()
{
return this->_occ_positions;
}
long STnode::getOccPosNum()
{
OccPos* cur_p = this->_occ_positions;
long pos_num=0;
while( cur_p!=NULL )
{
pos_num++;
cur_p = cur_p->_next_pos;
}
return pos_num;
}
void STnode::printOccPos()
{
OccPos* cur_p = this->_occ_positions;
while( cur_p!=NULL )
{
cout<<"("<<cur_p->_str_id<<","<<cur_p->_occ_pos<<") ";
cur_p = cur_p->_next_pos;
}
}
void STnode::getSubtreeLeaves(vector<STnode*>& leaves)
{
STnode* ch_node = this->getChildren(); //Point to the first child
if( ch_node == NULL ) //this is a leaf
leaves.push_back(this);
while(ch_node != NULL)
{
if( ch_node->isLeaf() )
leaves.push_back(ch_node); //record this leaf
else
ch_node->getSubtreeLeaves(leaves); //find this node's leaves
ch_node = ch_node->getRightSibling(); //Check the next sibling
}
}
void STnode::getSubtreeOccs(vector<OccPos*>& occs)
{
STnode* ch_node = this->getChildren(); //Point to the first child
OccPos* cur_p;
if( ch_node == NULL ) //this is a leaf
{
// add all starting positions to occs
OccPos* cur_p = this->_occ_positions;
while( cur_p != NULL )
{
occs.push_back(cur_p);
cur_p = cur_p->_next_pos;
}
}
while(ch_node != NULL)
{
if( ch_node->isLeaf() )
{
cur_p = ch_node->_occ_positions;
while( cur_p!=NULL )
{
occs.push_back(cur_p);
cur_p = cur_p->_next_pos;
}
}
else
{
ch_node->getSubtreeOccs(occs); //find this node's leaves
}
ch_node = ch_node->getRightSibling(); //Check the next sibling
}
}
void STnode::updateSubtreeOccNum()
{
vector<OccPos*> occs;
this->getSubtreeOccs(occs);
this->_occs_num = occs.size();
//cout << this->_occs_num << endl;
}
long STnode::getOccsNum() const {
return _occs_num;
}