17
17
******************************************************************************/
18
18
19
19
#include < string>
20
+ // #include <tr1/unordered_map>
20
21
#include < limits>
21
22
#include < map>
22
23
#include < vector>
@@ -31,12 +32,13 @@ using std::cout;
31
32
using std::endl;
32
33
using std::out_of_range;
33
34
using std::ostream;
35
+ // typedef tr1::unordered_map map;
34
36
35
37
// TODO: upgrade it to process trace. Rule: char-->elem string-->elem_list
36
38
class SuffixTree {
37
39
public:
38
40
// active point is initialized as (root, None, 0), remainder initialized as 1
39
- SuffixTree (string str):test_str(str), pos( 0 ), root(test_str), active_point(&root, 0 , 0 ), remainder(0 ), ls() {}
41
+ SuffixTree (string str):test_str(str), root(test_str), active_point(&root, 0 , 0 ), remainder( 0 ), pos (0 ), ls() {}
40
42
int construct (void );
41
43
42
44
// return -1 if no such sub exist, return the beginning postion of this substring in thr original string if it exist
@@ -54,7 +56,6 @@ class SuffixTree {
54
56
int edge_len = -1 ;
55
57
bool flag = true ;
56
58
57
-
58
59
while (flag) {
59
60
if (edge == NULL ) {
60
61
edge = node->find_edge (*result);
@@ -96,27 +97,25 @@ class SuffixTree {
96
97
97
98
struct Edge {
98
99
// the begin and end pos of this edge, note that INT_MAX stands for #(the changing end pos of this entire string)
99
- int begin, end;
100
+ unsigned int begin, end;
100
101
// Is there a better way to find test_str?
101
102
string& test_node_str;
102
103
103
104
Node * endpoint;
104
105
105
- Edge (int b, int e, string& str):
106
- test_node_str (str) {
106
+ Edge (unsigned int b, unsigned int e, string& str): test_node_str(str) {
107
107
begin = b;
108
108
end = e;
109
109
endpoint = NULL ;
110
110
// std::cout << "Edge initialized" << std::endl;
111
111
}
112
112
113
- void change_edge (int b, int e) {
113
+ void change_edge (unsigned int b, unsigned int e) {
114
114
begin = b;
115
115
end = e;
116
116
}
117
117
118
118
int length (void ) {
119
-
120
119
if (end > test_node_str.size ())
121
120
return test_node_str.size () - begin;
122
121
else
@@ -128,7 +127,7 @@ class SuffixTree {
128
127
return me.begin < other.begin ;
129
128
}
130
129
131
- char operator [](int i) {
130
+ char operator [](unsigned int i) {
132
131
i += begin;
133
132
if (i > end)
134
133
throw out_of_range (" Edge [] out of range." );
@@ -137,12 +136,12 @@ class SuffixTree {
137
136
}
138
137
139
138
friend ostream& operator <<(ostream& os, Edge& edge) {
140
- int end = edge.test_node_str .size ()-1 ;
139
+ unsigned int end = edge.test_node_str .size ()-1 ;
141
140
if (end >= edge.end )
142
141
end = edge.end ;
143
142
144
143
char c;
145
- for (int i=edge.begin ; i<=end; i++) {
144
+ for (unsigned int i=edge.begin ; i<=end; i++) {
146
145
c = edge.test_node_str [i];
147
146
os << c;
148
147
}
@@ -167,8 +166,10 @@ class SuffixTree {
167
166
168
167
friend class LinkState ;
169
168
170
- Node (string& str) :
171
- test_node_str (str), suffix_link(NULL ) { edges.clear (); findedges.clear (); }
169
+ Node (string& str) : test_node_str(str), suffix_link(NULL ) {
170
+ edges.clear ();
171
+ findedges.clear ();
172
+ }
172
173
173
174
void add_edge (Edge* edge) {
174
175
if (edge->endpoint == NULL )
@@ -195,8 +196,7 @@ class SuffixTree {
195
196
}
196
197
197
198
// find edge by the first char
198
- Edge* find_edge (char c)
199
- {
199
+ Edge* find_edge (char c) {
200
200
// cout << "finding edge";
201
201
map<char , Edge*>::iterator iter = findedges.find (c);
202
202
// cout << "founded?" << endl;
@@ -228,14 +228,14 @@ class SuffixTree {
228
228
};
229
229
// typedef struct Node Node;
230
230
231
- class ActivePoint {
231
+ class ActivePoint {
232
232
public:
233
233
Node* active_node;
234
234
char active_edge;
235
235
int active_length;
236
236
237
- ActivePoint (Node* node, char edge, int length):
238
- active_node (node), active_edge(edge), active_length(length) { std::cout << " ActivePoint initialized" << std::endl; }
237
+ ActivePoint (Node* node, char edge, int length): active_node(node),
238
+ active_edge (edge), active_length(length) { std::cout << " ActivePoint initialized" << std::endl; }
239
239
};
240
240
241
241
Node root;
@@ -253,7 +253,7 @@ class SuffixTree {
253
253
// how many suffixes is to be inserted?
254
254
int remainder;
255
255
// how many characters inserted?
256
- int pos;
256
+ unsigned int pos;
257
257
char get_ele (int i) { return test_str[i]; }
258
258
// insert a char from pos to suffix tree
259
259
int insert ();
0 commit comments